Window not displaying SDL2
Asked Answered
K

1

5

I am using SDL2 for the first time, and when I try to create a window, it's not displaying. The only sight of the window is an icon spawning in my dock (Image of the icon, SDLTest.out is the name of my executable file). I found out that it spawned when SDL_INIT()was called.

I tried updating the window, changing its color and adding the flag SDL_WINDOW_SHOWN, but none of these solutions worked. I even pasted a code from the Internet, but it didn't work better.

Here is the code that I pasted:

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


int main(int argc, char *argv[])
{
    SDL_Window *window = NULL;
    SDL_Renderer *renderer = NULL;
    int status = EXIT_FAILURE;
    SDL_Color orange = {255, 127, 40, 255};
    
    if(0 != SDL_Init(SDL_INIT_VIDEO))
    {
        fprintf(stderr, "Error SDL_Init : %s", SDL_GetError());
        goto Quit;
    }
    window = SDL_CreateWindow("SDL2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                              640, 480, SDL_WINDOW_SHOWN);
    if(NULL == window)
    {
        fprintf(stderr, "Error SDL_CreateWindow : %s", SDL_GetError());
        goto Quit;
    }
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if(NULL == renderer)
    {
        fprintf(stderr, "Error SDL_CreateRenderer : %s", SDL_GetError());
        goto Quit;
    }
    
    if(0 != SDL_SetRenderDrawColor(renderer, orange.r, orange.g, orange.b, orange.a))
    {
        fprintf(stderr, "Error SDL_SetRenderDrawColor : %s", SDL_GetError());
        goto Quit;
    }
    
    if(0 != SDL_RenderClear(renderer))
    {
        fprintf(stderr, "Error SDL_SetRenderDrawColor : %s", SDL_GetError());
        goto Quit;
    }
    
    SDL_Delay(500);
    SDL_RenderPresent(renderer);
    SDL_Delay(500);
    
    status = EXIT_SUCCESS;

Quit:
    if(NULL != renderer)
        SDL_DestroyRenderer(renderer);
    if(NULL != window)
        SDL_DestroyWindow(window);
    SDL_Quit();
    return status;
}

My OS is MacOS 11.6, my compiler GCC, my computer has a graphic card Intel Iris Pro Graphics 6200, and I installed SDL2 using Homebrew.

Can someone help me?

Keating answered 28/12, 2021 at 20:7 Comment(4)
I tested your code in my Linux, and it work: an empty window is shown, then it is filled orange, and then it closes.Rico
This is what it is supposed to do, but this doesn't work on my computer ;(Keating
You need an event loop. Stuff doesn't work without one on some platforms. Continue reading your tutorial, it should be there somewhere.Lion
It worked properly, thank you so much for your answer!Keating
K
7

I just needed an event loop. I added this code and it worked:

    SDL_bool quit = SDL_FALSE;
    while(!quit)
    {
        SDL_RenderPresent(renderer);
        SDL_WaitEvent(&event);
        if(event.type == SDL_QUIT)
            quit = SDL_TRUE;
    }

Thank you to HolyBlackCat for your comment!

Keating answered 28/12, 2021 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.