Skip to content

Commit 99decbf

Browse files
Some changes to SDL2 documentation
1 parent 1297dbc commit 99decbf

File tree

1 file changed

+82
-34
lines changed

1 file changed

+82
-34
lines changed

doc/SDL2.md

Lines changed: 82 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,47 @@
33
## General
44

55
Create flags and set attributes for SDL2 relevant for your target backend.
6+
67
```cpp
78
SDL_Init(SDL_INIT_EVERYTHING);
8-
SDL_WindowFlags flags = {};
9+
SDL_WindowFlags flags{};
910

10-
auto window_width = 1280;
11+
auto window_width = 1280;
1112
auto window_height = 720;
1213
```
1314
14-
E.g. set flag for Vulkan
15+
For example, set the flag for Vulkan:
16+
1517
```cpp
1618
flags = flags | SDL_WINDOW_VULKAN
1719
```
1820

19-
Or set flag and attributes for OpenGL 4.6
21+
Or set the flags and attributes for OpenGL 4.6:
22+
2023
```cpp
2124
flags = flags | SDL_WINDOW_OPENGL
2225
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
2326
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
2427
```
2528
26-
No flag needed for DirectX
29+
No flags are needed for DirectX.
30+
31+
Create the window:
2732
28-
Create the window
2933
```cpp
3034
auto window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, flags);
3135
```
3236

33-
Create GL Context if using OpenGL
37+
Create GL Context if using OpenGL:
38+
3439
```cpp
3540
auto gl_context = SDL_GL_CreateContext(window);
3641
// Check if succeeded, etc.
3742
```
3843

39-
If necessary, get a handle to the platform's native window, then set it in the Diligent NativeWindow struct. Example for Windows:
44+
If necessary, get a handle to the platform's native window, then set it in the Diligent NativeWindow struct.
45+
For example, for Windows:
46+
4047
```cpp
4148
SDL_SysWMinfo info;
4249
SDL_VERSION(&info.version);
@@ -45,11 +52,12 @@ If necessary, get a handle to the platform's native window, then set it in the D
4552
native_window.hWnd = info.info.win.window;
4653
```
4754
48-
Finally setup the Diligent swapchain as normal (using the NativeWindow if needed)
55+
Finally, setup the Diligent swapchain as normal (using the NativeWindow if needed).
4956
5057
### Handling window events
5158
52-
Depending on your platform, certain window events may require making some sort of call into your graphics engine.
59+
Depending on your platform, certain window events may require making some calls into your graphics engine.
60+
5361
```cpp
5462
SDL_Event event;
5563
while (true)
@@ -87,14 +95,17 @@ while (true)
8795

8896
## Mac OSX
8997

90-
Set the Vulkan and HighDPI flags when creating window
98+
Set the Vulkan and HighDPI flags when creating window:
99+
91100
```cpp
92101
auto window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, SDL_WINDOW_VULKAN | SDL_WINDOW_ALLOW_HIGHDPI);
93102
```
94103

95104

96-
Copy [SurfaceHelper.mm](https://github.com/DiligentGraphics/DiligentSamples/blob/master/Samples/GLFWDemo/src/SurfaceHelper.mm) file from the GLFW sample into your project.
105+
Copy [SurfaceHelper.mm](https://github.com/DiligentGraphics/DiligentSamples/blob/master/Samples/GLFWDemo/src/SurfaceHelper.mm)
106+
file from the GLFW sample into your project.
97107
Modify the following from:
108+
98109
```objective-c
99110
#define GLFW_EXPOSE_NATIVE_COCOA 1
100111
#include "GLFW/glfw3.h"
@@ -109,7 +120,9 @@ void* GetNSWindowView(GLFWwindow* wnd)
109120
// ...
110121
}
111122
```
123+
112124
to:
125+
113126
```objective-c
114127
#include <SDL.h>
115128
#include <SDL_syswm.h>
@@ -128,7 +141,7 @@ void* GetNSWindowView(SDL_Window* wnd)
128141
}
129142
```
130143

131-
Use the helper function to get the NSWindowView, setting it in the NativeWindow struct. Then create the Vulkan swapchain as normal.
144+
Use the helper function to get the `NSWindowView`, setting it in the `NativeWindow` struct. Then create the Vulkan swapchain as normal.
132145

133146
## Android
134147

@@ -137,6 +150,7 @@ Use the helper function to get the NSWindowView, setting it in the NativeWindow
137150
Follow the [official documentation for setting up an android project](https://github.com/libsdl-org/SDL/blob/main/docs/README-android.md) for SDL2.
138151

139152
#### Optional: Handle devices with screen notch in your derived SDLActivity class:
153+
140154
```java
141155
@Override
142156
protected void onCreate(Bundle savedInstanceState) {
@@ -157,9 +171,8 @@ Follow the [official documentation for setting up an android project](https://gi
157171

158172
### Building Diligent Engine separately
159173

160-
The following is a very rough guide of building Diligent separately to then be used in your own project.
161-
162174
Build Diligent Engine using CMake with an official NDK toolchain:
175+
163176
```bash
164177
mkdir DiligentEngine_android_build
165178
cd DiligentEngine_android_build
@@ -179,7 +192,10 @@ cmake -G"Ninja" \
179192
ninja -j9
180193
```
181194

182-
Setup your build system to be able to find the interface headers and built libraries. Here the variables `DILIGENT_PATH` and `DILIGENT_BUILD_PATH` refer to the locations of your root DiligentEngine repo copy and your build folder respectively.
195+
Setup your build system to be able to find the interface headers and built libraries
196+
Here the variables `DILIGENT_PATH` and `DILIGENT_BUILD_PATH` refer to the locations of your root
197+
DiligentEngine repo copy and your build folder respectively.
198+
183199
```cmake
184200
target_include_directories(app
185201
PRIVATE
@@ -262,11 +278,13 @@ target_link_libraries(app
262278
### Vulkan
263279

264280
Set the Vulkan and fullscreen / resizable window flags when creating the window.
281+
265282
```cpp
266283
auto window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_FULLSCREEN);
267284
```
268285

269286
Then create the swapchain as normal. Keep the swapchain description to be used.
287+
270288
```cpp
271289
auto *pFactoryVk = GetEngineFactoryVk();
272290

@@ -279,15 +297,17 @@ pFactoryVk->CreateSwapChainVk(*device, *immediate_context, SCDesc,
279297
280298
#### Recreate swapchain
281299
282-
When the app goes out of focus, destroy the swapchain
300+
When the app goes out of focus, destroy the swapchain:
301+
283302
```cpp
284303
if (device->GetDeviceInfo().IsVulkanDevice()) {
285304
swapchain.Release();
286305
device->IdleGPU();
287306
}
288307
```
289308

290-
When the app regains focus, create the swapchain again
309+
When the app regains focus, create the swapchain again:
310+
291311
```cpp
292312
if (device->GetDeviceInfo().IsVulkanDevice())
293313
{
@@ -303,7 +323,9 @@ if (device->GetDeviceInfo().IsVulkanDevice())
303323

304324
### OpenGLES
305325

306-
Set the attributes Diligent expects, set the OpenGL and fullscreen / resizable window flags, create the window, create the context, and finally create the swapchain
326+
Set the attributes Diligent expects, set the OpenGL and fullscreen / resizable window flags, create the window, create the context,
327+
and finally create the swapchain:
328+
307329
```cpp
308330
const auto color_size = 8;
309331
const auto depth_size = 24;
@@ -332,15 +354,17 @@ auto gl_context = SDL_GL_CreateContext(window);
332354
// ...
333355
```
334356
335-
When focus is lost, call suspend on the swapchain
357+
When focus is lost, call suspend on the swapchain:
358+
336359
```cpp
337360
if (device->GetDeviceInfo().IsGLDevice())
338361
{
339362
static_cast<IRenderDeviceGLES*>(device.RawPtr())->Suspend();
340363
}
341364
```
342365

343-
When focus is resumed, call resume on the swapchain (Native window pointer not required)
366+
When focus is resumed, call resume on the swapchain (Native window pointer is not required):
367+
344368
```cpp
345369
if (device->GetDeviceInfo().IsGLDevice())
346370
{
@@ -355,12 +379,18 @@ if (device->GetDeviceInfo().IsGLDevice())
355379
- `SDL_GetDisplayMode()` can be used to determine max resolution.
356380

357381
## ImGui
382+
358383
### Compiling and Linking
359-
Whatever is most convenient for your build system, compile and link [imgui_impl_sdl.cpp](https://github.com/DiligentGraphics/imgui/blob/66ad2ad5398cb61433009553e10fd326d13acb84/backends/imgui_impl_sdl.cpp) into your project
384+
385+
Add [imgui_impl_sdl.cpp](https://github.com/DiligentGraphics/imgui/blob/66ad2ad5398cb61433009553e10fd326d13acb84/backends/imgui_impl_sdl.cpp) to your project
360386

361387
### Using the ImGuiImplSDL implementation
388+
362389
#### Setup
363-
Add the [ImGuiImplSDL.hpp](https://github.com/DiligentGraphics/DiligentTools/blob/fbd6c1054744724387e37ae69b1636782f703ca0/Imgui/interface/ImGuiImplSDL.hpp) header included in Diligent. After creating the swapchain, call `ImGuiImplSDL::Create(CI, sdl_window)` and store the result for usage as normal
390+
391+
Include the [ImGuiImplSDL.hpp](https://github.com/DiligentGraphics/DiligentTools/blob/master/Imgui/interface/ImGuiImplSDL.hpp) header.
392+
After creating the swapchain, call `ImGuiImplSDL::Create(CI, sdl_window)` and store the result to be used as normal:
393+
364394
```cpp
365395
#include "Imgui/interface/ImGuiImplSDL.hpp"
366396

@@ -375,29 +405,35 @@ imgui_impl = ImGui::ImplSDL::Create(CI, sdl_window);
375405
```
376406
377407
#### Rendering
378-
Before creating an imgui window for the current frame call `ImGuiImplDiligent::NewFrame()`
408+
409+
Before creating an imgui window for the current frame, call `ImGuiImplDiligent::NewFrame()`
410+
379411
```cpp
380412
auto SCDesc = swapchain->GetDesc();
381413
382-
// You may want to check here if the render size given by SDL is different then
414+
// You may want to check here if the render size given by SDL is different than
383415
// the swapchain's. For example if the window was just resized and the values
384416
// given by SDL_Vulkan_GetDrawableSize() don't match the swapchain's, you might
385-
// want to return early for this frame otherwise ImGui may assert.
417+
// want to return early for this frame, otherwise ImGui may assert.
386418
387419
imgui_impl->NewFrame(SCDesc.Width, SCDesc.Height, SCDesc.PreTransform);
388420
389421
// Begin drawing with ImGui
390422
ImGui::Begin("Debug Menu");
391423
```
392-
Then finally at the end of your other render passes then call `ImGuiImplDiligent::Render()`
424+
425+
At the end of your frame, call `ImGuiImplDiligent::Render()`:
426+
393427
```cpp
394428
imgui_impl->Render(immediate_context);
395429
// Finish rendering
396430
swapchain->Present();
397431
```
398432
399433
### Passing input events
400-
At the start of your main event loop, add a call to `ImGui::ImplSDL::HandleSDLEvent()`. It is not necessary to save the return value
434+
435+
At the start of your main event loop, add a call to `ImGui::ImplSDL::HandleSDLEvent()`. The retun value may be ignored.
436+
401437
```cpp
402438
SDL_Event event{};
403439
auto& io = ImGui::GetIO();
@@ -411,7 +447,9 @@ while (true)
411447
// ...
412448
}
413449
```
414-
Then afterward in the loop you can filter ImGui events as normal
450+
451+
Filter ImGui events as normal:
452+
415453
```cpp
416454
switch(event)
417455
{
@@ -421,20 +459,30 @@ switch(event)
421459
// Do your normal non-imgui logic here
422460
}
423461
break;
462+
424463
case SDL_KeyUp:
425-
// ditto
464+
// ...
465+
break;
466+
426467
case SDL_MOUSEMOTION:
427468
if (!io.WantCaptureMouse)
428469
{
429470
// Do your normal non-imgui logic here
430471
}
472+
break;
473+
431474
case SDL_MOUSEBUTTONDOWN:
432-
// ditto
475+
// ...
476+
break;
477+
433478
case SDL_MOUSEBUTTONUP:
434-
// ditto
479+
// ...
480+
break;
481+
435482
case SDL_MOUSEWHEEL:
436-
// ditto
483+
// ...
484+
break;
437485
}
438486
```
439487

440-
It's possible to use gamepad input as well, but you will have to come up with a way to filter the events
488+
To use gamepad input, filter the corresponding events.

0 commit comments

Comments
 (0)