Skip to content

Commit

Permalink
NEVERHOOD: Implement an experimental "render queue" to only draw and …
Browse files Browse the repository at this point in the history
…update what's actually changed; it works surprisingly well so far and only needs a few more changes here and there
  • Loading branch information
johndoe123 authored and wjp committed May 8, 2013
1 parent 692edf4 commit c09c0b8
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 29 deletions.
7 changes: 4 additions & 3 deletions engines/neverhood/gamemodule.cpp
Expand Up @@ -358,12 +358,13 @@ void GameModule::startup() {
setGlobalVar(V_CREATURE_ANGRY, 1);
setGlobalVar(V_RADIO_ENABLED, 1);
setGlobalVar(V_TNT_DUMMY_BUILT, 1);
setGlobalVar(V_FLYTRAP_RING_DOOR, 1);
// <<<DEBUG

#if 1
_vm->gameState().which = 1;
_vm->gameState().sceneNum = 0;
createModule(2500, -1);
_vm->gameState().which = 0;
_vm->gameState().sceneNum = 3;
createModule(1300, -1);
#endif
#if 0
_vm->gameState().sceneNum = 0;
Expand Down
16 changes: 11 additions & 5 deletions engines/neverhood/graphics.cpp
Expand Up @@ -28,7 +28,7 @@ namespace Neverhood {

BaseSurface::BaseSurface(NeverhoodEngine *vm, int priority, int16 width, int16 height)
: _vm(vm), _priority(priority), _visible(true), _transparent(true),
_clipRects(NULL), _clipRectsCount(0) {
_clipRects(NULL), _clipRectsCount(0), _version(0) {

_drawRect.x = 0;
_drawRect.y = 0;
Expand All @@ -54,11 +54,11 @@ BaseSurface::~BaseSurface() {
void BaseSurface::draw() {
if (_surface && _visible && _drawRect.width > 0 && _drawRect.height > 0) {
if (_clipRects && _clipRectsCount) {
_vm->_screen->drawSurfaceClipRects(_surface, _drawRect, _clipRects, _clipRectsCount, _transparent);
_vm->_screen->drawSurfaceClipRects(_surface, _drawRect, _clipRects, _clipRectsCount, _transparent, _version);
} else if (_sysRect.x == 0 && _sysRect.y == 0) {
_vm->_screen->drawSurface2(_surface, _drawRect, _clipRect, _transparent);
_vm->_screen->drawSurface2(_surface, _drawRect, _clipRect, _transparent, _version);
} else {
_vm->_screen->drawUnk(_surface, _drawRect, _sysRect, _clipRect, _transparent);
_vm->_screen->drawUnk(_surface, _drawRect, _sysRect, _clipRect, _transparent, _version);
}
}
}
Expand All @@ -69,13 +69,15 @@ void BaseSurface::addDirtyRect() {

void BaseSurface::clear() {
_surface->fillRect(Common::Rect(0, 0, _surface->w, _surface->h), 0);
++_version;
}

void BaseSurface::drawSpriteResource(SpriteResource &spriteResource) {
if (spriteResource.getDimensions().width <= _drawRect.width &&
spriteResource.getDimensions().height <= _drawRect.height) {
clear();
spriteResource.draw((byte*)_surface->pixels, _surface->pitch, false, false);
++_version;
}
}

Expand All @@ -89,6 +91,7 @@ void BaseSurface::drawSpriteResourceEx(SpriteResource &spriteResource, bool flip
if (_surface) {
clear();
spriteResource.draw((byte*)_surface->pixels, _surface->pitch, flipX, flipY);
++_version;
}
}
}
Expand All @@ -102,13 +105,15 @@ void BaseSurface::drawAnimResource(AnimResource &animResource, uint frameIndex,
clear();
if (frameIndex < animResource.getFrameCount()) {
animResource.draw(frameIndex, (byte*)_surface->pixels, _surface->pitch, flipX, flipY);
++_version;
}
}
}

void BaseSurface::drawMouseCursorResource(MouseCursorResource &mouseCursorResource, int frameNum) {
if (frameNum < 3) {
mouseCursorResource.draw(frameNum, (byte*)_surface->pixels, _surface->pitch);
++_version;
}
}

Expand All @@ -132,6 +137,7 @@ void BaseSurface::copyFrom(Graphics::Surface *sourceSurface, int16 x, int16 y, N
dest += _surface->pitch;
}
}
++_version;
}

// ShadowSurface
Expand All @@ -143,7 +149,7 @@ ShadowSurface::ShadowSurface(NeverhoodEngine *vm, int priority, int16 width, int

void ShadowSurface::draw() {
if (_surface && _visible && _drawRect.width > 0 && _drawRect.height > 0) {
_vm->_screen->drawSurface2(_surface, _drawRect, _clipRect, _transparent, _shadowSurface->getSurface());
_vm->_screen->drawSurface2(_surface, _drawRect, _clipRect, _transparent, _version, _shadowSurface->getSurface());
}
}

Expand Down
1 change: 1 addition & 0 deletions engines/neverhood/graphics.h
Expand Up @@ -118,6 +118,7 @@ class BaseSurface {
NRect *_clipRects;
uint _clipRectsCount;
bool _transparent;
byte _version;
};

class ShadowSurface : public BaseSurface {
Expand Down
4 changes: 3 additions & 1 deletion engines/neverhood/neverhood.cpp
Expand Up @@ -153,12 +153,14 @@ Common::Error NeverhoodEngine::run() {
if (_system->getMillis() >= nextFrameTime) {
_gameModule->handleUpdate();
_gameModule->draw();
_screen->update();
nextFrameTime = _screen->getNextFrameTime();
};

_soundMan->update();
_audioResourceMan->update();
_screen->update();
//_screen->update();
_system->updateScreen();
_system->delayMillis(10);

debug(0, "---------------------------------------");
Expand Down
162 changes: 147 additions & 15 deletions engines/neverhood/screen.cpp
Expand Up @@ -33,19 +33,80 @@ Screen::Screen(NeverhoodEngine *vm)
_backScreen = new Graphics::Surface();
_backScreen->create(640, 480, Graphics::PixelFormat::createFormatCLUT8());

_renderQueue = new RenderQueue();
_prevRenderQueue = new RenderQueue();

}

Screen::~Screen() {
delete _renderQueue;
delete _prevRenderQueue;
_backScreen->free();
delete _backScreen;
}

void Screen::update() {
_ticks = _vm->_system->getMillis();
updatePalette();
// TODO: Implement actual code
_vm->_system->copyRectToScreen((const byte*)_backScreen->pixels, _backScreen->pitch, 0, 0, 640, 480);
_vm->_system->updateScreen();

if (_fullRefresh) {
// NOTE When playing a fullscreen/doubled Smacker video usually a full screen refresh is needed
_vm->_system->copyRectToScreen((const byte*)_backScreen->pixels, _backScreen->pitch, 0, 0, 640, 480);
_fullRefresh = false;
return;
}

// NOTE This is more or less experimental code for a smart "render queue".
// It works well so far but needs some optimizing, e.g. reducing overdraw.
// Maybe I'll use my microtiles code from the Toltecs engine.
// Also better move this to a separate method or even class.

Common::Array<Common::Rect> updateRects;

for (RenderQueue::iterator jt = _prevRenderQueue->begin(); jt != _prevRenderQueue->end(); ++jt)
(*jt)._refresh = true;

for (RenderQueue::iterator it = _renderQueue->begin(); it != _renderQueue->end(); ++it) {
RenderItem &renderItem = (*it);
renderItem._refresh = true;
for (RenderQueue::iterator jt = _prevRenderQueue->begin(); jt != _prevRenderQueue->end(); ++jt) {
RenderItem &prevRenderItem = (*jt);
if (prevRenderItem == renderItem) {
prevRenderItem._refresh = false;
renderItem._refresh = false;
}
}
}

for (RenderQueue::iterator jt = _prevRenderQueue->begin(); jt != _prevRenderQueue->end(); ++jt) {
RenderItem &prevRenderItem = (*jt);
if (prevRenderItem._refresh)
updateRects.push_back(Common::Rect(prevRenderItem._destX, prevRenderItem._destY, prevRenderItem._destX + prevRenderItem._width, prevRenderItem._destY + prevRenderItem._height));
}

for (RenderQueue::iterator it = _renderQueue->begin(); it != _renderQueue->end(); ++it) {
RenderItem &renderItem = (*it);
if (renderItem._refresh)
updateRects.push_back(Common::Rect(renderItem._destX, renderItem._destY, renderItem._destX + renderItem._width, renderItem._destY + renderItem._height));
}

/*
for (Common::Array<Common::Rect>::iterator ri = updateRects.begin(); ri != updateRects.end(); ++ri)
debug("## (%d, %d, %d, %d)", (*ri).left, (*ri).top, (*ri).right, (*ri).bottom);
*/

for (RenderQueue::iterator it = _renderQueue->begin(); it != _renderQueue->end(); ++it) {
RenderItem &renderItem = (*it);
for (Common::Array<Common::Rect>::iterator ri = updateRects.begin(); ri != updateRects.end(); ++ri)
blitRenderItem(renderItem, *ri);
}

SWAP(_renderQueue, _prevRenderQueue);
_renderQueue->clear();

for (Common::Array<Common::Rect>::iterator ri = updateRects.begin(); ri != updateRects.end(); ++ri)
_vm->_system->copyRectToScreen((const byte*)_backScreen->getBasePtr((*ri).left, (*ri).top), _backScreen->pitch, (*ri).left, (*ri).top, (*ri).width(), (*ri).height());

}

uint32 Screen::getNextFrameTime() {
Expand Down Expand Up @@ -99,7 +160,7 @@ void Screen::clear() {
memset(_backScreen->pixels, 0, _backScreen->pitch * _backScreen->h);
}

void Screen::drawSurface2(const Graphics::Surface *surface, NDrawRect &drawRect, NRect &clipRect, bool transparent,
void Screen::drawSurface2(const Graphics::Surface *surface, NDrawRect &drawRect, NRect &clipRect, bool transparent, byte version,
const Graphics::Surface *shadowSurface) {

int16 destX, destY;
Expand Down Expand Up @@ -133,7 +194,7 @@ void Screen::drawSurface2(const Graphics::Surface *surface, NDrawRect &drawRect,

//debug(2, "draw: x = %d; y = %d; (%d, %d, %d, %d)", destX, destY, ddRect.x1, ddRect.y1, ddRect.x2, ddRect.y2);

blit(surface, destX, destY, ddRect, transparent, shadowSurface);
queueBlit(surface, destX, destY, ddRect, transparent, version, shadowSurface);

// Useful for debugging
//_backScreen->frameRect(Common::Rect(clipRect.x1, clipRect.y1, clipRect.x2, clipRect.y2), 250);
Expand All @@ -142,7 +203,7 @@ void Screen::drawSurface2(const Graphics::Surface *surface, NDrawRect &drawRect,

}

void Screen::drawSurface3(const Graphics::Surface *surface, int16 x, int16 y, NDrawRect &drawRect, NRect &clipRect, bool transparent) {
void Screen::drawSurface3(const Graphics::Surface *surface, int16 x, int16 y, NDrawRect &drawRect, NRect &clipRect, bool transparent, byte version) {

int16 destX, destY;
NRect ddRect;
Expand Down Expand Up @@ -173,7 +234,7 @@ void Screen::drawSurface3(const Graphics::Surface *surface, int16 x, int16 y, ND
ddRect.y1 = drawRect.y;
}

blit(surface, destX, destY, ddRect, transparent);
queueBlit(surface, destX, destY, ddRect, transparent, version);

}

Expand Down Expand Up @@ -220,7 +281,7 @@ void Screen::drawDoubleSurface2(const Graphics::Surface *surface, NDrawRect &dra

const byte *source = (const byte*)surface->getBasePtr(0, 0);
byte *dest = (byte*)_backScreen->getBasePtr(drawRect.x, drawRect.y);

for (int16 yc = 0; yc < surface->h; yc++) {
byte *row = dest;
for (int16 xc = 0; xc < surface->w; xc++) {
Expand All @@ -231,10 +292,12 @@ void Screen::drawDoubleSurface2(const Graphics::Surface *surface, NDrawRect &dra
dest += _backScreen->pitch;
dest += _backScreen->pitch;
}

_fullRefresh = true; // See Screen::update

}

void Screen::drawUnk(const Graphics::Surface *surface, NDrawRect &drawRect, NDrawRect &sysRect, NRect &clipRect, bool transparent) {
void Screen::drawUnk(const Graphics::Surface *surface, NDrawRect &drawRect, NDrawRect &sysRect, NRect &clipRect, bool transparent, byte version) {

int16 x, y;
bool xflag, yflag;
Expand Down Expand Up @@ -271,7 +334,7 @@ void Screen::drawUnk(const Graphics::Surface *surface, NDrawRect &drawRect, NDra
newDrawRect.height = drawRect.height;
}

drawSurface3(surface, drawRect.x, drawRect.y, newDrawRect, clipRect, transparent);
drawSurface3(surface, drawRect.x, drawRect.y, newDrawRect, clipRect, transparent, version);

if (!xflag) {
newDrawRect.x = 0;
Expand All @@ -280,7 +343,7 @@ void Screen::drawUnk(const Graphics::Surface *surface, NDrawRect &drawRect, NDra
newDrawRect.height = sysRect.height - y;
if (drawRect.height < newDrawRect.height)
newDrawRect.height = drawRect.height;
drawSurface3(surface, sysRect.width + drawRect.x - x, drawRect.y, newDrawRect, clipRect, transparent);
drawSurface3(surface, sysRect.width + drawRect.x - x, drawRect.y, newDrawRect, clipRect, transparent, version);
}

if (!yflag) {
Expand All @@ -290,23 +353,92 @@ void Screen::drawUnk(const Graphics::Surface *surface, NDrawRect &drawRect, NDra
newDrawRect.height = y + drawRect.height - sysRect.height;
if (drawRect.width < newDrawRect.width)
newDrawRect.width = drawRect.width;
drawSurface3(surface, drawRect.x, sysRect.height + drawRect.y - y, newDrawRect, clipRect, transparent);
drawSurface3(surface, drawRect.x, sysRect.height + drawRect.y - y, newDrawRect, clipRect, transparent, version);
}

if (!xflag && !yflag) {
newDrawRect.x = 0;
newDrawRect.y = 0;
newDrawRect.width = x + drawRect.width - sysRect.width;
newDrawRect.height = y + drawRect.height - sysRect.height;
drawSurface3(surface, sysRect.width + drawRect.x - x, sysRect.height + drawRect.y - y, newDrawRect, clipRect, transparent);
drawSurface3(surface, sysRect.width + drawRect.x - x, sysRect.height + drawRect.y - y, newDrawRect, clipRect, transparent, version);
}

}

void Screen::drawSurfaceClipRects(const Graphics::Surface *surface, NDrawRect &drawRect, NRect *clipRects, uint clipRectsCount, bool transparent) {
void Screen::drawSurfaceClipRects(const Graphics::Surface *surface, NDrawRect &drawRect, NRect *clipRects, uint clipRectsCount, bool transparent, byte version) {
NDrawRect clipDrawRect(0, 0, drawRect.width, drawRect.height);
for (uint i = 0; i < clipRectsCount; i++)
drawSurface3(surface, drawRect.x, drawRect.y, clipDrawRect, clipRects[i], transparent);
drawSurface3(surface, drawRect.x, drawRect.y, clipDrawRect, clipRects[i], transparent, version);
}

void Screen::queueBlit(const Graphics::Surface *surface, int16 destX, int16 destY, NRect &ddRect, bool transparent, byte version,
const Graphics::Surface *shadowSurface) {

int width = ddRect.x2 - ddRect.x1;
int height = ddRect.y2 - ddRect.y1;

if (width <= 0 || height <= 0)
return;

RenderItem renderItem;
renderItem._surface = surface;
renderItem._shadowSurface = shadowSurface;
renderItem._destX = destX;
renderItem._destY = destY;
renderItem._srcX = ddRect.x1;
renderItem._srcY = ddRect.y1;
renderItem._width = width;
renderItem._height = height;
renderItem._transparent = transparent;
renderItem._version = version;
_renderQueue->push_back(renderItem);

}

void Screen::blitRenderItem(const RenderItem &renderItem, const Common::Rect &clipRect) {

const Graphics::Surface *surface = renderItem._surface;
const Graphics::Surface *shadowSurface = renderItem._shadowSurface;
const int16 x0 = MAX<int16>(clipRect.left, renderItem._destX);
const int16 y0 = MAX<int16>(clipRect.top, renderItem._destY);
const int16 x1 = MIN<int16>(clipRect.right, renderItem._destX + renderItem._width);
const int16 y1 = MIN<int16>(clipRect.bottom, renderItem._destY + renderItem._height);
int16 width = x1 - x0;
int16 height = y1 - y0;

if (width < 0 || height < 0)
return;

const byte *source = (const byte*)surface->getBasePtr(renderItem._srcX + x0 - renderItem._destX, renderItem._srcY + y0 - renderItem._destY);
byte *dest = (byte*)_backScreen->getBasePtr(x0, y0);

if (shadowSurface) {
const byte *shadowSource = (const byte*)shadowSurface->getBasePtr(x0, y0);
while (height--) {
for (int xc = 0; xc < width; xc++)
if (source[xc] != 0)
dest[xc] = shadowSource[xc];
source += surface->pitch;
shadowSource += shadowSurface->pitch;
dest += _backScreen->pitch;
}
} else if (!renderItem._transparent) {
while (height--) {
memcpy(dest, source, width);
source += surface->pitch;
dest += _backScreen->pitch;
}
} else {
while (height--) {
for (int xc = 0; xc < width; xc++)
if (source[xc] != 0)
dest[xc] = source[xc];
source += surface->pitch;
dest += _backScreen->pitch;
}
}

}

} // End of namespace Neverhood

0 comments on commit c09c0b8

Please sign in to comment.