From b218a0eeb31b674ddf40302ddde155639deced79 Mon Sep 17 00:00:00 2001 From: marouault Date: Sun, 2 Jan 2022 00:11:35 +0100 Subject: [PATCH] Added ArrayUtils to facilitate use of dynamic arrays. Tested some solutions to the InputHandler problem (The one which is commited mostly work) --- Pontu/entryPoints/main.c | 30 +++++++++++++- Pontu/include/debug/printer.h | 8 ++++ Pontu/include/engine/ArrayUtils.h | 64 +++++++++++++++++++++++++++++ Pontu/include/model/Coord.h | 10 +++++ Pontu/include/model/Game.h | 10 +++++ Pontu/include/model/arrayCoord.h | 9 ++++ Pontu/src/debug/printer.c | 7 ++++ Pontu/src/model/Coord.c | 5 +++ Pontu/src/model/Game.c | 68 +++++++++++++++++++++++++++++++ 9 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 Pontu/include/debug/printer.h create mode 100644 Pontu/include/engine/ArrayUtils.h create mode 100644 Pontu/include/model/arrayCoord.h create mode 100644 Pontu/src/debug/printer.c diff --git a/Pontu/entryPoints/main.c b/Pontu/entryPoints/main.c index deadfcd..a8dc4f5 100644 --- a/Pontu/entryPoints/main.c +++ b/Pontu/entryPoints/main.c @@ -5,7 +5,10 @@ #include "engine/InputElement.h" #include "engine/TextureHandler.h" #include "model/Game.h" +#include "model/arrayCoord.h" #include "view/GameDrawer.h" +#include "engine/ArrayUtils.h" +#include "debug/printer.h" int main(int argc, char* argv[]) { @@ -35,19 +38,22 @@ int main(int argc, char* argv[]) } InputProcessor inputProcessor = {.selectedCase = {.x=-1, .y=-1}}; + struct array_Coord interactiveCases = array_Coord_Create(); + int wBoardRect=99*3, hBoardRect=99*3; SDL_Rect boardRect = {.x=windowSize.w/2 - wBoardRect/2, .y=windowSize.h/2 - hBoardRect/2, .w=wBoardRect, .h=99*3}; const char* pseudos[] = {"Azerty","Bépo"}; Game game = newGame(2, pseudos); TextureHandler textureHandler = newTextureHandler(renderer); + bool quit = false; while(!quit) { // Event handling InputElement inputElement; while (InputType_None != (inputElement = proccessInput(&inputProcessor, &boardRect)).type) { - + switch (inputElement.type) { case InputType_ActivateUI: @@ -66,19 +72,38 @@ int main(int argc, char* argv[]) break; case InputType_MoveGame: fprintf(stderr, "Move on board\n"); + fprintf(stderr, "From (%d; %d)\n", inputElement.data.move.start.x, inputElement.data.move.start.y); + fprintf(stderr, "To (%d; %d)\n", inputElement.data.move.end.x, inputElement.data.move.end.y); + moveOnBoard(inputElement.data.move.start, inputElement.data.move.end, &game); break; case InputType_ClickGame: fprintf(stderr, "Clic on board (%d; %d)\n", inputElement.data.coord.x, inputElement.data.coord.y); - fflush(stderr); + fprintf(stderr, "\tSelected case : (%d; %d)\n", inputProcessor.selectedCase.x, inputProcessor.selectedCase.y); + + if(!array_Coord_Contains(&interactiveCases, inputElement.data.coord, *coordEqual)) { + fprintf(stderr, "\tselected case reset\n"); + inputProcessor.selectedCase = newCoord(-1,-1); + } + if (clickOnBoard(inputElement.data.coord, &game)) { + fprintf(stderr, "\tselected case reset\n"); inputProcessor.selectedCase = newCoord(-1,-1); } + + break; case InputType_None: default: break; } + + array_Coord_Free(&interactiveCases); + interactiveCases = getInteractiveCases(&game, inputProcessor.selectedCase); + fprintf(stderr, "Interactive cases : {"); + array_Coord_Foreach(&interactiveCases, *printCoord); + fprintf(stderr, "}\n"); + fflush(stderr); } @@ -95,6 +120,7 @@ int main(int argc, char* argv[]) Quit: freeTextureHandler(&textureHandler); + array_Coord_Free(&interactiveCases); if(renderer != NULL) { SDL_DestroyRenderer(renderer); } diff --git a/Pontu/include/debug/printer.h b/Pontu/include/debug/printer.h new file mode 100644 index 0000000..38064fb --- /dev/null +++ b/Pontu/include/debug/printer.h @@ -0,0 +1,8 @@ +#ifndef PRINTER_INCLUDED +#define PRINTER_INCLUDED + +#include + +void printCoord(const Coord coord); + +#endif //PRINTER_INCLUDED diff --git a/Pontu/include/engine/ArrayUtils.h b/Pontu/include/engine/ArrayUtils.h new file mode 100644 index 0000000..e00e0e2 --- /dev/null +++ b/Pontu/include/engine/ArrayUtils.h @@ -0,0 +1,64 @@ +#ifndef ARRAY_UTILS_INCLUDED +#define ARRAY_UTILS_INCLUDED + +#include +#include +#include + +/* +This file define a macro used to generate array structs and associated functions + +If you want an int array use + GENERATE_DYNAMIC_ARRAY(int) + +If you miss a function, write it with a type as int first then rewrite it in the macro + You will need to follow function naming convetions to avoid multiple functions with the same name + Functions in header must be marked as inline + Don't forget antislashes +*/ + +#define GENERATE_DYNAMIC_ARRAY(T) \ +struct array_##T { \ + T* elems; \ + size_t arraySize; \ +}; \ +\ +/*Contruct an empty array*/\ +inline struct array_##T array_##T##_Create() { \ + struct array_##T array = {.elems=NULL, .arraySize=0}; \ + return array; \ +} \ +\ +/*Free an array*/\ +inline void array_##T##_Free(struct array_##T* array) { \ + free(array->elems); \ + array->arraySize = 0; \ +} \ +\ +/*Add an element to an array*/\ +inline void array_##T##_AddElement(struct array_##T* array, const T element) { \ + ++(array->arraySize); \ + array->elems = realloc(array->elems, sizeof(T)*(array->arraySize)); \ + array->elems[array->arraySize - 1] = element; \ +} \ +\ +/*Check if an array contains an element*/\ +inline bool array_##T##_Contains(const struct array_##T* const array, const T element, bool (*areEqual)(const T, const T)) { \ + for (size_t i = 0; i < array->arraySize; i++) { \ + if (areEqual(array->elems[i], element)) { \ + return true; \ + } \ + } \ + return false; \ +}\ +\ +/*Apply a function to each element in the array*/\ +inline void array_##T##_Foreach(const struct array_##T* const array, void (*func)(const T)) { \ + for(size_t i = 0; iarraySize; ++i) { \ + func(array->elems[i]);\ + }\ +} + + + +#endif //ARRAY_UTILS_INCLUDED diff --git a/Pontu/include/model/Coord.h b/Pontu/include/model/Coord.h index c41afbd..0891866 100644 --- a/Pontu/include/model/Coord.h +++ b/Pontu/include/model/Coord.h @@ -9,6 +9,8 @@ #define COORD_INCLUDED #include +#include "model/Island.h" +#include "engine/ArrayUtils.h" /** * \struct Coord @@ -46,4 +48,12 @@ bool coordValid(const Coord coord); */ bool coordEqual(const Coord a, const Coord b); +/** + * \brief Convert an island to a coord + * + * \param[in] island The island to convert + * \return Coord from the island + */ +Coord islandToCoord(const Island* island); + #endif //COORD_INCLUDED diff --git a/Pontu/include/model/Game.h b/Pontu/include/model/Game.h index 94ce9ab..d1f3e5b 100644 --- a/Pontu/include/model/Game.h +++ b/Pontu/include/model/Game.h @@ -13,6 +13,7 @@ #include "model/Bridge.h" #include "model/Island.h" #include "model/Coord.h" +#include "model/arrayCoord.h" #include #include @@ -177,5 +178,14 @@ bool clickOnBoard(const Coord coord, Game* game); */ bool rmBridge(Bridge bridge, Board* board); +/** + * \brief List cases that can be interacted with for movement + * + * \param[in] game The game + * \param[in] selectedCase The selected case + * \return struct array_Coord An array of coord /!\ Care to free this array with array_Coord_Free + */ +struct array_Coord getInteractiveCases(const Game* const game, const Coord selectedCase); + #endif //GAME_H diff --git a/Pontu/include/model/arrayCoord.h b/Pontu/include/model/arrayCoord.h new file mode 100644 index 0000000..031be97 --- /dev/null +++ b/Pontu/include/model/arrayCoord.h @@ -0,0 +1,9 @@ +#ifndef ARRAY_COORD_INCLUDED +#define ARRAY_COORD_INCLUDED + +#include "model/Coord.h" +#include "engine/ArrayUtils.h" + +GENERATE_DYNAMIC_ARRAY(Coord) + +#endif //ARRAY_COORD_INCLUDED diff --git a/Pontu/src/debug/printer.c b/Pontu/src/debug/printer.c new file mode 100644 index 0000000..a1b8451 --- /dev/null +++ b/Pontu/src/debug/printer.c @@ -0,0 +1,7 @@ +#include "debug/printer.h" +#include + +void printCoord(const Coord coord) { + fprintf(stderr, "(%d, %d)", coord.x, coord.y); +} + diff --git a/Pontu/src/model/Coord.c b/Pontu/src/model/Coord.c index 3bacee9..efd7dea 100644 --- a/Pontu/src/model/Coord.c +++ b/Pontu/src/model/Coord.c @@ -15,3 +15,8 @@ bool coordEqual(const Coord a, const Coord b) { return a.x == b.x && a.y == b.y; } + +Coord islandToCoord(const Island* island) { + Coord coord = {.x = island->x*2, .y = island->y*2}; + return coord; +} \ No newline at end of file diff --git a/Pontu/src/model/Game.c b/Pontu/src/model/Game.c index 39d33e6..28a73f6 100644 --- a/Pontu/src/model/Game.c +++ b/Pontu/src/model/Game.c @@ -1,5 +1,6 @@ #include "model/Game.h" #include "model/IslandOrBridge.h" +#include "engine/ArrayUtils.h" #include // Not defined in header to not pollute inferface @@ -376,3 +377,70 @@ bool rmBridge(Bridge bridge, Board* board) return false; } + + +struct array_Coord getInteractiveCases(const Game* const game, const Coord selectedCase) { + switch (game->phase) + { + case PLACEMENT: + assert(false && "To be implemented"); + return array_Coord_Create(); + case MOVE_PIECE: { + struct array_Coord retVal = array_Coord_Create(); + + for (size_t i = 0; i < game->board.nbPieces; ++i) + { + if (game->board.arrPieces[i].idJ == game->currentPlayerID && !game->board.arrPieces[i].stuck) { + size_t nbIsland; + Island* islands = islandsAround(game->board.arrPieces[i].island, &nbIsland); + + if (nbIsland != 0) { + Coord pieceCoord = islandToCoord(&game->board.arrPieces[i].island); + if (!coordValid(selectedCase)) { + array_Coord_AddElement(&retVal, pieceCoord); + } + else { + if (coordEqual(pieceCoord, selectedCase)) { + for (size_t iIsle = 0; iIsle < nbIsland; ++iIsle) + { + if (pieceCanMoveTo(&game->board.arrPieces[i], islands[iIsle], &game->board)) { + Coord coordIsland = islandToCoord(&islands[iIsle]); + array_Coord_AddElement(&retVal, coordIsland); + } + } + free(islands); + return retVal; + } + } + } + free(islands); + } + } + return retVal; + } + case RM_BRIDGE: { + struct array_Coord retVal = array_Coord_Create(); + + for (size_t y = 0; y<5; ++y) { + for (size_t x = 0; x<4; ++x) { + if (game->board.hBridges[y][x]) { + Coord coord = {.x=x*2+1, .y=y*2}; + array_Coord_AddElement(&retVal, coord); + } + } + } + for (size_t y = 0; y<4; ++y) { + for (size_t x = 0; x<5; ++x) { + if (game->board.vBridges[y][x]) { + Coord coord = {.x=x*2, .y=y*2+1}; + array_Coord_AddElement(&retVal, coord); + } + } + } + + return retVal; + } + default: + return array_Coord_Create(); + } +}