From d683587d58f8793c3fb81a5abfdbd828fa584e12 Mon Sep 17 00:00:00 2001 From: VisualGMQ <2142587070@qq.com> Date: Fri, 4 Feb 2022 14:07:03 +0800 Subject: [PATCH] use strip and upx to compress executable --- .gitignore | 6 ++++++ CMakeLists.txt | 29 ++++++++++++++++++++++++++++- ReadMe.md | 22 ++++++++++++++++++++++ include/tinyengine/engine.hpp | 2 ++ include/tinyengine/pch.hpp | 2 +- include/tinyengine/tool.hpp | 7 +++++++ libs/stb_image/stbi_image.cpp | 1 + pack.sh | 9 +++++++++ src/tinyengine/engine.cpp | 7 +++---- src/tinyengine/renderer.cpp | 8 ++++---- tests/unittest.hpp | 5 +++-- 11 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 .gitignore create mode 100755 pack.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee8655c --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +output +compile_flags.txt +build +xcode-build +.cache +macos diff --git a/CMakeLists.txt b/CMakeLists.txt index 33d2b94..2883eac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ aux_source_directory(libs/stb_image ENGINE_SRC) add_library(${ENGINE_NAME} STATIC ${ENGINE_SRC}) -set(BUILD_SHARED_LIBS CACHE BOOL OFF "Build shared libraries") +set(GLFW_BUILD_SHARED_LIBRARY CACHE BOOL OFF "Build shared libraries") set(GLFW_BUILD_DOCS CACHE BOOL OFF"Build the GLFW documentation") set(GLFW_INSTALL CACHE BOOL OFF "Generate installation target") add_subdirectory(libs/glfw) @@ -42,6 +42,11 @@ target_compile_definitions( PUBLIC $<$,"DEBUG">:GAME_DEBUG> ) +target_compile_options( + ${ENGINE_NAME} + PUBLIC $<$,"RELEASE">:-Os> -fno-rtti -fno-exceptions +) + ################ # space sector ################ @@ -63,8 +68,30 @@ target_compile_features( PUBLIC cxx_std_17 ) +add_custom_target( + CompressExe + COMMAND strip ${PROJECT_NAME} + COMMAND upx --best --ultra-brute ${PROJECT_NAME} + DEPENDS ${PROJECT_NAME} + COMMENT "make SpaceSector more small" + VERBATIM +) + ############# # unit test ############# include(CTest) add_subdirectory(tests) + +############### +# install game +############### +install( + PROGRAMS ${CMAKE_BINARY_DIR}/SpaceSector + DESTINATION ${PROJECT_SOURCE_DIR}/output +) + +install( + DIRECTORY ${PROJECT_SOURCE_DIR}/assets + DESTINATION ${PROJECT_SOURCE_DIR}/output +) diff --git a/ReadMe.md b/ReadMe.md index 3d821df..e4299bc 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -2,3 +2,25 @@ 为1MGames游戏开发大赛制作的游戏。 +## 编译方法 + +使用CMake 3.20及以上进行编译。 + +```bash +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release +cmake --build build +``` + +如果想要压缩文件至1M以下,你需要有`strip`程序和`upx`程序,并执行: + +```bash +cmake --build build --target CompressExe +``` + +编译好后执行pack程序打包: + +```bash +pack.sh +``` + +最终的结果在`output`文件夹下。 diff --git a/include/tinyengine/engine.hpp b/include/tinyengine/engine.hpp index fbc3d1a..ebf6a34 100644 --- a/include/tinyengine/engine.hpp +++ b/include/tinyengine/engine.hpp @@ -4,6 +4,8 @@ #include "renderer.hpp" #include "scence.hpp" #include "inner_bmpfont.hpp" +#include "event.hpp" +#include "tool.hpp" class Engine final { public: diff --git a/include/tinyengine/pch.hpp b/include/tinyengine/pch.hpp index 44e7c06..d07016a 100644 --- a/include/tinyengine/pch.hpp +++ b/include/tinyengine/pch.hpp @@ -4,7 +4,6 @@ #include "GLFW/glfw3.h" #include "stb_image.h" #include -#include #include #include #include @@ -20,6 +19,7 @@ #include #include #include +#include #ifdef USE_GLM #include "glm/glm.hpp" diff --git a/include/tinyengine/tool.hpp b/include/tinyengine/tool.hpp index 9fa13ca..22d567b 100644 --- a/include/tinyengine/tool.hpp +++ b/include/tinyengine/tool.hpp @@ -1,4 +1,5 @@ #pragma once + #include "pch.hpp" template @@ -6,3 +7,9 @@ using Unique = std::unique_ptr; template using Ref = std::shared_ptr; + +#ifdef GAME_DEBUG +#define FATAL_ERROR(msg) assert((void(msg), false)) +#else +#define FATAL_ERROR(msg) +#endif diff --git a/libs/stb_image/stbi_image.cpp b/libs/stb_image/stbi_image.cpp index 8ddfd1f..f81a87a 100644 --- a/libs/stb_image/stbi_image.cpp +++ b/libs/stb_image/stbi_image.cpp @@ -1,2 +1,3 @@ #define STB_IMAGE_IMPLEMENTATION +#define STBI_ONLY_PNG #include "stb_image.h" diff --git a/pack.sh b/pack.sh new file mode 100755 index 0000000..3c962b5 --- /dev/null +++ b/pack.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +if [ -d "./output" ]; then + rm -r ./output +fi + +mkdir output +cp ./build/SpaceSector output +cp -r ./assets output diff --git a/src/tinyengine/engine.cpp b/src/tinyengine/engine.cpp index c082d2b..ee9504f 100644 --- a/src/tinyengine/engine.cpp +++ b/src/tinyengine/engine.cpp @@ -1,5 +1,4 @@ #include "engine.hpp" -#include "event.hpp" void error_callback(int error, const char* description) { fprintf(stderr, "Error: %s\n", description); @@ -7,7 +6,7 @@ void error_callback(int error, const char* description) { void Engine::Init(const std::string& title, const Size& size, Scence* scence) { if (!glfwInit()) { - throw std::runtime_error("glfw init failed"); + FATAL_ERROR("glfw init failed"); } glfwSetErrorCallback(error_callback); @@ -23,7 +22,7 @@ void Engine::Init(const std::string& title, const Size& size, Scence* scence) { if (!window_) { glfwTerminate(); - throw std::runtime_error("glfw window create failed"); + FATAL_ERROR("glfw window create failed"); } glfwMakeContextCurrent(window_); @@ -32,7 +31,7 @@ void Engine::Init(const std::string& title, const Size& size, Scence* scence) { if (!gladLoadGL(glfwGetProcAddress)) { glfwTerminate(); - throw std::runtime_error("glad load failed"); + FATAL_ERROR("glad load failed"); } int width, height; diff --git a/src/tinyengine/renderer.cpp b/src/tinyengine/renderer.cpp index 0e32ee4..69f3814 100644 --- a/src/tinyengine/renderer.cpp +++ b/src/tinyengine/renderer.cpp @@ -59,7 +59,7 @@ void initShader(const std::string& vertex, const std::string& fragment) { std::ifstream file(vertex); if (file.fail()) { Log("vertex shader %s load failed", vertex.c_str()); - throw std::runtime_error("vertex shader load failed"); + FATAL_ERROR("vertex shader load failed"); } std::string vertexCode((std::istreambuf_iterator(file)), std::istreambuf_iterator()); @@ -68,7 +68,7 @@ void initShader(const std::string& vertex, const std::string& fragment) { file.open(fragment); if (file.fail()) { Log("fragment shader %s load failed", fragment.c_str()); - throw std::runtime_error("fragment shader load failed"); + FATAL_ERROR("fragment shader load failed"); } std::string fragmentCode((std::istreambuf_iterator(file)), std::istreambuf_iterator()); @@ -88,8 +88,8 @@ void setShaderOrtho() { void Renderer::Init() { createWhiteTexture(); initRenderContext(); - initShader("./assets/shaders/vertex.shader", - "./assets/shaders/fragment.shader"); + initShader("assets/shaders/vertex.shader", + "assets/shaders/fragment.shader"); setShaderOrtho(); } diff --git a/tests/unittest.hpp b/tests/unittest.hpp index b476a99..cdb49a5 100644 --- a/tests/unittest.hpp +++ b/tests/unittest.hpp @@ -1,18 +1,19 @@ #pragma once #include "tinyengine/libmath.hpp" +#include "tinyengine/tool.hpp" #define TEST_TRUE(condition) do { \ if (!(condition)) { \ std::cout << "[TEST_TRUE] " << #condition << " test failed!" << std::endl; \ - throw std::runtime_error("TEST TRUE failed"); \ + FATAL_ERROR("TEST TRUE failed"); \ } \ } while(0) #define TEST_FALSE(condition) do { \ if (condition) { \ std::cout << "[TEST FALSE] " << #condition << " test failed!" << std::endl; \ - throw std::runtime_error("TEST FALSE failed"); \ + FATAL_ERROR("TEST FALSE failed"); \ } \ } while(0)