diff options
author | Strangerke | 2011-09-11 14:19:46 +0200 |
---|---|---|
committer | Strangerke | 2011-09-11 14:19:46 +0200 |
commit | 08d87130aa31606bfb62ef8a574e161a30866225 (patch) | |
tree | e5c9df626c8896a892f49392b996f7b0f1c99d12 | |
parent | 3715d6d444792fc93fe98df4f0274f0be846eed1 (diff) | |
download | scummvm-rg350-08d87130aa31606bfb62ef8a574e161a30866225.tar.gz scummvm-rg350-08d87130aa31606bfb62ef8a574e161a30866225.tar.bz2 scummvm-rg350-08d87130aa31606bfb62ef8a574e161a30866225.zip |
CGE: Use EncryptedStream in Text: rewrite caching, remove some methods.
-rw-r--r-- | engines/cge/cge.cpp | 2 | ||||
-rw-r--r-- | engines/cge/cge_main.cpp | 5 | ||||
-rw-r--r-- | engines/cge/text.cpp | 138 | ||||
-rw-r--r-- | engines/cge/text.h | 9 |
4 files changed, 54 insertions, 100 deletions
diff --git a/engines/cge/cge.cpp b/engines/cge/cge.cpp index 1b9fcf64ed..ade3071497 100644 --- a/engines/cge/cge.cpp +++ b/engines/cge/cge.cpp @@ -91,7 +91,7 @@ void CGEEngine::init() { Cluster::init(this); // Initialise engine objects - _text = new Text(this, "CGE", 128); + _text = new Text(this, "CGE"); _vga = new Vga(); _sys = new System(this); _pocLight = new PocLight(this); diff --git a/engines/cge/cge_main.cpp b/engines/cge/cge_main.cpp index b9033a81ee..d3c88845c2 100644 --- a/engines/cge/cge_main.cpp +++ b/engines/cge/cge_main.cpp @@ -623,7 +623,6 @@ void CGEEngine::caveUp() { showBak(BakRef); loadMapping(); - _text->preload(BakRef, BakRef + 1000); Sprite *spr = _vga->_spareQ->first(); while (spr) { Sprite *n = spr->_next; @@ -689,8 +688,6 @@ void CGEEngine::caveDown() { } spr = n; } - - _text->clear(1000); } void CGEEngine::xCave() { @@ -1305,8 +1302,6 @@ void CGEEngine::runGame() { if (_eventManager->_quitFlag) return; - _text->clear(); - _text->preload(100, 1000); loadHeroXY(); _cavLight->_flags._tran = true; diff --git a/engines/cge/text.cpp b/engines/cge/text.cpp index 25af8eccfc..7e574a00bc 100644 --- a/engines/cge/text.cpp +++ b/engines/cge/text.cpp @@ -31,22 +31,26 @@ #include "cge/game.h" #include "cge/snail.h" #include "cge/cge_main.h" +#include "common/str.h" namespace CGE { Text *_text; Talk *_talk = NULL; -Text::Text(CGEEngine *vm, const char *fname, int size) : _vm(vm) { - _cache = new Handler[size]; +Text::Text(CGEEngine *vm, const char *fname) : _vm(vm) { mergeExt(_fileName, fname, kSayExt); if (!_cat->exist(_fileName)) error("No talk (%s)\n", _fileName); + int16 txtCount = count() + 1; + warning("Number of texts: %d", txtCount); - for (_size = 0; _size < size; _size++) { + _cache = new Handler[txtCount]; + for (_size = 0; _size < txtCount; _size++) { _cache[_size]._ref = 0; _cache[_size]._text = NULL; } + load(); } Text::~Text() { @@ -54,125 +58,81 @@ Text::~Text() { delete[] _cache; } -void Text::clear(int from, int upto) { - for (Handler *p = _cache, *q = p + _size; p < q; p++) { - if (p->_ref && p->_ref >= from && p->_ref < upto) { - p->_ref = 0; - delete[] p->_text; - p->_text = NULL; - } - } -} - -int Text::find(int ref) { - int i = 0; - for (Handler *p = _cache, *q = p + _size; p < q; p++) { - if (p->_ref == ref) - break; - else - i++; - } - return i; -} - - -void Text::preload(int from, int upto) { - VFile tf = _fileName; +int16 Text::count() { + EncryptedStream tf = _fileName; if (tf._error) - return; - - Handler *CacheLim = _cache + _size; - char line[kLineMax + 1]; - int n; + return NULL; - while ((n = tf.read((uint8 *)line)) != 0) { - if (line[n - 1] == '\n') - line[--n] = '\0'; + Common::String line; + char tmpStr[kLineMax + 1]; + int n, count = 0; + for (line = tf._readStream->readLine(); !tf._readStream->eos(); line = tf._readStream->readLine()) { + n = line.size(); char *s; - if ((s = strtok(line, " =,;/\t\n")) == NULL) + + strcpy(tmpStr, line.c_str()); + if ((s = strtok(tmpStr, " =,;/\t\n")) == NULL) continue; if (!isdigit(*s)) continue; - int ref = atoi(s); - if (ref && ref >= from && ref < upto) { - Handler *p = &_cache[find(ref)]; - - if (p < CacheLim) { - delete[] p->_text; - p->_text = NULL; - } else - p = &_cache[find(0)]; - - if (p >= CacheLim) - break; - - s += strlen(s); - if (s < line + n) - ++s; - if ((p->_text = new char[strlen(s) + 1]) == NULL) - break; + count++; + } + return count; +} - p->_ref = ref; - strcpy(p->_text, s); +void Text::clear() { + for (Handler *p = _cache, *q = p + _size; p < q; p++) { + if (p->_ref) { + p->_ref = 0; + delete[] p->_text; + p->_text = NULL; } } } +void Text::load() { + EncryptedStream tf = _fileName; + assert(!tf._error); -char *Text::load(int idx, int ref) { - VFile tf = _fileName; - if (tf._error) - return NULL; - - char line[kLineMax + 1]; - int n; + Common::String line; + char tmpStr[kLineMax + 1]; + int idx; - while ((n = tf.read((uint8 *)line)) != 0) { + for (idx = 0, line = tf._readStream->readLine(); !tf._readStream->eos(); line = tf._readStream->readLine()) { + int n = line.size(); char *s; - if (line[n - 1] == '\n') - line[-- n] = '\0'; - if ((s = strtok(line, " =,;/\t\n")) == NULL) + strcpy(tmpStr, line.c_str()); + if ((s = strtok(tmpStr, " =,;/\t\n")) == NULL) continue; if (!isdigit(*s)) continue; int r = atoi(s); - if (r < ref) - continue; - if (r > ref) - break; - // (r == ref) s += strlen(s); - if (s < line + n) + if (s < tmpStr + n) ++s; - Handler *p = &_cache[idx]; - p->_ref = ref; - - if ((p->_text = new char[strlen(s) + 1]) == NULL) - return NULL; - return strcpy(p->_text, s); + _cache[idx]._ref = r; + _cache[idx]._text = new char[strlen(s) + 1]; + strcpy(_cache[idx]._text, s); + idx++; } - return NULL; } char *Text::getText(int ref) { int i; - if ((i = find(ref)) < _size) + for (i = 0; (i < _size) && (_cache[i]._ref != ref); i++) + ; + + if (i < _size) return _cache[i]._text; - if ((i = find(0)) >= _size) { - clear(kSysTextMax); // clear non-system - if ((i = find(0)) >= _size) { - clear(); // clear all - i = 0; - } - } - return load(i, ref); + warning("getText: Unable to find ref %d", ref); + return NULL; } void Text::say(const char *text, Sprite *spr) { diff --git a/engines/cge/text.h b/engines/cge/text.h index 30bb77faae..668f307f72 100644 --- a/engines/cge/text.h +++ b/engines/cge/text.h @@ -48,13 +48,12 @@ class Text { } *_cache; int _size; char _fileName[kPathMax]; - char *load(int idx, int ref); - int find(int ref); + void load(); + int16 count(); public: - Text(CGEEngine *vm, const char *fname, int size); + Text(CGEEngine *vm, const char *fname); ~Text(); - void clear(int from = 1, int upto = 0x7FFF); - void preload(int from = 1, int upto = 0x7FFF); + void clear(); char *getText(int ref); void say(const char *text, Sprite *spr); void sayTime(Sprite *spr); |