– SDL3 has renamed many event types. The quit event is now SDL_EVENT_QUIT (instead of SDL_QUIT ). Key events follow a similar pattern: SDL_EVENT_KEY_DOWN and the key code is accessed via event.key.key (where SDLK_ESCAPE is unchanged). The event loop is non-blocking thanks to SDL_PollEvent .
// Render SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // black background SDL_RenderClear(renderer); // Draw a filled circle (using SDL_RenderFillRect would be simpler, but we approximate) // For a real circle, we'd use a texture, but SDL3's renderer still lacks native circle. // Let's draw a simple rectangle to keep the example focused. SDL_FRect ball_rect = { ball_x - BALL_RADIUS, ball_y - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2 }; SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); // blue SDL_RenderFillRect(renderer, &ball_rect); // In SDL3, we call SDL_RenderPresent (same as SDL2) SDL_RenderPresent(renderer); // Frame rate control (optional: ~60 FPS) SDL_Delay(16); }
– SDL_CreateWindow now takes width, height, and flags directly, omitting the separate x,y parameters (defaulting to centered). The SDL_WINDOW_RESIZABLE flag allows the user to resize the window. The window title is set to “SDL3 Bouncing Ball”. sdl3 example
#define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 #define BALL_RADIUS 20
int main(int argc, char* argv[]) { // 1. Initialize SDL3 video subsystem only if (!SDL_Init(SDL_INIT_VIDEO)) { SDL_Log("SDL_Init Error: %s", SDL_GetError()); return 1; } – SDL3 has renamed many event types
– Simple bounding-box collision with the window edges. Because the ball is represented by a rectangle for simplicity, we adjust the bounce condition to consider the radius. The velocity vector is inverted on collision, and the ball is repositioned to prevent sticking.
// Update based on delta time Uint64 current_time = SDL_GetTicks(); float delta_time = (current_time - last_time) / 1000.0f; if (delta_time > 0.05f) delta_time = 0.05f; // clamp for safety last_time = current_time; The event loop is non-blocking thanks to SDL_PollEvent
– We store position and velocity as floats for smooth sub-pixel movement. The key addition in SDL3’s API is the use of SDL_GetTicks() returning Uint64 (milliseconds), which is far less prone to overflow than SDL2’s 32-bit value. Delta time calculation ensures the ball moves at a consistent speed regardless of frame rate.