In this section, we create a basic template that we use continuously in the SDL3 programs with the C Programming Language. New code lines will be added to this template at each new step.
The program creates a rectangle with dimensions of 160 x 120 pixels, exactly in the middle of the main window it creates.
The template we will create will contain the codes that will perform the operations shown below:
When we run the program, it may seem that the rectangle in the main window is drawn only once, but in fact, unless the program is exited, the rectangle is redrawn with the draw_screen() function at each iteration of the while loop, which is the main loop of the program that runs endlessly.
main.c file content will be as follows:
#include <SDL3/SDL.h>
// Window width and height
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
// Global variables
int is_running = false; // Variable that controls the execution of the main loop of the program
SDL_Window *window = NULL; // Main window variable
SDL_Renderer *renderer = NULL; // Renderer variable
// Function prototypes
int init_window(void); // Create window and renderer
void init_vars(void); // Initialize variables
void process_event(void); // Process events
void update_screen(); // Update values
void draw_screen(void); // Draw screen
void destroy_window(void); // Destroy window
int main(int argc, char* argv[])
{
// Create window and renderer
is_running = init_window();
// Initialize variables
init_vars();
// Main loop
while (is_running) {
process_event();// Processing SDL events (Here keyboard inputs)
update_screen(); // Updating variables
draw_screen(); // Drawing objects on the window (Rendering)
}
// Destroy renderer and SDL window
destroy_window();
return 0;
}
// Create window and renderer
int init_window(void)
{
// Initialize the SDL library.
if(SDL_Init(SDL_INIT_VIDEO) == false) {
SDL_Log("SDL init error: %s\n", SDL_GetError());
return false;
}
// Create a window with the specified dimensions and flags.
window = SDL_CreateWindow("SDL3 window", 640, 480, SDL_WINDOW_OPENGL);
// Check window
if(window == NULL) {
SDL_Log("Window creation error: %s\n", SDL_GetError());
return false;
}
// Create a 2D rendering context for the window.
renderer = SDL_CreateRenderer(window, NULL);
// Check renderer
if(renderer == NULL) {
SDL_Log("Renderer creation error: %s\n", SDL_GetError());
return false;
}
return true;
}
// Initialization function that runs only once at the beginning of the program
void init_vars(void)
{
}
// Function to control SDL events and process keyboard inputs
void process_event(void)
{
SDL_Event event;
// Creating a loop to process user inputs
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT: // Logout action by the user (x button at the top right of the window)
is_running = false; // Terminates the execution of the program main loop.
break;
case SDL_EVENT_KEY_DOWN: // Indicate that a key was pressed.
if(event.key.key == SDLK_ESCAPE) { // Indicates that the pressed key is the Esc key.
is_running = false; // Terminates the execution of the program main loop.
}
break;
}
}
}
// Updates objects in the main window
void update_screen(void)
{
}
// Render function used to draw game objects in the main window
void draw_screen(void)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Set the color used for drawing operations.
SDL_RenderClear(renderer); // Clear the current rendering target with the drawing color.
// Setting rectangle dimensions
SDL_FRect frect = { (WINDOW_WIDTH-160)/2, (WINDOW_HEIGHT-120)/2, 160, 120 };
SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255); // Set the color to fill rectangle.
SDL_RenderFillRect(renderer, &frect); // Fill a rectangle on the current rendering target with the drawing color at subpixel precision.
// Loading all objects drawn one by one to the back buffer to the front buffer at once and loading them onto the screen
// This process prevents each drawn object from being displayed on the screen one by one.
SDL_RenderPresent(renderer); // Update on screen all operations performed since the previous call
}
// Destroy Renderer and SDL window, exit from SDL3
void destroy_window(void)
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}