Added ArrayUtils to facilitate use of dynamic arrays. Tested some solutions to the InputHandler problem (The one which is commited mostly work)

merge-requests/1/merge
marouault 4 years ago
parent 28c65bb4db
commit b218a0eeb3

@ -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,12 +38,15 @@ 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)
{
@ -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);
}

@ -0,0 +1,8 @@
#ifndef PRINTER_INCLUDED
#define PRINTER_INCLUDED
#include <model/Coord.h>
void printCoord(const Coord coord);
#endif //PRINTER_INCLUDED

@ -0,0 +1,64 @@
#ifndef ARRAY_UTILS_INCLUDED
#define ARRAY_UTILS_INCLUDED
#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
/*
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; i<array->arraySize; ++i) { \
func(array->elems[i]);\
}\
}
#endif //ARRAY_UTILS_INCLUDED

@ -9,6 +9,8 @@
#define COORD_INCLUDED
#include <stdbool.h>
#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

@ -13,6 +13,7 @@
#include "model/Bridge.h"
#include "model/Island.h"
#include "model/Coord.h"
#include "model/arrayCoord.h"
#include <SDL2/SDL_pixels.h>
#include <stdbool.h>
@ -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

@ -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

@ -0,0 +1,7 @@
#include "debug/printer.h"
#include <stdio.h>
void printCoord(const Coord coord) {
fprintf(stderr, "(%d, %d)", coord.x, coord.y);
}

@ -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;
}

@ -1,5 +1,6 @@
#include "model/Game.h"
#include "model/IslandOrBridge.h"
#include "engine/ArrayUtils.h"
#include <assert.h>
// 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();
}
}

Loading…
Cancel
Save