Skip to content
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
7 changes: 5 additions & 2 deletions Core/Entities/GameObject.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using SharpEngine.Core.Attributes;
using SharpEngine.Core.Entities.Properties;
using SharpEngine.Core.Entities.Properties.Meshes;
using SharpEngine.Core.Extensions;
using SharpEngine.Core.Scenes;
using SharpEngine.Core.Shaders;
using SharpEngine.Core.Textures;

using Silk.NET.OpenGL;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

namespace SharpEngine.Core.Entities;
Expand All @@ -21,8 +24,8 @@ public class GameObject : SceneNode
/// </summary>
public GameObject()
{
var shader = ShaderService.Instance.LoadShader("Shaders/shader.vert", "Shaders/lighting.frag", "lighting");
var diffuse = TextureService.Instance.LoadTexture("Textures/DefaultTextures/debug.JPG");
var shader = ShaderService.Instance.LoadShader(PathExtensions.GetPath("Shaders/shader.vert"), PathExtensions.GetPath("Shaders/lighting.frag"), "lighting");
var diffuse = TextureService.Instance.LoadTexture(PathExtensions.GetPath("Textures/DefaultTextures/debug.JPG"));
Material = new(shader, diffuse);

BoundingBox = BoundingBox.CalculateBoundingBox(Transform);
Expand Down
3 changes: 2 additions & 1 deletion Core/Entities/Lights/DirectionalLight.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SharpEngine.Core.Entities.Properties;
using SharpEngine.Core.Extensions;
using SharpEngine.Core.Shaders;
using System.Numerics;
using System.Threading.Tasks;
Expand All @@ -23,7 +24,7 @@ public DirectionalLight()
Diffuse = new Vector3(0.4f, 0.4f, 0.4f);
Specular = new Vector3(0.5f, 0.5f, 0.5f);

Material.Shader = ShaderService.Instance.LoadShader("Shaders/shader.vert", "Shaders/lighting.frag", "lighting");
Material.Shader = ShaderService.Instance.LoadShader(PathExtensions.GetPath("Shaders/shader.vert"), PathExtensions.GetPath("Shaders/lighting.frag"), "lighting");
}

/// <inheritdoc />
Expand Down
5 changes: 3 additions & 2 deletions Core/Entities/Lights/PointLight.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SharpEngine.Core.Shaders;
using SharpEngine.Core.Extensions;
using SharpEngine.Core.Shaders;
using System.Numerics;
using System.Threading.Tasks;

Expand All @@ -24,7 +25,7 @@ public PointLight(Vector3 position, int index)

_index = index;

Material.Shader = ShaderService.Instance.LoadShader("Shaders/shader.vert", "Shaders/lighting.frag", "lighting");
Material.Shader = ShaderService.Instance.LoadShader(PathExtensions.GetPath("Shaders/shader.vert"), PathExtensions.GetPath("Shaders/lighting.frag"), "lighting");
LampShader = new LampShader();

}
Expand Down
5 changes: 3 additions & 2 deletions Core/Entities/Lights/SpotLight.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SharpEngine.Core.Shaders;
using SharpEngine.Core.Extensions;
using SharpEngine.Core.Shaders;
using System.Numerics;
using System.Threading.Tasks;

Expand All @@ -21,7 +22,7 @@ public SpotLight()
Linear = 0.09f;
Quadratic = 0.032f;

Material.Shader = ShaderService.Instance.LoadShader("Shaders/shader.vert", "Shaders/lighting.frag", "lighting");
Material.Shader = ShaderService.Instance.LoadShader(PathExtensions.GetPath("Shaders/shader.vert"), PathExtensions.GetPath("Shaders/lighting.frag"), "lighting");
}

/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions Core/Extensions/PathExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.IO;
using System.Reflection;

namespace SharpEngine.Core.Extensions;
public static class PathExtensions
{
public static string GetPath(string file)
=> Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file);
}
10 changes: 7 additions & 3 deletions Core/Primitives/PrimitiveFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using SharpEngine.Core.Entities;
using SharpEngine.Core.Entities.Properties;

using SharpEngine.Core.Extensions;
using System;
using System.Numerics;

Expand All @@ -11,13 +11,17 @@ namespace SharpEngine.Core.Primitives;
/// </summary>
public static class PrimitiveFactory
{
private static string DebugTexture => PathExtensions.GetPath("Textures/DefaultTextures/debug.JPG");
private static string DefaultVertexShader => PathExtensions.GetPath("Shaders/shader.vert");
private static string DefaultFragmentShader => PathExtensions.GetPath("Shaders/lighting.frag");

/// <summary>
/// Creates a new <see cref="GameObject"/> by the given primitive type.
/// </summary>
/// <param name="primitiveType">The type of primitive to create.</param>
/// <param name="position">Where the game object should be placed.</param>
public static GameObject Create(PrimitiveType primitiveType, Vector3 position)
=> Create(primitiveType, position, "Textures/DefaultTextures/debug.JPG");
=> Create(primitiveType, position, DebugTexture);

/// <inheritdoc cref="Create(PrimitiveType, Vector3)"/>
/// <param name="primitiveType">The type of primitive to be created.</param>
Expand All @@ -29,7 +33,7 @@ public static GameObject Create(PrimitiveType primitiveType, Vector3 position)
/// <returns>A new game object.</returns>
/// <exception cref="InvalidOperationException">Thrown when the specified primitive type does not exist.</exception>
public static GameObject Create(PrimitiveType primitiveType, Vector3 position, string diffuseMapFile, string? specularMapFile = null, string? vertShaderFile = null, string? fragShaderFile = null)
=> new(diffuseMapFile, specularMapFile ?? "Textures/DefaultTextures/debug.JPG", vertShaderFile ?? "Shaders/shader.vert", fragShaderFile ?? "Shaders/lighting.frag")
=> new(diffuseMapFile, specularMapFile ?? DebugTexture, vertShaderFile ?? DefaultVertexShader, fragShaderFile ?? DefaultFragmentShader)
{
Transform = new Transform { Position = position },
Mesh = primitiveType switch
Expand Down
11 changes: 10 additions & 1 deletion Core/Scenes/Scene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ public IEnumerable<Task> IterateAsync(List<SceneNode> elements, Func<SceneNode,
/// <returns>The scene from the given file. Loads an empty scene if unable to load the scene.</returns>
public static Scene LoadScene(string sceneFile)
{
Console.WriteLine(sceneFile);

var loadedScene = JsonSerializer.Deserialize<Scene>(sceneFile);

if (loadedScene is not null)
Expand All @@ -191,7 +193,7 @@ public static Scene LoadScene(string sceneFile)
return new();
}

private void SetFileFullPath(string sceneFile)
public void SetFileFullPath(string sceneFile)
{
_fileFullPath = sceneFile;
Name = System.IO.Path.GetFileNameWithoutExtension(sceneFile);
Expand All @@ -204,6 +206,13 @@ private void SetFileFullPath(string sceneFile)
public bool HasSaveFile()
=> _fileFullPath is not null;

/// <summary>
/// Saves the scene.
/// </summary>
/// <returns>A <see cref="Task"/> representing an asynchronous operation.</returns>
public async Task SaveScene()
=> await SaveScene(_fileFullPath!);

/// <summary>
/// Saves the scene to the given <paramref name="fileName"/>.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion Core/Shaders/LampShader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SharpEngine.Core.Entities.Properties;
using SharpEngine.Core.Extensions;
using Silk.NET.OpenGL;

namespace SharpEngine.Core.Shaders;
Expand All @@ -7,7 +8,7 @@ internal class LampShader : ShaderBase
{
public LampShader()
{
Shader = ShaderService.Instance.LoadShader("Shaders/shader.vert", "Shaders/shader.frag", "lamp");
Shader = ShaderService.Instance.LoadShader(PathExtensions.GetPath("Shaders/shader.vert"), PathExtensions.GetPath("Shaders/shader.frag"), "lamp");

Vao = Window.GL.GenVertexArray();
Window.GL.BindVertexArray(Vao);
Expand Down
3 changes: 2 additions & 1 deletion Core/Shaders/LightingShader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SharpEngine.Core.Entities.Properties;
using SharpEngine.Core.Extensions;
using Silk.NET.OpenGL;

namespace SharpEngine.Core.Shaders;
Expand All @@ -7,7 +8,7 @@ internal class LightingShader : ShaderBase
{
public LightingShader()
{
Shader = ShaderService.Instance.LoadShader("Shaders/shader.vert", "Shaders/lighting.frag", "lighting");
Shader = ShaderService.Instance.LoadShader(PathExtensions.GetPath("Shaders/shader.vert"), PathExtensions.GetPath("Shaders/lighting.frag"), "lighting");

Vao = Window.GL.GenVertexArray();
Window.GL.BindVertexArray(Vao);
Expand Down
5 changes: 3 additions & 2 deletions Core/Shaders/UIShader.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Silk.NET.OpenGL;
using SharpEngine.Core.Extensions;
using Silk.NET.OpenGL;

namespace SharpEngine.Core.Shaders;

internal class UIShader : ShaderBase
{
public UIShader()
{
Shader = new Shader("Shaders/uiShader.vert", "Shaders/uiShader.frag", "ui");
Shader = new Shader(PathExtensions.GetPath("Shaders/uiShader.vert"), PathExtensions.GetPath("Shaders/uiShader.frag"), "ui");
}

/// <inheritdoc />
Expand Down
12 changes: 10 additions & 2 deletions Editor/EditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace SharpEngine.Editor;
/// </summary>
public class EditorWindow : Window
{
private Project _project = new Project() { Path = Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\..\Examples\Minimal"), Name = "Minimal" };
private Project _project;
private readonly List<ImGuiWindowBase> _windows = [];

private ContextMenuWindow? _contextMenuWindow;
Expand All @@ -28,8 +28,12 @@ public class EditorWindow : Window
/// Initializes an editor window with a specified scene and view settings.
/// </summary>
/// <param name="scene">Represents the current scene to be displayed in the editor window.</param>
/// <param name="project">The project loaded to the editor.</param>
/// <param name="settings">Contains configuration options for how the view should be rendered.</param>
public EditorWindow(Scene scene, IViewSettings settings) : base(scene, settings) { }
public EditorWindow(Scene scene, Project project, IViewSettings settings) : base(scene, settings)
{
_project = project;
}

/// <inheritdoc />
public override void OnLoad()
Expand All @@ -50,6 +54,7 @@ public override void OnLoad()

// Initialize all ImGuiWindowBase implementations
for (int i = 0; i < windowTypes.Length; i++)
{
try
{
var type = windowTypes[i];
Expand All @@ -66,6 +71,7 @@ public override void OnLoad()
{
Debug.LogInformation(e.Message, e);
}
}

_contextMenuWindow = (ContextMenuWindow?)_windows.FirstOrDefault(w => w.GetType() == typeof(ContextMenuWindow));
if (_contextMenuWindow is null)
Expand All @@ -74,6 +80,8 @@ public override void OnLoad()
_actionsMenuWindow = (ActionsMenuWindow?)_windows.FirstOrDefault(w => w.GetType() == typeof(ActionsMenuWindow));
if (_actionsMenuWindow is null)
Debug.LogInformation("ActionsMenuWindow not found.");
else
_actionsMenuWindow.OnSceneLoaded += SetScene;
}

/// <inheritdoc />
Expand Down
44 changes: 38 additions & 6 deletions Editor/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using SharpEngine.Core.Entities.Views.Settings;
using Launcher.UI;
using SharpEngine.Core;
using SharpEngine.Core.Entities.Views.Settings;
using SharpEngine.Core.Scenes;
using SharpEngine.Editor.Windows;
using SharpEngine.Shared;
using System.Reflection;

namespace SharpEngine.Editor;

Expand All @@ -16,12 +20,40 @@ public static class Program
[STAThread]
public static void Main(string[] args)
{
//var editor = new Editor();
var scene = args.Length > 0 ? Scene.LoadScene(args[0]) : new Scene();
try
{
var editorPath = Environment.GetEnvironmentVariable(EnvironmentVariables.EDITOR_PATH_ENVIRONMENT_VARIABLE);
if (string.IsNullOrWhiteSpace(editorPath))
Environment.SetEnvironmentVariable(EnvironmentVariables.EDITOR_PATH_ENVIRONMENT_VARIABLE, System.Environment.ProcessPath, EnvironmentVariableTarget.User);

using var window = new EditorWindow(scene, new DefaultViewSettings());
window.OnLoaded += () => Console.WriteLine("test");
string? sceneFile = null;
string? projectFile = null;

window.Run();
foreach (var arg in args)
{
if (arg.EndsWith(".sharpscene", StringComparison.OrdinalIgnoreCase))
sceneFile = arg;

else if (arg.EndsWith(".sharpproject", StringComparison.OrdinalIgnoreCase))
projectFile = arg;
}

var scene = !string.IsNullOrEmpty(sceneFile) ? Scene.LoadScene(sceneFile) : new Scene();
var project = !string.IsNullOrEmpty(projectFile) ? Project.LoadProject(projectFile)
#if DEBUG
// TODO: We should create an additional getter for the .sharpproject file.
: new Project() { Path = Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\..\Examples\Minimal"), Name = "Minimal" };
#else
: new Project();
#endif
using var window = new EditorWindow(scene, project!, new DefaultViewSettings());
window.OnLoaded += () => Console.WriteLine("test");

window.Run();
}
catch (Exception e)
{
Debug.LogInformation($"Failed to start the editor: {e.Message}", e, true);
}
}
}
43 changes: 20 additions & 23 deletions Editor/Windows/ActionsMenuWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ActionsMenuWindow : ImGuiWindowBase
/// <summary>
/// An event executed when a new scene is loaded.
/// </summary>
public event Action? OnSceneLoaded;
public event Action<Scene>? OnSceneLoaded;

/// <inheritdoc />
public override void Render()
Expand All @@ -36,41 +36,36 @@ public override void Render()

private void StartGame()
{
Debug.LogInformation($"Starting {Project.Name}.");

Thread thread = new Thread(() =>
if (Project is null)
{
var process = ProcessExtensions.GetProcess($"dotnet run --project {Project.Path}/{Project.Name}.csproj", true);
process.ErrorDataReceived += (sender, e) => Debug.LogInformation(e.Data);
process.OutputDataReceived += (sender, e) => Debug.LogInformation(e.Data);
process.Exited += (sender, e) => Debug.LogInformation("Process exited.");

process.Start();
// TODO: This should never be possible. If the Project is null when it's loaded we should make sure it's initialized.
// Editor service probably needs to be made shared so that we can initialize the project / solution when the editor is opened.

// Read and display output
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

if (!string.IsNullOrWhiteSpace(output))
Debug.LogInformation(output);

if (!string.IsNullOrWhiteSpace(error))
Debug.LogInformation(error);
});
// TODO: Do we want to save here automatically or prompt the user to save?
}

thread.Start();
Debug.LogInformation($"Starting {Project.Name}.");
ProcessExtensions.RunProcess($"dotnet run --project {Project.Path}/{Project.Name}.csproj", true, msg => Debug.LogInformation(msg));
}

private static void SaveScene(Scene scene)
private void SaveScene(Scene scene)
{
Console.WriteLine("Saving..");

if (!scene.HasUnsavedChanges && !scene.HasSaveFile())
if (!scene.HasUnsavedChanges && scene.HasSaveFile())
{
Console.WriteLine("No changes to save.");
return;
}

if (scene.HasSaveFile())
{
scene.SaveScene().Wait();
OnSceneLoaded?.Invoke(scene);
Console.WriteLine("File saved successfully!");
return;
}

using var dialog = new SaveFileDialog()
{
Filter = $"Scene files (*.{Scene.SceneFileExtension})|*.{Scene.SceneFileExtension}",
Expand All @@ -84,6 +79,8 @@ private static void SaveScene(Scene scene)
if (dialogResult == DialogResult.OK)
{
scene.SaveScene(dialog.FileName).Wait();
Scene.SetFileFullPath(dialog.FileName);

Console.WriteLine("File saved successfully!");
}
else
Expand Down
Loading