当我从主目录中删除SDL_PollEvent行时,为什么程序会崩溃?我想将输入管理保留在游戏系统类中,而不使用事件。
我也尝试过将!gamesystem.boot()放入while语句本身,也无济于事。
主要:
int main(int argc, char* args[])
{
if(!init())
{
printf("Failed to initialize!\n");
}
else
{
GameSystem gamesystem;
gamesystem.startup(gRenderer,gGameController1,gGameController2);
bool quit = false;
SDL_Event e;
while(!quit)
{
while(SDL_PollEvent(&e)!=0)
{
if(e.type == SDL_QUIT)
{
quit = true;
}
}
if(!gamesystem.boot())
quit = true;
}
}
close();
return 0;
}
游戏系统:
#include "GameSystem.h"
GameSystem::GameSystem()
{
}
void GameSystem::startup(SDL_Renderer* gRenderer, SDL_Joystick* gGameController1, SDL_Joystick* gGameController2)
{
gameRenderer = gRenderer;
controller1 = gGameController1;
controller2 = gGameController2;
title.loadFile(gameRenderer, "Art/Title.png");
arrow.loadFile(gameRenderer, "Art/Arrow.png");
beep = Mix_LoadWAV("SFX/Beep.wav");
boop = Mix_LoadWAV("SFX/Boop.wav");
buup = Mix_LoadWAV("SFX/Buup.wav");
baap = Mix_LoadWAV("SFX/Baap.wav");
}
bool GameSystem::boot()
{
renderClear();
if(menu == 0)
{
title.render(gameRenderer, 0,0);
if(selection == 0)
arrow.render(gameRenderer, 215, 400);
else if(selection == 1)
arrow.render(gameRenderer, 250, 440);
else if(selection == 2)
arrow.render(gameRenderer, 235, 482);
else if(selection == 3)
arrow.render(gameRenderer, 255, 528);
}
update();
return quit;
}
enum buttons GameSystem::controls()
{
const Uint8 *state = SDL_GetKeyboardState(NULL);
if(state [SDL_SCANCODE_DOWN] || SDL_JoystickGetHat(controller1, 0) == SDL_HAT_DOWN)
return u;
else if(state [SDL_SCANCODE_UP] || SDL_JoystickGetHat(controller1, 0) == SDL_HAT_UP)
return d;
else if(state [SDL_SCANCODE_LEFT] || SDL_JoystickGetHat(controller1, 0) == SDL_HAT_LEFT)
return l;
else if(state [SDL_SCANCODE_RIGHT] || SDL_JoystickGetHat(controller1, 0) == SDL_HAT_RIGHT)
return r;
else if(state [SDL_SCANCODE_X] || SDL_JoystickGetButton(controller1, 0))
return a;
else if(state [SDL_SCANCODE_C] || SDL_JoystickGetButton(controller1, 1))
return b;
else if(state [SDL_SCANCODE_S] || SDL_JoystickGetButton(controller1, 2))
return x;
else if(state [SDL_SCANCODE_D] || SDL_JoystickGetButton(controller1, 3))
return y;
else if(state [SDL_SCANCODE_RETURN])
return start;
else if(state [SDL_SCANCODE_SPACE])
return select;
return nopress;
}
void GameSystem::renderClear()
{
SDL_SetRenderDrawColor(gameRenderer, 0, 0, 0, 0xFF);
SDL_RenderClear(gameRenderer);
}
void GameSystem::update()
{
switch(controls())
{
case u:
Mix_PlayChannel(-1,beep,0);
selection++;
if(selection > 3) selection = 0;
break;
case d:
Mix_PlayChannel(-1,beep,0);
selection--;
if(selection < 0) selection = 3;
break;
case l:
Mix_PlayChannel(-1,beep,0);
break;
case r:
Mix_PlayChannel(-1,beep,0);
break;
case a:
Mix_PlayChannel(-1,beep,0);
if(selection == 3) quit = false;
break;
case b:
Mix_PlayChannel(-1,boop,0);
break;
case x:
Mix_PlayChannel(-1,buup,0);
break;
case y:
Mix_PlayChannel(-1,baap,0);
break;
}
//takes in vector of things to render after they have been sorted and renders them//
//^render vector here^//
SDL_RenderPresent(gameRenderer);
}
答案 0 :(得分:2)
您的程序不会崩溃,它只是忽略输入(它“没有响应”)。它只会重复运行gamesystem.boot()
,而不会检测到输入。
实际上,SDL_PollEvent()
内部调用SDL_PumpEvents()
来更新输入。删除SDL_PollEvent()
时,SDL从不更新其内部输入设备状态,因此SDL_GetKeyboardState()
始终返回其初始状态。
用SDL_PumpEvents()
替换PollEvent循环应该可以解决此问题。