Game input is now a InputProcessor tanks to a pseudo "inheritance"

master
marouault 3 years ago
parent 8413cb0c06
commit 71c47eb4c1

@ -11,6 +11,7 @@
#include "engine/Button.h" #include "engine/Button.h"
#include "engine/arrayButton.h" #include "engine/arrayButton.h"
#include "engine/InputElement.h" #include "engine/InputElement.h"
#include "engine/InputProcessor.h"
#include "model/Coord.h" #include "model/Coord.h"
/** /**
@ -19,7 +20,7 @@
*/ */
typedef struct typedef struct
{ {
struct array_P_Button tabButton; InputProcessor inputProcessor;
Coord selectedCase; ///< A case in Board (used to handle move actions) , (-1;-1) si inexistant Coord selectedCase; ///< A case in Board (used to handle move actions) , (-1;-1) si inexistant
} GameInputProcessor; } GameInputProcessor;
@ -56,4 +57,7 @@ Coord screenCoordToGameCoord(const SDL_Point* point, const SDL_Rect* boardRect);
*/ */
InputElement proccessGameInput(GameInputProcessor* gameInputProcessor, const SDL_Rect* boardRect); InputElement proccessGameInput(GameInputProcessor* gameInputProcessor, const SDL_Rect* boardRect);
InputElement interpretSDL_EventGameInput(GameInputProcessor* gameInputProcessor, const SDL_Rect* boardRect, const SDL_Event* event);
#endif // GAME_INPUT_PROCESSOR_INCLUDED #endif // GAME_INPUT_PROCESSOR_INCLUDED

@ -49,4 +49,6 @@ void freeInputProcessor(InputProcessor* inputProcessor);
*/ */
InputElement proccessInput(InputProcessor* inputProcessor); InputElement proccessInput(InputProcessor* inputProcessor);
InputElement interpretSDL_EventInput(InputProcessor* inputProcessor, const SDL_Event* event);
#endif // INPUT_PROCESSOR_INCLUDED #endif // INPUT_PROCESSOR_INCLUDED

@ -1,43 +1,37 @@
#include "engine/GameInputProcessor.h" #include "engine/GameInputProcessor.h"
Coord screenCoordToGameCoord(const SDL_Point* point, const SDL_Rect* boardRect){ Coord screenCoordToGameCoord(const SDL_Point* point, const SDL_Rect* boardRect)
Coord coord = { {
coord.x = (point->x-boardRect->x)*9/boardRect->w, Coord coord = { coord.x = (point->x - boardRect->x) * 9 / boardRect->w,
coord.y = (point->y-boardRect->y)*9/boardRect->h coord.y = (point->y - boardRect->y) * 9 / boardRect->h };
};
return coord; return coord;
} }
GameInputProcessor createGameInputProcessor() { GameInputProcessor createGameInputProcessor()
GameInputProcessor gameInputProcessor = { {
.selectedCase = {.x=-1, .y=-1}, GameInputProcessor gameInputProcessor = { .selectedCase = { .x = -1, .y = -1 },
.tabButton = array_P_Button_Create() .inputProcessor = createInputProcessor() };
};
return gameInputProcessor; return gameInputProcessor;
} }
void freeGameInputProcessor(GameInputProcessor* gameInputProcessor) { void freeGameInputProcessor(GameInputProcessor* gameInputProcessor)
array_P_Button_Free(&gameInputProcessor->tabButton);
}
InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL_Rect* boardRect)
{ {
SDL_Event event; freeInputProcessor(&gameInputProcessor->inputProcessor);
if (!SDL_PollEvent(&event))
{
return createInputElementNone();
} }
switch (event.type) InputElement interpretSDL_EventGameInput(GameInputProcessor* gameInputProcessor,
const SDL_Rect* boardRect,
const SDL_Event* event)
{
switch (event->type)
{ {
case SDL_QUIT:
return createInputElementUIQuit();
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
{ {
const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y}; const SDL_Point mousePoint = { .x = event->button.x, .y = event->button.y };
if (SDL_PointInRect(&mousePoint, boardRect)) if (SDL_PointInRect(&mousePoint, boardRect))
{ {
if (!coordValid(gameInputProcessor->selectedCase)) { if (!coordValid(gameInputProcessor->selectedCase))
{
gameInputProcessor->selectedCase = screenCoordToGameCoord(&mousePoint, boardRect); gameInputProcessor->selectedCase = screenCoordToGameCoord(&mousePoint, boardRect);
} }
} }
@ -45,49 +39,44 @@ InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL
} }
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
{ {
const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y}; const SDL_Point mousePoint = { .x = event->button.x, .y = event->button.y };
if (SDL_PointInRect(&mousePoint, boardRect)) if (SDL_PointInRect(&mousePoint, boardRect))
{ {
if (coordValid(gameInputProcessor->selectedCase)) if (coordValid(gameInputProcessor->selectedCase))
{ {
Coord newCoords = screenCoordToGameCoord(&mousePoint, boardRect); Coord newCoords = screenCoordToGameCoord(&mousePoint, boardRect);
if (coordEqual(gameInputProcessor->selectedCase, newCoords)) { if (coordEqual(gameInputProcessor->selectedCase, newCoords))
{
gameInputProcessor->selectedCase = newCoords; gameInputProcessor->selectedCase = newCoords;
return createInputElementClickBoard(newCoords); return createInputElementClickBoard(newCoords);
} }
else { else
{
const Coord oldCoord = gameInputProcessor->selectedCase; const Coord oldCoord = gameInputProcessor->selectedCase;
gameInputProcessor->selectedCase = newCoord(-1, -1); gameInputProcessor->selectedCase = newCoord(-1, -1);
return createInputElementMoveBoard(oldCoord, newCoords); return createInputElementMoveBoard(oldCoord, newCoords);
} }
} }
} }
else break;
{
for (size_t i = 0; i<gameInputProcessor->tabButton.size; ++i) {
P_Button* b = &gameInputProcessor->tabButton.elems[i];
if (SDL_PointInRect(&mousePoint, &b->rect)) {
b->onClick(b);
} }
} }
return createInputElementNone(); return createInputElementNone();
} }
break;
} InputElement proccessGameInput(GameInputProcessor* gameInputProcessor, const SDL_Rect* boardRect)
case SDL_MOUSEMOTION:
{ {
for (size_t i = 0; i<gameInputProcessor->tabButton.size; ++i) { SDL_Event event;
P_Button* b = &gameInputProcessor->tabButton.elems[i]; if (!SDL_PollEvent(&event))
isButtonInteractWithCursor(b, event.motion.x, event.motion.y); {
} return createInputElementNone();
break;
}
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
return createInputElementResizeWindow(event.window.data1, event.window.data2);
}
break;
} }
return createInputElementNone(); InputElement ret = interpretSDL_EventGameInput(gameInputProcessor, boardRect, &event);
if (ret.type == InputType_None)
{
return interpretSDL_EventInput(&gameInputProcessor->inputProcessor, &event);
}
return ret;
} }

@ -1,60 +1,62 @@
#include "engine/InputProcessor.h" #include "engine/InputProcessor.h"
InputProcessor createInputProcessor() { InputProcessor createInputProcessor()
InputProcessor inputProcessor = { {
.tabButton = array_P_Button_Create(), InputProcessor inputProcessor = { .tabButton = array_P_Button_Create(),
.tabTextInput = array_TextInput_Create(), .tabTextInput = array_TextInput_Create(),
.selectedTextInput = NULL .selectedTextInput = NULL };
};
SDL_StopTextInput(); SDL_StopTextInput();
return inputProcessor; return inputProcessor;
} }
void freeInputProcessor(InputProcessor* inputProcessor) { void freeInputProcessor(InputProcessor* inputProcessor)
{
array_P_Button_Free(&inputProcessor->tabButton); array_P_Button_Free(&inputProcessor->tabButton);
} }
InputElement proccessInput(InputProcessor *inputProcessor)
{
SDL_Event event;
if (!SDL_PollEvent(&event))
{
return createInputElementNone();
}
switch (event.type) InputElement interpretSDL_EventInput(InputProcessor* inputProcessor, const SDL_Event* event)
{
switch (event->type)
{ {
case SDL_QUIT: case SDL_QUIT: return createInputElementUIQuit();
return createInputElementUIQuit();
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
{ {
const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y}; const SDL_Point mousePoint = { .x = event->button.x, .y = event->button.y };
for (size_t i = 0; i<inputProcessor->tabButton.size; ++i) { for (size_t i = 0; i < inputProcessor->tabButton.size; ++i)
{
P_Button* b = &inputProcessor->tabButton.elems[i]; P_Button* b = &inputProcessor->tabButton.elems[i];
if (isHover(b)) { if (isHover(b))
{
b->onClick(b); b->onClick(b);
break; break;
} }
} }
bool textInputClicked = false; bool textInputClicked = false;
for (size_t i = 0; i<inputProcessor->tabTextInput.size; ++i) { for (size_t i = 0; i < inputProcessor->tabTextInput.size; ++i)
{
TextInput* ti = &inputProcessor->tabTextInput.elems[i]; TextInput* ti = &inputProcessor->tabTextInput.elems[i];
if (SDL_PointInRect(&mousePoint, &ti->size)) { if (SDL_PointInRect(&mousePoint, &ti->size))
if (inputProcessor->selectedTextInput == NULL) { {
if (inputProcessor->selectedTextInput == NULL)
{
SDL_StartTextInput(); SDL_StartTextInput();
} }
else { else
{
inputProcessor->selectedTextInput->isActive = false; inputProcessor->selectedTextInput->isActive = false;
} }
inputProcessor->selectedTextInput = ti; inputProcessor->selectedTextInput = ti;
textInputClicked = true; textInputClicked = true;
inputProcessor->selectedTextInput->isActive = true; inputProcessor->selectedTextInput->isActive = true;
inputProcessor->selectedTextInput->cursorPosition = strlen(inputProcessor->selectedTextInput->value); inputProcessor->selectedTextInput->cursorPosition =
strlen(inputProcessor->selectedTextInput->value);
break; break;
} }
} }
if (!textInputClicked && inputProcessor->selectedTextInput != NULL) { if (!textInputClicked && inputProcessor->selectedTextInput != NULL)
{
inputProcessor->selectedTextInput->isActive = false; inputProcessor->selectedTextInput->isActive = false;
inputProcessor->selectedTextInput = NULL; inputProcessor->selectedTextInput = NULL;
@ -64,33 +66,38 @@ InputElement proccessInput(InputProcessor *inputProcessor)
} }
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
{ {
for (size_t i = 0; i<inputProcessor->tabButton.size; ++i) { for (size_t i = 0; i < inputProcessor->tabButton.size; ++i)
{
P_Button* b = &inputProcessor->tabButton.elems[i]; P_Button* b = &inputProcessor->tabButton.elems[i];
int res = isButtonInteractWithCursor(b, event.motion.x, event.motion.y); int res = isButtonInteractWithCursor(b, event->motion.x, event->motion.y);
if (res != BUTTON_NOTHING) { if (res != BUTTON_NOTHING)
{
return createInputElementButtonChanged(b, res); return createInputElementButtonChanged(b, res);
} }
} }
break; break;
} }
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
return createInputElementResizeWindow(event.window.data1, event.window.data2); {
return createInputElementResizeWindow(event->window.data1, event->window.data2);
} }
if (event.window.event == SDL_WINDOWEVENT_CLOSE) { if (event->window.event == SDL_WINDOWEVENT_CLOSE)
{
return createInputElementCloseWindow(); return createInputElementCloseWindow();
} }
break; break;
case SDL_TEXTINPUT: case SDL_TEXTINPUT:
addStringToInputTextValue(inputProcessor->selectedTextInput, event.text.text); addStringToInputTextValue(inputProcessor->selectedTextInput, event->text.text);
return createInputElementTextInput(inputProcessor->selectedTextInput); return createInputElementTextInput(inputProcessor->selectedTextInput);
break; break;
case SDL_TEXTEDITING: case SDL_TEXTEDITING:
inputProcessor->selectedTextInput->cursorPosition = event.edit.start; inputProcessor->selectedTextInput->cursorPosition = event->edit.start;
return createInputElementTextInput(inputProcessor->selectedTextInput); return createInputElementTextInput(inputProcessor->selectedTextInput);
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if (inputProcessor->selectedTextInput != NULL && event.key.keysym.sym == SDLK_BACKSPACE) { if (inputProcessor->selectedTextInput != NULL && event->key.keysym.sym == SDLK_BACKSPACE)
{
removeCharacterToInputTextValueAtCursor(inputProcessor->selectedTextInput); removeCharacterToInputTextValueAtCursor(inputProcessor->selectedTextInput);
return createInputElementTextInput(inputProcessor->selectedTextInput); return createInputElementTextInput(inputProcessor->selectedTextInput);
} }
@ -99,3 +106,14 @@ InputElement proccessInput(InputProcessor *inputProcessor)
return createInputElementNone(); return createInputElementNone();
} }
InputElement proccessInput(InputProcessor* inputProcessor)
{
SDL_Event event;
if (!SDL_PollEvent(&event))
{
return createInputElementNone();
}
return interpretSDL_EventInput(inputProcessor, &event);
}

@ -39,13 +39,13 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
if (*generalState != GS_Game) { if (*generalState != GS_Game) {
return; return;
} }
GameInputProcessor inputProcessor = createGameInputProcessor(); GameInputProcessor gameInputProcessor = createGameInputProcessor();
struct array_Coord interactiveCases = array_Coord_Create(); struct array_Coord interactiveCases = array_Coord_Create();
Game game = newGame(nbPlayers, players); Game game = newGame(nbPlayers, players);
TextureHandler textureHandler = newTextureHandler(renderer); TextureHandler textureHandler = newTextureHandler(renderer);
inputProcessor.tabButton = createGameInterfaceButtons(renderer, fontHandler, generalState,audioHandler); gameInputProcessor.inputProcessor.tabButton = createGameInterfaceButtons(renderer, fontHandler, generalState,audioHandler);
struct array_TextLabel tabLabel = createGameInterfaceLabels(renderer,fontHandler); struct array_TextLabel tabLabel = createGameInterfaceLabels(renderer,fontHandler);
@ -59,8 +59,8 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
SDL_SetRenderDrawColor(renderer, 50,10,10, SDL_ALPHA_OPAQUE); SDL_SetRenderDrawColor(renderer, 50,10,10, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
redrawGameBoard(renderer, game.arrPlayers, game.nbPlayers, &textureHandler, &boardRect, &game.board); redrawGameBoard(renderer, game.arrPlayers, game.nbPlayers, &textureHandler, &boardRect, &game.board);
for (size_t i=0; i<inputProcessor.tabButton.size; ++i) { for (size_t i=0; i<gameInputProcessor.inputProcessor.tabButton.size; ++i) {
drawButtonOnRenderer(renderer, &inputProcessor.tabButton.elems[i]); drawButtonOnRenderer(renderer, &gameInputProcessor.inputProcessor.tabButton.elems[i]);
} }
for (size_t i=0; i<tabLabel.size; ++i){ for (size_t i=0; i<tabLabel.size; ++i){
drawTextLabel(renderer,&tabLabel.elems[i]); drawTextLabel(renderer,&tabLabel.elems[i]);
@ -72,7 +72,7 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
{ {
// Event handling // Event handling
InputElement inputElement; InputElement inputElement;
while (InputType_None != (inputElement = proccessGameInput(&inputProcessor, &boardRect)).type) { while (InputType_None != (inputElement = proccessGameInput(&gameInputProcessor, &boardRect)).type) {
switch (inputElement.type) switch (inputElement.type)
{ {
@ -109,7 +109,7 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
int nbRounds= game.nb_rounds; //Store nb_round int nbRounds= game.nb_rounds; //Store nb_round
if(!array_Coord_Contains(&interactiveCases, inputElement.data.coord, *coordEqual)) { if(!array_Coord_Contains(&interactiveCases, inputElement.data.coord, *coordEqual)) {
inputProcessor.selectedCase = newCoord(-1,-1); gameInputProcessor.selectedCase = newCoord(-1,-1);
} }
const GameAction actionRealized = clickOnBoard(inputElement.data.coord, &game); const GameAction actionRealized = clickOnBoard(inputElement.data.coord, &game);
@ -126,7 +126,7 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
} }
if (actionRealized != GameAction_None) { if (actionRealized != GameAction_None) {
inputProcessor.selectedCase = newCoord(-1,-1); gameInputProcessor.selectedCase = newCoord(-1,-1);
if (game.phase == GAME_ENDED) { if (game.phase == GAME_ENDED) {
*generalState = GS_EndOfGameMenu; *generalState = GS_EndOfGameMenu;
} }
@ -154,8 +154,8 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
boardRect = adaptPosToRect(&boardRPositionSpecifier, &windowRect); boardRect = adaptPosToRect(&boardRPositionSpecifier, &windowRect);
redrawGameBoard(renderer, game.arrPlayers, game.nbPlayers, &textureHandler, &boardRect, &game.board); redrawGameBoard(renderer, game.arrPlayers, game.nbPlayers, &textureHandler, &boardRect, &game.board);
for (size_t i=0; i<inputProcessor.tabButton.size; ++i) { for (size_t i=0; i<gameInputProcessor.inputProcessor.tabButton.size; ++i) {
drawButtonOnRenderer(renderer, &inputProcessor.tabButton.elems[i]); drawButtonOnRenderer(renderer, &gameInputProcessor.inputProcessor.tabButton.elems[i]);
} }
for (size_t i=0; i<tabLabel.size; ++i){ for (size_t i=0; i<tabLabel.size; ++i){
drawTextLabel(renderer,&tabLabel.elems[i]); drawTextLabel(renderer,&tabLabel.elems[i]);
@ -168,7 +168,7 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
} }
array_Coord_Free(&interactiveCases); array_Coord_Free(&interactiveCases);
interactiveCases = getInteractiveCases(&game, inputProcessor.selectedCase); interactiveCases = getInteractiveCases(&game, gameInputProcessor.selectedCase);
} }
if(needToPresent) if(needToPresent)
@ -186,5 +186,5 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
freeTextureHandler(&textureHandler); freeTextureHandler(&textureHandler);
array_Coord_Free(&interactiveCases); array_Coord_Free(&interactiveCases);
freeGameInputProcessor(&inputProcessor); freeGameInputProcessor(&gameInputProcessor);
} }

Loading…
Cancel
Save