In this section, we will examine the program that resizes and moves the main window with window operations, detects whether the mouse is inside the main window, and switches the main window between full screen and normal view with key operations.
When the program runs, the following operations are performed in addition to the basic operations:
main.c file content will be as follows:
#include <stdio.h>
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
// 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_Texture *texture = NULL; // Texture variable
SDL_Renderer *renderer = NULL; // Renderer variable
int window_width = 640; // Window width
int window_height = 480; // Window height
int window_x; // Window x coordinate
int window_y; // Window y coordinate
bool mouse_in; // Mouse position
bool full_screen; // Full screen control
// Function prototypes
int init_window(void);
void init_vars(void);
void load_img(void);
void process_event(void);
void update_screen(void);
void draw_screen(void);
void destroy_window(void);
int main(int argc, char* argv[])
{
// Create window and renderer
is_running = init_window();
// Initialize variables
init_vars();
// Load image
load_img();
// 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 and a 2D rendering context for the window.
if(!SDL_CreateWindowAndRenderer("SDL3 window", window_width, window_height, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
return false;
}
return true;
}
// Initialization function that runs only once at the beginning of the program
void init_vars(void)
{
window_x = 0;
window_y = 0;
mouse_in = false;
full_screen = false;
SDL_GetWindowPosition(window, &window_x, &window_y);
}
// Load image
void load_img(void)
{
// Load image to texture
texture = IMG_LoadTexture(renderer, "window_bg.png");
if(texture == NULL) {
SDL_Log("IMG_LoadTexture error: %s\n", SDL_GetError());
}
}
// 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) {
// Get new dimensions and repaint on window size change
case SDL_EVENT_WINDOW_RESIZED:
window_width = event.window.data1;
window_height = event.window.data2;
SDL_RenderPresent(renderer);
break;
case SDL_EVENT_WINDOW_EXPOSED:
SDL_RenderPresent(renderer);
break;
case SDL_EVENT_WINDOW_MOVED:
window_x = event.window.data1;
window_y = event.window.data2;
case SDL_EVENT_WINDOW_MOUSE_ENTER:
mouse_in = true;
break;
case SDL_EVENT_WINDOW_MOUSE_LEAVE:
mouse_in = false;
break;
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: // Bir tuşa basıldığını gösterir.
switch(event.key.key) {
case SDLK_F:
if(full_screen) {
SDL_SetWindowFullscreen(window, false);
full_screen = false;
}
else {
SDL_SetWindowFullscreen(window, true);
full_screen = true;
}
break;
case SDLK_ESCAPE: is_running = false; break;
}
break;
}
}
}
// Updates objects in the main window
void update_screen(void)
{
char cdizi[100];
sprintf(cdizi, "Window x: %d y: %d width: %d height: %d mouse: %s",
window_x, window_y, window_width , window_height, mouse_in ? "in" : "out");
SDL_SetWindowTitle(window, cdizi);
}
// 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.
SDL_FRect rect_dst = { 0, 0, window_width, window_height };
SDL_RenderTexture(renderer, texture, NULL, &rect_dst);
// 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_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}