我目前正在通读Lazy Foo教程学习SDL。在Linux上,我使用的是Code Blocks 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;
}
`