add glew support; fixed cmake to pack game

This commit is contained in:
VisualGMQ 2022-02-18 16:33:24 +08:00
parent fe12fb4c70
commit e3278bea9b
10 changed files with 96 additions and 26 deletions

View File

@ -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 $<$<BOOL:"${USE_GLEW}">: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
#############

View File

@ -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)

View File

@ -63,8 +63,6 @@ public:
float liveTime;
};
class SpaceshipWeaponCmpt: public Component {
public:
enum Type {

View File

@ -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 <string>

12
pack.sh
View File

@ -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

View File

@ -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<LifeCmpt>()) {
life = PlayerSpaceship->Get<LifeCmpt>()->hp;
}
float xOffset = 16 * life;
@ -203,8 +208,10 @@ void SpaceScence::renderGUI() {
if (PlayerSpaceship->Has<FreightShipCmpt>()) {
renderWeapons(PlayerSpaceship->Get<FreightShipCmpt>()->weapon, nullptr);
} else {
auto fightShip = PlayerSpaceship->Get<FightShipCmpt>();
renderWeapons(fightShip->weapon1, fightShip->weapon2);
if (PlayerSpaceship->Has<FightShipCmpt>()) {
auto fightShip = PlayerSpaceship->Get<FightShipCmpt>();
renderWeapons(fightShip->weapon1, fightShip->weapon2);
}
}
}
@ -246,6 +253,7 @@ void SpaceScence::renderMiniMap() {
for (auto& entity: Entities) {
if (entity != PlayerSpaceship) {
if (!entity->Has<MoveCmpt>()) continue;
const auto& pos = entity->Get<MoveCmpt>()->position;
Point entityOnMapPos = (pos - PlayerSpaceship->Get<MoveCmpt>()->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) {

View File

@ -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);