Added Editor Camera
This commit is contained in:
parent
ae7a8b2b29
commit
a71d221ad9
38
build.log
38
build.log
@ -1,5 +1,5 @@
|
|||||||
[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
|
[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:24:9: warning: "NOMINMAX" redefined
|
||||||
23 | #define NOMINMAX
|
24 | #define NOMINMAX
|
||||||
| ^~~~~~~~
|
| ^~~~~~~~
|
||||||
In file included from C:/msys64/mingw64/include/c++/14.2.0/x86_64-w64-mingw32/bits/c++config.h:680,
|
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/bits/memoryfwd.h:48,
|
||||||
@ -11,32 +11,30 @@ C:/msys64/mingw64/include/c++/14.2.0/x86_64-w64-mingw32/bits/os_defines.h:45:9:
|
|||||||
| ^~~~~~~~
|
| ^~~~~~~~
|
||||||
In file included from C:/msys64/mingw64/include/yaml-cpp/parser.h:13,
|
In file included from C:/msys64/mingw64/include/yaml-cpp/parser.h:13,
|
||||||
from C:/msys64/mingw64/include/yaml-cpp/yaml.h:10,
|
from C:/msys64/mingw64/include/yaml-cpp/yaml.h:10,
|
||||||
from src\src\Entitys/Object.h:6,
|
from src\src\Entitys/Object.h:7,
|
||||||
from src\src\Engine.cpp:2:
|
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'
|
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" )
|
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,
|
[COMPILE] g++ -std=c++20 -Wall -Isrc/include -Isrc/vendor -Isrc/vendor/imgui -IC:/msys64/mingw64/include -MMD -MP -c src\src\Renderer.cpp -o src\build\src\Renderer.oIn file included from C:/msys64/mingw64/include/yaml-cpp/parser.h:13,
|
||||||
from C:/msys64/mingw64/include/yaml-cpp/yaml.h:10,
|
from C:/msys64/mingw64/include/yaml-cpp/yaml.h:10,
|
||||||
from src\src\Entitys\Object.h:6,
|
from src\src\Components/Component.h:5,
|
||||||
from src\src\Entitys\Object.cpp:1:
|
from src\src\Components/SpriteComponent.h:3,
|
||||||
|
from src\src\Renderer.cpp:2:
|
||||||
C:/msys64/mingw64/include/yaml-cpp/dll.h:22:65: note: '#pragma message: Defining YAML_CPP_API for DLL import'
|
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" )
|
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
|
[COMPILE] g++ -std=c++20 -Wall -Isrc/include -Isrc/vendor -Isrc/vendor/imgui -IC:/msys64/mingw64/include -MMD -MP -c src\src\Components\CameraComponent.cpp -o src\build\src\Components\CameraComponent.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\Components\Component.h:5,
|
||||||
|
from src\src\Components\CameraComponent.h:2,
|
||||||
|
from src\src\Components\CameraComponent.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" )
|
||||||
|
| ^
|
||||||
|
|
||||||
[TIME] Build duration: 9.21s
|
[LINK] g++ src\build\src\Engine.o src\build\src\main.o src\build\src\Renderer.o src\build\src\Components\CameraComponent.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
|
||||||
[RUN] Executed app.exe successfully.
|
|
||||||
[TIME] Total runtime: 55.09s
|
[TIME] Build duration: 11.26s
|
||||||
|
@ -1,12 +1,41 @@
|
|||||||
engine_version: 0.1.0
|
engine_version: 0.1.0
|
||||||
scene_name: test
|
scene_name: test
|
||||||
scene_hash: 2bef506b8552068aab28115edf9550411c1fab67befdc32a4946ec33da7a8021
|
scene_hash: b3488ddfbb343e554cb1782c1b79598e15545d752cfec63be97f90c8d422aaf5
|
||||||
format_version: 1
|
format_version: 1
|
||||||
objects:
|
objects:
|
||||||
- name: Face Image
|
- name: Sprite Object
|
||||||
position: [0, 0]
|
position: [438, 0]
|
||||||
|
layer: 0
|
||||||
|
components:
|
||||||
|
- type: CameraComponent
|
||||||
|
fov: 45
|
||||||
|
aspect: 1.76999998
|
||||||
|
zoom: 1
|
||||||
|
- type: SpriteComponent
|
||||||
|
texture: C:\Users\spenc\OneDrive\Pictures\49555.jpg
|
||||||
|
normalMap: ""
|
||||||
|
children:
|
||||||
|
- name: NewObject
|
||||||
|
position: [190, 189]
|
||||||
|
layer: 0
|
||||||
|
components:
|
||||||
|
- type: SpriteComponent
|
||||||
|
texture: C:\Users\spenc\OneDrive\Pictures\49555.jpg
|
||||||
|
normalMap: ""
|
||||||
|
children: []
|
||||||
|
- name: NewObject
|
||||||
|
position: [0, 444]
|
||||||
|
layer: 0
|
||||||
|
components:
|
||||||
|
- type: SpriteComponent
|
||||||
|
texture: C:\Users\spenc\OneDrive\Pictures\49555.jpg
|
||||||
|
normalMap: ""
|
||||||
|
children: []
|
||||||
|
- name: NewObject
|
||||||
|
position: [0, 444]
|
||||||
|
layer: 0
|
||||||
components:
|
components:
|
||||||
- type: SpriteComponent
|
- type: SpriteComponent
|
||||||
texture: C:\Users\spenc\OneDrive\Pictures\49555.jpg
|
texture: C:\Users\spenc\OneDrive\Pictures\49555.jpg
|
||||||
normalMap: C:\Users\spenc\OneDrive\Pictures\49555.jpg
|
normalMap: ""
|
||||||
children: []
|
children: []
|
@ -1,19 +1,29 @@
|
|||||||
#include "CameraComponent.h"
|
#include "CameraComponent.h"
|
||||||
|
#include "../Entitys/Object.h"
|
||||||
|
|
||||||
CameraComponent::CameraComponent(Object* owner)
|
CameraComponent::CameraComponent(Object* owner) : Component(owner) {}
|
||||||
: Component(owner) {}
|
|
||||||
|
|
||||||
void CameraComponent::Save(YAML::Emitter& out) {
|
std::string CameraComponent::GetName() const {
|
||||||
|
return "CameraComponent";
|
||||||
|
}
|
||||||
|
|
||||||
|
void CameraComponent::Save(YAML::Emitter& out) const {
|
||||||
out << YAML::BeginMap;
|
out << YAML::BeginMap;
|
||||||
out << YAML::Key << "type" << YAML::Value << "CameraComponent";
|
out << YAML::Key << "type" << YAML::Value << GetName();
|
||||||
out << YAML::Key << "zoom" << YAML::Value << zoom;
|
|
||||||
out << YAML::Key << "aspectRatio" << YAML::Value << aspectRatio;
|
|
||||||
out << YAML::Key << "fov" << YAML::Value << fov;
|
out << YAML::Key << "fov" << YAML::Value << fov;
|
||||||
|
out << YAML::Key << "aspect" << YAML::Value << aspect;
|
||||||
|
out << YAML::Key << "zoom" << YAML::Value << zoom;
|
||||||
|
out << YAML::Key << "primary" << YAML::Value << primary;
|
||||||
|
|
||||||
|
|
||||||
out << YAML::EndMap;
|
out << YAML::EndMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CameraComponent::Load(const YAML::Node& node) {
|
void CameraComponent::Load(const YAML::Node& node) {
|
||||||
zoom = node["zoom"] ? node["zoom"].as<float>() : 1.0f;
|
if (node["fov"]) fov = node["fov"].as<float>();
|
||||||
aspectRatio = node["aspectRatio"] ? node["aspectRatio"].as<float>() : 16.0f / 9.0f;
|
if (node["aspect"]) aspect = node["aspect"].as<float>();
|
||||||
fov = node["fov"] ? node["fov"].as<float>() : 45.0f;
|
if (node["zoom"]) zoom = node["zoom"].as<float>();
|
||||||
|
if (node["primary"]) primary = node["primary"].as<bool>();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,35 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Component.h"
|
#include "Component.h"
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <yaml-cpp/yaml.h>
|
|
||||||
|
|
||||||
class CameraComponent : public Component {
|
class CameraComponent : public Component {
|
||||||
public:
|
public:
|
||||||
float zoom = 1.0f;
|
|
||||||
float aspectRatio = 16.0f / 9.0f;
|
|
||||||
float fov = 45.0f;
|
|
||||||
|
|
||||||
CameraComponent(Object* owner);
|
CameraComponent(Object* owner);
|
||||||
|
|
||||||
void Save(YAML::Emitter& out) override;
|
std::string GetName() const override;
|
||||||
|
void Save(YAML::Emitter& out) const override;
|
||||||
void Load(const YAML::Node& node) override;
|
void Load(const YAML::Node& node) override;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
float GetZoom() const { return zoom; }
|
float GetZoom() const { return zoom; }
|
||||||
float GetAspectRatio() const { return aspectRatio; }
|
float GetAspect() const { return aspect; }
|
||||||
float GetFOV() const { return fov; }
|
float GetFOV() const { return fov; }
|
||||||
|
bool IsPrimary() const { return primary; }
|
||||||
|
|
||||||
|
|
||||||
void SetZoom(float z) { zoom = z; }
|
void SetZoom(float z) { zoom = z; }
|
||||||
void SetAspectRatio(float a) { aspectRatio = a; }
|
void SetAspect(float a) { aspect = a; }
|
||||||
void SetFOV(float f) { fov = f; }
|
void SetFOV(float f) { fov = f; }
|
||||||
|
void SetPrimary(bool p) { primary = p; }
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
float zoom = 1.0f;
|
||||||
|
float fov = 45.0f;
|
||||||
|
float aspect = 1.77f;
|
||||||
|
bool primary = true;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
#include "Entitys/Object.h"
|
#include "Entitys/Object.h"
|
||||||
#include "Renderer.h"
|
#include "Renderer.h"
|
||||||
#include "components/SpriteComponent.h"
|
#include "components/SpriteComponent.h"
|
||||||
|
#include "components/CameraComponent.h"
|
||||||
|
|
||||||
#include "utils/FileDialog.h"
|
#include "utils/FileDialog.h"
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
@ -10,7 +12,6 @@
|
|||||||
#include <imgui_impl_glfw.h>
|
#include <imgui_impl_glfw.h>
|
||||||
#include <imgui_impl_opengl3.h>
|
#include <imgui_impl_opengl3.h>
|
||||||
|
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@ -28,6 +29,36 @@
|
|||||||
static std::vector<std::shared_ptr<Object>> objects;
|
static std::vector<std::shared_ptr<Object>> objects;
|
||||||
static std::shared_ptr<Object> selected = nullptr;
|
static std::shared_ptr<Object> selected = nullptr;
|
||||||
static bool playing = false;
|
static bool playing = false;
|
||||||
|
|
||||||
|
struct EditorCamera
|
||||||
|
{
|
||||||
|
glm::vec2 position = glm::vec2(0.0f);
|
||||||
|
float zoom = 1.0f;
|
||||||
|
|
||||||
|
void HandleInput()
|
||||||
|
{
|
||||||
|
if (ImGui::IsWindowHovered())
|
||||||
|
{
|
||||||
|
if (ImGui::IsMouseDragging(ImGuiMouseButton_Right))
|
||||||
|
{
|
||||||
|
ImVec2 delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right);
|
||||||
|
ImGui::ResetMouseDragDelta(ImGuiMouseButton_Right);
|
||||||
|
position -= glm::vec2(delta.x, -delta.y) / zoom;
|
||||||
|
}
|
||||||
|
|
||||||
|
float wheel = ImGui::GetIO().MouseWheel;
|
||||||
|
if (wheel != 0.0f)
|
||||||
|
{
|
||||||
|
float zoomFactor = 1.1f;
|
||||||
|
zoom *= (wheel > 0) ? zoomFactor : (1.0f / zoomFactor);
|
||||||
|
zoom = std::clamp(zoom, 0.1f, 10.0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static EditorCamera editorCamera;
|
||||||
|
|
||||||
GLFWwindow *window = nullptr;
|
GLFWwindow *window = nullptr;
|
||||||
|
|
||||||
Engine::Engine()
|
Engine::Engine()
|
||||||
@ -106,8 +137,6 @@ void Engine::Run()
|
|||||||
|
|
||||||
ImGui::Begin("Scene Tree");
|
ImGui::Begin("Scene Tree");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (ImGui::BeginPopupContextWindow("SceneTreeContext", ImGuiPopupFlags_MouseButtonRight))
|
if (ImGui::BeginPopupContextWindow("SceneTreeContext", ImGuiPopupFlags_MouseButtonRight))
|
||||||
{
|
{
|
||||||
if (ImGui::MenuItem("Create New Object"))
|
if (ImGui::MenuItem("Create New Object"))
|
||||||
@ -130,15 +159,10 @@ void Engine::Run()
|
|||||||
ImGui::EndPopup();
|
ImGui::EndPopup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for (auto &obj : objects)
|
for (auto &obj : objects)
|
||||||
if (!obj->GetParent()) // Only draw root nodes
|
if (!obj->GetParent()) // Only draw root nodes
|
||||||
DrawObjectNode(obj);
|
DrawObjectNode(obj);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
ImGui::Begin("Inspector");
|
ImGui::Begin("Inspector");
|
||||||
@ -155,13 +179,20 @@ void Engine::Run()
|
|||||||
|
|
||||||
ImGui::InputInt("Layer", &selected->layer);
|
ImGui::InputInt("Layer", &selected->layer);
|
||||||
|
|
||||||
// Add component
|
// Add SpriteComponent
|
||||||
if (ImGui::Button("Add SpriteComponent"))
|
if (ImGui::Button("Add SpriteComponent"))
|
||||||
{
|
{
|
||||||
if (!selected->GetComponent<SpriteComponent>())
|
if (!selected->GetComponent<SpriteComponent>())
|
||||||
selected->AddComponent<SpriteComponent>();
|
selected->AddComponent<SpriteComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add CameraComponent
|
||||||
|
if (ImGui::Button("Add CameraComponent"))
|
||||||
|
{
|
||||||
|
if (!selected->GetComponent<CameraComponent>())
|
||||||
|
selected->AddComponent<CameraComponent>();
|
||||||
|
}
|
||||||
|
|
||||||
// Show SpriteComponent UI
|
// Show SpriteComponent UI
|
||||||
if (auto sprite = selected->GetComponent<SpriteComponent>())
|
if (auto sprite = selected->GetComponent<SpriteComponent>())
|
||||||
{
|
{
|
||||||
@ -192,6 +223,29 @@ void Engine::Run()
|
|||||||
selected->RemoveComponent<SpriteComponent>();
|
selected->RemoveComponent<SpriteComponent>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Show CameraComponent UI
|
||||||
|
if (auto cam = selected->GetComponent<CameraComponent>())
|
||||||
|
{
|
||||||
|
ImGui::Separator();
|
||||||
|
ImGui::Text("Camera Component");
|
||||||
|
|
||||||
|
float fov = cam->GetFOV();
|
||||||
|
float aspect = cam->GetAspect();
|
||||||
|
float zoom = cam->GetZoom();
|
||||||
|
|
||||||
|
if (ImGui::DragFloat("FOV", &fov, 0.1f, 1.0f, 179.0f))
|
||||||
|
cam->SetFOV(fov);
|
||||||
|
if (ImGui::DragFloat("Aspect", &aspect, 0.01f, 0.1f, 5.0f))
|
||||||
|
cam->SetAspect(aspect);
|
||||||
|
if (ImGui::DragFloat("Zoom", &zoom, 0.1f, 0.1f, 10.0f))
|
||||||
|
cam->SetZoom(zoom);
|
||||||
|
|
||||||
|
if (ImGui::Button("Remove CameraComponent"))
|
||||||
|
{
|
||||||
|
selected->RemoveComponent<CameraComponent>();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -199,55 +253,78 @@ void Engine::Run()
|
|||||||
}
|
}
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Viewport
|
// Viewport
|
||||||
ImGui::Begin("Viewport", nullptr, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
|
ImGui::Begin("Viewport", nullptr, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
|
||||||
ImVec2 size = ImGui::GetContentRegionAvail();
|
ImVec2 size = ImGui::GetContentRegionAvail();
|
||||||
|
|
||||||
|
if (!playing)
|
||||||
|
editorCamera.HandleInput();
|
||||||
|
|
||||||
|
glm::vec2 cameraOffset = editorCamera.position;
|
||||||
|
float camZoom = editorCamera.zoom;
|
||||||
|
|
||||||
|
if (playing)
|
||||||
|
{
|
||||||
|
for (const auto &obj : objects)
|
||||||
|
{
|
||||||
|
if (auto cam = obj->GetComponent<CameraComponent>())
|
||||||
|
{
|
||||||
|
if (cam->IsPrimary())
|
||||||
|
{
|
||||||
|
cameraOffset = obj->GetWorldPosition();
|
||||||
|
camZoom = cam->GetZoom();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Renderer::Resize((int)size.x, (int)size.y);
|
Renderer::Resize((int)size.x, (int)size.y);
|
||||||
Renderer::Begin();
|
Renderer::Begin();
|
||||||
|
|
||||||
std::vector<std::shared_ptr<Object>> toDraw;
|
std::vector<std::shared_ptr<Object>> toDraw;
|
||||||
|
std::function<void(const std::shared_ptr<Object> &)> collect = [&](const std::shared_ptr<Object> &obj)
|
||||||
std::function<void(const std::shared_ptr<Object>&)> collect = [&](const std::shared_ptr<Object>& obj) {
|
{
|
||||||
toDraw.push_back(obj);
|
toDraw.push_back(obj);
|
||||||
for (const auto& child : obj->GetChildren())
|
for (const auto &child : obj->GetChildren())
|
||||||
collect(child);
|
collect(child);
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const auto& obj : objects)
|
for (const auto &obj : objects)
|
||||||
if (!obj->GetParent())
|
if (!obj->GetParent())
|
||||||
collect(obj);
|
collect(obj);
|
||||||
|
|
||||||
std::sort(toDraw.begin(), toDraw.end(), [](const auto& a, const auto& b) {
|
std::sort(toDraw.begin(), toDraw.end(), [](const auto &a, const auto &b)
|
||||||
return a->layer < b->layer;
|
{ return a->layer < b->layer; });
|
||||||
});
|
|
||||||
|
|
||||||
for (const auto& obj : toDraw) {
|
for (const auto &obj : toDraw)
|
||||||
if (auto sprite = obj->GetComponent<SpriteComponent>()) {
|
{
|
||||||
Renderer::DrawSprite(sprite.get(), obj->GetWorldPosition());
|
if (auto sprite = obj->GetComponent<SpriteComponent>())
|
||||||
|
{
|
||||||
|
glm::vec2 renderPos = (obj->GetWorldPosition() - cameraOffset) * camZoom;
|
||||||
|
Renderer::DrawSprite(sprite.get(), renderPos, camZoom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Renderer::End();
|
Renderer::End();
|
||||||
GLuint texID = Renderer::GetRenderTexture();
|
GLuint texID = Renderer::GetRenderTexture();
|
||||||
ImGui::Image((ImTextureID)(uintptr_t)texID, size, ImVec2(0, 1), ImVec2(1, 0));
|
ImGui::Image((ImTextureID)(uintptr_t)texID, size, ImVec2(0, 1), ImVec2(1, 0));
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
if (ImGui::BeginPopup("RenameObject")) {
|
if (ImGui::BeginPopup("RenameObject"))
|
||||||
|
{
|
||||||
static char nameBuffer[128];
|
static char nameBuffer[128];
|
||||||
static bool once = true;
|
static bool once = true;
|
||||||
|
|
||||||
if (once && selected) {
|
if (once && selected)
|
||||||
|
{
|
||||||
strcpy(nameBuffer, selected->GetName().c_str());
|
strcpy(nameBuffer, selected->GetName().c_str());
|
||||||
once = false;
|
once = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui::InputText("##rename", nameBuffer, sizeof(nameBuffer));
|
ImGui::InputText("##rename", nameBuffer, sizeof(nameBuffer));
|
||||||
if (ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::Button("OK")) {
|
if (ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::Button("OK"))
|
||||||
|
{
|
||||||
if (selected)
|
if (selected)
|
||||||
selected->SetName(nameBuffer);
|
selected->SetName(nameBuffer);
|
||||||
ImGui::CloseCurrentPopup();
|
ImGui::CloseCurrentPopup();
|
||||||
@ -255,7 +332,8 @@ void Engine::Run()
|
|||||||
}
|
}
|
||||||
|
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
if (ImGui::Button("Cancel")) {
|
if (ImGui::Button("Cancel"))
|
||||||
|
{
|
||||||
ImGui::CloseCurrentPopup();
|
ImGui::CloseCurrentPopup();
|
||||||
once = true;
|
once = true;
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
#include "Object.h"
|
#include "Object.h"
|
||||||
#include "../Components/SpriteComponent.h"
|
|
||||||
#include "../Components/Component.h"
|
#include "../Components/Component.h"
|
||||||
|
#include "../Components/SpriteComponent.h"
|
||||||
|
#include "../Components/CameraComponent.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
static int nextID = 0;
|
static int nextID = 0;
|
||||||
|
|
||||||
Object::Object(const std::string& name)
|
Object::Object(const std::string& name)
|
||||||
: name(name), localPosition(0.0f, 0.0f), id(nextID++), layer(1) {}
|
: name(name), localPosition(0.0f, 0.0f), id(nextID++) {}
|
||||||
|
|
||||||
Object::~Object() {}
|
Object::~Object() {}
|
||||||
|
|
||||||
@ -49,79 +49,48 @@ void Object::SetName(const std::string& n) { name = n; }
|
|||||||
std::vector<std::shared_ptr<Object>>& Object::GetChildren() { return children; }
|
std::vector<std::shared_ptr<Object>>& Object::GetChildren() { return children; }
|
||||||
Object* Object::GetParent() const { return parent; }
|
Object* Object::GetParent() const { return parent; }
|
||||||
|
|
||||||
// === Component System ===
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
std::shared_ptr<T> Object::GetComponent() const {
|
|
||||||
for (const auto& comp : components) {
|
|
||||||
if (auto casted = std::dynamic_pointer_cast<T>(comp))
|
|
||||||
return casted;
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
std::shared_ptr<T> Object::AddComponent() {
|
|
||||||
auto existing = GetComponent<T>();
|
|
||||||
if (existing) return existing;
|
|
||||||
|
|
||||||
std::shared_ptr<T> component = std::make_shared<T>(this);
|
|
||||||
components.push_back(component);
|
|
||||||
return component;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void Object::RemoveComponent() {
|
|
||||||
components.erase(std::remove_if(components.begin(), components.end(),
|
|
||||||
[](const std::shared_ptr<Component>& comp) {
|
|
||||||
return std::dynamic_pointer_cast<T>(comp) != nullptr;
|
|
||||||
}), components.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Explicit template instantiation
|
|
||||||
template std::shared_ptr<class SpriteComponent> Object::GetComponent<class SpriteComponent>() const;
|
|
||||||
template std::shared_ptr<class SpriteComponent> Object::AddComponent<class SpriteComponent>();
|
|
||||||
template void Object::RemoveComponent<class SpriteComponent>();
|
|
||||||
|
|
||||||
void Object::Save(YAML::Emitter& out) const {
|
void Object::Save(YAML::Emitter& out) const {
|
||||||
out << YAML::BeginMap;
|
out << YAML::BeginMap;
|
||||||
out << YAML::Key << "name" << YAML::Value << name;
|
out << YAML::Key << "name" << YAML::Value << name;
|
||||||
out << YAML::Key << "position" << YAML::Value << YAML::Flow << YAML::BeginSeq << localPosition.x << localPosition.y << YAML::EndSeq;
|
out << YAML::Key << "position" << YAML::Value << YAML::Flow << YAML::BeginSeq << localPosition.x << localPosition.y << YAML::EndSeq;
|
||||||
|
out << YAML::Key << "layer" << YAML::Value << layer;
|
||||||
out << YAML::Key << "components" << YAML::Value << YAML::BeginSeq;
|
out << YAML::Key << "components" << YAML::Value << YAML::BeginSeq;
|
||||||
for (const auto& comp : components) {
|
for (const auto& comp : components)
|
||||||
comp->Save(out);
|
comp->Save(out);
|
||||||
}
|
|
||||||
out << YAML::EndSeq;
|
out << YAML::EndSeq;
|
||||||
|
|
||||||
out << YAML::Key << "children" << YAML::Value << YAML::BeginSeq;
|
out << YAML::Key << "children" << YAML::Value << YAML::BeginSeq;
|
||||||
for (const auto& child : children)
|
for (const auto& child : children)
|
||||||
child->Save(out);
|
child->Save(out);
|
||||||
out << YAML::EndSeq;
|
out << YAML::EndSeq;
|
||||||
|
|
||||||
out << YAML::EndMap;
|
out << YAML::EndMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Object::Load(const YAML::Node& node) {
|
void Object::Load(const YAML::Node& node) {
|
||||||
name = node["name"].as<std::string>();
|
name = node["name"].as<std::string>();
|
||||||
auto pos = node["position"];
|
auto pos = node["position"];
|
||||||
localPosition = { pos[0].as<float>(), pos[1].as<float>() };
|
if (pos && pos.IsSequence() && pos.size() == 2) {
|
||||||
|
localPosition.x = pos[0].as<float>();
|
||||||
|
localPosition.y = pos[1].as<float>();
|
||||||
|
}
|
||||||
|
if (node["layer"])
|
||||||
|
layer = node["layer"].as<int>();
|
||||||
|
|
||||||
components.clear();
|
|
||||||
if (node["components"]) {
|
if (node["components"]) {
|
||||||
for (const auto& compNode : node["components"]) {
|
for (const auto& compNode : node["components"]) {
|
||||||
std::string type = compNode["type"].as<std::string>();
|
std::string type = compNode["type"].as<std::string>();
|
||||||
if (type == "SpriteComponent") {
|
if (type == "SpriteComponent") {
|
||||||
auto comp = AddComponent<class SpriteComponent>();
|
auto comp = AddComponent<SpriteComponent>();
|
||||||
|
comp->Load(compNode);
|
||||||
|
} else if (type == "CameraComponent") {
|
||||||
|
auto comp = AddComponent<CameraComponent>();
|
||||||
comp->Load(compNode);
|
comp->Load(compNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
children.clear();
|
|
||||||
if (node["children"]) {
|
if (node["children"]) {
|
||||||
for (const auto& childNode : node["children"]) {
|
for (const auto& childNode : node["children"]) {
|
||||||
auto child = std::make_shared<Object>("(loaded)");
|
auto child = std::make_shared<Object>("Child");
|
||||||
child->Load(childNode);
|
child->Load(childNode);
|
||||||
AddChild(child);
|
AddChild(child);
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,30 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <yaml-cpp/yaml.h>
|
#include <yaml-cpp/yaml.h>
|
||||||
|
|
||||||
#include "../Components/Component.h"
|
class Component;
|
||||||
|
|
||||||
class Object {
|
class Object {
|
||||||
public:
|
public:
|
||||||
Object(const std::string& name);
|
Object(const std::string& name);
|
||||||
~Object();
|
~Object();
|
||||||
|
|
||||||
void SetParent(Object* newParent);
|
|
||||||
void AddChild(std::shared_ptr<Object> child);
|
|
||||||
void RemoveChild(Object* child);
|
|
||||||
|
|
||||||
glm::vec2 GetWorldPosition() const;
|
|
||||||
glm::vec2 GetLocalPosition() const;
|
|
||||||
void SetLocalPosition(glm::vec2 pos);
|
|
||||||
|
|
||||||
const std::string& GetName() const;
|
const std::string& GetName() const;
|
||||||
void SetName(const std::string& name);
|
void SetName(const std::string& name);
|
||||||
|
|
||||||
std::vector<std::shared_ptr<Object>>& GetChildren();
|
glm::vec2 GetLocalPosition() const;
|
||||||
|
void SetLocalPosition(glm::vec2 pos);
|
||||||
|
glm::vec2 GetWorldPosition() const;
|
||||||
|
|
||||||
|
void SetParent(Object* parent);
|
||||||
Object* GetParent() const;
|
Object* GetParent() const;
|
||||||
|
void AddChild(std::shared_ptr<Object> child);
|
||||||
|
void RemoveChild(Object* child);
|
||||||
|
std::vector<std::shared_ptr<Object>>& GetChildren();
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::shared_ptr<T> GetComponent() const;
|
std::shared_ptr<T> GetComponent() const;
|
||||||
@ -38,10 +38,8 @@ public:
|
|||||||
void Save(YAML::Emitter& out) const;
|
void Save(YAML::Emitter& out) const;
|
||||||
void Load(const YAML::Node& node);
|
void Load(const YAML::Node& node);
|
||||||
|
|
||||||
int id;
|
int id = 0;
|
||||||
bool selected = false;
|
int layer = 0;
|
||||||
|
|
||||||
int layer;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string name;
|
std::string name;
|
||||||
@ -50,3 +48,30 @@ private:
|
|||||||
std::vector<std::shared_ptr<Object>> children;
|
std::vector<std::shared_ptr<Object>> children;
|
||||||
std::vector<std::shared_ptr<Component>> components;
|
std::vector<std::shared_ptr<Component>> components;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::shared_ptr<T> Object::GetComponent() const {
|
||||||
|
for (const auto& comp : components) {
|
||||||
|
if (auto casted = std::dynamic_pointer_cast<T>(comp))
|
||||||
|
return casted;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::shared_ptr<T> Object::AddComponent() {
|
||||||
|
auto existing = GetComponent<T>();
|
||||||
|
if (existing) return existing;
|
||||||
|
|
||||||
|
std::shared_ptr<T> component = std::make_shared<T>(this);
|
||||||
|
components.push_back(component);
|
||||||
|
return component;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void Object::RemoveComponent() {
|
||||||
|
components.erase(std::remove_if(components.begin(), components.end(),
|
||||||
|
[](const std::shared_ptr<Component>& comp) {
|
||||||
|
return std::dynamic_pointer_cast<T>(comp) != nullptr;
|
||||||
|
}), components.end());
|
||||||
|
}
|
||||||
|
@ -92,23 +92,22 @@ void Renderer::End() {
|
|||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos) {
|
void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float scale) {
|
||||||
if (!sprite || sprite->GetTextureID() == 0) return;
|
GLuint tex = sprite->GetTextureID();
|
||||||
|
if (!tex) return;
|
||||||
|
|
||||||
spriteShader.Use();
|
glBindTexture(GL_TEXTURE_2D, tex);
|
||||||
spriteShader.SetVec2("uPos", pos);
|
glBegin(GL_QUADS);
|
||||||
spriteShader.SetVec2("uSize", sprite->GetSize());
|
float size = 100.0f * scale;
|
||||||
spriteShader.SetVec2("uScreen", glm::vec2(width, height));
|
|
||||||
spriteShader.SetInt("uTex", 0);
|
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glTexCoord2f(0, 0); glVertex2f(pos.x, pos.y);
|
||||||
glBindTexture(GL_TEXTURE_2D, sprite->GetTextureID());
|
glTexCoord2f(1, 0); glVertex2f(pos.x + size, pos.y);
|
||||||
|
glTexCoord2f(1, 1); glVertex2f(pos.x + size, pos.y + size);
|
||||||
glBindVertexArray(quadVAO);
|
glTexCoord2f(0, 1); glVertex2f(pos.x, pos.y + size);
|
||||||
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
glEnd();
|
||||||
glBindVertexArray(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
GLuint Renderer::GetRenderTexture() {
|
GLuint Renderer::GetRenderTexture() {
|
||||||
return textureColorBuffer;
|
return textureColorBuffer;
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ public:
|
|||||||
static void Resize(int w, int h);
|
static void Resize(int w, int h);
|
||||||
static void Begin();
|
static void Begin();
|
||||||
static void End();
|
static void End();
|
||||||
static void DrawSprite(SpriteComponent* sprite, const glm::vec2& pos);
|
static void DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float scale);
|
||||||
static GLuint GetRenderTexture();
|
static GLuint GetRenderTexture();
|
||||||
static glm::ivec2 GetSize();
|
static glm::ivec2 GetSize();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user