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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
#include "common.hpp"
static sf::RectangleShape wot_rect;
static sf::Font font;
namespace render {
state_t::state_t(sf::RenderWindow *window_)
{
window = window_;
font.loadFromFile("assets/FanwoodText.otf");
prng.seed(396);
perlin.generate(&prng, 16);
}
// FIXME: rename
static sf::Texture *tiles[256] = {0};
void register_tile(uint8_t type, const char *path)
{
printf("load %s\n", path);
tiles[type] = new sf::Texture;
tiles[type]->loadFromFile(path);
tiles[type]->setRepeated(true);
}
void state_t::begin_frame(double now_, double dt_)
{
now = now_;
dt = dt_;
window->clear();
stats.sectors = 0;
stats.tiles = 0;
stats.entities = 0;
}
void state_t::end_frame(void)
{
window->display();
}
void state_t::drender_text(rectf_t rect, std::string str)
{
sf::Text text(str, font, 20);
sf::FloatRect text_rect;
v2f_t text_size, x;
text.setScale(0.006f, 0.006f);
text_rect = text.getGlobalBounds();
text_size = v2f_t(text_rect.width, text_rect.height);
x = rect.center() - text_size / 2;
text.setPosition(x + v2f_t(0.01f, 0.01f));
text.setFillColor(sf::Color::Black);
window->draw(text);
text.setPosition(x);
text.setFillColor(sf::Color::White);
window->draw(text);
}
void state_t::drender_entity(world::entity_t *ent)
{
std::stringstream ss;
render_hlrect(ent->render_bounds, sf::Color::Red);
render_hlrect(ent->cmodel.bounds, sf::Color::Yellow);
ss << (void*)ent << "\n";
ss << "CF=" << ent->cmodel.cflags;
drender_text(ent->render_bounds, ss.str());
}
static const v2f_t tile_base[8] ={
{1.0f, 0.5f}, {1.0f, 1.0f}, {0.5f, 1.0f}, {0.0f, 1.0f},
{0.0f, 0.5f}, {0.0f, 0.0f}, {0.5f, 0.0f}, {1.0f, 0.0f}
};
static void generate_tile_verts(sf::Vertex *verts, v2f_t tx, procgen::perlin_noise_t *perlin)
{
for (size_t i = 0; i < 8; i++) {
v2f_t base, turb;
base = tile_base[i] + tx;
turb[0] = perlin->get(base, 1.0f) * 0.5f;
turb[1] = perlin->get(base, 2.0f) * 0.5f;
verts[i].position = base + turb;
verts[i].texCoords = (base + turb) * 32.0f;
verts[i].color = sf::Color::White;
}
}
void state_t::render_sector(world::sector_t *sector)
{
for (ssize_t y = 0; y < SECTOR_SIZE; y++)
for (ssize_t x = 0; x < SECTOR_SIZE; x++) {
sf::Vertex verts[8];
sf::Texture *texture;
v2f_t tx;
world::tile_t *tile;
tx = sector->bounds.v[0] + v2f_t(x, y);
tile = sector->tiles + y * SECTOR_SIZE + x;
texture = tiles[tile->type];
if (!texture) {
printf("draw_tile: tile %i not registered\n", tile->type);
abort();
}
sf::RenderStates states(texture);
generate_tile_verts(verts, tx, &perlin);
window->draw(verts, 8, sf::TrianglesFan, states);
if (debug_draw_tile_coords) {
world::tile_index_t local(x, y);
std::stringstream ss;
ss << "L=" << local;
sf::Text text(ss.str(), font, 20);
text.setPosition(tx);
text.setScale(0.005, 0.005);
window->draw(text);
}
stats.tiles++;
}
stats.sectors++;
}
void state_t::render(game::state_t *game)
{
sf::Vector2u size = window->getSize();
v2f_t A, B, C, D;
const v2f_t margin(1.0f, 1.0f);
rectf_t bbox;
std::list<world::entity_t*> ents;
A = window->mapPixelToCoords(sf::Vector2i(0, 0));
B = window->mapPixelToCoords(sf::Vector2i(size.x, 0));
C = window->mapPixelToCoords(sf::Vector2i(0, size.y));
D = window->mapPixelToCoords(sf::Vector2i(size.x, size.y));
bbox[0][0] = std::min({A[0], B[0], C[0], D[0]});
bbox[0][1] = std::min({A[1], B[1], C[1], D[1]});
bbox[1][0] = std::max({A[0], B[0], C[0], D[0]});
bbox[1][1] = std::max({A[1], B[1], C[1], D[1]});
bbox[0] -= margin;
bbox[1] += margin;
for (world::sector_t *sector : game->world.get_sectors(bbox))
render_sector(sector);
ents = game->world.get_render_entities(bbox);
ents.sort(
[](const world::entity_t *x, const world::entity_t *y) -> bool
{
if (x->render_layer < y->render_layer)
return true;
else if (x->render_layer > y->render_layer)
return false;
else
return x->render_bounds[1][1] < y->render_bounds[1][1];
});
for (world::entity_t *ent : ents) {
ent->render_to(this);
if (debug_draw_cmodels)
drender_entity(ent);
stats.entities++;
}
}
void state_t::render(double phase, animated_texture_t *anim, rectf_t bounds, sf::Color color, bool mirror){
size_t frame;
if (!anim)
return;
if (!anim->frame_count)
return;
frame = floor(fmod(phase, 1.0) * anim->frame_count);
wot_rect.setTexture(anim->frames + frame, true);
wot_rect.setFillColor(color);
if (!mirror) {
wot_rect.setPosition(bounds[0]);
wot_rect.setSize(bounds[1] - bounds[0]);
} else {
float dx = bounds[1][0] - bounds[0][0];
wot_rect.setPosition(bounds[0] + v2f_t(dx, 0));
wot_rect.setSize(bounds[1] - bounds[0]);
wot_rect.setScale(v2f_t(-1, 1));
}
window->draw(wot_rect);
wot_rect.setTexture(NULL);
wot_rect.setScale(v2f_t(1, 1));
}
void state_t::render(double phase, oriented_sprite_t *sprite, rectf_t bounds, float angle)
{
size_t index;
bool mirror;
index = sprite->select_index(angle, &mirror);
render(phase, sprite->textures + index, bounds, sf::Color::White, mirror);
}
void state_t::render_text(v2f_t x, float height, std::string str,
text_align_t align, sf::Color color)
{
sf::Text text(sf::String::fromUtf8(str.begin(), str.end()), font, 40);
sf::FloatRect rect;
float scale;
v2f_t offset;
rect = text.getGlobalBounds();
scale = height / 40.0f;
switch (align) {
case ALIGN_LEFT_TOP:
offset = v2f_t(0, 0);
break;
case ALIGN_CENTER_BOTTOM:
offset[0] = -rect.width / 2;
offset[1] = -rect.height;
break;
}
offset *= scale;
text.setScale(scale, scale);
text.setPosition(x + offset);
text.setFillColor(color);
window->draw(text);
}
void state_t::render_rect(rectf_t rect, sf::Color color)
{
wot_rect.setSize(rect.dims());
wot_rect.setPosition(rect[0]);
wot_rect.setFillColor(color);
window->draw(wot_rect);
}
void state_t::render_hlrect(rectf_t rect, sf::Color color)
{
sf::Color fill;
fill = sf::Color(color.r, color.g, color.b, 50);
wot_rect.setSize(rect.dims());
wot_rect.setPosition(rect[0]);
wot_rect.setFillColor(fill);
wot_rect.setOutlineThickness(0.01);
wot_rect.setOutlineColor(color);
window->draw(wot_rect);
wot_rect.setOutlineColor(sf::Color::Transparent);
}
void state_t::render_line(v2f_t x0, v2f_t x1, sf::Color color)
{
sf::Vertex line[2] = {
sf::Vertex(x0, color),
sf::Vertex(x1, color)
};
window->draw(line, 2, sf::Lines);
}
void state_t::debug_path(std::list<v2f_t> *path)
{
bool first = true;
sf::Vertex line[2];
for (v2f_t &point : *path) {
line[1] = line[0];
line[0] = sf::Vertex(point, sf::Color::Blue);
if (first) {
first = false;
continue;
}
window->draw(line, 2, sf::Lines);
}
}
animated_texture_t::~animated_texture_t(void)
{
delete[] frames;
}
bool animated_texture_t::load(std::string prefix, size_t frame_count_)
{
frame_count = frame_count_;
frames = new sf::Texture[frame_count];
for (size_t i = 0; i < frame_count; i++) {
std::string path;
path = prefix + std::to_string(i) + ".png";
std::cout << "load " << path << "\n";
if (!frames[i].loadFromFile(path)) {
delete[] frames;
frames = NULL;
frame_count = 0;
return false;
}
}
return true;
}
oriented_sprite_t::~oriented_sprite_t(void)
{
delete[] textures;
}
float normalize_angle(float angle)
{
float t;
t = angle / (2 * M_PI);
t -= floor(t);
return t * 2 * M_PI;
}
size_t oriented_sprite_4M_t::select_index(float angle, bool *mirror)
{
angle = normalize_angle(angle);
if (angle < 0.25f * M_PI) {
select_x:
*mirror = false;
return 0;
} else if (angle < 0.75f * M_PI) {
*mirror = false;
return 1;
} else if (angle < 1.25f * M_PI) {
*mirror = true;
return 0;
} else if (angle < 1.75f * M_PI) {
*mirror = false;
return 2;
} else
goto select_x;
}
void oriented_sprite_4M_t::load(std::string prefix, size_t xc, size_t yc, size_t nyc)
{
textures = new animated_texture_t[3];
textures[0].load(prefix + "_x_", xc);
textures[1].load(prefix + "_y_", yc);
textures[2].load(prefix + "_ny_", nyc);
}
size_t oriented_sprite_4M2_t::select_index(float angle, bool *mirror)
{
angle = normalize_angle(angle + 0.01f);
if (angle < 0.25f * M_PI) {
select_x:
*mirror = false;
return 0;
} else if (angle < 0.75f * M_PI) {
*mirror = false;
return 1;
} else if (angle < 1.25f * M_PI) {
*mirror = true;
return 0;
} else if (angle < 1.75f * M_PI) {
*mirror = false;
return 1;
} else
goto select_x;
}
void oriented_sprite_4M2_t::load(std::string prefix, size_t xc, size_t yc)
{
textures = new animated_texture_t[2];
textures[0].load(prefix + "_x_", xc);
textures[1].load(prefix + "_y_", yc);
}
} // namespace render
|