// sdl3_bounce.c // Compile (Linux/macOS): gcc sdl3_bounce.c -o bounce `pkg-config --cflags --libs sdl3` // Compile (Windows with vcpkg): cl sdl3_bounce.c /Ipath\to\SDL3\include /link path\to\SDL3\lib\SDL3.lib #include <SDL3/SDL.h> #include <stdio.h> #include <stdbool.h>
// 3. Create a renderer for accelerated 2D SDL_Renderer* renderer = SDL_CreateRenderer(window, NULL, SDL_RENDERER_ACCELERATED); if (!renderer) { SDL_Log("SDL_CreateRenderer Error: %s", SDL_GetError()); SDL_DestroyWindow(window); SDL_Quit(); return 1; } sdl3 example
ball_x += velocity_x * delta_time; ball_y += velocity_y * delta_time; // sdl3_bounce
– SDL_SetRenderDrawColor sets the drawing color. SDL_RenderClear fills the whole window with black. We then draw a filled rectangle (representing the ball) in blue. Note: SDL3’s renderer still does not include a native filled circle primitive, so a rectangle suffices for demonstration. Finally, SDL_RenderPresent swaps the buffers to show the frame. We then draw a filled rectangle (representing the
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; }
– 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.
– 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 .