scummvm/engines/sludge/speech.cpp

311 lines
8.5 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.
*
*/
2017-06-05 19:10:47 +02:00
#include "sludge/allfiles.h"
#include "sludge/backdrop.h"
2017-12-19 21:24:31 +01:00
#include "sludge/fonttext.h"
#include "sludge/freeze.h"
2017-07-20 00:41:13 +02:00
#include "sludge/graphics.h"
2017-12-19 21:24:31 +01:00
#include "sludge/moreio.h"
#include "sludge/newfatal.h"
2017-06-05 19:10:47 +02:00
#include "sludge/objtypes.h"
#include "sludge/people.h"
2017-12-19 21:24:31 +01:00
#include "sludge/region.h"
2017-07-18 19:50:45 +02:00
#include "sludge/sludge.h"
2017-12-19 21:24:31 +01:00
#include "sludge/sludger.h"
2017-06-05 19:10:47 +02:00
#include "sludge/sound.h"
2017-12-19 21:24:31 +01:00
#include "sludge/speech.h"
#include "sludge/sprbanks.h"
#include "sludge/sprites.h"
2017-05-26 21:25:11 +02:00
namespace Sludge {
2017-12-19 21:24:31 +01:00
void SpeechManager::init() {
_speechMode = 0;
_speechSpeed = 1;
_speech = new SpeechStruct;
if (checkNew(_speech)) {
_speech->currentTalker = NULL;
_speech->allSpeech = NULL;
_speech->speechY = 0;
_speech->lastFile = -1;
}
}
2017-12-19 21:24:31 +01:00
void SpeechManager::kill() {
if (!_speech)
return;
2017-12-19 21:24:31 +01:00
if (_speech->lastFile != -1) {
_vm->_soundMan->huntKillSound(_speech->lastFile);
_speech->lastFile = -1;
}
2017-12-19 21:24:31 +01:00
if (_speech->currentTalker) {
2018-04-15 21:10:02 +02:00
g_sludge->_peopleMan->makeSilent(*(_speech->currentTalker));
2017-12-19 21:24:31 +01:00
_speech->currentTalker = NULL;
}
SpeechLine *killMe;
2017-12-19 21:24:31 +01:00
while (_speech->allSpeech) {
killMe = _speech->allSpeech;
_speech->allSpeech = _speech->allSpeech->next;
delete killMe;
}
}
2017-12-19 21:24:31 +01:00
void SpeechManager::setObjFontColour(ObjectType *t) {
_speech->talkCol.setColor(t->r, t->g, t->b);
}
2017-12-19 21:24:31 +01:00
void SpeechManager::addSpeechLine(const Common::String &theLine, int x, int &offset) {
2017-07-20 00:41:13 +02:00
float cameraZoom = g_sludge->_gfxMan->getCamZoom();
2017-07-21 00:11:17 +02:00
int halfWidth = (g_sludge->_txtMan->stringWidth(theLine) >> 1) / cameraZoom;
int xx1 = x - (halfWidth);
int xx2 = x + (halfWidth);
SpeechLine *newLine = new SpeechLine;
checkNew(newLine);
2017-12-19 21:24:31 +01:00
newLine->next = _speech->allSpeech;
newLine->textLine.clear();
newLine->textLine = theLine;
newLine->x = xx1;
2017-12-19 21:24:31 +01:00
_speech->allSpeech = newLine;
if ((xx1 < 5) && (offset < (5 - xx1))) {
offset = 5 - xx1;
2017-07-20 00:41:13 +02:00
} else if ((xx2 >= ((float) g_system->getWidth() / cameraZoom) - 5)
&& (offset > (((float) g_system->getWidth() / cameraZoom) - 5 - xx2))) {
offset = ((float) g_system->getWidth() / cameraZoom) - 5 - xx2;
}
}
2017-12-19 21:24:31 +01:00
int SpeechManager::isThereAnySpeechGoingOn() {
return _speech->allSpeech ? _speech->lookWhosTalking : -1;
}
2017-12-19 21:24:31 +01:00
int SpeechManager::getLastSpeechSound() {
return _vm->_soundMan->findInSoundCache(_speech->lastFile);
}
int SpeechManager::wrapSpeechXY(const Common::String &theText, int x, int y, int wrap, int sampleFile) {
2017-07-20 00:41:13 +02:00
float cameraZoom = g_sludge->_gfxMan->getCamZoom();
2017-07-21 00:11:17 +02:00
int fontHeight = g_sludge->_txtMan->getFontHeight();
2017-07-20 00:41:13 +02:00
int cameraY = g_sludge->_gfxMan->getCamY();
int a, offset = 0;
2017-12-19 21:24:31 +01:00
kill();
2017-12-19 21:24:31 +01:00
int speechTime = (theText.size() + 20) * _speechSpeed;
2017-05-29 08:02:59 +02:00
if (speechTime < 1)
speechTime = 1;
if (sampleFile != -1) {
2017-12-19 21:24:31 +01:00
if (_speechMode >= 1) {
2017-07-20 23:18:05 +02:00
if (g_sludge->_soundMan->startSound(sampleFile, false)) {
speechTime = -10;
2017-12-19 21:24:31 +01:00
_speech->lastFile = sampleFile;
if (_speechMode == 2) return -10;
}
2017-07-15 17:33:10 +02:00
}
}
2017-12-19 21:24:31 +01:00
_speech->speechY = y;
char *tmp, *txt;
tmp = txt = createCString(theText);
while ((int)strlen(txt) > wrap) {
a = wrap;
while (txt[a] != ' ') {
2017-05-29 08:02:59 +02:00
a--;
if (a == 0) {
a = wrap;
break;
}
}
txt[a] = 0;
addSpeechLine(txt, x, offset);
txt[a] = ' ';
txt += a + 1;
y -= fontHeight / cameraZoom;
}
addSpeechLine(txt, x, offset);
y -= fontHeight / cameraZoom;
delete []tmp;
2017-05-29 08:02:59 +02:00
if (y < 0)
2017-12-19 21:24:31 +01:00
_speech->speechY -= y;
else if (_speech->speechY > cameraY + (float) (g_system->getHeight() - fontHeight / 3) / cameraZoom)
_speech->speechY = cameraY
2017-07-20 00:41:13 +02:00
+ (float) (g_system->getHeight() - fontHeight / 3) / cameraZoom;
if (offset) {
2017-12-19 21:24:31 +01:00
SpeechLine *viewLine = _speech->allSpeech;
while (viewLine) {
viewLine->x += offset;
viewLine = viewLine->next;
}
}
return speechTime;
}
2017-12-19 21:24:31 +01:00
int SpeechManager::wrapSpeechPerson(const Common::String &theText, OnScreenPerson &thePerson, int sampleFile, bool animPerson) {
2017-07-20 00:41:13 +02:00
int cameraX = g_sludge->_gfxMan->getCamX();
int cameraY = g_sludge->_gfxMan->getCamY();
2017-05-29 08:02:59 +02:00
int i = wrapSpeechXY(theText, thePerson.x - cameraX,
thePerson.y - cameraY
- (thePerson.scale * (thePerson.height - thePerson.floaty))
- thePerson.thisType->speechGap,
thePerson.thisType->wrapSpeech, sampleFile);
if (animPerson) {
2018-04-15 21:10:02 +02:00
g_sludge->_peopleMan->makeTalker(thePerson);
2017-12-19 21:24:31 +01:00
_speech->currentTalker = &thePerson;
}
return i;
}
2017-12-19 21:24:31 +01:00
int SpeechManager::wrapSpeech(const Common::String &theText, int objT, int sampleFile, bool animPerson) {
int i;
2017-07-20 00:41:13 +02:00
int cameraX = g_sludge->_gfxMan->getCamX();
int cameraY = g_sludge->_gfxMan->getCamY();
2017-12-19 21:24:31 +01:00
_speech->lookWhosTalking = objT;
2018-04-15 21:10:02 +02:00
OnScreenPerson *thisPerson = g_sludge->_peopleMan->findPerson(objT);
if (thisPerson) {
setObjFontColour(thisPerson->thisType);
2017-05-29 08:02:59 +02:00
i = wrapSpeechPerson(theText, *thisPerson, sampleFile, animPerson);
} else {
2018-04-15 00:21:17 +02:00
ScreenRegion *thisRegion = g_sludge->_regionMan->getRegionForObject(objT);
if (thisRegion) {
setObjFontColour(thisRegion->thisType);
2017-05-29 08:02:59 +02:00
i = wrapSpeechXY(theText,
((thisRegion->x1 + thisRegion->x2) >> 1) - cameraX,
thisRegion->y1 - thisRegion->thisType->speechGap - cameraY,
thisRegion->thisType->wrapSpeech, sampleFile);
} else {
2017-07-18 19:50:45 +02:00
ObjectType *temp = g_sludge->_objMan->findObjectType(objT);
setObjFontColour(temp);
2017-07-20 00:41:13 +02:00
i = wrapSpeechXY(theText, g_system->getWidth() >> 1, 10, temp->wrapSpeech,
2017-05-29 08:02:59 +02:00
sampleFile);
}
}
return i;
}
2017-12-19 21:24:31 +01:00
void SpeechManager::display() {
2017-07-20 00:41:13 +02:00
float cameraZoom = g_sludge->_gfxMan->getCamZoom();
2017-07-21 00:11:17 +02:00
int fontHeight = g_sludge->_txtMan->getFontHeight();
2017-12-19 21:24:31 +01:00
int viewY = _speech->speechY;
SpeechLine *viewLine = _speech->allSpeech;
while (viewLine) {
2017-12-19 21:24:31 +01:00
g_sludge->_txtMan->pasteString(viewLine->textLine, viewLine->x, viewY, _speech->talkCol);
viewY -= fontHeight / cameraZoom;
viewLine = viewLine->next;
}
}
2017-12-19 21:24:31 +01:00
void SpeechManager::save(Common::WriteStream *stream) {
stream->writeByte(_speechMode);
SpeechLine *viewLine = _speech->allSpeech;
2017-12-19 21:24:31 +01:00
stream->writeByte(_speech->talkCol.originalRed);
stream->writeByte(_speech->talkCol.originalGreen);
stream->writeByte(_speech->talkCol.originalBlue);
2017-12-19 21:24:31 +01:00
stream->writeFloatLE(_speechSpeed);
// Write y co-ordinate
2017-12-19 21:24:31 +01:00
stream->writeUint16BE(_speech->speechY);
// Write which character's talking
2017-12-19 21:24:31 +01:00
stream->writeUint16BE(_speech->lookWhosTalking);
if (_speech->currentTalker) {
stream->writeByte(1);
2017-12-19 21:24:31 +01:00
stream->writeUint16BE(_speech->currentTalker->thisType->objectNum);
} else {
stream->writeByte(0);
}
// Write what's being said
while (viewLine) {
stream->writeByte(1);
writeString(viewLine->textLine, stream);
stream->writeUint16BE(viewLine->x);
viewLine = viewLine->next;
}
stream->writeByte(0);
}
2017-12-19 21:24:31 +01:00
bool SpeechManager::load(Common::SeekableReadStream *stream) {
// read speech mode
_speechMode = stream->readByte();
_speech->currentTalker = nullptr;
kill();
byte r = stream->readByte();
byte g = stream->readByte();
byte b = stream->readByte();
_speech->talkCol.setColor(r, g, b);
2017-12-19 21:24:31 +01:00
_speechSpeed = stream->readFloatLE();
// Read y co-ordinate
2017-12-19 21:24:31 +01:00
_speech->speechY = stream->readUint16BE();
// Read which character's talking
2017-12-19 21:24:31 +01:00
_speech->lookWhosTalking = stream->readUint16BE();
if (stream->readByte()) {
2018-04-15 21:10:02 +02:00
_speech->currentTalker = g_sludge->_peopleMan->findPerson(stream->readUint16BE());
} else {
2017-12-19 21:24:31 +01:00
_speech->currentTalker = NULL;
}
// Read what's being said
2017-12-19 21:24:31 +01:00
SpeechLine **viewLine = &_speech->allSpeech;
SpeechLine *newOne;
2017-12-19 21:24:31 +01:00
_speech->lastFile = -1;
while (stream->readByte()) {
newOne = new SpeechLine;
2017-12-19 21:24:31 +01:00
if (!checkNew(newOne))
return false;
newOne->textLine = readString(stream);
newOne->x = stream->readUint16BE();
newOne->next = NULL;
2017-12-19 21:24:31 +01:00
(*viewLine) = newOne;
viewLine = &(newOne->next);
}
return true;
}
2017-05-26 21:25:11 +02:00
2017-12-19 21:24:31 +01:00
void SpeechManager::freeze(FrozenStuffStruct *frozenStuff) {
frozenStuff->speech = _speech;
init();
}
void SpeechManager::restore(FrozenStuffStruct *frozenStuff) {
kill();
delete _speech;
_speech = frozenStuff->speech;
}
2017-05-26 21:25:11 +02:00
} // End of namespace Sludge