]> Shamusworld >> Repos - legend/blob - src/video.cpp
Initial commit for the Legend of A... project!
[legend] / 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 //
11
12 #include "video.h"
13 #include <string.h>     // Why??? (for memset, etc... Lazy!) Dunno why, but this just strikes me as wrong...
14 #include <malloc.h>
15 #include <SDL2/SDL_image.h>
16 #include "log.h"
17
18
19 // Exported global variables (actually, these are LOCAL global variables, EXPORTED...)
20
21 static SDL_Window * sdlWindow = NULL;
22 SDL_Renderer * sdlRenderer = NULL;
23 static SDL_Texture * sdlTexture = NULL;
24 uint32_t scrBuffer[VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT * sizeof(uint32_t)];
25 bool fullscreen = false;
26
27
28 //
29 // Prime SDL and create surfaces
30 //
31 bool InitVideo(void)
32 {
33         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) != 0)
34         {
35                 WriteLog("Video: Could not initialize the SDL library: %s\n", SDL_GetError());
36                 return false;
37         }
38
39         int retVal = SDL_CreateWindowAndRenderer(VIRTUAL_SCREEN_WIDTH * 2, VIRTUAL_SCREEN_HEIGHT * 2, SDL_WINDOW_OPENGL, &sdlWindow, &sdlRenderer);
40
41         if (retVal != 0)
42         {
43                 WriteLog("Video: Could not window and/or renderer: %s\n", SDL_GetError());
44                 return false;
45         }
46
47         // Set up SDL_image
48         retVal = IMG_Init(IMG_INIT_PNG);
49
50         if (retVal != IMG_INIT_PNG)
51         {
52                 WriteLog("Video: Could not init SDL_image for PNG loading!\n");
53                 WriteLog("IMG_Init: %s\n", IMG_GetError());
54         }
55
56         // Make the scaled rendering look smoother.
57         SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
58 //      SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
59         SDL_RenderSetLogicalSize(sdlRenderer, VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT);
60
61         // Set the application's icon & title...
62 //      SDL_Surface * iconSurface = SDL_CreateRGBSurfaceFrom(icon, 64, 64, 32, 64*4, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
63 //      SDL_SetWindowIcon(sdlWindow, iconSurface);
64 //      SDL_FreeSurface(iconSurface);
65         SDL_SetWindowTitle(sdlWindow, "Legend of ?");
66
67         sdlTexture = SDL_CreateTexture(sdlRenderer,
68                 SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING,
69                 VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT);
70
71         WriteLog("Video: Successfully initialized.\n");
72         return true;
73 }
74
75
76 //
77 // Free various SDL components
78 //
79 void VideoDone(void)
80 {
81         WriteLog("Video: Shutting down SDL...\n");
82         IMG_Quit();
83         SDL_Quit();
84         WriteLog("Video: Done.\n");
85 }
86
87
88 //
89 // Render the screen buffer to the primary screen surface
90 //
91 void RenderScreenBuffer(void)
92 {
93         SDL_UpdateTexture(sdlTexture, NULL, scrBuffer, VIRTUAL_SCREEN_WIDTH * sizeof(Uint32));
94         SDL_RenderClear(sdlRenderer);
95         SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
96 }
97
98
99 //
100 // Fullscreen <-> window switching
101 //
102 void ToggleFullScreen(void)
103 {
104         fullscreen = !fullscreen;
105
106         int retVal = SDL_SetWindowFullscreen(sdlWindow, (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
107         SDL_ShowCursor(fullscreen ? 0 : 1);
108
109         if (retVal != 0)
110                 WriteLog("Video::ToggleFullScreen: SDL error = %s\n", SDL_GetError());
111 }
112
113