Added a Texture handler (care MACROS (ask me if you want explainations)), First version of game's sprites and drawBoard completed for now
parent
d6ddd1e1ed
commit
718fdee338
Binary file not shown.
Binary file not shown.
@ -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
|
After Width: | Height: | Size: 535 B |
After Width: | Height: | Size: 598 B |
After Width: | Height: | Size: 624 B |
After Width: | Height: | Size: 512 B |
@ -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…
Reference in new issue