我一直在关注LazyFoo的SDL2教程并略微修改代码,因此我可以在Linux上将其编译为C代码(我是C程序员并且不太了解C ++)。在second tutorial中,他将代码分为四个函数:init()
处理所有初始化,loadMedia()
处理图像,close()
释放所有内存。结束,main()
。
我的问题是,在init()
函数的中途(在调用SDL_Init(SDL_INIT_VIDEO)
之后),close()
被重复调用(181次),然后执行init()
的其余部分。 close()
然后在程序段错误之前再运行几次。
我对代码的修改如下:
#include <SDL.h>
更改为#include <SDL2/SDL.h>
include <stdbool.h>
,按照C 在main()
中为程序的渲染部分添加while循环:
SDL_Event event;
int quit = false;
while( !quit ) {
SDL_WaitEvent( &event );
if( event.type == SDL_QUIT ) {
quit = true;
}
//Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
}
这是为了解决我在第一个教程中遇到的问题,其中没有任何内容被绘制到窗口
SDL_Delay( 2000 );
,因为渲染循环不需要02_getting_an_image_on_the_screen/hello_.bmp
更改为hello.bmp
,因为源和图像位于同一文件夹中我发现即使我删除对close()
的调用也会发生错误,但如果我完全删除该函数则运行正常。如果我将文件扩展名保留为.cpp,它也可以正常运行,尽管据我所知,没有一个代码使用C ++特定的功能。值得注意的是,a窗口已成功创建,但没有任何东西被吸引到它。我真的不知道这一切的原因是什么。
我在Debian Stretch安装上使用GCC V6.3.0编译代码。原始来源可在上面链接的页面下载。我的修改版本如下。
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.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", "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
{
SDL_Event event;
int quit = false;
while( !quit ) {
SDL_WaitEvent( &event );
if( event.type == SDL_QUIT ) {
quit = true;
}
//Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
}
}
}
//Free resources and close SDL
close();
return 0;
}
答案 0 :(得分:3)
close
是一个操作系统函数,用于关闭文件句柄(打开的文件,或套接字,或几乎任何东西)。
您已经定义了自己的函数close
,并且您自己的函数优先于库函数。每次想要关闭文件句柄时,它都会调用close
函数。 (而且文件句柄也没有关闭,因为没有调用真正的close
函数)。