use strip and upx to compress executable

This commit is contained in:
VisualGMQ 2022-02-04 14:07:03 +08:00
parent b6c4d6bf47
commit d683587d58
11 changed files with 86 additions and 12 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
output
compile_flags.txt
build
xcode-build
.cache
macos

View File

@ -16,7 +16,7 @@ aux_source_directory(libs/stb_image ENGINE_SRC)
add_library(${ENGINE_NAME} STATIC ${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_BUILD_DOCS CACHE BOOL OFF"Build the GLFW documentation")
set(GLFW_INSTALL CACHE BOOL OFF "Generate installation target") set(GLFW_INSTALL CACHE BOOL OFF "Generate installation target")
add_subdirectory(libs/glfw) add_subdirectory(libs/glfw)
@ -42,6 +42,11 @@ target_compile_definitions(
PUBLIC $<$<STREQUAL:$<UPPER_CASE:"${CMAKE_BUILD_TYPE}">,"DEBUG">:GAME_DEBUG> PUBLIC $<$<STREQUAL:$<UPPER_CASE:"${CMAKE_BUILD_TYPE}">,"DEBUG">:GAME_DEBUG>
) )
target_compile_options(
${ENGINE_NAME}
PUBLIC $<$<STREQUAL:$<UPPER_CASE:"${CMAKE_BUILD_TYPE}">,"RELEASE">:-Os> -fno-rtti -fno-exceptions
)
################ ################
# space sector # space sector
################ ################
@ -63,8 +68,30 @@ target_compile_features(
PUBLIC cxx_std_17 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 # unit test
############# #############
include(CTest) include(CTest)
add_subdirectory(tests) 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
)

View File

@ -2,3 +2,25 @@
为1MGames游戏开发大赛制作的游戏。 为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`文件夹下。

View File

@ -4,6 +4,8 @@
#include "renderer.hpp" #include "renderer.hpp"
#include "scence.hpp" #include "scence.hpp"
#include "inner_bmpfont.hpp" #include "inner_bmpfont.hpp"
#include "event.hpp"
#include "tool.hpp"
class Engine final { class Engine final {
public: public:

View File

@ -4,7 +4,6 @@
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include "stb_image.h" #include "stb_image.h"
#include <string> #include <string>
#include <exception>
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
@ -20,6 +19,7 @@
#include <stack> #include <stack>
#include <utility> #include <utility>
#include <limits> #include <limits>
#include <cassert>
#ifdef USE_GLM #ifdef USE_GLM
#include "glm/glm.hpp" #include "glm/glm.hpp"

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "pch.hpp" #include "pch.hpp"
template <typename T> template <typename T>
@ -6,3 +7,9 @@ using Unique = std::unique_ptr<T>;
template <typename T> template <typename T>
using Ref = std::shared_ptr<T>; using Ref = std::shared_ptr<T>;
#ifdef GAME_DEBUG
#define FATAL_ERROR(msg) assert((void(msg), false))
#else
#define FATAL_ERROR(msg)
#endif

View File

@ -1,2 +1,3 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG
#include "stb_image.h" #include "stb_image.h"

9
pack.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/bash
if [ -d "./output" ]; then
rm -r ./output
fi
mkdir output
cp ./build/SpaceSector output
cp -r ./assets output

View File

@ -1,5 +1,4 @@
#include "engine.hpp" #include "engine.hpp"
#include "event.hpp"
void error_callback(int error, const char* description) { void error_callback(int error, const char* description) {
fprintf(stderr, "Error: %s\n", 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) { void Engine::Init(const std::string& title, const Size& size, Scence* scence) {
if (!glfwInit()) { if (!glfwInit()) {
throw std::runtime_error("glfw init failed"); FATAL_ERROR("glfw init failed");
} }
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
@ -23,7 +22,7 @@ void Engine::Init(const std::string& title, const Size& size, Scence* scence) {
if (!window_) { if (!window_) {
glfwTerminate(); glfwTerminate();
throw std::runtime_error("glfw window create failed"); FATAL_ERROR("glfw window create failed");
} }
glfwMakeContextCurrent(window_); glfwMakeContextCurrent(window_);
@ -32,7 +31,7 @@ void Engine::Init(const std::string& title, const Size& size, Scence* scence) {
if (!gladLoadGL(glfwGetProcAddress)) { if (!gladLoadGL(glfwGetProcAddress)) {
glfwTerminate(); glfwTerminate();
throw std::runtime_error("glad load failed"); FATAL_ERROR("glad load failed");
} }
int width, height; int width, height;

View File

@ -59,7 +59,7 @@ void initShader(const std::string& vertex, const std::string& fragment) {
std::ifstream file(vertex); std::ifstream file(vertex);
if (file.fail()) { if (file.fail()) {
Log("vertex shader %s load failed", vertex.c_str()); 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<char>(file)), std::string vertexCode((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()); std::istreambuf_iterator<char>());
@ -68,7 +68,7 @@ void initShader(const std::string& vertex, const std::string& fragment) {
file.open(fragment); file.open(fragment);
if (file.fail()) { if (file.fail()) {
Log("fragment shader %s load failed", fragment.c_str()); 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<char>(file)), std::string fragmentCode((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()); std::istreambuf_iterator<char>());
@ -88,8 +88,8 @@ void setShaderOrtho() {
void Renderer::Init() { void Renderer::Init() {
createWhiteTexture(); createWhiteTexture();
initRenderContext(); initRenderContext();
initShader("./assets/shaders/vertex.shader", initShader("assets/shaders/vertex.shader",
"./assets/shaders/fragment.shader"); "assets/shaders/fragment.shader");
setShaderOrtho(); setShaderOrtho();
} }

View File

@ -1,18 +1,19 @@
#pragma once #pragma once
#include "tinyengine/libmath.hpp" #include "tinyengine/libmath.hpp"
#include "tinyengine/tool.hpp"
#define TEST_TRUE(condition) do { \ #define TEST_TRUE(condition) do { \
if (!(condition)) { \ if (!(condition)) { \
std::cout << "[TEST_TRUE] " << #condition << " test failed!" << std::endl; \ std::cout << "[TEST_TRUE] " << #condition << " test failed!" << std::endl; \
throw std::runtime_error("TEST TRUE failed"); \ FATAL_ERROR("TEST TRUE failed"); \
} \ } \
} while(0) } while(0)
#define TEST_FALSE(condition) do { \ #define TEST_FALSE(condition) do { \
if (condition) { \ if (condition) { \
std::cout << "[TEST FALSE] " << #condition << " test failed!" << std::endl; \ std::cout << "[TEST FALSE] " << #condition << " test failed!" << std::endl; \
throw std::runtime_error("TEST FALSE failed"); \ FATAL_ERROR("TEST FALSE failed"); \
} \ } \
} while(0) } while(0)