Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Application.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Development Kit"
3 // For conditions of distribution and use, see copyright notice in Prerequesites.hpp
4 
5 #pragma once
6 
7 #ifndef NDK_APPLICATION_HPP
8 #define NDK_APPLICATION_HPP
9 
10 #include <NDK/Prerequesites.hpp>
11 #include <NDK/World.hpp>
12 #include <Nazara/Core/Clock.hpp>
13 #include <map>
14 #include <list>
15 #include <set>
16 
17 #ifndef NDK_SERVER
18 #include <NDK/Console.hpp>
19 #include <Nazara/Core/Log.hpp>
20 #include <Nazara/Lua/LuaInstance.hpp>
21 #include <Nazara/Graphics/TextSprite.hpp>
22 #include <Nazara/Renderer/RenderTarget.hpp>
23 #include <Nazara/Platform/Window.hpp>
24 #endif
25 
26 namespace Ndk
27 {
28  class NDK_API Application
29  {
30  public:
31  #ifndef NDK_SERVER
32  struct ConsoleOverlay;
33  struct FPSCounterOverlay;
34  #endif
35 
36  inline Application();
37  Application(int argc, char* argv[]);
38  Application(const Application&) = delete;
39  Application(Application&&) = delete;
40  inline ~Application();
41 
42  #ifndef NDK_SERVER
43  template<typename T, typename... Args> T& AddWindow(Args&&... args);
44  #endif
45  template<typename... Args> World& AddWorld(Args&&... args);
46 
47  #ifndef NDK_SERVER
48  inline void EnableConsole(bool enable);
49  inline void EnableFPSCounter(bool enable);
50 
51  inline ConsoleOverlay& GetConsoleOverlay(std::size_t windowIndex = 0U);
52  inline FPSCounterOverlay& GetFPSCounterOverlay(std::size_t windowIndex = 0U);
53  #endif
54 
55  inline const std::set<Nz::String>& GetOptions() const;
56  inline const std::map<Nz::String, Nz::String>& GetParameters() const;
57 
58  inline float GetUpdateTime() const;
59 
60  inline bool HasOption(const Nz::String& option) const;
61  inline bool HasParameter(const Nz::String& key, Nz::String* value) const;
62 
63  #ifndef NDK_SERVER
64  inline bool IsConsoleEnabled() const;
65  inline bool IsFPSCounterEnabled() const;
66  #endif
67 
68  bool Run();
69 
70  #ifndef NDK_SERVER
71  inline void MakeExitOnLastWindowClosed(bool exitOnClosedWindows);
72  #endif
73 
74  inline void Quit();
75 
76  Application& operator=(const Application&) = delete;
77  Application& operator=(Application&&) = delete;
78 
79  inline static Application* Instance();
80 
81  #ifndef NDK_SERVER
82  struct ConsoleOverlay
83  {
84  std::unique_ptr<Console> console;
85  Nz::LuaInstance lua;
86 
87  NazaraSlot(Nz::EventHandler, OnEvent, eventSlot);
88  NazaraSlot(Nz::EventHandler, OnKeyPressed, keyPressedSlot);
89  NazaraSlot(Nz::RenderTarget, OnRenderTargetSizeChange, resizedSlot);
90  NazaraSlot(Nz::Log, OnLogWrite, logSlot);
91  };
92 
93  struct FPSCounterOverlay
94  {
95  Nz::TextSpriteRef sprite;
96  EntityOwner entity;
97  float elapsedTime = 0.f;
98  unsigned int frameCount = 0;
99  };
100  #endif
101 
102  private:
103  #ifndef NDK_SERVER
104  enum OverlayFlags
105  {
106  OverlayFlags_Console = 0x1,
107  OverlayFlags_FPSCounter = 0x2
108  };
109 
110  struct WindowInfo
111  {
112  inline WindowInfo(std::unique_ptr<Nz::Window>&& window);
113 
114  Nz::RenderTarget* renderTarget;
115  std::unique_ptr<Nz::Window> window;
116  std::unique_ptr<ConsoleOverlay> console;
117  std::unique_ptr<FPSCounterOverlay> fpsCounter;
118  std::unique_ptr<World> overlayWorld;
119  };
120 
121  void SetupConsole(WindowInfo& info);
122  void SetupFPSCounter(WindowInfo& info);
123  void SetupOverlay(WindowInfo& info);
124 
125  template<typename T> void SetupWindow(WindowInfo& info, T* renderTarget, std::true_type /*isRenderTarget*/);
126  template<typename T> void SetupWindow(WindowInfo& /*info*/, T* /*renderTarget*/, std::false_type /*isNotRenderTarget*/);
127 
128  std::vector<WindowInfo> m_windows;
129  #endif
130 
131  std::map<Nz::String, Nz::String> m_parameters;
132  std::set<Nz::String> m_options;
133  std::list<World> m_worlds;
134  Nz::Clock m_updateClock;
135 
136  #ifndef NDK_SERVER
137  Nz::UInt32 m_overlayFlags;
138  bool m_exitOnClosedWindows;
139  #endif
140  bool m_shouldQuit;
141  float m_updateTime;
142 
143  static Application* s_application;
144  };
145 }
146 
147 #include <NDK/Application.inl>
148 
149 #endif // NDK_APPLICATION_HPP
Utility class that measure the elapsed time.
Definition: Clock.hpp:20
TODO: For now is unable to display different color in the history, it needs a RichTextDrawer to do so...
Definition: Algorithm.hpp:12
NDK class that represents the owner of the entity and so its lifetime.
Definition: EntityOwner.hpp:14
Core class that represents a string.
Definition: String.hpp:22
Core class that represents a reference to an object.
Definition: ObjectRef.hpp:18
Core class that represents a logger.
Definition: Log.hpp:32
NDK class that represents the application, it offers a set of tools to ease the development.
Definition: Application.hpp:28
NDK class that represents a world.
Definition: World.hpp:25