我目前正在通过阅读 Lazy foo 教程来学习 SDL。我在 Linux 上使用代码块 13.12。我无法使事件处理正常工作。
我基本上是在尝试显示图像(效果很好),但无论我单击关闭按钮多少次,它都不会关闭
Code:
#include <SDL2/SDL.h>
#include <stdio.h>
//Declaring the main window, the main surface and the image surface
SDL_Window *window = NULL;
SDL_Surface *scrsurface = NULL;
SDL_Surface *imgSurface = NULL;
SDL_Event event;
int run = 1;
//The function where SDL will be initialized
int init();
//The function where the image will be loaded into memory
int loadImage();
//The function that will properly clean up and close SDL and the variables
int close();
//The main function
int main(void){
if(init() == -1)
printf("Init failed!!!");
else{
if(loadImage() == -1)
printf("loadImage failed!!!");
else{
//Displaying the image
while(run){
//Event handling
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
run = 0;
fprintf(stderr, "Run set to 0");
break;
default:
fprintf(stderr, "Unhandled event");
break;
}
}
//Blitting nad updating
SDL_BlitSurface(imgSurface, NULL, scrsurface, NULL);
SDL_UpdateWindowSurface(window);
}
close();
}
}
return 0;
}
int init(){
if(SDL_Init(SDL_INIT_VIDEO) < 0)
return -1;
else{
window = SDL_CreateWindow("SDL_WINDOW", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 900, 900, SDL_WINDOW_SHOWN);
if(window == NULL)
return -1;
scrsurface = SDL_GetWindowSurface(window);
}
return 0;
}
int loadImage(){
imgSurface = SDL_LoadBMP("Test.bmp");
if(imgSurface == NULL)
return -1;
else{
}
return 0;
}
int close(){
SDL_FreeSurface(imgSurface);
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
return 0;
}
`
虽然需要彻底的调试才能确定到底发生了什么,但您的问题很可能是由 libc 的别名引起的close
功能和你的。close
是许多库使用的非常重要的调用,包括 Xlib(由 SDL 调用)。例如。SDL_CreateWindow
calls XOpenDisplay
/XCloseDisplay
测试显示能力,但是XCloseDisplay
calls close
在其连接套接字上,它将调用您的函数。很难说之后会发生什么,但这肯定不是我们想要的。
解决方法是将函数重命名为其他名称(例如,通过给它一个前缀)或声明它static
所以它的名字不会被导出。请注意,静态函数只能在单个翻译单元中使用(即,如果您的 .c 文件包含静态函数,则无法轻松地从另一个 .c 文件使用它)。
链接器不会在这里报告多个定义,因为 libc 的 close 是一个弱符号(nm -D /lib/libc.so.6 | egrep ' close$'
报告W
).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)