mirror of
https://github.com/lov3b/Pong.git
synced 2025-01-18 20:50:12 +01:00
refactor event handling
This commit is contained in:
parent
9c77a9fde6
commit
de58a7f405
35
src/Game.cpp
35
src/Game.cpp
@ -80,32 +80,17 @@ void Game::update() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Game::handleEvents() {
|
|
||||||
SDL_Event event;
|
|
||||||
|
|
||||||
while (SDL_PollEvent(&event)) {
|
void Game::handleKeyDown(SDL_Event &event) {
|
||||||
if (event.type == SDL_QUIT)
|
if (gameState == GameState::START_SCREEN) {
|
||||||
return false;
|
|
||||||
|
|
||||||
switch (gameState) {
|
|
||||||
case GameState::START_SCREEN:
|
|
||||||
startScreen->handleEvent(event);
|
startScreen->handleEvent(event);
|
||||||
break;
|
return;
|
||||||
case GameState::GAME:
|
} else if (gameState == GameState::END_SCREEN) {
|
||||||
handleGameEvent(event);
|
|
||||||
break;
|
|
||||||
case GameState::END_SCREEN:
|
|
||||||
if (event.type == SDL_KEYDOWN && !endScreen->hasStartedCounting())
|
if (event.type == SDL_KEYDOWN && !endScreen->hasStartedCounting())
|
||||||
endScreen->startCountDown();
|
endScreen->startCountDown();
|
||||||
break;
|
return;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Game::handleGameEvent(SDL_Event &event) {
|
|
||||||
if (event.type == SDL_KEYDOWN) {
|
|
||||||
switch (event.key.keysym.sym) {
|
switch (event.key.keysym.sym) {
|
||||||
case SDLK_w:
|
case SDLK_w:
|
||||||
leftPaddle->startMoving(true);
|
leftPaddle->startMoving(true);
|
||||||
@ -121,7 +106,13 @@ void Game::handleGameEvent(SDL_Event &event) {
|
|||||||
rightPaddle->startMoving(false);
|
rightPaddle->startMoving(false);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (event.type == SDL_KEYUP) {
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::handleKeyUp(SDL_Event &event) {
|
||||||
|
if (gameState != GameState::GAME)
|
||||||
|
return;
|
||||||
|
|
||||||
switch (event.key.keysym.sym) {
|
switch (event.key.keysym.sym) {
|
||||||
case SDLK_w:
|
case SDLK_w:
|
||||||
leftPaddle->stopMoving(true);
|
leftPaddle->stopMoving(true);
|
||||||
@ -138,6 +129,4 @@ void Game::handleGameEvent(SDL_Event &event) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -36,7 +36,7 @@ public:
|
|||||||
|
|
||||||
void update() override;
|
void update() override;
|
||||||
|
|
||||||
bool handleEvents() override;
|
void handleKeyUp(SDL_Event &event) override;
|
||||||
|
|
||||||
void handleGameEvent(SDL_Event &event);
|
void handleKeyDown(SDL_Event &event) override;
|
||||||
};
|
};
|
@ -53,6 +53,25 @@ SdlWrapper::~SdlWrapper() {
|
|||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SdlWrapper::handleEvents() {
|
||||||
|
SDL_Event event;
|
||||||
|
|
||||||
|
while (SDL_PollEvent(&event)) {
|
||||||
|
switch (event.type) {
|
||||||
|
case SDL_QUIT:
|
||||||
|
return false;
|
||||||
|
case SDL_KEYDOWN:
|
||||||
|
handleKeyDown(event);
|
||||||
|
break;
|
||||||
|
case SDL_KEYUP:
|
||||||
|
handleKeyUp(event);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
int SdlWrapper::loop() {
|
int SdlWrapper::loop() {
|
||||||
while (running) {
|
while (running) {
|
||||||
if (!handleEvents())
|
if (!handleEvents())
|
||||||
|
@ -30,5 +30,11 @@ protected:
|
|||||||
|
|
||||||
virtual void update() = 0;
|
virtual void update() = 0;
|
||||||
|
|
||||||
virtual bool handleEvents() = 0;
|
virtual void handleKeyUp(SDL_Event &event) = 0;
|
||||||
|
|
||||||
|
virtual void handleKeyDown(SDL_Event &event) = 0;
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool handleEvents();
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user