-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDL2.cpp
180 lines (152 loc) · 3.31 KB
/
SDL2.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
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
169
170
171
172
173
174
175
176
177
178
179
180
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#include <cmath>
#include <vector>
class Screen
{
SDL_Event e;
SDL_Window* window;
SDL_Renderer* renderer;
std::vector<SDL_FPoint> points;
public:
Screen()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(640*1,480*1,0,&window,&renderer);
SDL_RenderSetScale(renderer,1,1);
}
void pixel(float x, float y)
{
points.emplace_back(x,y);
}
void clear()
{
points.clear();
}
void show()
{
SDL_SetRenderDrawColor(renderer,0,0,0,255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer,255,255,255,255);
for(auto& point : points)
{
SDL_RenderDrawPointF(renderer, point.x, point.y);
}
SDL_RenderPresent(renderer);
}
void input()
{
while(SDL_PollEvent(&e))
{
if(e.type == SDL_QUIT)
{
SDL_Quit();
exit(0);
}
}
}
};
struct vec3
{
float x,y,z;
};
struct connection
{
int a,b;
};
void rotate(vec3& point, float x = 1, float y = 1, float z = 1)
{
float rad = 0;
rad = x;
point.y = std::cos(rad) * point.y - std::sin(rad) * point.z;
point.z = std::sin(rad) * point.y + std::cos(rad) * point.z;
rad = y;
point.x = std::cos(rad) * point.x + std::sin(rad) * point.z;
point.z = -std::sin(rad) * point.x + std::cos(rad) * point.z;
rad = z;
point.x = std::cos(rad) * point.x - std::sin(rad) * point.y;
point.y = std::sin(rad) * point.x + std::cos(rad) * point.y;
}
void line(Screen& screen, float x1, float y1, float x2, float y2)
{
float dx = x2 - x1;
float dy = y2 - y1;
float length = std::sqrt(dx * dx + dy * dy);
float angle = std::atan2(dy,dx);
for(float i = 0; i < length; i++)
{
screen.pixel(
x1 + std::cos(angle) * i,
y1 + std::sin(angle) * i
);
}
}
int main(int argc, char* argv[])
{
Screen screen;
std::vector<vec3> points
{
{100,100,100},
{200,100,100},
{200,200,100},
{100,200,100},
{100,100,200},
{200,100,200},
{200,200,200},
{100,200,200}
};
std::vector<connection> connections
{
{0,4},
{1,5},
{2,6},
{3,7},
{0,1},
{1,2},
{2,3},
{3,0},
{4,5},
{5,6},
{6,7},
{7,4}
};
vec3 c{0,0,0};
for(auto& p : points)
{
c.x += p.x;
c.y += p.y;
c.z += p.z;
}
c.x /= points.size();
c.y /= points.size();
c.z /= points.size();
while(true)
{
for(auto& p : points)
{
p.x -= c.x;
p.y -= c.y;
p.z -= c.z;
rotate(p, 0.002, 0.001, 0.004);
p.x += c.x;
p.y += c.y;
p.z += c.z;
screen.pixel(p.x, p.y);
}
for(auto& conn : connections)
{
line(
screen,
points[conn.a].x,
points[conn.a].y,
points[conn.b].x,
points[conn.b].y
);
}
screen.show();
screen.clear();
screen.input();
SDL_Delay(30);
}
return 0;
}