-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
190 lines (168 loc) · 5.07 KB
/
main.cpp
File metadata and controls
190 lines (168 loc) · 5.07 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "MyWidget.hpp"
#include <Tails/Application.hpp>
#include <Tails/World/Actor.hpp>
#include <Tails/World/Components/SpriteComponent.hpp>
#include <Tails/World/WorldSubsystem.hpp>
#include <Tails/World/Level.hpp>
#include <Tails/String.hpp>
#include <Tails/World/ActorRegistry.hpp>
#include <Tails/UI/WidgetSubsystem.hpp>
#include <Tails/UI/Layout/StackBox.hpp>
#include <Tails/UI/Layout/Canvas.hpp>
#include <Tails/Memory.hpp>
#include <Tails/Log.hpp>
#include <Tails/Input/Event.hpp>
#include <Tails/Templated/Array.hpp>
#include <Tails/Audio/AudioSubsystem.hpp>
#include <Tails/Audio/BusHandle.hpp>
#include <Tails/Assets/Sound.hpp>
#include <Tails/Assets/AssetPtr.hpp>
#include <Tails/Window.hpp>
#include <Tails/Debug.hpp>
#include <Tails/Input/InputSubsystem.hpp>
#include <Tails/Templated/Slice.hpp>
class CTestActor : public tails::CActor
{
public:
CTestActor()
{
auto const sprite = createComponent<tails::CSpriteComponent>();
sprite->size = {32.f, 32.f};
sprite->colour = tails::SColour::magenta;
setRootComponent(sprite);
}
};
TAILS_REGISTER_ACTOR(CTestActor, "TestActor")
class CTestActor2 : public tails::CActor
{
public:
CTestActor2()
{
auto const sprite = createComponent<tails::CSpriteComponent>();
sprite->size = {32.f, 32.f};
sprite->colour = tails::SColour::cyan;
setRootComponent(sprite);
}
};
TAILS_REGISTER_ACTOR(CTestActor2, "TestActor2")
struct STestStruct
{
STestStruct(const int j)
: i(j)
{}
int i {0};
};
namespace
{
std::shared_ptr<CMyWidget> gMyWidget;
void pollInputCallback(const tails::CEvent& ev)
{
if (auto const keyEv = ev.getIf<tails::CEvent::SKeyDown>())
{
if (keyEv->key == tails::input::EKeys::Escape)
tails::app::exit();
}
}
}
int main(const int argc, char* argv[])
{
using namespace tails;
// app initialisation
constexpr SWindowInfo windowInfo {
.title = "My game!",
};
if (!app::init(argc, argv, windowInfo))
return -1;
// world and levels
{
auto const level = world::getCurrentLevel();
if (!level)
return 0;
level->spawnActor(
"Player",
{
{0.f, -128.f},
0.f,
{1.f, 1.f}
},
1
);
auto test = level->spawnActor(
"TestActor",
{{}, 0.f, {2.f, 4.f}},
-1
);
auto transform = test->getTransform();
transform.setOrigin({0.5f});
test->setTransform(transform);
}
// widgets
{
gMyWidget = ui::createWidget<CMyWidget>(ui::getRootPanel());
const std::vector colours {SColour::magenta, SColour::blue, SColour::green, SColour::red};
gMyWidget->refreshContents(colours);
auto const myWidgetSlot = ui::CCanvas::slotAsCanvasSlot(gMyWidget);
myWidgetSlot->position.x = 16.f;
myWidgetSlot->position.y = 16.f;
}
// memory functions
{
auto const testStruct = mem::alloc<STestStruct>();
TAILS_LOG_VA(Game, Message, "Test Struct value: {}", testStruct->i);
mem::construct(*testStruct, 7);
TAILS_LOG_VA(Game, Message, "Test Struct value: {}", testStruct->i);
mem::destroy(testStruct);
}
// dynamic array
{
TArray<STestStruct> testStructs;
testStructs.add(STestStruct {55});
testStructs.emplace(76);
for (usize i {0}; i < testStructs.size(); i++)
{
TAILS_LOG(Game, Message, TAILS_FMT("TestStruct {} value: {}", i, testStructs[i].i));
}
}
// sounds
{
// doesn't play the sound (yet), just testing that it fails gracefully
TAssetPtr<CSound> mySound {"test_sound.wav"};
auto testBusHandle = audio::addBus("test_bus");
testBusHandle.playSound(mySound.load());
}
// slice
{
TArray numbers {1, 2, 3, 6, 4, 5, 7, 8, 9};
TSlice<int> slice {numbers.begin(), numbers.end()};
for (usize i {0}; i < slice.size(); i++)
{
TAILS_LOG(Game, Message, TAILS_FMT("Slice {} value: {}", i, slice[i]));
}
}
// clang thinks i is unused! So commenting out for now
//TStaticArray<int, 5> numbers {4, 2, 6, 4, 8};
//for (const auto i : numbers)
//{
// TAILS_LOG(Game, Message, TAILS_FMT("Number is: {}", i));
//}
// default run sequence
//app::run();
auto level = world::getCurrentLevel();
auto testActor = level->spawnActor<CTestActor2>(
{{128.f}, 0.f, {2.f}}
);
// customised run sequence with custom input polling callback (to close the window with the escape key)
while (!app::shouldExit())
{
app::startFrame();
app::pollInput(pollInputCallback);
app::tick(app::getCurrentFrameInfo().getDeltaSeconds());
// doesn't work at the moment!
auto mousePos = level->screenToWorld(input::getMousePosition());
testActor->setPosition(mousePos);
app::render();
app::endFrame();
}
app::deinit();
return 0;
}