Debian Codegraph @

src/IOGfxDisplay.cpp

raw
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include "IOGfxDisplay.h"
6
7 #include "SDL_image.h"
8
9 #include "log.h"
10 #include "freedink_xpm.h"
11
12 IOGfxDisplay::IOGfxDisplay(int w, int h, bool truecolor, Uint32 flags)
13 : w(w), h(h), truecolor(truecolor), flags(flags),
14 initializedVideo(false), window(NULL),
15 brightness(256) {
16 }
17
18 IOGfxDisplay::~IOGfxDisplay() {
19 close(); // non-virtual call
20 }
21
22 bool IOGfxDisplay::open() {
23 if (!SDL_WasInit(SDL_INIT_VIDEO)) {
24 if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1) {
25 log_error("Video initialization error: %s", SDL_GetError());
26 return false;
27 }
28 initializedVideo = true;
29 }
30
31
32 if (!createWindow()) return false;
33
34 return true;
35 }
36
37 void IOGfxDisplay::close() {
38 if (window) SDL_DestroyWindow(window);
39 window = NULL;
40
41 if (initializedVideo) {
42 SDL_QuitSubSystem(SDL_INIT_VIDEO);
43 initializedVideo = false;
44 }
45 }
46
47 bool IOGfxDisplay::createWindow() {
48 window = SDL_CreateWindow(PACKAGE_STRING,
49 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
50 w, h, flags);
51 if (window == NULL) {
52 log_error("Unable to create %dx%d window: %s\n",
53 w, h, SDL_GetError());
54 return false;
55 }
56
57 SDL_GetWindowSize(window, &w, &h);
58 // There's a delay between theoretical and actual full-screen resize, impacting e.g. in OpenGL targets
59 // Also (not worked-around here): starting in full-screen mode make the window keep the full size even when toggleFullScreen
60 // TODO: fix me in SDL2 [master 2015-08]
61 if (flags & SDL_WINDOW_FULLSCREEN)
62 SDL_Delay(100);
63
64 /* Window configuration */
65 {
66 SDL_Surface *icon = NULL;
67 if ((icon = IMG_ReadXPMFromArray(freedink_xpm)) == NULL) {
68 log_error("Error loading icon: %s", IMG_GetError());
69 } else {
70 SDL_SetWindowIcon(window, icon);
71 SDL_FreeSurface(icon);
72 }
73 }
74
75 return true;
76 }
77
78
79
80 void IOGfxDisplay::logDisplayInfo() {
81 log_info("Truecolor mode: %s", truecolor ? "on" : "off");
82
83 Uint32 Rmask=0, Gmask=0, Bmask=0, Amask=0; int bpp=0;
84 SDL_PixelFormatEnumToMasks(getFormat(), &bpp,
85 &Rmask, &Gmask, &Bmask, &Amask);
86 log_info("SW buffer format: %s %d-bit R=0x%08x G=0x%08x B=0x%08x A=0x%08x",
87 SDL_GetPixelFormatName(getFormat()),
88 bpp, Rmask, Gmask, Bmask, Amask);
89
90 logWindowInfo();
91 }
92
93 void IOGfxDisplay::logWindowInfo() {
94 log_info("Video driver: %s", SDL_GetCurrentVideoDriver());
95 log_info("Video fall-back surface (unused): %s",
96 SDL_GetPixelFormatName(SDL_GetWindowPixelFormat(window)));
97 if (0) {
98 // Segfaults on quit:
99 //SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "opengl");
100 SDL_Surface* window_surface = SDL_GetWindowSurface(window);
101 log_info("Video fall-back surface (unused): %s",
102 SDL_GetPixelFormatName(window_surface->format->format));
103 }
104 }
105
106
107
108 Uint32 IOGfxDisplay::getFormat() {
109 if (!truecolor)
110 return SDL_PIXELFORMAT_INDEX8;
111 else
112 return SDL_PIXELFORMAT_RGB888;
113 }
114
115 void IOGfxDisplay::toggleFullScreen() {
116 SDL_SetWindowFullscreen(window,
117 (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)
118 ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
119 // Note: software renderer is buggy in SDL 2.0.2: it doesn't resize the surface
120 }
121
122 void IOGfxDisplay::toggleScreenKeyboard() {
123 if (SDL_HasScreenKeyboardSupport()) {
124 if (!SDL_IsScreenKeyboardShown(window))
125 SDL_StartTextInput();
126 else
127 SDL_StopTextInput();
128 }
129 }
130
131
132 void IOGfxDisplay::flipStretch(IOGfxSurface* backbuffer) {
133 if (backbuffer == NULL)
134 SDL_SetError("IOGfxDisplay::flipStretch: passed a NULL surface");
135 SDL_Rect dstrect;
136 centerScaledSurface(backbuffer, &dstrect);
137 flip(backbuffer, &dstrect, true, true);
138 }
139
140 /* Non-stretched, backbuffer-only blit so we can extract texture buffer */
141 void IOGfxDisplay::flipDebug(IOGfxSurface* backbuffer) {
142 if (backbuffer == NULL)
143 SDL_SetError("IOGfxDisplay::flipDebug: passed a NULL surface");
144 SDL_Rect dstrect = {0,0,backbuffer->w,backbuffer->h};
145 flip(backbuffer, &dstrect, false, false);
146 }
147
148
149 void IOGfxDisplay::centerScaledSurface(IOGfxSurface* surf, SDL_Rect* dst) {
150 double game_ratio = 1.0 * surf->w / surf->h;
151 double disp_ratio = 1.0 * w / h;
152 if (game_ratio < disp_ratio) {
153 // left/right bars
154 dst->w = surf->w * h / surf->h;
155 dst->h = h;
156 dst->x = (w - dst->w) / 2;
157 dst->y = 0;
158 } else {
159 // top/bottom bars
160 dst->w = w;
161 dst->h = surf->h * w / surf->w;
162 dst->x = 0;
163 dst->y = (h - dst->h) / 2;
164 }
165 }
166
167 void IOGfxDisplay::surfToDisplayCoords(IOGfxSurface* backbuffer, int &x, int &y) {
168 SDL_Rect r;
169 centerScaledSurface(backbuffer, &r);
170 // center of scaled pixels:
171 double fx = x+0.5;
172 double fy = y+0.5;
173 x = r.x + (fx / backbuffer->w) * r.w;
174 y = r.y + (fy / backbuffer->h) * r.h;
175 }
176
177
178 SDL_Surface* IOGfxDisplay::screenshot() {
179 SDL_Rect rect = { 0,0,w,h };
180 return screenshot(&rect);
181 }
182
183 void IOGfxDisplay::screenshot(const char* output_file) {
184 SDL_Surface* image = screenshot();
185 SDL_SaveBMP(image, output_file);
186 SDL_FreeSurface(image);
187 }