diff --git a/CMakeLists.txt b/CMakeLists.txt index 420794a0..e9afa7f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,6 +118,7 @@ elseif(TARGET_TYPE STREQUAL game) locale_en ) + add_dependencies() elseif(TARGET_GAME STREQUAL sandbox) add_compile_definitions( GAME_NAME="Sandbox" @@ -128,7 +129,12 @@ elseif(TARGET_TYPE STREQUAL game) GAME_DISPOSE=sandboxGameDispose GAME_VERSION=1.0 ) + tool_texture(test_texture + poker/characters/penny/sprites/sheet.png out/test.texture + ) tool_assets( + test_texture + shader_textured font_opensans texture_test @@ -136,7 +142,7 @@ elseif(TARGET_TYPE STREQUAL game) endif() # Common Game Dependencies. - add_dependencies(${PROJECT_NAME} assets) + add_dependencies(${PROJECT_NAME} assets ) add_subdirectory(client) endif() diff --git a/src/file/asset.c b/src/file/asset.c index 700488c7..d4140b05 100644 --- a/src/file/asset.c +++ b/src/file/asset.c @@ -32,6 +32,29 @@ char * assetStringLoad(char *assetName) { return str; } +uint8_t * assetRawLoad(char *assetName) { + // Open a buffer. + assetbuffer_t *fptr = assetBufferOpen(assetName); + if(fptr == NULL) return NULL; + + // Read the count of bytes in the file + fseek(fptr, 0, SEEK_END);// Seek to the end + size_t length = ftell(fptr);// Get our current position (the end) + fseek(fptr, 0, SEEK_SET);// Reset the seek + + // Create the string buffer + uint8_t *str = malloc(length); + if(str == NULL) { + assetBufferClose(fptr); + return NULL; + } + + // Read and seal the string. + fread(str, 1, length, fptr);// Read all the bytes + assetBufferClose(fptr); // Close the buffer. + return str; +} + assetbuffer_t * assetBufferOpen(char *assetName) { // Get the directory based on the raw input by creating a new string. FILE *fptr; diff --git a/src/file/asset.h b/src/file/asset.h index 295bd87a..e696b32e 100644 --- a/src/file/asset.h +++ b/src/file/asset.h @@ -27,6 +27,8 @@ typedef FILE assetbuffer_t; */ char * assetStringLoad(char *assetName); +uint8_t * assetRawLoad(char *assetName); + /** * Platform-centric method to open a file buffer to an asset. * @param assetName The asset name to open a buffer for. diff --git a/src/game/sandbox/game.c b/src/game/sandbox/game.c index 01074b9d..ef8048a2 100644 --- a/src/game/sandbox/game.c +++ b/src/game/sandbox/game.c @@ -11,9 +11,6 @@ bool sandboxGameInit(sandboxgame_t *game) { quadInit(&game->quad, 0, 0,0,0,0, 500,500,1,1); assetManagerInit(&game->manager); - assetManagerLoadTexture(&game->manager, &game->texture, - "textures/test_texture.png" - ); assetManagerLoadFont(&game->manager, &game->font, "fonts/opensans/OpenSans-Regular.ttf" ); @@ -24,6 +21,10 @@ bool sandboxGameInit(sandboxgame_t *game) { assetManagerStart(&game->manager); + pixel_t *data = (pixel_t *)assetRawLoad("out/test.texture"); + textureInit(&game->texture, 4360, 1920, (pixel_t *)data); + free(data); + return true; } diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 32b68141..f7e2203e 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -4,4 +4,5 @@ # https://opensource.org/licenses/MIT add_subdirectory(file) -add_subdirectory(vn) \ No newline at end of file +add_subdirectory(vn) +add_subdirectory(display) \ No newline at end of file diff --git a/tools/display/CMakeLists.txt b/tools/display/CMakeLists.txt new file mode 100644 index 00000000..2b561cdf --- /dev/null +++ b/tools/display/CMakeLists.txt @@ -0,0 +1,32 @@ +# Copyright (c) 2021 Dominic Msters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +cmake_minimum_required(VERSION 3.13) +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_STANDARD_REQUIRED ON) + +SET(TOOL_NAME texture_generation) +set(TOOL_TEXTURE_GENERATION_SOURCE_DIR) + +# Build Tool +project(${TOOL_NAME} VERSION 1.0) +add_executable(${TOOL_NAME}) +target_sources(${TOOL_NAME} + PRIVATE + texture_generation.c +) +target_link_libraries(${TOOL_NAME} + PUBLIC + stb +) + +# Function for creating the target +function(tool_texture target in out) + add_custom_target(${target} + COMMAND texture_generation "${ROOT_DIR}/${ASSETS_DIR}/${in}" "${ASSETS_DIR}/${out}" + COMMENT "Generating texture ${target} from ${in}" + SOURCES ${TOOL_NAME} + ) +endfunction() \ No newline at end of file diff --git a/tools/utils/file.h b/tools/utils/file.h new file mode 100644 index 00000000..f79ef68d --- /dev/null +++ b/tools/utils/file.h @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include +#include +#include +#include + +#if defined(_MSC_VER) + #include + #define getcwd _getcwd + #define FILE_PATH_SEP '\\' +#elif defined(__GNUC__) + #include + #define FILE_PATH_SEP '/' +#endif + +#ifndef STB_IMAGE_IMPLEMENTATION + #define STB_IMAGE_IMPLEMENTATION + #include +#endif + +void fileNormalizeSlashes(char *string) { + char c; + int i = 0; + + while(c = string[i++]) { + if(c != '\\' && c != '/') continue; + string[i-1] = FILE_PATH_SEP; + } +} + +void fileMkdirp(char *path) { + char buffer[FILENAME_MAX]; + char c; + int i = 0; + bool inFile; + bool hasMore; + + inFile = false; + hasMore = false; + while(c = path[i]) { + if((c == '\\' || c == '/') && i > 0) { + buffer[i] = '\0'; + _mkdir(buffer); + inFile = false; + hasMore = false; + buffer[i] = FILE_PATH_SEP; + i++; + continue; + } + + if(c == '.') inFile = true; + hasMore = true; + buffer[i] = c; + i++; + } + + if(!inFile && hasMore) { + buffer[i] = '\0'; + _mkdir(buffer); + } +} \ No newline at end of file