]> Shamusworld >> Repos - apple2/blob - src/video.cpp
Misc. cleanups, incl. cleanup up Makefile.
[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 "apple2-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
31 //
32 // Prime SDL and create surfaces
33 //
34 bool InitVideo(void)
35 {
36         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) != 0)
37         {
38                 WriteLog("Video: Could not initialize the SDL library: %s\n", SDL_GetError());
39                 return false;
40         }
41
42         int retVal = SDL_CreateWindowAndRenderer(VIRTUAL_SCREEN_WIDTH * 2, VIRTUAL_SCREEN_HEIGHT * 2, SDL_WINDOW_OPENGL, &sdlWindow, &sdlRenderer);
43
44         if (retVal != 0)
45         {
46                 WriteLog("Video: Could not window and/or renderer: %s\n", SDL_GetError());
47                 return false;
48         }
49
50         // Make the scaled rendering look smoother.
51         SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
52 //      SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
53         SDL_RenderSetLogicalSize(sdlRenderer, VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT);
54
55         // Set the application's icon & title...
56         SDL_Surface * iconSurface = SDL_CreateRGBSurfaceFrom(icon, 64, 64, 32, 64*4, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
57         SDL_SetWindowIcon(sdlWindow, iconSurface);
58         SDL_FreeSurface(iconSurface);
59         SDL_SetWindowTitle(sdlWindow, "Apple2 Emulator");
60
61         sdlTexture = SDL_CreateTexture(sdlRenderer,
62                 SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING,
63                 VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT);
64
65         WriteLog("Video: Successfully initialized.\n");
66         return true;
67 }
68
69
70 //
71 // Free various SDL components
72 //
73 void VideoDone(void)
74 {
75         WriteLog("Video: Shutting down SDL...\n");
76         SDL_Quit();
77         WriteLog("Video: Done.\n");
78 }
79
80
81 //
82 // Render the screen buffer to the primary screen surface
83 //
84 void RenderScreenBuffer(void)
85 {
86         SDL_UpdateTexture(sdlTexture, NULL, scrBuffer, VIRTUAL_SCREEN_WIDTH * sizeof(Uint32));
87         SDL_RenderClear(sdlRenderer);
88         SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
89 }
90
91
92 //
93 // Fullscreen <-> window switching
94 //
95 void ToggleFullScreen(void)
96 {
97         settings.fullscreen = !settings.fullscreen;
98
99         int retVal = SDL_SetWindowFullscreen(sdlWindow, (settings.fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
100         SDL_ShowCursor(settings.fullscreen ? 0 : 1);
101
102         if (retVal != 0)
103                 WriteLog("Video::ToggleFullScreen: SDL error = %s\n", SDL_GetError());
104 }
105
106