// 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); }
// 5. Main loop while (running) { // Handle events while (SDL_PollEvent(&event)) { if (event.type == SDL_EVENT_QUIT) { running = false; } else if (event.type == SDL_EVENT_KEY_DOWN) { if (event.key.key == SDLK_ESCAPE) { running = false; } } } sdl3 example
// 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; } // Render SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); //
// Bounce off edges (with radius adjustment) if (ball_x - BALL_RADIUS < 0) { ball_x = BALL_RADIUS; velocity_x = -velocity_x; } else if (ball_x + BALL_RADIUS > WINDOW_WIDTH) { ball_x = WINDOW_WIDTH - BALL_RADIUS; velocity_x = -velocity_x; } if (ball_y - BALL_RADIUS < 0) { ball_y = BALL_RADIUS; velocity_y = -velocity_y; } else if (ball_y + BALL_RADIUS > WINDOW_HEIGHT) { ball_y = WINDOW_HEIGHT - BALL_RADIUS; velocity_y = -velocity_y; } Main loop while (running) { // Handle events
#define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 #define BALL_RADIUS 20
// 2. Create a window SDL_Window* window = SDL_CreateWindow("SDL3 Bouncing Ball", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE); if (!window) { SDL_Log("SDL_CreateWindow Error: %s", SDL_GetError()); SDL_Quit(); return 1; }
– SDL_CreateRenderer is simplified: it no longer requires an index or a “driver” parameter; passing NULL for the second argument auto-selects the best backend. The flags SDL_RENDERER_ACCELERATED ensure we use GPU hardware.