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/arrayButton.h"
#include "engine/InputElement.h"
#include "engine/InputProcessor.h"
#include "model/Coord.h"
/**
@ -19,7 +20,7 @@
*/
typedef struct
{
struct array_P_Button tabButton;
InputProcessor inputProcessor;
Coord selectedCase; ///< A case in Board (used to handle move actions) , (-1;-1) si inexistant
} GameInputProcessor;
@ -56,4 +57,7 @@ Coord screenCoordToGameCoord(const SDL_Point* point, 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

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

@ -1,43 +1,37 @@
#include "engine/GameInputProcessor.h"
Coord screenCoordToGameCoord(const SDL_Point* point, const SDL_Rect* boardRect){
Coord coord = {
coord.x = (point->x-boardRect->x)*9/boardRect->w,
coord.y = (point->y-boardRect->y)*9/boardRect->h
};
Coord screenCoordToGameCoord(const SDL_Point* point, const SDL_Rect* boardRect)
{
Coord coord = { coord.x = (point->x - boardRect->x) * 9 / boardRect->w,
coord.y = (point->y - boardRect->y) * 9 / boardRect->h };
return coord;
}
GameInputProcessor createGameInputProcessor() {
GameInputProcessor gameInputProcessor = {
.selectedCase = {.x=-1, .y=-1},
.tabButton = array_P_Button_Create()
};
GameInputProcessor createGameInputProcessor()
{
GameInputProcessor gameInputProcessor = { .selectedCase = { .x = -1, .y = -1 },
.inputProcessor = createInputProcessor() };
return gameInputProcessor;
}
void freeGameInputProcessor(GameInputProcessor* gameInputProcessor) {
array_P_Button_Free(&gameInputProcessor->tabButton);
void freeGameInputProcessor(GameInputProcessor* gameInputProcessor)
{
freeInputProcessor(&gameInputProcessor->inputProcessor);
}
InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL_Rect* boardRect)
InputElement interpretSDL_EventGameInput(GameInputProcessor* gameInputProcessor,
const SDL_Rect* boardRect,
const SDL_Event* event)
{
SDL_Event event;
if (!SDL_PollEvent(&event))
{
return createInputElementNone();
}
switch (event.type)
switch (event->type)
{
case SDL_QUIT:
return createInputElementUIQuit();
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 (!coordValid(gameInputProcessor->selectedCase)) {
if (!coordValid(gameInputProcessor->selectedCase))
{
gameInputProcessor->selectedCase = screenCoordToGameCoord(&mousePoint, boardRect);
}
}
@ -45,49 +39,44 @@ InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL
}
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 (coordValid(gameInputProcessor->selectedCase))
{
Coord newCoords = screenCoordToGameCoord(&mousePoint, boardRect);
if (coordEqual(gameInputProcessor->selectedCase, newCoords)) {
if (coordEqual(gameInputProcessor->selectedCase, newCoords))
{
gameInputProcessor->selectedCase = newCoords;
return createInputElementClickBoard(newCoords);
}
else {
else
{
const Coord oldCoord = gameInputProcessor->selectedCase;
gameInputProcessor->selectedCase = newCoord(-1,-1);
gameInputProcessor->selectedCase = newCoord(-1, -1);
return createInputElementMoveBoard(oldCoord, newCoords);
}
}
}
else
{
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);
break;
}
}
return createInputElementNone();
}
break;
}
case SDL_MOUSEMOTION:
}
InputElement proccessGameInput(GameInputProcessor* gameInputProcessor, const SDL_Rect* boardRect)
{
SDL_Event event;
if (!SDL_PollEvent(&event))
{
for (size_t i = 0; i<gameInputProcessor->tabButton.size; ++i) {
P_Button* b = &gameInputProcessor->tabButton.elems[i];
isButtonInteractWithCursor(b, event.motion.x, event.motion.y);
}
break;
}
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
return createInputElementResizeWindow(event.window.data1, event.window.data2);
}
break;
return createInputElementNone();
}
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"
InputProcessor createInputProcessor() {
InputProcessor inputProcessor = {
.tabButton = array_P_Button_Create(),
InputProcessor createInputProcessor()
{
InputProcessor inputProcessor = { .tabButton = array_P_Button_Create(),
.tabTextInput = array_TextInput_Create(),
.selectedTextInput = NULL
};
.selectedTextInput = NULL };
SDL_StopTextInput();
return inputProcessor;
}
void freeInputProcessor(InputProcessor* inputProcessor) {
void freeInputProcessor(InputProcessor* inputProcessor)
{
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:
return createInputElementUIQuit();
case SDL_QUIT: return createInputElementUIQuit();
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];
if (isHover(b)) {
if (isHover(b))
{
b->onClick(b);
break;
}
}
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];
if (SDL_PointInRect(&mousePoint, &ti->size)) {
if (inputProcessor->selectedTextInput == NULL) {
if (SDL_PointInRect(&mousePoint, &ti->size))
{
if (inputProcessor->selectedTextInput == NULL)
{
SDL_StartTextInput();
}
else {
else
{
inputProcessor->selectedTextInput->isActive = false;
}
inputProcessor->selectedTextInput = ti;
textInputClicked = true;
inputProcessor->selectedTextInput->isActive = true;
inputProcessor->selectedTextInput->cursorPosition = strlen(inputProcessor->selectedTextInput->value);
inputProcessor->selectedTextInput->cursorPosition =
strlen(inputProcessor->selectedTextInput->value);
break;
}
}
if (!textInputClicked && inputProcessor->selectedTextInput != NULL) {
if (!textInputClicked && inputProcessor->selectedTextInput != NULL)
{
inputProcessor->selectedTextInput->isActive = false;
inputProcessor->selectedTextInput = NULL;
@ -64,33 +66,38 @@ InputElement proccessInput(InputProcessor *inputProcessor)
}
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];
int res = isButtonInteractWithCursor(b, event.motion.x, event.motion.y);
if (res != BUTTON_NOTHING) {
int res = isButtonInteractWithCursor(b, event->motion.x, event->motion.y);
if (res != BUTTON_NOTHING)
{
return createInputElementButtonChanged(b, res);
}
}
break;
}
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
return createInputElementResizeWindow(event.window.data1, event.window.data2);
if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
return createInputElementResizeWindow(event->window.data1, event->window.data2);
}
if (event.window.event == SDL_WINDOWEVENT_CLOSE) {
if (event->window.event == SDL_WINDOWEVENT_CLOSE)
{
return createInputElementCloseWindow();
}
break;
case SDL_TEXTINPUT:
addStringToInputTextValue(inputProcessor->selectedTextInput, event.text.text);
addStringToInputTextValue(inputProcessor->selectedTextInput, event->text.text);
return createInputElementTextInput(inputProcessor->selectedTextInput);
break;
case SDL_TEXTEDITING:
inputProcessor->selectedTextInput->cursorPosition = event.edit.start;
inputProcessor->selectedTextInput->cursorPosition = event->edit.start;
return createInputElementTextInput(inputProcessor->selectedTextInput);
break;
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);
return createInputElementTextInput(inputProcessor->selectedTextInput);
}
@ -99,3 +106,14 @@ InputElement proccessInput(InputProcessor *inputProcessor)
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) {
return;
}
GameInputProcessor inputProcessor = createGameInputProcessor();
GameInputProcessor gameInputProcessor = createGameInputProcessor();
struct array_Coord interactiveCases = array_Coord_Create();
Game game = newGame(nbPlayers, players);
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);
@ -59,8 +59,8 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
SDL_SetRenderDrawColor(renderer, 50,10,10, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
redrawGameBoard(renderer, game.arrPlayers, game.nbPlayers, &textureHandler, &boardRect, &game.board);
for (size_t i=0; i<inputProcessor.tabButton.size; ++i) {
drawButtonOnRenderer(renderer, &inputProcessor.tabButton.elems[i]);
for (size_t i=0; i<gameInputProcessor.inputProcessor.tabButton.size; ++i) {
drawButtonOnRenderer(renderer, &gameInputProcessor.inputProcessor.tabButton.elems[i]);
}
for (size_t i=0; i<tabLabel.size; ++i){
drawTextLabel(renderer,&tabLabel.elems[i]);
@ -72,7 +72,7 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
{
// Event handling
InputElement inputElement;
while (InputType_None != (inputElement = proccessGameInput(&inputProcessor, &boardRect)).type) {
while (InputType_None != (inputElement = proccessGameInput(&gameInputProcessor, &boardRect)).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
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);
@ -126,7 +126,7 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
}
if (actionRealized != GameAction_None) {
inputProcessor.selectedCase = newCoord(-1,-1);
gameInputProcessor.selectedCase = newCoord(-1,-1);
if (game.phase == GAME_ENDED) {
*generalState = GS_EndOfGameMenu;
}
@ -154,8 +154,8 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
boardRect = adaptPosToRect(&boardRPositionSpecifier, &windowRect);
redrawGameBoard(renderer, game.arrPlayers, game.nbPlayers, &textureHandler, &boardRect, &game.board);
for (size_t i=0; i<inputProcessor.tabButton.size; ++i) {
drawButtonOnRenderer(renderer, &inputProcessor.tabButton.elems[i]);
for (size_t i=0; i<gameInputProcessor.inputProcessor.tabButton.size; ++i) {
drawButtonOnRenderer(renderer, &gameInputProcessor.inputProcessor.tabButton.elems[i]);
}
for (size_t i=0; i<tabLabel.size; ++i){
drawTextLabel(renderer,&tabLabel.elems[i]);
@ -168,7 +168,7 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
}
array_Coord_Free(&interactiveCases);
interactiveCases = getInteractiveCases(&game, inputProcessor.selectedCase);
interactiveCases = getInteractiveCases(&game, gameInputProcessor.selectedCase);
}
if(needToPresent)
@ -186,5 +186,5 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
freeTextureHandler(&textureHandler);
array_Coord_Free(&interactiveCases);
freeGameInputProcessor(&inputProcessor);
freeGameInputProcessor(&gameInputProcessor);
}

Loading…
Cancel
Save