-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleForSiv3D.cpp
96 lines (83 loc) · 1.73 KB
/
SampleForSiv3D.cpp
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
# include <Siv3D.hpp>
# include <list>
# include "CoroAsync/Task.hpp"
class Bar
{
public:
Bar(const Point& pos)
: m_value{ 0 },
m_rect{ Arg::center = pos, 150, 30 },
m_rectBar{ Arg::center = pos, 100, 10 } {}
void update()
{
if (m_value < 100)
{
++m_value;
}
}
void draw() const
{
m_rect
.drawShadow(Vec2{ 2, 2 }, 8, 1)
.draw(m_value == 100 ? Palette::Yellow : Palette::White);
m_rectBar.draw(Palette::Gray);
m_rectBar.stretched(0, m_value - 100, 0, 0).draw(Palette::Lightgreen);
}
void setValue(uint32 value)
{
m_value = Clamp(value, 0u, 100u);
}
private:
uint32 m_value;
Rect m_rect;
Rect m_rectBar;
};
cra::Task<> CountUpAsync()
{
int32 i = 0;
while (true)
{
Print << i++;
co_await 100ms;
}
}
cra::Task<> MainAsync()
{
Scene::SetBackground(ColorF{ 0.8, 0.9, 1.0 });
auto task = CountUpAsync();
std::list<Bar> bars;
Array<cra::Task<>> barTasks;
while (System::Update())
{
if (barTasks.size() >= 1000)
{
barTasks.remove_if([](auto&& task) { return task.isReady(); });
}
for (auto&& bar : bars)
{
bar.draw();
}
Circle{ Cursor::Pos(), 40 }.draw(ColorF{ 1, 0, 0, 0.5 });
if (SimpleGUI::Button(U"タスクを生成", Vec2{ 100, 50 }))
{
bars.push_front(Bar{ Point{ RandomClosed(0, Scene::Width()), RandomClosed(0, Scene::Height()) } });
barTasks << [&](auto it, uint32 duration) -> cra::Task<>
{
for ([[maybe_unused]] auto i : step(100))
{
co_await duration;
it->update();
}
co_await 1s;
bars.erase(it);
}(bars.begin(), RandomClosed(10, 50));
}
// co_await での待機中に別のタスクが走る。
// 60FPSに間に合うような待機時間を設定する。
co_await 5ms;
}
}
void Main()
{
MainAsync().wait();
}