Compare commits
3 Commits
7945ff246b
...
1f7fceb9b5
Author | SHA1 | Date | |
---|---|---|---|
|
1f7fceb9b5 | ||
|
a71d221ad9 | ||
|
ae7a8b2b29 |
31
build.log
31
build.log
@ -1,6 +1,27 @@
|
||||
[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
|
||||
[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\Engine.cpp -o src\build\src\Engine.osrc\src\Engine.cpp:24:9: warning: "NOMINMAX" redefined
|
||||
24 | #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:7,
|
||||
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" )
|
||||
| ^
|
||||
src\src\Engine.cpp:35:13: warning: 'isDragging' defined but not used [-Wunused-variable]
|
||||
35 | static bool isDragging = false;
|
||||
| ^~~~~~~~~~
|
||||
|
||||
[TIME] Build duration: 1.23s
|
||||
[ERROR] Runtime crash
|
||||
Command 'src\build\app.exe' returned non-zero exit status 3221226356.
|
||||
[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
|
||||
|
||||
[TIME] Build duration: 6.12s
|
||||
[RUN] Executed app.exe successfully.
|
||||
[TIME] Total runtime: 72.36s
|
||||
|
@ -1,12 +1,41 @@
|
||||
engine_version: 0.1.0
|
||||
scene_name: test
|
||||
scene_hash: 2bef506b8552068aab28115edf9550411c1fab67befdc32a4946ec33da7a8021
|
||||
scene_hash: b3488ddfbb343e554cb1782c1b79598e15545d752cfec63be97f90c8d422aaf5
|
||||
format_version: 1
|
||||
objects:
|
||||
- name: Face Image
|
||||
position: [0, 0]
|
||||
- name: Sprite Object
|
||||
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: 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:
|
||||
- type: SpriteComponent
|
||||
texture: C:\Users\spenc\OneDrive\Pictures\49555.jpg
|
||||
normalMap: ""
|
||||
children: []
|
31
src/build/imgui.ini
Normal file
31
src/build/imgui.ini
Normal file
@ -0,0 +1,31 @@
|
||||
[Window][WindowOverViewport_11111111]
|
||||
Pos=0,19
|
||||
Size=1280,701
|
||||
Collapsed=0
|
||||
|
||||
[Window][Debug##Default]
|
||||
Pos=60,60
|
||||
Size=400,400
|
||||
Collapsed=0
|
||||
|
||||
[Window][Scene Tree]
|
||||
Pos=60,60
|
||||
Size=128,48
|
||||
Collapsed=0
|
||||
|
||||
[Window][Inspector]
|
||||
Pos=1123,19
|
||||
Size=157,701
|
||||
Collapsed=0
|
||||
DockId=0x00000002,0
|
||||
|
||||
[Window][Viewport]
|
||||
Pos=60,60
|
||||
Size=32,35
|
||||
Collapsed=0
|
||||
|
||||
[Docking][Data]
|
||||
DockSpace ID=0x11111111 Window=0x1BBC0F80 Pos=0,19 Size=1280,701 Split=X
|
||||
DockNode ID=0x00000001 Parent=0x11111111 SizeRef=1121,701 CentralNode=1
|
||||
DockNode ID=0x00000002 Parent=0x11111111 SizeRef=157,701 Selected=0x36DC96AB
|
||||
|
29
src/src/Components/CameraComponent.cpp
Normal file
29
src/src/Components/CameraComponent.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include "CameraComponent.h"
|
||||
#include "../Entitys/Object.h"
|
||||
|
||||
CameraComponent::CameraComponent(Object* owner) : Component(owner) {}
|
||||
|
||||
std::string CameraComponent::GetName() const {
|
||||
return "CameraComponent";
|
||||
}
|
||||
|
||||
void CameraComponent::Save(YAML::Emitter& out) const {
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "type" << YAML::Value << GetName();
|
||||
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;
|
||||
}
|
||||
|
||||
void CameraComponent::Load(const YAML::Node& node) {
|
||||
if (node["fov"]) fov = node["fov"].as<float>();
|
||||
if (node["aspect"]) aspect = node["aspect"].as<float>();
|
||||
if (node["zoom"]) zoom = node["zoom"].as<float>();
|
||||
if (node["primary"]) primary = node["primary"].as<bool>();
|
||||
|
||||
|
||||
}
|
35
src/src/Components/CameraComponent.h
Normal file
35
src/src/Components/CameraComponent.h
Normal file
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
#include "Component.h"
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class CameraComponent : public Component {
|
||||
public:
|
||||
CameraComponent(Object* owner);
|
||||
|
||||
std::string GetName() const override;
|
||||
void Save(YAML::Emitter& out) const override;
|
||||
void Load(const YAML::Node& node) override;
|
||||
|
||||
|
||||
|
||||
float GetZoom() const { return zoom; }
|
||||
float GetAspect() const { return aspect; }
|
||||
float GetFOV() const { return fov; }
|
||||
bool IsPrimary() const { return primary; }
|
||||
|
||||
|
||||
void SetZoom(float z) { zoom = z; }
|
||||
void SetAspect(float a) { aspect = a; }
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
|
@ -11,7 +11,7 @@ unsigned int SpriteComponent::LoadTexture(const std::string& path) {
|
||||
stbi_set_flip_vertically_on_load(1);
|
||||
unsigned char* data = stbi_load(path.c_str(), &w, &h, &channels, 4);
|
||||
if (!data) {
|
||||
std::cerr << "Failed to load image: " << path << std::endl;
|
||||
std::cerr << "Failed to load image: " << path << "\nReason: " << stbi_failure_reason() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -33,6 +33,7 @@ unsigned int SpriteComponent::LoadTexture(const std::string& path) {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
void SpriteComponent::SetTexture(const std::string& path) {
|
||||
texturePath = path;
|
||||
textureID = LoadTexture(path);
|
||||
|
@ -2,6 +2,8 @@
|
||||
#include "Entitys/Object.h"
|
||||
#include "Renderer.h"
|
||||
#include "components/SpriteComponent.h"
|
||||
#include "components/CameraComponent.h"
|
||||
|
||||
#include "utils/FileDialog.h"
|
||||
|
||||
#include <GL/glew.h>
|
||||
@ -10,6 +12,7 @@
|
||||
#include <imgui_impl_glfw.h>
|
||||
#include <imgui_impl_opengl3.h>
|
||||
|
||||
#include <functional>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
@ -20,12 +23,18 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#include <commdlg.h> // includes GetOpenFileNameA
|
||||
#include <commdlg.h>
|
||||
#include <openssl/sha.h>
|
||||
|
||||
static std::vector<std::shared_ptr<Object>> objects;
|
||||
static std::shared_ptr<Object> selected = nullptr;
|
||||
static bool playing = false;
|
||||
|
||||
static glm::vec2 cameraPos = {0, 0};
|
||||
static float cameraZoom = 1.0f;
|
||||
static bool isDragging = false;
|
||||
static ImVec2 lastMousePos = {};
|
||||
|
||||
GLFWwindow *window = nullptr;
|
||||
|
||||
Engine::Engine()
|
||||
@ -104,8 +113,6 @@ void Engine::Run()
|
||||
|
||||
ImGui::Begin("Scene Tree");
|
||||
|
||||
|
||||
|
||||
if (ImGui::BeginPopupContextWindow("SceneTreeContext", ImGuiPopupFlags_MouseButtonRight))
|
||||
{
|
||||
if (ImGui::MenuItem("Create New Object"))
|
||||
@ -128,15 +135,10 @@ void Engine::Run()
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
for (auto &obj : objects)
|
||||
if (!obj->GetParent()) // Only draw root nodes
|
||||
DrawObjectNode(obj);
|
||||
|
||||
|
||||
|
||||
ImGui::End();
|
||||
|
||||
ImGui::Begin("Inspector");
|
||||
@ -151,13 +153,22 @@ void Engine::Run()
|
||||
if (ImGui::DragFloat2("Position", &pos.x))
|
||||
selected->SetLocalPosition(pos);
|
||||
|
||||
// Add component
|
||||
ImGui::InputInt("Layer", &selected->layer);
|
||||
|
||||
// Add SpriteComponent
|
||||
if (ImGui::Button("Add SpriteComponent"))
|
||||
{
|
||||
if (!selected->GetComponent<SpriteComponent>())
|
||||
selected->AddComponent<SpriteComponent>();
|
||||
}
|
||||
|
||||
// Add CameraComponent
|
||||
if (ImGui::Button("Add CameraComponent"))
|
||||
{
|
||||
if (!selected->GetComponent<CameraComponent>())
|
||||
selected->AddComponent<CameraComponent>();
|
||||
}
|
||||
|
||||
// Show SpriteComponent UI
|
||||
if (auto sprite = selected->GetComponent<SpriteComponent>())
|
||||
{
|
||||
@ -188,6 +199,29 @@ void Engine::Run()
|
||||
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
|
||||
{
|
||||
@ -195,21 +229,71 @@ void Engine::Run()
|
||||
}
|
||||
ImGui::End();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Viewport
|
||||
ImGui::Begin("Viewport", nullptr, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
|
||||
ImVec2 size = ImGui::GetContentRegionAvail();
|
||||
Renderer::Resize((int)size.x, (int)size.y);
|
||||
Renderer::Begin();
|
||||
|
||||
for (auto &obj : objects)
|
||||
if (!playing && ImGui::IsWindowHovered())
|
||||
{
|
||||
if (ImGui::IsMouseDragging(ImGuiMouseButton_Right))
|
||||
{
|
||||
ImVec2 mouseDelta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right);
|
||||
ImGui::ResetMouseDragDelta(ImGuiMouseButton_Right);
|
||||
cameraPos -= glm::vec2(mouseDelta.x, mouseDelta.y) / cameraZoom;
|
||||
}
|
||||
|
||||
float wheel = ImGui::GetIO().MouseWheel;
|
||||
if (wheel != 0.0f)
|
||||
{
|
||||
float zoomFactor = (wheel > 0) ? 1.1f : 1.0f / 1.1f;
|
||||
|
||||
// Get mouse position in screen space
|
||||
ImVec2 mousePos = ImGui::GetMousePos();
|
||||
ImVec2 windowPos = ImGui::GetWindowPos();
|
||||
ImVec2 contentRegionMin = ImGui::GetWindowContentRegionMin();
|
||||
glm::vec2 relativeMouse = glm::vec2(mousePos.x - windowPos.x - contentRegionMin.x,
|
||||
mousePos.y - windowPos.y - contentRegionMin.y);
|
||||
|
||||
// Convert to world position before zoom
|
||||
glm::vec2 beforeZoom = (relativeMouse - glm::vec2(size.x / 2, size.y / 2)) / cameraZoom + cameraPos;
|
||||
|
||||
// Apply zoom
|
||||
cameraZoom *= zoomFactor;
|
||||
cameraZoom = std::clamp(cameraZoom, 0.1f, 10.0f);
|
||||
|
||||
// Convert to world position after zoom
|
||||
glm::vec2 afterZoom = (relativeMouse - glm::vec2(size.x / 2, size.y / 2)) / cameraZoom + cameraPos;
|
||||
|
||||
// Offset camera so zoom is centered at mouse
|
||||
cameraPos += beforeZoom - afterZoom;
|
||||
}
|
||||
}
|
||||
|
||||
Renderer::Resize((int)size.x, (int)size.y);
|
||||
Renderer::Begin(); // assumes orthographic matrix is set up here
|
||||
|
||||
std::vector<std::shared_ptr<Object>> toDraw;
|
||||
|
||||
std::function<void(const std::shared_ptr<Object> &)> collect = [&](const std::shared_ptr<Object> &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<SpriteComponent>())
|
||||
{
|
||||
Renderer::DrawSprite(sprite.get(), obj->GetWorldPosition());
|
||||
glm::vec2 worldPos = obj->GetWorldPosition();
|
||||
Renderer::DrawSprite(sprite.get(), worldPos, cameraZoom, cameraPos);
|
||||
}
|
||||
}
|
||||
|
||||
@ -218,17 +302,20 @@ void Engine::Run()
|
||||
ImGui::Image((ImTextureID)(uintptr_t)texID, size, ImVec2(0, 1), ImVec2(1, 0));
|
||||
ImGui::End();
|
||||
|
||||
if (ImGui::BeginPopup("RenameObject")) {
|
||||
if (ImGui::BeginPopup("RenameObject"))
|
||||
{
|
||||
static char nameBuffer[128];
|
||||
static bool once = true;
|
||||
|
||||
if (once && selected) {
|
||||
if (once && selected)
|
||||
{
|
||||
strcpy(nameBuffer, selected->GetName().c_str());
|
||||
once = false;
|
||||
}
|
||||
|
||||
ImGui::InputText("##rename", nameBuffer, sizeof(nameBuffer));
|
||||
if (ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::Button("OK")) {
|
||||
if (ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::Button("OK"))
|
||||
{
|
||||
if (selected)
|
||||
selected->SetName(nameBuffer);
|
||||
ImGui::CloseCurrentPopup();
|
||||
@ -236,7 +323,8 @@ void Engine::Run()
|
||||
}
|
||||
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Cancel")) {
|
||||
if (ImGui::Button("Cancel"))
|
||||
{
|
||||
ImGui::CloseCurrentPopup();
|
||||
once = true;
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "Object.h"
|
||||
#include "../Components/SpriteComponent.h"
|
||||
#include "../Components/Component.h"
|
||||
|
||||
#include "../Components/SpriteComponent.h"
|
||||
#include "../Components/CameraComponent.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
@ -49,79 +49,48 @@ void Object::SetName(const std::string& n) { name = n; }
|
||||
std::vector<std::shared_ptr<Object>>& Object::GetChildren() { return children; }
|
||||
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 {
|
||||
out << YAML::BeginMap;
|
||||
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 << "layer" << YAML::Value << layer;
|
||||
out << YAML::Key << "components" << YAML::Value << YAML::BeginSeq;
|
||||
for (const auto& comp : components) {
|
||||
for (const auto& comp : components)
|
||||
comp->Save(out);
|
||||
}
|
||||
out << YAML::EndSeq;
|
||||
|
||||
out << YAML::Key << "children" << YAML::Value << YAML::BeginSeq;
|
||||
for (const auto& child : children)
|
||||
child->Save(out);
|
||||
out << YAML::EndSeq;
|
||||
|
||||
out << YAML::EndMap;
|
||||
}
|
||||
|
||||
void Object::Load(const YAML::Node& node) {
|
||||
name = node["name"].as<std::string>();
|
||||
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"]) {
|
||||
for (const auto& compNode : node["components"]) {
|
||||
std::string type = compNode["type"].as<std::string>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
children.clear();
|
||||
if (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);
|
||||
AddChild(child);
|
||||
}
|
||||
|
@ -1,30 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include "../Components/Component.h"
|
||||
class Component;
|
||||
|
||||
class Object {
|
||||
public:
|
||||
Object(const std::string& name);
|
||||
~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;
|
||||
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;
|
||||
void AddChild(std::shared_ptr<Object> child);
|
||||
void RemoveChild(Object* child);
|
||||
std::vector<std::shared_ptr<Object>>& GetChildren();
|
||||
|
||||
template<typename T>
|
||||
std::shared_ptr<T> GetComponent() const;
|
||||
@ -38,8 +38,8 @@ public:
|
||||
void Save(YAML::Emitter& out) const;
|
||||
void Load(const YAML::Node& node);
|
||||
|
||||
int id;
|
||||
bool selected = false;
|
||||
int id = 0;
|
||||
int layer = 0;
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
@ -48,3 +48,30 @@ private:
|
||||
std::vector<std::shared_ptr<Object>> children;
|
||||
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,12 +92,29 @@ void Renderer::End() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos) {
|
||||
//void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float scale) {
|
||||
// GLuint tex = sprite->GetTextureID();
|
||||
// if (!tex) return;
|
||||
//
|
||||
// glBindTexture(GL_TEXTURE_2D, tex);
|
||||
// glBegin(GL_QUADS);
|
||||
// float size = 100.0f * scale;
|
||||
//
|
||||
// glTexCoord2f(0, 0); glVertex2f(pos.x, pos.y);
|
||||
// glTexCoord2f(1, 0); glVertex2f(pos.x + size, pos.y);
|
||||
// glTexCoord2f(1, 1); glVertex2f(pos.x + size, pos.y + size);
|
||||
// glTexCoord2f(0, 1); glVertex2f(pos.x, pos.y + size);
|
||||
// glEnd();
|
||||
//}
|
||||
|
||||
void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float zoom, glm::vec2& CameraPos) {
|
||||
if (!sprite || sprite->GetTextureID() == 0) return;
|
||||
|
||||
spriteShader.Use();
|
||||
spriteShader.SetVec2("uPos", pos);
|
||||
spriteShader.SetVec2("uSize", sprite->GetSize());
|
||||
glm::vec2 size = sprite->GetSize();
|
||||
glm::vec2 screenPos = (pos - CameraPos) * zoom + glm::vec2(size.x / 2, size.y / 2);
|
||||
spriteShader.SetVec2("uPos", screenPos);
|
||||
spriteShader.SetVec2("uSize", size*zoom);
|
||||
spriteShader.SetVec2("uScreen", glm::vec2(width, height));
|
||||
spriteShader.SetInt("uTex", 0);
|
||||
|
||||
@ -109,6 +126,41 @@ void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos) {
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
|
||||
void Renderer::DrawEditorGrid(const glm::vec2& cameraPos, float zoom) {
|
||||
glUseProgram(0);
|
||||
glColor4f(0.5f, 0.5f, 0.5f, 0.25f);
|
||||
glLineWidth(1.0f);
|
||||
|
||||
glBegin(GL_LINES);
|
||||
float spacing = 100.0f;
|
||||
float viewWidth = width / zoom;
|
||||
float viewHeight = height / zoom;
|
||||
|
||||
float left = cameraPos.x - viewWidth / 2;
|
||||
float right = cameraPos.x + viewWidth / 2;
|
||||
float bottom = cameraPos.y + viewHeight / 2;
|
||||
float top = cameraPos.y - viewHeight / 2;
|
||||
|
||||
int minX = static_cast<int>(std::floor(left / spacing)) * spacing;
|
||||
int maxX = static_cast<int>(std::ceil(right / spacing)) * spacing;
|
||||
int minY = static_cast<int>(std::floor(top / spacing)) * spacing;
|
||||
int maxY = static_cast<int>(std::ceil(bottom / spacing)) * spacing;
|
||||
|
||||
for (int x = minX; x <= maxX; x += spacing) {
|
||||
glVertex2f((float)x, top);
|
||||
glVertex2f((float)x, bottom);
|
||||
}
|
||||
|
||||
for (int y = minY; y <= maxY; y += spacing) {
|
||||
glVertex2f(left, (float)y);
|
||||
glVertex2f(right, (float)y);
|
||||
}
|
||||
|
||||
glEnd();
|
||||
}
|
||||
|
||||
|
||||
GLuint Renderer::GetRenderTexture() {
|
||||
return textureColorBuffer;
|
||||
}
|
||||
|
@ -10,7 +10,8 @@ public:
|
||||
static void Resize(int w, int h);
|
||||
static void Begin();
|
||||
static void End();
|
||||
static void DrawSprite(SpriteComponent* sprite, const glm::vec2& pos);
|
||||
static void DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float zoom, glm::vec2& CameraPos);
|
||||
static void DrawEditorGrid(const glm::vec2& cameraPos, float zoom);
|
||||
static GLuint GetRenderTexture();
|
||||
static glm::ivec2 GetSize();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user