]> Shamusworld >> Repos - stargem2/blob - src/video.cpp
Converted to SDL 2, added fullscreen support (F12 to toggle).
[stargem2] / src / video.cpp
1 //
2 // VIDEO.CPP: SDL/local hardware specific video routines
3 //
4 // by James L. Hammons
5 //
6
7 #include "video.h"
8 #include <SDL2/SDL.h>
9 #include <string.h>     // Why??? (for memset, etc... Lazy!) Dunno why, but this just strikes me as wrong...
10 #include <malloc.h>
11 #include "log.h"
12 #include "settings.h"
13
14
15 // Exported global variables (actually, these are LOCAL global variables, EXPORTED...)
16
17 static SDL_Window * sdlWindow = NULL;
18 static SDL_Renderer * sdlRenderer = NULL;
19 static SDL_Texture * sdlTexture = NULL;
20 uint32_t scrBuffer[VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT * sizeof(uint32_t)];
21
22
23 //
24 // Prime SDL and create surfaces
25 //
26 bool InitVideo(void)
27 {
28         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) != 0)
29         {
30                 WriteLog("Video: Could not initialize the SDL library: %s\n", SDL_GetError());
31                 return false;
32         }
33
34         int retVal = SDL_CreateWindowAndRenderer(VIRTUAL_SCREEN_WIDTH * 3, VIRTUAL_SCREEN_HEIGHT * 3, SDL_WINDOW_OPENGL, &sdlWindow, &sdlRenderer);
35
36         if (retVal != 0)
37         {
38                 WriteLog("Video: Could not window and/or renderer: %s\n", SDL_GetError());
39                 return false;
40         }
41
42         // Make the scaled rendering look smoother.
43 //      SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
44         SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
45         SDL_RenderSetLogicalSize(sdlRenderer, VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT);
46
47         sdlTexture = SDL_CreateTexture(sdlRenderer,
48                 SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING,
49                 VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT);
50
51         WriteLog("Video: Successfully initialized.\n");
52         return true;
53 }
54
55
56 //
57 // Free various SDL components
58 //
59 void VideoDone(void)
60 {
61         SDL_Quit();
62 }
63
64
65 //
66 // Render the backbuffer to the primary screen surface
67 //
68 void RenderScreenBuffer(void)
69 {
70         SDL_UpdateTexture(sdlTexture, NULL, scrBuffer, VIRTUAL_SCREEN_WIDTH * sizeof(Uint32));
71         SDL_RenderClear(sdlRenderer);
72         SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
73         SDL_RenderPresent(sdlRenderer);
74 }
75
76
77 //
78 // Fullscreen <-> window switching
79 //
80 void ToggleFullScreen(void)
81 {
82         settings.fullscreen = !settings.fullscreen;
83         int retVal;
84
85         if (settings.fullscreen)
86         {
87                 retVal = SDL_SetWindowFullscreen(sdlWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
88                 SDL_ShowCursor(0);
89         }
90         else
91         {
92                 retVal = SDL_SetWindowFullscreen(sdlWindow, 0);
93                 SDL_ShowCursor(1);
94         }
95
96         if (retVal != 0)
97         {
98                 WriteLog("Video::ToggleFullScreen: SDL error = %s\n", SDL_GetError());
99         }
100 }
101