簡體   English   中英

窗戶關得太快

[英]Window closes too fast

因此,我希望此代碼創建一個帶有圖像的窗口(Hello World) ,然后在5秒鍾后使用SDL2,Visual Studio和C ++退出。 我認為應該足夠簡單。 我寫出了代碼,並且在構建它時沒有錯誤,但是問題是窗口一創建就退出。 我最初以為添加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;
}

您僅初始化了Video子系統。 如果要使用SDL_Delay和其他與時間有關的功能,則需要初始化Timer子系統。

將您的SDL_Init(SDL_INIT_VIDEO)更改為SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)

您的第一行應該是...

if (init())

僅當init()失敗時,才調用“ if(!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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM