简体   繁体   中英

Window closes too fast

So I want this code to create a window with an image on it (hello world) and then quit after 5 seconds using SDL2, visual studio, and c++. I thought it should be simple enough. I wrote out the code and when I built it there were no errors, however the problem is that the window quits as soon as it's created. I thought intially that adding SDL_Delay(5000) would cause the desired effect but I guess it didn't work that way. Can anyone tell me as to why this is?

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

You only initialized the Video sub-system; you need to initialize the Timer sub-system if you want to use SDL_Delay and other time-related functions.

Change your SDL_Init(SDL_INIT_VIDEO) to SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)

Your first line should be...

if (init())

calling 'if (!init())' continues to the rest of the code only if init() fails. I think the window briefly appears because in init(), the window is created, but the rest of your code (including timing) is skipped and the window closes right away.

I would revise to:

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

Here you have the code that works, with all the suggestions from comments and other answers:

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

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