-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpirographs.cpp
More file actions
70 lines (53 loc) · 1.82 KB
/
Spirographs.cpp
File metadata and controls
70 lines (53 loc) · 1.82 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
#include "defGameEngine.hpp"
constexpr int BIG_GEAR_RADIUS = 100;
constexpr int SMALL_GEAR_RADIUS = 20;
class Spirographs : public def::GameEngine
{
public:
Spirographs()
{
GetWindow()->SetTitle("Spirographs!");
}
def::Vector2i bigGearPos;
float smallGearAngle = 0.0f;
float penAngle = 0.0f;
float speed = 10.0f;
def::Pixel* pathScene = nullptr;
protected:
bool OnUserCreate() override
{
bigGearPos = GetWindow()->GetScreenSize() / 2;
const auto ss = GetWindow()->GetScreenSize();
pathScene = new def::Pixel[ss.x * ss.y];
for (int y = 0; y < ss.y; y++)
for (int x = 0; x < ss.x; x++)
pathScene[y * ss.x + x] = def::NONE;
return true;
}
bool OnUserUpdate(float deltaTime) override
{
def::Vector2i smallGearPos = bigGearPos + def::Vector2f(BIG_GEAR_RADIUS - SMALL_GEAR_RADIUS, smallGearAngle).Cartesian();
def::Vector2i penPos = smallGearPos + def::Vector2f(SMALL_GEAR_RADIUS * 2 / 3, penAngle).Cartesian();
// Change the speeds of changing the angles
smallGearAngle -= deltaTime * 0.1f * speed;
penAngle -= deltaTime * 10.f * speed;
Clear(def::BLACK);
DrawCircle(bigGearPos, BIG_GEAR_RADIUS);
DrawCircle(smallGearPos, SMALL_GEAR_RADIUS);
const auto ss = GetWindow()->GetScreenSize();
pathScene[penPos.y * ss.x + penPos.x] = def::Pixel(fabs(sinf(penAngle)) * 0xFFFFFFFF);
for (int y = 0; y < ss.y; y++)
for (int x = 0; x < ss.x; x++)
{
if (pathScene[y * ss.x + x] != def::NONE)
Draw(x, y, pathScene[y * ss.x + x]);
}
return true;
}
};
int main()
{
Spirographs app;
if (app.Construct(256, 240, 4, 4))
app.Run();
}