scummvm/engines/cge/talk.cpp

305 lines
7.0 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 09:57:24 +00:00
#include "cge/general.h"
#include "cge/talk.h"
#include "cge/game.h"
#include "cge/events.h"
2011-06-10 20:57:09 +00:00
namespace CGE {
2011-08-19 14:04:10 +00:00
2011-07-03 09:28:22 +00:00
Font::Font(const char *name) {
2011-08-19 14:04:10 +00:00
_map = (uint8 *)malloc(kMapSize);
_pos = (uint16 *)malloc(kPosSize * sizeof(uint16));
_wid = (uint8 *)malloc(kWidSize);
assert((_map != NULL) && (_pos != NULL) && (_wid != NULL));
2011-07-19 05:50:07 +00:00
mergeExt(_path, name, kFontExt);
2011-07-03 09:28:22 +00:00
load();
}
2011-07-03 09:28:22 +00:00
Font::~Font() {
free(_map);
free(_pos);
free(_wid);
}
2011-07-03 09:28:22 +00:00
void Font::load() {
VFile f(_path);
2011-08-19 14:04:10 +00:00
if (f._error)
return;
f.read(_wid, kWidSize);
if (f._error)
return;
uint16 p = 0;
for (uint16 i = 0; i < kPosSize; i++) {
_pos[i] = p;
warning("Fonts 0x%X 0x%X", i, p);
2011-08-19 14:04:10 +00:00
p += _wid[i];
}
2011-08-19 14:04:10 +00:00
f.read(_map, p);
}
2011-07-03 09:28:22 +00:00
uint16 Font::width(const char *text) {
2011-06-13 09:57:24 +00:00
uint16 w = 0;
2011-08-19 14:04:10 +00:00
if (!text)
return 0;
while (*text)
w += _wid[(unsigned char)*(text++)];
2011-06-13 09:57:24 +00:00
return w;
}
Talk::Talk(CGEEngine *vm, const char *text, TextBoxStyle mode)
2011-07-03 09:28:22 +00:00
: Sprite(vm, NULL), _mode(mode), _vm(vm) {
_ts = NULL;
_flags._syst = true;
update(text);
}
2011-07-03 09:28:22 +00:00
Talk::Talk(CGEEngine *vm)
2011-07-24 21:42:03 +00:00
: Sprite(vm, NULL), _mode(kTBPure), _vm(vm) {
_ts = NULL;
_flags._syst = true;
}
2011-07-03 09:28:22 +00:00
Font *Talk::_font;
2011-07-03 09:28:22 +00:00
void Talk::init() {
_font = new Font(progName());
}
2011-07-03 09:28:22 +00:00
void Talk::deinit() {
delete _font;
}
void Talk::update(const char *text) {
2011-08-19 14:04:10 +00:00
const uint16 vmarg = (_mode) ? kTextVMargin : 0;
const uint16 hmarg = (_mode) ? kTextHMargin : 0;
uint16 mw = 0;
uint16 ln = vmarg;
2011-06-13 09:57:24 +00:00
uint8 *m;
if (!_ts) {
2011-06-13 09:57:24 +00:00
uint16 k = 2 * hmarg;
uint16 mh = 2 * vmarg + kFontHigh;
2011-08-19 14:04:10 +00:00
for (const char *p = text; *p; p++) {
2011-06-13 09:57:24 +00:00
if (*p == '|' || *p == '\n') {
2011-07-19 05:50:07 +00:00
mh += kFontHigh + kTextLineSpace;
2011-06-13 11:07:45 +00:00
if (k > mw)
2011-06-13 09:57:24 +00:00
mw = k;
k = 2 * hmarg;
2011-06-13 11:07:45 +00:00
} else
2011-07-03 09:28:22 +00:00
k += _font->_wid[(unsigned char)*p];
2011-06-13 09:57:24 +00:00
}
2011-06-13 11:07:45 +00:00
if (k > mw)
2011-06-13 09:57:24 +00:00
mw = k;
_ts = new BitmapPtr[2];
2011-07-03 09:28:22 +00:00
_ts[0] = box(mw, mh);
_ts[1] = NULL;
}
2011-06-13 09:57:24 +00:00
2011-07-03 09:28:22 +00:00
m = _ts[0]->_m + ln * mw + hmarg;
2011-06-13 09:57:24 +00:00
while (*text) {
2011-08-19 14:04:10 +00:00
if (*text == '|' || *text == '\n') {
2011-07-19 05:50:07 +00:00
m = _ts[0]->_m + (ln += kFontHigh + kTextLineSpace) * mw + hmarg;
2011-08-19 14:04:10 +00:00
} else {
int cw = _font->_wid[(unsigned char)*text];
uint8 *f = _font->_map + _font->_pos[(unsigned char)*text];
2011-08-19 14:04:10 +00:00
for (int i = 0; i < cw; i++) {
uint8 *pp = m;
2011-06-13 09:57:24 +00:00
uint16 n;
2011-08-19 14:04:10 +00:00
uint16 b = *(f++);
2011-07-19 05:50:07 +00:00
for (n = 0; n < kFontHigh; n++) {
2011-06-13 11:07:45 +00:00
if (b & 1)
2011-07-19 05:50:07 +00:00
*pp = kTextColFG;
2011-06-13 09:57:24 +00:00
b >>= 1;
pp += mw;
2011-06-13 09:57:24 +00:00
}
2011-06-29 14:13:17 +00:00
m++;
2011-06-13 09:57:24 +00:00
}
}
text++;
}
2011-07-03 09:28:22 +00:00
_ts[0]->code();
setShapeList(_ts);
}
2011-07-03 09:28:22 +00:00
Bitmap *Talk::box(uint16 w, uint16 h) {
2011-06-13 11:07:45 +00:00
if (w < 8)
2011-06-13 09:57:24 +00:00
w = 8;
2011-06-13 11:07:45 +00:00
if (h < 8)
2011-06-13 09:57:24 +00:00
h = 8;
2011-07-24 21:42:03 +00:00
uint16 n = w * h;
2011-08-19 14:04:10 +00:00
uint8 *b = (uint8 *)malloc(n);
assert(b != NULL);
2011-07-19 05:50:07 +00:00
memset(b, kTextColBG, n);
2011-07-03 09:28:22 +00:00
if (_mode) {
2011-08-19 14:04:10 +00:00
uint8 *p = b;
uint8 *q = b + n - w;
2011-08-21 09:15:28 +00:00
memset(p, kVgaColLightGray, w);
memset(q, kVgaColDarkGray, w);
2011-06-13 09:57:24 +00:00
while (p < q) {
p += w;
2011-08-21 09:15:28 +00:00
*(p - 1) = kVgaColDarkGray;
*p = kVgaColLightGray;
2011-06-13 09:57:24 +00:00
}
p = b;
2011-08-19 14:04:10 +00:00
const uint16 r = (_mode == kTBRound) ? kTextRoundCorner : 0;
for (int i = 0; i < r; i++) {
2011-06-13 09:57:24 +00:00
int j;
for (j = 0; j < r - i; j++) {
2011-07-19 06:42:58 +00:00
p[j] = kPixelTransp;
p[w - j - 1] = kPixelTransp;
q[j] = kPixelTransp;
q[w - j - 1] = kPixelTransp;
2011-06-13 09:57:24 +00:00
}
2011-08-21 09:15:28 +00:00
p[j] = kVgaColLightGray;
p[w - j - 1] = kVgaColDarkGray;
q[j] = kVgaColLightGray;
q[w - j - 1] = kVgaColDarkGray;
2011-06-13 09:57:24 +00:00
p += w;
q -= w;
}
}
2011-06-29 14:13:17 +00:00
return new Bitmap(w, h, b);
}
2011-07-03 09:28:22 +00:00
void Talk::putLine(int line, const char *text) {
2011-08-19 14:04:10 +00:00
// Note: (_ts[0]._w % 4) must be 0
2011-07-03 09:28:22 +00:00
uint16 w = _ts[0]->_w;
uint16 h = _ts[0]->_h;
2011-08-19 14:04:10 +00:00
uint8 *v = _ts[0]->_v;
uint16 dsiz = w >> 2; // data size (1 plane line size)
2011-06-13 09:57:24 +00:00
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
2011-07-19 05:50:07 +00:00
uint16 rsiz = kFontHigh * lsiz; // length of whole text row map
2011-06-13 09:57:24 +00:00
// set desired line pointer
2011-07-19 05:50:07 +00:00
v += (kTextVMargin + (kFontHigh + kTextLineSpace) * line) * lsiz;
2011-08-19 14:04:10 +00:00
uint8 *p = v; // assume blanked line above text
2011-06-13 09:57:24 +00: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 09:57:24 +00:00
// paint text line
2011-08-19 14:04:10 +00:00
if (!text)
return;
p = v + 2 + (kTextHMargin / 4) + (kTextHMargin % 4) * psiz;
uint8 *q = v + size;
2011-06-13 09:57:24 +00:00
2011-08-19 14:04:10 +00:00
while (*text) {
uint16 cw = _font->_wid[(unsigned char)*text], i;
uint8 *fp = _font->_map + _font->_pos[(unsigned char)*text];
for (i = 0; i < cw; i++) {
uint16 b = fp[i];
uint16 n;
for (n = 0; n < kFontHigh; n++) {
if (b & 1)
*p = kTextColFG;
b >>= 1;
p += lsiz;
2011-06-13 09:57:24 +00:00
}
2011-08-19 14:04:10 +00:00
p = p - rsiz + psiz;
if (p >= q)
p = p - size + 1;
}
2011-08-19 14:04:10 +00:00
text++;
}
}
InfoLine::InfoLine(CGEEngine *vm, uint16 w) : Talk(vm), _oldText(NULL), _vm(vm) {
if (!_ts) {
_ts = new BitmapPtr[2];
_ts[1] = NULL;
}
2011-07-19 05:50:07 +00:00
_ts[0] = new Bitmap(w, kFontHigh, kTextColBG);
2011-07-03 09:28:22 +00:00
setShapeList(_ts);
}
void InfoLine::update(const char *text) {
2011-08-19 14:04:10 +00:00
if (text == _oldText)
return;
uint16 w = _ts[0]->_w;
uint16 h = _ts[0]->_h;
uint8 *v = (uint8 *)_ts[0]->_v;
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
// clear whole rectangle
memset(v + 2, kTextColBG, dsiz); // data bytes
for (byte *pDest = v + lsiz; pDest < (v + psiz); pDest += lsiz) {
Common::copy(v, v + lsiz, pDest);
}
*(uint16 *)(v + psiz - 2) = TO_LE_16(kBmpEOI); // plane trailer uint16
for (byte *pDest = v + psiz; pDest < (v + 4 * psiz); pDest += psiz) {
Common::copy(v, v + psiz, pDest);
}
2011-08-19 14:04:10 +00:00
// paint text line
if (text) {
uint8 *p = v + 2, * q = p + size;
while (*text) {
uint16 cw = _font->_wid[(unsigned char)*text];
uint8 *fp = _font->_map + _font->_pos[(unsigned char)*text];
for (uint16 i = 0; i < cw; i++) {
uint16 b = fp[i];
for (uint16 n = 0; n < kFontHigh; n++) {
if (b & 1)
*p = kTextColFG;
b >>= 1;
p += lsiz;
2011-06-13 09:57:24 +00:00
}
2011-08-19 14:04:10 +00:00
if (p >= q)
p = p - size + 1;
2011-06-13 09:57:24 +00:00
}
2011-08-19 14:04:10 +00:00
text++;
}
}
2011-08-19 14:04:10 +00:00
_oldText = text;
}
2011-06-10 20:57:09 +00:00
} // End of namespace CGE