GOB: Add a simple class for PreGob TXT files

This commit is contained in:
Sven Hesse
2012-07-30 01:44:43 +02:00
parent 9af01cd584
commit 4b3aa88c8a
5 changed files with 341 additions and 0 deletions
+1
View File
@@ -79,6 +79,7 @@ MODULE_OBJS := \
demos/batplayer.o \
detection/detection.o \
pregob/pregob.o \
pregob/txtfile.o \
pregob/onceupon/onceupon.o \
pregob/onceupon/abracadabra.o \
pregob/onceupon/babayaga.o \
+13
View File
@@ -26,6 +26,7 @@
#include "gob/global.h"
#include "gob/util.h"
#include "gob/surface.h"
#include "gob/dataio.h"
#include "gob/palanim.h"
#include "gob/draw.h"
#include "gob/video.h"
@@ -202,4 +203,16 @@ void PreGob::redrawAnim(ANIObject &ani) {
drawAnim(ani);
}
TXTFile *PreGob::loadTXT(const Common::String &txtFile, TXTFile::Format format) const {
Common::SeekableReadStream *txtStream = _vm->_dataIO->getFile(txtFile);
if (!txtStream)
error("PreGob::loadTXT(): Failed to open \"%s\"", txtFile.c_str());
TXTFile *txt = new TXTFile(*txtStream, format);
delete txtStream;
return txt;
}
} // End of namespace Gob
+4
View File
@@ -25,6 +25,8 @@
#include "gob/util.h"
#include "gob/pregob/txtfile.h"
namespace Gob {
class GobEngine;
@@ -77,6 +79,8 @@ protected:
void drawAnim(ANIObject &ani);
void redrawAnim(ANIObject &ani);
TXTFile *loadTXT(const Common::String &txtFile, TXTFile::Format format) const;
GobEngine *_vm;
+232
View File
@@ -0,0 +1,232 @@
/* 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 "common/stream.h"
#include "gob/draw.h"
#include "gob/pregob/txtfile.h"
namespace Gob {
TXTFile::TXTFile(Common::SeekableReadStream &txt, Format format) {
load(txt, format);
}
TXTFile::~TXTFile() {
}
TXTFile::LineArray &TXTFile::getLines() {
return _lines;
}
void TXTFile::load(Common::SeekableReadStream &txt, Format format) {
if (format == kFormatStringPositionColorFont) {
int numLines = getInt(txt);
_lines.reserve(numLines);
}
while (!txt.eos()) {
Line line;
line.text = getStr(txt);
line.x = (format >= kFormatStringPosition) ? getInt(txt) : 0;
line.y = (format >= kFormatStringPosition) ? getInt(txt) : 0;
line.color = (format >= kFormatStringPositionColor) ? getInt(txt) : 0;
line.font = (format >= kFormatStringPositionColorFont) ? getInt(txt) : 0;
_lines.push_back(line);
}
while (!_lines.empty() && _lines.back().text.empty())
_lines.pop_back();
}
bool TXTFile::draw(Surface &surface, int16 &left, int16 &top, int16 &right, int16 &bottom,
const Font * const *fonts, uint fontCount, int color) {
trashBuffer();
if (!getArea(left, top, right, bottom, fonts, fontCount))
return false;
resizeBuffer(right - left + 1, bottom - top + 1);
saveScreen(surface, left, top, right, bottom);
for (LineArray::const_iterator l = _lines.begin(); l != _lines.end(); ++l) {
if (l->font >= fontCount)
continue;
fonts[l->font]->drawString(l->text, l->x, l->y, (color < 0) ? l->color : color, 0, true, surface);
}
return true;
}
bool TXTFile::draw(uint line, Surface &surface, int16 &left, int16 &top, int16 &right, int16 &bottom,
const Font * const *fonts, uint fontCount, int color) {
trashBuffer();
if (!getArea(line, left, top, right, bottom, fonts, fontCount))
return false;
resizeBuffer(right - left + 1, bottom - top + 1);
saveScreen(surface, left, top, right, bottom);
const Line &l = _lines[line];
fonts[l.font]->drawString(l.text, l.x, l.y, (color < 0) ? l.color : color, 0, true, surface);
return true;
}
bool TXTFile::draw(Surface &surface, const Font * const *fonts, uint fontCount, int color) {
int16 left, top, right, bottom;
return draw(surface, left, top, right, bottom, fonts, fontCount, color);
}
bool TXTFile::draw(uint line, Surface &surface, const Font * const *fonts, uint fontCount, int color) {
int16 left, top, right, bottom;
return draw(line, surface, left, top, right, bottom, fonts, fontCount, color);
}
bool TXTFile::clear(Surface &surface, int16 &left, int16 &top, int16 &right, int16 &bottom) {
return restoreScreen(surface, left, top, right, bottom);
}
bool TXTFile::getArea(int16 &left, int16 &top, int16 &right, int16 &bottom,
const Font * const *fonts, uint fontCount) const {
bool hasLine = false;
left = 0x7FFF;
top = 0x7FFF;
right = 0x0000;
bottom = 0x0000;
for (uint i = 0; i < _lines.size(); i++) {
int16 lLeft, lTop, lRight, lBottom;
if (getArea(i, lLeft, lTop, lRight, lBottom, fonts, fontCount)) {
left = MIN(left , lLeft );
top = MIN(top , lTop );
right = MAX(right , lRight );
bottom = MAX(bottom, lBottom);
hasLine = true;
}
}
return hasLine;
}
bool TXTFile::getArea(uint line, int16 &left, int16 &top, int16 &right, int16 &bottom,
const Font * const *fonts, uint fontCount) const {
if ((line >= _lines.size()) || (_lines[line].font >= fontCount))
return false;
const Line &l = _lines[line];
left = l.x;
top = l.y;
right = l.x + l.text.size() * fonts[l.font]->getCharWidth() - 1;
bottom = l.y + fonts[l.font]->getCharHeight() - 1;
return true;
}
Common::String TXTFile::getStr(Common::SeekableReadStream &txt) {
// Skip all ' ', '\n' and '\r'
while (!txt.eos()) {
char c = txt.readByte();
if (txt.eos())
break;
if ((c != ' ') && (c != '\n') && (c != '\r')) {
txt.seek(-1, SEEK_CUR);
break;
}
}
if (txt.eos())
return "";
// Read string until ' ', '\n' or '\r'
Common::String string;
while (!txt.eos()) {
char c = txt.readByte();
if ((c == ' ') || (c == '\n') || (c == '\r'))
break;
string += c;
}
// Replace all '#' with ' ' and throw out non-printables
Common::String cleanString;
for (uint i = 0; i < string.size(); i++) {
if (string[i] == '#')
cleanString += ' ';
else if ((unsigned char)string[i] >= ' ')
cleanString += string[i];
}
return cleanString;
}
int TXTFile::getInt(Common::SeekableReadStream &txt) {
// Skip all [^-0-9]
while (!txt.eos()) {
char c = txt.readByte();
if (txt.eos())
break;
if ((c == '-') || ((c >= '0') && (c <= '9'))) {
txt.seek(-1, SEEK_CUR);
break;
}
}
if (txt.eos())
return 0;
// Read until [^-0-9]
Common::String string;
while (!txt.eos()) {
char c = txt.readByte();
if ((c != '-') && ((c < '0') || (c > '9')))
break;
string += c;
}
// Convert to integer
return atoi(string.c_str());
}
} // End of namespace Gob
+91
View File
@@ -0,0 +1,91 @@
/* 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 GOB_PREGOB_TXTFILE_H
#define GOB_PREGOB_TXTFILE_H
#include "common/system.h"
#include "common/str.h"
#include "common/array.h"
#include "gob/backbuffer.h"
namespace Common {
class SeekableReadStream;
}
namespace Gob {
class Surface;
class Font;
class TXTFile : public BackBuffer {
public:
enum Format {
kFormatString,
kFormatStringPosition,
kFormatStringPositionColor,
kFormatStringPositionColorFont
};
struct Line {
Common::String text;
int x, y;
int color;
uint font;
};
typedef Common::Array<Line> LineArray;
TXTFile(Common::SeekableReadStream &txt, Format format);
~TXTFile();
LineArray &getLines();
bool draw( Surface &surface, const Font * const *fonts, uint fontCount, int color = -1);
bool draw(uint line, Surface &surface, const Font * const *fonts, uint fontCount, int color = -1);
bool draw( Surface &surface, int16 &left, int16 &top, int16 &right, int16 &bottom,
const Font * const *fonts, uint fontCount, int color = -1);
bool draw(uint line, Surface &surface, int16 &left, int16 &top, int16 &right, int16 &bottom,
const Font * const *fonts, uint fontCount, int color = -1);
bool clear(Surface &surface, int16 &left, int16 &top, int16 &right, int16 &bottom);
private:
LineArray _lines;
void load(Common::SeekableReadStream &txt, Format format);
Common::String getStr(Common::SeekableReadStream &txt);
int getInt(Common::SeekableReadStream &txt);
bool getArea( int16 &left, int16 &top, int16 &right, int16 &bottom,
const Font * const *fonts, uint fontCount) const;
bool getArea(uint line, int16 &left, int16 &top, int16 &right, int16 &bottom,
const Font * const *fonts, uint fontCount) const;
};
} // End of namespace Gob
#endif // GOB_PREGOB_TXTFILE_H