-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
93 lines (79 loc) · 2.07 KB
/
main.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
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "../../include/wasm-dom.hpp"
auto canvas = dom::HTMLDocument::current()
.querySelector("#canvas")
.cast<dom::HTMLCanvasElement>();
auto ctx = canvas.getContext("2d");
const int height = dom::Window::current().getInnerHeight();
const int width = dom::Window::current().getInnerWidth();
namespace Math {
constexpr double PI = 3.141592653589793238463;
}
class Vector {
public:
double x, y;
Vector(double x = 0, double y = 0) : x(x), y(y) {}
};
class Ball {
private:
Vector pos;
Vector vel;
int radius;
public:
Ball(int x = 0, int y = 0, Vector vel = Vector(), int radius = 10)
: pos(x, y), vel(vel), radius(radius) {}
void show() {
ctx.beginPath();
ctx.arc(pos.x, pos.y, radius, 0, 2 * Math::PI);
ctx.closePath();
ctx.fill();
}
void update() {
double &x = pos.x;
double &y = pos.y;
x += vel.x;
y += vel.y;
if (x >= width || x <= 0) {
vel.x = -vel.x;
}
if (y >= height || y <= 0) {
vel.y = -vel.y;
}
}
};
void update(void *vector) {
ctx.clearRect(0, 0, width, height);
for (auto &b : *static_cast<std::vector<Ball> *>(vector)) {
b.update();
b.show();
}
static unsigned char rgbHexParts[3] = {0x00, 0x00, 0x00};
char style[8];
sprintf(style, "#%2.2x%2.2x%2.2x", rgbHexParts[0], rgbHexParts[1],
rgbHexParts[2]);
for (int i = 0; i < 3; i++) {
rgbHexParts[i] = rand() % 256;
}
printf("%ld\n", time(NULL)); // roughly track fps
ctx.setFillStyle(style);
}
void setUp() {
canvas.setHeight(height);
canvas.setWidth(width);
srand(time(NULL));
}
int main(int argc, char **argv) {
setUp();
static std::vector<Ball>
balls; // static, otherwise will leave scope after first update() call
for (int i = 0; i < 20; i++) {
balls.push_back(Ball(rand() % width, rand() % height,
Vector(rand() % 40 - 20, rand() % 40 - 20),
rand() % 35));
}
emscripten_set_main_loop_arg(update, &balls, 0, 0);
return 0;
}