Loading...
Searching...
No Matches
AppManager.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4//
5// Created by Dave R. Smith on 3/5/25.
6//
7
8#ifndef MALENA_APPMANAGER_H
9#define MALENA_APPMANAGER_H
10
11#pragma once
12
16#include <SFML/Graphics.hpp>
17#include <functional>
18#include <optional>
19#include <string>
20#include <cstdint>
21
23
24namespace ml
25{
107 class MALENA_API AppManager : public Lifecycle, public CoreManager<Core>
108 {
109 public:
117 {
121 };
122
123 private:
124 sf::RenderWindow* window = nullptr;
125
126 Architecture _architecture;
127 sf::Color _clearColor { sf::Color::Black };
128 sf::Clock _clock;
129 bool _paused { false };
130
131 // Stored for window recreation (setWindowStyle)
132 sf::VideoMode _videoMode;
133 std::string _title;
134 std::uint32_t _windowStyle;
135 unsigned int _framerateLimit { 60 };
136
137 std::function<void()> _preRenderHook;
138 std::function<void()> _postRenderHook;
139 std::function<bool()> _closeHandler;
140 std::function<void(unsigned int, unsigned int)> _resizeHandler;
141
143 inline static bool _isDrawing = false;
144 inline static float _deltaTime = 0.f;
145 inline static AppManager* _instance = nullptr;
146 inline static Core* _exclusiveOwner = nullptr;
147 inline static std::vector<std::function<void()>> _deferredUnloads;
149
150 public:
167 AppManager(const sf::VideoMode& videoMode,
168 const std::string& title,
170 Architecture architecture = MVC,
171 std::uint32_t windowStyle = sf::Style::Default);
172
186 void run();
187
188 virtual ~AppManager() = default;
189
190 // ── Window appearance ─────────────────────────────────────────────────
191
198
204 void setTitle(const std::string& title);
205
211 void setIcon(const sf::Image& icon);
212
219 void setWindowPosition(int x, int y);
220
231 void setWindowStyle(std::uint32_t style);
232
233 // ── Timing ────────────────────────────────────────────────────────────
234
242 void setFramerateLimit(unsigned int limit);
243
249 void setVSync(bool enabled);
250
257 static float getDeltaTime() { return _deltaTime; }
258
259 // ── Pause / resume ────────────────────────────────────────────────────
260
268 void pause();
269
271 void resume();
272
274 bool isPaused() const { return _paused; }
275
276 // ── Lifecycle hooks ───────────────────────────────────────────────────
277
286 void onPreRender(std::function<void()> hook);
287
296 void onPostRender(std::function<void()> hook);
297
305 void onClose(std::function<bool()> handler);
306
312 void onResize(std::function<void(unsigned int, unsigned int)> handler);
313
314 // ── Architecture ──────────────────────────────────────────────────────
315
317 Architecture getArchitecture() const { return _architecture; }
318
319 // ── Exclusive interaction ─────────────────────────────────────────────
320
324 static void setExclusiveOwner(Core* owner);
325
327 static void clearExclusiveOwner();
328
330 static Core* exclusiveOwner() { return _exclusiveOwner; }
331
332 // ── Internal ──────────────────────────────────────────────────────────
333
335 static bool isDrawing() { return _isDrawing; }
336
337 static bool isUnderExclusiveOwner(Core* component);
338
339 static void deferUnload(std::function<void()> op)
340 {
341 _deferredUnloads.push_back(std::move(op));
342 }
343
351 static AppManager& get() { return *_instance; }
353
354 private:
355 void fireInputEvents(const std::optional<sf::Event>& event);
356 void fireUpdateEvents();
357 void draw();
358 void flushDeferredUnloads();
359 };
360
361} // namespace ml
362
363#endif // MALENA_APPMANAGER_H
void setVSync(bool enabled)
Enable or disable vertical synchronisation.
static Core * exclusiveOwner()
The current exclusive-interaction owner, or nullptr.
Definition AppManager.h:330
void onPostRender(std::function< void()> hook)
Register a callback invoked after all components are drawn and before display().
Architecture
Architectural style hint passed at construction.
Definition AppManager.h:117
@ EDA
Event-Driven Architecture.
Definition AppManager.h:119
@ ECS
Entity-Component-System.
Definition AppManager.h:120
@ MVC
Model-View-Controller.
Definition AppManager.h:118
void run()
Enter the main loop and run until the window is closed.
void onPreRender(std::function< void()> hook)
Register a callback invoked after clear() and before drawing components.
void setIcon(const sf::Image &icon)
Set the window icon from an sf::Image.
static float getDeltaTime()
Return the elapsed time of the previous frame in seconds.
Definition AppManager.h:257
bool isPaused() const
Return true if the application is currently paused.
Definition AppManager.h:274
void resume()
Resume update events after a pause().
void setBackgroundColor(sf::Color color)
Set the colour used to clear the window at the start of each frame.
void pause()
Suspend per-frame update events.
static void clearExclusiveOwner()
Lift any active exclusive-owner restriction.
void onClose(std::function< bool()> handler)
Register a callback invoked when the OS close button is pressed.
void setFramerateLimit(unsigned int limit)
Cap the frame rate.
void setTitle(const std::string &title)
Change the window title at runtime.
Architecture getArchitecture() const
Return the architectural mode set at construction.
Definition AppManager.h:317
static void setExclusiveOwner(Core *owner)
Restrict click and hover dispatch to owner and its descendants. Any component outside that subtree si...
void setWindowPosition(int x, int y)
Move the window to a position on the desktop.
virtual ~AppManager()=default
void setWindowStyle(std::uint32_t style)
Recreate the window with new decoration-style flags.
AppManager(const sf::VideoMode &videoMode, const std::string &title, sf::RenderWindow &window=WindowManager::getWindow(), Architecture architecture=MVC, std::uint32_t windowStyle=sf::Style::Default)
Construct an AppManager and create the SFML window.
void onResize(std::function< void(unsigned int, unsigned int)> handler)
Register a callback invoked whenever the window is resized.
Virtual base class for all Malena framework objects.
Definition Core.h:97
Static, type-safe collection manager for Core-derived objects.
Definition CoreManager.h:56
Trait that adds one-time initialization lifecycle hooks to any class.
Definition Lifecycle.h:73
static const Color Black
sf::RenderWindow & getWindow()
Return the framework's shared sf::RenderWindow.
#define MALENA_API
Definition Component.h:22