scummvm/engines/cge/text.cpp

260 lines
5.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 09:57:24 +00:00
#include "cge/general.h"
#include "cge/text.h"
#include "cge/talk.h"
#include "cge/vol.h"
#include "cge/bitmaps.h"
#include "cge/game.h"
#include "cge/snail.h"
#include "cge/cge_main.h"
2011-06-13 09:57:24 +00:00
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
2011-06-10 20:57:09 +00:00
namespace CGE {
TEXT *Text;
2011-06-13 09:57:24 +00:00
TALK *Talk = NULL;
2011-06-13 09:57:24 +00:00
TEXT::TEXT(const char *fname, int size) {
Cache = new HAN[size];
MergeExt(FileName, fname, SAY_EXT);
if (!INI_FILE::Exist(FileName))
error("No talk\n");
2011-06-13 09:57:24 +00:00
for (Size = 0; Size < size; Size ++) {
Cache[Size].Ref = 0;
Cache[Size].Txt = NULL;
}
}
2011-06-13 09:57:24 +00:00
TEXT::~TEXT(void) {
Clear();
delete[] Cache;
}
2011-06-13 09:57:24 +00:00
void TEXT::Clear(int from, int upto) {
HAN *p, * q;
for (p = Cache, q = p + Size; p < q; p ++) {
if (p->Ref && p->Ref >= from && p->Ref < upto) {
p->Ref = 0;
delete p->Txt;
p->Txt = NULL;
}
}
}
2011-06-13 09:57:24 +00:00
int TEXT::Find(int ref) {
HAN *p, * q;
int i = 0;
for (p = Cache, q = p + Size; p < q; p ++) {
if (p->Ref == ref)
break;
2011-06-13 11:07:45 +00:00
else
2011-06-13 09:57:24 +00:00
++i;
}
return i;
}
2011-06-13 09:57:24 +00:00
void TEXT::Preload(int from, int upto) {
INI_FILE tf = FileName;
if (! tf.Error) {
HAN *CacheLim = Cache + Size;
char line[LINE_MAX + 1];
int n;
while ((n = tf.Read((uint8 *)line)) != 0) {
char *s;
int ref;
if (line[n - 1] == '\n')
line[-- n] = '\0';
if ((s = strtok(line, " =,;/\t\n")) == NULL)
continue;
if (! IsDigit(*s))
continue;
ref = atoi(s);
if (ref && ref >= from && ref < upto) {
HAN *p;
p = &Cache[Find(ref)];
if (p < CacheLim) {
delete[] p->Txt;
p->Txt = NULL;
2011-06-13 11:07:45 +00:00
} else
2011-06-13 09:57:24 +00:00
p = &Cache[Find(0)];
2011-06-13 11:07:45 +00:00
if (p >= CacheLim)
2011-06-13 09:57:24 +00:00
break;
s += strlen(s);
2011-06-13 11:07:45 +00:00
if (s < line + n)
2011-06-13 09:57:24 +00:00
++s;
2011-06-13 11:07:45 +00:00
if ((p->Txt = new char[strlen(s) + 1]) == NULL)
2011-06-13 09:57:24 +00:00
break;
p->Ref = ref;
strcpy(p->Txt, s);
}
}
}
}
2011-06-13 09:57:24 +00:00
char *TEXT::Load(int idx, int ref) {
INI_FILE tf = FileName;
if (! tf.Error) {
HAN *p = &Cache[idx];
char line[LINE_MAX + 1];
int n;
while ((n = tf.Read((uint8 *)line)) != 0) {
char *s;
2011-06-13 11:07:45 +00:00
if (line[n - 1] == '\n')
2011-06-13 09:57:24 +00:00
line[-- n] = '\0';
2011-06-13 11:07:45 +00:00
if ((s = strtok(line, " =,;/\t\n")) == NULL)
2011-06-13 09:57:24 +00:00
continue;
2011-06-13 11:07:45 +00:00
if (! IsDigit(*s))
2011-06-13 09:57:24 +00:00
continue;
int r = atoi(s);
2011-06-13 11:07:45 +00:00
if (r < ref)
2011-06-13 09:57:24 +00:00
continue;
2011-06-13 11:07:45 +00:00
if (r > ref)
2011-06-13 09:57:24 +00:00
break;
// (r == ref)
s += strlen(s);
2011-06-13 11:07:45 +00:00
if (s < line + n)
2011-06-13 09:57:24 +00:00
++s;
p->Ref = ref;
2011-06-13 11:07:45 +00:00
if ((p->Txt = new char[strlen(s) + 1]) == NULL)
2011-06-13 09:57:24 +00:00
return NULL;
return strcpy(p->Txt, s);
}
}
2011-06-13 09:57:24 +00:00
return NULL;
}
char *TEXT::getText(int ref) {
2011-06-13 09:57:24 +00:00
int i;
2011-06-13 11:07:45 +00:00
if ((i = Find(ref)) < Size)
2011-06-13 09:57:24 +00:00
return Cache[i].Txt;
2011-06-13 09:57:24 +00:00
if ((i = Find(0)) >= Size) {
Clear(SYSTXT_MAX); // clear non-system
if ((i = Find(0)) >= Size) {
Clear(); // clear all
i = 0;
}
}
2011-06-13 09:57:24 +00:00
return Load(i, ref);
}
2011-06-13 09:57:24 +00:00
void Say(const char *txt, SPRITE *spr) {
KillText();
Talk = new TALK(txt, ROUND);
if (Talk) {
bool east = spr->Flags.East;
int x = (east) ? (spr->X + spr->W - 2) : (spr->X + 2);
int y = spr->Y + 2;
SPRITE *spike = new SPRITE(SP);
uint16 sw = spike->W;
if (east) {
2011-06-13 11:07:45 +00:00
if (x + sw + TEXT_RD + 5 >= SCR_WID)
2011-06-13 09:57:24 +00:00
east = false;
} else {
2011-06-13 11:07:45 +00:00
if (x <= 5 + TEXT_RD + sw)
2011-06-13 09:57:24 +00:00
east = true;
}
x = (east) ? (spr->X + spr->W - 2) : (spr->X + 2 - sw);
2011-06-13 11:07:45 +00:00
if (spr->Ref == 1)
x += ((east) ? -10 : 10); // Hero
2011-06-13 09:57:24 +00:00
Talk->Flags.Kill = true;
Talk->Flags.BDel = true;
Talk->SetName(Text->getText(SAY_NAME));
2011-06-13 09:57:24 +00:00
Talk->Goto(x - (Talk->W - sw) / 2 - 3 + 6 * east, y - spike->H - Talk->H + 1);
Talk->Z = 125;
Talk->Ref = SAY_REF;
spike->Goto(x, Talk->Y + Talk->H - 1);
spike->Z = 126;
spike->Flags.Slav = true;
spike->Flags.Kill = true;
spike->SetName(Text->getText(SAY_NAME));
2011-06-13 09:57:24 +00:00
spike->Step(east);
spike->Ref = SAY_REF;
Vga->ShowQ->Insert(Talk, Vga->ShowQ->Last());
Vga->ShowQ->Insert(spike, Vga->ShowQ->Last());
}
}
2011-06-13 09:57:24 +00:00
void Inf(const char *txt) {
KillText();
Talk = new TALK(txt, RECT);
if (Talk) {
Talk->Flags.Kill = true;
Talk->Flags.BDel = true;
Talk->SetName(Text->getText(INF_NAME));
2011-06-13 09:57:24 +00:00
Talk->Center();
Talk->Goto(Talk->X, Talk->Y - 20);
Talk->Z = 126;
Talk->Ref = INF_REF;
Vga->ShowQ->Insert(Talk, Vga->ShowQ->Last());
2011-06-13 09:57:24 +00:00
}
}
2011-06-13 09:57:24 +00:00
void SayTime(SPRITE *spr) {
/*
static char t[] = "00:00";
struct time ti;
gettime(&ti);
wtom(ti.ti_hour, t+0, 10, 2);
wtom(ti.ti_min, t+3, 10, 2);
Say((*t == '0') ? (t+1) : t, spr);
*/
warning("STUB: SayTime");
}
2011-06-13 09:57:24 +00:00
void KillText(void) {
if (Talk) {
SNPOST_(SNKILL, -1, 0, Talk);
Talk = NULL;
}
}
2011-06-10 20:57:09 +00:00
} // End of namespace CGE