-
Notifications
You must be signed in to change notification settings - Fork 1
/
imapp_platform_sdl.cpp
282 lines (242 loc) · 7.68 KB
/
imapp_platform_sdl.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
// imapp: standalone application starter kit
// (SDL is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
#include "imapp.h"
#include "imapp_internal.h"
#include "imgui.h"
#include "imgui_impl_sdl.h"
#ifdef IMAPP_RENDERER_OPENGL
#include "imapp_opengl_loader.h"
#endif
#if defined(IMAPP_SYSTEM_EMSCRIPTEN)
#include <emscripten.h>
#endif
#include <SDL.h>
#ifdef IMAPP_RENDERER_VULKAN
#include <SDL_vulkan.h>
#include <vulkan/vulkan.h>
#endif
namespace
{
SDL_Window* window = NULL;
#ifdef IMAPP_RENDERER_OPENGL
SDL_GLContext gl_context;
#endif
typedef void (* FramebufferSizeCallback)(void*,int,int);
FramebufferSizeCallback framebuffersize_callback = NULL;
} // namespace
namespace ImApp
{
bool SetupPlatform(const char* name, const ImVec2& size)
{
bool succeeded = false;
// Setup SDL
// (Some versions of SDL before <2.0.10 appears to have performance/stalling issues on a minority of Windows systems,
// depending on whether SDL_INIT_GAMECONTROLLER is enabled or disabled.. updating to latest version of SDL is recommended!)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
// Error
}
else
{
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
#if defined(IMAPP_RENDERER_OPENGL)
window_flags = (SDL_WindowFlags)(window_flags | SDL_WINDOW_OPENGL);
// Decide GL+GLSL versions
#if !defined(IMAPP_RENDERER_OPENGL3)
// not OpenGL 3
#elif defined(IMAPP_SYSTEM_EMSCRIPTEN)
// OpenGL ES 2 + Emscripten
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
#elif __APPLE__
// GL 3.2 Core + GLSL 150
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#else
// GL 3.0 + GLSL 130
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#endif
// GL Context version
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, IMAPP_GL_CONTEXT_MAJOR_VERSION);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, IMAPP_GL_CONTEXT_MINOR_VERSION);
// Create window with graphics context
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
#elif defined(IMAPP_RENDERER_VULKAN)
window_flags = (SDL_WindowFlags)(window_flags | SDL_WINDOW_VULKAN);
#endif
SDL_DisplayMode current;
SDL_GetCurrentDisplayMode(0, ¤t);
window = SDL_CreateWindow(
name,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
size.x > 0 ? (int)size.x : 1280,
size.y > 0 ? (int)size.y : 720,
window_flags
);
#ifdef IMAPP_RENDERER_OPENGL
gl_context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
succeeded = InitOpenGLLoader();
#else
succeeded = true;
#endif
}
return succeeded;
}
void ShutdownPlatform()
{
#ifdef IMAPP_RENDERER_OPENGL
SDL_GL_DeleteContext(gl_context);
#endif
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
bool InitPlatform()
{
// Setup Platform bindings
#if defined(IMAPP_RENDERER_OPENGL)
return ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
#elif defined(IMAPP_RENDERER_VULKAN)
return ImGui_ImplSDL2_InitForVulkan(window);
#else
return false;
#endif
}
void CleanupPlatform()
{
ImGui_ImplSDL2_Shutdown();
}
void BeginFramePlatform()
{
ImGui_ImplSDL2_NewFrame(window);
}
void EndFramePlatform()
{
#if defined(IMAPP_RENDERER_OPENGL)
SDL_GL_SwapWindow(window);
#endif
}
void UpdateViewportPlatform()
{
#ifdef IMGUI_HAS_VIEWPORT
// Update and Render additional Platform Windows
ImGuiIO& io = ImGui::GetIO();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
#if defined(IMAPP_RENDERER_OPENGL)
// (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
// For this specific demo app we could also call SDL_GL_MakeCurrent(window, gl_context) directly)
SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
#endif
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
#if defined(IMAPP_RENDERER_OPENGL)
SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
#endif
}
#endif
}
bool ProcessEventPlatform()
{
bool processed = false;
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
SDL_Event event;
while (SDL_PollEvent(&event))
{
processed = ImGui_ImplSDL2_ProcessEvent(&event);
if (!processed)
{
if (event.type == SDL_QUIT)
{
processed = true;
RequestQuit();
}
else if (event.type == SDL_WINDOWEVENT)
{
#ifndef IMGUI_HAS_VIEWPORT
if (event.window.windowID != SDL_GetWindowID(window))
{
// not main window
}
else
#endif
if (event.window.event == SDL_WINDOWEVENT_CLOSE)
{
processed = true;
RequestQuit();
}
else if (event.window.event == SDL_WINDOWEVENT_RESIZED)
{
if (framebuffersize_callback)
{
framebuffersize_callback(window, (int)event.window.data1, (int)event.window.data2);
processed = true;
}
}
}
else
{
// none.
}
}
}
return processed;
}
void GetFramebufferSize(int &width, int &height)
{
SDL_GetWindowSize(window, &width, &height);
}
void SetFramebufferSizeCallback(void* callback)
{
framebuffersize_callback = (FramebufferSizeCallback)callback;
}
void *GetProcAddress(const char* proc_name)
{
return SDL_GL_GetProcAddress(proc_name);
}
const char** GetInstanceExtensions(unsigned int* extensions_count)
{
#ifdef IMAPP_RENDERER_VULKAN
SDL_Vulkan_GetInstanceExtensions(window, extensions_count, NULL);
const char** extensions = new const char*[*extensions_count];
SDL_Vulkan_GetInstanceExtensions(window, extensions_count, extensions);
return extensions;
#else
return NULL;
#endif
}
void ReleaseInstanceExtensions(const char** extensions)
{
#ifdef IMAPP_RENDERER_VULKAN
delete[] extensions;
#endif
}
int CreateWindowSurface(void* instance, const void* allocator, void* surface)
{
#ifdef IMAPP_RENDERER_VULKAN
VkResult err;
if (SDL_Vulkan_CreateSurface(window, (VkInstance)instance, (VkSurfaceKHR*)surface) == 0)
{
err = VK_NOT_READY;
}
else
{
err = VK_SUCCESS;
}
return err;
#else
return 0;
#endif
}
}