-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRendering.c
464 lines (396 loc) · 12.5 KB
/
Rendering.c
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "Rendering.h"
Term_s *initDisplaying()
{
raw();
noecho();
curs_set(0);
if (has_colors() == FALSE)
{
endwin();
printf("Your terminal does not support color\n");
exit(1);
}
start_color();
Term_s *term = calloc(1, sizeof(Term_s));
term->height = getmaxy(stdscr);
term->width = getmaxx(stdscr);
term->displayMode = WORLD;
term->world = createWindow(term->height - 5, term->width, 0, 0);
term->stats = createWindow(5, term->width, term->height - 5, 0);
// term->crafts = createWindow(5, 20, term->height - 5, term->width - 29);
init_color(COLOR_WATER, 500, 500, 1000);
init_color(COLOR_GRASS, 10, 700, 450);
init_color(COLOR_SAND, 1000, 800, 0);
init_color(COLOR_STONE, 500, 500, 500);
for (int i = 1; i < 16; i++)
init_pair(i, COLOR_WHITE, COLOR_RED);
init_pair(COLOR_WATER, COLOR_WHITE, COLOR_WATER);
init_pair(COLOR_GRASS, COLOR_WHITE, COLOR_GRASS);
init_pair(COLOR_SAND, COLOR_WHITE, COLOR_SAND);
init_pair(COLOR_STONE, COLOR_WHITE, COLOR_STONE);
init_pair(WHITE, COLOR_WHITE, COLOR_WHITE);
return term;
}
void displayPlayerStats(Term_s *term);
void displayCrafts(Term_s *term);
void displayTerm(Term_s *term, View_s *view)
{
int h = getmaxy(stdscr);
int w = getmaxx(stdscr) - ((getmaxx(stdscr) + 1) % 2);
if (term->displayMode == WORLD)
{
if (term->height != h || term->width != w)
{
term->height = h;
term->width = w;
wclear(term->world); // clear all screen for deleting remanent caracters on resizing
wclear(term->stats); // clear all screen for deleting remanent caracters on resizing
wrefresh(stdscr);
}
wresize(term->world, term->height - 5, term->width);
wresize(term->stats, 5, term->width);
mvwin(term->stats, term->height - 5, 0);
displayPlayerStats(term);
displayWorld(term, view);
// displayCrafts(term);
}
}
WINDOW *createWindow(int height, int width, int starty, int startx)
{
WINDOW *window;
window = newwin(height, width, starty, startx);
box(window, 0, 0); /* 0, 0 gives default characters
* for the vertical and horizontal
* lines */
wrefresh(window); /* Show that box */
nodelay(window, TRUE);
return window;
}
void displayWorld(Term_s *term, View_s *view)
{
int minDisplay_x = MIN(term->world->_maxx/2, (CHUNK_SIZE * MAX_CHUNK_DISTANCE));
int maxDisplay_x = minDisplay_x;
minDisplay_x = (CHUNK_SIZE * MAX_CHUNK_DISTANCE) / 2 - (minDisplay_x / 2);
maxDisplay_x = maxDisplay_x + minDisplay_x;
int minDisplay_y = MIN(term->world->_maxy, (CHUNK_SIZE * MAX_CHUNK_DISTANCE));
int maxDisplay_y = minDisplay_y;
minDisplay_y = (CHUNK_SIZE * MAX_CHUNK_DISTANCE) / 2 - (minDisplay_y / 2);
maxDisplay_y = maxDisplay_y + minDisplay_y;
Coordinate_s entity_tilemap_coord = getEntityTilemapCoordinate(term->tilemap->m_entities->m_entity);
Coordinate_s screen_world_coord = term->tilemap->m_chunks[0][0]->world_position;
screen_world_coord.m_x += minDisplay_x;
screen_world_coord.m_y += minDisplay_y;
int initial_x = screen_world_coord.m_x;
for (int h = minDisplay_y; h < maxDisplay_y; ++h, screen_world_coord.m_y++, screen_world_coord.m_x = initial_x)
{
for (int w = minDisplay_x; w < maxDisplay_x ; ++w, screen_world_coord.m_x++)
{
Block_s **actualBlock = term->tilemap->m_blocks[h * CHUNK_SIZE * MAX_CHUNK_DISTANCE + w];
short color = 0;
if (actualBlock[0])
{
switch (actualBlock[0]->m_type)
{
case WATER:
color = COLOR_WATER;
break;
case GRASS:
color = COLOR_GRASS;
break;
case SAND:
color = COLOR_SAND;
break;
case STONE:
color = COLOR_STONE;
break;
}
}
wattron(term->world, COLOR_PAIR(color));
if (actualBlock[1])
{
switch (actualBlock[1]->m_type)
{
case EVERGREEN_TREE:
waddwstr(term->world, L"🌲");
break;
case PLANK:
waddwstr(term->world, L"🟫"); //🟫
break;
case ROCK:
waddwstr(term->world, L"🗿");
break;
case SURFBOARD_B:
waddwstr(term->world, L"🛹");
break;
case APPLE:
waddwstr(term->world, L"🍎");
break;
}
}
else if (term->tilemap->m_entities->m_entity->m_position.m_x == screen_world_coord.m_x &&
term->tilemap->m_entities->m_entity->m_position.m_y == screen_world_coord.m_y)
{
switch (term->tilemap->m_player->m_action)
{
case MOVE:
waddwstr(term->world, L"🏃");
break;
case IDLE:
case BREAK:
case PICK:
waddwstr(term->world, L"🧍"); //🧍 🏊 🏄🪵
break;
case SURFING:
waddwstr(term->world, L"🏄");
break;
default:
break;
}
}
else
wprintw(term->world, " ");
}
wprintw(term->world, "\n");
}
wattron(term->world, COLOR_PAIR(1));
mvwprintw(term->world, 0, 0, "%.2lf", term->framerate);
wrefresh(term->world);
wmove(term->world, 0, 0);
}
void displayPlayerStats(Term_s *term)
{
// wborder(term->stats, '|', '|', '-', '-', '+', '+', '+', '+');
box(term->stats, 0, 0);
Player_s *player = term->tilemap->m_player;
// if (player->update_stats)
// {
static size_t previous_heart_lvl = 0;
static size_t previous_food_lvl = 0;
static size_t previous_water_lvl = 0;
size_t hp_lvl = player->m_base->m_health;
size_t food_lvl = player->m_vitals[FOOD_LVL] / 10;
size_t water_lvl = player->m_vitals[WATER_LVL] / 10;
// if ((previous_food_lvl == food_lvl) && (previous_water_lvl == water_lvl) && (hp_lvl == previous_heart_lvl))
// return;
previous_heart_lvl = hp_lvl;
previous_food_lvl = food_lvl;
previous_water_lvl = water_lvl;
mvwprintw(term->stats, 1, 1, "HEART : ");
// Print player HP
for (size_t i = 0; i < player->m_base->m_health; i++)
{
wprintw(term->stats, "♥");
}
// Print player food level
mvwprintw(term->stats, 2, 1, "FOOD : ");
for (size_t i = 0; i < food_lvl; i++)
{
wprintw(term->stats, "🍗");
}
for (size_t i = 0; i < 10 - food_lvl; i++)
{
wprintw(term->stats, " ");
}
// Print player water level
mvwprintw(term->stats, 3, 1, "WATER : ");
for (size_t i = 0; i < water_lvl; i++)
{
wprintw(term->stats, "💧");
}
for (size_t i = 0; i < 10 - water_lvl; i++)
{
wprintw(term->stats, " ");
}
// Print player current action
Block_s **block_in_front;
mvwprintw(term->stats, 1, 35, "ACTION : ");
switch (term->tilemap->m_player->m_action)
{
case MOVE:
wprintw(term->stats, "MOVING ");
break;
case SURFING:
wprintw(term->stats, "SURFING ");
break;
case BREAK:
block_in_front = getFrontBlock(player->m_base, term->tilemap);
if (block_in_front[1]->m_health <= 0)
{
player->m_action = IDLE;
break;
}
wprintw(term->stats, "BREAKING");
switch (block_in_front[1]->m_type)
{
case EVERGREEN_TREE:
wprintw(term->stats, "(TREE, %d♥)", block_in_front[1]->m_health);
break;
default:
break;
}
break;
default:
wprintw(term->stats, "IDLE ");
break;
}
// Print player current orientation
mvwprintw(term->stats, 2, 35, "🧭 : ");
switch (player->m_base->m_direction)
{
case NORTH:
wprintw(term->stats, "↑");
break;
case SOUTH:
wprintw(term->stats, "↓");
break;
case WEST:
wprintw(term->stats, "←");
break;
case EAST:
wprintw(term->stats, "→");
break;
default:
break;
}
// Print player holding tool
mvwprintw(term->stats, 3, 35, "TOOL : ");
Block_s *block;
Tool_s *tool;
// Print all inventory
for (size_t i = 0; i < 9; i++)
{
if (i == player->m_inventory.m_idx)
wattron(term->stats, COLOR_PAIR(COLOR_WHITE));
else
wattroff(term->stats, COLOR_PAIR(COLOR_WHITE));
switch (player->m_inventory.m_objects[i].m_type)
{
case BLOCK:
block = player->m_inventory.m_objects[i].m_data;
switch (block->m_type)
{
case PLANK:
wprintw(term->stats, "🟫");
break;
case APPLE:
wprintw(term->stats, "🍎");
break;
default:
break;
}
break;
case TOOL:
tool = player->m_inventory.m_objects[i].m_data;
if (!tool)
break;
switch (tool->m_type)
{
case SURFBOARD:
wprintw(term->stats, "🛹");
break;
case PICKAXE:
break;
case SWORD:
break;
default:
break;
}
break;
default:
wprintw(term->stats, "▫️ ");
break;
}
}
wattroff(term->stats, COLOR_PAIR(COLOR_WHITE));
mvwprintw(term->stats, 2, term->width / 2 + 10, "POSSIBLE CRAFTS : ");
if (player->m_craft_selected)
{
switch (player->m_craft_selected->m_craft)
{
case SURF_CRAFT:
mvwprintw(term->stats, 2, term->width / 2 + 30, "SURF");
break;
default:
break;
}
}
player->update_stats = false;
wmove(term->stats, 0, 0);
wrefresh(term->stats);
}
void RenderCameraView(Term_s *term, struct Camera_s *camera)
{
for (int h = 0; h < term->tilemap->m_height; ++h)
{
for (int w = 0; w < term->tilemap->m_width; ++w)
{
Block_s **actualBlock = term->tilemap->m_blocks[h * CHUNK_SIZE + w];
attr_t attr = 1 & A_STANDOUT;
short color = 0;
if (actualBlock[0])
{
switch (actualBlock[0]->m_type)
{
case WATER:
color = COLOR_WATER;
break;
case GRASS:
color = COLOR_GRASS;
break;
case SAND:
color = COLOR_SAND;
break;
case STONE:
color = COLOR_STONE;
break;
}
}
wattron(term->world, COLOR_PAIR(color));
if (actualBlock[1])
{
switch (actualBlock[1]->m_type)
{
case EVERGREEN_TREE:
waddwstr(term->world, L"^");
break;
case ROCK:
waddwstr(term->world, L"r");
break;
}
}
else
wprintw(term->world, " ");
}
wprintw(term->world, "\n");
}
wrefresh(term->world);
wmove(term->world, 0, 0);
}
void displayCrafts(Term_s *term)
{
Player_s *player = term->tilemap->m_player;
// Get list of possible crafts
static CraftType_e previous_crafts; // = NONE_CRAFT;
CraftType_e possible_crafts; // = getPossibleCrafts(player);
if (previous_crafts == possible_crafts)
return;
wmove(term->crafts, 1, 0);
if (possible_crafts & SURF_CRAFT)
{
wprintw(term->crafts, "SURF");
}
wrefresh(term->crafts);
wmove(term->crafts, 0, 0);
previous_crafts = possible_crafts;
}
void cookedOnExit()
{
noraw();
echo();
endwin();
return;
}
void calculateFPS(Term_s *term, double timeDif)
{
term->framerate = 1 / (timeDif / 1000.0);
}