2015-03-16 23:52:08 -04:00
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sherlock/screen.h"
|
|
|
|
#include "sherlock/sherlock.h"
|
|
|
|
#include "common/system.h"
|
|
|
|
#include "graphics/palette.h"
|
|
|
|
|
|
|
|
namespace Sherlock {
|
|
|
|
|
|
|
|
Screen::Screen(SherlockEngine *vm) : Surface(SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCREEN_HEIGHT), _vm(vm),
|
2015-03-17 23:09:04 -04:00
|
|
|
_backBuffer(SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCREEN_HEIGHT),
|
2015-03-16 23:52:08 -04:00
|
|
|
_backBuffer2(SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCREEN_HEIGHT) {
|
2015-03-18 19:02:17 -04:00
|
|
|
_transitionSeed = 1;
|
2015-03-18 22:32:41 -04:00
|
|
|
_fadeStyle = false;
|
2015-03-20 19:44:32 -04:00
|
|
|
Common::fill(&_cMap[0], &_cMap[PALETTE_SIZE], 0);
|
|
|
|
Common::fill(&_sMap[0], &_sMap[PALETTE_SIZE], 0);
|
2015-03-16 23:52:08 -04:00
|
|
|
setFont(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Screen::setFont(int fontNumber) {
|
|
|
|
_fontNumber = fontNumber;
|
|
|
|
Common::String fname = Common::String::format("FONT%d.VGS", fontNumber);
|
|
|
|
Common::SeekableReadStream *stream = _vm->_res->load(fname);
|
|
|
|
|
|
|
|
debug("TODO: Loading font %s, size - %d", fname.c_str(), stream->size());
|
|
|
|
|
|
|
|
delete stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Screen::update() {
|
2015-03-17 00:01:12 -04:00
|
|
|
// Merge the dirty rects
|
|
|
|
mergeDirtyRects();
|
|
|
|
|
|
|
|
// Loop through copying dirty areas to the physical screen
|
|
|
|
Common::List<Common::Rect>::iterator i;
|
|
|
|
for (i = _dirtyRects.begin(); i != _dirtyRects.end(); ++i) {
|
|
|
|
const Common::Rect &r = *i;
|
|
|
|
const byte *srcP = (const byte *)getBasePtr(r.left, r.top);
|
|
|
|
g_system->copyRectToScreen(srcP, this->pitch, r.left, r.top,
|
|
|
|
r.width(), r.height());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal the physical screen to update
|
2015-03-16 23:52:08 -04:00
|
|
|
g_system->updateScreen();
|
2015-03-17 00:01:12 -04:00
|
|
|
_dirtyRects.clear();
|
2015-03-16 23:52:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Screen::getPalette(byte palette[PALETTE_SIZE]) {
|
|
|
|
g_system->getPaletteManager()->grabPalette(palette, 0, PALETTE_COUNT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Screen::setPalette(const byte palette[PALETTE_SIZE]) {
|
|
|
|
g_system->getPaletteManager()->setPalette(palette, 0, PALETTE_COUNT);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Screen::equalizePalette(const byte palette[PALETTE_SIZE]) {
|
|
|
|
int total = 0;
|
|
|
|
byte tempPalette[PALETTE_SIZE];
|
|
|
|
getPalette(tempPalette);
|
|
|
|
|
|
|
|
// For any palette component that doesn't already match the given destination
|
|
|
|
// palette, change by 1 towards the reference palette component
|
|
|
|
for (int idx = 0; idx < PALETTE_SIZE; ++idx) {
|
|
|
|
if (tempPalette[idx] > palette[idx])
|
|
|
|
{
|
2015-03-17 08:31:29 -04:00
|
|
|
tempPalette[idx] = MAX((int)palette[idx], (int)tempPalette[idx] - 4);
|
2015-03-16 23:52:08 -04:00
|
|
|
++total;
|
|
|
|
} else if (tempPalette[idx] < palette[idx]) {
|
2015-03-17 08:31:29 -04:00
|
|
|
tempPalette[idx] = MIN((int)palette[idx], (int)tempPalette[idx] + 4);
|
2015-03-16 23:52:08 -04:00
|
|
|
++total;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (total > 0)
|
|
|
|
// Palette changed, so reload it
|
|
|
|
setPalette(tempPalette);
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2015-03-17 23:09:04 -04:00
|
|
|
/**
|
|
|
|
* Fade out the palette to black
|
|
|
|
*/
|
|
|
|
void Screen::fadeToBlack(int speed) {
|
2015-03-16 23:52:08 -04:00
|
|
|
byte tempPalette[PALETTE_SIZE];
|
2015-03-17 23:09:04 -04:00
|
|
|
Common::fill(&tempPalette[0], &tempPalette[PALETTE_SIZE], 0);
|
2015-03-16 23:52:08 -04:00
|
|
|
|
2015-03-17 23:09:04 -04:00
|
|
|
while (equalizePalette(tempPalette)) {
|
|
|
|
_vm->_events->delay(15 * speed);
|
|
|
|
}
|
2015-03-16 23:52:08 -04:00
|
|
|
|
2015-03-17 23:09:04 -04:00
|
|
|
setPalette(tempPalette);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fade in a given palette
|
|
|
|
*/
|
|
|
|
void Screen::fadeIn(const byte palette[PALETTE_SIZE], int speed) {
|
|
|
|
int count = 50;
|
|
|
|
while (equalizePalette(palette) && --count) {
|
|
|
|
_vm->_events->delay(15 * speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
setPalette(palette);
|
2015-03-16 23:52:08 -04:00
|
|
|
}
|
|
|
|
|
2015-03-17 00:01:12 -04:00
|
|
|
/**
|
|
|
|
* Adds a rectangle to the list of modified areas of the screen during the
|
|
|
|
* current frame
|
|
|
|
*/
|
|
|
|
void Screen::addDirtyRect(const Common::Rect &r) {
|
|
|
|
_dirtyRects.push_back(r);
|
|
|
|
assert(r.isValidRect() && r.width() > 0 && r.height() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges together overlapping dirty areas of the screen
|
|
|
|
*/
|
|
|
|
void Screen::mergeDirtyRects() {
|
|
|
|
Common::List<Common::Rect>::iterator rOuter, rInner;
|
|
|
|
|
|
|
|
// Ensure dirty rect list has at least two entries
|
|
|
|
rOuter = _dirtyRects.begin();
|
|
|
|
for (int i = 0; i < 2; ++i, ++rOuter) {
|
|
|
|
if (rOuter == _dirtyRects.end())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the dirty rect list to find any rects to merge
|
|
|
|
for (rOuter = _dirtyRects.begin(); rOuter != _dirtyRects.end(); ++rOuter) {
|
|
|
|
rInner = rOuter;
|
|
|
|
while (++rInner != _dirtyRects.end()) {
|
|
|
|
|
|
|
|
if ((*rOuter).intersects(*rInner)) {
|
|
|
|
// these two rectangles overlap or
|
|
|
|
// are next to each other - merge them
|
|
|
|
|
|
|
|
unionRectangle(*rOuter, *rOuter, *rInner);
|
|
|
|
|
|
|
|
// remove the inner rect from the list
|
|
|
|
_dirtyRects.erase(rInner);
|
|
|
|
|
|
|
|
// move back to beginning of list
|
|
|
|
rInner = rOuter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the union of two dirty area rectangles
|
|
|
|
*/
|
|
|
|
bool Screen::unionRectangle(Common::Rect &destRect, const Common::Rect &src1, const Common::Rect &src2) {
|
|
|
|
destRect = src1;
|
|
|
|
destRect.extend(src2);
|
|
|
|
|
|
|
|
return !destRect.isEmpty();
|
|
|
|
}
|
|
|
|
|
2015-03-17 23:09:04 -04:00
|
|
|
/**
|
|
|
|
* Do a random pixel transition in from _backBuffer surface to the screen
|
|
|
|
*/
|
|
|
|
void Screen::randomTransition() {
|
|
|
|
EventsManager &events = *_vm->_events;
|
2015-03-18 19:02:17 -04:00
|
|
|
const int TRANSITION_MULTIPLIER = 0x15a4e35;
|
|
|
|
_dirtyRects.clear();
|
2015-03-17 23:09:04 -04:00
|
|
|
|
|
|
|
for (int idx = 0; idx <= 65535; ++idx) {
|
2015-03-18 19:02:17 -04:00
|
|
|
_transitionSeed = _transitionSeed * TRANSITION_MULTIPLIER + 1;
|
|
|
|
int offset = _transitionSeed & 65535;
|
|
|
|
|
|
|
|
if (offset < (SHERLOCK_SCREEN_WIDTH * SHERLOCK_SCREEN_HEIGHT))
|
|
|
|
*((byte *)getPixels() + offset) = *((const byte *)_backBuffer.getPixels() + offset);
|
2015-03-17 23:09:04 -04:00
|
|
|
|
|
|
|
if (idx != 0 && (idx % 100) == 0) {
|
2015-03-18 19:02:17 -04:00
|
|
|
// Ensure there's a full screen dirty rect for the next frame update
|
|
|
|
if (_dirtyRects.empty())
|
|
|
|
addDirtyRect(Common::Rect(0, 0, this->w, this->h));
|
|
|
|
|
|
|
|
events.pollEvents();
|
|
|
|
events.delay(1);
|
2015-03-17 23:09:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure everything has been transferred
|
|
|
|
blitFrom(_backBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transition to the surface from _backBuffer using a vertical transition
|
|
|
|
*/
|
|
|
|
void Screen::verticalTransition() {
|
|
|
|
EventsManager &events = *_vm->_events;
|
|
|
|
|
|
|
|
byte table[SHERLOCK_SCREEN_WIDTH];
|
|
|
|
Common::fill(&table[0], &table[SHERLOCK_SCREEN_WIDTH], 0);
|
|
|
|
|
|
|
|
for (int yp = 0; yp < SHERLOCK_SCREEN_HEIGHT; ++yp) {
|
|
|
|
for (int xp = 0; xp < SHERLOCK_SCREEN_WIDTH; ++xp) {
|
|
|
|
int temp = (table[xp] >= 197) ? SHERLOCK_SCREEN_HEIGHT - table[xp] :
|
|
|
|
_vm->getRandomNumber(3) + 1;
|
|
|
|
|
|
|
|
if (temp) {
|
|
|
|
blitFrom(_backBuffer, Common::Point(xp, table[xp]),
|
|
|
|
Common::Rect(xp, table[xp], xp + 1, table[xp] + temp));
|
|
|
|
table[xp] += temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
events.delay(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:52:08 -04:00
|
|
|
} // End of namespace Sherlock
|