ASYLUM: add a simple resource viewer

This commit is contained in:
alxpnv 2021-09-28 14:59:45 +03:00
parent 3c71f32950
commit dad4565809
5 changed files with 314 additions and 40 deletions

View File

@ -253,7 +253,7 @@ static const int32 itemIndices[][16] = {
{69, 70, 78}
};
Console::Console(AsylumEngine *engine) : _vm(engine), _insertDisc(engine) {
Console::Console(AsylumEngine *engine) : _vm(engine), _insertDisc(engine), _resViewer(engine) {
// Commands
registerCmd("help", WRAP_METHOD(Console, cmdHelp));
@ -287,7 +287,7 @@ Console::Console(AsylumEngine *engine) : _vm(engine), _insertDisc(engine) {
registerCmd("throw", WRAP_METHOD(Console, cmdRemoveFromInventory));
registerCmd("palette", WRAP_METHOD(Console, cmdSetPalette));
registerCmd("draw", WRAP_METHOD(Console, cmdDrawResource));
registerCmd("view", WRAP_METHOD(Console, cmdViewResource));
registerCmd("toggle_flag", WRAP_METHOD(Console, cmdToggleFlag));
@ -350,7 +350,7 @@ bool Console::cmdHelp(int, const char **) {
debugPrintf(" throw - remove an item from inventory\n");
debugPrintf("\n");
debugPrintf(" palette - set the screen palette\n");
debugPrintf(" draw - draw a resource\n");
debugPrintf(" view - view game resources\n");
debugPrintf("\n");
debugPrintf(" toggle_flag - toggle a flag\n");
debugPrintf("\n");
@ -1046,22 +1046,26 @@ bool Console::cmdSetPalette(int argc, const char **argv) {
return true;
}
bool Console::cmdDrawResource(int argc, const char **argv) {
if (argc != 3 && argc != 4) {
debugPrintf("Syntax: %s <pack> <index> (<frame>)\n", argv[0]);
bool Console::cmdViewResource(int argc, const char **argv) {
if (argc != 2 && argc != 3) {
debugPrintf("Syntax: %s <pack> (<index>)\n", argv[0]);
debugPrintf("\nControls:\n");
debugPrintf(" Space/Backspace - next/previous resource\n");
debugPrintf(" Enter - toggle animation\n");
debugPrintf(" PageDown/PageUp - next/previous palette\n");
debugPrintf(" Arrow keys - scroll the image\n");
debugPrintf(" Escape - quit\n");
return true;
}
int32 pack = atoi(argv[1]);
int32 index = atoi(argv[2]);
int32 frame = 0;
if (argc == 4)
frame = atoi(argv[3]);
int32 index = pack < 18 ? 0 : 8;
if (argc > 2)
index = atoi(argv[2]);
// Check resource pack
if (pack < 0 || pack > 18) {
debugPrintf("[Error] Invalid resource pack (was: %d - valid: [0-18])\n", pack);
if (pack < 1 || (pack > 1 && pack < 5)|| pack > 18) {
debugPrintf("[Error] Invalid resource pack (was: %d - valid: [1,5-18])\n", pack);
return true;
}
@ -1073,34 +1077,14 @@ bool Console::cmdDrawResource(int argc, const char **argv) {
ResourceId resourceId = MAKE_RESOURCE((uint32)pack, index);
// Try loading resource
GraphicResource *resource = new GraphicResource(_vm);
if (!resource->load(resourceId)) {
debugPrintf("[Error] Invalid resource index (was: %d)\n", index);
delete resource;
if (_resViewer.setResourceId(resourceId)) {
_resViewer.setEventHandler(_vm->getEventHandler());
_vm->switchEventHandler(&_resViewer);
return false;
} else {
debugPrintf("[Error] Could not load resource 0x%X\n", resourceId);
return true;
}
if (frame < 0 || frame >= (int32)resource->count()) {
debugPrintf("[Error] Invalid resource frame index (was: %d , max: %d)\n", frame, resource->count() - 1);
delete resource;
return true;
}
delete resource;
// Stop current event handler (to prevent screen refresh)
_vm->switchEventHandler(NULL);
getCursor()->hide();
// Draw resource
getScreen()->clear();
getScreen()->draw(resourceId, (uint32)frame, Common::Point(0, 0));
getScreen()->copyBackBufferToScreen();
g_system->updateScreen();
return false;
}
//////////////////////////////////////////////////////////////////////////

View File

@ -28,6 +28,7 @@
#include "gui/debugger.h"
#include "views/insertdisc.h"
#include "views/resviewer.h"
namespace Asylum {
@ -60,6 +61,7 @@ public:
private:
AsylumEngine *_vm;
InsertDisc _insertDisc;
ResourceViewer _resViewer;
bool cmdHelp(int argc, const char **argv);
@ -92,7 +94,7 @@ private:
bool cmdRemoveFromInventory(int argc, const char **argv);
bool cmdSetPalette(int argc, const char **argv);
bool cmdDrawResource(int argc, const char **argv);
bool cmdViewResource(int argc, const char **argv);
bool cmdToggleFlag(int argc, const char **argv);
};

View File

@ -38,6 +38,7 @@ MODULE_OBJS := \
system/text.o \
views/insertdisc.o \
views/menu.o \
views/resviewer.o \
views/scene.o \
views/scenetitle.o \
views/video.o \

View File

@ -0,0 +1,222 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "asylum/views/resviewer.h"
#include "asylum/system/cursor.h"
#include "asylum/system/screen.h"
#include "asylum/system/text.h"
#include "asylum/asylum.h"
#include "asylum/respack.h"
namespace Asylum {
#define SCROLL_STEP 10
#define NOPALETTE {0, 0, 0, 0, 0, 0, 0, 0}
static int paletteIds[][8] {
NOPALETTE,
{0x0011, 0x001A, 0x001F, 0x003B, 0x003C},
NOPALETTE,
NOPALETTE,
NOPALETTE,
{0x0014, 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x00B7, 0x00CD},
{0x0014, 0x0020, 0x026C, 0x0277, 0x02A6},
{0x0014, 0x0018, 0x0019, 0x001A, 0x0163, 0x0191},
{0x0014, 0x0018, 0x01BC},
{0x0014, 0x008C, 0x00AB, 0x00D4},
{0x0014, 0x001E, 0x001F, 0x0171, 0x0185, 0x01C0},
{0x0012, 0x001F, 0x011B, 0x011E, 0x0120, 0x0135, 0x0159},
{0x0014, 0x018F, 0x01A3, 0x01B0, 0x01C1},
{0x0014, 0x019D, 0x019E, 0x019F, 0x01A7},
{0x0014, 0x001E, 0x001F, 0x0121, 0x0124},
{0x0014, 0x00AD},
{0x0014, 0x0018, 0x0095},
{0x0014, 0x006B},
{0x000B, 0x000C, 0x000D}
};
static const int resPackSizes[] = {
4112, 61, 28, 534, 1, 215, 701, 418, 458, 230, 475, 354, 461, 459, 310, 176, 168, 121, 22
};
ResourceViewer::ResourceViewer(AsylumEngine *engine) : _vm(engine), _resource(_vm) {
_handler = NULL;
_resourceId = kResourceNone;
_frameIndex = _frameCount = 0;
_frameIncrement = 1;
_x = _y = 0;
_width = _height = 0;
_scroll = false;
_resPack = -1;
_paletteIndex = 0;
_animate = true;
}
bool ResourceViewer::setResourceId(ResourceId resourceId) {
if (resourceId == kResourceNone ||
!getResource()->get(resourceId) ||
strncmp((const char *)getResource()->get(resourceId)->data, "D3GR", 4) ||
getResource()->get(resourceId)->size == 800)
return false;
_resourceId = resourceId;
_frameIndex = 0;
_frameCount = GraphicResource::getFrameCount(_vm, _resourceId);
_resource.load(_resourceId);
_frameIncrement = 1;
_x = _y = 0;
_width = _resource.getFrame(0)->getWidth();
_height = _resource.getFrame(0)->getHeight();
_scroll = _width > 640 || _height > 480;
_resPack = RESOURCE_PACK(_resourceId);
_paletteIndex = 0;
int fontIndex = 13;
if (_resPack == 1)
fontIndex = 16;
else if (_resPack == 18)
fontIndex = 19;
getText()->loadFont(MAKE_RESOURCE(_resPack, fontIndex));
return true;
}
void ResourceViewer::update() {
int16 x, y;
GraphicFrame *frame = _resource.getFrame(_frameIndex);
if (_scroll) {
x = _x;
y = _y;
} else {
x = (640 - frame->getWidth()) / 2 - frame->x;
y = (480 - frame->getHeight()) / 2 - frame->y;
}
getScreen()->setPalette(MAKE_RESOURCE(_resPack, paletteIds[_resPack][_paletteIndex]));
getCursor()->hide();
getScreen()->clear();
getScreen()->draw(_resourceId, _frameIndex, Common::Point(x, y));
getText()->draw(Common::Point(615, 440), Common::String::format("%X", _resourceId).c_str());
getScreen()->copyBackBufferToScreen();
if (_frameCount > 1 && _animate) {
if (_frameIndex + 1 >= _frameCount)
_frameIncrement = -1;
else if (_frameIndex == 0)
_frameIncrement = 1;
_frameIndex += _frameIncrement;
}
}
void ResourceViewer::key(const AsylumEvent &evt) {
switch (evt.kbd.keycode) {
default:
break;
case Common::KEYCODE_ESCAPE:
_vm->switchEventHandler(_handler);
break;
case Common::KEYCODE_SPACE:
if (RESOURCE_INDEX(_resourceId) < resPackSizes[_resPack] - 1) {
int i = 1;
do {
if (setResourceId(_resourceId + i))
break;
i++;
} while (RESOURCE_INDEX(_resourceId + i) < resPackSizes[_resPack] - 1);
}
break;
case Common::KEYCODE_BACKSPACE:
if (RESOURCE_INDEX(_resourceId)) {
int i = 0;
do {
i++;
if (setResourceId(_resourceId - i))
break;
} while (RESOURCE_INDEX(_resourceId - i));
}
break;
case Common::KEYCODE_RETURN:
_animate = !_animate;
break;
case Common::KEYCODE_UP:
case Common::KEYCODE_DOWN:
case Common::KEYCODE_RIGHT:
case Common::KEYCODE_LEFT:
if (_scroll) {
int16 x = _x, y = _y;
int dir = (int)(evt.kbd.keycode - Common::KEYCODE_UP);
if (dir < 2)
y -= SCROLL_STEP * (2 * dir - 1);
else
x -= SCROLL_STEP * (1 - 2 * (dir - 2));
if (640 - x <= _width && x <= 0 && 480 - y <= _height && y <= 0) {
_x = x;
_y = y;
}
}
break;
case Common::KEYCODE_PAGEUP:
if (_paletteIndex)
_paletteIndex = _paletteIndex - 1;
break;
case Common::KEYCODE_PAGEDOWN:
if (_paletteIndex < 8 && paletteIds[_resPack][_paletteIndex + 1])
_paletteIndex = _paletteIndex + 1;
break;
}
}
bool ResourceViewer::handleEvent(const AsylumEvent &evt) {
switch ((int32)evt.type) {
default:
break;
case EVENT_ASYLUM_UPDATE:
update();
return true;
case Common::EVENT_KEYDOWN:
key(evt);
return true;
}
return false;
}
} // End of namespace Asylum

View File

@ -0,0 +1,65 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef ASYLUM_VIEWS_RESVIEWER_H
#define ASYLUM_VIEWS_RESVIEWER_H
#include "asylum/system/graphics.h"
#include "asylum/eventhandler.h"
#include "asylum/shared.h"
namespace Asylum {
class AsylumEngine;
class ResourceViewer : public EventHandler {
public:
ResourceViewer(AsylumEngine *engine);
~ResourceViewer() {};
void setEventHandler(EventHandler *handler) { _handler = handler; }
bool setResourceId(ResourceId resourceId);
bool handleEvent(const AsylumEvent &evt);
private:
AsylumEngine *_vm;
EventHandler *_handler;
ResourceId _resourceId;
GraphicResource _resource;
int _frameIndex;
uint _frameCount;
int _frameIncrement;
int16 _x, _y;
uint16 _width, _height;
bool _scroll;
int _resPack;
int _paletteIndex;
bool _animate;
void key(const AsylumEvent &evt);
void update();
};
} // End of namespace Asylum
#endif // ASYLUM_VIEWS_RESVIEWER_H