Skip to content

Commit

Permalink
ADL: Clean up file error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
waltervn committed Jun 6, 2016
1 parent 148814b commit 0686ba9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 42 deletions.
7 changes: 2 additions & 5 deletions engines/adl/adl.cpp
Expand Up @@ -85,12 +85,9 @@ Common::String AdlEngine::readStringAt(Common::SeekableReadStream &stream, uint
return readString(stream, until);
}

Common::File *AdlEngine::openFile(const Common::String &name) const {
Common::File *f = new Common::File();
if (!f->open(name))
void AdlEngine::openFile(Common::File &file, const Common::String &name) const {
if (!file.open(name))
error("Error opening '%s'", name.c_str());

return f;
}

void AdlEngine::printMessage(uint idx, bool wait) const {
Expand Down
2 changes: 1 addition & 1 deletion engines/adl/adl.h
Expand Up @@ -143,7 +143,7 @@ class AdlEngine : public Engine {

Common::String readString(Common::ReadStream &stream, byte until = 0) const;
Common::String readStringAt(Common::SeekableReadStream &stream, uint offset, byte until = 0) const;
Common::File *openFile(const Common::String &name) const;
void openFile(Common::File &file, const Common::String &name) const;

virtual void printMessage(uint idx, bool wait = true) const;
void delay(uint32 ms) const;
Expand Down
34 changes: 12 additions & 22 deletions engines/adl/hires1.cpp
Expand Up @@ -25,6 +25,7 @@
#include "common/error.h"
#include "common/file.h"
#include "common/stream.h"
#include "common/ptr.h"

#include "adl/hires1.h"
#include "adl/display.h"
Expand All @@ -34,8 +35,7 @@ namespace Adl {
void HiRes1Engine::runIntro() const {
Common::File file;

if (!file.open(IDS_HR1_EXE_0))
error("Failed to open file '" IDS_HR1_EXE_0 "'");
openFile(file, IDS_HR1_EXE_0);

file.seek(IDI_HR1_OFS_LOGO_0);
_display->setMode(DISPLAY_MODE_HIRES);
Expand All @@ -49,8 +49,7 @@ void HiRes1Engine::runIntro() const {
_display->setMode(DISPLAY_MODE_TEXT);

Common::File basic;
if (!basic.open(IDS_HR1_LOADER))
error("Failed to open file '" IDS_HR1_LOADER "'");
openFile(basic, IDS_HR1_LOADER);

Common::String str;

Expand Down Expand Up @@ -126,10 +125,8 @@ void HiRes1Engine::runIntro() const {

_display->setMode(DISPLAY_MODE_MIXED);

if (!file.open(IDS_HR1_EXE_1))
error("Failed to open file '" IDS_HR1_EXE_1 "'");

// Title screen shown during loading
openFile(file, IDS_HR1_EXE_1);
file.seek(IDI_HR1_OFS_LOGO_1);
_display->loadFrameBuffer(file);
_display->updateHiResScreen();
Expand All @@ -140,17 +137,13 @@ void HiRes1Engine::init() {
_graphics = new Graphics_v1(*_display);

Common::File f;

if (!f.open(IDS_HR1_MESSAGES))
error("Failed to open file '" IDS_HR1_MESSAGES "'");
openFile(f, IDS_HR1_MESSAGES);

for (uint i = 0; i < IDI_HR1_NUM_MESSAGES; ++i)
_messages.push_back(readString(f, APPLECHAR('\r')) + APPLECHAR('\r'));

f.close();

if (!f.open(IDS_HR1_EXE_1))
error("Failed to open file '" IDS_HR1_EXE_1 "'");
openFile(f, IDS_HR1_EXE_1);

// Some messages have overrides inside the executable
_messages[IDI_HR1_MSG_CANT_GO_THERE - 1] = readStringAt(f, IDI_HR1_OFS_STR_CANT_GO_THERE);
Expand Down Expand Up @@ -223,8 +216,7 @@ void HiRes1Engine::initState() {
_state.vars.clear();
_state.vars.resize(IDI_HR1_NUM_VARS);

if (!f.open(IDS_HR1_EXE_1))
error("Failed to open file '" IDS_HR1_EXE_1 "'");
openFile(f, IDS_HR1_EXE_1);

// Load room data from executable
_state.rooms.clear();
Expand Down Expand Up @@ -277,9 +269,7 @@ void HiRes1Engine::drawPic(byte pic, Common::Point pos) const {
Common::File f;
Common::String name = Common::String::format("BLOCK%i", _pictures[pic].block);

if (!f.open(name))
error("Failed to open file '%s'", name.c_str());

openFile(f, name);
f.seek(_pictures[pic].offset);
_graphics->drawPic(f, pos, 0x7f);
}
Expand All @@ -305,10 +295,10 @@ void HiRes1Engine::printMessage(uint idx, bool wait) const {

void HiRes1Engine::drawItem(const Item &item, const Common::Point &pos) const {
if (item.isLineArt) {
Common::File *f = openFile(IDS_HR1_EXE_1);
f->seek(_corners[item.picture - 1]);
static_cast<Graphics_v1 *>(_graphics)->drawCorners(*f, pos);
delete f;
Common::File f;
openFile(f, IDS_HR1_EXE_1);
f.seek(_corners[item.picture - 1]);
static_cast<Graphics_v1 *>(_graphics)->drawCorners(f, pos);
} else
drawPic(item.picture, pos);
}
Expand Down
18 changes: 4 additions & 14 deletions engines/adl/hires2.cpp
Expand Up @@ -34,10 +34,7 @@ namespace Adl {

void HiRes2Engine::runIntro() const {
Common::File f;

if (!f.open(IDS_HR2_DISK_IMAGE))
error("Failed to open file '" IDS_HR2_DISK_IMAGE "'");

openFile(f, IDS_HR2_DISK_IMAGE);
f.seek(IDI_HR2_OFS_INTRO_TEXT);

_display->setMode(DISPLAY_MODE_TEXT);
Expand All @@ -55,9 +52,7 @@ void HiRes2Engine::init() {
_graphics = new Graphics_v2(*_display);

Common::File f;

if (!f.open(IDS_HR2_DISK_IMAGE))
error("Failed to open file '" IDS_HR2_DISK_IMAGE "'");
openFile(f, IDS_HR2_DISK_IMAGE);

for (uint i = 0; i < IDI_HR2_NUM_MESSAGES; ++i) {
f.seek(IDI_HR2_OFS_MESSAGES + i * 4);
Expand Down Expand Up @@ -97,9 +92,7 @@ void HiRes2Engine::init() {

void HiRes2Engine::initState() {
Common::File f;

if (!f.open(IDS_HR2_DISK_IMAGE))
error("Failed to open file '" IDS_HR2_DISK_IMAGE "'");
openFile(f, IDS_HR2_DISK_IMAGE);

_state.rooms.clear();
f.seek(IDI_HR2_OFS_ROOMS);
Expand Down Expand Up @@ -127,10 +120,7 @@ void HiRes2Engine::drawPic(byte pic, Common::Point pos) const {
// Temp hack to show a pic

Common::File f;

if (!f.open(IDS_HR2_DISK_IMAGE))
error("Failed to open file '" IDS_HR2_DISK_IMAGE "'");

openFile(f, IDS_HR2_DISK_IMAGE);
f.seek(0x1000);

_graphics->drawPic(f, pos, 0);
Expand Down

0 comments on commit 0686ba9

Please sign in to comment.