diff --git a/Makefile b/Makefile index bacfd7f5..f0a89770 100644 --- a/Makefile +++ b/Makefile @@ -229,4 +229,4 @@ reset: git submodule foreach --recursive git clean -xfd cloc: - cloc --by-file --force-lang="java",wren --fullpath --not-match-d "font" -not-match-f ".inc" src + cloc --by-file --force-lang="java",wren --fullpath --not-match-d "font|dSYM" -not-match-f ".inc" src include/dome.h diff --git a/src/engine.c b/src/engine.c index 7ac0edc9..c4ea727a 100644 --- a/src/engine.c +++ b/src/engine.c @@ -7,24 +7,12 @@ getColorComponents(uint32_t color, uint8_t *r, uint8_t *g, uint8_t *b) { internal void ENGINE_openLogFile(ENGINE* engine) { - // DOME-2020-02-02-090000.log char* filename = "DOME-out.log"; engine->debug.logFile = fopen(filename, "w+"); } internal void -ENGINE_printLogVariadic(ENGINE* engine, const char* line, va_list argList) { - va_list args; - va_copy(args, argList); - size_t bufSize = vsnprintf(NULL, 0, line, args) + 1; - va_end(args); - - char buffer[bufSize]; - buffer[0] = '\0'; - va_copy(args, argList); - vsnprintf(buffer, bufSize, line, args); - va_end(args); - +ENGINE_writeToLog(ENGINE* engine, const char* buffer) { // Output to console printf("%s", buffer); @@ -38,6 +26,24 @@ ENGINE_printLogVariadic(ENGINE* engine, const char* line, va_list argList) { } } +#define RENDER_VARIADIC_STRING(buffer, line, argList) \ + va_list args;\ + va_copy(args, argList);\ + size_t bufSize = vsnprintf(NULL, 0, line, args) + 1;\ + va_end(args);\ + char buffer[bufSize];\ + buffer[0] = '\0';\ + va_copy(args, argList);\ + vsnprintf(buffer, bufSize, line, args);\ + va_end(args); + +internal void +ENGINE_printLogVariadic(ENGINE* engine, const char* line, va_list argList) { + RENDER_VARIADIC_STRING(buffer, line, argList); + ENGINE_writeToLog(engine, buffer); +} + + internal void ENGINE_printLog(ENGINE* engine, char* line, ...) { // Args is mutated by each vsnprintf call, @@ -1164,16 +1170,25 @@ ENGINE_takeScreenshot(ENGINE* engine) { stbi_write_png("screenshot.png", canvas.width, canvas.height, 4, canvas.pixels, canvas.width * 4); } - internal void -ENGINE_reportError(ENGINE* engine) { - if (engine->debug.errorBuf != NULL) { - ENGINE_printLog(engine, engine->debug.errorBuf); - if (engine->debug.errorDialog) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, - "DOME - Error", - engine->debug.errorBuf, - NULL); - } +ENGINE_reportErrorVariadic(ENGINE* engine, const char* line, va_list argList) { + if (line == NULL) { + return; + } + RENDER_VARIADIC_STRING(buffer, line, argList); + ENGINE_writeToLog(engine, buffer); + if (engine->debug.errorDialog) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, + "DOME - Error", + buffer, + NULL); } } + +internal void +ENGINE_reportError(ENGINE* engine, const char* line, ...) { + va_list args; + va_start(args, line); + ENGINE_reportErrorVariadic(engine, line, args); + va_end(args); +} diff --git a/src/game.c b/src/game.c index 6bf2370b..342cd48e 100644 --- a/src/game.c +++ b/src/game.c @@ -251,9 +251,9 @@ int DOME_begin(ENGINE* engine, char* entryPath) { gameFile = ENGINE_readFile(engine, entryPath, &gameFileLength, NULL); if (gameFile == NULL) { if (engine->tar != NULL) { - ENGINE_printLog(engine, "Error: Could not load %s in bundle.\n", entryPath); + ENGINE_reportError(engine, "Error: Could not load %s in bundle.\n", entryPath); } else { - ENGINE_printLog(engine, "Error: Could not load %s.\n", engine->argv[1]); + ENGINE_reportError(engine, "Error: Could not load %s\n", engine->argv[1]); } result = EXIT_FAILURE; goto cleanup; @@ -415,7 +415,7 @@ int DOME_begin(ENGINE* engine, char* entryPath) { } // Free resources - ENGINE_reportError(engine); + ENGINE_reportError(engine, engine->debug.errorBuf); if (initMethod != NULL) { wrenReleaseHandle(vm, initMethod); diff --git a/src/io.c b/src/io.c index ca022887..8a44db7b 100644 --- a/src/io.c +++ b/src/io.c @@ -115,6 +115,20 @@ doesFileExist(char* path) { return access(path, F_OK) != -1; } +internal bool +isPathAbsolute(const char* path) { + +#ifdef _WIN32 + return (path[0] == '/' || (strlen(path) > 3 && + isalpha(path[0]) && + path[1] == ':' && + (path[2] == '/' || path[2] == '\\'))); +#else + return (path[0] == '/'); +#endif + +} + internal int readFileFromTar(mtar_t* tar, char* path, size_t* lengthPtr, char** data) { // We assume the tar open has been done already @@ -232,4 +246,3 @@ readEntireFile(char* path, size_t* lengthPtr, char** error) { return source; } - diff --git a/src/main.c b/src/main.c index ff46f08d..e1b49b04 100644 --- a/src/main.c +++ b/src/main.c @@ -195,8 +195,13 @@ char* resolveEntryPath(ENGINE* engine, char* entryArgument, bool autoResolve) { } else { if (entryArgument != NULL) { // Set the basepath according to the incoming argument - strcpy(entryPath, base); - strcat(entryPath, entryArgument); + if (isPathAbsolute(entryArgument)) { + strcpy(entryPath, entryArgument); + } else { + strcpy(entryPath, base); + strcat(entryPath, entryArgument); + } + if (isDirectory(entryPath)) { autoResolve = true; BASEPATH_set(entryPath); @@ -247,7 +252,7 @@ char* resolveEntryPath(ENGINE* engine, char* entryArgument, bool autoResolve) { } if (!engine->fused && !resolved) { - ENGINE_printLog(engine, "Error: Could not find an entry point at: %s\n", dirname(entryPath)); + ENGINE_reportError(engine, "Error: Could not find an entry point at: %s\n", dirname(entryPath)); printUsage(engine); return NULL; } else { diff --git a/src/modules/graphics.c b/src/modules/graphics.c index f7c33b9f..81eca42b 100644 --- a/src/modules/graphics.c +++ b/src/modules/graphics.c @@ -292,8 +292,8 @@ CANVAS_clip(WrenVM* vm) { DOME_RECT rect = { .x = x, .y = y, - .w = min(width, w), - .h = min(height, h) + .w = min(width - x, w), + .h = min(height - y, h) }; engine->canvas.clip = rect; } diff --git a/src/tools/embed.c b/src/tools/embed.c index 4ae12189..261952ef 100644 --- a/src/tools/embed.c +++ b/src/tools/embed.c @@ -64,5 +64,8 @@ EMBED_perform(ENGINE* engine, char** argv) { int result = EMBED_encode(fileToConvert, length, moduleName, destination); free(fileToConvert); + if (shouldFree) { + free(destination); + } return result; } diff --git a/src/tools/nest.c b/src/tools/nest.c index 1ba58483..2abc7983 100644 --- a/src/tools/nest.c +++ b/src/tools/nest.c @@ -34,7 +34,7 @@ NEST_writeFile(ENGINE* engine, mtar_t* tar, char* filePath, char* tarPath) { internal int NEST_packDirectory(ENGINE* engine, mtar_t* tar, char* directory, size_t start) { - tinydir_dir dir; + INIT_TO_ZERO(tinydir_dir, dir); tinydir_open(&dir, directory); while (dir.has_next) {