1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "graphics/palette.h"
#include "neverhood/resource.h"
#include "neverhood/resourceman.h"
#include "neverhood/screen.h"
#include "neverhood/subtitles.h"
namespace Neverhood {
namespace {
void drawSubtitles(Graphics::Surface *surf, const Common::String &str, const SubtitleGlyph *subfont,
int x0, byte outlineColor, byte alphaColor) {
if (!surf || surf->h < SubtitlePlayer::kSubtitleCharHeight || !subfont)
return;
byte *dest0 = (byte*)surf->getBasePtr(0, 0);
int lastx = MIN<int>(str.size() * SubtitlePlayer::kSubtitleCharWidth + x0 + 1, surf->w);
for (int16 yc = 0; yc < SubtitlePlayer::kSubtitleCharHeight; yc++) {
byte *dest = dest0 + yc * surf->pitch;
memset(dest, alphaColor, x0 + 2);
memset(dest + lastx, alphaColor, surf->w - lastx);
}
for (int i = 0; i < (int)str.size() && i * SubtitlePlayer::kSubtitleCharWidth < surf->w; i++) {
byte c = str[i];
byte *dest = dest0 + i * SubtitlePlayer::kSubtitleCharWidth + x0;
for (int16 yc = 0; yc < SubtitlePlayer::kSubtitleCharHeight; yc++) {
byte *row = dest;
// Outline of leftmost pixel
if (*row == alphaColor && (subfont[c].bitmap[yc] & 0x80))
*row = outlineColor;
row++;
for (int16 xc = 0; xc < SubtitlePlayer::kSubtitleCharWidth; xc++, row++) {
if ((subfont[c].bitmap[yc] << xc) & 0x80)
*row = 0xff;
else if ((subfont[c].outline[yc] << xc) & 0x80)
*row = outlineColor;
else if (xc != 0)
*row = alphaColor;
}
// Outline of rightmost pixel
*row = (subfont[c].bitmap[yc] & 0x1) ? outlineColor : alphaColor;
dest += surf->pitch;
}
}
}
}
SubtitlePlayer::SubtitlePlayer(NeverhoodEngine *vm, uint32 fileHash, int width) :
_vm(vm), _haveBottomSubs(false), _haveTopSubs(false), _currentFrame(-1), _isValid(false) {
if (!vm->getSubfont())
return;
_isValid = true;
_bottomSubs.create(width, kSubtitleCharHeight, Graphics::PixelFormat::createFormatCLUT8());
_topSubs.create(width, kSubtitleCharHeight, Graphics::PixelFormat::createFormatCLUT8());
Common::SeekableReadStream *s = vm->_res->createNhcStream(fileHash, kResNhcTypeSubtitles);
while (s && !s->eos()) {
Subtitle sub;
sub.fromFrame = s->readUint32LE();
sub.toFrame = s->readUint32LE();
sub.text = s->readString('\0', 40);
if (!sub.text.empty() && sub.text[0] == '^') {
sub.isTop = true;
sub.text = sub.text.substr(1);
} else {
sub.isTop = false;
}
_subtitles.push_back(sub);
}
delete s;
}
void SubtitlePlayer::renderFrame(uint frameNumber, int centerX) {
// Reuse old rendering if no frame has passed
if (_currentFrame == (int64)frameNumber)
return;
const SubtitleGlyph *subFont = _vm->getSubfont();
if (!subFont)
return;
int screenWidth = _bottomSubs.w;
_haveBottomSubs = false;
_haveTopSubs = false;
Graphics::PaletteLookup palLookup(_vm->_screen->getPaletteData(), 256);
byte outlineColor = palLookup.findBestColor(0, 0, 0);
_alphaColor = 0x77;
if (_alphaColor == outlineColor)
_alphaColor++;
// TODO: Optimize this
for (uint i = 0; i < _subtitles.size(); i++) {
if (frameNumber < _subtitles[i].fromFrame || frameNumber > _subtitles[i].toFrame)
continue;
Common::String curStr = _subtitles[i].text;
if ((int)curStr.size() > (screenWidth - 2) / SubtitlePlayer::kSubtitleCharWidth)
curStr = curStr.substr(0, screenWidth / SubtitlePlayer::kSubtitleCharWidth - 3) + "...";
int width = curStr.size() * SubtitlePlayer::kSubtitleCharWidth + 2;
int startX = MAX(MIN(centerX - width / 2, screenWidth - width), 0);
if (_subtitles[i].isTop) {
drawSubtitles(&_topSubs, curStr, subFont, startX, outlineColor, _alphaColor);
_haveTopSubs = true;
} else {
drawSubtitles(&_bottomSubs, curStr, subFont, startX, outlineColor, _alphaColor);
_haveBottomSubs = true;
}
}
}
}
|