Skip to content

Commit

Permalink
NEVERHOOD: Change graphic resource draw method to get a Surface inste…
Browse files Browse the repository at this point in the history
…ad of separate pixels/pitch

- Merge SpriteResource::load and load2
  • Loading branch information
johndoe123 authored and wjp committed May 8, 2013
1 parent 5749781 commit f945448
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 38 deletions.
12 changes: 6 additions & 6 deletions engines/neverhood/graphics.cpp
Expand Up @@ -71,7 +71,7 @@ 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);
spriteResource.draw(_surface, false, false);
++_version;
}
}
Expand All @@ -85,7 +85,7 @@ void BaseSurface::drawSpriteResourceEx(SpriteResource &spriteResource, bool flip
_drawRect.height = height;
if (_surface) {
clear();
spriteResource.draw((byte*)_surface->pixels, _surface->pitch, flipX, flipY);
spriteResource.draw(_surface, flipX, flipY);
++_version;
}
}
Expand All @@ -99,15 +99,15 @@ void BaseSurface::drawAnimResource(AnimResource &animResource, uint frameIndex,
if (_surface) {
clear();
if (frameIndex < animResource.getFrameCount()) {
animResource.draw(frameIndex, (byte*)_surface->pixels, _surface->pitch, flipX, flipY);
animResource.draw(frameIndex, _surface, flipX, flipY);
++_version;
}
}
}

void BaseSurface::drawMouseCursorResource(MouseCursorResource &mouseCursorResource, int frameNum) {
if (frameNum < 3) {
mouseCursorResource.draw(frameNum, (byte*)_surface->pixels, _surface->pitch);
mouseCursorResource.draw(frameNum, _surface);
++_version;
}
}
Expand Down Expand Up @@ -156,7 +156,7 @@ FontSurface::FontSurface(NeverhoodEngine *vm, uint32 fileHash, uint charsPerRow,
_firstChar(firstChar), _charWidth(charWidth), _charHeight(charHeight), _tracking(NULL) {

SpriteResource fontSpriteResource(_vm);
fontSpriteResource.load2(fileHash);
fontSpriteResource.load(fileHash, true);
drawSpriteResourceEx(fontSpriteResource, false, false, 0, 0);
}

Expand Down Expand Up @@ -200,7 +200,7 @@ FontSurface *FontSurface::createFontSurface(NeverhoodEngine *vm, uint32 fileHash
uint16 charWidth = fontData.getPoint(calcHash("meCharWidth")).x;
uint16 charHeight = fontData.getPoint(calcHash("meCharHeight")).x;
NPointArray *tracking = fontData.getPointArray(calcHash("meTracking"));
fontSprite.load2(fileHash);
fontSprite.load(fileHash, true);
fontSurface = new FontSurface(vm, tracking, 16, numRows, firstChar, charWidth, charHeight);
fontSurface->drawSpriteResourceEx(fontSprite, false, false, 0, 0);
return fontSurface;
Expand Down
6 changes: 3 additions & 3 deletions engines/neverhood/menumodule.cpp
Expand Up @@ -540,7 +540,7 @@ void TextEditWidget::onClick() {
void TextEditWidget::addSprite() {
SpriteResource cursorSpriteResource(_vm);

_spriteResource.load2(_fileHash);
_spriteResource.load(_fileHash, true);
createSurface(_baseSurfacePriority, _spriteResource.getDimensions().width, _spriteResource.getDimensions().height);
refreshPosition();
_parentScene->addSprite(this);
Expand All @@ -550,7 +550,7 @@ void TextEditWidget::addSprite() {
0, _parentScene, _baseObjectPriority + 1, _baseSurfacePriority + 1,
(const byte*)_entryString.c_str(), _entryString.size(), _surface, _x, _y, _fontSurface);
_textLabelWidget->addSprite();
cursorSpriteResource.load2(_cursorFileHash);
cursorSpriteResource.load(_cursorFileHash, true);
_cursorSurface = new BaseSurface(_vm, 0, cursorSpriteResource.getDimensions().width, cursorSpriteResource.getDimensions().height);
_cursorSurface->drawSpriteResourceEx(cursorSpriteResource, false, false, cursorSpriteResource.getDimensions().width, cursorSpriteResource.getDimensions().height);
_cursorSurface->setVisible(true);
Expand Down Expand Up @@ -704,7 +704,7 @@ void SavegameListBox::onClick() {
}

void SavegameListBox::addSprite() {
_spriteResource.load2(_bgFileHash);
_spriteResource.load(_bgFileHash, true);
createSurface(_baseSurfacePriority, _spriteResource.getDimensions().width, _spriteResource.getDimensions().height);
refreshPosition();
_parentScene->addSprite(this);
Expand Down
8 changes: 5 additions & 3 deletions engines/neverhood/module2200.cpp
Expand Up @@ -2139,15 +2139,17 @@ Scene2208::Scene2208(NeverhoodEngine *vm, Module *parentModule, int which)
_fontSurface = FontSurface::createFontSurface(_vm, 0x0800090C);

_backgroundSurface = new BaseSurface(_vm, 0, 640, 480);
spriteResource.load2(0x08100289);
spriteResource.load(0x08100289, true);
_backgroundSurface->drawSpriteResourceEx(spriteResource, false, false, 0, 0);

_topBackgroundSurface = new BaseSurface(_vm, 0, 640, 192);
spriteResource.load2(!getGlobalVar(V_COLUMN_BACK_NAME) ? kScene2208FileHashes1[getGlobalVar(V_CLICKED_COLUMN_INDEX) % 6] : getGlobalVar(V_COLUMN_BACK_NAME));
spriteResource.load(!getGlobalVar(V_COLUMN_BACK_NAME)
? kScene2208FileHashes1[getGlobalVar(V_CLICKED_COLUMN_INDEX) % 6]
: getGlobalVar(V_COLUMN_BACK_NAME), true);
_topBackgroundSurface->drawSpriteResourceEx(spriteResource, false, false, 0, 0);

_bottomBackgroundSurface = new BaseSurface(_vm, 0, 640, 192);
spriteResource.load2(kScene2208FileHashes2[getGlobalVar(V_CLICKED_COLUMN_INDEX) % 6]);
spriteResource.load(kScene2208FileHashes2[getGlobalVar(V_CLICKED_COLUMN_INDEX) % 6], true);
_bottomBackgroundSurface->drawSpriteResourceEx(spriteResource, false, false, 0, 0);

SetUpdateHandler(&Scene2208::update);
Expand Down
32 changes: 13 additions & 19 deletions engines/neverhood/resource.cpp
Expand Up @@ -37,36 +37,26 @@ SpriteResource::~SpriteResource() {
unload();
}

void SpriteResource::draw(byte *dest, int destPitch, bool flipX, bool flipY) {
void SpriteResource::draw(Graphics::Surface *destSurface, bool flipX, bool flipY) {
if (_pixels) {
byte *dest = (byte*)destSurface->pixels;
const int destPitch = destSurface->pitch;
if (_rle)
unpackSpriteRle(_pixels, _dimensions.width, _dimensions.height, dest, destPitch, flipX, flipY);
else
unpackSpriteNormal(_pixels, _dimensions.width, _dimensions.height, dest, destPitch, flipX, flipY);
}
}

bool SpriteResource::load(uint32 fileHash) {
bool SpriteResource::load(uint32 fileHash, bool doLoadPosition) {
debug(2, "SpriteResource::load(%08X)", fileHash);
// TODO: Later merge with load2 and make the mode a parameter
unload();
_vm->_res->queryResource(fileHash, _resourceHandle);
if (_resourceHandle.isValid() && _resourceHandle.type() == kResTypeBitmap) {
_vm->_res->loadResource(_resourceHandle);
const byte *spriteData = _resourceHandle.data();
parseBitmapResource(spriteData, &_rle, &_dimensions, NULL, NULL, &_pixels);
}
return _pixels != NULL;
}

bool SpriteResource::load2(uint32 fileHash) {
debug(2, "SpriteResource::load2(%08X)", fileHash);
unload();
_vm->_res->queryResource(fileHash, _resourceHandle);
if (_resourceHandle.isValid() && _resourceHandle.type() == kResTypeBitmap) {
_vm->_res->loadResource(_resourceHandle);
const byte *spriteData = _resourceHandle.data();
parseBitmapResource(spriteData, &_rle, &_dimensions, &_position, NULL, &_pixels);
NPoint *position = doLoadPosition ? &_position : NULL;
parseBitmapResource(spriteData, &_rle, &_dimensions, position, NULL, &_pixels);
}
return _pixels != NULL;
}
Expand Down Expand Up @@ -128,8 +118,10 @@ AnimResource::~AnimResource() {
unload();
}

void AnimResource::draw(uint frameIndex, byte *dest, int destPitch, bool flipX, bool flipY) {
void AnimResource::draw(uint frameIndex, Graphics::Surface *destSurface, bool flipX, bool flipY) {
const AnimFrameInfo frameInfo = _frames[frameIndex];
byte *dest = (byte*)destSurface->pixels;
const int destPitch = destSurface->pitch;
_currSpriteData = _spriteData + frameInfo.spriteDataOffs;
_width = frameInfo.drawOffset.width;
_height = frameInfo.drawOffset.height;
Expand Down Expand Up @@ -305,10 +297,12 @@ NDrawRect& MouseCursorResource::getRect() {
return _rect;
}

void MouseCursorResource::draw(int frameNum, byte *dest, int destPitch) {
void MouseCursorResource::draw(int frameNum, Graphics::Surface *destSurface) {
if (_cursorSprite.getPixels()) {
int sourcePitch = (_cursorSprite.getDimensions().width + 3) & 0xFFFC; // 4 byte alignment
const int sourcePitch = (_cursorSprite.getDimensions().width + 3) & 0xFFFC; // 4 byte alignment
const int destPitch = destSurface->pitch;
const byte *source = _cursorSprite.getPixels() + _cursorNum * (sourcePitch * 32) + frameNum * 32;
byte *dest = (byte*)destSurface->pixels;
for (int16 yc = 0; yc < 32; yc++) {
memcpy(dest, source, 32);
source += sourcePitch;
Expand Down
9 changes: 4 additions & 5 deletions engines/neverhood/resource.h
Expand Up @@ -46,9 +46,8 @@ class SpriteResource {
public:
SpriteResource(NeverhoodEngine *vm);
~SpriteResource();
void draw(byte *dest, int destPitch, bool flipX, bool flipY);
bool load(uint32 fileHash);
bool load2(uint32 fileHash);
void draw(Graphics::Surface *destSurface, bool flipX, bool flipY);
bool load(uint32 fileHash, bool doLoadPosition = false);
void unload();
const NDimensions& getDimensions() { return _dimensions; }
NPoint& getPosition() { return _position; }
Expand Down Expand Up @@ -90,7 +89,7 @@ class AnimResource {
public:
AnimResource(NeverhoodEngine *vm);
~AnimResource();
void draw(uint frameIndex, byte *dest, int destPitch, bool flipX, bool flipY);
void draw(uint frameIndex, Graphics::Surface *destSurface, bool flipX, bool flipY);
bool load(uint32 fileHash);
void unload();
void clear();
Expand Down Expand Up @@ -120,7 +119,7 @@ class MouseCursorResource {
void load(uint32 fileHash);
void unload();
NDrawRect& getRect();
void draw(int frameNum, byte *dest, int destPitch);
void draw(int frameNum, Graphics::Surface *destSurface);
int getCursorNum() const { return _cursorNum; }
void setCursorNum(int cursorNum) { _cursorNum = cursorNum; }
protected:
Expand Down
4 changes: 2 additions & 2 deletions engines/neverhood/sprite.cpp
Expand Up @@ -116,7 +116,7 @@ StaticSprite::StaticSprite(NeverhoodEngine *vm, int objectPriority)
StaticSprite::StaticSprite(NeverhoodEngine *vm, uint32 fileHash, int surfacePriority, int16 x, int16 y)
: Sprite(vm, 0), _spriteResource(vm) {

_spriteResource.load2(fileHash);
_spriteResource.load(fileHash, true);
createSurface(surfacePriority, _spriteResource.getDimensions().width, _spriteResource.getDimensions().height);
_x = x == kDefPosition ? _spriteResource.getPosition().x : x;
_y = y == kDefPosition ? _spriteResource.getPosition().y : y;
Expand All @@ -126,7 +126,7 @@ StaticSprite::StaticSprite(NeverhoodEngine *vm, uint32 fileHash, int surfacePrio
}

void StaticSprite::loadSprite(uint32 fileHash, uint flags, int surfacePriority, int16 x, int16 y) {
_spriteResource.load2(fileHash);
_spriteResource.load(fileHash, true);
if (!_surface)
createSurface(surfacePriority, _spriteResource.getDimensions().width, _spriteResource.getDimensions().height);
if (flags & kSLFDefDrawOffset)
Expand Down

0 comments on commit f945448

Please sign in to comment.