From 3812df597584dd57d63b7d5297a847f2fe6131e2 Mon Sep 17 00:00:00 2001 From: marouault Date: Mon, 6 Dec 2021 10:57:06 +0100 Subject: [PATCH] Started event processing --- Pontu/include/engine/Button.h | 2 +- Pontu/include/engine/Coord.h | 18 ++++++++ Pontu/include/engine/InputElement.h | 56 +++++++++++++++++++++++ Pontu/include/engine/InputProcessor.h | 65 +++++++++++++++++++++++++++ Pontu/src/main.c | 12 +---- 5 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 Pontu/include/engine/Coord.h create mode 100644 Pontu/include/engine/InputElement.h create mode 100644 Pontu/include/engine/InputProcessor.h diff --git a/Pontu/include/engine/Button.h b/Pontu/include/engine/Button.h index e948f0a..87c8f77 100644 --- a/Pontu/include/engine/Button.h +++ b/Pontu/include/engine/Button.h @@ -36,6 +36,6 @@ typedef struct P_Button createButton(const SDL_Texture* texture, const int coordx, const int coordy, const int sizex, const int sizey, void (*onClick)(void)); // texture: design du bouton, si rien n'est passer, le bouton sera générer dans la fonction. -//SDL_bool putButtonOnRenderer(SDL_Renderer* renderer, TTF_Font* police, P_Button* button); + #endif diff --git a/Pontu/include/engine/Coord.h b/Pontu/include/engine/Coord.h new file mode 100644 index 0000000..e9cd137 --- /dev/null +++ b/Pontu/include/engine/Coord.h @@ -0,0 +1,18 @@ +#ifndef COORD_INPUT +#define COORD_INPUT + +struct p_coord +{ + int x; + int y; +}; + +bool coordValide(struct p_coord coord) { + return coord.x>=0 && coord.y>=0; +} + +bool coordEqual(struct p_coord a, struct p_coord b) { + return a.x == b.x && a.y == b.y; +} + +#endif //COORD_INPUT \ No newline at end of file diff --git a/Pontu/include/engine/InputElement.h b/Pontu/include/engine/InputElement.h new file mode 100644 index 0000000..81c43ad --- /dev/null +++ b/Pontu/include/engine/InputElement.h @@ -0,0 +1,56 @@ +#ifndef INPUT_ELEMENT_INCLUDED +#define INPUT_ELEMENT_INCLUDED + +/* +Clique sur un element (param coord du jeu) +Deplace Element (params coord du jeu debut et fin) + +Clique sur element UI +Fleche pour selectionner element UI +Entré -> active element UI +*/ + +#include "Coord.h" + +typedef enum {InputType_None, InputType_ClickGame, InputType_MoveGame, InputType_ActivateUI} InputType; +typedef enum {UIAction_Validate, UIAction_Cancel, UIAction_Quit } UIAction; + +typedef struct { + union + { + struct p_coord coord; + + struct { + struct p_coord start; + struct p_coord end; + } move; + + UIAction uiAction; + } data; + + InputType type; +} InputElement; + + +InputElement createInputElementNone() { + InputElement i = {.type=InputType_None}; + return i; +} + +InputElement createInputElementUIQuit() { + InputElement i = {.type=InputType_ActivateUI, .data.uiAction=UIAction_Quit}; + return i; +} + +InputElement createInputElementClickBoard(const struct p_coord newCoord) { + InputElement i = {.type=InputType_ClickGame, .data.coord = newCoord}; + return i; +} + +InputElement createInputElementMoveBoard(const struct p_coord newCoord) { + InputElement i = {.type=InputType_MoveGame, .data.coord = newCoord}; + return i; +} + + +#endif // INPUT_ELEMENT_INCLUDED diff --git a/Pontu/include/engine/InputProcessor.h b/Pontu/include/engine/InputProcessor.h new file mode 100644 index 0000000..3c1dcf7 --- /dev/null +++ b/Pontu/include/engine/InputProcessor.h @@ -0,0 +1,65 @@ +#ifndef INPUT_PROCESSOR_INCLUDED +#define INPUT_PROCESSOR_INCLUDED + +#include "Button.h" +#include "Coord.h" +#include "InputElement.h" + +typedef struct +{ + ArrayButton tabButton; + struct p_coord selectedCase; ///< Board , (-1;-1) si inexistant +} InputProcessor; + +InputElement proccessInput(InputProcessor *inputProcessor, const SDL_Rect boardRect) +{ + SDL_Event event; + if (!SDL_PollEvent(&event)) + { + return createInputElementNone(); + } + + switch (event.type) + { + case SDL_QUIT: + return createInputElementUIQuit(); + case SDL_MOUSEBUTTONDOWN: + { + const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y}; + if (SDL_PointInRect(&mousePoint, &boardRect)) + { + inputProcessor->selectedCase = screenCoordToGameCoord(mousePoint); + } + else + { + } + break; + } + case SDL_MOUSEBUTTONUP: + { + const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y}; + if (SDL_PointInRect(&mousePoint, &boardRect)) + { + if (coordValide(inputProcessor->selectedCase)) + { + struct p_coord newCoord = screenCoordToGameCoord(mousePoint); + if (coordEqual(inputProcessor->selectedCase, newCoord)) { + return createInputElementClickBoard(newCoord); + } + else { + return createInputElementMoveBoard(inputProcessor->selectedCase, newCoord); + } + inputProcessor->selectedCase = newCoord; + } + } + else + { + for (size_t i = 0; itabButton.size; ++i) { + inputProcessor->tabButton.button[i]; + } + } + } + } +} + +#endif //INPUT_PROCESSOR_INCLUDED diff --git a/Pontu/src/main.c b/Pontu/src/main.c index d5e2603..921d546 100644 --- a/Pontu/src/main.c +++ b/Pontu/src/main.c @@ -31,17 +31,7 @@ int main(int argc, char* argv[]) while(!quit) { // Event handling - SDL_Event event; - while(SDL_PollEvent(&event)) - { - switch (event.type) - { - case SDL_QUIT: - quit = SDL_TRUE; - break; - - } - } + // Drawing