gpt4 book ai didi

c - SDL 事件处理不起作用

转载 作者:行者123 更新时间:2023-12-02 20:38:09 24 4
gpt4 key购买 nike

我目前正在通过阅读 Lazy foo 教程来学习 SDL。我在 Linux 上使用代码块 13.12。我无法使事件处理正常工作。

我基本上是在尝试显示图像(效果很好),但无论我单击关闭按钮多少次,它都不会关闭

代码:

#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 调用 XOpenDisplay/XCloseDisplay 来测试显示功能,但 XCloseDisplay 调用 close它的连接套接字,它将调用您的函数。很难说之后会发生什么,但这肯定不是我们想要的。

解决方法是将函数重命名为其他名称(例如,通过给它一个前缀)或将其声明为static,这样它的名称就不会被导出。请注意,静态函数只能在单个翻译单元中使用(即,如果您的 .c 文件包含静态函数,则无法轻松地从另一个 .c 文件中使用它)。

链接器不会在此处报告多个定义,因为 libc 的 close 是一个弱符号(nm -D/lib/libc.so.6 | egrep ' close$' 报告 W).

关于c - SDL 事件处理不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31482117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com