窗口关闭太快

Window closes too fast

本文关键字:窗口      更新时间:2023-10-16

所以我希望这段代码创建一个窗口,在它上面有一个图像(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;
}

您只初始化了视频子系统;如果你想使用SDL_Delay和其他与时间相关的功能,你需要初始化定时器子系统。

SDL_Init(SDL_INIT_VIDEO)更改为SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)

第一行应该是…

if (init())

调用'if (!init())'只在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;
}

这里你有工作的代码,所有的意见和其他答案的建议:

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