Added a Texture handler (care MACROS (ask me if you want explainations)), First version of game's sprites and drawBoard completed for now

merge-requests/1/merge
marouault 4 years ago
parent d6ddd1e1ed
commit 718fdee338

@ -3,6 +3,9 @@
#include <stdbool.h> #include <stdbool.h>
#include "engine/InputProcessor.h" #include "engine/InputProcessor.h"
#include "engine/InputElement.h" #include "engine/InputElement.h"
#include "engine/TextureHandler.h"
#include "model/Game.h"
#include "view/BoardDrawer.h"
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
@ -31,14 +34,18 @@ int main(int argc, char* argv[])
} }
InputProcessor inputProcessor = {.selectedCase = {.x=-1, .y=-1}}; InputProcessor inputProcessor = {.selectedCase = {.x=-1, .y=-1}};
SDL_Rect rectBoard = {.x=20, .y=20, .w=99, .h=99}; SDL_Rect boardRect = {.x=20, .y=20, .w=99*3, .h=99*3};
const char* pseudos[] = {"Azerty","Bépo"};
Game game = newGame(2, pseudos);
TextureHandler textureHandler = newTextureHandler(renderer);
bool quit = false; bool quit = false;
while(!quit) while(!quit)
{ {
// Event handling // Event handling
InputElement inputElement; InputElement inputElement;
while (InputType_None != (inputElement = proccessInput(&inputProcessor, &rectBoard)).type) { while (InputType_None != (inputElement = proccessInput(&inputProcessor, &boardRect)).type) {
switch (inputElement.type) switch (inputElement.type)
{ {
@ -70,7 +77,7 @@ int main(int argc, char* argv[])
// Drawing // Drawing
drawBoard(renderer, &boardRect, &game.board, textureHandler.textures[TEXTURE_Island], textureHandler.textures[TEXTURE_Bridge], textureHandler.textures[TEXTURE_Water]);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
@ -80,12 +87,14 @@ int main(int argc, char* argv[])
statut = EXIT_SUCCESS; statut = EXIT_SUCCESS;
Quit: Quit:
freeTextureHandler(&textureHandler);
if(renderer != NULL) { if(renderer != NULL) {
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
} }
if(window != NULL) { if(window != NULL) {
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
} }
SDL_Quit(); SDL_Quit();
return statut; return statut;
} }

@ -0,0 +1,32 @@
#ifndef TEXTURE_HANDLER_INCLUDED
#define TEXTURE_HANDLER_INCLUDED
#include <SDL2/SDL.h>
#include <stdio.h>
#include "engine/TextureLoader.h"
#define MACRO_FOR_ALL_TEXTURES(M) \
M(Island) \
M(Bridge) \
M(Piece) \
M(Water)
#define MACRO_IDENTITY_COMMA(E) TEXTURE_##E,
typedef enum
{
MACRO_FOR_ALL_TEXTURES(MACRO_IDENTITY_COMMA)
NB_TEXTURES_DEFINED
} EnumTextures;
typedef struct
{
SDL_Texture* textures[NB_TEXTURES_DEFINED];
} TextureHandler;
TextureHandler newTextureHandler(SDL_Renderer* renderer);
void freeTextureHandler(TextureHandler* textureHandler);
#endif // TEXTURE_HANDLER_INCLUDED

@ -5,7 +5,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "model/Board.h" #include "model/Board.h"
bool drawBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* board, SDL_Texture* island, SDL_Texture* bridge); bool drawBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* board, SDL_Texture* island, SDL_Texture* bridge, SDL_Texture* water);
#endif #endif

Binary file not shown.

After

Width:  |  Height:  |  Size: 535 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 598 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 624 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 512 B

@ -23,6 +23,7 @@ bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button)
return false; return false;
} }
button->drawn = true; button->drawn = true;
return true;
} }
bool isHover(P_Button* button,const int x,const int y) bool isHover(P_Button* button,const int x,const int y)
@ -41,6 +42,7 @@ bool changeButtonTexture(P_Button* button, SDL_Texture* texture)
return false; return false;
} }
button->texture = texture; button->texture = texture;
return true;
} }
bool changeButtonHoverTexture(P_Button* button, SDL_Texture* texture) bool changeButtonHoverTexture(P_Button* button, SDL_Texture* texture)
@ -50,4 +52,5 @@ bool changeButtonHoverTexture(P_Button* button, SDL_Texture* texture)
return false; return false;
} }
button->hoverTexture = texture; button->hoverTexture = texture;
return true;
} }

@ -0,0 +1,33 @@
#include "engine/TextureHandler.h"
#define MACRO_TO_TEXTURE_NAME(N) #N".png",
const char* texturesNames[] = {
MACRO_FOR_ALL_TEXTURES(MACRO_TO_TEXTURE_NAME)
};
TextureHandler newTextureHandler(SDL_Renderer* renderer) {
TextureHandler tH;
const char directoryPath[] = "rsrc/img/";
fprintf(stderr, "Nb textures %d\n", NB_TEXTURES_DEFINED);
for (size_t i = 0; i<NB_TEXTURES_DEFINED; ++i) {
char* path = (char*)malloc((strlen(directoryPath)+strlen(texturesNames[i])+1)*sizeof(char));
strcpy(path, directoryPath);
tH.textures[i] = createTextureFromPath(renderer, strcat(path,texturesNames[i]));
free(path);
}
return tH;
}
void freeTextureHandler(TextureHandler* textureHandler) {
for (size_t i = 0; i<NB_TEXTURES_DEFINED; ++i) {
if (textureHandler->textures[i] != NULL) {
SDL_DestroyTexture(textureHandler->textures[i]);
textureHandler->textures[i] = NULL;
}
}
}

@ -1,9 +1,55 @@
#include "view/BoardDrawer.h" #include "view/BoardDrawer.h"
#include "model/Game.h"
bool drawBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* board, SDL_Texture* island, SDL_Texture* bridge) bool drawBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* board, SDL_Texture* island, SDL_Texture* bridge, SDL_Texture* water)
{ {
int h = boardRect->h / 9; int h = boardRect->h / 9;
int w = boardRect->w / 9; int w = boardRect->w / 9;
SDL_RenderCopy(renderer, water, NULL, boardRect);
//Islands
for (int y=0; y<9; y+=2) {
for (int x=0; x<9; x+=2) {
const SDL_Rect destRect = {
.x = boardRect->x+x*w,
.y = boardRect->y+y*h,
.w = w,
.h = h,
};
SDL_RenderCopy(renderer, island, NULL, &destRect);
}
}
//HBridge
for (int y=0; y<5; ++y) {
for (int x=0; x<4; ++x) {
if (board->hBridges[y][x]) {
const SDL_Rect destRect = {
.x = boardRect->x+w+x*w*2,
.y = boardRect->y+y*h*2,
.w = w,
.h = h,
};
SDL_RenderCopy(renderer, bridge, NULL, &destRect);
}
}
}
//HBridge
for (int y=0; y<4; ++y) {
for (int x=0; x<5; ++x) {
if (board->vBridges[y][x]) {
const SDL_Rect destRect = {
.x = boardRect->x+x*w*2,
.y = boardRect->y+h+y*h*2,
.w = w,
.h = h,
};
SDL_RenderCopyEx(renderer, bridge, NULL, &destRect, 90.0, NULL, SDL_FLIP_NONE);
}
}
}
return true;
} }

Loading…
Cancel
Save