-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_create_destroy.cpp
More file actions
113 lines (92 loc) · 4.23 KB
/
Copy pathtest_create_destroy.cpp
File metadata and controls
113 lines (92 loc) · 4.23 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
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <random>
#include <format>
#include <vector>
#include <nothofagus.h>
int generateRandomInt(int min, int max)
{
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(min, max);
return dist(rng);
}
float generateRandomFloat(float min, float max)
{
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_real_distribution<> dist(min, max);
return dist(rng);
}
glm::vec2 generateRandomPosition(unsigned int width, unsigned int height)
{
return {generateRandomFloat(0, width), generateRandomFloat(0, height)};
}
Nothofagus::TextureId addTextureWithText(Nothofagus::Canvas& canvas, Nothofagus::ColorPallete pallete, const std::string& text)
{
const glm::vec4 bgColor = pallete.colors.empty() ? glm::vec4(0.0f) : pallete.colors[0];
const glm::vec4 fgColor = pallete.colors.size() > 1 ? pallete.colors[1] : glm::vec4(1.0f);
Nothofagus::IndirectTexture texture = Nothofagus::makeTextTexture(text, Nothofagus::FontType::Basic, fgColor, bgColor);
return canvas.addTexture(texture);
}
Nothofagus::BellotaId addBellotaWithWrittenId(Nothofagus::Canvas& canvas, Nothofagus::TextureId dummyTextureId, Nothofagus::ColorPallete pallete, glm::vec2 position)
{
/* We do not know the bellota id before inserting the bellota into the canvas, so we insert the
* bellota with a fallback texture, and then change it for the new one with the text
*/
Nothofagus::BellotaId bellotaId = canvas.addBellota({{position}, dummyTextureId});
Nothofagus::Bellota& bellota = canvas.bellota(bellotaId);
// This is to ensure bellotas are drawn in order according to their id. This way, transparency allow proper visibility.
bellota.depthOffset() = -127 + bellotaId.id;
const std::string text = std::format("{}", bellotaId.id);
Nothofagus::TextureId textureId = addTextureWithText(canvas, pallete, text);
canvas.setTexture(bellotaId, textureId);
return bellotaId;
}
int main()
{
Nothofagus::ScreenSize screenSize{150, 100};
Nothofagus::Canvas canvas(screenSize, "Creating and destroying bellotas", {0.7, 0.7, 0.7}, 6);
Nothofagus::ColorPallete pallete{
{0.0, 0.0, 0.0, 0.8},
{1.0, 1.0, 1.0, 0.8 }
};
Nothofagus::IndirectTexture dummyTexture({2, 2}, {1.0, 1.0, 1.0, 1.0});
Nothofagus::TextureId dummyTextureId = canvas.addTexture(dummyTexture);
// this invisible Bellota is just to keep the texture alive, as Nothofagus removes unused textures every frame.
Nothofagus::BellotaId dummyBellotaId = canvas.addBellota({{{10.0, 10.0}}, dummyTextureId});
Nothofagus::Bellota& dummyBellota = canvas.bellota(dummyBellotaId);
dummyBellota.visible() = false;
std::vector<Nothofagus::BellotaId> bellotaIds;
auto update = [&](float dt)
{
ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f), ImGuiCond_Once);
ImGui::Begin("Use W to create and S to destroy");
ImGui::Text("Handling %zu bellotas", bellotaIds.size());
ImGui::End();
};
Nothofagus::Controller controller;
controller.registerAction({Nothofagus::Key::W, Nothofagus::DiscreteTrigger::Press}, [&]()
{
glm::vec2 randomPosition = generateRandomPosition(screenSize.width, screenSize.height);
Nothofagus::BellotaId newBellotaId = addBellotaWithWrittenId(canvas, dummyTextureId, pallete, randomPosition);
bellotaIds.push_back(newBellotaId);
spdlog::info("Bellota {} created!", newBellotaId.id);
});
controller.registerAction({Nothofagus::Key::S, Nothofagus::DiscreteTrigger::Press}, [&]()
{
if (bellotaIds.empty())
return;
std::size_t bellotaIndexToDelete = generateRandomInt(0, bellotaIds.size() - 1);
Nothofagus::BellotaId bellotaIdToDelete = bellotaIds.at(bellotaIndexToDelete);
canvas.removeBellota(bellotaIdToDelete);
// replacing the element that we wish to remove for the last one and then removing the last one.
bellotaIds.at(bellotaIndexToDelete) = bellotaIds.back();
bellotaIds.pop_back();
spdlog::info("Bellota {} destroyed :(", bellotaIdToDelete.id);
});
canvas.run(update, controller);
return 0;
}