【发布时间】:2015-07-17 18:10:05
【问题描述】:
我目前正在通过阅读 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;
}
`
【问题讨论】:
-
另外,fprintf 不会在 stdout 和 stderr 流上显示任何内容
-
调用非静态函数
close通常是一个非常糟糕的主意,因为它与libc 的close(int)有别名,并且Xlib/SDL 可能会在尝试关闭其文件时调用您的函数描述符。这可能是您看不到任何打印内容的原因,也许更多。 -
谢谢老兄,成功了!希望您将其发布为答案
标签: c codeblocks sdl-2