GigaPasjans
Loading...
Searching...
No Matches
GameBootstrapper.h
1// Wykonane przez Piotra ChudziƄskiego w dniu 10.04.2025
2#pragma once
3
4#include "GameOptions.h"
5#include "GameSerializer.h"
6#include "IGame.h"
7#include "andromenda/Surface.h"
8#include "andromenda/Terminal.h"
9#include "foundation/Common.h"
10
11#include <Windows.h>
12#include <conio.h>
13
14namespace Phosphorus
15{
16 enum class ExitCode : int32
17 {
18 Peacefully = 0,
19 NonAuthorizedExit = 1
20 };
21
22 inline IGame *GameInstance;
23 inline enum class ExitCode ExitCode = ExitCode::NonAuthorizedExit;
24 volatile inline bool RequestExit = false;
25
26 template<typename T>
27 T *GetGameAs()
28 {
29 return dynamic_cast<T *>(GameInstance);
30 }
31
36 {
37 public:
38 template<typename T>
39 static int32 Run(GameOptions &options)
40 {
41 if (options.Verbose)
42 Log::Init();
43 else
44 Log::Disable();
45
46 GameInstance = new T(options);
47
48 Andromenda::Surface &surface = *GameInstance->m_Surface;
49
50 const ProductInfo &product = GameInstance->GetProductInfo();
51 INFO("Phosphorus is launching: {} {}", product.Name, product.Version);
52
54
55 std::map<ParentGameObject *, std::vector<String>> objects;
56
57 if (std::filesystem::exists("./settings.json"))
58 GameInstance->GameSettings =
59 GameSerializer::LoadGameObject(GameSerializer::LoadFromFile("./settings.json"), objects).second;
60 else
61 GameInstance->GameSettings = product.GameSettingsSupplier();
62
63 GameInstance->RegisterGameObject("Global::GameSettings", GameInstance->GameSettings);
64
65 INFO("Entering into loop");
66
67 GameInstance->Run();
68 while (!RequestExit)
69 {
70 }
71
72 surface.FinalizeThread();
73 surface.LockForWrite();
74 surface.UnlockForWrite();
75
77 "Global::GameSettings", GameInstance->GameSettings));
79
80 delete GameInstance;
81
82 INFO("Program finished execution with code {}", static_cast<int32>(ExitCode));
83 return static_cast<int32>(ExitCode);
84 }
85 };
86} // namespace Phosphorus
The renderer of component registry.
Definition Surface.h:28
void UnlockForWrite()
Unlocks a surface rendering locker.
Definition Surface.cpp:285
void FinalizeThread()
Sets m_ShouldExit to true and waits until thread is finalized.
Definition Surface.cpp:60
void LockForWrite()
Locks a surface rendering locker.
Definition Surface.cpp:283
Phosphorus game based runner.
Definition GameBootstrapper.h:36
static nlohmann::json LoadFromFile(const String &filename)
Loads JSON object from file.
Definition GameSerializer.cpp:96
static std::pair< String, GameObject * > LoadGameObject(const nlohmann::json &obj, std::map< ParentGameObject *, std::vector< String > > &childrenMap)
Loads GameObject and children from JSON object.
Definition GameSerializer.cpp:12
static void SaveToFile(const String &filename, const nlohmann::json &json)
Saves JSON object to file.
Definition GameSerializer.cpp:105
static nlohmann::json SerializeGameObject(const String &name, GameObject *object)
Serializes GameObject and children to JSON object.
Definition GameSerializer.cpp:42
static void Clear()
Deletes all objects in repository.
Definition GameObject.h:480
static void RegisterGameObject(GameObject *object)
Registers GameObject in repository.
Definition GameObject.h:491
Represents serializable GameObjectRegistry with rendering, asset manager.
Definition IGame.h:42
Game launch options.
Definition GameOptions.h:11
Game product info.
Definition IGame.h:17
String Name
Product name.
Definition IGame.h:22
int32 Version
Game version.
Definition IGame.h:28
std::function< GameObject *()> GameSettingsSupplier
Lambda for creating game settings.
Definition IGame.h:34