首页 > 解决方案 > SDL2 编译但不打开窗口

问题描述

我正在尝试在 Mac 上的 Eclipse 上设置 SDL2 项目。

我尝试了以下代码,但没有报告任何错误。但是,窗口并没有打开,而是打开了一个“幽灵”程序的图标。

“幽灵”计划:

这

#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;
}

标签: ceclipsemacossdl-2

解决方案


SDL 覆盖 main 但它希望 main 被声明为

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

如果将其声明为 char** 而不是 char* argv[],则不会拾取模板。

延迟不会太大:您将得到的只是一个标题和一个框架。将 SDL_Delay 更改为这样的事件处理程序

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

然后,您可以拖动窗口。它将包含背景。


推荐阅读