Compare commits

..

No commits in common. "1f7fceb9b585dddebdd6c3e5c69eb2b3f6f16287" and "7945ff246be89a60200cf90a9b811f5f9733cb5c" have entirely different histories.

11 changed files with 97 additions and 380 deletions

View File

@ -1,27 +1,6 @@
[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 [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
24 | #define NOMINMAX [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
| ^~~~~~~~
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;
| ^~~~~~~~~~
[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: 1.23s
[ERROR] Runtime crash
[TIME] Build duration: 6.12s Command 'src\build\app.exe' returned non-zero exit status 3221226356.
[RUN] Executed app.exe successfully.
[TIME] Total runtime: 72.36s

View File

@ -1,41 +1,12 @@
engine_version: 0.1.0 engine_version: 0.1.0
scene_name: test scene_name: test
scene_hash: b3488ddfbb343e554cb1782c1b79598e15545d752cfec63be97f90c8d422aaf5 scene_hash: 2bef506b8552068aab28115edf9550411c1fab67befdc32a4946ec33da7a8021
format_version: 1 format_version: 1
objects: objects:
- name: Sprite Object - name: Face Image
position: [438, 0] position: [0, 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: "" normalMap: C:\Users\spenc\OneDrive\Pictures\49555.jpg
children: [] children: []

View File

@ -1,31 +0,0 @@
[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

View File

@ -1,29 +0,0 @@
#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>();
}

View File

@ -1,35 +0,0 @@
#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;
};

View File

@ -11,11 +11,11 @@ unsigned int SpriteComponent::LoadTexture(const std::string& path) {
stbi_set_flip_vertically_on_load(1); stbi_set_flip_vertically_on_load(1);
unsigned char* data = stbi_load(path.c_str(), &w, &h, &channels, 4); unsigned char* data = stbi_load(path.c_str(), &w, &h, &channels, 4);
if (!data) { if (!data) {
std::cerr << "Failed to load image: " << path << "\nReason: " << stbi_failure_reason() << std::endl; std::cerr << "Failed to load image: " << path << std::endl;
return 0; return 0;
} }
size = glm::vec2(w, h); size = glm::vec2(w,h);
unsigned int id; unsigned int id;
glGenTextures(1, &id); glGenTextures(1, &id);
@ -33,7 +33,6 @@ unsigned int SpriteComponent::LoadTexture(const std::string& path) {
return id; return id;
} }
void SpriteComponent::SetTexture(const std::string& path) { void SpriteComponent::SetTexture(const std::string& path) {
texturePath = path; texturePath = path;
textureID = LoadTexture(path); textureID = LoadTexture(path);

View File

@ -2,8 +2,6 @@
#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>
@ -12,7 +10,6 @@
#include <imgui_impl_glfw.h> #include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h> #include <imgui_impl_opengl3.h>
#include <functional>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
@ -23,18 +20,12 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#define NOMINMAX #define NOMINMAX
#include <windows.h> #include <windows.h>
#include <commdlg.h> #include <commdlg.h> // includes GetOpenFileNameA
#include <openssl/sha.h> #include <openssl/sha.h>
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;
static glm::vec2 cameraPos = {0, 0};
static float cameraZoom = 1.0f;
static bool isDragging = false;
static ImVec2 lastMousePos = {};
GLFWwindow *window = nullptr; GLFWwindow *window = nullptr;
Engine::Engine() Engine::Engine()
@ -113,6 +104,8 @@ 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"))
@ -135,10 +128,15 @@ 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");
@ -153,22 +151,13 @@ void Engine::Run()
if (ImGui::DragFloat2("Position", &pos.x)) if (ImGui::DragFloat2("Position", &pos.x))
selected->SetLocalPosition(pos); selected->SetLocalPosition(pos);
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>())
{ {
@ -199,29 +188,6 @@ 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
{ {
@ -229,71 +195,21 @@ 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 && 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::Resize((int)size.x, (int)size.y);
Renderer::Begin(); // assumes orthographic matrix is set up here Renderer::Begin();
std::vector<std::shared_ptr<Object>> toDraw; for (auto &obj : objects)
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>()) if (auto sprite = obj->GetComponent<SpriteComponent>())
{ {
glm::vec2 worldPos = obj->GetWorldPosition(); Renderer::DrawSprite(sprite.get(), obj->GetWorldPosition());
Renderer::DrawSprite(sprite.get(), worldPos, cameraZoom, cameraPos);
} }
} }
@ -302,20 +218,17 @@ void Engine::Run()
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();
@ -323,8 +236,7 @@ void Engine::Run()
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Cancel")) if (ImGui::Button("Cancel")) {
{
ImGui::CloseCurrentPopup(); ImGui::CloseCurrentPopup();
once = true; once = true;
} }

View File

@ -1,7 +1,7 @@
#include "Object.h" #include "Object.h"
#include "../Components/Component.h"
#include "../Components/SpriteComponent.h" #include "../Components/SpriteComponent.h"
#include "../Components/CameraComponent.h" #include "../Components/Component.h"
#include <algorithm> #include <algorithm>
@ -49,48 +49,79 @@ 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"];
if (pos && pos.IsSequence() && pos.size() == 2) { localPosition = { pos[0].as<float>(), pos[1].as<float>() };
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<SpriteComponent>(); auto comp = AddComponent<class 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>("Child"); auto child = std::make_shared<Object>("(loaded)");
child->Load(childNode); child->Load(childNode);
AddChild(child); AddChild(child);
} }

View File

@ -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>
class Component; #include "../Components/Component.h"
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);
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(); std::vector<std::shared_ptr<Object>>& GetChildren();
Object* GetParent() const;
template<typename T> template<typename T>
std::shared_ptr<T> GetComponent() const; std::shared_ptr<T> GetComponent() const;
@ -38,8 +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 = 0; int id;
int layer = 0; bool selected = false;
private: private:
std::string name; std::string name;
@ -48,30 +48,3 @@ 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());
}

View File

@ -92,29 +92,12 @@ void Renderer::End() {
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
//void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float scale) { void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos) {
// 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; if (!sprite || sprite->GetTextureID() == 0) return;
spriteShader.Use(); spriteShader.Use();
glm::vec2 size = sprite->GetSize(); spriteShader.SetVec2("uPos", pos);
glm::vec2 screenPos = (pos - CameraPos) * zoom + glm::vec2(size.x / 2, size.y / 2); spriteShader.SetVec2("uSize", sprite->GetSize());
spriteShader.SetVec2("uPos", screenPos);
spriteShader.SetVec2("uSize", size*zoom);
spriteShader.SetVec2("uScreen", glm::vec2(width, height)); spriteShader.SetVec2("uScreen", glm::vec2(width, height));
spriteShader.SetInt("uTex", 0); spriteShader.SetInt("uTex", 0);
@ -126,41 +109,6 @@ void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float z
glBindVertexArray(0); 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() { GLuint Renderer::GetRenderTexture() {
return textureColorBuffer; return textureColorBuffer;
} }

View File

@ -10,8 +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, float zoom, glm::vec2& CameraPos); static void DrawSprite(SpriteComponent* sprite, const glm::vec2& pos);
static void DrawEditorGrid(const glm::vec2& cameraPos, float zoom);
static GLuint GetRenderTexture(); static GLuint GetRenderTexture();
static glm::ivec2 GetSize(); static glm::ivec2 GetSize();