From 9a6000af36d36a88accdcbbb0bd9df8765fb7abd Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Mon, 11 Jul 2022 20:12:17 -0400 Subject: [PATCH] In process of creating the orthographic camera and camera component In process of Implementing world rendering and the world view panel in the editor --- CMakeLists.txt | 1 + docs/tasks/core.todo | 199 +++++++++--------- docs/tasks/editor.todo | 1 + src/core/types.h | 8 + src/graphics/camera.h | 20 -- src/graphics/orthographic_camera.cpp | 72 +++++++ src/graphics/orthographic_camera.h | 47 +++++ src/gui/panel.cpp | 3 +- src/run_modes/editor/contents/World.cpp | 2 +- src/run_modes/editor/panels/asset_browser.cpp | 4 +- src/run_modes/editor/panels/world_view.cpp | 42 +++- src/run_modes/editor/panels/world_view.h | 12 +- src/world/components.h | 14 ++ src/world/world.cpp | 32 +-- src/world/world.h | 17 +- 15 files changed, 325 insertions(+), 149 deletions(-) delete mode 100644 src/graphics/camera.h create mode 100644 src/graphics/orthographic_camera.cpp create mode 100644 src/graphics/orthographic_camera.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fdf7eef..4b1d9df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,7 @@ set(LUNARIUM_SRC "src/graphics/opengl/glGraphics.cpp" "src/graphics/opengl/glText.cpp" "src/graphics/opengl/glShader.cpp" +"src/graphics/orthographic_camera.cpp" "src/gui/gui.cpp" "src/gui/imgui_ext.cpp" "src/gui/panel.cpp" diff --git a/docs/tasks/core.todo b/docs/tasks/core.todo index c9d0d2a..0090c14 100644 --- a/docs/tasks/core.todo +++ b/docs/tasks/core.todo @@ -31,77 +31,96 @@ Core: ✔ Add run mode interface class @done (9/15/2021, 8:22:35 PM) ✔ Read the window size and position on shutdown and write these to the state file @done (2/8/2022, 4:39:37 PM) - Graphics: - ☐ Move the openGL reference out of the Image class (OpenGL ID) and make the ID more generic - ☐ Add layer to interface API for setting the Images ID in a generic way - ☐ Implement batch rendering - ☐ Allow vertices to be submitted before rendering - ☐ Add texture sampler id to the vertex layout - ✔ Decide on a font/text rendering system @done (9/7/2021, 1:39:53 PM) - ✔ Add FreeType to the project @done (9/7/2021, 2:23:13 PM) - ✔ Add a new class for font loading/management and text rendering @done (9/7/2021, 3:57:08 PM) - ✔ Make the text renderer smarter about breaking up words on multiple lines @low @done (9/8/2021, 2:23:03 PM) - ✔ Implement the Image creation methods @done (9/9/2021, 2:50:20 PM) - ✔ Implement Render to Texture @done (9/15/2021, 7:00:33 PM) - ✔ Adjust the font loading code to use the binary file buffer instead of ifstream @done (9/17/2021, 6:11:06 PM) - ✔ Find a way to add rotation to shapes and images @done (10/29/2021, 7:35:14 PM) - ✔ Add a DrawPolygon method that takes vertices and draws arbirary shapes @done (10/29/2021, 6:24:14 PM) - ☐ Allow DrawPolygon to add a texture to the polygon @low - ✔ Refactor the drawing code to allow for rotation with cleaner code @high @done (10/29/2021, 8:36:24 PM) - ✔ Test rotation of images @done (11/1/2021, 2:11:13 PM) - ✔ Fix line rotation @low @done (2/8/2022, 4:39:25 PM) - ✔ Add Roboto-Regular.ttf as an internal font @high @done (11/3/2021, 8:35:51 PM) - ✔ Allow an image size to be passed in for rendering to an image @high @done (2/3/2022, 4:07:33 PM) - - Scripting: - ☐ Switch to Wren instead of LUA (https://github.com/wren-lang/wren) @high - ☐ Remove SOL - - Script Managment class: - ✘ Manage LUA states @cancelled(22-05-13 17:31) - ☐ Initialize new scripts - ☐ Run given script with given state - ☐ Add any generated errors to the Script object +Graphics: + ☐ Add view matrix to the shader @critical + ☐ Remove projection matrix from the renderer internals @critical + ☐ Move the openGL reference out of the Image class (OpenGL ID) and make the ID more generic + ☐ Add layer to interface API for setting the Images ID in a generic way + ☐ Implement batch rendering @high + ☐ Allow vertices to be submitted before rendering + ☐ Add texture sampler id to the vertex layout + ✔ Decide on a font/text rendering system @done (9/7/2021, 1:39:53 PM) + ✔ Add FreeType to the project @done (9/7/2021, 2:23:13 PM) + ✔ Add a new class for font loading/management and text rendering @done (9/7/2021, 3:57:08 PM) + ✔ Make the text renderer smarter about breaking up words on multiple lines @low @done (9/8/2021, 2:23:03 PM) + ✔ Implement the Image creation methods @done (9/9/2021, 2:50:20 PM) + ✔ Implement Render to Texture @done (9/15/2021, 7:00:33 PM) + ✔ Adjust the font loading code to use the binary file buffer instead of ifstream @done (9/17/2021, 6:11:06 PM) + ✔ Find a way to add rotation to shapes and images @done (10/29/2021, 7:35:14 PM) + ✔ Add a DrawPolygon method that takes vertices and draws arbirary shapes @done (10/29/2021, 6:24:14 PM) + ☐ Allow DrawPolygon to add a texture to the polygon @low + ✔ Refactor the drawing code to allow for rotation with cleaner code @high @done (10/29/2021, 8:36:24 PM) + ✔ Test rotation of images @done (11/1/2021, 2:11:13 PM) + ✔ Fix line rotation @low @done (2/8/2022, 4:39:25 PM) + ✔ Add Roboto-Regular.ttf as an internal font @high @done (11/3/2021, 8:35:51 PM) + ✔ Allow an image size to be passed in for rendering to an image @high @done (2/3/2022, 4:07:33 PM) + +GUI: + ☐ Improve the GUI API! + ☐ Implement a better way to handle popup windows and context menus + ✔ Dear ImGui class with basic initialization @done (9/10/2021, 1:42:19 PM) + ✔ Debug log window @done (9/10/2021, 4:44:48 PM) + ✔ Add key to show debug log window @done (9/13/2021, 6:47:44 PM) + ☐ Add checkboxes to disable log categories and levels + ✔ Add LUA Console window @done (10/26/2021, 4:43:41 PM) + ✔ Improve the interfaces for the Lua Editor and Console (partial transparancy for one thing) @high @done(22-06-23 15:54) + FileBrowser: + ✔ Allow opening of listed directories @done (11/8/2021, 3:16:26 PM) + ✔ Add indication that an item is directory @done (11/8/2021, 6:19:20 PM) + ✔ Sort items by type (Directories should come first) @done (11/8/2021, 6:26:01 PM) + ✔ Allow the user to type in a filename @done (11/9/2021, 3:26:16 PM) + ✔ Add a "New Directory" button @done (11/8/2021, 7:15:51 PM) + ✔ Selected files should show up in the text box @done(22-04-18 13:33) + +Scripting: + ☐ Switch to Wren instead of LUA (https://github.com/wren-lang/wren) @high + ☐ Remove SOL - Interface Class (Core API): - ☐ Provide Methods that give access to the C++ code - - ECS: - ✔ Figure out how to serialize Entities @done(22-06-28 14:16) - - Looks like we just need to if check for each component an entity could have... - ✔ Research EnTT for the ECS (https://github.com/skypjack/entt/) @done(22-06-01 14:01) - ✔ Research using ECS with a quadtree (regions may essentially be a quadtree) @done(22-06-01 14:01) - ☐ Use 1 Entt registry with a component that stores the region ID (Index?) - - Enitity: - ✔ Single UUID @done(22-06-01 14:01) - ✔ Functionality for adding/working with components (through EnTT) @done(22-06-01 14:01) - ☐ Serialize - ☐ JSON - ☐ Binary - - Components: - ✔ Tag @done(22-06-23 15:49) - ☐ Transform - ☐ SpriteRenderer - ☐ Animation Controller - ☐ Script - ☐ Audio Listener - Physics: - ☐ Rigid Body (Box2D) - ☐ Box Collider (Box2D) + Script Managment class: + ✘ Manage LUA states @cancelled(22-05-13 17:31) + ☐ Initialize new scripts + ☐ Run given script with given state + ☐ Add any generated errors to the Script object - World (Lunariums version of a "Scene"): - ☐ Implement memento pattern to save the initial state of the world - ☐ Implement running the world and resetting to initial state - ☐ Implement the world without Regions first - ✔ Serialize world @done(22-07-06 18:33) - ✔ JSON @done(22-07-06 18:33) + Interface Class (Core API): + ☐ Provide Methods that give access to the C++ code + +ECS: + ✔ Figure out how to serialize Entities @done(22-06-28 14:16) + - Looks like we just need to if check for each component an entity could have... + ✔ Research EnTT for the ECS (https://github.com/skypjack/entt/) @done(22-06-01 14:01) + ✔ Research using ECS with a quadtree (regions may essentially be a quadtree) @done(22-06-01 14:01) + ☐ Use 1 Entt registry with a component that stores the region ID (Index?) + + Enitity: + ✔ Single UUID @done(22-06-01 14:01) + ✔ Functionality for adding/working with components (through EnTT) @done(22-06-01 14:01) + ☐ Serialize + ☐ JSON ☐ Binary - Implement Regions: - ☐ Track/manage active regions - ☐ Render active Regions + Components: + ✔ Tag @done(22-06-23 15:49) + ☐ Transform + ☐ SpriteRenderer + ☐ Animation Controller + ☐ Script + ☐ Audio Listener + Physics: + ☐ Rigid Body (Box2D) + ☐ Box Collider (Box2D) + +World (Lunariums version of a "Scene"): + ☐ Implement memento pattern to save the initial state of the world + ☐ Implement running the world and resetting to initial state + ☐ Implement the world without Regions first + ✔ Serialize world @done(22-07-06 18:33) + ✔ JSON @done(22-07-06 18:33) + ☐ Binary + + Implement Regions: + ☐ Track/manage active regions + ☐ Render active Regions Region: ☐ List of renderable images for each layer @@ -109,39 +128,21 @@ Core: ☐ Implement image grid within regions [Regions could potentially be split into multiple images (an internal grid). To support larger region sizes without needing single images that are like 1048576x1048576 or some nonsense.] - - GUI: - ☐ Improve the GUI API! - ☐ Implement a better way to handle popup windows and context menus - ✔ Dear ImGui class with basic initialization @done (9/10/2021, 1:42:19 PM) - ✔ Debug log window @done (9/10/2021, 4:44:48 PM) - ✔ Add key to show debug log window @done (9/13/2021, 6:47:44 PM) - ☐ Add checkboxes to disable log categories and levels - ✔ Add LUA Console window @done (10/26/2021, 4:43:41 PM) - ✔ Improve the interfaces for the Lua Editor and Console (partial transparancy for one thing) @high @done(22-06-23 15:54) - FileBrowser: - ✔ Allow opening of listed directories @done (11/8/2021, 3:16:26 PM) - ✔ Add indication that an item is directory @done (11/8/2021, 6:19:20 PM) - ✔ Sort items by type (Directories should come first) @done (11/8/2021, 6:26:01 PM) - ✔ Allow the user to type in a filename @done (11/9/2021, 3:26:16 PM) - ✔ Add a "New Directory" button @done (11/8/2021, 7:15:51 PM) - ✔ Selected files should show up in the text box @done(22-04-18 13:33) - - Input: - ✔ Port over the Element2D input system and adjust it to use glfw @done (9/8/2021, 8:20:07 PM) - ✔ Add the InputManager to the core @done (9/9/2021, 2:57:06 PM) - - Physics: - ✔ Research Box2D as a possible physics library @done (10/27/2021, 7:40:44 PM) - ✔ Add Box2D to the project as an external library @done (10/27/2021, 7:40:46 PM) - ✔ Add a scene to the tester to test Box2D usage @done (10/28/2021, 2:42:45 PM) - ☐ Come up with a way to wrap Box2D into an API - - Audio: - ☐ Research the usage of OpenAL - ☐ Add OpenAL to the project - ☐ Design Audio API +Input: + ✔ Port over the Element2D input system and adjust it to use glfw @done (9/8/2021, 8:20:07 PM) + ✔ Add the InputManager to the core @done (9/9/2021, 2:57:06 PM) + +Physics: + ✔ Research Box2D as a possible physics library @done (10/27/2021, 7:40:44 PM) + ✔ Add Box2D to the project as an external library @done (10/27/2021, 7:40:46 PM) + ✔ Add a scene to the tester to test Box2D usage @done (10/28/2021, 2:42:45 PM) + ☐ Come up with a way to wrap Box2D into an API + +Audio: + ☐ Research the usage of OpenAL + ☐ Add OpenAL to the project + ☐ Design Audio API Utils: diff --git a/docs/tasks/editor.todo b/docs/tasks/editor.todo index 0bf3989..30f3d24 100644 --- a/docs/tasks/editor.todo +++ b/docs/tasks/editor.todo @@ -1,5 +1,6 @@ Editor: + ☐ Design a custom editor style @low ☐ Save and unload world data when a new world is selected ✔ Load the selected world when double clicked on in the asset browser @done(22-07-05 14:29) ✔ Asset Location MUST be relative to the project root directory @critical @done(22-05-20 18:35) diff --git a/src/core/types.h b/src/core/types.h index 285fc4f..44ae27f 100644 --- a/src/core/types.h +++ b/src/core/types.h @@ -27,6 +27,8 @@ namespace lunarium bool operator==(const Vec2& rhs) const { return (this->X == rhs.X && this->Y == rhs.Y); } bool operator!=(const Vec2& rhs) const { return !((*this) == rhs); } + + operator glm::vec3() { return glm::vec3(X, Y, 0.0f); } }; typedef Vec2 Vec2i; @@ -44,6 +46,10 @@ namespace lunarium bool operator==(const Size& rhs) const { return (this->Width == rhs.Width && this->Height == rhs.Height); } bool operator!=(const Size& rhs) const { return !((*this) == rhs); } + + + operator glm::vec3() { return glm::vec3(Width, Height, 0.0f); } + operator glm::vec2() { return glm::vec2(Width, Height, 0.0f); } }; typedef Size Sizei; @@ -72,6 +78,8 @@ namespace lunarium Color(); Color(float _r, float _g, float _b, float _a = 1.0f); + operator glm::vec4() { return glm::vec4(R, G, B, A); } + static Color White(); static Color Black(); static Color Red(); diff --git a/src/graphics/camera.h b/src/graphics/camera.h deleted file mode 100644 index dd60067..0000000 --- a/src/graphics/camera.h +++ /dev/null @@ -1,20 +0,0 @@ -/****************************************************************************** -* File - camera.h -* Author - Joey Pollack -* Date - 2022/01/25 (y/m/d) -* Mod Date - 2022/01/25 (y/m/d) -* Description - A 2D camera to be used with the World -******************************************************************************/ - -#ifndef CAMERA_H_ -#define CAMERA_H_ - -namespace lunarium -{ - class Camera - { - // TODO: class Camera - }; -} - -#endif // CAMERA_H_ \ No newline at end of file diff --git a/src/graphics/orthographic_camera.cpp b/src/graphics/orthographic_camera.cpp new file mode 100644 index 0000000..b340971 --- /dev/null +++ b/src/graphics/orthographic_camera.cpp @@ -0,0 +1,72 @@ +/****************************************************************************** +* File - orthographic_camera.cpp +* Author - Joey Pollack +* Date - 2022/01/25 (y/m/d) +* Mod Date - 2022/01/25 (y/m/d) +* Description - A 2D camera to be used with the World +******************************************************************************/ + +#include "orthographic_camera.h" +#include + +namespace lunarium +{ + OrthographicCamera::OrthographicCamera(Vec2f position, Sizef viewport_size) + : mPosition(position), mRotation(0.0f), mViewportSize(viewport_size), mView(1.0f) + { + RecalculateView(); + } + + void OrthographicCamera::MoveLeft(float amt) + { + mPosition.X += amt; + RecalculateView(); + } + + void OrthographicCamera::MoveUp(float amt) + { + mPosition.Y += amt; + RecalculateView(); + } + + void OrthographicCamera::Rotate(float amt) + { + mRotation += amt; + RecalculateView(); + } + + void OrthographicCamera::SetPosition(Vec2f position) + { + mPosition = position; + RecalculateView(); + } + + void OrthographicCamera::SetRotation(float rot) + { + mRotation = rot; + RecalculateView(); + } + + glm::mat4 OrthographicCamera::GetViewProjection() + { + return mViewProj; + } + + void OrthographicCamera::SetViewportSize(Sizef size) + { + mViewportSize = size; + RecalculateView(); + } + + void OrthographicCamera::RecalculateView() + { + mProjection = glm::ortho(0.0f, mViewportSize.Width, mViewportSize.Height, 0.0f, -1.0f, 1.0f); + + glm::mat4 transform = glm::translate(glm::mat4(1.0f), (glm::vec3)mPosition); + transform *= glm::rotate(glm::mat4(1.0f), glm::radians(mRotation), glm::vec3(0, 0, 1)); + + mView = glm::inverse(transform); + mViewProj = mProjection * mView; + } + +} \ No newline at end of file diff --git a/src/graphics/orthographic_camera.h b/src/graphics/orthographic_camera.h new file mode 100644 index 0000000..a96f41c --- /dev/null +++ b/src/graphics/orthographic_camera.h @@ -0,0 +1,47 @@ +/****************************************************************************** +* File - orthographic_camera.h +* Author - Joey Pollack +* Date - 2022/01/25 (y/m/d) +* Mod Date - 2022/01/25 (y/m/d) +* Description - A 2D camera to be used with the World +******************************************************************************/ + +#ifndef CAMERA_H_ +#define CAMERA_H_ + +#include + +namespace lunarium +{ + class OrthographicCamera + { + public: + OrthographicCamera(Vec2f position = { 0.0f, 0.0f }, Sizef viewport_size = {0.0f, 0.0f }); + + void MoveLeft(float amt); + void MoveUp(float amt); + void Rotate(float amt); + + void SetPosition(Vec2f position); + void SetRotation(float rot); + + void SetViewportSize(Sizef size); + + glm::mat4 GetViewProjection(); + + private: + void RecalculateView(); + + private: + + Vec2f mPosition; + float mRotation; + Sizef mViewportSize; + + glm::mat4 mProjection; + glm::mat4 mView; + glm::mat4 mViewProj; + }; +} + +#endif // CAMERA_H_ \ No newline at end of file diff --git a/src/gui/panel.cpp b/src/gui/panel.cpp index ea73cf1..e33480a 100644 --- a/src/gui/panel.cpp +++ b/src/gui/panel.cpp @@ -13,7 +13,8 @@ namespace lunarium { Panel::Panel(std::string name, PanelDockZone dock_zone, bool isOpen, int window_flags) - : mIsOpen(isOpen), mPanelName(name), mDockZone(dock_zone), mWindowFlags(window_flags) + : mIsOpen(isOpen), mPanelName(name), mDockZone(dock_zone), mWindowFlags(window_flags), + mX(-1), mY(-1), mWidth(-1), mHeight(-1) { } diff --git a/src/run_modes/editor/contents/World.cpp b/src/run_modes/editor/contents/World.cpp index ddb1db9..97016b6 100644 --- a/src/run_modes/editor/contents/World.cpp +++ b/src/run_modes/editor/contents/World.cpp @@ -17,7 +17,7 @@ namespace lunarium { namespace editor { World::World(std::filesystem::path name) - : EditorAsset(AssetType::EATYPE_WORLD), mpWorld(new lunarium::World) + : EditorAsset(AssetType::EATYPE_WORLD), mpWorld(new lunarium::World(name.stem().string())) { mLocation = name.string() + ".wld"; } diff --git a/src/run_modes/editor/panels/asset_browser.cpp b/src/run_modes/editor/panels/asset_browser.cpp index 04e8438..8db7ccd 100644 --- a/src/run_modes/editor/panels/asset_browser.cpp +++ b/src/run_modes/editor/panels/asset_browser.cpp @@ -183,7 +183,7 @@ namespace editor if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) { // TODO: Open relevant editor - Logger::Info(Editor::LogCat, "Directory double clicked on. Open Folder"); + //Logger::Info(Editor::LogCat, "Directory double clicked on. Open Folder"); // This will force open the tree nodes on the next update so the tree is synced with the content window mSelectedDir = dir_entry.path(); @@ -203,7 +203,7 @@ namespace editor if (ImGui::Selectable((*iter)->GetFileLocation().stem().string().c_str())) { // TODO: Show properties if this is new selection (meaning wasn't selected last frame) - Logger::Info(Editor::LogCat, "Asset selected. Properties shold show in the PropertiesView Panel"); + // Logger::Info(Editor::LogCat, "Asset selected. Properties shold show in the PropertiesView Panel"); } if (ImGui::BeginDragDropSource()) { diff --git a/src/run_modes/editor/panels/world_view.cpp b/src/run_modes/editor/panels/world_view.cpp index fcd9d8c..a2faa90 100644 --- a/src/run_modes/editor/panels/world_view.cpp +++ b/src/run_modes/editor/panels/world_view.cpp @@ -7,6 +7,9 @@ ******************************************************************************/ #include "world_view.h" +#include +#include +#include #include #include #include @@ -16,13 +19,48 @@ namespace lunarium { namespace editor { WorldView::WorldView() - : Panel("World View", PanelDockZone::DDZ_CENTER, true, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar), mpWorld(nullptr) + : Panel("World View", PanelDockZone::DDZ_CENTER, true, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar), mpWorld(nullptr), + mFrameBuffer(-1), mPrevWidth(0), mPrevHeight(0), mpCanvasImage(nullptr) { } + void WorldView::Update(float delta) + { + // Need to remake the frame buffer if the window size has changed + int width, height; + GetSize(width, height); + + if ((width > 0 && height > 0) && (mPrevWidth != width || mPrevHeight != height)) + { + mPrevWidth = width; + mPrevHeight = height; + + if (mFrameBuffer > -1) + { + Core::Graphics().DestroyRenderTexture(mFrameBuffer); + } + + mFrameBuffer = Core::Graphics().CreateRenderTexture(width, height, 4); + } + + // TODO: Handle view navigation input + + // Render the current state of the world + if (mpWorld) + { + Core::GetInstance().BeginRenderToTexture(mFrameBuffer).LogIfFailed(Editor::LogCat); + mpWorld->Render(&Core::Graphics()); + mpCanvasImage = Core::GetInstance().EndRenderToTexture(); + } + } + void WorldView::DoFrame() { - + if (mpCanvasImage) + { + ImGui::Image((ImTextureID)mpCanvasImage->GetGLTextureID64(), + ImVec2(mpCanvasImage->GetWidth(), mpCanvasImage->GetHeight()), ImVec2(0, 1), ImVec2(1, 0)); + } } }} \ No newline at end of file diff --git a/src/run_modes/editor/panels/world_view.h b/src/run_modes/editor/panels/world_view.h index 9729700..e471e4c 100644 --- a/src/run_modes/editor/panels/world_view.h +++ b/src/run_modes/editor/panels/world_view.h @@ -13,8 +13,8 @@ namespace lunarium { - - class World; // TODO: Use the editor::World class instead? + class Image; + class World; namespace editor { @@ -25,12 +25,18 @@ namespace editor WorldView(); void DoFrame(); + void Update(float delta) override; + void SetWorld(World* pWorld); World* GetWorld(); private: - World* mpWorld; + lunarium::World* mpWorld; Editor* mpEditor; + int mPrevWidth; + int mPrevHeight; + int mFrameBuffer; + Image* mpCanvasImage; }; }} // lunarium::editor diff --git a/src/world/components.h b/src/world/components.h index 27ffc05..0cfba38 100644 --- a/src/world/components.h +++ b/src/world/components.h @@ -16,6 +16,8 @@ #define GLM_ENABLE_EXPERIMENTAL #include +#include + #include namespace lunarium @@ -29,6 +31,8 @@ namespace lunarium Info.reserve(256); Info.insert(0, _info); } + + TagComponent(const TagComponent&) = default; }; struct TransformComponent @@ -43,6 +47,8 @@ namespace lunarium } + TransformComponent(const TransformComponent&) = default; + glm::mat4 GetTransform() { // Quaternion code taken from Hazel engine @@ -50,6 +56,14 @@ namespace lunarium return (glm::translate(glm::mat4(1.0f), Position) * glm::toMat4(glm::quat(Rotation)) * glm::scale(glm::mat4(1.0f), Scale )); } }; + + struct CameraComponent + { + OrthographicCamera Camera; + + CameraComponent() = default; + CameraComponent(const CameraComponent&) = default; + }; } #endif // LUNARIUM_COMPONENTS_H_ \ No newline at end of file diff --git a/src/world/world.cpp b/src/world/world.cpp index 91fb41c..49df084 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include "entity.h" namespace lunarium @@ -25,8 +25,8 @@ namespace lunarium // mActiveRegion = { 0, 0 }; // } - World::World() - : mUUID(UUID::GetNewID()) + World::World(std::string name) + : mUUID(UUID::GetNewID()), mName(name), mpActiveCamera(nullptr) { } @@ -43,19 +43,27 @@ namespace lunarium void World::Update(float dt) { - // TODO: Call Update in the world script and on each region script + // TODO: Update all entities } - void World::Render(Graphics* pGraphics) const + void World::Render(lunarium::IGraphics* pGraphics) const { - // TODO: Call Render in the world script and on each region - } + OrthographicCamera* pCam = mpActiveCamera; + + if (!pCam) + { + // Get a camera component from an entity + } + + // Get ViewProject matrix + // Render each entity that has a renderable component and a transform + } + - void World::RenderToTexture(Graphics* pGraphics, Image* pTexture) const + void World::SetActiveCamera(OrthographicCamera* pCam) { - + mpActiveCamera = pCam; } - entt::registry* World::GetEntityRegistry() { @@ -116,7 +124,7 @@ namespace lunarium for (int i = 0; i < mEntities.size(); i++) { nlohmann::ordered_json e; - mEntities[i]->Serialize(e); + mEntities[i]->Serialize(e).LogIfFailed(LogCategory::GAME_SYSTEM); ents.emplace_back(e); } @@ -142,7 +150,7 @@ namespace lunarium return OpRes::Fail("Invalid entity node"); } - new_ent->Deserialize(ent); + new_ent->Deserialize(ent).LogIfFailed(LogCategory::GAME_SYSTEM); mEntities.push_back(new_ent); } diff --git a/src/world/world.h b/src/world/world.h index 8ddfad8..7d0e553 100644 --- a/src/world/world.h +++ b/src/world/world.h @@ -23,10 +23,10 @@ namespace lunarium { - class Graphics; + class IGraphics; class Image; class Script; - //class Camera; + class OrthographicCamera; //class GameObject; class Entity; } @@ -54,17 +54,14 @@ namespace lunarium public: // INTERFACE //World(Camera* pCam, Sizei region_size, Sizei world_size); - World(); + World(std::string name); void OnLoad(); void OnUnload(); void Update(float dt); - void Render(Graphics* pGraphics) const; - void RenderToTexture(Graphics* pGraphics, Image* pTexture) const; + void Render(lunarium::IGraphics* pGraphics) const; - OpRes SetRegion(Region* region, Vec2i at); - Region* GetRegion(Vec2i at); - bool RemoveRegion(Vec2i at); + void SetActiveCamera(OrthographicCamera* pCam); entt::registry* GetEntityRegistry(); LUUID CreateEntity(); @@ -82,10 +79,12 @@ namespace lunarium private: LUUID mUUID; - std::string mWorldName; + std::string mName; entt::registry mECSRegistry; std::vector mEntities; + OrthographicCamera* mpActiveCamera; + // TODO: Move these into a TileMap class? // This would allow worlds to support non-tile based levels/worlds Sizei mRegionSize; // in tiles