简体   繁体   中英

SDL2 compiles but doen't open window

I am trying to setup a SDL2 project on Eclipse on Mac.

I tried the following code and I have no errors reported. However, the window does not open but the icon of a "ghost" program that opens.

The "ghost" program:

“幽灵”计划

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

int main(int argc, char** argv)
{
    if (SDL_Init(SDL_INIT_VIDEO) != 0 )
    {
        fprintf(stdout,"Failed to initialize the SDL (%s)\n",SDL_GetError());
        return -1;
    }

    {
        SDL_Window* pWindow = NULL;
        pWindow = SDL_CreateWindow("My first SDL2 application",SDL_WINDOWPOS_UNDEFINED,
                                                                  SDL_WINDOWPOS_UNDEFINED,
                                                                  640,
                                                                  480,
                                                                  SDL_WINDOW_SHOWN);

        if( pWindow )
        {
            SDL_Delay(3000);

            SDL_DestroyWindow(pWindow);
        }
        else
        {
            fprintf(stderr,"Error creating the window: %s\n",SDL_GetError());
        }
    }

    SDL_Quit();

    return 0;
}

SDL overwrites the main but it expects main to be declared as

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

if you declare it as char** instead of char* argv[], the template will not be picked up.

The delay won't do very much: all you will get is a title and a frame. Change the SDL_Delay to an event handler like this

bool running = true;
while (running)
{
    SDL_Event e;
    while (SDL_PollEvent(&e) != 0)
    {
        if (e.type == SDL_QUIT)
        {
            running = false;
            break;
        }
    }
 }

You can then drag the window around. It will contain the the background.

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