Sdl3 Example -
– 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.
// 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
– 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”. – We store position and velocity as floats
// 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); } The window title is set to “SDL3 Bouncing Ball”
– 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.