diff --git a/GlitchyEngine/src/Application.bf b/GlitchyEngine/src/Application.bf index e880451..0473710 100644 --- a/GlitchyEngine/src/Application.bf +++ b/GlitchyEngine/src/Application.bf @@ -1,5 +1,5 @@ -using GlitchyEngine.Platform.Windows; using GlitchyEngine.Events; +using GlitchyEngine.Platform.Windows; namespace GlitchyEngine { @@ -8,6 +8,8 @@ namespace GlitchyEngine private Window _window ~ delete _; private bool _running = true; + private LayerStack _layerStack = new LayerStack() ~ delete _; + public bool IsRunning => _running; public this() @@ -17,22 +19,34 @@ namespace GlitchyEngine _window.EventCallback = new => OnEvent; } - public void Run() - { - while(_running) - { - _window.Update(); - } - } - public void OnEvent(Event e) { EventDispatcher dispatcher = scope .(e); dispatcher.Dispatch(scope => OnWindowClose); - Log.EngineLogger.Trace("{}", e); + for(Layer layer in _layerStack) + { + layer.OnEvent(e); + if(e.Handled) + break; + } } - + + public void Run() + { + while(_running) + { + for(Layer layer in _layerStack) + layer.Update(); + + _window.Update(); + } + } + + public void PushLayer(Layer ownLayer) => _layerStack.PushLayer(ownLayer); + + public void PushOverlay(Layer ownOverlay) => _layerStack.PushOverlay(ownOverlay); + public bool OnWindowClose(WindowCloseEvent e) { _running = false; diff --git a/GlitchyEngine/src/Events/Event.bf b/GlitchyEngine/src/Events/Event.bf index 490960d..182fd02 100644 --- a/GlitchyEngine/src/Events/Event.bf +++ b/GlitchyEngine/src/Events/Event.bf @@ -29,6 +29,14 @@ namespace GlitchyEngine.Events { protected bool _handled; + public bool Handled + { + [Inline] + get => _handled; + [Inline] + set => _handled = value; + } + public abstract EventType EventType {get;} public abstract StringView Name {get;} public abstract EventCategory Category {get;} diff --git a/GlitchyEngine/src/Layer.bf b/GlitchyEngine/src/Layer.bf new file mode 100644 index 0000000..b996e01 --- /dev/null +++ b/GlitchyEngine/src/Layer.bf @@ -0,0 +1,25 @@ +using System; +using GlitchyEngine.Events; + +namespace GlitchyEngine +{ + public abstract class Layer + { + protected String _debugName; + + [Inline] + public StringView Name => _debugName; + + [AllowAppend] + public this(StringView name = "Layer") + { + String debugName = append String(name); + _debugName = debugName; + } + + public virtual void OnAttach() { } + public virtual void OnDetach() { } + public virtual void Update() { } + public virtual void OnEvent(Event event) { } + } +} diff --git a/GlitchyEngine/src/LayerStack.bf b/GlitchyEngine/src/LayerStack.bf new file mode 100644 index 0000000..3b335b5 --- /dev/null +++ b/GlitchyEngine/src/LayerStack.bf @@ -0,0 +1,61 @@ +using System.Collections; + +namespace GlitchyEngine +{ + public class LayerStack + { + private List _layers = new List() ~ DeleteContainerAndItems!(_); + + /* + * Marks the index of the last layer. + * Used to insert new layers before overlays. + */ + private int _insertIndex = 0; + + /** + * Pushes the provided layer onto the layer stack. + * @param ownLayer The layer that will be pushed onto the layerstack. + * @remarks The layerstack takes ownership of ownLayer. + */ + public void PushLayer(Layer ownLayer) + { + _layers.Insert(_insertIndex++, ownLayer); + } + + /** + * Pushes the provided overlay onto the layer stack. + * @param ownLayer The overlay that will be pushed onto the layerstack. + * @remarks The layerstack takes ownership of ownOverlay. + */ + public void PushOverlay(Layer ownOverlay) + { + _layers.Add(ownOverlay); + } + + /** + * Removes the given overlay from the layer stack. + * @param layer The layer to pop from the LayerStack. + */ + public void PopLayer(Layer layer) + { + if(_layers.Remove(layer)) + { + _insertIndex--; + } + } + + /** + * Removes the given overlay from the layer stack. + * @param overlay The overlay to pop from the LayerStack. + */ + public void PopOverlay(Layer overlay) + { + _layers.Remove(overlay); + } + + /** + * Returns an enumerator for enumerating the layers. + */ + public List.Enumerator GetEnumerator() => _layers.GetEnumerator(); + } +} diff --git a/GlitchyEngine/src/Platform/Windows/WindowsWindow.bf b/GlitchyEngine/src/Platform/Windows/WindowsWindow.bf index e94f7c4..f1118c8 100644 --- a/GlitchyEngine/src/Platform/Windows/WindowsWindow.bf +++ b/GlitchyEngine/src/Platform/Windows/WindowsWindow.bf @@ -348,9 +348,7 @@ namespace GlitchyEngine.Platform.Windows { /* Remove beeping sound when ALT + some key is pressed. */ if ( wParam == SC_KEYMENU ) - { return 0; - } } // Window closing case WM_CLOSE: diff --git a/Sandbox/src/SandboxApp.bf b/Sandbox/src/SandboxApp.bf index 851821c..a9cdf4d 100644 --- a/Sandbox/src/SandboxApp.bf +++ b/Sandbox/src/SandboxApp.bf @@ -1,17 +1,44 @@ using System; using GlitchyEngine; +using GlitchyEngine.Events; using System.Diagnostics; using GlitchLog; namespace Sandbox { + class ExampleLayer : Layer + { + [AllowAppend] + public this() : base("Example") { } + + public override void Update() + { + Log.ClientLogger.Info("ExampleLayer.Update"); + + // Just for temporary vsyncing + // Todo: remove + DwmFlush(); + } + + [CLink, Import("Dwmapi.lib")] + static extern void DwmFlush(); + + public override void OnEvent(Event event) + { + Log.ClientLogger.Trace("{}", event); + } + } + class SandboxApp : Application { + public this() + { + PushLayer(new ExampleLayer()); + } + [Export, LinkName("CreateApplication")] public static Application CreateApplication() { - - return new SandboxApp(); } }