scummvm/engines/cge/talk.cpp

349 lines
7.3 KiB
C++
Raw Normal View History

/* 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.
*
*/
/*
* This code is based on original Soltys source code
* Copyright (c) 1994-1995 Janus B. Wisniewski and L.K. Avalon
*/
2011-06-13 11:57:24 +02:00
#include "cge/general.h"
#include "cge/talk.h"
#include "cge/vol.h"
#include "cge/game.h"
#include "cge/events.h"
2011-06-10 22:57:09 +02:00
namespace CGE {
2011-06-13 11:57:24 +02:00
#define WID_SIZ 256
#define POS_SIZ 256
#define MAP_SIZ (256*8)
2011-06-13 11:57:24 +02:00
//uint8 FONT::Wid[WID_SIZ];
//uint16 FONT::Pos[POS_SIZ];
//uint8 FONT::Map[MAP_SIZ];
2011-07-03 11:28:22 +02:00
Font::Font(const char *name) {
_map = farnew(uint8, MAP_SIZ);
_pos = farnew(uint16, POS_SIZ);
_wid = farnew(uint8, WID_SIZ);
if ((_map == NULL) || (_pos == NULL) || (_wid == NULL))
2011-06-13 11:57:24 +02:00
error("No core");
2011-07-03 11:28:22 +02:00
mergeExt(_path, name, FONT_EXT);
load();
}
2011-07-03 11:28:22 +02:00
Font::~Font() {
free(_map);
free(_pos);
free(_wid);
}
2011-07-03 11:28:22 +02:00
void Font::load() {
INI_FILE f(_path);
2011-07-01 08:37:40 +02:00
if (!f._error) {
2011-07-03 11:28:22 +02:00
f.read(_wid, WID_SIZ);
2011-07-01 08:37:40 +02:00
if (!f._error) {
2011-06-13 11:57:24 +02:00
uint16 i, p = 0;
for (i = 0; i < POS_SIZ; i++) {
2011-07-03 11:28:22 +02:00
_pos[i] = p;
p += _wid[i];
2011-06-13 11:57:24 +02:00
}
2011-07-03 11:28:22 +02:00
f.read(_map, p);
2011-06-13 11:57:24 +02:00
}
}
}
2011-07-03 11:28:22 +02:00
uint16 Font::width(const char *text) {
2011-06-13 11:57:24 +02:00
uint16 w = 0;
2011-06-13 13:07:45 +02:00
if (text)
while (* text)
2011-07-03 11:28:22 +02:00
w += _wid[(unsigned char)*(text++)];
2011-06-13 11:57:24 +02:00
return w;
}
/*
2011-07-03 11:28:22 +02:00
void Font::save() {
CFILE f((const char *) _path, WRI);
if (!f._error) {
f.Write(_wid, WID_SIZ);
if (!f._error)
f.Write(_map, _pos[POS_SIZ - 1] + _wid[WID_SIZ - 1]);
}
}
*/
2011-07-03 11:28:22 +02:00
Talk::Talk(CGEEngine *vm, const char *tx, TBOX_STYLE mode)
: Sprite(vm, NULL), _mode(mode), _vm(vm) {
_ts = NULL;
2011-06-29 16:13:17 +02:00
_flags._syst = true;
2011-07-03 11:28:22 +02:00
update(tx);
}
2011-07-03 11:28:22 +02:00
Talk::Talk(CGEEngine *vm)
: Sprite(vm, NULL), _mode(PURE), _vm(vm) {
_ts = NULL;
2011-06-29 16:13:17 +02:00
_flags._syst = true;
}
/*
2011-07-03 11:28:22 +02:00
Talk::~Talk() {
for (uint16 i = 0; i < ShpCnt; i++) {
2011-07-03 11:28:22 +02:00
if (FP_SEG(_shpList[i]) != _DS) { // small model: always false
delete _shpList[i];
2011-06-13 11:57:24 +02:00
ShpList[i] = NULL;
}
}
}
*/
2011-07-03 11:28:22 +02:00
Font *Talk::_font;
2011-07-03 11:28:22 +02:00
void Talk::init() {
_font = new Font(progName());
}
2011-07-03 11:28:22 +02:00
void Talk::deinit() {
delete _font;
}
2011-07-03 11:28:22 +02:00
void Talk::update(const char *tx) {
uint16 vmarg = (_mode) ? TEXT_VM : 0;
uint16 hmarg = (_mode) ? TEXT_HM : 0;
2011-06-13 11:57:24 +02:00
uint16 mw = 0, mh, ln = vmarg;
const char *p;
uint8 *m;
if (!_ts) {
2011-06-13 11:57:24 +02:00
uint16 k = 2 * hmarg;
mh = 2 * vmarg + FONT_HIG;
for (p = tx; *p; p++) {
2011-06-13 11:57:24 +02:00
if (*p == '|' || *p == '\n') {
mh += FONT_HIG + TEXT_LS;
2011-06-13 13:07:45 +02:00
if (k > mw)
2011-06-13 11:57:24 +02:00
mw = k;
k = 2 * hmarg;
2011-06-13 13:07:45 +02:00
} else
2011-07-03 11:28:22 +02:00
k += _font->_wid[(unsigned char)*p];
2011-06-13 11:57:24 +02:00
}
2011-06-13 13:07:45 +02:00
if (k > mw)
2011-06-13 11:57:24 +02:00
mw = k;
_ts = new BMP_PTR[2];
2011-07-03 11:28:22 +02:00
_ts[0] = box(mw, mh);
_ts[1] = NULL;
}
2011-06-13 11:57:24 +02:00
2011-07-03 11:28:22 +02:00
m = _ts[0]->_m + ln * mw + hmarg;
2011-06-13 11:57:24 +02:00
while (* tx) {
if (*tx == '|' || *tx == '\n')
2011-07-03 11:28:22 +02:00
m = _ts[0]->_m + (ln += FONT_HIG + TEXT_LS) * mw + hmarg;
2011-06-13 11:57:24 +02:00
else {
2011-07-03 11:28:22 +02:00
int cw = _font->_wid[(unsigned char)*tx], i;
uint8 *f = _font->_map + _font->_pos[(unsigned char)*tx];
2011-06-13 11:57:24 +02:00
for (i = 0; i < cw; i++) {
uint8 *pp = m;
2011-06-13 11:57:24 +02:00
uint16 n;
register uint16 b = *(f++);
for (n = 0; n < FONT_HIG; n++) {
2011-06-13 13:07:45 +02:00
if (b & 1)
*pp = TEXT_FG;
2011-06-13 11:57:24 +02:00
b >>= 1;
pp += mw;
2011-06-13 11:57:24 +02:00
}
2011-06-29 16:13:17 +02:00
m++;
2011-06-13 11:57:24 +02:00
}
}
2011-06-29 16:13:17 +02:00
tx++;
}
2011-07-03 11:28:22 +02:00
_ts[0]->code();
setShapeList(_ts);
}
2011-07-03 11:28:22 +02:00
Bitmap *Talk::box(uint16 w, uint16 h) {
2011-06-13 11:57:24 +02:00
uint8 *b, * p, * q;
2011-07-03 11:28:22 +02:00
uint16 n, r = (_mode == ROUND) ? TEXT_RD : 0;
2011-06-13 13:07:45 +02:00
if (w < 8)
2011-06-13 11:57:24 +02:00
w = 8;
2011-06-13 13:07:45 +02:00
if (h < 8)
2011-06-13 11:57:24 +02:00
h = 8;
b = farnew(uint8, n = w * h);
if (! b)
error("No core");
memset(b, TEXT_BG, n);
2011-07-03 11:28:22 +02:00
if (_mode) {
2011-06-13 11:57:24 +02:00
p = b;
q = b + n - w;
memset(p, LGRAY, w);
memset(q, DGRAY, w);
while (p < q) {
p += w;
*(p - 1) = DGRAY;
*p = LGRAY;
}
p = b;
for (int i = 0; i < r; i++) {
2011-06-13 11:57:24 +02:00
int j;
for (j = 0; j < r - i; j++) {
2011-06-13 11:57:24 +02:00
p[j] = TRANS;
p[w - j - 1] = TRANS;
q[j] = TRANS;
q[w - j - 1] = TRANS;
}
p[j] = LGRAY;
p[w - j - 1] = DGRAY;
q[j] = LGRAY;
q[w - j - 1] = DGRAY;
p += w;
q -= w;
}
}
2011-06-29 16:13:17 +02:00
return new Bitmap(w, h, b);
}
2011-07-03 11:28:22 +02:00
void Talk::putLine(int line, const char *text) {
// Note: (TS[0].W % 4) have to be 0
2011-07-03 11:28:22 +02:00
uint16 w = _ts[0]->_w;
uint16 h = _ts[0]->_h;
uint8 *v = _ts[0]->_v, * p;
2011-06-13 11:57:24 +02:00
uint16 dsiz = w >> 2; // data size (1 plane line size)
uint16 lsiz = 2 + dsiz + 2; // uint16 for line header, uint16 for gap
uint16 psiz = h * lsiz; // - last gap, but + plane trailer
uint16 size = 4 * psiz; // whole map size
uint16 rsiz = FONT_HIG * lsiz; // length of whole text row map
// set desired line pointer
v += (TEXT_VM + (FONT_HIG + TEXT_LS) * line) * lsiz;
p = v; // assume blanked line above text
2011-06-13 11:57:24 +02:00
// clear whole rectangle
assert((rsiz % lsiz) == 0);
for (int planeCtr = 0; planeCtr < 4; ++planeCtr, p += psiz) {
for (byte *pDest = p; pDest < (p + (rsiz - lsiz)); pDest += lsiz)
Common::copy(p - lsiz, p, pDest);
}
2011-06-13 11:57:24 +02:00
// paint text line
if (text) {
uint8 *q;
p = v + 2 + TEXT_HM / 4 + (TEXT_HM % 4) * psiz;
q = v + size;
while (* text) {
2011-07-03 11:28:22 +02:00
uint16 cw = _font->_wid[(unsigned char)*text], i;
uint8 *fp = _font->_map + _font->_pos[(unsigned char)*text];
2011-06-13 11:57:24 +02:00
for (i = 0; i < cw; i++) {
2011-06-13 11:57:24 +02:00
register uint16 b = fp[i];
uint16 n;
for (n = 0; n < FONT_HIG; n++) {
2011-06-13 13:07:45 +02:00
if (b & 1)
2011-06-13 11:57:24 +02:00
*p = TEXT_FG;
b >>= 1;
p += lsiz;
}
p = p - rsiz + psiz;
2011-06-13 13:07:45 +02:00
if (p >= q)
2011-06-13 11:57:24 +02:00
p = p - size + 1;
}
2011-06-29 16:13:17 +02:00
text++;
}
}
}
2011-07-03 11:28:22 +02:00
InfoLine::InfoLine(CGEEngine *vm, uint16 w) : Talk(vm), _oldTxt(NULL), _vm(vm) {
if (!_ts) {
_ts = new BMP_PTR[2];
_ts[1] = NULL;
}
2011-07-03 11:28:22 +02:00
_ts[0] = new Bitmap(w, FONT_HIG, TEXT_BG);
setShapeList(_ts);
}
2011-07-03 11:28:22 +02:00
void InfoLine::update(const char *tx) {
if (tx != _oldTxt) {
uint16 w = _ts[0]->_w;
uint16 h = _ts[0]->_h;
uint8 *v = (uint8 *) _ts[0]->_v;
2011-06-13 11:57:24 +02:00
uint16 dsiz = w >> 2; // data size (1 plane line size)
uint16 lsiz = 2 + dsiz + 2; // uint16 for line header, uint16 for gap
uint16 psiz = h * lsiz; // - last gape, but + plane trailer
uint16 size = 4 * psiz; // whole map size
2011-07-03 11:28:22 +02:00
// clear whole rectangle
2011-06-13 11:57:24 +02:00
memset(v + 2, TEXT_BG, dsiz); // data bytes
for (byte *pDest = v + lsiz; pDest < (v + psiz); pDest += lsiz)
*pDest = 0;
// Common::set_to(pDest, pDest + lsiz, 0);
//Common::copy(v, v + lsiz, pDest);
*(uint16 *)(v + psiz - 2) = EOI; // plane trailer uint16
memmove(v + psiz, v, 3 * psiz);
2011-06-13 11:57:24 +02:00
// paint text line
if (tx) {
uint8 *p = v + 2, * q = p + size;
while (*tx) {
2011-07-03 11:28:22 +02:00
uint16 cw = _font->_wid[(unsigned char)*tx];
uint8 *fp = _font->_map + _font->_pos[(unsigned char)*tx];
2011-06-13 11:57:24 +02:00
for (uint16 i = 0; i < cw; i++) {
register uint16 b = fp[i];
for (uint16 n = 0; n < FONT_HIG; n++) {
2011-06-13 13:07:45 +02:00
if (b & 1)
2011-06-13 11:57:24 +02:00
*p = TEXT_FG;
b >>= 1;
p += lsiz;
}
2011-06-13 13:07:45 +02:00
if (p >= q)
2011-06-13 11:57:24 +02:00
p = p - size + 1;
}
2011-06-29 16:13:17 +02:00
tx++;
2011-06-13 11:57:24 +02:00
}
}
2011-07-03 11:28:22 +02:00
_oldTxt = tx;
}
}
2011-06-10 22:57:09 +02:00
} // End of namespace CGE