Skip to content

feature: asset manager #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Sandbox/src/ExampleLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "StarEngine.h"

#include "StarEngine/Asset/TextureImporter.h"

ExampleLayer::ExampleLayer()
: Layer("ExampleLayer"), m_CameraController(1280.0f / 720.0f)
{
Expand Down Expand Up @@ -113,8 +115,8 @@ ExampleLayer::ExampleLayer()
m_FlatColorShader = StarEngine::Shader::Create("FlatColor", flatColorShaderVertexSrc, flatColorShaderFragmentSrc);
auto textureShader = m_ShaderLibrary.Load("assets/shaders/Texture.glsl");

m_Texture = StarEngine::Texture2D::Create("assets/textures/Checkerboard.png");
m_starLogoTexture = StarEngine::Texture2D::Create("assets/textures/starLogo.png");
m_Texture = StarEngine::TextureImporter::LoadTexture2D("assets/textures/Checkerboard.png");
m_starLogoTexture = StarEngine::TextureImporter::LoadTexture2D("assets/textures/starLogo.png");

textureShader->Bind();
textureShader->SetInt("u_Texture", 0);
Expand Down
4 changes: 3 additions & 1 deletion Sandbox/src/Sandbox2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "imgui/imgui_internal.h"

#include "StarEngine/Asset/TextureImporter.h"


Sandbox2D::Sandbox2D()
:Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f, true), m_SquareColor({ 0.2f, 0.3f, 0.8f, 1.0f })
Expand All @@ -18,7 +20,7 @@ void Sandbox2D::OnAttach()
{
SE_PROFILE_FUNCTION();

m_CheckerboardTexture = StarEngine::Texture2D::Create("assets/textures/Checkerboard.png");
m_CheckerboardTexture = StarEngine::TextureImporter::LoadTexture2D("assets/textures/Checkerboard.png");
}

void Sandbox2D::OnDetach()
Expand Down
7 changes: 7 additions & 0 deletions StarEditor/SandboxProject/Assets/AssetRegistry.ser
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
AssetRegistry:
- Handle: 9263618904997247981
FilePath: Textures/starLogo.png
Type: AssetType::Texture2D
- Handle: 10448957311305808839
FilePath: Scenes/Physics2D.starscene
Type: AssetType::Scene
119 changes: 61 additions & 58 deletions StarEditor/SandboxProject/Assets/Scenes/Physics2D.starscene
Original file line number Diff line number Diff line change
@@ -1,37 +1,64 @@
Scene: Untitled
Entities:
- Entity: 12327645238897851592
- Entity: 14040563719264005740
TagComponent:
Tag: Welcome
Tag: Player
TransformComponent:
Translation: [-3.79999995, 1.79999995, 0]
Translation: [0, 5, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
TextComponent:
TextString: Welcome to StarEditor
SpriteRendererComponent:
Color: [1, 1, 1, 1]
Kerning: 0
LineSpacing: 0
- Entity: 6793217508611625246
TextureHandle: 0
TilingFactor: 1
RigidBody2DComponent:
BodyType: Dynamic
FixedRotation: false
BoxCollider2DComponent:
Offset: [0, 0]
Size: [0.5, 0.5]
Density: 1
Friction: 0.5
Restitution: 0
RestitutionThreshold: 0.5
- Entity: 16241606122818465121
TagComponent:
Tag: Circle
Tag: Camera
TransformComponent:
Translation: [0, 0, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
CircleRendererComponent:
Color: [0.934362948, 0.497845858, 0, 1]
Thickness: 1
Fade: 0.00499999989
CameraComponent:
Camera:
ProjectionType: 1
PerspectiveFOV: 0.785398185
PerspectiveNear: 0.00999999978
PerspectiveFar: 1000
OrthographicSize: 25
OrthographicNear: -1
OrthographicFar: 1
Primary: true
FixedAspectRatio: false
- Entity: 16059016071823036758
TagComponent:
Tag: Cube
TransformComponent:
Translation: [0, 10, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
SpriteRendererComponent:
Color: [1, 1, 1, 1]
TextureHandle: 0
TilingFactor: 1
RigidBody2DComponent:
BodyType: Dynamic
FixedRotation: false
CircleCollider2DComponent:
BoxCollider2DComponent:
Offset: [0, 0]
Radius: 0.5
Size: [0.5, 0.5]
Density: 1
Friction: 0.5
Restitution: 1
Restitution: 0.100000001
RestitutionThreshold: 0.5
- Entity: 7665505456185074185
TagComponent:
Expand All @@ -42,6 +69,7 @@ Entities:
Scale: [10, 1, 1]
SpriteRendererComponent:
Color: [1, 1, 1, 1]
TextureHandle: 0
TilingFactor: 1
RigidBody2DComponent:
BodyType: Static
Expand All @@ -53,61 +81,36 @@ Entities:
Friction: 0.5
Restitution: 0
RestitutionThreshold: 0.5
- Entity: 16059016071823036758
- Entity: 6793217508611625246
TagComponent:
Tag: Cube
Tag: Circle
TransformComponent:
Translation: [0, 10, 0]
Translation: [0, 0, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
SpriteRendererComponent:
Color: [1, 1, 1, 1]
TilingFactor: 1
CircleRendererComponent:
Color: [0.934362948, 0.497845858, 0, 1]
Thickness: 1
Fade: 0.00499999989
RigidBody2DComponent:
BodyType: Dynamic
FixedRotation: false
BoxCollider2DComponent:
CircleCollider2DComponent:
Offset: [0, 0]
Size: [0.5, 0.5]
Radius: 0.5
Density: 1
Friction: 0.5
Restitution: 0.100000001
Restitution: 1
RestitutionThreshold: 0.5
- Entity: 16241606122818465121
TagComponent:
Tag: Camera
TransformComponent:
Translation: [0, 0, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
CameraComponent:
Camera:
ProjectionType: 1
PerspectiveFOV: 0.785398185
PerspectiveNear: 0.00999999978
PerspectiveFar: 1000
OrthographicSize: 25
OrthographicNear: -1
OrthographicFar: 1
Primary: true
FixedAspectRatio: false
- Entity: 14040563719264005740
- Entity: 12327645238897851592
TagComponent:
Tag: Player
Tag: Welcome
TransformComponent:
Translation: [0, 5, 0]
Translation: [-3.79999995, 1.79999995, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
SpriteRendererComponent:
TextComponent:
TextString: Welcome to StarEditor
Color: [1, 1, 1, 1]
TilingFactor: 1
RigidBody2DComponent:
BodyType: Dynamic
FixedRotation: false
BoxCollider2DComponent:
Offset: [0, 0]
Size: [0.5, 0.5]
Density: 1
Friction: 0.5
Restitution: 0
RestitutionThreshold: 0.5
Kerning: 0
LineSpacing: 0
3 changes: 2 additions & 1 deletion StarEditor/SandboxProject/Sandbox.starproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Project:
Name: Sandbox
StartScene: "Scenes/Physics2D.starscene"
StartScene: "0"
AssetDirectory: "Assets"
AssetRegistryPath: "AssetRegistry.ser"
ScriptModulePath: "Scripts/Binaries/Sandbox.dll"
71 changes: 38 additions & 33 deletions StarEditor/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "StarEngine/Scripting/ScriptEngine.h"
#include "StarEngine/Renderer/Font.h"

#include "StarEngine/Asset/AssetManager.h"
#include "StarEngine/Asset/TextureImporter.h"
#include "StarEngine/Asset/SceneImporter.h"

#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

Expand All @@ -29,13 +33,11 @@ namespace StarEngine {
{
SE_PROFILE_FUNCTION();

m_CheckerboardTexture = Texture2D::Create("assets/textures/Checkerboard.png");

m_IconPlay = Texture2D::Create("Resources/Icons/PlayButton.png");
m_IconPause = Texture2D::Create("Resources/Icons/PauseButton.png");
m_IconSimulate = Texture2D::Create("Resources/Icons/SimulateButton.png");
m_IconStep = Texture2D::Create("Resources/Icons/StepButton.png");
m_IconStop = Texture2D::Create("Resources/Icons/StopButton.png");
m_IconPlay = TextureImporter::LoadTexture2D("Resources/Icons/PlayButton.png");
m_IconPause = TextureImporter::LoadTexture2D("Resources/Icons/PauseButton.png");
m_IconSimulate = TextureImporter::LoadTexture2D("Resources/Icons/SimulateButton.png");
m_IconStep = TextureImporter::LoadTexture2D("Resources/Icons/StepButton.png");
m_IconStop = TextureImporter::LoadTexture2D("Resources/Icons/StopButton.png");

FramebufferSpecification fbSpec;
fbSpec.Attachments = { FramebufferTextureFormat::RGBA8, FramebufferTextureFormat::RED_INTEGER, FramebufferTextureFormat::Depth };
Expand Down Expand Up @@ -297,8 +299,8 @@ namespace StarEngine {
{
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CONTENT_BROWSER_ITEM"))
{
const wchar_t* path = (const wchar_t*)payload->Data;
OpenScene(path);
AssetHandle handle = *(AssetHandle*)payload->Data;
OpenScene(handle);
}
ImGui::EndDragDropTarget();
}
Expand Down Expand Up @@ -464,6 +466,7 @@ namespace StarEngine {
EventDispatcher dispatcher(e);
dispatcher.Dispatch<KeyPressedEvent>(SE_BIND_EVENT_FN(EditorLayer::OnKeyPressed));
dispatcher.Dispatch<MouseButtonPressedEvent>(SE_BIND_EVENT_FN(EditorLayer::OnMouseButtonPressed));
dispatcher.Dispatch<WindowDropEvent>(SE_BIND_EVENT_FN(EditorLayer::OnWindowDrop));
}

bool EditorLayer::OnKeyPressed(KeyPressedEvent& e)
Expand Down Expand Up @@ -578,6 +581,15 @@ namespace StarEngine {
return false;
}

bool EditorLayer::OnWindowDrop(WindowDropEvent& e)
{
// TODO: if a project is dropped in, probably open it

//AssetManager::ImportAsset();

return true;
}

void EditorLayer::OnOverlayRender()
{
if (m_SceneState == SceneState::Play)
Expand Down Expand Up @@ -652,9 +664,10 @@ namespace StarEngine {
{
ScriptEngine::Init();

auto startScenePath = Project::GetAssetFileSystemPath(Project::GetActive()->GetConfig().StartScene);
OpenScene(startScenePath);
m_ContentBrowserPanel = CreateScope<ContentBrowserPanel>();
AssetHandle startScene = Project::GetActive()->GetConfig().StartScene;
if (startScene)
OpenScene(startScene);
m_ContentBrowserPanel = CreateScope<ContentBrowserPanel>(Project::GetActive());
}
}

Expand Down Expand Up @@ -684,33 +697,26 @@ namespace StarEngine {

void EditorLayer::OpenScene()
{
std::string filepath = FileDialogs::OpenFile("StarEngine Scene (*.starscene)\0*.starscene\0");
if (!filepath.empty())
OpenScene(filepath);
// std::string filepath = FileDialogs::OpenFile("Hazel Scene (*.hazel)\0*.hazel\0");
// if (!filepath.empty())
// OpenScene(filepath);
}

void EditorLayer::OpenScene(const std::filesystem::path& path)
void EditorLayer::OpenScene(AssetHandle handle)
{
SE_CORE_ASSERT(handle);

if (m_SceneState != SceneState::Edit)
OnSceneStop();

if (path.extension().string() != ".starscene")
{
SE_WARN("Could not load {0} - not a scene file", path.filename().string());
return;
}
Ref<Scene> readOnlyScene = AssetManager::GetAsset<Scene>(handle);
Ref<Scene> newScene = Scene::Copy(readOnlyScene);

Ref<Scene> newScene = CreateRef<Scene>();
SceneSerializer serializer(newScene);
if (serializer.Deserialize(path.string()))
{
m_EditorScene = newScene;
m_EditorScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y);
m_SceneHierarchyPanel.SetContext(m_EditorScene);
m_EditorScene = newScene;
m_SceneHierarchyPanel.SetContext(m_EditorScene);

m_ActiveScene = m_EditorScene;
m_EditorScenePath = path;
}
m_ActiveScene = m_EditorScene;
m_EditorScenePath = Project::GetActive()->GetEditorAssetManager()->GetFilePath(handle);
}

void EditorLayer::SaveScene()
Expand All @@ -733,8 +739,7 @@ namespace StarEngine {

void EditorLayer::SerializeScene(Ref<Scene> scene, const std::filesystem::path& path)
{
SceneSerializer serializer(scene);
serializer.Serialize(path.string());
SceneImporter::SaveScene(scene, path);
}

void EditorLayer::OnScenePlay()
Expand Down
3 changes: 2 additions & 1 deletion StarEditor/src/EditorLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace StarEngine
private:
bool OnKeyPressed(KeyPressedEvent& e);
bool OnMouseButtonPressed(MouseButtonPressedEvent& e);
bool OnWindowDrop(WindowDropEvent& e);

void OnOverlayRender();

Expand All @@ -37,7 +38,7 @@ namespace StarEngine

void NewScene();
void OpenScene();
void OpenScene(const std::filesystem::path& path);
void OpenScene(AssetHandle handle);
void SaveScene();
void SaveSceneAs();

Expand Down
Loading
Loading