-
Notifications
You must be signed in to change notification settings - Fork 277
/
sdl2_display.cpp
376 lines (314 loc) · 8.71 KB
/
sdl2_display.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// SHE library
// Copyright (C) 2021 LibreSprite contributors
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "she/she.h"
#include "she/system.h"
#include "base/concurrent_queue.h"
#include "base/exception.h"
#include "base/string.h"
#include "she/common/system.h"
#include "she/logger.h"
#include "she/native_cursor.h"
#include "she/sdl2/sdl2_display.h"
#include "she/sdl2/sdl2_surface.h"
#if __has_include(<SDL2/SDL.h>)
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
#else
#include <SDL.h>
#include <SDL_syswm.h>
#endif
#include <sstream>
#include <iostream>
#include <unordered_map>
#include <memory>
#include <vector>
namespace she {
SDL2Display* unique_display = NULL;
int display_scale;
namespace sdl {
she::SDL2Surface* screen;
she::SDL2Surface* tempSurface;
extern bool isMaximized;
extern bool isMinimized;
std::unordered_map<int, SDL2Display*> windowIdToDisplay;
}
SDL2Display::SDL2Display(int width, int height, int scale) :
m_window(nullptr),
m_renderer(nullptr),
m_surface(nullptr),
m_scale(0),
m_nativeCursor(kNoCursor),
m_restoredWidth(0),
m_restoredHeight(0) {
unique_display = this;
width = 800;
height = 600;
instance()->gfx([&]{
m_window = SDL_CreateWindow("",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width, height,
SDL_WINDOW_RESIZABLE);
if (!m_window)
throw DisplayCreationException(SDL_GetError());
if (gpu)
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED);
sdl::windowIdToDisplay[SDL_GetWindowID(m_window)] = this;
SDL_GetWindowSize(m_window, &width, &height);
m_width = width;
m_height = height;
SDL_ShowCursor(SDL_DISABLE);
}, true);
setScale(scale);
}
SDL2Display::~SDL2Display()
{
unique_display = NULL;
if (sdl::tempSurface)
sdl::tempSurface->dispose();
if (m_window) {
sdl::windowIdToDisplay.erase(SDL_GetWindowID(m_window));
m_surface->dispose();
if (m_doublebuffer)
m_doublebuffer->dispose();
if (m_renderer)
SDL_DestroyRenderer(m_renderer);
SDL_DestroyWindow(m_window);
}
}
void SDL2Display::dispose()
{
delete this;
}
void SDL2Display::toggleFullscreen()
{
m_isFullscreen = !m_isFullscreen;
SDL_SetWindowFullscreen(m_window, m_isFullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
}
bool SDL2Display::setIcon(Surface* surface) {
SDL_SetWindowIcon(m_window, static_cast<SDL_Surface*>(static_cast<SDL2Surface*>(surface)->nativeHandle()));
return true;
}
int SDL2Display::width() const
{
return m_width;
}
int SDL2Display::height() const
{
return m_height;
}
void SDL2Display::setWidth(int newWidth)
{
m_width = newWidth;
}
void SDL2Display::setHeight(int newHeight)
{
m_height = newHeight;
}
int SDL2Display::originalWidth() const
{
return m_restoredWidth > 0 ? m_restoredWidth: width();
}
int SDL2Display::originalHeight() const
{
return m_restoredHeight > 0 ? m_restoredHeight: height();
}
void SDL2Display::setOriginalWidth(int width)
{
m_restoredWidth = width;
}
void SDL2Display::setOriginalHeight(int height)
{
m_restoredHeight = height;
}
int SDL2Display::scale() const
{
return m_scale;
}
void SDL2Display::setScale(int scale)
{
ASSERT(scale >= 1);
if (m_scale == scale)
return;
m_scale = scale;
recreateSurface();
}
void SDL2Display::recreateSurface()
{
if (!m_scale)
return;
auto newSurface = new SDL2Surface(width() / m_scale, height() / m_scale, SDL2Surface::DeleteAndDestroy);
if (m_surface) {
m_surface->blitTo(newSurface, 0, 0, 0, 0, width(), height());
m_surface->dispose();
}
m_dirty = true;
m_surface = newSurface;
she::sdl::screen = newSurface;
#ifdef EMSCRIPTEN
newSurface = new SDL2Surface(width() / m_scale, height() / m_scale, SDL2Surface::DeleteAndDestroy);
if (m_doublebuffer) {
m_doublebuffer->blitTo(newSurface, 0, 0, 0, 0, width(), height());
m_doublebuffer->dispose();
}
m_doublebuffer = newSurface;
#endif
}
Surface* SDL2Display::getSurface()
{
return m_surface;
}
void SDL2Display::present()
{
if (!m_dirty || !she::instance()->isGfxThread() || !m_surface)
return;
m_dirty = false;
if (m_renderer) {
#ifdef EMSCRIPTEN
auto texture = static_cast<SDL2Surface*>(m_doublebuffer)->getTexture(nullptr);
#else
SDL_Rect empty{0, 0, 0, 0};
auto texture = static_cast<SDL2Surface*>(m_surface)->getTexture(&empty);
#endif
SDL_RenderCopy(m_renderer, texture, nullptr, nullptr);
SDL_RenderPresent(m_renderer);
} else
SDL_UpdateWindowSurface(m_window);
}
void SDL2Display::flip(const gfx::Rect& bounds)
{
m_dirty = true;
if (!she::instance()->isGfxThread()) {
SDL_Rect rect {bounds.x, bounds.y, bounds.w, bounds.h};
SDL_Rect dst { rect.x, rect.y, rect.w, rect.h };
SDL_BlitScaled((SDL_Surface*)m_surface->nativeHandle(), &rect,
(SDL_Surface*)m_doublebuffer->nativeHandle(), &dst);
return;
}
SDL_Rect rect {bounds.x, bounds.y, bounds.w, bounds.h};
if (m_renderer) {
static_cast<SDL2Surface*>(m_surface)->getTexture(&rect);
return;
}
auto nativeSurface = SDL_GetWindowSurface(m_window);
SDL_Rect dst {
rect.x * m_scale, rect.y * m_scale,
rect.w * m_scale, rect.h * m_scale
};
SDL_BlitScaled((SDL_Surface*)m_surface->nativeHandle(), &rect, nativeSurface, &dst);
}
void SDL2Display::maximize()
{
SDL_MaximizeWindow(m_window);
}
bool SDL2Display::isMaximized() const
{
return sdl::isMaximized;
}
bool SDL2Display::isMinimized() const
{
return sdl::isMinimized;
}
void SDL2Display::setTitleBar(const std::string& title)
{
SDL_SetWindowTitle(m_window, title.c_str());
}
NativeCursor SDL2Display::nativeMouseCursor()
{
return m_nativeCursor;
}
std::vector<std::shared_ptr<SDL_Cursor>> m_cursors;
void applyCursor(SDL_SystemCursor id)
{
if (id >= m_cursors.size()) {
m_cursors.resize(id + 1);
}
if (!m_cursors[id]) {
auto ptr = SDL_CreateSystemCursor(id);
m_cursors[id] = std::shared_ptr<SDL_Cursor>(ptr, [](SDL_Cursor* ptr){
if (ptr) {
SDL_FreeCursor(ptr);
}
});
}
SDL_SetCursor(m_cursors[id].get());
SDL_ShowCursor(SDL_ENABLE);
}
bool SDL2Display::setNativeMouseCursor(NativeCursor cursor)
{
switch (cursor) {
case she::kArrowCursor: applyCursor(SDL_SYSTEM_CURSOR_ARROW); return true;
case she::kIBeamCursor: applyCursor(SDL_SYSTEM_CURSOR_IBEAM); return true;
case she::kWaitCursor: applyCursor(SDL_SYSTEM_CURSOR_WAIT); return true;
case she::kLinkCursor: applyCursor(SDL_SYSTEM_CURSOR_HAND); return true;
case she::kForbiddenCursor: applyCursor(SDL_SYSTEM_CURSOR_NO); return true;
case she::kMoveCursor: applyCursor(SDL_SYSTEM_CURSOR_SIZEALL); return true;
default: break;
}
SDL_ShowCursor(SDL_DISABLE);
return false;
}
void SDL2Display::setMousePosition(const gfx::Point& position)
{
SDL_WarpMouseInWindow(
m_window,
m_scale * position.x,
m_scale * position.y);
}
void SDL2Display::captureMouse()
{
SDL_CaptureMouse(SDL_TRUE);
}
void SDL2Display::releaseMouse()
{
SDL_CaptureMouse(SDL_FALSE);
}
std::string SDL2Display::getLayout()
{
int x, y;
SDL_GetWindowPosition(m_window, &x, &y);
return "2 " + std::to_string(x) + " " + std::to_string(y);
}
void SDL2Display::setLayout(const std::string& layout)
{
std::istringstream s(layout);
int ver, x, y;
s >> ver;
if (ver == 2) {
s >> x >> y;
SDL_SetWindowPosition(m_window, x, y);
}
}
void* SDL2Display::nativeHandle()
{
SDL_SysWMinfo info;
SDL_VERSION(&info.version); /* initialize info structure with SDL version info */
if (!SDL_GetWindowWMInfo(m_window, &info))
return nullptr;
#if defined(SDL_VIDEO_DRIVER_WINDOWS)
return reinterpret_cast<void*>(info.info.win.window);
#endif
#if defined(SDL_VIDEO_DRIVER_X11)
return reinterpret_cast<void*>(info.info.x11.window);
#endif
#if defined(SDL_VIDEO_DRIVER_DIRECTFB)
return reinterpret_cast<void*>(info.info.dfb.window);
#endif
#if defined(SDL_VIDEO_DRIVER_COCOA)
return reinterpret_cast<void*>(info.info.cocoa.window);
#endif
#if defined(SDL_VIDEO_DRIVER_UIKIT)
return reinterpret_cast<void*>(info.info.uikit.window);
#endif
#if defined(SDL_VIDEO_DRIVER_ANDROID)
return reinterpret_cast<void*>(info.info.android.window);
#endif
return nullptr;
}
} // namespace she