~leinnan/raylibGame unlisted

15ab87e11f1df2860b973ca250f75f9dad13165c — Piotr Siuszko 4 months ago 5bf527b
Small refactor
9 files changed, 103 insertions(+), 40 deletions(-)

M CMakeLists.txt
M data/test.json
R src/components/{ModelLightsShader.hpp => ModelShader.hpp}
M src/main.cpp
M src/systems/RenderLightingSystem.cpp
M src/systems/RenderLightingSystem.hpp
M src/systems/RenderSystem.cpp
M src/utils/JsonParser.cpp
M src/utils/JsonParser.hpp
M CMakeLists.txt => CMakeLists.txt +1 -1
@@ 31,7 31,7 @@ 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/ModelLightsShader.hpp src/systems/RenderLightingSystem.cpp src/systems/RenderLightingSystem.hpp src/components/LightComponent.hpp)
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)
#set(raylib_VERBOSE 1)
target_link_libraries(${PROJECT_NAME} raylib EnTT) #spdlog::spdlog_header_only)
if(${PLATFORM} MATCHES "Web")

M data/test.json => data/test.json +39 -0
@@ 1,6 1,13 @@
[
    [
        {
            "type": "ModelShader",
            "fragmentShader": "data/shaders/lighting.fs",
            "vertexShader": "data/shaders/base_lighting.vs"
        }
    ],
    [
        {
            "type": "RectangleVeil"
        }
    ],


@@ 150,6 157,38 @@
    ],
    [
        {
            "colorR": 155,
            "colorG": 222,
            "colorB": 222,
            "strength": 1.0,
            "type": "Light"
        },
        {
            "angle": 10.0,
            "posX": -15.0,
            "posY": 15.0,
            "posZ": -10.0,
            "type": "Transform"
        }
    ],
    [
        {
            "colorR": 255,
            "colorG": 255,
            "colorB": 255,
            "strength": 1.0,
            "type": "Light"
        },
        {
            "angle": 0.0,
            "posX": 4.0,
            "posY": 2.0,
            "posZ": 15.0,
            "type": "Transform"
        }
    ],
    [
        {
            "diffusePath": "data/gfx/rocks.dds",
            "modelPath": "data/gfx/rocks.obj",
            "scale": 4.0,

R src/components/ModelLightsShader.hpp => src/components/ModelShader.hpp +6 -3
@@ 2,9 2,12 @@
#include "raylib.h"

namespace Components {
    struct ModelLightsShader {
        ModelLightsShader(){}
        ModelLightsShader(const Shader &shader) : shader(shader){};
    struct ModelShader {
        ModelShader(){}
        ModelShader(const Shader &shader) : shader(shader){}
        ModelShader(const std::string &fs, const std::string &vs) : fs(fs), vs(vs){};
        Shader shader;
        std::string fs;
        std::string vs;
    };
}
\ No newline at end of file

M src/main.cpp => src/main.cpp +0 -1
@@ 53,7 53,6 @@ void resetCamera() {
void restartGame() {
    registry.reset();
    resetCamera();
    renderLightingSystem.Prepare(registry);
    Components::readFromFile(registry, jsonFile.c_str());
}


M src/systems/RenderLightingSystem.cpp => src/systems/RenderLightingSystem.cpp +9 -25
@@ 3,25 3,25 @@
//
#include "RenderLightingSystem.hpp"
#include "components/LightComponent.hpp"
#include "components/ModelLightsShader.hpp"
#include "components/ModelShader.hpp"
#include "components/Transform.hpp"
#include "utils/lightArkona.h"
namespace Systems {
void RenderLightingSystem::Init(entt::registry<> &registry) {
    registry.construction<Components::ModelLightsShader>().connect<&RenderLightingSystem::LoadLightingShader>(this);
    registry.destruction<Components::ModelLightsShader>().connect<&RenderLightingSystem::UnloadLightingShader>(this);
    registry.construction<Components::ModelShader>().connect<&RenderLightingSystem::LoadLightingShader>(this);
    registry.destruction<Components::ModelShader>().connect<&RenderLightingSystem::UnloadLightingShader>(this);
}

void RenderLightingSystem::Update(entt::registry<> &registry, Camera &camera) {
    const float cameraPos[3] = {camera.position.x, camera.position.y, camera.position.z};
    int entityNr = -1;
    registry.view<Components::ModelLightsShader>().each([&](const auto entity, const auto &shader) {
    registry.view<Components::ModelShader>().each([&](const auto entity, const auto &shader) {
        entityNr = entity;
    });
    if(entityNr < 0) {
        return;
    }
    auto shader = registry.get<Components::ModelLightsShader>(entityNr);
    auto shader = registry.get<Components::ModelShader>(entityNr);
    const auto vectorViewLoc = shader.shader.locs[LOC_VECTOR_VIEW];
    SetShaderValue(shader.shader, vectorViewLoc, cameraPos, UNIFORM_VEC3);
    int counter = 0;


@@ 32,37 32,21 @@ void RenderLightingSystem::Update(entt::registry<> &registry, Camera &camera) {
            counter++;
        });
}
void RenderLightingSystem::Prepare(entt::registry<> &registry) {
    {
        auto id = registry.create();
        registry.assign<Components::ModelLightsShader>(id);
    }
    auto createLight = [&](Vector3 pos, Color color, float strength){
        auto id = registry.create();
        Components::Light ligth(color,strength);
        registry.assign<Components::Light>(id,ligth);
        registry.assign<Components::Transform>(id,Components::Transform(pos,0.0f));
    };

    createLight({ 4, 2, 15 }, WHITE, 1.f);
    createLight({ 4, 2, 4 }, RED, 1.f);
    createLight({ 0, 4, 2 }, GREEN, 1.f);
    createLight({ 0, 4, 2 }, BLUE, 1.f);
}

void RenderLightingSystem::LoadLightingShader(entt::registry<> &registry, std::uint32_t entity) {
    auto newShader = LoadShader("data/shaders/base_lighting.vs", "data/shaders/lighting.fs");
    auto object = registry.get<Components::ModelShader>(entity);
    auto newShader = LoadShader(object.vs.c_str(), object.fs.c_str());
    newShader.locs[LOC_MATRIX_MODEL] = GetShaderLocation(newShader, "matModel");
    newShader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(newShader, "viewPos");
    const int ambientLoc = GetShaderLocation(newShader, "ambient");
    const float values[4]{0.2f, 0.2f, 0.2f, 1.0f};

    SetShaderValue(newShader, ambientLoc, values, UNIFORM_VEC4);
    registry.assign_or_replace<Components::ModelLightsShader>(entity, newShader);
    registry.assign_or_replace<Components::ModelShader>(entity, newShader);
}

void RenderLightingSystem::UnloadLightingShader(entt::registry<> &registry, std::uint32_t entity) {
    auto object = registry.get<Components::ModelLightsShader>(entity);
    auto object = registry.get<Components::ModelShader>(entity);
    UnloadShader(object.shader);
}
} // namespace Systems
\ No newline at end of file

M src/systems/RenderLightingSystem.hpp => src/systems/RenderLightingSystem.hpp +0 -1
@@ 11,7 11,6 @@ namespace Systems {
class RenderLightingSystem {
  public:
    void Init(entt::registry<> &registry);
    void Prepare(entt::registry<> &registry);
    void Update(entt::registry<> &registry, Camera &camera);

  private:

M src/systems/RenderSystem.cpp => src/systems/RenderSystem.cpp +3 -3
@@ 6,7 6,7 @@
#include "components/CylinderRenderer.hpp"
#include "components/DamageBox.hpp"
#include "components/MeshRenderer.hpp"
#include "components/ModelLightsShader.hpp"
#include "components/ModelShader.hpp"
#include "components/PlayerInput.hpp"
#include "components/RectangleVeil.hpp"
#include "components/Transform.hpp"


@@ 28,7 28,7 @@ void RenderSystem::Update(entt::registry<> &registry, Camera &camera) {
    const float h = GetScreenHeight();

    Shader modelLightsShader;
    registry.view<Components::ModelLightsShader>().each(
    registry.view<Components::ModelShader>().each(
        [&](const auto entity, const auto &shader) { modelLightsShader = shader.shader; });
    auto target = LoadRenderTexture(w, h);
    auto fxShader = LoadShader(0, "data/shaders/pixelizer.fs");


@@ 127,7 127,7 @@ void RenderSystem::LoadEntityTexture(entt::registry<> &registry, std::uint32_t e
            if(mesh.model.materialCount > 0) {
                mesh.model.materials[0].maps[MAP_DIFFUSE].texture = mesh.diffuse;
                Shader modelLightsShader;
                registry.view<Components::ModelLightsShader>().each(
                registry.view<Components::ModelShader>().each(
                    [&](const auto entity, const auto &shader) { modelLightsShader = shader.shader; });
                mesh.model.materials[0].shader = modelLightsShader;
            }

M src/utils/JsonParser.cpp => src/utils/JsonParser.cpp +37 -6
@@ 250,6 250,16 @@ void Components::parseToFile(entt::registry<> &registry, const char *filename) {
            Components::to_json(component, registry.get<Components::RectangleVeil>(entity));
            entityObject.push_back(component);
        }
        if(registry.has<Components::Light>(entity)) {
            json component;
            Components::to_json(component, registry.get<Components::Light>(entity));
            entityObject.push_back(component);
        }
        if(registry.has<Components::ModelShader>(entity)) {
            json component;
            Components::to_json(component, registry.get<Components::ModelShader>(entity));
            entityObject.push_back(component);
        }
        jsonReader.push_back(entityObject);
    });



@@ 314,6 324,10 @@ uint32_t Components::createEntityFromJson(entt::registry<> &registry, json &obje
            registry.assign<Components::SpawnPoint>(entity, component.get<Components::SpawnPoint>());
        } else if(type.compare("RectangleVeil") == 0) {
            registry.assign<Components::RectangleVeil>(entity, component.get<Components::RectangleVeil>());
        } else if(type.compare("Light") == 0) {
            registry.assign<Components::Light>(entity, component.get<Components::Light>());
        } else if(type.compare("ModelShader") == 0) {
            registry.assign<Components::ModelShader>(entity, component.get<Components::ModelShader>());
        }
    }
    return entity;


@@ 323,13 337,9 @@ uint32_t Components::createEntityFromJson(entt::registry<> &registry, const std:
    return createEntityFromJson(registry, json::parse(s));
}

void Components::to_json(nlohmann::json &j, const Components::RectangleVeil &t)
{
 j = json{{"type", "RectangleVeil"}};
}
void Components::to_json(nlohmann::json &j, const Components::RectangleVeil &t) { j = json{{"type", "RectangleVeil"}}; }

void Components::from_json(const nlohmann::json &j, Components::RectangleVeil &t)
{
void Components::from_json(const nlohmann::json &j, Components::RectangleVeil &t) {
    t.elapsed = 0.0f;
    if(j.find("frameDelay") != j.end())
        t.frameDelay = j.at("frameDelay").get<float>();


@@ 338,3 348,24 @@ void Components::from_json(const nlohmann::json &j, Components::RectangleVeil &t
    if(j.find("rectAnimDuration") != j.end())
        t.rectAnimDuration = j.at("rectAnimDuration").get<float>();
}
void Components::to_json(json &j, const Components::Light &l) {
    j = json{{"type", "Light"},
             {"colorR", (int)l.color.r},
             {"colorG", (int)l.color.g},
             {"colorB", (int)l.color.b},
             {"strength", l.strength}};
}
void Components::from_json(const json &j, Components::Light &l) {

    l.color.r = j.at("colorR").get<int>();
    l.color.g = j.at("colorG").get<int>();
    l.color.b = j.at("colorB").get<int>();
    l.strength = j.at("strength").get<float>();
}
void Components::to_json(json &j, const Components::ModelShader &ms) {
    j = json{{"type", "ModelShader"}, {"vertexShader", ms.vs}, {"fragmentShader", ms.fs}};
}
void Components::from_json(const json &j, Components::ModelShader &ms) {
    ms.fs = j.at("fragmentShader").get<std::string>();
    ms.vs = j.at("vertexShader").get<std::string>();
}

M src/utils/JsonParser.hpp => src/utils/JsonParser.hpp +8 -0
@@ 8,6 8,8 @@
#include "components/DestroyAfterTime.hpp"
#include "components/Destroyable.hpp"
#include "components/Fighter.hpp"
#include "components/ModelShader.hpp"
#include "components/LightComponent.hpp"
#include "components/MeshRenderer.hpp"
#include "components/Missile.hpp"
#include "components/Patrol.hpp"


@@ 72,9 74,15 @@ void from_json(const json &j, Components::Destroyable &t);
void to_json(json &j, const Components::SpawnPoint &t);
void from_json(const json &j, Components::SpawnPoint &t);

void to_json(json &j, const Components::Light &l);
void from_json(const json &j, Components::Light &l);

void to_json(json &j, const Components::RectangleVeil &t);
void from_json(const json &j, Components::RectangleVeil &t);

void to_json(json &j, const Components::ModelShader &ms);
void from_json(const json &j, Components::ModelShader &ms);

void parseToFile(entt::registry<> &registry, const char *filename);
void readFromFile(entt::registry<> &registry, const char *filename);