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