SDL事件处理无法正常工作 [英] SDL event handling not working
问题描述
我目前正在读懒富教程学习SDL。我在Linux上使用code块13.12。我无法获得事件处理能够正常工作。
I'm currently learning SDL by reading Lazy foo tutorials. I'm using code blocks 13.12 on Linux. I'm unable to get event handling to work correctly.
我基本上想显示图像(它的伟大工程),但不管多少次,我点击关闭按钮,也不会关闭
I'm basically trying to display an image (which works great), but no matter how many times I click on the close button, it won't close
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中的关闭$走样所致C $ C>的功能和你的。
关闭
是许多库,包括×程序库(由SDL调用)使用了非常重要的电话。例如。 SDL_CreateWindow
要求 XOpenDisplay
/ XCloseDisplay
来测试显示功能,但 XCloseDisplay
通话关闭它的连接插座上,它会调用你的函数。这很难说以后会发生什么,但它肯定是不想要的结果。
While it requires thorough debugging to determine what exactly happens, your problem is very likely to be caused by aliasing of libc's close
function and yours. close
is very important call used by many libraries, including Xlib (which is called by SDL). E.g. SDL_CreateWindow
calls XOpenDisplay
/XCloseDisplay
to test display capabilities, but XCloseDisplay
calls close
on its connection socket, and it will call your function instead. It's hard to say what will happen after that, but it is certainly not what was intended.
解决方法是(通过给它一个preFIX例如)或者声明其重命名功能,别的东西静态
那么它的名字将不会被导出。需要注意的是静态函数只能单个转换单元中使用(即如果.c文件包含静态函数,你不能轻易使用它从另一个.c文件)。
Workaround is either renaming function to something else (e.g. by giving it a prefix) or by declaring it static
so its name wouldn't be exported. Note that static functions may only be used within single translation unit (i.e. if your .c file contains static function, you cannot easily use it from another .c file).
链接器不会在这里报告多重定义,因为libc中的接近是一个弱符号(纳米-D /lib/libc.so.6 | egrep的亲密$
报告是W
)。
Linker doesn't report multiple definitions here because libc's close is a weak symbol (nm -D /lib/libc.so.6 | egrep ' close$'
reports W
).
这篇关于SDL事件处理无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!