-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
168 lines (136 loc) · 4.26 KB
/
main.cpp
File metadata and controls
168 lines (136 loc) · 4.26 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
#include "rulesystem.hpp"
#include "turtle.hpp"
#include "vector2d.hpp"
#include "window.hpp"
#include <SDL2/SDL.h>
#include <SDL2pp/SDL.hh>
#include <SDL2pp/Exception.hh>
#include <cmath>
#include <functional>
#include <iostream>
#include <unordered_map>
#include <vector>
// Simple example
void simple(Turtle& turtle)
{
std::string axiom { "F-F-F-F" };
RuleSystem system {{{ 'F', "F-F+F+FF-F-F+F" }}};
std::string word = system.iterate(axiom, 3);
const int length = 5;
const int angle = 90;
turtle.state.position = { -200, 150 };
turtle.state.angle = 0;
turtle.state.color = { 250, 175, 65 };
turtle.execute(word, length, angle);
}
void koch_snowflake(Turtle& turtle)
{
std::string axiom { "F++F++F" };
RuleSystem system {{{ 'F', "F-F++F-F" }}};
std::string word = system.iterate(axiom, 4);
const double length = 6;
const double angle = 60;
turtle.state.position = { -250, -150 };
turtle.state.angle = 0;
turtle.state.color = { 30, 210, 150 };
turtle.execute(word, length, angle);
}
void sierpinski_arrowhead(Turtle& turtle)
{
std::string axiom { "A" };
RuleSystem system {{
{ 'A', "B-A-B" },
{ 'B', "A+B+A" }
}};
std::string word = system.iterate(axiom, 6);
const double length = 9;
const double angle = 60;
Turtle::CommandTable table = {
{ 'A', [&](){ turtle.forward(length); }},
{ 'B', [&](){ turtle.forward(length); }},
{ '+', [&](){ turtle.state.angle += angle; }},
{ '-', [&](){ turtle.state.angle -= angle; }},
};
turtle.state.position = { -300, -250 };
turtle.state.angle = 0;
turtle.state.color = { 240, 40, 45 };
turtle.execute(word, table);
}
void fern(Turtle& turtle)
{
std::string axiom { "F" };
RuleSystem system {{{ 'F', "F[-F]F[+F]F" }}};
std::string word = system.iterate(axiom, 4);
turtle.state.position = { 0, -290 };
turtle.state.angle = 90;
turtle.state.color = { 20, 165, 45 };
turtle.execute(word, 7, 20);
}
void buch_of_ferns(Turtle& turtle)
{
std::string axiom { "F" };
RuleSystem system {{{ 'F', "F[-F]F[+F]F" }}};
std::vector<std::string> words;
for (int i=0; i<6; ++i) {
words.push_back(system.iterate(axiom, i));
}
const double angle = 20;
const Vector2d<double> start_position { 0, -200 };
for (size_t i=0; i<words.size(); ++i) {
double length = 400 * std::pow(1.0 / 3.0, i);
SDL2pp::Color color(5 + (double)i / (double)words.size() * 250,
5 + (double)i / (double)words.size() * 125,
250 - (double)i / (double)words.size() * 250);
turtle.state.position = start_position;
turtle.state.angle = 150 - 20 * i;
turtle.state.color = color;
turtle.execute(words[i], length, angle);
}
}
int main(int argc, char* argv[])
{
std::vector<std::function<void(Turtle&)>> examples = {
simple, koch_snowflake, sierpinski_arrowhead, fern, buch_of_ferns
};
size_t example_nr = 0;
if (argc > 1) {
try {
example_nr = std::stol(argv[1]);
}
catch (const std::invalid_argument&) {}
catch (const std::out_of_range&) {}
}
if (example_nr < 0) example_nr = 0;
if (example_nr >= examples.size()) example_nr = examples.size() - 1;
try {
Window window {};
Turtle turtle {window};
examples[example_nr](turtle);
window.upate();
// Main loop
SDL_Event event;
bool running = true;
while (running && SDL_WaitEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
running = false;
break;
}
break;
case SDL_WINDOWEVENT:
window.upate();
break;
case SDL_QUIT:
running = false;
break;
}
}
} catch (SDL2pp::Exception& e) {
std::cerr << "Error in: " << e.GetSDLFunction() << std::endl;
std::cerr << " Reason: " << e.GetSDLError() << std::endl;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
}