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