C++ SDL2 window 未打开

C++ SDL2 window not opening

我编码了这个。

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

int main(int argc , char** args)
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window* win = SDL_CreateWindow("my window", 100, 100, 640, 480, SDL_WINDOW_SHOWN);

if (!win) 
{
    std :: cout << "Failed to create a window! Error: " << SDL_GetError() << "\n";

}


SDL_Surface* winSurface = SDL_GetWindowSurface(win);



SDL_UpdateWindowSurface(win);

SDL_FillRect(winSurface, NULL, SDL_MapRGB(winSurface->format, 255, 90, 120));

SDL_DestroyWindow(win);
win = NULL;
winSurface = NULL;

return 0;




}

当我编译它时,它会打开 window,然后立即关闭。但是控制台没有。这是我的控制台的屏幕截图(也许它可以帮助解决问题?)

是否有任何解决方案可以使 Window 不关闭?

Would there be any solution to get the Window to not close?

启动事件处理循环并处理一些事件:

// g++ main.cpp `pkg-config --cflags --libs sdl2`
#include <SDL.h>
#include <iostream>

int main( int argc, char** argv )
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Window* win = SDL_CreateWindow("my window", 100, 100, 640, 480, SDL_WINDOW_SHOWN);

    if (!win)
    {
        std :: cout << "Failed to create a window! Error: " << SDL_GetError() << "\n";
    }

    SDL_Surface* winSurface = SDL_GetWindowSurface(win);

    bool running = true;
    while( running )
    {
        SDL_Event ev;
        while( SDL_PollEvent( &ev ) )
        {
            if( ( SDL_QUIT == ev.type ) ||
                ( SDL_KEYDOWN == ev.type && SDL_SCANCODE_ESCAPE == ev.key.keysym.scancode ) )
            {
                running = false;
                break;
            }
        }

        SDL_FillRect(winSurface, NULL, SDL_MapRGB(winSurface->format, 255, 90, 120));
        SDL_UpdateWindowSurface(win);
    }

    SDL_DestroyWindow(win);
    SDL_Quit();
    return 0;
}