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