diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..dd9b076 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Pontu/cmake"] + path = Pontu/cmake + url = https://github.com/aminosbh/sdl2-cmake-modules.git diff --git a/Pontu/CMakeLists.txt b/Pontu/CMakeLists.txt index 71c6839..dcedcc5 100644 --- a/Pontu/CMakeLists.txt +++ b/Pontu/CMakeLists.txt @@ -1,15 +1,29 @@ cmake_minimum_required(VERSION 3.13) -SET(ProjetName Pontu) +SET(PROJECT_NAME Pontu) -project(${ProjetName}) +project(${PROJECT_NAME}) file(GLOB_RECURSE MY_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c) -add_executable(${ProjetName} ${MY_SOURCES}) +add_executable(${PROJECT_NAME} ${MY_SOURCES}) -SET(WarningGCC -Wall -Wextra -pedantic -Wconversion -std=c17 -D_REENTRANT) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) -target_compile_options(${ProjetName} PRIVATE ${WarningGCC} -Og -fdiagnostics-color=always) -target_link_libraries(${ProjetName} SDL2) -target_include_directories(${ProjetName} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include /usr/include/SDL2) +find_package(SDL2 REQUIRED) +#find_package(SDL2_net REQUIRED) +#find_package(SDL2_image REQUIRED) + + +target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${SDL2_INCLUDE_DIR}) + +SET(OptionGCC -Wall -Wextra -pedantic -Wconversion -std=c17) + +if (MINGW) + target_link_options(${PROJECT_NAME} PRIVATE -lmingw32 -lSDL2main -lSDL2 -mwindows) + target_compile_options(${PROJECT_NAME} PRIVATE ${OptionGCC} -Dmain=SDL_main -fdiagnostics-color=always) +elseif(UNIX) + target_compile_options(${PROJECT_NAME} PRIVATE ${OptionGCC} -D_REENTRANT) +endif() + +target_link_libraries(${PROJECT_NAME} SDL2::Main) diff --git a/Pontu/cmake b/Pontu/cmake new file mode 160000 index 0000000..ad006a3 --- /dev/null +++ b/Pontu/cmake @@ -0,0 +1 @@ +Subproject commit ad006a3daae65a612ed87415037e32188b81071e diff --git a/Pontu/include/engine/Button.h b/Pontu/include/engine/Button.h index c34b8e3..fba1609 100644 --- a/Pontu/include/engine/Button.h +++ b/Pontu/include/engine/Button.h @@ -1,17 +1,18 @@ #ifndef BUTTON_INCLUDED #define BUTTON_INCLUDED -#include #include -typedef struct { - SDL_Texture texture; //sprite ou texture de fond - SDL_Rect rect; //définie les coordonnées et la taile, pour la hitbox et l'affichage - void (*onClick)(void); +#include +typedef struct +{ + SDL_Texture* texture; // sprite ou texture de fond + SDL_Rect rect; // définie les coordonnées et la taile, pour la hitbox et l'affichage + void (*onClick)(void); } P_Button; -P_Button createButton(SDL_Texture* texture, int coordx, int coordy, int sizex, int sizey,void (*onClick)(void)); +P_Button createButton(SDL_Texture* texture, int coordx, int coordy, int sizex, 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); +SDL_bool putButtonOnRenderer(SDL_Renderer* renderer, TTF_Font* police, P_Button* button); #endif diff --git a/Pontu/src/engine/Button.c b/Pontu/src/engine/Button.c index ed307ab..81a5f66 100644 --- a/Pontu/src/engine/Button.c +++ b/Pontu/src/engine/Button.c @@ -1,8 +1,11 @@ -#include "../moteur/boutton.h" +#include "engine/Button.h" +#include +#include /* - * Return : a button within written , placed in (; ), sized x. The button returned will do when it will be clicked - * If text and texture are not define, the function will return NULL and an error is printed in STDERR. + * Return : a button within written , placed in (; ), sized + *x. The button returned will do when it will be clicked If text and texture + *are not define, the function will return NULL and an error is printed in STDERR. * * Params : texture => background image for the button (can be NULL) * coordx => placement in width @@ -12,33 +15,15 @@ * onClick => Acction to do when the button is clicked * * Finality : Create a brand new button with specs -*/ -P_Button* createButton(SDL_Texture* texture, int coorx, int coordy, int sizex, int sizey, void (*onClick)(void)) + */ +P_Button createButton(SDL_Texture* texture, int coordx, int coordy, int sizex, int sizey, void (*onClick)(void)) { // Declarations - P_Button* b; - SDL_Rect rect; + P_Button b = { .rect = { .x = coordx, .y = coordy, .w = sizex, .h = sizey }, .onClick = onClick }; - if(texture == NULL) - { - fprintf(stderr,"WARNING: Button created without texture"); - return NULL; - } - - // Rect specs - rect.x = coordx; - rect.y = coordy; - rect.w = sizex; - rect.h = sizey; - - // Button specs - b->onClick = onClick; - b->rect = rect; + assert(texture != NULL && "WARNING: Button created without texture"); return b; } -SDL_Bool putButtonOnRenderer(SDL_Renderer* renderer, TTF_Font* police, P_Button* button) -{ - -} +SDL_bool putButtonOnRenderer(SDL_Renderer* renderer, TTF_Font* police, P_Button* button) {} diff --git a/Pontu/src/main.c b/Pontu/src/main.c index 619350a..27b25f2 100644 --- a/Pontu/src/main.c +++ b/Pontu/src/main.c @@ -1,7 +1,245 @@ #include +#include +#include +#include -int main() { +typedef enum {Absent, Present} EtatPont; - SDL_Init(SDL_INIT_EVERYTHING); - return 0; +typedef struct { + SDL_Rect zone; + EtatPont pontsH[5][4]; // [y][x] pour acceder au coord + EtatPont pontsV[4][5]; // [y][x] pour acceder au coord +} Plateau; + +Plateau CreatePlateau(int x, int y, int w, int h) { + Plateau p = {.zone={x,y,w,h}}; + for (int x = 0; x < 5; ++x) + { + for (int y = 0; y < 4; ++y) + { + p.pontsH[x][y] = Present; // inverser ici car les tableaux sont de dimentions transposées + p.pontsV[y][x] = Present; + } + } + return p; +} + +typedef struct { + int x; + int y; +} Ile; + +typedef struct { + Ile ileA; + Ile ileB; +} Pont; + +int minInt(const int a, const int b) { + return apontsV[minInt(pont.ileA.y, pont.ileB.y)][pont.ileA.x] = Absent; + return true; + } + else if(dx == 1 && dy == 0) { + plateau->pontsH[pont.ileB.y][minInt(pont.ileA.x, pont.ileB.x)] = Absent; + return true; + } + return false; +} + + + + + +typedef enum {NestRien, EstUnPont, EstUneIle} Questce; + +typedef struct { + union { + Ile ile; + Pont pont; + } value; + Questce questce; +} PontIleOuRien; + +PontIleOuRien getObjetClique(const Plateau p, const SDL_Point coordMouse) { + // On calcule la taille d'une ile / d'un pont (en x et en y) + const int ecartX = p.zone.w*2/9; + const int ecartY = p.zone.h*2/9; + + // On calcule si les coordonnees correspondes à des interval (entre iles) ou non + bool estUnIntervalX = coordMouse.x%ecartX > p.zone.w/9; + bool estUnIntervalY = coordMouse.y%ecartY > p.zone.h/9; + + // Si on tombe dans un intervalle en X et en Y on est dans un "trou" + if (estUnIntervalX && estUnIntervalY) { + PontIleOuRien retVal = {.questce=NestRien}; + return retVal; + } + + // On calcule les indices de l'ile (ou de l'ile "de départ" du pont) + int indiceX = coordMouse.x/ecartX; + int indiceY = coordMouse.y/ecartY; + + // On est pas dans un intervalle ni en X ni en Y donc on est sur l'ile (indiceX; indiceY) + if (!estUnIntervalX && !estUnIntervalY) { + PontIleOuRien retVal = {.value.ile = {.x=indiceX, .y=indiceY}, .questce=EstUneIle}; + return retVal; + } + // On est dans un intervalle en X mais pas en Y donc on est sur le pont horizontal qui va de l'ile (indiceX; indiceY) à l'ile (indiceX+1; indiceY) + else if (estUnIntervalX && !estUnIntervalY) { // pont h + PontIleOuRien retVal = {.value.pont = {.ileA={.x=indiceX, .y=indiceY}, .ileB={.x=indiceX+1, .y=indiceY}}, .questce=EstUnPont}; + return retVal; + } + // On est dans un intervalle en Y mais pas en X donc on est sur le pont vertical qui va de l'ile (indiceX; indiceY) à l'ile (indiceX; indiceY+1) + else { // pont v + PontIleOuRien retVal = {.value.pont = {.ileA={.x=indiceX, .y=indiceY}, .ileB={.x=indiceX, .y=indiceY+1}}, .questce=EstUnPont}; + return retVal; + } +} + +void affichePontsPontu(SDL_Renderer* renderer, const Plateau p) { + SDL_Color marron = {100, 100, 10, 255}; + SDL_SetRenderDrawColor(renderer, marron.r, marron.g, marron.b, marron.a); + + // Un rect que l'on déplace sur chaque emplacement de pont horizontal + SDL_Rect pontsH = {p.zone.x+p.zone.w/9, p.zone.y+p.zone.h/(4*9), p.zone.w/9, p.zone.h/(2*9)}; + for (int y = 0; y < 5; ++y) + { + pontsH.x = p.zone.x+p.zone.w/9; + for (int x = 0; x < 4; ++x) + { + if (p.pontsH[y][x] == Present) { + SDL_RenderFillRect(renderer, &pontsH); + } + pontsH.x += p.zone.w*2/9; + } + pontsH.y += p.zone.h*2/9; + } + + // Un rect que l'on déplace sur chaque emplacement de pont vertical + SDL_Rect pontsV = {p.zone.x+p.zone.w/(4*9), p.zone.y+p.zone.h/9, p.zone.w/(2*9), p.zone.h/9}; + for (int y = 0; y < 4; ++y) + { + pontsV.x = p.zone.x+p.zone.w/(4*9); + for (int x = 0; x < 5; ++x) + { + if (p.pontsV[y][x] == Present) { + SDL_RenderFillRect(renderer, &pontsV); + } + pontsV.x += p.zone.w*2/9; + } + pontsV.y += p.zone.h*2/9; + } +} + +void affichePlateauPontu(SDL_Renderer* renderer, const SDL_Rect zone) +{ + SDL_Color bleu = {10, 10, 200, 255}; + SDL_Color vert = {50, 220, 10, 255}; + + SDL_SetRenderDrawColor(renderer, bleu.r, bleu.g, bleu.b, bleu.a); + SDL_RenderClear(renderer); + + SDL_SetRenderDrawColor(renderer, vert.r, vert.g, vert.b, vert.a); + + SDL_Rect iles = {zone.x, zone.y, zone.w/9, zone.h/9}; + for (int y = 0; y < 5; ++y) + { + iles.x = zone.x; + for (int x = 0; x < 5; ++x) + { + SDL_RenderFillRect(renderer, &iles); + iles.x += zone.w*2/9; + } + iles.y += zone.h*2/9; + } +} + +int main(int argc, char* argv[]) +{ + SDL_Window* window = NULL; + SDL_Renderer* renderer = NULL; + + int statut = EXIT_FAILURE; + + if(SDL_Init(SDL_INIT_VIDEO) != 0) { + fprintf(stderr, "Erreur : %s\n", SDL_GetError()); + goto Quit; + } + + window = SDL_CreateWindow("Coucouc c Martin",10,10,600,600,SDL_WINDOW_SHOWN); + if (!window) + { + fprintf(stderr, "Erreur : %s\n", SDL_GetError()); + goto Quit; + } + + renderer = SDL_CreateRenderer(window,-1,0); + if(!renderer) + { + fprintf(stderr, "Erreur : %s", SDL_GetError()); + goto Quit; + } + + //const SDL_Texture* texturePont = CreateTexture("img/pont.bmp"); + + Plateau plateau = CreatePlateau(5,5,9*60, 9*60); // Les tailles doivent-être des multiples de 9 + + SDL_bool quit = SDL_FALSE; + while(!quit) + { + SDL_Event event; + + while(SDL_PollEvent(&event)) + { + switch (event.type) { + case SDL_QUIT: + quit = SDL_TRUE; + break; + case SDL_MOUSEBUTTONDOWN: { + SDL_Point coordMouse = {.x = event.button.x, .y=event.button.y}; + if (SDL_PointInRect(&coordMouse,&(plateau.zone))) { + PontIleOuRien objetClique = getObjetClique(plateau, coordMouse); + switch (objetClique.questce) { + case EstUnPont: + printf("Pont de {%d; %d} à {%d; %d}\n", objetClique.value.pont.ileA.x, objetClique.value.pont.ileA.y, objetClique.value.pont.ileB.x, objetClique.value.pont.ileB.y); + supprimerPont(&plateau,objetClique.value.pont); + break; + case EstUneIle: + printf("Ile {%d; %d}\n", objetClique.value.ile.x, objetClique.value.ile.y); + break; + default: + printf("Rien\n"); + break; + } + } + break; + } + } + } + + // Affichage + affichePlateauPontu(renderer, plateau.zone); + affichePontsPontu(renderer, plateau); + + SDL_RenderPresent(renderer); + SDL_Delay(20); + } + + statut = EXIT_SUCCESS; +Quit: + if(renderer != NULL) { + SDL_DestroyRenderer(renderer); + } + if(window != NULL) { + SDL_DestroyWindow(window); + } + SDL_Quit(); + return statut; } \ No newline at end of file diff --git a/Pontu/src/model/Player.c b/Pontu/src/model/Player.c index 559291d..3f0294b 100644 --- a/Pontu/src/model/Player.c +++ b/Pontu/src/model/Player.c @@ -6,5 +6,7 @@ Player newPlayer(char pseudo[PSEUDO_LENMAX], strcpy(player.pseudo, pseudo); player.color = color; player.rank = 0; + + return player; }