From 40c0efcb594ea5a107434b2b1fea417b977ce83d Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 5 Sep 2020 12:58:27 +0200 Subject: [PATCH] Fix building via CMake. --- .gitignore | 1 + CMakeLists.txt | 57 ++++++++++++++++++++++++++++++++++++++++- README.md | 9 +++++++ filelist.sh | 29 ++++++++++++++++++--- src/main.cpp | 1 - src/menu/MainWindow.cpp | 3 --- 6 files changed, 91 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 5757349..c5e827c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ build-pc-win/ data/images/button.png.h data/images/button.png.o src/resources/filelist.h +cmake-build/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 48b686f..5bfb342 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,4 +45,59 @@ find_package(SDL2_image REQUIRED) find_package(SDL2_ttf REQUIRED) find_package(SDL2_mixer REQUIRED) -target_link_libraries(${PROJECT_NAME} SDL2::Main SDL2::Image SDL2::TTF SDL2::Mixer) +enable_language(ASM ) + +include(ExternalProject) +ExternalProject_Add(bin2s_git + PREFIX vendor/ + GIT_REPOSITORY https://github.com/Maschell/bin2s + GIT_TAG master + GIT_SUBMODULES + UPDATE_COMMAND "" + PATCH_COMMAND "" + BUILD_COMMAND "" + CMAKE_ARGS + "-DCMAKE_BUILD_TYPE=Release" + "-DCMAKE_INSTALL_PREFIX=" + INSTALL_COMMAND + "${CMAKE_COMMAND}" + --build . + --target install + --config Release) + +add_executable(bin2s IMPORTED GLOBAL) +set_target_properties(bin2s PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/vendor/bin/bin2s) +add_dependencies(bin2s bin2s_git) + +function(add_binfile_library target_name) + if (NOT ${ARGC} GREATER 1) + message(FATAL_ERROR "add_binfile_library : Argument error (no input files)") + endif() + + get_cmake_property(_enabled_languages ENABLED_LANGUAGES) + if (NOT _enabled_languages MATCHES ".*ASM.*") + message(FATAL_ERROR "add_binfile_library : ASM language needs to be enabled") + endif() + + set(_output_dir ${CMAKE_CURRENT_BINARY_DIR}/binfile_asm) + set(_output_file ${_output_dir}/${target_name}.s) + + file(MAKE_DIRECTORY ${_output_dir}) + + add_custom_command(OUTPUT ${_output_file} + COMMAND bin2s -o "${_output_file}" ${ARGN} + DEPENDS ${ARGN} + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}) + + add_library(${target_name} ${_output_file}) +endfunction() + +add_binfile_library(resources data/fonts/FreeSans.ttf + data/sounds/bgMusic.ogg + data/sounds/button_click.mp3 + data/images/button.png + ) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_CMAKE_BUILD_") + +target_link_libraries(${PROJECT_NAME} SDL2::Main SDL2::Image SDL2::TTF SDL2::Mixer resources) diff --git a/README.md b/README.md index 0c692d1..eaca9fd 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,14 @@ # Building +## With CMake +Make sure you have installed `build-essential make git cmake libsdl2-ttf-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-dev`. Then you can do: +``` +# Build the filelist.h +bash ./filelist.sh +# +mkdir build && cd build && cmake .. && make +``` + ## Windows binaries ### Windows with devkitPros version of msys2 diff --git a/filelist.sh b/filelist.sh index 0a3bb17..70d9dc7 100644 --- a/filelist.sh +++ b/filelist.sh @@ -39,12 +39,10 @@ cat < $outFile * NOTE: * Any manual modification of this file will be overwriten by the generation. ****************************************************************************/ -#ifndef _FILELIST_H_ -#define _FILELIST_H_ - +#pragma once #include "Resources.h" - +#ifndef _CMAKE_BUILD_ EOF for i in ${files[@]} @@ -69,7 +67,30 @@ done echo -e '\t{NULL, NULL, 0, NULL, 0}' >> $outFile echo '};' >> $outFile echo '' >> $outFile +echo '#else' >> $outFile +for i in ${files[@]} +do + filename=${i%.*} + extension=${i##*.} + echo 'extern uint8_t '$filename'_'$extension'[];' >> $outFile + echo 'extern uint32_t '$filename'_'$extension'_size;' >> $outFile +done +echo '' >> $outFile + +echo 'static RecourceFile RecourceList[] =' >> $outFile +echo '{' >> $outFile + +for i in ${files[@]} +do + filename=${i%.*} + extension=${i##*.} + echo -e '\t{"'$i'", (uint8_t*)&'$filename'_'$extension', '$filename'_'$extension'_size, NULL, 0},' >> $outFile +done + +echo -e '\t{NULL, NULL, 0, NULL, 0}' >> $outFile +echo '};' >> $outFile +echo '' >> $outFile echo '#endif' >> $outFile fi diff --git a/src/main.cpp b/src/main.cpp index c575c45..73a0ddd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,7 +48,6 @@ bool CheckRunning(){ int main(int argc, char *args[]) { auto *system = new SDLSystem(); - #if defined _WIN32 // Create the Console AllocConsole(); diff --git a/src/menu/MainWindow.cpp b/src/menu/MainWindow.cpp index 1e0798d..2ea0516 100644 --- a/src/menu/MainWindow.cpp +++ b/src/menu/MainWindow.cpp @@ -18,9 +18,6 @@ MainWindow::~MainWindow() { } MainWindow::MainWindow(int32_t w, int32_t h, Renderer* renderer) : GuiFrame(w, h) { -#if defined _WIN32 - Resources::LoadFiles("."); -#endif auto picture_path = "button.png"; auto font_path = "FreeSans.ttf"; auto bgMusic_path = "bgMusic.ogg";