Kale
Loading...
Searching...
No Matches
Application.hpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
22
23#include <string>
24#include <memory>
25#include <thread>
26#include <list>
27#include <vector>
28#include <mutex>
29#include <queue>
30#include <functional>
31#include <condition_variable>
32
36int main();
37
38namespace Kale {
39
44 private:
45
49 static std::vector<std::function<void()>> nodeSetupFuncs;
50
54 static std::vector<std::function<void()>> nodeCleanupFuncs;
55
59 std::list<std::thread> updateThreads;
60
64 std::queue<std::function<void()>> tasks;
65
69 std::mutex threadSyncMutex;
70
74 std::mutex taskManagerMutex;
75
79 std::condition_variable threadSyncCondVar;
80
84 std::condition_variable renderSyncCondVar;
85
90
95
100
105
109 std::shared_ptr<Scene> presentedScene;
110
114 std::shared_ptr<Scene> sceneToPresent;
115
120 void update(size_t threadNum) noexcept;
121
125 void synchronizeUpdate();
126
127 protected:
128
133
137 virtual void onBegin() {}
138
142 virtual void onEnd() {}
143
147 void run() noexcept;
148
153 explicit Application(const char* applicationName) noexcept;
154
155 friend int ::main();
156
157 public:
158
162 const std::string applicationName;
163
164 // App class doesn't support copying/moving
165 Application(const Application& other) = delete;
166 Application(Application&& other) = delete;
167 void operator=(const Application& other) = delete;
168 void operator=(Application&& other) = delete;
169
174 Window& getWindow() noexcept;
175
180 [[nodiscard]] std::shared_ptr<Scene> getPresentedScene() noexcept;
181
186 [[nodiscard]] const Window& getWindow() const noexcept;
187
192 [[nodiscard]] std::shared_ptr<const Scene> getPresentedScene() const noexcept;
193
198 void presentScene(const std::shared_ptr<Scene>& scene);
199
204 [[nodiscard]] size_t getNumUpdateThreads() const noexcept;
205
211 void runTaskOnMainThread(std::function<void()> task);
212
217 std::string getAssetFolderPath() const;
218
224 static void addNodeSetupFunction(std::function<void()> func) {
225 nodeSetupFuncs.push_back(func);
226 }
227
233 static void addNodeCleanupFunction(std::function<void()> func) {
234 nodeCleanupFuncs.push_back(func);
235 }
236 };
237
241 inline Application* mainApp = nullptr;
242}
243
Kale::Application * createApplication()
int main()
std::string getAssetFolderPath() const
std::condition_variable threadSyncCondVar
static std::vector< std::function< void()> > nodeCleanupFuncs
std::condition_variable renderSyncCondVar
static void addNodeSetupFunction(std::function< void()> func)
void runTaskOnMainThread(std::function< void()> task)
std::shared_ptr< Scene > presentedScene
virtual void onBegin()
const std::string applicationName
std::shared_ptr< Scene > sceneToPresent
std::mutex taskManagerMutex
std::list< std::thread > updateThreads
virtual void onEnd()
std::mutex threadSyncMutex
static void addNodeCleanupFunction(std::function< void()> func)
static std::vector< std::function< void()> > nodeSetupFuncs
void presentScene(const std::shared_ptr< Scene > &scene)
size_t getNumUpdateThreads() const noexcept
void run() noexcept
friend int::main()
std::queue< std::function< void()> > tasks
Window & getWindow() noexcept
std::shared_ptr< Scene > getPresentedScene() noexcept
void update(size_t threadNum) noexcept
Application * mainApp