Implement input.c

- Implement input handling in input.c
  - test successful
- Add missing function showPauseScreen to menu.h/c
This commit is contained in:
jonny_jr9 2023-11-11 13:39:54 +01:00
parent 061b4431c7
commit d94eed0136
5 changed files with 113 additions and 9 deletions

View File

@ -1,8 +1,6 @@
#pragma once
// checks for and processes queued SDL input events and passes them
// to menu.c or controls snake depending on current game.gameState
// => has to be run repeatedly from main()
void processInputEvent();
//wird von SDL aufgerufen, wenn Taste gedrückt wurde
//bekommt Info darüber, welche Taste gedrückt wurde
//ruft zugehörige Aktion über switch caseauf
// z.B. bei Pfeiltaste -> rufe snakeSetDir auf
// im Menü bei Settings -> rufe menuNavigate auf

View File

@ -1,15 +1,19 @@
#pragma once
#include "SDL.h"
void showStartScreen();
//zum Starten Enter drücken
//optional: "E" eingeben für Settings
void showPauseScreen();
void showLeaderboard();
//zeigt die besten Spieldurchläufe inkl. Punktestand an
void showSettings(); //optional
//startet Settungs-Menü
void menuHandleInput(int event); //als Übergabeparameter: int(?) event -> welcher Datentyp hängt von SDL ab
void menuHandleInput(SDL_Event event); //als Übergabeparameter: int(?) event -> welcher Datentyp hängt von SDL ab
//switch case für welcher Modus
//switch case für welche Taste gedrückt wurde

View File

@ -1,5 +1,100 @@
#include "input.h"
#include "common.h"
#include "SDL.h"
#include "game.h"
#include "menu.h"
#include "snake.h"
//--------------------------------
//--- handleInput_runningState ---
//--------------------------------
// local function that handles keyboard input when in RUNNING gameState
// - control snake via WASD or arrow-keys
// - quit with q
// - pause with p
void handleInput_runningState(SDL_Event event)
{
switch (event.key.keysym.sym)
{
case SDLK_q:
game.gameState = EXIT;
break;
case SDLK_p:
case SDLK_ESCAPE:
game.gameState = PAUSED;
showPauseScreen();
break;
case SDLK_UP:
case SDLK_w:
snakeSetDir(UP);
break;
case SDLK_DOWN:
case SDLK_s:
snakeSetDir(DOWN);
break;
case SDLK_LEFT:
case SDLK_a:
snakeSetDir(LEFT);
break;
case SDLK_RIGHT:
case SDLK_d:
snakeSetDir(RIGHT);
break;
default:
LOGD("input: key %d is not handled in RUNNING mode\n", event.key.keysym.sym);
}
return;
}
//=============================
//===== processInputEvent =====
//=============================
// checks for and processes queued SDL input events and passes them
// to menu.c or controls snake depending on current game.gameState
// => has to be run repeatedly from main()
void processInputEvent()
{
SDL_Event event;
// loop through all queued input events that occoured since last run
while (SDL_PollEvent(&event))
{
// LOGD("event: %d detected", event.type);
//--- quit event ---
if (event.type == SDL_QUIT)
{
game.gameState = EXIT;
return;
}
//--- key pressed ---
// TODO also send mouse-events to menu?
else if (event.type == SDL_KEYDOWN)
{
//LOGD("keydown event key=%d\n", event.key.keysym.sym);
// run functions that handle the input depending on game.state
switch (game.gameState)
{
case RUNNING:
// control snake with keys (function above)
handleInput_runningState(event);
break;
case PAUSED:
case MENU:
// pass key event to menu handle function which updates menu
menuHandleInput(event);
break;
}
}
}
void processInputEvent(){
return;
}

View File

@ -9,10 +9,15 @@ void showLeaderboard(){
return;
}
void showPauseScreen(){
return;
}
void showSettings(){
return;
}
void menuHandleInput(int event){
void menuHandleInput(SDL_Event event){
//compare 'handleInput_runningState(SDL_Event event)' in input.c
return;
}

View File

@ -1,5 +1,7 @@
#include "render.h"
void renderGame(){
//note: maybe re-use renderGameToArray() from map.h?
//e.g. as used in map.c for printMap()
return;
}