C++ 대신 C에서 SDL을 사용하고 싶지만 작동 할 이벤트를 얻을 수 없습니다. 이 코드는 .c 또는 cpp 파일이 완벽하게 작동하며 .cpp 파일이고 gcc로 컴파일 된 경우 g ++로 컴파일됩니다. 그러나 .c 파일이고 gcc로 컴파일 된 경우 잘 컴파일되지만 SDL_QUIT 이벤트는 실제로 아무 것도하지 않습니다. 창이 영원히 멈 춥니 다.SDL_QUIT 이벤트는 g ++에서 작동하지만 gcc에서는 작동하지 않습니다.
#include<SDL2/SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 400;
int init();
int loadMedia();
void mainLoop();
void close();
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
SDL_Surface* helloWorld = NULL;
int main(int argc, char* args[]){
if(!init()){
printf("Failed to initialize!\n");
}
else{
if(!loadMedia()){
printf("Failed to load media!\n");
}
else{
mainLoop();
}
}
close();
return 0;
}
void mainLoop(){
int quit = 0;
SDL_Event e;
while(!quit){
while(SDL_PollEvent(&e) != 0){
if(e.type == SDL_QUIT){
quit = 1;
}
}
SDL_BlitSurface(helloWorld, NULL, screenSurface, NULL);
SDL_UpdateWindowSurface(window);
}
}
int init(){
int success = 1;
//initialize sdl
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0){
printf("SDL could not initialize! SDL_Error: %s\n" , SDL_GetError());
success = 0;
}
else {
//create window
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL){
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
success = 0;
}
else {
screenSurface = SDL_GetWindowSurface(window);
}
}
return success;
}
int loadMedia(){
int success = 1;
helloWorld = SDL_LoadBMP("images/helloWorld.bmp");
if(helloWorld == NULL){
printf(" Unable to load image %s! SDL_Error: %s\n", "images/helloWorld.bmp", SDL_GetError());
success = 0;
}
return success;
}
void close(){
//deallocate surface
SDL_FreeSurface(helloWorld);
helloWorld = NULL;
//Destroy window
SDL_DestroyWindow(window);
window = NULL;
//Quit SDL subsystems
SDL_Quit();
}
코드는 잘못이 아니다. QUIT 이벤트를 만들기 위해 어떤 작업을 수행합니까? 문제에 대한 완전한 편집 가능한 최소 예를 작성하고 질문에 포함하십시오. – keltar
창에서 X 버튼을 클릭해도 아무런 효과가 없습니다. 또한 다른 키 또는 마우스 이벤트를 추가하면 순수 C에서는 작동하지 않지만 C++ 컴파일러 또는 소스 코드 결과로 전환하면 모든 sdl 이벤트가 올바르게 작동합니다. – Manjiru
완전한 예제를 게시하지 않으려는 경우 질문에 답할 수 없습니다. – keltar