8#include <BS_thread_pool.hpp>
9#include <entt/locator/locator.hpp>
10#include <entt/signal/dispatcher.hpp>
11#include <SFML/Graphics.hpp>
12#include <SFML/Window.hpp>
33 App::App(std::string_view log_dir, std::string_view config_file)
61 entt::locator<scene::SceneManager>::reset();
62 entt::locator<sol::state>::reset();
63 entt::locator<meta::EntityFactory>::reset();
64 entt::locator<meta::SystemFactory>::reset();
65 entt::locator<entt::dispatcher>::reset();
66 entt::locator<sf::RenderWindow>::reset();
67 entt::locator<fs::VirtualFileSystem>::reset();
68 entt::locator<Config>::reset();
69 entt::locator<BS::light_thread_pool>::reset();
72 entt::locator<logging::Log>::reset();
79 auto& sm = entt::locator<scene::SceneManager>::value();
80 sm.load_app(path.string());
87 auto& window = entt::locator<sf::RenderWindow>::value();
88 auto& dispatcher = entt::locator<entt::dispatcher>::value();
89 auto& manager = entt::locator<scene::SceneManager>::value();
91 using clock = std::chrono::high_resolution_clock;
92 using duration = std::chrono::duration<double>;
93 using time_point = std::chrono::time_point<clock, duration>;
95 constexpr const auto one_second = std::chrono::seconds {1};
102 duration elapsed {0};
105 time_point now = clock::now();
106 time_point previous = now;
108 unsigned int frames = 0u;
109 unsigned int updates = 0u;
110 duration perf_counter {0};
112 while (window.isOpen())
115 elapsed = now - previous;
119 perf_counter += elapsed;
124 alpha = dt / one_second;
135 window.clear(sf::Color::White);
141 if (perf_counter >= one_second)
147 perf_counter = std::chrono::nanoseconds {0};
155 platform::configure_terminal();
158 std::srand(
static_cast<unsigned int>(std::time(
nullptr)));
164 const auto now = std::chrono::zoned_time {std::chrono::current_zone(), std::chrono::system_clock::now()}.get_local_time();
165 const std::string log_path = std::format(
"{0}{1}{2}", log_dir, std::format(
"{0:%d-%m-%Y-[%H-%M]}", now),
".log");
166 if (!std::filesystem::exists(log_dir))
168 std::filesystem::create_directory(log_dir);
171 entt::locator<logging::Log>::emplace();
181 auto cores = (std::thread::hardware_concurrency() / 2) - 2;
187 entt::locator<BS::light_thread_pool>::emplace(cores);
192 auto& config = entt::locator<Config>::emplace(config_file);
199 entt::locator<fs::VirtualFileSystem>::emplace();
204 auto& window = entt::locator<sf::RenderWindow>::emplace();
205 window.setVisible(
false);
208 mode.bitsPerPixel = sf::VideoMode::getDesktopMode().bitsPerPixel;
212 sf::ContextSettings context;
214 context.attributeFlags = sf::ContextSettings::Default;
215 context.depthBits = 24;
216 context.majorVersion = 3;
217 context.minorVersion = 2;
218 context.sRgbCapable =
false;
219 context.stencilBits = 8;
226 auto& fs = entt::locator<fs::VirtualFileSystem>::value();
230 if (image.loadFromMemory(data.data(), data.size()))
233 window.setIcon(image);
243 window.setMouseCursorVisible(
true);
247 GALAXY_LOG(
GALAXY_WARNING,
"Did not specify cursor size properly, must be same size as texture. Reverting to system default.");
251 auto& fs = entt::locator<fs::VirtualFileSystem>::value();
257 window.setMouseCursor(
m_cursor.value());
273 if (!window.setActive(
true))
278 window.setVisible(
true);
280 if (!window.hasFocus())
282 window.requestFocus();
288 entt::locator<entt::dispatcher>::emplace();
289 for (
auto i = 0; i < sf::Joystick::Count; i++)
291 if (sf::Joystick::isConnected(i))
309 auto&
sf = entt::locator<meta::SystemFactory>::emplace();
310 auto& ef = entt::locator<meta::EntityFactory>::emplace();
343 auto& lua = entt::locator<sol::state>::emplace();
372 entt::locator<scene::SceneManager>::emplace();
377 window.handleEvents([](
auto&& event) {
378 using Event = std::decay_t<
decltype(event)>;
380 if constexpr (std::is_same_v<Event, sf::Event::Closed>)
382 entt::locator<sf::RenderWindow>::value().close();
386 entt::locator<entt::dispatcher>::value().trigger(event);
#define GALAXY_ADD_SINK(sink,...)
#define GALAXY_INFO
Log.hpp galaxy.
#define GALAXY_LOG(level, msg,...)
void setup_config(std::string_view config_file)
void setup_logging(std::string_view log_dir)
void load()
Loads the default appdata file.
std::optional< sf::Cursor > m_cursor
SFML cursor handle. Needs to be kept in memory.
void handle_events(sf::RenderWindow &window)
App(std::string_view log_dir, std::string_view config_file)
Default constructor.
Logs a message to the console.
void inject()
Inject everything into Lua.
static auto cursor_icon() noexcept -> const std::string &
Cursor texture file in vfs.
static auto window_icon() noexcept -> const std::string &
Window icon file in vfs.
static auto msaa() noexcept -> bool
Enable MSAA.
static auto vsync() noexcept -> bool
Vsync control.
static auto window_width() noexcept -> int
Window creation width.
static auto asset_pack() noexcept -> const std::string &
Name of packed assets file.
static auto window_height() noexcept -> int
Window creation height.
static auto root_dir() noexcept -> std::filesystem::path
Current root directory of application, unless it has been changed.
static auto cursor_hotspot() noexcept -> const sf::Vector2u &
Cursor selector point (hotspot).
static auto cursor_visible() noexcept -> bool
Show/hide mouse cursor.
static auto set_config_to_default(core::Config &config) -> void
Restore all config settings to default.
static auto set_delta_time(const double dt) noexcept -> void
Set galaxy delta time.
static auto window_title() noexcept -> const std::string &
Window title.
static auto cursor_grabbed() noexcept -> bool
Grab/release mouse cursor.
static auto ups() noexcept -> double
Game updates per second, independant of FPS, see "fixed timestep gameloop".
static auto cursor_icon_size() noexcept -> const sf::Vector2u &
Cursor icon texture size.
static auto set_settings_from_config(core::Config &config) -> void
Set all our settings using the provided config file.
static auto fullscreen() noexcept -> bool
Window maximized state.