-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdl_demo.cpp
More file actions
313 lines (254 loc) · 6.15 KB
/
sdl_demo.cpp
File metadata and controls
313 lines (254 loc) · 6.15 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright (C) 2018 - Sebastien Alaiwan
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
///////////////////////////////////////////////////////////////////////////////
// simulation
#include "collision.h"
#include <vector>
#include <cmath>
using namespace std;
struct Input
{
bool left, right, up, down;
bool force;
bool changeShape;
bool quit;
};
struct World
{
Vec2 pos;
Vec2 rayEnd;
float angle;
Shape shape = Circle;
vector<Segment> segments;
};
Vec2 direction(float angle)
{
return Vec2(cos(angle), sin(angle));
}
template<size_t N>
void pushPolygon(vector<Segment>& out, Vec2 const (&data)[N])
{
for(size_t i = 0; i < N; ++i)
{
auto a = data[(i + 0) % N];
auto b = data[(i + 1) % N];
out.push_back(Segment { a, b });
}
}
World createWorld()
{
World world;
world.pos = Vec2(4, 2);
world.angle = 0;
static const Vec2 points1[] =
{
Vec2(8, -3),
Vec2(8, 2),
Vec2(12, 2),
Vec2(12, 3),
Vec2(12, 5),
Vec2(14, 5),
Vec2(12, 5),
Vec2(12, 7),
Vec2(15, 7),
Vec2(15, 16),
Vec2(-3, 16),
Vec2(-3, 14),
Vec2(10, 14),
Vec2(10, 4),
Vec2(8, 4),
Vec2(4, 4),
Vec2(4, 5),
Vec2(8, 8),
Vec2(3, 8),
Vec2(3, 13),
Vec2(1, 13),
Vec2(-1, 13),
Vec2(-3, 13),
Vec2(-3, 10),
Vec2(1, 10),
Vec2(1, 8),
Vec2(0, 8),
Vec2(-1, 3.5),
Vec2(-2, 3.5),
Vec2(-2, 3.0),
Vec2(-3, 3.0),
Vec2(-3, 2.5),
Vec2(-4, 2.5),
Vec2(-4, 2),
Vec2(-5, 2),
Vec2(-5, -3),
};
pushPolygon(world.segments, points1);
static const Vec2 points2[] =
{
Vec2(-1, -2.0),
Vec2(-2, -2.0),
Vec2(-1.5, -1),
};
pushPolygon(world.segments, points2);
static const Vec2 points3[] =
{
Vec2(2, -3),
Vec2(3, -2),
Vec2(4, -3),
Vec2(3, -2),
Vec2(4, -1),
Vec2(5, -2),
Vec2(6, -3),
};
pushPolygon(world.segments, points3);
static const Vec2 points4[] =
{
Vec2(12, 7),
Vec2(12, 9),
Vec2(13, 9),
Vec2(13, 7),
};
pushPolygon(world.segments, points4);
static const Vec2 points5[] =
{
Vec2(12, 9),
Vec2(12, 11),
Vec2(13, 11),
Vec2(13, 9),
};
pushPolygon(world.segments, points5);
return world;
}
void tick(World& world, Input input)
{
float omega = 0;
float thrust = 0;
if(input.left)
omega += 0.1;
if(input.right)
omega -= 0.1;
if(input.down)
thrust -= 0.08;
if(input.up)
thrust += 0.08;
if(input.changeShape)
world.shape = Shape(1 - world.shape);
world.angle += omega;
auto const delta = direction(world.angle) * thrust;
auto segments = span<Segment> { world.segments.size(), world.segments.data() };
if(input.force)
world.pos += delta;
else
slideMove(world.pos, world.shape, delta, segments);
{
auto delta = direction(world.angle) * 10;
auto ratio = raycast(world.pos, world.pos + delta, segments);
world.rayEnd = world.pos + delta * ratio;
}
}
///////////////////////////////////////////////////////////////////////////////
// sdl entry point
#include <stdexcept>
#include "SDL.h"
#include "collision.h"
void drawScreen(SDL_Renderer* renderer, World& world)
{
auto transform = [] (Vec2 v)
{
auto const scale = 20.0;
SDL_Point r;
r.x = 220 + v.x * scale;
r.y = 400 - v.y * scale;
return r;
};
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
for(auto& segment : world.segments)
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
auto a = transform(segment.a);
auto b = transform(segment.b);
SDL_RenderDrawLine(renderer, a.x, a.y, b.x, b.y);
SDL_Rect rect { a.x - 2, a.y - 2, 4, 4 };
SDL_RenderDrawRect(renderer, &rect);
}
{
SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE);
{
auto origin = transform(world.pos);
auto target = transform(world.pos + direction(world.angle) * RAY);
SDL_RenderDrawLine(renderer, origin.x, origin.y, target.x, target.y);
}
if(world.shape == Circle)
{
auto const N = 20;
auto prev = transform(world.pos);
for(int i = 0; i <= N; ++i)
{
auto a = transform(world.pos + direction(world.angle + i * 2 * M_PI / N) * RAY);
SDL_RenderDrawLine(renderer, a.x, a.y, prev.x, prev.y);
prev = a;
}
}
else
{
SDL_Point corners[4];
corners[0] = transform(world.pos + Vec2(-RAY, -RAY));
corners[1] = transform(world.pos + Vec2(-RAY, +RAY));
corners[2] = transform(world.pos + Vec2(+RAY, +RAY));
corners[3] = transform(world.pos + Vec2(+RAY, -RAY));
corners[4] = corners[0];
SDL_RenderDrawLines(renderer, corners, 5);
}
}
{
auto a = transform(world.pos);
auto b = transform(world.rayEnd);
SDL_SetRenderDrawColor(renderer, 0, 255, 255, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer, a.x, a.y, b.x, b.y);
}
SDL_RenderPresent(renderer);
}
Input readInput()
{
Input r {};
SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_ESCAPE))
r.quit = true;
if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE)
r.changeShape = true;
}
auto keys = SDL_GetKeyboardState(nullptr);
r.left = keys[SDL_SCANCODE_LEFT];
r.right = keys[SDL_SCANCODE_RIGHT];
r.down = keys[SDL_SCANCODE_DOWN];
r.up = keys[SDL_SCANCODE_UP];
r.force = keys[SDL_SCANCODE_HOME];
return r;
}
int main()
{
if(SDL_Init(SDL_INIT_VIDEO) != 0)
throw runtime_error("Can't init SDL");
SDL_Window* window;
SDL_Renderer* renderer;
if(SDL_CreateWindowAndRenderer(640, 480, 0, &window, &renderer) != 0)
{
SDL_Quit();
throw runtime_error("Can't create window");
}
auto world = createWorld();
while(1)
{
auto const input = readInput();
if(input.quit)
break;
tick(world, input);
drawScreen(renderer, world);
SDL_Delay(10);
}
SDL_Quit();
return 0;
}