From ae7a8b2b29e43afda7f31308d718224d59f060db Mon Sep 17 00:00:00 2001 From: OusmBlueNinja <89956790+OusmBlueNinja@users.noreply.github.com> Date: Sat, 12 Apr 2025 20:34:29 -0500 Subject: [PATCH] Added Camera Component and layers --- build.log | 44 +++++++++++++++++++++++--- src/src/Components/CameraComponent.cpp | 19 +++++++++++ src/src/Components/CameraComponent.h | 24 ++++++++++++++ src/src/Engine.cpp | 29 ++++++++++++++--- src/src/Entitys/Object.cpp | 2 +- src/src/Entitys/Object.h | 2 ++ 6 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 src/src/Components/CameraComponent.cpp create mode 100644 src/src/Components/CameraComponent.h diff --git a/build.log b/build.log index 6f89293..8b7274a 100644 --- a/build.log +++ b/build.log @@ -1,6 +1,42 @@ -[COMPILE] g++ -std=c++20 -Wall -Isrc/include -Isrc/vendor -Isrc/vendor/imgui -IC:/msys64/mingw64/include -MMD -MP -c src\src\main.cpp -o src\build\src\main.o +[COMPILE] g++ -std=c++20 -Wall -Isrc/include -Isrc/vendor -Isrc/vendor/imgui -IC:/msys64/mingw64/include -MMD -MP -c src\src\Engine.cpp -o src\build\src\Engine.osrc\src\Engine.cpp:23:9: warning: "NOMINMAX" redefined + 23 | #define NOMINMAX + | ^~~~~~~~ +In file included from C:/msys64/mingw64/include/c++/14.2.0/x86_64-w64-mingw32/bits/c++config.h:680, + from C:/msys64/mingw64/include/c++/14.2.0/bits/memoryfwd.h:48, + from C:/msys64/mingw64/include/c++/14.2.0/memory:63, + from src\src\Engine.h:2, + from src\src\Engine.cpp:1: +C:/msys64/mingw64/include/c++/14.2.0/x86_64-w64-mingw32/bits/os_defines.h:45:9: note: this is the location of the previous definition + 45 | #define NOMINMAX 1 + | ^~~~~~~~ +In file included from C:/msys64/mingw64/include/yaml-cpp/parser.h:13, + from C:/msys64/mingw64/include/yaml-cpp/yaml.h:10, + from src\src\Entitys/Object.h:6, + from src\src\Engine.cpp:2: +C:/msys64/mingw64/include/yaml-cpp/dll.h:22:65: note: '#pragma message: Defining YAML_CPP_API for DLL import' + 22 | # pragma message( "Defining YAML_CPP_API for DLL import" ) + | ^ + +[COMPILE] g++ -std=c++20 -Wall -Isrc/include -Isrc/vendor -Isrc/vendor/imgui -IC:/msys64/mingw64/include -MMD -MP -c src\src\Entitys\Object.cpp -o src\build\src\Entitys\Object.oIn file included from C:/msys64/mingw64/include/yaml-cpp/parser.h:13, + from C:/msys64/mingw64/include/yaml-cpp/yaml.h:10, + from src\src\Entitys\Object.h:6, + from src\src\Entitys\Object.cpp:1: +C:/msys64/mingw64/include/yaml-cpp/dll.h:22:65: note: '#pragma message: Defining YAML_CPP_API for DLL import' + 22 | # pragma message( "Defining YAML_CPP_API for DLL import" ) + | ^ +src\src\Entitys\Object.h: In constructor 'Object::Object(const std::string&)': +src\src\Entitys\Object.h:48:15: warning: 'Object::localPosition' will be initialized after [-Wreorder] + 48 | glm::vec2 localPosition; + | ^~~~~~~~~~~~~ +src\src\Entitys\Object.h:41:9: warning: 'int Object::id' [-Wreorder] + 41 | int id; + | ^~ +src\src\Entitys\Object.cpp:10:1: warning: when initialized here [-Wreorder] + 10 | Object::Object(const std::string& name) + | ^~~~~~ + [LINK] g++ src\build\src\Engine.o src\build\src\main.o src\build\src\Renderer.o src\build\src\Components\SpriteComponent.o src\build\src\Entitys\Object.o src\build\src\utils\FileDialog.o src\build\src\utils\Shader.o src\build\vendor\imgui\imgui.o src\build\vendor\imgui\imgui_demo.o src\build\vendor\imgui\imgui_draw.o src\build\vendor\imgui\imgui_impl_glfw.o src\build\vendor\imgui\imgui_impl_opengl3.o src\build\vendor\imgui\imgui_tables.o src\build\vendor\imgui\imgui_widgets.o -o src\build\app.exe -LC:/msys64/mingw64/lib -lglfw3 -lglew32 -lopengl32 -lgdi32 -lyaml-cpp -lcomdlg32 -lssl -lcrypto -[TIME] Build duration: 1.23s -[ERROR] Runtime crash -Command 'src\build\app.exe' returned non-zero exit status 3221226356. +[TIME] Build duration: 9.21s +[RUN] Executed app.exe successfully. +[TIME] Total runtime: 55.09s diff --git a/src/src/Components/CameraComponent.cpp b/src/src/Components/CameraComponent.cpp new file mode 100644 index 0000000..78107fc --- /dev/null +++ b/src/src/Components/CameraComponent.cpp @@ -0,0 +1,19 @@ +#include "CameraComponent.h" + +CameraComponent::CameraComponent(Object* owner) + : Component(owner) {} + +void CameraComponent::Save(YAML::Emitter& out) { + out << YAML::BeginMap; + out << YAML::Key << "type" << YAML::Value << "CameraComponent"; + out << YAML::Key << "zoom" << YAML::Value << zoom; + out << YAML::Key << "aspectRatio" << YAML::Value << aspectRatio; + out << YAML::Key << "fov" << YAML::Value << fov; + out << YAML::EndMap; +} + +void CameraComponent::Load(const YAML::Node& node) { + zoom = node["zoom"] ? node["zoom"].as() : 1.0f; + aspectRatio = node["aspectRatio"] ? node["aspectRatio"].as() : 16.0f / 9.0f; + fov = node["fov"] ? node["fov"].as() : 45.0f; +} diff --git a/src/src/Components/CameraComponent.h b/src/src/Components/CameraComponent.h new file mode 100644 index 0000000..9b6cae1 --- /dev/null +++ b/src/src/Components/CameraComponent.h @@ -0,0 +1,24 @@ +#pragma once +#include "Component.h" +#include +#include + +class CameraComponent : public Component { +public: + float zoom = 1.0f; + float aspectRatio = 16.0f / 9.0f; + float fov = 45.0f; + + CameraComponent(Object* owner); + + void Save(YAML::Emitter& out) override; + void Load(const YAML::Node& node) override; + + float GetZoom() const { return zoom; } + float GetAspectRatio() const { return aspectRatio; } + float GetFOV() const { return fov; } + + void SetZoom(float z) { zoom = z; } + void SetAspectRatio(float a) { aspectRatio = a; } + void SetFOV(float f) { fov = f; } +}; diff --git a/src/src/Engine.cpp b/src/src/Engine.cpp index 61830fb..1524c2b 100644 --- a/src/src/Engine.cpp +++ b/src/src/Engine.cpp @@ -10,6 +10,8 @@ #include #include + +#include #include #include #include @@ -20,7 +22,7 @@ #define WIN32_LEAN_AND_MEAN #define NOMINMAX #include -#include // includes GetOpenFileNameA +#include #include static std::vector> objects; @@ -151,6 +153,8 @@ void Engine::Run() if (ImGui::DragFloat2("Position", &pos.x)) selected->SetLocalPosition(pos); + ImGui::InputInt("Layer", &selected->layer); + // Add component if (ImGui::Button("Add SpriteComponent")) { @@ -205,13 +209,28 @@ void Engine::Run() Renderer::Resize((int)size.x, (int)size.y); Renderer::Begin(); - for (auto &obj : objects) - { - if (auto sprite = obj->GetComponent()) - { + std::vector> toDraw; + + std::function&)> collect = [&](const std::shared_ptr& obj) { + toDraw.push_back(obj); + for (const auto& child : obj->GetChildren()) + collect(child); + }; + + for (const auto& obj : objects) + if (!obj->GetParent()) + collect(obj); + + std::sort(toDraw.begin(), toDraw.end(), [](const auto& a, const auto& b) { + return a->layer < b->layer; + }); + + for (const auto& obj : toDraw) { + if (auto sprite = obj->GetComponent()) { Renderer::DrawSprite(sprite.get(), obj->GetWorldPosition()); } } + Renderer::End(); GLuint texID = Renderer::GetRenderTexture(); diff --git a/src/src/Entitys/Object.cpp b/src/src/Entitys/Object.cpp index 61d1771..b2057c3 100644 --- a/src/src/Entitys/Object.cpp +++ b/src/src/Entitys/Object.cpp @@ -8,7 +8,7 @@ static int nextID = 0; Object::Object(const std::string& name) - : name(name), localPosition(0.0f, 0.0f), id(nextID++) {} + : name(name), localPosition(0.0f, 0.0f), id(nextID++), layer(1) {} Object::~Object() {} diff --git a/src/src/Entitys/Object.h b/src/src/Entitys/Object.h index ab3a376..330fbab 100644 --- a/src/src/Entitys/Object.h +++ b/src/src/Entitys/Object.h @@ -41,6 +41,8 @@ public: int id; bool selected = false; + int layer; + private: std::string name; glm::vec2 localPosition;