[SOLVED] Issue closing window with SDL2 in C

Issue

I have this code that is setting up a window, along with the close button functionality.

#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>

int initialize();
void handle_input();

typedef struct {
    SDL_Renderer *renderer;
    SDL_Window *window;
    bool running;
    int FPS;
    int width;
    int height;
    bool close_requested;
} Game;

Game game = {
  .running = true,
  .FPS = 60,
  .width = 600,
  .height = 600,
  .close_requested = false,
};

int main(int argc, char* argv[]) {

    initialize();
    handle_input();

    while(game.running) { //Game loop
        SDL_SetRenderDrawColor(game.renderer, 255, 0, 0, 255);
        SDL_RenderPresent(game.renderer);
        SDL_Delay(1000/game.FPS);
    } //End of game loop

    SDL_DestroyRenderer(game.renderer);
    SDL_DestroyWindow(game.window);
    SDL_Quit();

    return 0;
}

int initialize() {

    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) { //return 0 on success
        printf("error initializing SDL: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Window* window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, game.width, game.height, 0); //creates window
    if (!window) {
        printf("error creating window: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC; //creates a renderer
    game.renderer = SDL_CreateRenderer(window, -1, render_flags);
    if (!game.renderer) {
        printf("error creating renderer: %s\n", SDL_GetError());
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }
}

void handle_input() {

    SDL_Event event;

    while (!game.close_requested) { //close button
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                game.close_requested = true;
                game.running = false;
            }
        }
    }
}

However, whenever I try to run this program, the window won’t even open and I’m getting 0 errors in the console, besides a warning that states control may reach end of non-void function at the initialize() function. What does that warning mean? What is going on with my code? What did I do wrong?

Solution

I see 2 issues here:

  1. you call handle_input() in your main before you enter the main loop and it gets stuck there until you exit, so SDL_RenderPresent() is never called.
  2. the initialize function doesn’t return anything in case of success, this is why you see the warning.

Try with the following changes, in main() move your input handling inside the loop:

int main(int argc, char* argv[]) {

    initialize();

    while(game.running && !game.close_requested) {
        handle_input(); // <-- move here
        SDL_SetRenderDrawColor(game.renderer, 255, 0, 0, 255);
        SDL_RenderPresent(game.renderer);
        SDL_Delay(1000/game.FPS);
    }
    // the rest...
}

In initialize() add a return 0; at the end.

In handle_input() remove the outer loop:

void handle_input() {
    SDL_Event event;

    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT) {
            game.close_requested = true;
            game.running = false;
        }
    }
}

Answered By – Shlomo Gottlieb

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.