Repository navigation
Expand file tree
/
Copy pathTactilityApp.cpp
More file actions
163 lines (139 loc) · 4.29 KB
/
Copy pathTactilityApp.cpp
File metadata and controls
163 lines (139 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// The Tactility app's entry point.
//
// Tactility runs an external app by loading its binary and calling its main()
// on the app's own task: a relocated ELF on a device, a dlopen()ed shared
// object in the simulator. This is that main(). It sets up the providers the
// engine needs, hooks the OS's close request into the engine's main loop, and
// runs the game through Deki::Main() like every other target.
//
// What an app build ships, and where the engine finds it:
// assets/ (F:/) the boot payload: project_data.bin and boot.scene
// assets/assets/ (F:/assets/) the asset export: asset_table.bin,
// pack_index.bin and the assets themselves
// S:/ is the app's user data directory, the one place it may write.
#if defined(DEKI_TACTILITY_TARGET) && !defined(DEKI_EDITOR)
#include <deki/Engine.h>
#include <deki/LogSystem.h>
#include <deki/Main.h>
#include <deki/Time.h>
#include <deki/providers/FileSystem.h>
#include <deki/providers/IFileSystem.h>
#include <deki/providers/Memory.h>
#include <memory>
#include "TactilityAudio.h"
#include "TactilityFileSystem.h"
#include "TactilityMemoryProvider.h"
#include "TactilityRuntime.h"
#include "TactilityTimeProvider.h"
extern "C"
{
#include <app/event.h>
#include <tactility/concurrent/task_event_group.h>
#include <tactility/log.h>
}
#ifndef DEKI_TACTILITY_APP_ID
#error "DEKI_TACTILITY_APP_ID must name the app (the Tactility builder defines it from the platform's appId)"
#endif
namespace DekiTactility
{
namespace
{
constexpr const char* kTag = "Deki";
// The OS asks an app to close through an app event, and an app that does not
// listen cannot be closed. The engine's loop polls for it every frame and
// stops when one arrives.
struct CloseListener
{
TaskEventGroup group{};
AppEventSubscription subscription{};
bool subscribed = false;
bool Subscribe()
{
task_event_group_construct(&group);
subscribed = app_event_subscribe(&subscription, &group) == ERROR_NONE;
return subscribed;
}
bool CloseRequested()
{
AppEvent event{};
while (subscribed && app_event_poll(&subscription, &event) == ERROR_NONE)
{
if (event.type == APP_EVENT_CLOSE)
{
return true;
}
}
return false;
}
~CloseListener()
{
if (subscribed)
{
app_event_unsubscribe(&subscription);
}
task_event_group_destruct(&group);
}
};
} // namespace
} // namespace DekiTactility
using namespace DekiTactility;
namespace
{
int RunGame()
{
Deki::LogSystem::SetLogCallback(
[](Deki::LogLevel level, const std::string& msg, const char*, int)
{
if (level == Deki::LogLevel::Error)
{
LOG_E(kTag, "%s", msg.c_str());
}
else if (level == Deki::LogLevel::Warning)
{
LOG_W(kTag, "%s", msg.c_str());
}
else
{
LOG_I(kTag, "%s", msg.c_str());
}
});
CloseListener close;
if (!close.Subscribe())
{
LOG_E(kTag, "Could not subscribe to app events; the OS would have no way to close the game");
return 1;
}
// Providers first: Engine::Initialize() initialises them, then finds the
// assets in F:/assets/.
Deki::Memory::SetBackend(new TactilityMemoryProvider());
Deki::Time::SetTimeProvider(std::make_unique<TactilityTimeProvider>());
if (!Deki::FileSystem::SetFileSystem(new TactilityFileSystem(DEKI_TACTILITY_APP_ID)))
{
return 1;
}
Deki::Engine::GetInstance().RegisterUpdate(
[&close](uint32_t)
{
if (close.CloseRequested())
{
Deki::Engine::GetInstance().StopMainLoop();
}
});
const int result = Deki::Main();
// The speaker's output is the device's only one: give it back.
ShutdownAudio();
return result;
}
} // namespace
extern "C" int main(int argc, char* argv[])
{
(void)argc;
(void)argv;
// Before anything touches a static: on a device nothing has constructed
// them yet (TactilityRuntime.h).
RunStaticConstructors();
const int result = RunGame();
RunStaticDestructors();
return result;
}
#endif // DEKI_TACTILITY_TARGET && !DEKI_EDITOR