~leinnan/raylibGame unlisted

b27545fab0fb010f22cd3278d07cf4438110fedf — Piotr 4 months ago 949947e
Code refactor
5 files changed, 137 insertions(+), 111 deletions(-)

M CMakeLists.txt
M ext/raylib
A src/game/BaseGame.cpp
A src/game/BaseGame.hpp
M src/main.cpp
M CMakeLists.txt => CMakeLists.txt +3 -9
@@ 18,12 18,6 @@ SET(BUILD_GAMES OFF CACHE BOOL "Build the example games.")
SET(SPDLOG_BUILD_EXAMPLES OFF CACHE BOOL "Build examples")
add_subdirectory(ext/raylib)
add_subdirectory(ext/entt)
#add_subdirectory(ext/spdlog)
#
#if(NOT TARGET spdlog)
#    # Stand-alone build
#   find_package(spdlog REQUIRED)
#endif()

if(${PLATFORM} MATCHES "Web")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 --profiling")


@@ 34,9 28,9 @@ file(COPY ${CMAKE_SOURCE_DIR}/data DESTINATION ${CMAKE_BINARY_DIR}/output)
file(MAKE_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/logs")

include_directories(src ext src/controllers src/components src/systems src/utils src/presets src/factories)
add_executable(${PROJECT_NAME} src/main.cpp src/utils/Arkona.cpp src/utils/JsonParser.cpp  src/systems/SpawnSystem.cpp src/systems/DamageSystem.cpp src/systems/MissileSystem.cpp src/systems/AiSystem.cpp src/systems/CollisionSystem.cpp src/systems/DestroySystem.cpp src/systems/MovementSystem.cpp src/systems/RenderSystem.cpp src/systems/PlayerInputSystem.cpp src/components/GameRestart.hpp src/components/ModelShader.hpp src/systems/RenderLightingSystem.cpp src/systems/RenderLightingSystem.hpp src/components/LightComponent.hpp src/utils/loguru.cpp)
#set(raylib_VERBOSE 1)
target_link_libraries(${PROJECT_NAME} raylib EnTT)# spdlog::spdlog_header_only)
add_executable(${PROJECT_NAME} src/main.cpp src/utils/Arkona.cpp src/utils/JsonParser.cpp  src/systems/SpawnSystem.cpp src/systems/DamageSystem.cpp src/systems/MissileSystem.cpp src/systems/AiSystem.cpp src/systems/CollisionSystem.cpp src/systems/DestroySystem.cpp src/systems/MovementSystem.cpp src/systems/RenderSystem.cpp src/systems/PlayerInputSystem.cpp src/components/GameRestart.hpp src/components/ModelShader.hpp src/systems/RenderLightingSystem.cpp src/systems/RenderLightingSystem.hpp src/components/LightComponent.hpp src/utils/loguru.cpp src/game/BaseGame.cpp src/game/BaseGame.hpp)

target_link_libraries(${PROJECT_NAME} raylib EnTT)
if(${PLATFORM} MATCHES "Web")
    set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-s DEMANGLE_SUPPORT=1 --preload-file ${CMAKE_SOURCE_DIR}/data --bind")
endif()

M ext/raylib => ext/raylib +1 -1
@@ 1,1 1,1 @@
Subproject commit 00431396b0b516b2296c17e5e5b5fd9e72bacfec
Subproject commit 7ef114d1da2c34a70bba5442497103441647d8f3

A src/game/BaseGame.cpp => src/game/BaseGame.cpp +71 -0
@@ 0,0 1,71 @@
//
// Created by MevLyshkin on 06.06.2020.
//

#include "BaseGame.hpp"
namespace Game {
BaseGame::BaseGame(const INIReader &reader) {
    const auto windowName = reader.Get("game", "name", "Raylib");
    jsonFile = reader.Get("game", "firstJson", "data.json");
    unitsFile = reader.Get("game", "units", "units.json");

    int flags = FLAG_WINDOW_RESIZABLE;
    if(reader.GetBoolean("gfx", "fullscreen", false))
        flags |= FLAG_FULLSCREEN_MODE;
    if(reader.GetBoolean("gfx", "msaa", false))
        flags |= FLAG_MSAA_4X_HINT;

    SetConfigFlags(flags);
    InitWindow(reader.GetInteger("gfx", "width", SCREEN_WIDTH), reader.GetInteger("gfx", "height", SCREEN_HEIGHT),
               windowName.c_str());
    renderLightingSystem.Init(registry);
    renderSystem.Init(registry);
    collisionSystem.Init(registry);
    aiSystem.Init(registry);
    damageSystem.Init();

#if !defined(PLATFORM_WEB)
    SetTargetFPS(60);
#endif
}
void BaseGame::UpdateDrawFrame(void) {
    spawnSystem.Update(registry);
    destroySystem.Update(registry);
    damageSystem.Update(registry, GetFrameTime());
    playerInputSystem.Update(registry, camera);
    missileSystem.Update(registry);
    aiSystem.Update(registry, GetFrameTime());
    movementSystem.Update(registry);
    collisionSystem.Update(registry);
    renderLightingSystem.Update(registry, camera);
    renderSystem.Update(registry, camera);
    bool restartRequired = false;
    registry.view<Components::GameRestart>().each([&](const auto, const auto &component) { restartRequired = true; });
    if(restartRequired) {
        startGame();
    }
}
void BaseGame::startGame() {
    LOG_F(INFO, "Loading files");
    Factories::UnitFactory::getInstance().loadFromFile(unitsFile.c_str());
    registry.reset();
    resetCamera();
    Components::readFromFile(registry, jsonFile.c_str());
    LOG_F(INFO, "Loading files ended");
}
void BaseGame::resetCamera() {
    camera = {0};
    camera.target = {0.0f, 0.5f, 0.0f};
    camera.up = {0.0f, 1.0f, 0.0f};
    camera.fovy = 20.0f;
    camera.type = CAMERA_ORTHOGRAPHIC;
    camera.position = {0.0f, 0.f, 0.0f};
}

bool BaseGame::shouldClose() { return WindowShouldClose(); }

void BaseGame::close() {
    registry.reset();
    CloseWindow();
}
} // namespace Game
\ No newline at end of file

A src/game/BaseGame.hpp => src/game/BaseGame.hpp +55 -0
@@ 0,0 1,55 @@
//
// Created by MevLyshkin on 06.06.2020.
//

#ifndef RAYLIBECS_BASEGAME_HPP
#define RAYLIBECS_BASEGAME_HPP
#include "INIReader.hpp"
#include "components/GameRestart.hpp"
#include "factories/UnitFactory.hpp"
#include "raylib.h"
#include "systems/AiSystem.hpp"
#include "systems/CollisionSystem.hpp"
#include "systems/DamageSystem.hpp"
#include "systems/DestroySystem.hpp"
#include "systems/MissileSystem.hpp"
#include "systems/MovementSystem.hpp"
#include "systems/PlayerInputSystem.hpp"
#include "systems/RenderLightingSystem.hpp"
#include "systems/RenderSystem.hpp"
#include "systems/SpawnSystem.hpp"
#include "utils/JsonParser.hpp"
#include "utils/loguru.hpp"
#include <entt/entt.hpp>
#include <iostream>
#include <string>
namespace Game {
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 480;
class BaseGame {
  public:
    BaseGame(const INIReader &reader);
    void startGame();
    void UpdateDrawFrame(void);
    void resetCamera();
    bool shouldClose();
    void close();

  private:
    Systems::RenderLightingSystem renderLightingSystem;
    Systems::RenderSystem renderSystem;
    Systems::SpawnSystem spawnSystem;
    Systems::PlayerInputSystem playerInputSystem;
    Systems::CollisionSystem collisionSystem;
    Systems::MovementSystem movementSystem;
    Systems::DestroySystem destroySystem;
    Systems::AiSystem aiSystem;
    Systems::MissileSystem missileSystem;
    Systems::DamageSystem damageSystem;
    entt::registry<> registry;
    Camera camera;
    std::string jsonFile;
    std::string unitsFile;
};
} // namespace Game
#endif // RAYLIBECS_BASEGAME_HPP

M src/main.cpp => src/main.cpp +7 -101
@@ 1,66 1,12 @@
#include "raylib.h"

#include "INIReader.hpp"
#include "components/GameRestart.hpp"
#include "factories/UnitFactory.hpp"
#include "systems/AiSystem.hpp"
#include "systems/CollisionSystem.hpp"
#include "systems/DamageSystem.hpp"
#include "systems/DestroySystem.hpp"
#include "systems/MissileSystem.hpp"
#include "systems/MovementSystem.hpp"
#include "systems/PlayerInputSystem.hpp"
#include "systems/RenderLightingSystem.hpp"
#include "systems/RenderSystem.hpp"
#include "systems/SpawnSystem.hpp"
#include "utils/JsonParser.hpp"
#include "game/BaseGame.hpp"
#include "utils/loguru.hpp"
#include <entt/entt.hpp>
#include <iostream>
#include <string>
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif

const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 480;
const char *CONFIG_PATH = "data/mod.ini";

void UpdateDrawFrame(void);

Systems::RenderLightingSystem renderLightingSystem;
Systems::RenderSystem renderSystem;
Systems::SpawnSystem spawnSystem;
Systems::PlayerInputSystem playerInputSystem;
Systems::CollisionSystem collisionSystem;
Systems::MovementSystem movementSystem;
Systems::DestroySystem destroySystem;
Systems::AiSystem aiSystem;
Systems::MissileSystem missileSystem;
Systems::DamageSystem damageSystem;
entt::registry<> registry;
Camera camera;
std::string jsonFile;
std::string unitsFile;

void resetCamera() {
    camera = {0};
    camera.target = {0.0f, 0.5f, 0.0f};
    camera.up = {0.0f, 1.0f, 0.0f};
    camera.fovy = 20.0f;
    camera.type = CAMERA_ORTHOGRAPHIC;
    camera.position = {0.0f, 0.f, 0.0f};
}

void restartGame() {
    LOG_F(INFO,"Loading files");
    Factories::UnitFactory::getInstance().loadFromFile(unitsFile.c_str());
    registry.reset();
    resetCamera();
    Components::readFromFile(registry, jsonFile.c_str());
    LOG_F(INFO,"Loading files ended");
}

int main(int argc, char **argv) {
    loguru::init(argc, argv);
    loguru::add_file("logs/everything.log", loguru::Append, loguru::Verbosity_MAX);


@@ 71,60 17,20 @@ int main(int argc, char **argv) {
        LOG_F(ERROR,"Some error message with arg: {}", argc == 2 ? argv[1] : CONFIG_PATH);
        return 1;
    }
    const auto windowName = reader.Get("game", "name", "Raylib");
    jsonFile = reader.Get("game", "firstJson", "data.json");
    unitsFile = reader.Get("game", "units", "units.json");
    Game::BaseGame game(reader);

    int flags = FLAG_WINDOW_RESIZABLE;
    if(reader.GetBoolean("gfx", "fullscreen", false))
        flags |= FLAG_FULLSCREEN_MODE;
    if(reader.GetBoolean("gfx", "msaa", false))
        flags |= FLAG_MSAA_4X_HINT;

    SetConfigFlags(flags);
    InitWindow(reader.GetInteger("gfx", "width", SCREEN_WIDTH), reader.GetInteger("gfx", "height", SCREEN_HEIGHT),
               windowName.c_str());
    renderLightingSystem.Init(registry);
    renderSystem.Init(registry);
    collisionSystem.Init(registry);
    aiSystem.Init(registry);
    damageSystem.Init();
    restartGame();
    game.startGame();

#if defined(PLATFORM_WEB)
    // TODO https://stackoverflow.com/questions/55415179/unable-to-pass-a-proper-lambda-to-emscripten-set-main-loop
    emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
    SetTargetFPS(60); // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while(!WindowShouldClose()) // Detect window close button or ESC key
    while(!game.shouldClose()) // Detect window close button or ESC key
    {
        UpdateDrawFrame();
        game.UpdateDrawFrame();
    }
#endif

    CloseWindow();

    game.close();
    return 0;
}

//----------------------------------------------------------------------------------
void UpdateDrawFrame(void) {

    spawnSystem.Update(registry);
    destroySystem.Update(registry);
    damageSystem.Update(registry, GetFrameTime());
    playerInputSystem.Update(registry, camera);
    missileSystem.Update(registry);
    aiSystem.Update(registry, GetFrameTime());
    movementSystem.Update(registry);
    collisionSystem.Update(registry);
    renderLightingSystem.Update(registry, camera);
    renderSystem.Update(registry, camera);
    bool restartRequired = false;
    registry.view<Components::GameRestart>().each([&](const auto, const auto &component) { restartRequired = true; });
    if(restartRequired) {
        restartGame();
    }
}