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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
|
/**
*
* @file video.cpp
*
* Part of the OpenJazz project
*
* @par History:
* - 23rd August 2005: Created main.c
* - 22nd July 2008: Created util.c from parts of main.c
* - 3rd February 2009: Renamed util.c to util.cpp
* - 13th July 2009: Created graphics.cpp from parts of util.cpp
* - 26th July 2009: Renamed graphics.cpp to video.cpp
*
* @par Licence:
* Copyright (c) 2005-2017 Alister Thomson
*
* OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @par Description:
* Contains graphics utility functions.
*
*/
#include "paletteeffects.h"
#include "video.h"
#ifdef SCALE
#include "io/gfx/scale2x/scalebit.h"
#endif
#include "util.h"
#include <string.h>
/**
* Creates a surface.
*
* @param pixels Pixel data to copy into the surface. Can be NULL.
* @param width Width of the pixel data and of the surface to be created
* @param height Height of the pixel data and of the surface to be created
*
* @return The completed surface
*/
SDL_Surface* createSurface (unsigned char * pixels, int width, int height) {
SDL_Surface *ret;
int y;
// Create the surface
ret = SDL_CreateRGBSurface(SDL_HWSURFACE, width, height, 8, 0, 0, 0, 0);
// Set the surface's palette
video.restoreSurfacePalette(ret);
if (pixels) {
// Upload pixel data to the surface
if (SDL_MUSTLOCK(ret)) SDL_LockSurface(ret);
for (y = 0; y < height; y++)
memcpy(((unsigned char *)(ret->pixels)) + (ret->pitch * y),
pixels + (width * y), width);
if (SDL_MUSTLOCK(ret)) SDL_UnlockSurface(ret);
}
return ret;
}
/**
* Create the video output object.
*/
Video::Video () {
int count;
screen = NULL;
#ifdef SCALE
scaleFactor = 1;
#endif
// Generate the logical palette
for (count = 0; count < 256; count++)
logicalPalette[count].r = logicalPalette[count].g =
logicalPalette[count].b = count;
currentPalette = logicalPalette;
return;
}
/**
* Find the maximum horizontal and vertical resolutions.
*/
void Video::findMaxResolution () {
#ifdef NO_RESIZE
maxW = DEFAULT_SCREEN_WIDTH;
maxH = DEFAULT_SCREEN_HEIGHT;
#else
SDL_Rect **resolutions;
int count;
resolutions = SDL_ListModes(NULL, fullscreen? FULLSCREEN_FLAGS: WINDOWED_FLAGS);
if (resolutions == (SDL_Rect **)(-1)) {
maxW = MAX_SCREEN_WIDTH;
maxH = MAX_SCREEN_HEIGHT;
} else {
maxW = SW;
maxH = SH;
for (count = 0; resolutions[count] != NULL; count++) {
if (resolutions[count]->w > maxW) maxW = resolutions[count]->w;
if (resolutions[count]->h > maxH) maxH = resolutions[count]->h;
}
if (maxW > MAX_SCREEN_WIDTH) maxW = MAX_SCREEN_WIDTH;
if (maxH > MAX_SCREEN_HEIGHT) maxH = MAX_SCREEN_HEIGHT;
}
#endif
return;
}
/**
* Initialise video output.
*
* @param width Width of the window or screen
* @param height Height of the window or screen
* @param startFullscreen Whether or not to start in full-screen mode
*
* @return Success
*/
bool Video::init (int width, int height, bool startFullscreen) {
fullscreen = startFullscreen;
if (fullscreen) SDL_ShowCursor(SDL_DISABLE);
if (!reset(width, height)) {
logError("Could not set video mode", SDL_GetError());
return false;
}
setTitle(NULL);
findMaxResolution();
return true;
}
/**
* Sets the size of the video window or the resolution of the screen.
*
* @param width New width of the window or screen
* @param height New height of the window or screen
*
* @return Success
*/
bool Video::reset (int width, int height) {
screenW = width;
screenH = height;
#ifdef SCALE
if (canvas != screen) SDL_FreeSurface(canvas);
#endif
#ifdef NO_RESIZE
screen = SDL_SetVideoMode(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 8, FULLSCREEN_FLAGS);
#else
screen = SDL_SetVideoMode(screenW, screenH, 8, fullscreen? FULLSCREEN_FLAGS: WINDOWED_FLAGS);
#endif
if (!screen) return false;
#ifdef SCALE
// Check that the scale will fit in the current resolution
while ( ((screenW/SW < scaleFactor) || (screenH/SH < scaleFactor)) && (scaleFactor > 1) ) {
scaleFactor--;
}
if (scaleFactor > 1) {
canvasW = screenW / scaleFactor;
canvasH = screenH / scaleFactor;
canvas = createSurface(NULL, canvasW, canvasH);
} else
#endif
{
canvasW = screenW;
canvasH = screenH;
canvas = screen;
}
#if !defined(WIZ) && !defined(GP2X)
expose();
#endif
/* A real 8-bit display is quite likely if the user has the right video
card, the right video drivers, the right version of DirectX/whatever, and
the right version of SDL. In other words, it's not likely enough. If a real
palette is assumed when
a) there really is a real palette, there will be an extremely small speed
gain.
b) the palette is emulated, there will be a HUGE speed loss.
Therefore, assume the palette is emulated. */
/// @todo Find a better way to determine if palette is emulated
fakePalette = true;
return true;
}
/**
* Sets the display palette.
*
* @param palette The new palette
*/
void Video::setPalette (SDL_Color *palette) {
// Make palette changes invisible until the next draw. Hopefully.
clearScreen(SDL_MapRGB(screen->format, 0, 0, 0));
flip(0);
SDL_SetPalette(screen, SDL_PHYSPAL, palette, 0, 256);
currentPalette = palette;
return;
}
/**
* Returns the current display palette.
*
* @return The current display palette
*/
SDL_Color* Video::getPalette () {
return currentPalette;
}
/**
* Sets some colours of the display palette.
*
* @param palette The palette containing the new colours
* @param first The index of the first colour in both the display palette and the specified palette
* @param amount The number of colours
*/
void Video::changePalette (SDL_Color *palette, unsigned char first, unsigned int amount) {
SDL_SetPalette(screen, SDL_PHYSPAL, palette, first, amount);
return;
}
/**
* Restores a surface's palette.
*
* @param surface Surface with a modified palette
*/
void Video::restoreSurfacePalette (SDL_Surface* surface) {
SDL_SetPalette(surface, SDL_LOGPAL, logicalPalette, 0, 256);
return;
}
/**
* Returns the maximum possible screen width.
*
* @return The maximum width
*/
int Video::getMaxWidth () {
return maxW;
}
/**
* Returns the maximum possible screen height.
*
* @return The maximum height
*/
int Video::getMaxHeight () {
return maxH;
}
/**
* Returns the current width of the window or screen.
*
* @return The width
*/
int Video::getWidth () {
return screenW;
}
/**
* Returns the current height of the window or screen.
*
* @return The height
*/
int Video::getHeight () {
return screenH;
}
/**
* Sets the window title.
*
* @param the title or NULL, to use default
*/
void Video::setTitle (const char *title) {
const char titleBase[] = "OpenJazz";
char *windowTitle = NULL;
int titleLen = strlen(titleBase) + 1;
if (title != NULL) {
titleLen = strlen(titleBase) + 3 + strlen(title) + 1;
}
windowTitle = new char[titleLen];
strcpy(windowTitle, titleBase);
if (title != NULL) {
strcat(windowTitle, " - ");
strcat(windowTitle, title);
}
SDL_WM_SetCaption(windowTitle, NULL);
delete[] windowTitle;
}
#ifdef SCALE
/**
* Returns the current scaling factor.
*
* @return The scaling factor
*/
int Video::getScaleFactor () {
return scaleFactor;
}
/**
* Sets the scaling factor.
*
* @param newScaleFactor The new scaling factor
*/
int Video::setScaleFactor (int newScaleFactor) {
if ((SW * newScaleFactor <= screenW) && (SH * newScaleFactor <= screenH)) {
scaleFactor = newScaleFactor;
if (screen) reset(screenW, screenH);
}
return scaleFactor;
}
#endif
#ifndef FULLSCREEN_ONLY
/**
* Determines whether or not full-screen mode is being used.
*
* @return Whether or not full-screen mode is being used
*/
bool Video::isFullscreen () {
return fullscreen;
}
#endif
/**
* Refresh display palette.
*/
void Video::expose () {
SDL_SetPalette(screen, SDL_LOGPAL, logicalPalette, 0, 256);
SDL_SetPalette(screen, SDL_PHYSPAL, currentPalette, 0, 256);
return;
}
/**
* Update video based on a system event.
*
* @param event The system event. Events not affecting video will be ignored
*/
void Video::update (SDL_Event *event) {
#if !defined(FULLSCREEN_ONLY) || !defined(NO_RESIZE)
switch (event->type) {
#ifndef FULLSCREEN_ONLY
case SDL_KEYDOWN:
// If Alt + Enter has been pressed, switch between windowed and full-screen mode.
if ((event->key.keysym.sym == SDLK_RETURN) &&
(event->key.keysym.mod & KMOD_ALT)) {
fullscreen = !fullscreen;
if (fullscreen) SDL_ShowCursor(SDL_DISABLE);
reset(screenW, screenH);
if (!fullscreen) SDL_ShowCursor(SDL_ENABLE);
findMaxResolution();
}
break;
#endif
#ifndef NO_RESIZE
case SDL_VIDEORESIZE:
reset(event->resize.w, event->resize.h);
break;
#endif
case SDL_VIDEOEXPOSE:
expose();
break;
}
#endif
return;
}
/**
* Draw graphics to screen.
*
* @param mspf Ticks per frame
* @param paletteEffects Palette effects to use
* @param effectsStopped Whether the effects should be applied without advancing
*/
void Video::flip (int mspf, PaletteEffect* paletteEffects, bool effectsStopped) {
SDL_Color shownPalette[256];
#ifdef SCALE
if (canvas != screen) {
// Copy everything that has been drawn so far
scale(scaleFactor,
screen->pixels, screen->pitch,
canvas->pixels, canvas->pitch,
screen->format->BytesPerPixel, canvas->w, canvas->h);
}
#endif
// Apply palette effects
if (paletteEffects) {
/* If the palette is being emulated, compile all palette changes and
apply them all at once.
If the palette is being used directly, apply all palette effects
directly. */
if (fakePalette) {
memcpy(shownPalette, currentPalette, sizeof(SDL_Color) * 256);
paletteEffects->apply(shownPalette, false, mspf, effectsStopped);
SDL_SetPalette(screen, SDL_PHYSPAL, shownPalette, 0, 256);
} else {
paletteEffects->apply(shownPalette, true, mspf, effectsStopped);
}
}
// Show what has been drawn
SDL_Flip(screen);
return;
}
/**
* Fill the screen with a colour.
*
* @param index Index of the colour to use
*/
void Video::clearScreen (int index) {
#if defined(CAANOO) || defined(WIZ) || defined(GP2X) || defined(GAMESHELL)
// always 240 lines cleared to black
memset(video.screen->pixels, index, 320*240);
#else
SDL_FillRect(canvas, NULL, index);
#endif
return;
}
/**
* Fill a specified rectangle of the screen with a colour.
*
* @param x X-coordinate of the left side of the rectangle
* @param y Y-coordinate of the top of the rectangle
* @param width Width of the rectangle
* @param height Height of the rectangle
* @param index Index of the colour to use
*/
void drawRect (int x, int y, int width, int height, int index) {
SDL_Rect dst;
dst.x = x;
dst.y = y;
dst.w = width;
dst.h = height;
SDL_FillRect(canvas, &dst, index);
return;
}
|