Skip to content
This repository has been archived by the owner on Apr 30, 2020. It is now read-only.

Commit

Permalink
Add Path utilities to easily deal with paths
Browse files Browse the repository at this point in the history
  • Loading branch information
geekymoose committed Apr 26, 2020
1 parent 9d9d400 commit 5dbf6df
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ These are the current features.
- Image
- Input system
- Logs
- Path
- Shader
- Sprite
- Str
Expand Down
2 changes: 0 additions & 2 deletions src/engine/fonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include "engine/texture.h"

// TODO Note: finally not used in fishing-time-2d

typedef struct
{
int width;
Expand Down
70 changes: 70 additions & 0 deletions src/engine/path.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "engine/path.h"

#include "engine/assertions.h"

#include <stdarg.h>
#include <string.h>

Path makePath(int _count, ...)
{
Path path;
memset(path.str, '\0', _PATH_MAX_SIXE);

va_list ap;
va_start(ap, _count);

for (int k = 0; k < _count; ++k) {
appendPath(&path, va_arg(ap, char*));
}

va_end(ap);

return path;
}

void appendPath(Path* _dest, const char* _append)
{
// ASSERT_MSG(_dest != NULL, "[Path] Invalid parameter");
// ASSERT_MSG(_append != NULL, "[Path] Invalid parameter");
ASSERT_MSG(_dest->str != _append, "[Path] Append string should not be the dest string");

if (_dest == NULL || _append == NULL || _dest->str == _append) {
return;
}

int sizeDest = strlen(_dest->str);

// Exception case where _dest is empty
if (sizeDest == 0) {
int size = strlen(_append);
size = size < _PATH_MAX_SIXE ? size : _PATH_MAX_SIXE - 1;
strncat(_dest->str, _append, size);
return;
}

// Exception case where _dest is already full
if (sizeDest == _PATH_MAX_SIXE - 1) {
return;
}

// Append / to the end of dest if not there
if (_dest->str[sizeDest - 1] != '/') {
strncat(_dest->str, "/", 2);
sizeDest += 1;
}

// Bypass the / in _append because we already added it in _dest
int offset = 0;
if (_append[0] == '/') {
offset = 1;
}

int sizeAppend = strlen(_append) - offset;
sizeAppend = ((sizeDest + sizeAppend) < _PATH_MAX_SIXE) ? sizeAppend : (_PATH_MAX_SIXE - 1 - sizeDest);
int finalSize = sizeDest + sizeAppend;
sizeAppend = sizeAppend >= 0 ? sizeAppend : 0;

strncat(_dest->str, _append + offset, sizeAppend);

_dest->str[finalSize] = '\0';
}
12 changes: 12 additions & 0 deletions src/engine/path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#define _PATH_MAX_SIXE 256 // Includes '\0'

/// Path is a fixed size string with / between each words.
typedef struct
{
char str[_PATH_MAX_SIXE];
} Path;

Path makePath(int _count, ...);
void appendPath(Path* _dest, const char* _append);
16 changes: 10 additions & 6 deletions src/engine/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@

void concatStrings(char* _dest, const size_t _destBufferSize, const char* _str1, const char* _str2)
{
ASSERT_MSG(_dest != NULL, "Invalid parameter: destination string should not be null");
ASSERT_MSG(_str1 != NULL, "Invalid parameter: source string should not be null");
ASSERT_MSG(_str2 != NULL, "Invalid parameter: source string should not be null");
ASSERT_MSG(_destBufferSize > 0, "Invalid parameter: destination string size is not valid");
ASSERT_MSG(_dest != NULL, "[Str] Invalid parameter: destination string should not be null");
ASSERT_MSG(_str1 != NULL, "[Str] Invalid parameter: source string should not be null");
ASSERT_MSG(_str2 != NULL, "[Str] Invalid parameter: source string should not be null");
ASSERT_MSG(_dest != _str1, "[Str] Source and destination overlap");
ASSERT_MSG(_destBufferSize > 0, "[Str] Invalid parameter: destination string size is not valid");

if (_dest == NULL || _str1 == NULL || _str2 == NULL) {
LOG_ERR("Unable to concat string (NULL given)");
LOG_ERR("[Str] Unable to concat string (NULL given)");
return;
} else if (_destBufferSize <= 0) {
LOG_ERR("Unable to concat string (invalid size given)");
LOG_ERR("[Str] Unable to concat string (invalid size given)");
return;
} else if (_dest == _str1 || _dest == _str2) {
LOG_ERR("[Str] Source and destination overlap");
return;
}

Expand Down
14 changes: 4 additions & 10 deletions src/gameplay/game_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include "engine/assertions.h"
#include "engine/inputs.h"
#include "engine/log.h"
#include "engine/path.h"
#include "engine/shader.h"
#include "engine/str.h"
#include "engine/types.h"
#include "gameplay/fishing.h"
#include "gameplay/game_menus.h"
Expand All @@ -18,15 +18,9 @@ int gameInit(void* _gamePtr)
GameApp* game = (GameApp*)_gamePtr;
ASSERT_MSG(game != NULL, "Internal critical error: NULL parameter from the engine");

char vertex_path[255];
concatStrings(vertex_path, 255, game->shadersPath, "/");
concatStrings(vertex_path, 255, vertex_path, "vertex_shader.glsl");

char fragment_path[255];
concatStrings(fragment_path, 255, game->shadersPath, "/");
concatStrings(fragment_path, 255, fragment_path, "fragment_shader.glsl");

game->engine->shaderID = createShaderProgramFromFile(vertex_path, fragment_path);
Path vertex_path = makePath(2, game->shadersPath, "vertex_shader.glsl");
Path fragment_path = makePath(2, game->shadersPath, "fragment_shader.glsl");
game->engine->shaderID = createShaderProgramFromFile(vertex_path.str, fragment_path.str);

s_fishingTime = malloc(sizeof(FishingTime));
if (s_fishingTime == NULL) {
Expand Down
19 changes: 5 additions & 14 deletions src/gameplay/game_resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "engine/files.h"
#include "engine/libmath.h"
#include "engine/log.h"
#include "engine/path.h"
#include "engine/resources.h"
#include "engine/str.h"
#include "engine/types.h"

#include <cjson/cJSON.h>
Expand All @@ -15,12 +15,8 @@

static unsigned int internalLoadTexture(const char* _resourceDirPath, const char* _resourceName)
{
const size_t size = strlen(_resourceDirPath) + strlen(_resourceName) + 2; // +2 for the '/' and '\0'
char fullpath[size];
concatStrings(fullpath, size, _resourceDirPath, "/");
concatStrings(fullpath, size, fullpath, _resourceName);

return resourceLoadTexture(fullpath);
Path fullpath = makePath(2, _resourceDirPath, _resourceName);
return resourceLoadTexture(fullpath.str);
}

static unsigned int internalLoadSpriteFromJSON(const cJSON* _json, const char* _name, unsigned int _texID)
Expand All @@ -46,14 +42,9 @@ static void internalLoadSpritesheet(GameResources* _resources, const char* _reso
{
LOG_INFO("[GameResources] Loading spritesheet resource");

const char* _resourceName = "spritesheet.json";

const size_t size = strlen(_resourcesDirPath) + strlen(_resourceName) + 2; // +2 for the '/' and '\0'
char fullpath[size];
concatStrings(fullpath, size, _resourcesDirPath, "/");
concatStrings(fullpath, size, fullpath, _resourceName);
Path fullpath = makePath(2, _resourcesDirPath, "spritesheet.json");

char* json_buffer = (char*)newReadFileContent(fullpath);
char* json_buffer = (char*)newReadFileContent(fullpath.str);
cJSON* json = cJSON_Parse(json_buffer);

if (json == NULL) {
Expand Down

0 comments on commit 5dbf6df

Please sign in to comment.