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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
/* 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 "image/bmp.h"
#include "engines/util.h"
#include "engines/nancy/nancy.h"
#include "engines/nancy/graphics.h"
#include "engines/nancy/renderobject.h"
#include "engines/nancy/resource.h"
#include "engines/nancy/cursor.h"
#include "engines/nancy/state/scene.h"
namespace Nancy {
GraphicsManager::GraphicsManager() :
_objects(objectComparator),
_inputPixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0),
_screenPixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0),
_clut8Format(Graphics::PixelFormat::createFormatCLUT8()),
_transparentPixelFormat(4, 8, 8, 8, 8, 8, 16, 24, 0),
_isSuppressed(false) {}
void GraphicsManager::init() {
auto *bsum = GetEngineData(BSUM);
assert(bsum);
// Extract transparent color from the boot summary
if (g_nancy->getGameType() == kGameTypeVampire) {
_transColor = bsum->paletteTrans;
} else {
_transColor = (bsum->rTrans << _inputPixelFormat.rShift) |
(bsum->gTrans << _inputPixelFormat.gShift) |
(bsum->bTrans << _inputPixelFormat.bShift);
}
initGraphics(640, 480, &_screenPixelFormat);
_screen.create(640, 480, _screenPixelFormat);
_screen.setTransparentColor(getTransColor());
_screen.clear();
const ImageChunk *ob0 = (const ImageChunk *)g_nancy->getEngineData("OB0");
assert(ob0);
g_nancy->_resource->loadImage(ob0->imageName, _object0);
}
void GraphicsManager::draw(bool updateScreen) {
if (_isSuppressed && updateScreen) {
_isSuppressed = false;
return;
}
g_nancy->_cursor->applyCursor();
// Update graphics for all RenderObjects and determine
// the areas of the screen that need to be redrawn
for (auto it : _objects) {
RenderObject ¤t = *it;
current.updateGraphics();
if (current._needsRedraw) {
if (current._isVisible) {
if (current.hasMoved() && !current.getPreviousScreenPosition().isEmpty()) {
// Object moved to a new location on screen, update the previous one
_dirtyRects.push_back(current.getPreviousScreenPosition());
}
// Redraw the current location
_dirtyRects.push_back(current.getScreenPosition());
} else if (!current.getPreviousScreenPosition().isEmpty()) {
// Object just turned invisible, redraw the last location
_dirtyRects.push_back(current.getPreviousScreenPosition());
}
}
current._needsRedraw = false;
current._hasMoved = false;
current._previousScreenPosition = current._screenPosition;
}
// Filter out dirty rects that are completely inside others to reduce overdraw
for (auto outer = _dirtyRects.begin(); outer != _dirtyRects.end(); ++outer) {
for (auto inner = _dirtyRects.begin(); inner != _dirtyRects.end(); ++inner) {
if (inner != outer && (*outer).contains(*inner)) {
_dirtyRects.erase(inner);
break;
}
}
}
// Perform the actual drawing. This checks for cases where something would be fully obscured,
// and skips them (e.g. redrawing the Viewport won't also redraw the background)
for (Common::Rect rect : _dirtyRects) {
for (RenderObject **it = _objects.begin(); it < _objects.end(); ++it) {
RenderObject ¤t = **it;
if (!current._isVisible || current.getScreenPosition().isEmpty()) {
continue;
}
bool shouldSkip = false;
Common::Rect intersection = rect.findIntersectingRect(current.getScreenPosition());
if (!intersection.isEmpty()) {
// Found an intersecting RenderObject. Loop through the following
// RenderObjects, and see if we have another that fully obscures the intersection
for (auto it2 = it + 1; it2 < _objects.end(); ++it2) {
RenderObject &other = **it2;
if (!other._isVisible || other.getScreenPosition().isEmpty()) {
continue;
}
Common::Rect intersection2 = intersection.findIntersectingRect(other.getScreenPosition());
if (intersection == intersection2) {
// The entire area that would be drawn is obscured by another RenderObject.
// If the obscuring RenderObject is not transparent, we skip drawing current
if (!other._drawSurface.hasTransparentColor() && other._drawSurface.format != _transparentPixelFormat) {
// No transparency, skip current
shouldSkip = true;
break;
}
}
}
if (shouldSkip) {
continue;
}
blitToScreen(current, rect.findIntersectingRect(current.getScreenPosition()));
}
}
}
// Draw the screen
if (updateScreen) {
_screen.update();
}
// Remove all dirty rects for the next frame
_dirtyRects.clear();
}
void GraphicsManager::loadFonts(Common::SeekableReadStream *chunkStream) {
auto *bsum = GetEngineData(BSUM);
assert(bsum);
assert(chunkStream);
chunkStream->seek(0);
_fonts.resize(bsum->numFonts);
for (uint i = 0; i < _fonts.size(); ++i) {
_fonts[i].read(*chunkStream);
}
delete chunkStream;
}
void GraphicsManager::addObject(RenderObject *object) {
for (auto &r : _objects) {
if (r == object) {
// Erase and re-add objects already in the array to make sure
// any changes in the z depth are reflected correctly
_objects.erase(&r);
}
}
_objects.insert(object);
}
void GraphicsManager::removeObject(RenderObject *object) {
for (auto &r : _objects) {
if (r == object) {
// Make sure the object gets properly cleared
_dirtyRects.push_back(r->getPreviousScreenPosition());
_objects.erase(&r);
break;
}
}
}
void GraphicsManager::clearObjects() {
_objects.clear();
}
void GraphicsManager::redrawAll() {
for (auto &obj : _objects) {
obj->_needsRedraw = true;
}
}
void GraphicsManager::suppressNextDraw() {
_isSuppressed = true;
}
void GraphicsManager::loadSurfacePalette(Graphics::ManagedSurface &inSurf, const Common::Path &paletteFilename, uint paletteStart, uint paletteSize) {
Common::File f;
if (f.open(paletteFilename.append(".bmp"))) {
Image::BitmapDecoder dec;
if (dec.loadStream(f)) {
inSurf.setPalette(dec.getPalette(), paletteStart, paletteSize);
}
}
}
void GraphicsManager::copyToManaged(const Graphics::Surface &src, Graphics::ManagedSurface &dst, bool verticalFlip, bool doubleSize) {
if (dst.w != (doubleSize ? src.w * 2 : src.w) || dst.h != (doubleSize ? src.h * 2 : src.h)) {
uint8 palette[256 * 3];
bool hasPalette = dst.hasPalette();
bool hasTransColor = dst.hasTransparentColor();
if (hasPalette && g_nancy->getGameType() == kGameTypeVampire) {
dst.grabPalette(palette, 0, 256);
}
dst.create(doubleSize ? src.w * 2 : src.w, doubleSize ? src.h * 2 : src.h, src.format);
if (hasPalette && g_nancy->getGameType() == kGameTypeVampire) {
dst.setPalette(palette, 0, 256);
}
if (hasTransColor) {
// Do the same trick with the transparent color
dst.setTransparentColor(dst.getTransparentColor());
}
}
if (!verticalFlip && !doubleSize) {
dst.copyRectToSurface(src, 0, 0, Common::Rect(0, 0, src.w, src.h));
return;
}
for (int y = 0; y < src.h; ++y) {
if (!doubleSize) {
// Copy single line bottom to top
memcpy(dst.getBasePtr(0, y), src.getBasePtr(0, src.h - y - 1), src.w * src.format.bytesPerPixel);
} else {
// Make four copies of each source pixel
for (int x = 0; x < src.w; ++x) {
switch (src.format.bytesPerPixel) {
case 1: {
const byte *srcP = (const byte *)src.getBasePtr(x, y);
uint dstX = x * 2;
uint dstY = verticalFlip ? (src.h - y - 1) * 2 : src.h - y - 1;
byte *dstP = ((byte *)dst.getBasePtr(dstX, dstY));
*dstP = *srcP;
*(dstP + 1) = *srcP;
dstP += dst.w;
*dstP = *srcP;
*(dstP + 1) = *srcP;
break;
}
case 2: {
const uint16 *srcP = (const uint16 *)src.getBasePtr(x, y);
uint dstX = x * 2;
uint dstY = verticalFlip ? (src.h - y - 1) * 2 : src.h - y - 1;
uint16 *dstP = ((uint16 *)dst.getBasePtr(dstX, dstY));
*dstP = *srcP;
*(dstP + 1) = *srcP;
dstP += dst.w;
*dstP = *srcP;
*(dstP + 1) = *srcP;
break;
}
case 4: {
const uint32 *srcP = (const uint32 *)src.getBasePtr(x, y);
uint dstX = x * 2;
uint dstY = verticalFlip ? (src.h - y - 1) * 2 : src.h - y - 1;
uint32 *dstP = ((uint32 *)dst.getBasePtr(dstX, dstY));
*dstP = *srcP;
*(dstP + 1) = *srcP;
dstP += dst.w;
*dstP = *srcP;
*(dstP + 1) = *srcP;
break;
}
default:
return;
}
}
}
}
}
void GraphicsManager::copyToManaged(void *src, Graphics::ManagedSurface &dst, uint srcW, uint srcH, const Graphics::PixelFormat &format, bool verticalFlip, bool doubleSize) {
// Do things the lazy way and simply create a Surface and pass it to the other overload
// We do NOT free the surface since it's a temporary object and does not own the pixels
Graphics::Surface surf;
surf.w = srcW;
surf.h = srcH;
surf.format = format;
surf.pitch = srcW * format.bytesPerPixel;
surf.setPixels(src);
copyToManaged(surf, dst, verticalFlip, doubleSize);
}
// Custom rotation code since Surface::rotoscale() produces incorrect results
// Only works on 16 bit surfaces and ignores transparency
// Rotation is a value between 0 and 3, corresponding to 0, 90, 180, or 270 degrees clockwise
void GraphicsManager::rotateBlit(const Graphics::ManagedSurface &src, Graphics::ManagedSurface &dest, byte rotation) {
assert(!src.empty() && !dest.empty());
assert(rotation <= 3);
assert(src.format.bytesPerPixel == 2 && dest.format.bytesPerPixel == 2);
uint srcW = src.w;
uint srcH = src.h;
const uint16 *s, *e;
if (rotation % 2) {
if (src.h != dest.w || src.w != dest.h) {
// Dest surface is wrong size, destroy it and create an appropriate one
dest.create(src.h, src.w, src.format);
}
} else {
if (src.w != dest.w || src.h != dest.h) {
// Dest surface is wrong size, destroy it and create an appropriate one
dest.create(src.w, src.h, src.format);
}
}
switch (rotation) {
case 0 :
// No rotation, just blit
dest.rawBlitFrom(src, src.getBounds(), Common::Point());
return;
case 2 : {
// 180 degrees
uint16 *d;
for (uint y = 0; y < srcH; ++y) {
s = (const uint16 *)src.getBasePtr(0, y);
e = (const uint16 *)src.getBasePtr(srcW, y);
d = (uint16 *)dest.getBasePtr(srcW - 1, srcH - y - 1);
for (; s < e; ++s, --d) {
*d = *s;
}
}
break;
}
case 1 :
// 90 degrees
for (uint y = 0; y < srcH; ++y) {
s = (const uint16 *)src.getBasePtr(0, y);
for (uint x = 0; x < srcW; ++x, ++s) {
*((uint16 *)dest.getBasePtr(srcH - y - 1, x)) = *s;
}
}
break;
case 3 :
// 270 degrees
for (uint y = 0; y < srcH; ++y) {
s = (const uint16 *)src.getBasePtr(0, y);
for (uint x = 0; x < srcW; ++x, ++s) {
*((uint16 *)dest.getBasePtr(y, srcW - x - 1)) = *s;
}
}
break;
}
}
void GraphicsManager::crossDissolve(const Graphics::ManagedSurface &from, const Graphics::ManagedSurface &to, byte alpha, const Common::Rect rect, Graphics::ManagedSurface &inResult) {
assert(from.getBounds() == to.getBounds());
inResult.blitFrom(from, rect, Common::Point());
inResult.transBlitFrom(to, rect, Common::Point(), (uint32)-1, false, 0, alpha);
}
void GraphicsManager::debugDrawToScreen(const Graphics::ManagedSurface &surf) {
_screen.blitFrom(surf, Common::Point());
_screen.update();
}
const Graphics::PixelFormat &GraphicsManager::getInputPixelFormat() {
if (g_nancy->getGameType() == kGameTypeVampire) {
return _clut8Format;
} else {
return _inputPixelFormat;
}
}
const Graphics::PixelFormat &GraphicsManager::getScreenPixelFormat() {
return _screenPixelFormat;
}
const Graphics::PixelFormat &GraphicsManager::getTransparentPixelFormat() {
return _transparentPixelFormat;
}
void GraphicsManager::grabViewportObjects(Common::Array<RenderObject *> &inArray) {
// Add the viewport
inArray.push_back(&(RenderObject &)NancySceneState.getViewport());
// Add all viewport-relative (non-UI) objects
for (RenderObject *obj : _objects) {
if (obj->isViewportRelative()) {
inArray.push_back(obj);
}
}
}
void GraphicsManager::screenshotScreen(Graphics::ManagedSurface &inSurf) {
draw(false);
inSurf.free();
inSurf.copyFrom(_screen);
}
// Draw a given screen-space rectangle to the screen
void GraphicsManager::blitToScreen(const RenderObject &src, Common::Rect screenRect) {
_screen.blitFrom(src._drawSurface, src._drawSurface.getBounds().findIntersectingRect(src.convertToLocal(screenRect)), screenRect);
}
int GraphicsManager::objectComparator(const void *a, const void *b) {
if (((const RenderObject*)a)->getZOrder() < ((const RenderObject*)b)->getZOrder()) {
return -1;
} else if (((const RenderObject*)a)->getZOrder() > ((const RenderObject*)b)->getZOrder()) {
return 1;
} else {
return 0;
}
}
} // End of namespace Nancy
|