窗口关闭太快

时间:2015-07-22 19:06:32

标签: c++ visual-studio window sdl-2

所以我希望这段代码创建一个带有图像的窗口(hello world)然后使用SDL2,visual studio和c ++在5秒后退出。我认为它应该很简单。我写了代码,当我构建它时没有错误,但问题是窗口一旦创建就退出。我认为添加SDL_Delay(5000)会产生预期的效果,但我想这不会那样。谁能告诉我为什么会这样?

#include <SDL.h>
#include <iostream>

bool init();
bool load_media();
void close();
const int s_height = 300;
const int s_width = 400;

SDL_Window* new_window = NULL;
SDL_Surface* new_surface = NULL;
SDL_Surface* new_image = NULL;

using namespace std;

bool init()
{
    bool success = true;
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        cout << "couldn't initialize" << endl;
        success = false;
    }
    else
    {
        new_window = SDL_CreateWindow(
            "SDL Tutorial 2", 
            SDL_WINDOWPOS_UNDEFINED, 
            SDL_WINDOWPOS_UNDEFINED, 
            s_width, 
            s_height, 
            SDL_WINDOW_SHOWN);
        if (new_window == NULL)
        {
            cout << "there's no window" << endl;
            success = false;
        }
        else
        {
            new_surface = SDL_GetWindowSurface(new_window);
        }

    }
    return success;
}

bool load_media()
{
    bool success = true;
    new_image = SDL_LoadBMP("SDL Tutorial 2/hello_world.bmp");
    if (new_image == NULL)
    {
        cout << "couldn't load image" << endl;
        success = false;
    }
    return success;
}

void close()
{
    SDL_FreeSurface(new_image);
    SDL_DestroyWindow(new_window);
    SDL_Quit;

}


int main(int argc, char *argv[])
{
    if (!init())
    {
        cout << "FAILED to Initialize!" << endl;
        if (!load_media())
        {
            cout << "FAILED to Load Media!" << endl;
        }
        else
        {
            SDL_BlitSurface(new_image, NULL, new_surface, NULL);
            SDL_UpdateWindowSurface(new_window);
            SDL_Delay(5000);
            SDL_Quit;
        }
    }

close();
return 0;
}

3 个答案:

答案 0 :(得分:2)

您只初始化了视频子系统;如果要使用SDL_Delay和其他与时间相关的函数,则需要初始化Timer子系统。

将您的SDL_Init(SDL_INIT_VIDEO)更改为SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)

答案 1 :(得分:2)

你的第一行应该是......

if (init())

调用&#39; if(!init())&#39;仅当init()失败时才继续执行其余代码。 我认为窗口会短暂出现,因为在init()中,窗口已创建,但代码的其余部分(包括时间)被跳过,窗口立即关闭。

我会修改为:

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

        //other code executes if init() is successful
        SDL_Delay(5000);
    }

    else {
        cout << "Failed to initialize!";
    }

    close();
    return 0;
}

答案 2 :(得分:1)

在这里,您可以获得有效的代码,以及评论和其他答案中的所有建议:

#include <SDL.h>
#include <iostream>

int const s_height = 300;
int const s_width  = 400;

SDL_Window  *new_window  = NULL;
SDL_Surface *new_surface = NULL;
SDL_Surface *new_image   = NULL;

using namespace std;

bool init(void) {
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
        cout << "couldn't initialize" << endl;
        return false;
    }
    new_window = SDL_CreateWindow("SDL Tutorial 2", 
                                  SDL_WINDOWPOS_UNDEFINED, 
                                  SDL_WINDOWPOS_UNDEFINED, 
                                  s_width, 
                                  s_height, 
                                  SDL_WINDOW_SHOWN
    );
    if (new_window == NULL) {
        cout << "there's no window" << endl;
        return false;
    }

    new_surface = SDL_GetWindowSurface(new_window);
    if (new_surface == NULL) {
        cout << "there's no surface" << endl;
        return false;
    }
    return true;
}

bool load_media(void) {
    new_image = SDL_LoadBMP("SDL Tutorial 2/hello_world.bmp");
    if (new_image == NULL) {
        cout << "couldn't load image" << endl;
        return false;
    }
    return true;
}

void finish(void) {
    if (new_image) {
        SDL_FreeSurface(new_image);
    }
    if (new_window) {
        SDL_DestroyWindow(new_window);
    }
    SDL_Quit();
}

int main(int argc, char *argv[]) {
    if (init()) {
        if (load_media()) {
            SDL_BlitSurface(new_image, NULL, new_surface, NULL);
            SDL_UpdateWindowSurface(new_window);
            SDL_Delay(5000);
            finish();
        } else {
            cout << "FAILED to Load Media!" << endl;
        }
    } else {
        cout << "FAILED to Initialize!" << endl;
    }
    return 0;
}