Skip to content

Commit

Permalink
fix: migrate map to new code after refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-kedis committed Jun 20, 2024
1 parent 2c27d1f commit 677b6e4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,6 @@ typedef struct
Settings settings;

Map map;
} Game_System;
} GameState;

#endif // STRUCTS_H
18 changes: 9 additions & 9 deletions src/system/clearResources.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
*********************************************************/

#include "../CRougeLite.h" // NOTE: declare global extern vars
#include "../game/player.h"
#include "../game/enemy.h"
#include "../game/combatAction.h"
#include "../game/enemy.h"
#include "../game/player.h"

// ***************************
// Private Function Prototypes
Expand Down Expand Up @@ -45,7 +45,8 @@ void clearGameState() {
}

void freeResource(void *item) {
if (item == NULL) return;
if (item == NULL)
return;
free(item);
}

Expand All @@ -54,7 +55,8 @@ void freeResource(void *item) {
// *****************
// FIXME: BROKEN???
static void clearDictionary(Dictionary *dict) {
if (dict == NULL) return;
if (dict == NULL)
return;

while (dict->opcode != -1) {
clearDictionaryItem(&dict);
Expand All @@ -68,15 +70,13 @@ static void clearDictionaryItem(Dictionary **dict) {
if (dict == NULL || *dict == NULL)
return;


free(*dict);
*dict = NULL;
}

void clearMap() {
Game_System *game_system = getGameSystemInstance();
Map *map = &(game_system->map);
TilesMapper *tiles_mapper = &(game_system->map.tilesMapper);
Map *map = &(gameState->map);
TilesMapper *tiles_mapper = &(gameState->map.tilesMapper);

for (int i = 0; i < tiles_mapper->numOfTiles; i++) {
free(tiles_mapper->mapper[i]);
Expand All @@ -90,4 +90,4 @@ void clearMap() {

free(map->textures);
free(map->isTexturesLoaded);
}
}
66 changes: 36 additions & 30 deletions src/system/map.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**********************************************
*
* ███╗ ███╗ █████╗ ██████╗
* ████╗ ████║██╔══██╗██╔══██╗
* ██╔████╔██║███████║██████╔╝
* ██║╚██╔╝██║██╔══██║██╔═══╝
* ██║ ╚═╝ ██║██║ ██║██║
* ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝
**********************************************/
*
* ███╗ ███╗ █████╗ ██████╗
* ████╗ ████║██╔══██╗██╔══██╗
* ██╔████╔██║███████║██████╔╝
* ██║╚██╔╝██║██╔══██║██╔═══╝
* ██║ ╚═╝ ██║██║ ██║██║
* ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝
**********************************************/

#include "map.h"
#include <raylib.h>
Expand All @@ -15,9 +15,9 @@
/**
* initTilesMapper - initilizes the TilesMapper mapper with NULL,
* and set path with mapper path.
*/
*/
static void initTilesMapper() {
Game_System *game_system = getGameSystemInstance();
GameState *game_system = gameState;
TilesMapper *tiles_mapper = &(game_system->map.tilesMapper);

for (int i = 0; i < MAX_TILES_NUM; i++) {
Expand All @@ -34,13 +34,13 @@ static void initTilesMapper() {
* loadTilesMapper - loades the tiles mapper file at the set path into memory.
*
* return: 0 if no errors, 1 if an error occured.
*/
*/
int loadTilesMapper() {
Game_System *game_system = getGameSystemInstance();
GameState *game_system = gameState;
TilesMapper *tiles_mapper = &(game_system->map.tilesMapper);

char *file_content = LoadFileText(tiles_mapper->path);

char *line = strtok(file_content, "\n");

while (line) {
Expand Down Expand Up @@ -73,18 +73,19 @@ int loadTilesMapper() {

/**
* initMap - initilizes the map and the tilesMapper.
*/
*/
void initMap() {
Game_System *game_system = getGameSystemInstance();
GameState *game_system = gameState;
Map *map = &(game_system->map);

initTilesMapper();

map->loaded = false;
map->currentLevel = 0;
map->numOfRows = 0;
map->numOfCols = 0;
map->currentLevelPath = "./src/resources/gfx/map-assets/level_one/level_one_map.csv";
map->currentLevelPath =
"./src/resources/gfx/map-assets/level_one/level_one_map.csv";
map->scale = 2.0f;

map->textures = malloc(map->tilesMapper.numOfTiles * sizeof(Texture2D));
Expand All @@ -96,7 +97,7 @@ void initMap() {
}

static void resetMapIds() {
Game_System *game_system = getGameSystemInstance();
GameState *game_system = gameState;
Map *map = &(game_system->map);

for (int row = 0; row < MAX_ROW_NUM; row++) {
Expand All @@ -110,9 +111,9 @@ static void resetMapIds() {

/**
* parseLevelFile - parses the current level file into the memory
*/
*/
void parseLevelFile() {
Game_System *game_system = getGameSystemInstance();
GameState *game_system = gameState;
Map *map = &(game_system->map);

resetMapIds();
Expand Down Expand Up @@ -154,41 +155,46 @@ void parseLevelFile() {
}

void drawMap() {
Game_System *game_system = getGameSystemInstance();
GameState *game_system = gameState;
Map *map = &(game_system->map);
TilesMapper *tilesMapper = &(game_system->map.tilesMapper);


for (int row = 0; row < map->numOfRows; row++) {
for (int col = 0; col < map->numOfCols; col++) {
int idIdx = 0;
int tileId = map->mapIds[row][col][idIdx++];

while (idIdx < 5 && tileId != -1) {
loadTileTexture(tileId);

int tileWidth = map->textures[tileId].width, tileHeight = map->textures[tileId].height;

Rectangle src = {0.0f, 0.0f, (float)tileWidth, (float)tileHeight};
Rectangle dest = {(float)(col * tileWidth) * map->scale, (float)(row * tileHeight) * map->scale,
(float)tileWidth * map->scale, (float)tileHeight * map->scale}; //Scaling the drawn texture by 2
int tileWidth = map->textures[tileId].width,
tileHeight = map->textures[tileId].height;

DrawTexturePro(map->textures[tileId], src, dest, (Vector2){0, 0}, (float)0, WHITE);
Rectangle src = {0.0f, 0.0f, (float)tileWidth, (float)tileHeight};
Rectangle dest = {(float)(col * tileWidth) * map->scale,
(float)(row * tileHeight) * map->scale,
(float)tileWidth * map->scale,
(float)tileHeight *
map->scale}; // Scaling the drawn texture by 2

DrawTexturePro(map->textures[tileId], src, dest, (Vector2){0, 0},
(float)0, WHITE);
tileId = map->mapIds[row][col][idIdx++];
}
}
}
}

static void loadTileTexture(int tileIdx) {
Game_System *game_system = getGameSystemInstance();
GameState *game_system = gameState;
Map *map = &(game_system->map);

if (map->isTexturesLoaded[tileIdx])
return;

char buffer[256];
sprintf(buffer, "./src/resources/gfx/assets-prepare/%s", map->tilesMapper.mapper[tileIdx]);
sprintf(buffer, "./src/resources/gfx/assets-prepare/%s",
map->tilesMapper.mapper[tileIdx]);

map->textures[tileIdx] = LoadTexture(buffer);
map->isTexturesLoaded[tileIdx] = true;
Expand Down

0 comments on commit 677b6e4

Please sign in to comment.