简体   繁体   中英

Window not displaying SDL2

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM