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 |
#include <SDL2/SDL.h> #include <iostream> // Screen dimensions const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; // Function to initialize SDL and create a window SDL_Window* initWindow(SDL_Renderer** renderer) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl; return nullptr; } SDL_Window* window = SDL_CreateWindow("SDL2 Graphics Simulation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == nullptr) { std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl; SDL_Quit(); return nullptr; } *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (*renderer == nullptr) { std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl; SDL_DestroyWindow(window); SDL_Quit(); return nullptr; } return window; } // Main function int main() { SDL_Renderer* renderer = nullptr; SDL_Window* window = initWindow(&renderer); if (window == nullptr) { return 1; } // Main loop flag bool quit = false; SDL_Event e; // Main loop while (!quit) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; } } // Clear screen SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // White background SDL_RenderClear(renderer); // Draw a red rectangle SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // Red color SDL_Rect fillRect = { 100, 100, 200, 150 }; SDL_RenderFillRect(renderer, &fillRect); // Draw a blue circle SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); // Blue color int centerX = 400, centerY = 300, radius = 100; for (int y = -radius; y <= radius; y++) { for (int x = -radius; x <= radius; x++) { if (x*x + y*y <= radius*radius) { SDL_RenderDrawPoint(renderer, centerX + x, centerY + y); } } } // Update screen SDL_RenderPresent(renderer); } // Clean up SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; } |
Explanation:
- Initialization:
initWindow(SDL_Renderer** renderer)
: Initializes SDL, creates a window, and sets up a renderer. Handles errors and prints messages if initialization fails.
Advertisement - Main Function:
- Creates a window and renderer using
initWindow()
Advertisement - Enters the main loop where it handles events (e.g., window close) and updates the graphics.
- Clears the screen with a white color.
- Draws a red rectangle using
SDL_RenderFillRect()
. - Draws a blue circle by iterating over pixel coordinates and checking if they fall within the circle’s radius using the equation x2+y2≤r2x^2 + y^2 \leq r^2 Advertisement
- Updates the screen with
SDL_RenderPresent()
.
- Creates a window and renderer using
- Cleanup:
- Destroys the renderer and window, and quits SDL before exiting the program.