简体   繁体   English

C++ SDL2 window 未打开

[英]C++ SDL2 window not opening

i coded this.我编码了这个。

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




}

when i compile it, it opens the window, and it immediately closes.当我编译它时,它会打开 window,然后立即关闭。 But the console doesn't.但控制台没有。 Here is a screenshot of my console(maybe it could help solving the problem?)这是我的控制台的屏幕截图(也许它可以帮助解决问题?)

截屏

Would there be any solution to get the Window to not close?是否有任何解决方案可以让 Window 不关闭?

Would there be any solution to get the Window to not close?是否有任何解决方案可以让 Window 不关闭?

Start up an event-handling loop and handle some events:启动一个事件处理循环并处理一些事件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM