Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in /var/www/vhosts/bilgigunlugum.net/httpdocs/index.php on line 43
SDL Game Programming

SDL3 Oyun Programlama sayfalarımız yayında...

Ana sayfa > Oyun programlama > SDL3 programming > Window operations

Window operations

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:

  1. At the beginning of the program, the following global variables are first defined:
    • int window_width: Main window width
    • int window_height: Main window height
    • int window_x: Main window x coordinate
    • int window_y: Main window y coordinate
    • bool mouse_in: Mouse position variable
    • bool full_screen: Full screen control variable
  2. In the init_vars() function,
    • Global variables are assigned an initial value.
    • The main window coordinates are assigned to the window_x and window_y variables with the SDL_GetWindowPosition() function.
  3. In the load_img() function, the background image file is loaded into the texture variable with the IMG_LoadTexture() function.
  4. In the process_event() function,
    • With the SDL_EVENT_WINDOW_RESIZED option, the new dimensions of the main window are window_width and window_height in the resizing operations of the main window. variables.
    • With the SDL_EVENT_WINDOW_EXPOSED option, the main window can be drawn directly when it is visible and needs to be redrawn.
    • With the SDL_EVENT_WINDOW_MOVED option, when the main window is moved, the coordinates of the main window are assigned to the window_x and window_y variables.
    • With the SDL_EVENT_WINDOW_MOUSE_ENTER and SDL_EVENT_WINDOW_MOUSE_LEAVE options, the mouse_in variable value is updated by checking whether the mouse is inside the main window.
    • With the SDL_EVENT_KEY_DOWN option, the main window is switched between full screen and normal view when the F key is pressed.
  5. In the update_screen() function, the coordinates, width, height of the main window and whether the mouse is inside the main window are written to the main window title.
  6. In the draw_screen() function,
    • The background texture value is loaded to the screen with the SDL_RenderTexture() function.
    • The drawings and loadings made with the SDL_RenderPresent() function are shown on the screen.

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();
}