diff --git a/CMakeLists.txt b/CMakeLists.txt index e95a01d..3944979 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,6 @@ set(ENGINE_NAME tinyengine) aux_source_directory(src/tinyengine ENGINE_SRC) aux_source_directory(src/component ENGINE_SRC) aux_source_directory(src/tinyengine/ecs ENGINE_SRC) -aux_source_directory(libs/glad/src ENGINE_SRC) aux_source_directory(libs/stb_image ENGINE_SRC) aux_source_directory(libs/miniaudio/ ENGINE_SRC) @@ -25,9 +24,36 @@ add_subdirectory(libs/glfw) target_include_directories( ${ENGINE_NAME} - PUBLIC include libs/glad/include libs/stb_image libs/miniaudio # /usr/local/Cellar/glm/0.9.9.8/include + PUBLIC include libs/stb_image libs/miniaudio ) +option(USE_GLEW "use glew rather than glad" OFF) + +if (USE_GLEW) + message(STATUS "use glew to load opengl functions") + set(GLEW_USE_STATIC_LIBS ON) + find_package(GLEW REQUIRED) + target_link_libraries( + ${ENGINE_NAME} + PUBLIC GLEW::GLEW + ) + target_compile_definitions( + ${ENGINE_NAME} + PUBLIC $<$:TINYENGINE_USE_GLEW> + ) +else() + message(STATUS "use glad to load opengl functions, this only work on MacOS") + target_include_directories( + ${ENGINE_NAME} + PUBLIC libs/glad/include + ) + aux_source_directory(libs/glad/src GLAD_SRC) + target_sources( + ${ENGINE_NAME} + PUBLIC ${GLAD_SRC} + ) +endif() + target_link_libraries( ${ENGINE_NAME} PUBLIC glfw @@ -75,6 +101,22 @@ add_custom_target( VERBATIM ) +add_custom_target( + Pack + COMMAND ${CMAKE_COMMAND} -E remove_directory output + COMMAND ${CMAKE_COMMAND} -E make_directory output + COMMAND ${CMAKE_COMMAND} -E make_directory output/game + COMMAND ${CMAKE_COMMAND} -E copy ./build/${PROJECT_NAME} output/game/${PROJECT_NAME} + COMMAND ${CMAKE_COMMAND} -E copy_directory ./assets output/game/assets + COMMAND ${CMAKE_COMMAND} -E remove_directory ./output/game/assets/test + COMMAND ${CMAKE_COMMAND} -E copy ./HowToPlay.md output/ + COMMAND ${CMAKE_COMMAND} -E copy_directory ./snapshot output/snapshot + DEPENDS ${PROJECT_NAME} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMENT "pack games" + VERBATIM +) + ############# # unit test ############# diff --git a/ReadMe.md b/ReadMe.md index 5d6d223..32b8dd2 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1,29 +1,46 @@ -# SpaceSector +# SpaceWar -为1MGames游戏开发大赛制作的游戏。 +为1MGames游戏开发比赛制作的游戏。 ## 编译方法 -使用CMake 3.20及以上进行编译。目前只能在Mac平台下编译(因为我用的glad是Mac专属,你也可以将你平台的glad替换`libs/glad`文件夹来编译,glad指定OpenGL3.3 Core版本) +编译平台为MacOS(在MacOS Big Sur 11.6中编译成功且结果在1M以下),也可以使用`glew`或平台相关`glad`在其他平台编译,编译结果**不保证在1M以下**,仅仅是为了方便不同平台进行编译。 + +使用CMake 3.20及以上进行编译。 ```bash cmake -S . -B build -DCMAKE_BUILD_TYPE=Release cmake --build build ``` +如果想要使用`glew`,可以通过以下命令: + +```bash +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DUSE_GLEW=ON +cmake --build build +``` + +cmake会在你的电脑上寻找`glew`。 + +也可以将你平台的glad替换`libs/glad`文件夹来使用`glad`编译,glad请指定OpenGL3.3 Core版本。 + 如果想要压缩文件至1M以下,你需要有`strip`程序和`upx`程序,并执行: ```bash cmake --build build --target CompressExe ``` -编译好后执行pack程序打包: +编译好后执行pack命令打包 ```bash -pack.sh +cmakd --build build --target Pack ``` -最终的结果在`output`文件夹下。 +最终的结果在`output`文件夹下: + +* game:包含了游戏本体 +* snapshot:包含了游戏的截图 +* HowToPlay.md:游戏说明 ## 游戏截图 @@ -33,6 +50,8 @@ pack.sh ![gaming](./snapshot/gaming.png) +![gaming2](./snapshot/gaming2.png) + ## 游戏操作 [游戏操作](./HowToPlay.md) diff --git a/include/game/component.hpp b/include/game/component.hpp index 01024a6..74dda49 100644 --- a/include/game/component.hpp +++ b/include/game/component.hpp @@ -63,8 +63,6 @@ public: float liveTime; }; - - class SpaceshipWeaponCmpt: public Component { public: enum Type { diff --git a/include/tinyengine/pch.hpp b/include/tinyengine/pch.hpp index 29f5a2e..79700d0 100644 --- a/include/tinyengine/pch.hpp +++ b/include/tinyengine/pch.hpp @@ -1,6 +1,11 @@ #pragma once -#include "glad/gl.h" +#ifdef TINYENGINE_USE_GLEW + #include "GL/glew.h" +#else + #include "glad/gl.h" +#endif + #include "GLFW/glfw3.h" #include diff --git a/license/glfw-license.txt b/licenses/glfw-license.txt similarity index 100% rename from license/glfw-license.txt rename to licenses/glfw-license.txt diff --git a/license/miniaudio-license.txt b/licenses/miniaudio-license.txt similarity index 100% rename from license/miniaudio-license.txt rename to licenses/miniaudio-license.txt diff --git a/license/stb_image-license.txt b/licenses/stb_image-license.txt similarity index 100% rename from license/stb_image-license.txt rename to licenses/stb_image-license.txt diff --git a/pack.sh b/pack.sh deleted file mode 100755 index 903c3fc..0000000 --- a/pack.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -if [ -d "./output" ]; then - rm -r ./output -fi - -mkdir output -cp ./build/SpaceWar output -cp -r ./assets output -rm -rf ./output/assets/test -cp ./HowToPlay.md output/ -cp -r ./snapshot output diff --git a/src/game/stages/space.cpp b/src/game/stages/space.cpp index 229ca32..6b6b7e8 100644 --- a/src/game/stages/space.cpp +++ b/src/game/stages/space.cpp @@ -4,6 +4,11 @@ void SpaceScence::OnInit() { Renderer::SetClearColor(Color{0, 0, 0, 255}); + Log("Entities size = %ld", Entities.Size()); + Log("Bullets size = %ld", Bullets.Size()); + for (int i = 0; i < 4; i++) { + Log("Group %d size = %ld", i, Groups[i].Size()); + } mode_ = Gaming; @@ -189,7 +194,7 @@ void SpaceScence::renderGUI() { Renderer::SetCamera(guiCamera_); int life = 0; - if (PlayerSpaceship && PlayerSpaceship->IsAlive()) { + if (PlayerSpaceship && PlayerSpaceship->IsAlive() && PlayerSpaceship->Has()) { life = PlayerSpaceship->Get()->hp; } float xOffset = 16 * life; @@ -203,8 +208,10 @@ void SpaceScence::renderGUI() { if (PlayerSpaceship->Has()) { renderWeapons(PlayerSpaceship->Get()->weapon, nullptr); } else { - auto fightShip = PlayerSpaceship->Get(); - renderWeapons(fightShip->weapon1, fightShip->weapon2); + if (PlayerSpaceship->Has()) { + auto fightShip = PlayerSpaceship->Get(); + renderWeapons(fightShip->weapon1, fightShip->weapon2); + } } } @@ -246,6 +253,7 @@ void SpaceScence::renderMiniMap() { for (auto& entity: Entities) { if (entity != PlayerSpaceship) { + if (!entity->Has()) continue; const auto& pos = entity->Get()->position; Point entityOnMapPos = (pos - PlayerSpaceship->Get()->position) * Size{mapRect.w, mapRect.h} / (GameWindowSize * 8) + Point{mapRect.x, mapRect.y} + Size{mapRect.w, mapRect.h} / 2; @@ -257,6 +265,9 @@ void SpaceScence::renderMiniMap() { } } } + Renderer::SetDrawColor(Color{1, 1, 1, 1}); + Renderer::FillRect(Rect{mapRect.x + mapRect.w / 2, mapRect.y + mapRect.h / 2, + 2, 2}); } void SpaceScence::renderWeapons(SpaceshipWeaponCmpt* weapon1, SpaceshipWeaponCmpt* weapon2) { diff --git a/src/tinyengine/engine.cpp b/src/tinyengine/engine.cpp index 54b86f6..c647d42 100644 --- a/src/tinyengine/engine.cpp +++ b/src/tinyengine/engine.cpp @@ -31,10 +31,17 @@ void Engine::Init(const std::string& title, const Size& size, Scence* scence) { glfwSetFramebufferSizeCallback(window_, OnWindowResize); glfwSetMouseButtonCallback(window_, MouseBtnCallback); +#ifdef TINYENGINE_USE_GLEW + if (GLEW_OK != glewInit()) { + glfwTerminate(); + FATAL_ERROR("glew init failed"); + } +#else if (!gladLoadGL(glfwGetProcAddress)) { glfwTerminate(); FATAL_ERROR("glad load failed"); } +#endif int width, height; glfwGetFramebufferSize(GetWindow(), &width, &height);