我最近一直在努力学习SDL2,我按照这里显示的说明安装了它:https://github.com/libsdl-org/SDL/blob/main/docs/README-linux.md。 在 VS Code 中,我设法正确包含 SDL2 库,以便代码可以编译。但是,无论我编译什么源代码(我正在遵循 Lazy Foo 的教程),当我实际运行该程序时,该窗口永远不会出现。该程序似乎运行,看着终端,但没有任何反应。
给你一个想法,这是我尝试编译的 Lazy Foo 的教程代码,当我运行它时没有任何反应。我没有收到任何错误消息。
/*This source code copyrighted by Lazy Foo' Productions (2004-2022)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load splash image
gHelloWorld = SDL_LoadBMP( "hello_world.bmp" );
if( gHelloWorld == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
success = false;
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface( gHelloWorld );
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
//Hack to get window to stay up
SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
}
}
//Free resources and close SDL
close();
return 0;
}
TLDR;我可以编译这个 SDL2 代码,但是当我运行这个程序时,没有窗口出现。 无论我写什么代码,或者为了测试而借用,似乎都是这种情况。 我没有收到任何错误消息。
您的程序找不到
hello_world.bmp
文件,它会导致它在不显示窗口的情况下过早退出。
修复方法如下:首先检查
hello_world.bmp
是否在正确的位置,然后在 close()
函数中的 main()
之前添加一个 get_char() 调用,这将暂停程序并让您看到如果有错误。
int main( int argc, char* args[] )
{
// Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
// Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
// Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
// Update the surface
SDL_UpdateWindowSurface( gWindow );
// Hack to get window to stay up
SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
}
}
// Add this line to pause the program before closing
getchar();
// Free resources and close SDL
close();
return 0;
}