Debian Codegraph @

src/io/gfx/video.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file video.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd August 2005: Created main.c
10 * - 22nd July 2008: Created util.c from parts of main.c
11 * - 3rd February 2009: Renamed util.c to util.cpp
12 * - 13th July 2009: Created graphics.cpp from parts of util.cpp
13 * - 26th July 2009: Renamed graphics.cpp to video.cpp
14 *
15 * @par Licence:
16 * Copyright (c) 2005-2017 AJ Thomson
17 * Copyright (c) 2015-2026 Carsten Teibes
18 *
19 * OpenJazz is distributed under the terms of
20 * the GNU General Public License, version 2.0
21 *
22 * @par Description:
23 * Contains graphics utility functions.
24 *
25 */
26
27
28 #include "paletteeffects.h"
29 #include "video.h"
30
31 #ifdef SCALE
32 #include <scalebit.h>
33 #endif
34
35 #include "setup.h"
36 #include "util.h"
37 #include "io/log.h"
38
39 #include <string.h>
40
41
42
43 /**
44 * Creates a surface.
45 *
46 * @param pixels Pixel data to copy into the surface. Can be nullptr.
47 * @param width Width of the pixel data and of the surface to be created
48 * @param height Height of the pixel data and of the surface to be created
49 *
50 * @return The completed surface
51 */
52 SDL_Surface* Video::createSurface (const unsigned char * pixels, int width, int height) {
53
54 // Create the surface
55 SDL_Surface *ret = nullptr;
56 #if OJ_SDL3
57 ret = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_INDEX8);
58 if(ret) {
59 SDL_Palette *pal = SDL_CreateSurfacePalette(ret);
60 if(!pal) {
61 LOG_FATAL("Could not create surface palette: %s", SDL_GetError());
62 return nullptr;
63 }
64 }
65 #elif OJ_SDL2
66 ret = SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, SDL_PIXELFORMAT_INDEX8);
67 #else
68 ret = SDL_CreateRGBSurface(SDL_HWSURFACE, width, height, 8, 0, 0, 0, 0);
69 #endif
70
71 if(!ret) {
72 LOG_FATAL("Could not create surface: %s", SDL_GetError());
73 return nullptr;
74 }
75
76 // Set the surface's palette
77 video.restoreSurfacePalette(ret);
78
79 if (pixels) {
80
81 // Upload pixel data to the surface
82 if (SDL_MUSTLOCK(ret)) SDL_LockSurface(ret);
83
84 for (int y = 0; y < height; y++)
85 memcpy(static_cast<unsigned char*>(ret->pixels) + (ret->pitch * y),
86 pixels + (width * y), width);
87
88 if (SDL_MUSTLOCK(ret)) SDL_UnlockSurface(ret);
89
90 }
91
92 return ret;
93
94 }
95
96 void Video::destroySurface (SDL_Surface *surface) {
97 if (!surface)
98 return;
99
100 #if OJ_SDL3
101 SDL_DestroySurface(surface);
102 #else
103 SDL_FreeSurface(surface);
104 #endif
105 surface = nullptr;
106 }
107
108 void Video::setClipRect (SDL_Surface *surface, const SDL_Rect *rect) {
109 #if OJ_SDL3
110 SDL_SetSurfaceClipRect(surface, rect);
111 #else
112 SDL_SetClipRect(surface, rect);
113 #endif
114 }
115
116 static void showCursor(bool enable) {
117 #if OJ_SDL3
118 if(enable) {
119 SDL_ShowCursor();
120 } else {
121 SDL_HideCursor();
122 }
123 #else
124 SDL_ShowCursor(enable? SDL_ENABLE: SDL_DISABLE);
125 #endif
126 }
127
128
129 /**
130 * Create the video output object.
131 */
132 Video::Video () :
133 #if OJ_SDL3 || OJ_SDL2
134 window(nullptr), renderer(nullptr), texture(nullptr),
135 #endif
136 #if OJ_SDL2
137 textureSurface(nullptr),
138 #endif
139 screen(nullptr), scaleFactor(MIN_SCALE), scaleMethod(scalerType::None),
140 fullscreen(false), isPlayingMovie(false) {
141
142 minW = maxW = screenW = DEFAULT_SCREEN_WIDTH;
143 minH = maxH = screenH = DEFAULT_SCREEN_HEIGHT;
144
145 // Generate the logical palette
146 #if OJ_SDL3
147 logicalPalette = SDL_CreatePalette(MAX_PALETTE_COLORS);
148 for (int i = 0; i < MAX_PALETTE_COLORS; i++) {
149 logicalPalette->colors[i].r = logicalPalette->colors[i].g =
150 logicalPalette->colors[i].b = i;
151 logicalPalette->colors[i].a = 0xFF;
152 }
153
154 texturePalette = SDL_CreatePalette(MAX_PALETTE_COLORS);
155 currentPalette = logicalPalette->colors;
156 #else
157 for (int i = 0; i < MAX_PALETTE_COLORS; i++) {
158 logicalPalette[i].r = logicalPalette[i].g = logicalPalette[i].b = i;
159 #if OJ_SDL2
160 logicalPalette[i].a = 0xFF;
161 #endif
162 }
163
164 currentPalette = logicalPalette;
165 #endif
166 }
167
168
169 /**
170 * Find the minimum and maximum horizontal and vertical resolutions.
171 */
172 void Video::findResolutions () {
173
174 #ifdef NO_RESIZE
175 minW = maxW = DEFAULT_SCREEN_WIDTH;
176 minH = maxH = DEFAULT_SCREEN_HEIGHT;
177
178 LOG_DEBUG("Using fixed resolution %dx%d.",
179 DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT);
180
181 // no need to sanitize
182 return;
183 #elif OJ_SDL3 || OJ_SDL2
184 minW = SW;
185 minH = SH;
186
187 bool queryOk = false;
188 #if OJ_SDL3
189 const SDL_DisplayMode *mode = SDL_GetDesktopDisplayMode(SDL_GetDisplayForWindow(window));
190 queryOk = mode != nullptr;
191 if(queryOk) {
192 maxW = mode->w;
193 maxH = mode->h;
194 }
195 #else
196 SDL_DisplayMode mode;
197 queryOk = SDL_GetDesktopDisplayMode(SDL_GetWindowDisplayIndex(window), &mode) == 0;
198 if(queryOk) {
199 maxW = mode.w;
200 maxH = mode.h;
201 }
202 #endif
203 if (!queryOk) {
204 LOG_WARN("Could not query display mode, using defaults.");
205
206 maxW = DEFAULT_SCREEN_WIDTH;
207 maxH = DEFAULT_SCREEN_HEIGHT;
208 }
209 #else
210 SDL_Rect **resolutions = SDL_ListModes(nullptr, fullscreen? FULLSCREEN_FLAGS: WINDOWED_FLAGS);
211
212 // All resolutions available, set to arbitrary limit
213 if (resolutions == reinterpret_cast<SDL_Rect**>(-1)) {
214 LOG_DEBUG("No display mode limit found.");
215 minW = SW;
216 minH = SH;
217 maxW = MAX_SCREEN_WIDTH;
218 maxH = MAX_SCREEN_HEIGHT;
219 } else {
220
221 for (int i = 0; resolutions[i] != nullptr; i++) {
222
223 // Save largest resolution
224 if (i == 0) {
225 maxW = resolutions[i]->w;
226 maxH = resolutions[i]->h;
227 }
228
229 // Save smallest resolution
230 if (resolutions[i + 1] == nullptr) {
231 minW = resolutions[i]->w;
232 minH = resolutions[i]->h;
233 }
234
235 }
236
237 }
238 #endif
239
240 // Sanitize
241 if (minW < SW) minW = SW;
242 if (minH < SH) minH = SH;
243 if (maxW > MAX_SCREEN_WIDTH) maxW = MAX_SCREEN_WIDTH;
244 if (maxH > MAX_SCREEN_HEIGHT) maxH = MAX_SCREEN_HEIGHT;
245
246 LOG_TRACE("Allowing resolutions between %dx%d and %dx%d.", minW, minH, maxW, maxH);
247
248 }
249
250
251 /**
252 * Initialise video output.
253 *
254 * @param cfg Video Options
255 *
256 * @return Success
257 */
258 bool Video::init (SetupOptions cfg) {
259
260 fullscreen = cfg.fullScreen;
261
262 // The window stays for whole runtime, as it is needed for
263 // e.g. determining desktop resolution
264 #if OJ_SDL3 || OJ_SDL2
265 #if OJ_SDL3
266 window = SDL_CreateWindow("OpenJazz", SW, SH, fullscreen? SDL_WINDOW_FULLSCREEN: SDL_WINDOW_RESIZABLE);
267 #elif OJ_SDL2
268 window = SDL_CreateWindow("OpenJazz", SDL_WINDOWPOS_UNDEFINED,
269 SDL_WINDOWPOS_UNDEFINED, SW, SH,
270 fullscreen? SDL_WINDOW_FULLSCREEN_DESKTOP: SDL_WINDOW_RESIZABLE);
271 #endif
272 if (!window) {
273 LOG_FATAL("Could not create window: %s", SDL_GetError());
274 return false;
275 }
276 #endif
277
278 if (fullscreen) showCursor(false);
279
280 findResolutions();
281
282 #ifdef SCALE
283 if ((SW * cfg.videoScale <= cfg.videoWidth) &&
284 (SH * cfg.videoScale <= cfg.videoHeight))
285 scaleFactor = cfg.videoScale;
286
287 scaleMethod = cfg.scaleMethod;
288 #endif
289
290 if (!reset(cfg.videoWidth, cfg.videoHeight)) {
291 LOG_FATAL("Could not set video mode: %s", SDL_GetError());
292 return false;
293 }
294
295 setTitle(nullptr);
296
297 return true;
298
299 }
300
301
302 /**
303 * Shared Deinitialisation code for reset() and deinit()
304 *
305 */
306 void Video::commonDeinit () {
307 // canvas is used when scaling or built with SDL2
308 if (canvas && canvas != screen)
309 destroySurface(canvas);
310
311 #if OJ_SDL2
312 if(textureSurface)
313 destroySurface(textureSurface);
314 #endif
315
316 #if OJ_SDL3 || OJ_SDL2
317 if(screen)
318 destroySurface(screen);
319
320 if(texture) {
321 SDL_DestroyTexture(texture);
322 texture = nullptr;
323 }
324
325 if(renderer) {
326 SDL_DestroyRenderer(renderer);
327 renderer = nullptr;
328 }
329 #endif
330 }
331
332 /**
333 * Deinitialise video output.
334 *
335 */
336 void Video::deinit () {
337
338 commonDeinit();
339
340 #if OJ_SDL3 || OJ_SDL2
341 if(window) {
342 SDL_DestroyWindow(window);
343 window = nullptr;
344 }
345 #endif
346 }
347
348 /**
349 * Sets the size of the video window or the resolution of the screen.
350 *
351 * @param width New width of the window or screen
352 * @param height New height of the window or screen
353 *
354 * @return Success
355 */
356 bool Video::reset (int width, int height) {
357
358 #ifdef NO_RESIZE
359 (void)width;
360 (void)height;
361 screenW = DEFAULT_SCREEN_WIDTH;
362 screenH = DEFAULT_SCREEN_HEIGHT;
363 fullscreen = true;
364 #else
365 screenW = width;
366 screenH = height;
367 #endif
368
369 commonDeinit();
370
371 // If video mode is not valid reset to low default
372 if (screenW < minW || screenW > maxW || screenH < minH || screenH > maxH) {
373 LOG_WARN("Video mode invalid, resetting.");
374 screenW = minW;
375 screenH = minH;
376 }
377
378 #if OJ_SDL3
379 renderer = SDL_CreateRenderer(window, nullptr);
380 if (!renderer) {
381 LOG_FATAL("Could not create renderer: %s", SDL_GetError());
382 return false;
383 }
384
385 SDL_SetWindowSize(window, screenW, screenH);
386 SDL_SetWindowFullscreen(window, fullscreen? SDL_WINDOW_FULLSCREEN: 0);
387 screen = createSurface(nullptr, screenW, screenH);
388 if (!screen) {
389 LOG_FATAL("Could not create screen surface: %s", SDL_GetError());
390 return false;
391 }
392 #elif OJ_SDL2
393 // Let the renderer scale the texture
394 switch(scaleMethod) {
395 case scalerType::Bilinear:
396 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
397 break;
398 default:
399 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
400 break;
401 }
402
403 renderer = SDL_CreateRenderer(window, -1, 0);
404 if (!renderer) {
405 LOG_FATAL("Could not create renderer: %s", SDL_GetError());
406 return false;
407 }
408
409 SDL_SetWindowSize(window, screenW, screenH);
410 SDL_SetWindowFullscreen(window, fullscreen? SDL_WINDOW_FULLSCREEN_DESKTOP: 0);
411 #else
412 screen = SDL_SetVideoMode(screenW, screenH, 8, fullscreen? FULLSCREEN_FLAGS: WINDOWED_FLAGS);
413 if (!screen) return false;
414 #endif
415
416 #ifdef SCALE
417 // sanitize
418 scaleFactor = CLAMP(scaleFactor, MIN_SCALE, MAX_SCALE);
419
420 // Check that the scale will fit in the current resolution
421 while ( ((screenW/SW < scaleFactor) || (screenH/SH < scaleFactor))
422 && (scaleFactor > MIN_SCALE) ) {
423 scaleFactor--;
424 }
425
426 if (scaleFactor > MIN_SCALE) {
427 // Do scaling
428 canvasW = screenW / scaleFactor;
429 canvasH = screenH / scaleFactor;
430 canvas = createSurface(nullptr, canvasW, canvasH);
431
432 } else
433 #endif
434 {
435 // No scaling
436 canvasW = screenW;
437 canvasH = screenH;
438
439 #if OJ_SDL3 || OJ_SDL2
440 canvas = createSurface(nullptr, canvasW, canvasH);
441 #else
442 canvas = screen;
443 #endif
444
445 }
446
447 #if OJ_SDL3 || OJ_SDL2
448 int renderW = canvasW;
449 int renderH = canvasH;
450
451 #if SCALE
452 // Do prescaling (scaleX, hqx)
453 if(scaleMethod != scalerType::None && scaleMethod != scalerType::Bilinear) {
454 renderW = screenW;
455 renderH = screenH;
456 }
457 #endif
458
459 #if OJ_SDL3
460 SDL_SetRenderLogicalPresentation(renderer, renderW, renderH, SDL_LOGICAL_PRESENTATION_LETTERBOX);
461
462 const char *name = SDL_GetRendererName(renderer);
463 LOG_TRACE("Using '%s' pixel format with '%s' renderer", SDL_GetPixelFormatName(SDL_PIXELFORMAT_INDEX8), name);
464
465 // Create texture for pixel data upload (8 bit)
466 texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_INDEX8, SDL_TEXTUREACCESS_STREAMING, renderW, renderH);
467 if (!texture) {
468 LOG_ERROR("Could not create texture: %s", SDL_GetError());
469 return false;
470 }
471 if(!SDL_SetTexturePalette(texture, texturePalette)) {
472 LOG_ERROR("Could not set texture palette: %s", SDL_GetError());
473 return false;
474 }
475 // Let the renderer scale the texture
476 switch(scaleMethod) {
477 case scalerType::Bilinear:
478 SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_LINEAR);
479 break;
480 default:
481 SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
482 break;
483 }
484 #elif OJ_SDL2
485 SDL_RenderSetLogicalSize(renderer, renderW, renderH);
486
487 Uint32 format = SDL_MasksToPixelFormatEnum(canvas->format->BitsPerPixel,
488 canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, canvas->format->Amask);
489 LOG_TRACE("Screen surface is using '%s' pixel format", SDL_GetPixelFormatName(format));
490 screen = SDL_CreateRGBSurfaceWithFormatFrom(canvas->pixels, canvasW, canvasH,
491 canvas->format->BitsPerPixel, canvas->pitch, format);
492
493 // Find a suitable renderer/texture format
494 format = SDL_PIXELFORMAT_RGB888;
495 SDL_RendererInfo info;
496 if (SDL_GetRendererInfo(renderer, &info) >= 0) {
497 for (Uint32 i = 0; i < info.num_texture_formats; i++) {
498 if (SDL_ISPIXELFORMAT_PACKED(info.texture_formats[i])) {
499 format = info.texture_formats[i];
500 break;
501 }
502 }
503 }
504 LOG_TRACE("Using '%s' pixel format with '%s' renderer", SDL_GetPixelFormatName(format), info.name);
505
506 // Create texture with compatible surface for pixel data upload (likely 32 bit)
507 texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, renderW, renderH);
508 if (!texture) {
509 LOG_ERROR("Could not create texture: %s", SDL_GetError());
510 return false;
511 }
512
513 textureSurface = SDL_CreateRGBSurfaceWithFormat(0, renderW, renderH, SDL_BITSPERPIXEL(format), format);
514 if (!textureSurface) {
515 LOG_ERROR("Could not create surface: %s", SDL_GetError());
516 return false;
517 }
518 #endif
519 #endif
520
521 #if !defined(WIZ) && !defined(GP2X)
522 expose();
523 #endif
524
525 return true;
526
527 }
528
529
530 /**
531 * Sets the display palette.
532 *
533 * @param palette The new palette
534 */
535 void Video::setPalette (SDL_Color *palette) {
536
537 // Make palette changes invisible until the next draw. Hopefully.
538 #if OJ_SDL3
539 const SDL_PixelFormatDetails *details = SDL_GetPixelFormatDetails(canvas->format);
540 clearScreen(SDL_MapRGB(details, nullptr, 0, 0, 0));
541 #else
542 clearScreen(SDL_MapRGB(screen->format, 0, 0, 0));
543 #endif
544 flip(0);
545
546 changePalette(palette, 0, MAX_PALETTE_COLORS);
547 currentPalette = palette;
548
549 }
550
551 /**
552 * Sets some colours of the display palette.
553 *
554 * @param palette The palette containing the new colours
555 * @param first The index of the first colour in both the display palette and the specified palette
556 * @param amount The number of colours
557 */
558 void Video::changePalette (SDL_Color *palette, unsigned char first, unsigned int amount) {
559 #if OJ_SDL3 || OJ_SDL2
560 setSurfacePalette(screen, palette, first, amount);
561 #if OJ_SDL3
562 if(!SDL_SetPaletteColors(texturePalette, palette, first, amount)) {
563 LOG_WARN("Could not set texture palette colors: %s", SDL_GetError());
564 }
565 #endif
566 #else
567 SDL_SetPalette(screen, SDL_PHYSPAL, palette, first, amount);
568 #endif
569 }
570
571
572 /**
573 * Restores a surface's palette.
574 *
575 * @param surface Surface with a modified palette
576 */
577 void Video::restoreSurfacePalette (SDL_Surface* surface) {
578 #if OJ_SDL3
579 setSurfacePalette(surface, logicalPalette->colors, 0, MAX_PALETTE_COLORS);
580 #else
581 setSurfacePalette(surface, logicalPalette, 0, MAX_PALETTE_COLORS);
582 #endif
583 }
584
585
586 /**
587 * Sets the window title.
588 *
589 * @param title the title or nullptr, to use default
590 */
591 void Video::setTitle (const char *title) {
592
593 constexpr const char *titleBase = "OpenJazz";
594 int titleLen = strlen(titleBase) + 1;
595
596 if (title != nullptr) {
597 titleLen = strlen(titleBase) + 3 + strlen(title) + 1;
598 }
599
600 char *windowTitle = new char[titleLen];
601 strcpy(windowTitle, titleBase);
602
603 if (title != nullptr) {
604 strcat(windowTitle, " - ");
605 strcat(windowTitle, title);
606 }
607
608 #if OJ_SDL3 || OJ_SDL2
609 SDL_SetWindowTitle(window, windowTitle);
610 #else
611 SDL_WM_SetCaption(windowTitle, nullptr);
612 #endif
613
614 delete[] windowTitle;
615
616 }
617
618
619 /**
620 * Sets the scaling factor and method.
621 *
622 * @param newScaleFactor The new scaling factor
623 * @param newScaleMethod The new scaling method
624 */
625 void Video::setScaling (int newScaleFactor, scalerType newScaleMethod) {
626 bool reset_needed = false;
627
628 if ((SW * newScaleFactor <= screenW) && (SH * newScaleFactor <= screenH)) {
629 reset_needed = (scaleFactor != newScaleFactor);
630 scaleFactor = newScaleFactor;
631 }
632
633 if(scaleMethod != newScaleMethod) {
634 scaleMethod = newScaleMethod;
635 reset_needed = true;
636 }
637
638 if(reset_needed) {
639 if (screen)
640 reset(screenW, screenH);
641 }
642 }
643
644
645 /**
646 * Refresh display palette.
647 */
648 void Video::expose () {
649 #if OJ_SDL3
650 setSurfacePalette(screen, logicalPalette->colors, 0, MAX_PALETTE_COLORS);
651 #else
652 setSurfacePalette(screen, logicalPalette, 0, MAX_PALETTE_COLORS);
653 #endif
654 changePalette(currentPalette, 0, MAX_PALETTE_COLORS);
655 }
656
657
658 /**
659 * Update video based on a system event.
660 *
661 * @param event The system event. Events not affecting video will be ignored
662 */
663 void Video::update (SDL_Event *event) {
664 #if !defined(FULLSCREEN_ONLY) || !defined(NO_RESIZE)
665 auto switchFullscreen = [&] () {
666 fullscreen = !fullscreen;
667 if (fullscreen) showCursor(false);
668
669 findResolutions();
670
671 reset(screenW, screenH);
672 if (!fullscreen) showCursor(true);
673 };
674
675 switch (event->type) {
676
677 #ifndef FULLSCREEN_ONLY
678 #if OJ_SDL3
679 case SDL_EVENT_KEY_DOWN:
680 // If Alt + Enter has been pressed, switch between windowed and full-screen mode.
681 if ((event->key.key == SDLK_RETURN) && (event->key.mod & SDL_KMOD_ALT)) {
682 switchFullscreen();
683 }
684 #else
685 case SDL_KEYDOWN:
686 // If Alt + Enter has been pressed, switch between windowed and full-screen mode.
687 if ((event->key.keysym.sym == SDLK_RETURN) &&
688 (event->key.keysym.mod & KMOD_ALT)) {
689 switchFullscreen();
690 }
691
692 #endif
693
694 break;
695 #endif
696
697 #if !OJ_SDL3 && !OJ_SDL2
698 #ifndef NO_RESIZE
699 case SDL_VIDEORESIZE:
700 reset(event->resize.w, event->resize.h);
701
702 break;
703 #endif
704
705 case SDL_VIDEOEXPOSE:
706 expose();
707
708 break;
709 #endif
710 }
711 #else
712 (void)event;
713 #endif
714 }
715
716
717 /**
718 * Draw graphics to screen.
719 *
720 * @param mspf Ticks per frame
721 * @param paletteEffects Palette effects to use
722 * @param effectsStopped Whether the effects should be applied without advancing
723 */
724 void Video::flip (int mspf, PaletteEffect* paletteEffects, bool effectsStopped) {
725
726 SDL_Color shownPalette[MAX_PALETTE_COLORS];
727
728 #if defined(SCALE) && !OJ_SDL2 && !OJ_SDL3
729 // for SDL1.2 scale 8 bit canvas surface
730 if (canvas != nullptr && canvas != screen) {
731
732 // Copy everything that has been drawn so far
733 if (scaleMethod == scalerType::Scale2x)
734 scale(scaleFactor,
735 screen->pixels, screen->pitch,
736 canvas->pixels, canvas->pitch,
737 screen->format->BytesPerPixel, canvas->w, canvas->h);
738 else
739 SDL_SoftStretch(canvas, nullptr, screen, nullptr);
740
741 }
742 #endif
743
744 // Apply palette effects
745 if (paletteEffects) {
746 // The palette is emulated, compile all palette changes and apply at once.
747 memcpy(shownPalette, currentPalette, sizeof(SDL_Color) * MAX_PALETTE_COLORS);
748 paletteEffects->apply(shownPalette, false, mspf, effectsStopped);
749 changePalette(shownPalette, 0, MAX_PALETTE_COLORS);
750 }
751
752 #if OJ_SDL3
753 // Show what has been drawn
754 SDL_UpdateTexture(texture, nullptr, canvas->pixels, canvas->pitch);
755 SDL_RenderClear(renderer);
756 if (isPlayingMovie) {
757 SDL_FRect src = {0, 0, static_cast<float>(canvasW), static_cast<float>(canvasH)};
758 SDL_RenderTexture(renderer, texture, &src, nullptr);
759 } else {
760 SDL_RenderTexture(renderer, texture, nullptr, nullptr);
761 }
762 SDL_RenderPresent(renderer);
763 #elif OJ_SDL2
764 #if defined(SCALE)
765 if(scaleFactor > MIN_SCALE && scaleMethod == scalerType::Scale2x) {
766 // scale 32 bit screen surface
767 scale(scaleFactor,
768 textureSurface->pixels, textureSurface->pitch,
769 screen->pixels, screen->pitch,
770 screen->format->BytesPerPixel, screen->w, screen->h);
771 } else if (scaleFactor > MIN_SCALE && scaleMethod == scalerType::hqx) {
772 // TODO
773 } else {
774 #endif
775 // copy unscaled
776 SDL_BlitSurface(screen, nullptr, textureSurface, nullptr);
777 #if defined(SCALE)
778 }
779 #endif
780
781 // Show what has been drawn
782 SDL_UpdateTexture(texture, nullptr, textureSurface->pixels, textureSurface->pitch);
783 SDL_RenderClear(renderer);
784
785 if (isPlayingMovie) {
786 SDL_Rect src = {0, 0, canvasW, canvasH};
787 SDL_RenderCopy(renderer, texture, &src, nullptr);
788 } else {
789 SDL_RenderCopy(renderer, texture, nullptr, nullptr);
790 }
791 SDL_RenderPresent(renderer);
792 #else
793 SDL_Flip(screen);
794 #endif
795
796 }
797
798
799 /**
800 * Fill the screen with a colour.
801 *
802 * @param index Index of the colour to use
803 */
804 void Video::clearScreen (int index) {
805
806 #if defined(CAANOO) || defined(WIZ) || defined(GP2X) || defined(GAMESHELL)
807 // always 240 lines cleared to black
808 memset(screen->pixels, index, 320*240);
809 #elif defined (__3DS__)
810 memset(screen->pixels, index, 400*240);
811 #elif OJ_SDL3
812 SDL_FillSurfaceRect(canvas, nullptr, index);
813 #else
814 SDL_FillRect(canvas, nullptr, index);
815 #endif
816
817 }
818
819
820 /**
821 * Sets up scaling for movie mode.
822 *
823 * @param status Whether or not movie mode will be enabled
824 */
825 void Video::moviePlayback (bool status) {
826 #if OJ_SDL3 || OJ_SDL2
827 static int movieW = SW;
828 static int movieH = SH;
829
830 if(isPlayingMovie == status)
831 return;
832
833 isPlayingMovie = status;
834
835 if (isPlayingMovie) {
836 // save size
837 movieW = canvasW;
838 movieH = canvasH;
839
840 canvasW = SW;
841 canvasH = SH;
842 } else {
843 // reset
844 canvasW = movieW;
845 canvasH = movieH;
846 }
847 #if OJ_SDL3
848 SDL_SetRenderLogicalPresentation(renderer, canvasW, canvasH, SDL_LOGICAL_PRESENTATION_LETTERBOX);
849 #else
850 SDL_RenderSetLogicalSize(renderer, canvasW, canvasH);
851 #endif
852 #else
853 (void)status;
854 #endif
855 }
856
857
858 /**
859 * Fill a specified rectangle of the screen with a colour.
860 *
861 * @param x X-coordinate of the left side of the rectangle
862 * @param y Y-coordinate of the top of the rectangle
863 * @param width Width of the rectangle
864 * @param height Height of the rectangle
865 * @param index Index of the colour to use
866 * @param fill Whether to fill or only color the borders
867 */
868 void Video::drawRect (int x, int y, int width, int height, int index, bool fill) {
869 SDL_Rect dst[4];
870
871 for(int i = 0; i < 4; i++) {
872 dst[i].x = x;
873 dst[i].y = y;
874 dst[i].w = width;
875 dst[i].h = height;
876 }
877
878 if (fill) {
879 #if OJ_SDL3
880 SDL_FillSurfaceRect(canvas, &dst[0], index);
881 #else
882 SDL_FillRect(canvas, &dst[0], index);
883 #endif
884 } else {
885 // left
886 dst[0].w = 1;
887 // right
888 dst[1].x = x + width - 1;
889 dst[1].w = 1;
890 // top
891 dst[2].h = 1;
892 // bottom
893 dst[3].y = y + height - 1;
894 dst[3].h = 1;
895 // draw each border
896 #if OJ_SDL3
897 SDL_FillSurfaceRects(canvas, dst, 4, index);
898 #elif OJ_SDL2
899 SDL_FillRects(canvas, dst, 4, index);
900 #else
901 SDL_FillRect(canvas, &dst[0], index);
902 SDL_FillRect(canvas, &dst[1], index);
903 SDL_FillRect(canvas, &dst[2], index);
904 SDL_FillRect(canvas, &dst[3], index);
905 #endif
906 }
907 }
908
909 /**
910 * Sets the Color key of provided surface.
911 *
912 * @param surface Surface to change
913 * @param index Color index
914 */
915 void Video::enableColorKey (SDL_Surface* surface, unsigned int index) {
916
917 #if OJ_SDL3
918 SDL_SetSurfaceColorKey(surface, true, index);
919 #elif OJ_SDL2
920 SDL_SetColorKey(surface, SDL_TRUE, index);
921 #else
922 SDL_SetColorKey(surface, SDL_SRCCOLORKEY, index);
923 #endif
924
925 }
926
927 /**
928 * Returns the Color key of provided surface.
929 *
930 * @param surface Surface to query
931 *
932 * @return color index
933 */
934 unsigned int Video::getColorKey (SDL_Surface* surface) {
935
936 #if OJ_SDL3 || OJ_SDL2
937 Uint32 key;
938 bool queryOk = false;
939
940 #if OJ_SDL3
941 queryOk = SDL_GetSurfaceColorKey(surface, &key);
942 #else
943 queryOk = SDL_GetColorKey(surface, &key) == 0;
944 #endif
945
946 if (!queryOk) {
947 LOG_WARN("Could not get Color Key: %s\n", SDL_GetError());
948 return -1;
949 }
950
951 return key;
952
953 #else
954 return surface->format->colorkey;
955 #endif
956
957 }
958
959 /**
960 * Sets the palette colors of provided surface.
961 *
962 * @param surface Surface to change
963 * @param palette Palette to copy colors from
964 * @param start index of first color copy
965 * @param length number of colors to copy
966 */
967 void Video::setSurfacePalette (SDL_Surface* surface, SDL_Color *palette, int start, int length) {
968 #if OJ_SDL3
969 SDL_Palette *surfacePalette = SDL_GetSurfacePalette(surface);
970 if(!surfacePalette) {
971 LOG_WARN("Could not set palette on surface without palette.");
972 return;
973 }
974
975 if(!SDL_SetPaletteColors(surfacePalette, palette, start, length)) {
976 LOG_WARN("Could not set palette colors: %s", SDL_GetError());
977 return;
978 }
979 #elif OJ_SDL2
980 if (surface->format->palette) {
981 SDL_SetPaletteColors(surface->format->palette, palette, start, length);
982 } else
983 LOG_WARN("Could not change palette on surface without palette.");
984 #else
985 SDL_SetPalette(surface, SDL_LOGPAL, palette, start, length);
986 #endif
987
988 }