-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleExample.cpp
More file actions
133 lines (104 loc) · 2.64 KB
/
ConsoleExample.cpp
File metadata and controls
133 lines (104 loc) · 2.64 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
#include "../Include/defGameEngine.hpp"
struct Ball
{
def::Vector2f pos;
def::Vector2f vel;
def::Pixel col;
};
class Demo : public def::GameEngine
{
public:
Demo()
{
GetWindow()->SetTitle("Demo");
UseOnlyTextures(true);
}
private:
std::vector<Ball> balls;
int ballRadius = 5;
float ballSpeed = 200.0f;
private:
float RandFloat(float start, float end)
{
return start + (float)rand() / (float)RAND_MAX * (end - start);
}
void AddBall(const def::Vector2f& pos, const def::Vector2f& vel)
{
balls.push_back({ pos, vel, def::Pixel(rand() % 256, rand() % 256, rand() % 256) });
}
protected:
bool OnUserCreate() override
{
return true;
}
bool OnConsoleCommand(const std::string& command, std::stringstream& output, def::Pixel& colour) override
{
std::stringstream ss(command);
std::string com;
ss >> com;
if (com == "add")
{
int count = 0;
ss >> count;
for (int i = 0; i < count; i++)
AddBall(GetWindow()->GetScreenSize() / 2, { RandFloat(-1.0f, 1.0f), RandFloat(-1.0f, 1.0f) });
output << "Added " << count << " balls";
}
else if (com == "remove")
{
int count = 0;
ss >> count;
for (int i = 0; i < count; i++)
balls.pop_back();
output << "Removed " << count << " balls";
}
else if (com == "count")
{
output << "Balls count = " << balls.size();
}
else if (com == "clear")
{
GetConsole()->Clear();
}
else
{
output << "Unexpected command";
colour = def::RED;
}
return true;
}
bool OnUserUpdate(float deltaTime) override
{
if (GetInput()->GetButtonState(def::Button::LEFT).pressed)
AddBall(GetWindow()->GetScreenSize() / 2, { RandFloat(-1.0f, 1.0f), RandFloat(-1.0f, 1.0f) });
if (GetInput()->GetKeyState(def::Key::TAB).pressed)
GetConsole()->Show(true);
if (GetInput()->GetKeyState(def::Key::ESCAPE).pressed)
GetConsole()->Show(false);
for (auto& ball : balls)
{
if (ball.pos.x <= ballRadius || ball.pos.x >= GetWindow()->GetScreenWidth() - ballRadius)
{
ball.vel.x *= -1.0f;
ball.pos.x = std::clamp(ball.pos.x, (float)ballRadius, float(GetWindow()->GetScreenWidth() - ballRadius - 1));
}
if (ball.pos.y <= ballRadius || ball.pos.y >= GetWindow()->GetScreenHeight() - ballRadius)
{
ball.vel.y *= -1.0f;
ball.pos.y = std::clamp(ball.pos.y, (float)ballRadius, float(GetWindow()->GetScreenHeight() - ballRadius - 1));
}
ball.pos += ballSpeed * ball.vel * deltaTime;
}
ClearTexture(def::DARK_BLUE);
for (const auto& ball : balls)
FillTextureCircle(ball.pos, ballRadius, ball.col);
return true;
}
};
int main()
{
Demo app;
if (app.Construct(256, 240, 4, 4, false, true))
app.Run();
return 0;
}