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