diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c605965..853ef904 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required (VERSION 3.20) include($ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake) set(SWA_THIRDPARTY_ROOT ${CMAKE_SOURCE_DIR}/thirdparty) +set(SWA_TOOLS_ROOT ${CMAKE_SOURCE_DIR}/tools) set(CMAKE_CXX_STANDARD 23) set(BUILD_SHARED_LIBS OFF) @@ -14,9 +15,9 @@ endif() set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") add_subdirectory(${SWA_THIRDPARTY_ROOT}) -project("UnleashedRecomp-ALL") +add_subdirectory(${SWA_TOOLS_ROOT}) -include("thirdparty/PowerRecomp/cmake/bin2h.cmake") +project("UnleashedRecomp-ALL") # Include sub-projects. add_subdirectory("UnleashedRecompLib") diff --git a/UnleashedRecomp/CMakeLists.txt b/UnleashedRecomp/CMakeLists.txt index ab279650..4edbfbed 100644 --- a/UnleashedRecomp/CMakeLists.txt +++ b/UnleashedRecomp/CMakeLists.txt @@ -3,6 +3,32 @@ set(TARGET_NAME "SWA") option(SWA_XAUDIO2 "Use XAudio2 for audio playback" OFF) +function(BIN2C) + cmake_parse_arguments(BIN2C_ARGS "" "TARGET_OBJ;SOURCE_FILE;DEST_FILE;ARRAY_TYPE;ARRAY_NAME" "" ${ARGN}) + + if(NOT BIN2C_ARGS_TARGET_OBJ) + message(FATAL_ERROR "TARGET_OBJ not specified.") + endif() + + if(NOT BIN2C_ARGS_SOURCE_FILE) + message(FATAL_ERROR "SOURCE_FILE not specified.") + endif() + + if(NOT BIN2C_ARGS_DEST_FILE) + set(BIN2C_ARGS_DEST_FILE "${BIN2C_ARGS_SOURCE_FILE}") + endif() + + add_custom_command(OUTPUT "${BIN2C_ARGS_DEST_FILE}.c" + COMMAND file_to_c "${BIN2C_ARGS_SOURCE_FILE}" "${BIN2C_ARGS_ARRAY_NAME}" "${BIN2C_ARGS_ARRAY_TYPE}" "${BIN2C_ARGS_DEST_FILE}.c" "${BIN2C_ARGS_DEST_FILE}.h" + DEPENDS "${BIN2C_ARGS_SOURCE_FILE}" file_to_c + BYPRODUCTS "${BIN2C_ARGS_DEST_FILE}.h" + COMMENT "Generating binary header for ${BIN2C_ARGS_SOURCE_FILE}..." + ) + + set_source_files_properties(${BIN2C_ARGS_DEST_FILE}.c PROPERTIES SKIP_PRECOMPILE_HEADERS ON) + target_sources(${BIN2C_ARGS_TARGET_OBJ} PRIVATE ${BIN2C_ARGS_DEST_FILE}.c) +endfunction() + add_compile_options( /fp:strict -march=sandybridge @@ -302,3 +328,10 @@ generate_aggregate_header( "${CMAKE_CURRENT_SOURCE_DIR}/api" "${CMAKE_CURRENT_SOURCE_DIR}/api/SWA.h" ) + +set(RESOURCES_SOURCE_PATH "${PROJECT_SOURCE_DIR}/../UnleashedRecompResources") +set(RESOURCES_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/res") +BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/achievements_menu/trophy.dds" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/achievements_menu/trophy.dds" ARRAY_TYPE "unsigned char" ARRAY_NAME "g_trophy") +BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/game_icon.bmp" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/game_icon.bmp" ARRAY_TYPE "unsigned char" ARRAY_NAME "g_game_icon") +BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/game_icon_night.bmp" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/game_icon_night.bmp" ARRAY_TYPE "unsigned char" ARRAY_NAME "g_game_icon_night") +BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/pause.dds" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/pause.dds" ARRAY_TYPE "unsigned char" ARRAY_NAME "g_pause") diff --git a/UnleashedRecomp/res/.gitignore b/UnleashedRecomp/res/.gitignore index e3285599..435cdeaa 100644 --- a/UnleashedRecomp/res/.gitignore +++ b/UnleashedRecomp/res/.gitignore @@ -1,2 +1,3 @@ ![Ww][Ii][Nn]32/ +*.c *.h \ No newline at end of file diff --git a/UnleashedRecomp/ui/window.h b/UnleashedRecomp/ui/window.h index 96464122..9e48300b 100644 --- a/UnleashedRecomp/ui/window.h +++ b/UnleashedRecomp/ui/window.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include @@ -47,11 +47,11 @@ public: { if (isNight) { - SetIcon((void*)g_iconNight, g_iconNight_size); + SetIcon(g_game_icon_night, sizeof(g_game_icon_night)); } else { - SetIcon((void*)g_icon, g_icon_size); + SetIcon(g_game_icon, sizeof(g_game_icon)); } } diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt new file mode 100644 index 00000000..d732343c --- /dev/null +++ b/tools/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(${SWA_TOOLS_ROOT}/file_to_c) diff --git a/tools/file_to_c/CMakeLists.txt b/tools/file_to_c/CMakeLists.txt new file mode 100644 index 00000000..08975574 --- /dev/null +++ b/tools/file_to_c/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.20) + +include(CMakeParseArguments) + +project("file_to_c") +set(CMAKE_CXX_STANDARD 17) + +add_executable(file_to_c "file_to_c.cpp") diff --git a/tools/file_to_c/file_to_c.cpp b/tools/file_to_c/file_to_c.cpp new file mode 100644 index 00000000..4d69e8c9 --- /dev/null +++ b/tools/file_to_c/file_to_c.cpp @@ -0,0 +1,105 @@ +/* + MIT License + + Copyright (c) 2024 RT64 Contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +#include +#include +#include +#include + +std::vector read_file(const char* path) { + std::ifstream input_file{path, std::ios::binary}; + std::vector ret{}; + + if (!input_file.good()) { + return ret; + } + + // Get the length of the file + input_file.seekg(0, std::ios::end); + ret.resize(input_file.tellg()); + + // Read the file contents into the vector + input_file.seekg(0, std::ios::beg); + input_file.read(ret.data(), ret.size()); + + return ret; +} + +void create_parent_if_needed(const char* path) { + std::filesystem::path parent_path = std::filesystem::path{path}.parent_path(); + if (!parent_path.empty()) { + std::filesystem::create_directories(parent_path); + } +} + +int main(int argc, const char** argv) { + if (argc != 6) { + printf("Usage: %s [input file] [array name] [array type] [output C file] [output C header]\n", argv[0]); + return EXIT_SUCCESS; + } + + const char* input_path = argv[1]; + const char* array_name = argv[2]; + const char* array_type = argv[3]; + const char* output_c_path = argv[4]; + const char* output_h_path = argv[5]; + + // Read the input file's contents + std::vector contents = read_file(input_path); + + if (contents.empty()) { + fprintf(stderr, "Failed to open file %s! (Or it's empty)\n", input_path); + return EXIT_FAILURE; + } + + // Create the output directories if they don't exist + create_parent_if_needed(output_c_path); + create_parent_if_needed(output_h_path); + + // Write the C file with the array + { + std::ofstream output_c_file{output_c_path}; + output_c_file << "extern " << array_type << " " << array_name << "[" << contents.size() << "];\n"; + output_c_file << array_type << " " << array_name << "[" << contents.size() << "] = {"; + + for (char x : contents) { + output_c_file << (int)x << ", "; + } + + output_c_file << "};\n"; + } + + // Write the header file with the extern array + { + std::ofstream output_h_file{output_h_path}; + output_h_file << + "#ifdef __cplusplus\n" + " extern \"C\" {\n" + "#endif\n" + "extern " << array_type << " " << array_name << "[" << contents.size() << "];\n" + "#ifdef __cplusplus\n" + " }\n" + "#endif\n"; + } +}