SDL3#
Example's page
This page describes an example listed in custom_windowing_toolkits category.
This code demonstrates how to integrate the AUI Framework with SDL3 to create a window with OpenGL rendering.
Overview#
This application creates a simple GUI window using SDL3 for window management and input handling, while AUI Framework handles the UI components and OpenGL rendering. The result is a window displaying "Hello, World!" text and a clickable button.
Core Components#
EmbedRenderingContext#
|
Purpose: This structure acts as a bridge between AUI's rendering system and the SDL3/OpenGL environment.
Key responsibilities:
- m_renderer: Holds the OpenGL renderer instance
- beginPaint(): Prepares the framebuffer for drawing by binding it and clearing it with white color
- endPaint(): Finalizes the drawing operations
- renderer(): Provides access to the OpenGL renderer
EmbedWindow#
|
Purpose: Represents the main application window and manages the connection between SDL3 and AUI Framework.
Key members:
sdl_window: The SDL3 window handlegl_context: The OpenGL context for renderingclose: Flag to signal when the application should exit
Methods:
init(): Initializes the AUI window system with the rendering context, sets viewport size based on actual pixel dimensions, and configures DPI scaling for high-resolution displays~EmbedWindow(): Destructor that properly cleans up SDL resources (GL context and window)
Input Handling#
sdlToAInput()#
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 | |
Purpose: Converts SDL3 key code constants to AUI's AInput::Key format.
sdlToAPointer()#
|
Purpose: Converts SDL3 mouse button constants to AUI's pointer index format.
Mappings:
SDL_BUTTON_LEFT→ Left mouse buttonSDL_BUTTON_MIDDLE→ Middle mouse buttonSDL_BUTTON_RIGHT→ Right mouse button
handleSDLEvent()#
|
Purpose: Central event dispatcher that processes all SDL events and forwards them to the AUI window.
Handled events:
SDL_EVENT_QUIT: Sets the close flag to terminate the applicationSDL_EVENT_WINDOW_RESIZED/SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: Updates the viewport size when window dimensions changeSDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED: Updates DPI ratio when the window moves between displays with different scalingSDL_EVENT_MOUSE_MOTION: Forwards mouse movement to AUI's pointer trackingSDL_EVENT_MOUSE_BUTTON_DOWN/UP: Forwards mouse clicks with proper button mappingSDL_EVENT_MOUSE_WHEEL: Forwards scroll wheel events with position and scroll deltaSDL_EVENT_KEY_DOWN/UP/TEXT_INPUT: Forwards keyboard input to ASurface
Main Application (AUI_ENTRY)#
SDL3 Initialization#
Purpose: Initializes SDL3's video subsystem. This must be called before any other SDL functions. If initialization fails, the error is logged and the application exits.
Window Creation#
|
Purpose: Creates the main window with the following properties:
- Title: "AUI + SDL3"
- Initial size: 600×400 pixels
SDL_WINDOW_OPENGL: Enables OpenGL renderingSDL_WINDOW_RESIZABLE: Allows the user to resize the windowSDL_WINDOW_HIGH_PIXEL_DENSITY: Supports high-DPI displays (Retina, 4K, etc.)
OpenGL Context Setup#
|
Purpose: Creates an OpenGL context and makes it current for rendering operations. This is necessary before any OpenGL calls can be made.
Renderer Setup#
|
Purpose: Creates the rendering pipeline by: 1. Loads OpenGL function pointers using SDL's procedure address function. 2. Instantiating the OpenGL renderer 3. Creating the rendering context wrapper 4. Linking them together 5. Initializing the AUI window with this rendering setup
UI Content Declaration#
Purpose: Defines the UI layout using AUI's declarative syntax:
Centered: Centers the content in the windowVertical: Arranges children verticallyLabel: Displays "Hello, World!" textButton: Creates a clickable button with:
This is similar to modern declarative UI frameworks like SwiftUI or React.
Main Event Loop#
|
Event Processing#
|
Purpose: Polls and processes all pending SDL events (mouse, keyboard, window events) in the queue. Non-blocking - returns immediately if no events are available.
Rendering#
|
Purpose: Conditionally renders the UI only when needed (dirty flag system): 1. requiresRedraw(): Checks if the UI needs updating (e.g., after user interaction or animation) 2. ARenderContext: Creates a rendering context with no clipping and the OpenGL renderer 3. window.render(): Executes the actual drawing operations 4. SDL_GL_SwapWindow(): Swaps the front and back buffers to display the newly rendered frame (double buffering)
Frame Rate Limiting#
Purpose: Limits the frame rate to match the display's refresh rate to:
- Prevent unnecessary CPU/GPU usage
- Synchronize with the monitor's refresh cycle
- Save battery on laptops
- Reduce heat generation
How it works: 1. Gets the display mode for the monitor showing the window 2. Calculates delay time based on refresh rate (e.g., 60Hz → ~16ms delay) 3. Sleeps the thread for that duration
Example: For a 60Hz display: 1000ms / 60 = ~16.67ms delay per frame
Source Code#
CMakeLists.txt#
|
src/main.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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |