-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrendering.c
1572 lines (1350 loc) · 50.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022 <Maros Varchola - mvarchdev>
#include "rendering.h"
#include <ctype.h>
#include <math.h>
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "common_tools.h"
/// @brief Up left border character
#define UPLEFTBORDER '#'
/// @brief Up right border character
#define UPRIGHTBORDER '#'
/// @brief Down left border character
#define DOWNLEFTBORDER '#'
/// @brief Down right border character
#define DOWNRIGHTBORDER '#'
/// @brief Border vertical character
#define VERTBORDER '-'
/// @brief Border horizontal character
#define HORBORDER '|'
/// @brief Up, down, right join border character
#define UPDOWNRIGHTBORDER '#'
/// @brief Up, down, left join border character
#define UPDOWNLEFTBORDER '#'
/// @brief Pipe border vertical character
#define PIPEVERT '_'
/// @brief Pipe border horizontal character
#define PIPEHORI '|'
/// @brief Pipe body character
#define PIPEBODY '#'
/// @brief End-of-pipe character
#define PIPEEND '*'
/// @brief Pipehole end width
#define PIPEHOLE_END_WIDTH 2
/// @brief Pipehole end height
#define PIPEHOLE_END_HEIGHT 1
/// @brief Default bird x offset
#define BIRDOFFX 30
/// @brief Maximal header string for game details
#define MAXHEADERSTRING 40
/// @brief Maximum of piped that can be rendered at once
#define MAX_PIPES 30
/// @brief Variable to save pipes to render
fbpipe pipe_array[MAX_PIPES] = {0};
/// @brief Loaded settings for screen
screen act_screen = {0};
/// @brief Loaded render settings
render_settings act_rndsett = {0};
/// @brief Speed in chars/s
float act_speed_chars = 0;
/// @brief Variable for computing speed increase
long long last_time = 0;
/// @brief Gravity constant
float gravity_constant = 9.8;
/// @brief If no gravity multiply is set, then default will be used
float def_grav_multiply = 1;
/// @brief Default speed increase
float def_speed_incr = 0.125;
/// @brief Total horizontal size of full render area
int xsize = 0;
/// @brief Total vertical size of full render area
int ysize = 0;
/// @brief Total horizontal offset of map render area
int mapoffsx = 0;
/// @brief Total vertical offset of map render area
int mapoffsy = 0;
/// @brief Total horizontal offset of header render area
int headeroffsx = 0;
/// @brief Total vertical offset of header render area
int headeroffsy = 0;
/// @brief Total horizontal size/width of header
int headersizex = 0;
/// @brief Maximum string length of menu item
#define menu_item_maxstrlen 30
/// @brief Total vertical offset of map render area
char menu_items[menu_inem_n + 1][menu_item_maxstrlen] = {
"Start (where you ended)", "Select level", "Change nickname",
"Show hall of fame", "About", "Exit",
};
/// @brief Function checks if intensity bit is set
/// @param fg Colorbits input
/// @return True, if intensity bit is set, else false
int is_bold_bits(int fg) {
int i;
i = 1 << 3;
return (i & fg);
}
/// @brief Get uniqe color pairnum
/// @param fg Foreground color bits
/// @param bg Background color bits
/// @return Uniqe pairnum
int get_col_pairnum(int fg, int bg) {
int B = 0, bbb = 0, ffff = 0;
B = 1 << 7;
bbb = (7 & bg) << 4;
ffff = 7 & fg;
return (B | bbb | ffff);
}
/// @brief Sets the actual terminal color based on uniqe colorpair
/// @param fg Foreground color bits
/// @param bg Background color bits
void setcolor_bits(int fg, int bg) {
attron(COLOR_PAIR(get_col_pairnum(fg, bg)));
if (is_bold_bits(fg)) {
attron(A_BOLD);
}
}
/// @brief Sets the actual terminal color based on uniqe colorpair
/// @param fg Foreground color bits
/// @param bg Background color bits
void unsetcolor_bits(int fg, int bg) {
attroff(COLOR_PAIR(get_col_pairnum(fg, bg)));
if (is_bold_bits(fg)) {
attroff(A_BOLD);
}
}
/// @brief Returns if input colorname/string indicates intensity bit set
/// @param colname String with colorname
/// @return True, if it is with intensity set
bool is_bold(char colname[]) {
if (!colname) return false;
for (int i = 0; i < strlen(colname); i++) colname[i] = toupper(colname[i]);
if (strstr(colname, "B_")) return true;
return false;
}
/// @brief Function will match colorname to native color number
/// @param colname String with colorname
/// @return Native color number, -1 if not existing
short string_to_color(char colname[]) {
if (!colname) return false;
for (int i = 0; i < strlen(colname); i++) colname[i] = toupper(colname[i]);
if (strstr(colname, "BLACK"))
return COLOR_BLACK;
else if (strstr(colname, "RED"))
return COLOR_RED;
else if (strstr(colname, "GREEN"))
return COLOR_GREEN;
else if (strstr(colname, "YELLOW"))
return COLOR_YELLOW;
else if (strstr(colname, "BLUE"))
return COLOR_BLUE;
else if (strstr(colname, "MAGENTA"))
return COLOR_MAGENTA;
else if (strstr(colname, "CYAN"))
return COLOR_CYAN;
else if (strstr(colname, "WHITE"))
return COLOR_WHITE;
return -1;
}
/// @brief Function will return oposite of native color
/// @param col Native color number
/// @return Native number of oposite color
short opposit_col(short col) {
switch (col) {
case COLOR_BLACK:
return (COLOR_WHITE);
case COLOR_RED:
return (COLOR_GREEN);
case COLOR_GREEN:
return (COLOR_RED);
case COLOR_YELLOW:
return (COLOR_CYAN);
case COLOR_BLUE:
return (COLOR_RED);
case COLOR_MAGENTA:
return (COLOR_BLACK);
case COLOR_CYAN:
return (COLOR_YELLOW);
case COLOR_WHITE:
return (COLOR_BLACK);
}
return -1;
}
/// @brief Will convert native color to foreground bitscolor
/// @param color Native color number
/// @param bold If color intensity bit should be set
/// @return Foreground bitscolor based on input
int native_to_bitscolor(short color, bool bold) {
int bitscolor = bold ? (1 << 3) : 0;
switch (color) { /* RGB */
case COLOR_BLACK:
return bitscolor; /* 000 */
case COLOR_BLUE:
return bitscolor | 1; /* 001 */
case COLOR_GREEN:
return bitscolor | 2; /* 010 */
case COLOR_CYAN:
return bitscolor | 3; /* 011 */
case COLOR_RED:
return bitscolor | 4; /* 100 */
case COLOR_MAGENTA:
return bitscolor | 5; /* 101 */
case COLOR_YELLOW:
return bitscolor | 6; /* 110 */
case COLOR_WHITE:
return bitscolor | 7; /* 111 */
}
return -1;
}
/// @brief Will convert native color to background bitscolor
/// @param color Native color number
/// @return Background bitscolor based on input
int native_to_bitscolor_bg(short color) {
int bcolor = native_to_bitscolor(color, false);
if (bcolor > -1) return (7 & bcolor) << 4;
return bcolor;
}
/// @brief Will shift background color to foreground from input
/// @param bg Bitscolor with background color set
/// @return Bitscolor shifted from bg to fg
int bitscolor_bg_to_fg(int bg) { return (bg >> 4) & 7; }
/// @brief Will convert bitscolor to native color
/// @param color Bitscolor
/// @return Native number of color
short bits_to_native_color(int color) {
switch (7 & color) { /* RGB */
case 0: /* 000 */
return (COLOR_BLACK);
case 1: /* 001 */
return (COLOR_BLUE);
case 2: /* 010 */
return (COLOR_GREEN);
case 3: /* 011 */
return (COLOR_CYAN);
case 4: /* 100 */
return (COLOR_RED);
case 5: /* 101 */
return (COLOR_MAGENTA);
case 6: /* 110 */
return (COLOR_YELLOW);
case 7: /* 111 */
return (COLOR_WHITE);
}
return -1;
}
/// @brief Will init all uniqe colorpairs
void init_colorpairs() {
int fg, bg;
int colorpair;
for (bg = 0; bg <= 7; bg++)
for (fg = 0; fg <= 7; fg++) {
colorpair = get_col_pairnum(fg, bg);
init_pair(colorpair, bits_to_native_color(fg), bits_to_native_color(bg));
}
}
/// @brief Will initialize screen and compute base offsets and sizes
/// @return Error code
int init_screen() {
load_settings();
initscr();
curs_set(0);
start_color();
init_colorpairs();
noecho();
xsize = (2 * OUTERMARGIN) + (2 * BORDERWIDTH) + (2 * MAPMARGIN) + MAPSIZEX;
ysize = (2 * OUTERMARGIN) + (3 * BORDERWIDTH) + (2 * MAPMARGIN) + MAPSIZEY +
act_screen.header_height + (act_screen.header_padding * 2);
mapoffsx = OUTERMARGIN + BORDERWIDTH + MAPMARGIN;
mapoffsy = OUTERMARGIN + BORDERWIDTH * 2 + act_screen.header_padding * 2 +
act_screen.header_height + MAPMARGIN;
headeroffsx = OUTERMARGIN + BORDERWIDTH + act_screen.header_padding;
headeroffsy = OUTERMARGIN + BORDERWIDTH + act_screen.header_padding;
headersizex = MAPSIZEX - 2 * act_screen.header_padding;
act_screen.header_width = xsize - (OUTERMARGIN * 2) - (BORDERWIDTH * 2) -
(act_screen.header_padding * 2);
resizeterm(ysize, xsize);
render_borders();
refresh();
return 0;
}
/// @brief Change speed of bird based jump speed
/// @param inpb Bird struct pointer to use
void jump_bird(bird *inpb) {
if (!inpb) return;
if (inpb->act_speed < 0) {
if ((inpb->act_speed - inpb->jump_speed) < -1.5 * inpb->jump_speed)
inpb->act_speed = -1.5 * inpb->jump_speed;
else
inpb->act_speed -= inpb->jump_speed;
} else
inpb->act_speed = -inpb->jump_speed;
}
/// @brief Will print actual running game details to the header area
/// @param actlives Actual lives
/// @param score Actual score
/// @param inplvl Level struct pointer
/// @param inpb Bird struct pointer
/// @return Error code
int print_game_details(int actlives, int score, level *inplvl, bird *inpb) {
if (!inplvl || !inpb) return -1;
int i = 0;
char headerinp[7][MAXHEADERSTRING] = {0};
sprintf(headerinp[i++], "Level name: %s", inplvl->levelname);
sprintf(headerinp[i++], "Score: %d", score);
sprintf(headerinp[i++], "Lives [Actual / Max]: %d / %d", actlives,
inplvl->max_lives);
sprintf(headerinp[i++], "Speed: %.3f [char/s]", act_speed_chars);
sprintf(headerinp[i++], "Bird speed: %.3f [char/s]", inpb->act_speed);
i++;
sprintf(headerinp[i++], "Pause: p | End game: e");
return render_header_text(7, MAXHEADERSTRING, headerinp);
}
/// @brief Game paused dialog
/// @return Error code
int game_paused_dialog() {
char headerinp[1][60] = {0};
sprintf(headerinp[0],
"GAME PAUSED - PRESS 'p' TO CONTINUE OR 'e' TO END GAME");
render_header_text(1, 60, headerinp);
timeout(-1);
while (true) {
int ch = getch();
if (ch == EOF)
continue;
else if (tolower(ch) == 'p')
return 0;
else if (tolower(ch) == 'e')
return 1;
}
}
/// @brief Force set of lastms rendered variable for enabled pipes
/// @param timems Time to set in ms
void update_last_ms_pipes(long long timems) {
for (int i = 0; i < MAX_PIPES; i++) {
if (!pipe_array[i].enabled) continue;
pipe_array[i].last_time_moved = timems;
}
}
/// @brief Collision dialog
/// @param actlives Actual lives
/// @param score Actual score
/// @return
int colision_dialog(int actlives, int score) {
char headerinp[4][90] = {0};
sprintf(headerinp[0],
"BANG! You crashed into the pipe or you fell down, you have %d more "
"lives left.",
actlives);
sprintf(headerinp[1], "YOUR ACTUAL SCORE IS: %d", score);
sprintf(headerinp[3],
"Do you want to try again (press 't') or end the game (press 'e') ?");
render_header_text(4, 90, headerinp);
timeout(-1);
while (true) {
int ch = getch();
if (ch == EOF)
continue;
else if (tolower(ch) == 't')
return 0;
else if (tolower(ch) == 'e')
return 1;
}
}
/// @brief Will disable all enabled pipes
void clear_all_pipes() {
for (int i = 0; i < MAX_PIPES; i++)
if (pipe_array[i].enabled) pipe_array[i].enabled = false;
}
/// @brief Function to run level
/// @param inplvl Pointer to level to use
/// @param status Pointer to status output
/// @return Score
int run_level(level *inplvl, int *status) {
if (!inplvl) return -1;
int actlives = inplvl->max_lives, score = 0;
act_speed_chars = inplvl->start_speed * METERTOCHARS;
int statustmp = 0;
bird usebird;
if (!status) status = &statustmp;
while (actlives != 0) {
clear_all_pipes();
usebird = get_bird(inplvl);
last_time = 0;
timeout(0);
while (true) {
int ch = getch();
if (ch != EOF) {
if (ch == ' ')
jump_bird(&usebird);
else if (tolower(ch) == 'e') {
*status = 1;
break;
} else if (tolower(ch) == 'p') {
if (game_paused_dialog() == 1) {
*status = 1;
break;
}
timeout(0);
usebird.last_time_ms = timeInMilliseconds();
update_last_ms_pipes(usebird.last_time_ms);
last_time = 0;
}
flushinp();
}
print_game_details(actlives, score, inplvl, &usebird);
move_bird(&usebird);
clear_map_area(inplvl, true);
render_pipes(inplvl);
if (bird_collision(&usebird, BIRDOFFX) ||
usebird.act_position >= MAPSIZEY - 1)
break;
render_bird(&usebird, BIRDOFFX, false);
score += move_pipes(inplvl);
process_pipes(inplvl);
// last_time = timeInMilliseconds();
increase_speed(inplvl);
refresh();
msleep(1000 / act_rndsett.fps);
// usleep(1000);
}
if (*status == 1) break;
actlives--;
render_bird(&usebird, BIRDOFFX, true);
if (actlives > 0)
if (colision_dialog(actlives, score) == 1) {
*status = 1;
break;
}
}
flushinp();
timeout(-1);
return score;
}
/// @brief Will render all borders
/// @return Error code
int render_borders() {
setcolor_bits(act_screen.border_color,
bitscolor_bg_to_fg(act_screen.border_color));
for (int i = 0; i < BORDERWIDTH; i++) {
mvaddch(OUTERMARGIN + i, OUTERMARGIN + i, UPLEFTBORDER);
mvaddch(OUTERMARGIN + i, xsize - OUTERMARGIN - 1 - i, UPRIGHTBORDER);
mvaddch(ysize - OUTERMARGIN - 1 - i, OUTERMARGIN + i, DOWNLEFTBORDER);
mvaddch(ysize - OUTERMARGIN - 1 - i, xsize - OUTERMARGIN - 1 - i,
DOWNRIGHTBORDER);
for (int z = OUTERMARGIN + i + 1; z < xsize - OUTERMARGIN - i - 1; z++) {
mvaddch(OUTERMARGIN + i, z, VERTBORDER);
mvaddch(ysize - OUTERMARGIN - i - 1, z, VERTBORDER);
}
for (int z = OUTERMARGIN + i + 1; z < ysize - OUTERMARGIN - i - 1; z++) {
if (i + 1 == BORDERWIDTH &&
(((1 * OUTERMARGIN) + (2 * BORDERWIDTH) + act_screen.header_height +
(act_screen.header_padding * 2) - 1) == z)) {
mvaddch(z, OUTERMARGIN + i, UPDOWNRIGHTBORDER);
for (int g = OUTERMARGIN + i + 1; g < xsize - OUTERMARGIN - 1; g++)
mvaddch(z, g, VERTBORDER);
mvaddch(z, xsize - OUTERMARGIN - 1 - i, UPDOWNLEFTBORDER);
} else {
mvaddch(z, OUTERMARGIN + i, HORBORDER);
mvaddch(z, xsize - OUTERMARGIN - 1 - i, HORBORDER);
}
}
}
unsetcolor_bits(act_screen.border_color,
bitscolor_bg_to_fg(act_screen.border_color));
return 0;
}
/// @brief Common function to render header text
/// @param lines How much lines is in header_text
/// @param maxstring String length of one line in header_text
/// @param header_text 2D Array of chars (More string lines)
/// @return Error code
int render_header_text(int lines, int maxstring,
char header_text[lines][maxstring]) {
setcolor_bits(act_screen.header_color,
bitscolor_bg_to_fg(act_screen.header_color));
clear_header(true);
int yoff = 0;
for (int i = 0; i < lines && i < act_screen.header_height; i++)
yoff += render_header_string(header_text[i], yoff, false, false);
unsetcolor_bits(act_screen.header_color,
bitscolor_bg_to_fg(act_screen.header_color));
return 0;
}
/// @brief Will set header color on
/// @param switchbgfg Switch fg to bg and viceversa
void turn_on_header_color(bool switchbgfg) {
if (switchbgfg)
setcolor_bits(bitscolor_bg_to_fg(act_screen.header_color),
act_screen.header_color);
else
setcolor_bits(act_screen.header_color,
bitscolor_bg_to_fg(act_screen.header_color));
}
/// @brief Will set header color off
/// @param switchbgfg Switch fg to bg and viceversa
void turn_off_header_color(bool switchbgfg) {
if (switchbgfg)
unsetcolor_bits(bitscolor_bg_to_fg(act_screen.header_color),
act_screen.header_color);
else
unsetcolor_bits(act_screen.header_color,
bitscolor_bg_to_fg(act_screen.header_color));
}
/// @brief Prints string and computes lines based on header width
/// @param header_text Input text that needs to be print out
/// @param yoff y offset
/// @param setcolor If true, it will set color based on the settings
/// @param cl_hdr If true, it will clear header before print out
/// @return How many lines this function have printed out
int render_header_string(char header_text[], int yoff, bool setcolor,
bool cl_hdr) {
if (yoff == -1) yoff = act_screen.header_height / 2;
if (setcolor)
setcolor_bits(act_screen.header_color,
bitscolor_bg_to_fg(act_screen.header_color));
if (cl_hdr) clear_header(true);
int parts = 1;
if (strlen(header_text) > headersizex)
parts += strlen(header_text) / headersizex;
int i = 0;
for (; i < parts; i++)
mvprintw(headeroffsy + yoff + i, headeroffsx, "%.*s", headersizex,
(headersizex * i) + header_text);
if (setcolor)
unsetcolor_bits(act_screen.header_color,
bitscolor_bg_to_fg(act_screen.header_color));
return i;
}
/// @brief Clear map area
/// @param inplvl Input level, if null, colors will not be used
/// @param full If true, will clear also margins
/// @return Error code
int clear_map_area(level *inplvl, bool full) {
int bgoppcolor = 0;
if (inplvl) {
bgoppcolor = native_to_bitscolor(
opposit_col(bits_to_native_color(bitscolor_bg_to_fg(inplvl->bgcolor))),
true);
setcolor_bits(bgoppcolor, bitscolor_bg_to_fg(inplvl->bgcolor));
}
for (int y = 0; y < MAPSIZEY + (full ? MAPMARGIN * 2 : 0); y++)
for (int x = 0; x < MAPSIZEX + (full ? MAPMARGIN * 2 : 0); x++)
mvaddch(y + mapoffsy - (full ? MAPMARGIN : 0),
x + mapoffsx - (full ? MAPMARGIN : 0), ' ');
if (inplvl) unsetcolor_bits(bgoppcolor, bitscolor_bg_to_fg(inplvl->bgcolor));
return 0;
}
/// @brief Clear header area
/// @param full If true, will clear also padding area
/// @return Error code
int clear_header(bool full) {
for (int y = 0; y < act_screen.header_height +
(full ? act_screen.header_padding * 2 : 0);
y++)
for (int x = 0; x < act_screen.header_width +
(full ? act_screen.header_padding * 2 : 0);
x++)
mvaddch(y + headeroffsy - (full ? act_screen.header_padding : 0),
x + headeroffsx - (full ? act_screen.header_padding : 0), ' ');
return 0;
}
/// @brief Render menu
/// @param option_selected Which option shoud be highlighted
/// @param nickname Nickname to be shown
/// @return Error code
int render_menu(int option_selected, char nickname[]) {
int err = print_header_options(menu_inem_n, 30, menu_items, option_selected,
(act_screen.header_height - 1) / 2);
if (err != 0) return err;
char namestr[120] = {0};
sprintf(namestr, "Welcome, %s!", nickname);
return render_header_string(namestr, 0, true, false);
}
/// @brief Will render 'About this game' page
/// @param yoffset Scroll offset
/// @return Maximal allowed scroll
int render_about_page(int yoffset) {
char aboutinfo[10][100] = {0};
int lineidx = 0;
sprintf(
aboutinfo[lineidx++],
"Welcome to this game named Flappy Bird - created in ncurses framework!");
sprintf(aboutinfo[lineidx++],
"This game was created by Maros Varchola as school project, but now, "
"as you can see.");
sprintf(aboutinfo[lineidx++], "It is used as part of my portfolio.");
sprintf(aboutinfo[lineidx++],
"If you have any ideas on how to improve this game, do not be shy "
"and try a PR.");
sprintf(aboutinfo[lineidx++],
"I believe it will be very interesting change/feature!");
sprintf(aboutinfo[lineidx++],
"In this game, the real physics calculations are used, so even "
"gravity constant is set here!");
sprintf(aboutinfo[lineidx++],
"If you have any questions, write me on [email protected] !");
lineidx++;
sprintf(aboutinfo[lineidx++], "ENJOY THE GAME!");
int bgoppcolor = native_to_bitscolor(
opposit_col(
bits_to_native_color(bitscolor_bg_to_fg(act_screen.header_color))),
true);
setcolor_bits(bgoppcolor, bitscolor_bg_to_fg(act_screen.header_color));
clear_map_area(NULL, true);
int lineidxcounter = yoffset, i = 0;
for (; lineidxcounter < lineidx && i < MAPSIZEY - 1; i++) {
mvprintw(mapoffsy + i, mapoffsx, aboutinfo[lineidxcounter]);
lineidxcounter++;
}
if (i >= MAPSIZEY - 1 && lineidxcounter < lineidx)
mvprintw(mapoffsy + i - 1, mapoffsx,
"There is more! Scroll down! (arrows up/down)");
unsetcolor_bits(bgoppcolor, bitscolor_bg_to_fg(act_screen.header_color));
if (i >= MAPSIZEY - 1 && lineidxcounter < lineidx) return i - MAPSIZEY;
return 0;
}
/// @brief Get random pipe based on level
/// @param x x offset
/// @param inplvl Level struct pointer to use
/// @param enable If enable pipe
/// @param prevupheight Height of previous pipe
/// @return Generated pipe struct
fbpipe get_pipe(int x, level *inplvl, bool enable, int prevupheight) {
fbpipe newpipe = {0};
if (!inplvl) return newpipe;
newpipe.pipewidth = rand_gen(inplvl->minimum_width, inplvl->maximum_width);
int holeheight = rand_gen(inplvl->minimum_space, inplvl->maximum_space);
int full_allowed_height =
MAPSIZEY - ((PIPEHOLE_END_HEIGHT + 1) * 2) - holeheight;
int minheight = 1, maxheight = full_allowed_height - 1;
if (prevupheight > 0) {
int useinterval = rand_gen(0, 1);
if (useinterval == 0) {
if (prevupheight - inplvl->maximum_distance_space <= 0) useinterval = 1;
} else if (prevupheight + inplvl->minimum_distance_space >=
full_allowed_height - 1)
useinterval = 0;
if (useinterval == 0) {
minheight = prevupheight - inplvl->maximum_distance_space;
maxheight = prevupheight - inplvl->minimum_distance_space;
} else {
minheight = prevupheight + inplvl->minimum_distance_space;
maxheight = prevupheight + inplvl->maximum_distance_space;
}
if (minheight <= 0)
minheight = 1;
else if (minheight >= full_allowed_height - 1)
minheight = full_allowed_height - 1;
if (maxheight >= full_allowed_height - 1)
maxheight = full_allowed_height - 1;
else if (maxheight <= 0)
maxheight = minheight;
}
if (minheight == maxheight)
newpipe.upheight = prevupheight;
else
newpipe.upheight = rand_gen(minheight, maxheight);
newpipe.downheight = full_allowed_height - newpipe.upheight;
while (newpipe.downheight < 1) newpipe.downheight++;
newpipe.position = x;
newpipe.enabled = enable;
newpipe.last_time_moved = timeInMilliseconds();
return newpipe;
}
/// @brief Checks if coordinates are in map area
/// @param y y coordinate
/// @param x x coordinate
/// @return True if cordinates are in map area, else false
bool check_in_map_ok(int y, int x) {
if (y < 0 || y >= MAPSIZEY || x < 0 || x >= MAPSIZEX) return false;
return true;
}
/// @brief Add character to map area
/// @param y y coordinate
/// @param x x coordinate
/// @param inp Character to add
/// @return Error code
int addch_maparea(int y, int x, const chtype inp) {
if (check_in_map_ok(y, x)) return mvaddch(mapoffsy + y, mapoffsx + x, inp);
return 0;
}
/// @brief Add character to pipe border
/// @param y y coordinate
/// @param x x coordinate
/// @param inp Character to use
/// @param inplvl level struct pointer to use
/// @return Error code
int addch_pipe_border(int y, int x, const chtype inp, level *inplvl) {
if (!inplvl) return -1;
unsetcolor_bits(inplvl->pipe_color_body,
bitscolor_bg_to_fg(inplvl->pipe_color_body));
setcolor_bits(inplvl->pipe_color_brd,
bitscolor_bg_to_fg(inplvl->pipe_color_brd));
return addch_maparea(y, x, inp);
}
/// @brief Add character to pipe body
/// @param y y coordinate
/// @param x x coordinate
/// @param inp Character to use
/// @param inplvl level struct pointer to use
/// @return Error code
int addch_pipe_body(int y, int x, const chtype inp, level *inplvl) {
if (!inplvl) return -1;
unsetcolor_bits(inplvl->pipe_color_brd,
bitscolor_bg_to_fg(inplvl->pipe_color_brd));
setcolor_bits(inplvl->pipe_color_body,
bitscolor_bg_to_fg(inplvl->pipe_color_body));
return addch_maparea(y, x, inp);
}
/// @brief Will render bird normally, except there is collision, then it will
/// switch fg and bg color
/// @param y y coordinate
/// @param x x coordinate
/// @param inpch Character to add
/// @param inpb Bird struct to use
/// @param showcol If true, then collision will be checked and printed
void render_bird_collision(int y, int x, chtype inpch, bird *inpb,
bool showcol) {
if (!showcol) {
mvaddch(y, x, inpch);
return;
}
if ((mvinch(y, x) & 255) != ' ')
setcolor_bits(bitscolor_bg_to_fg(inpb->colorbits), inpb->colorbits);
mvaddch(y, x, inpch);
if ((mvinch(y, x) & 255) != ' ') {
unsetcolor_bits(bitscolor_bg_to_fg(inpb->colorbits), inpb->colorbits);
setcolor_bits(inpb->colorbits, bitscolor_bg_to_fg(inpb->colorbits));
}
}
/// @brief Will render bird in map area
/// @param inpb Bird structure pointer to use
/// @param xpos x position where bird should be rendered
/// @param show_collision If true, will render also collision
/// @return Error code
int render_bird(bird *inpb, int xpos, bool show_collision) {
int xcenter = mapoffsx + xpos;
int ycenter = mapoffsy + inpb->act_position;
setcolor_bits(inpb->colorbits, bitscolor_bg_to_fg(inpb->colorbits));
if (ycenter - 1 >= mapoffsy && ycenter - 1 < (mapoffsy + MAPSIZEY))
render_bird_collision(ycenter - 1, xcenter, '\\', inpb, show_collision);
if (ycenter >= mapoffsy && ycenter < (mapoffsy + MAPSIZEY)) {
render_bird_collision(ycenter, xcenter - 2, '|', inpb, show_collision);
render_bird_collision(ycenter, xcenter + 3, '|', inpb, show_collision);
render_bird_collision(ycenter, xcenter + 2, '*', inpb, show_collision);
for (size_t i = 0; i < 3; i++)
render_bird_collision(ycenter, xcenter - 1 + i, '#', inpb,
show_collision);
}
if (ycenter + 1 >= mapoffsy && ycenter + 1 < (mapoffsy + MAPSIZEY))
render_bird_collision(ycenter + 1, xcenter, '/', inpb, show_collision);
unsetcolor_bits(inpb->colorbits, bitscolor_bg_to_fg(inpb->colorbits));
return 0;
}
/// @brief Process/Move bird
/// @param bird Bird structure pointer to use
/// @return Error code
int move_bird(bird *bird) {
if (!bird) return -1;
if (bird->last_time_ms == 0) {
bird->last_time_ms = timeInMilliseconds();
return 0;
}
long long act_time = timeInMilliseconds();
long long diff_time = act_time - bird->last_time_ms;
float seconds = ((float)diff_time / 1000);
float next_pos =
bird->act_position + (bird->act_speed * METERTOCHARS) * seconds;
if (next_pos >= MAPSIZEY) {
bird->last_time_ms = act_time;
bird->act_speed = 0;
bird->act_position = MAPSIZEY - 1;
return 0;
} else if (next_pos <= 0) {
bird->last_time_ms = act_time;
bird->act_speed = bird->gravity * seconds;
bird->act_position = 0;
return 0;
}
bird->act_position = next_pos;
bird->act_speed += bird->gravity * seconds;
bird->last_time_ms = act_time;
return 0;
}
/// @brief Will render single pipe
/// @param inputp Pipe struct pointer
/// @param inplvl Level struct pointer
/// @return Error code
int render_pipe(fbpipe *inputp, level *inplvl) {
if (!inputp || !inplvl) return -1;
if (inputp->position - 2 >= MAPSIZEX) return -1;
// RENDER UPPER PIPE
for (int y = 0; y < inputp->upheight && inputp->position < MAPSIZEX; y++) {
addch_pipe_border(y, inputp->position, PIPEHORI, inplvl);
for (int x = 0;
x < inputp->pipewidth && inputp->position + x + 1 < MAPSIZEX; x++)
addch_pipe_body(y, inputp->position + x + 1, PIPEBODY, inplvl);
if ((inputp->position + inputp->pipewidth + 1) < MAPSIZEX)
addch_pipe_border(y, inputp->position + inputp->pipewidth + 1, PIPEHORI,
inplvl);
}
// first layer turn border
for (int i = 0; i < PIPEHOLE_END_WIDTH &&
(inputp->position - PIPEHOLE_END_WIDTH + i) < MAPSIZEX;
i++)
addch_pipe_border(inputp->upheight - 1,
inputp->position - PIPEHOLE_END_WIDTH + i, PIPEVERT,
inplvl);
for (int i = 0; i < PIPEHOLE_END_WIDTH &&
(inputp->position + 1 + inputp->pipewidth + 1 + i) < MAPSIZEX;
i++)
addch_pipe_border(inputp->upheight - 1,
inputp->position + 1 + inputp->pipewidth + 1 + i,
PIPEVERT, inplvl);
// second layer turn border and body
for (int i = 0; i < inputp->pipewidth + 2 + PIPEHOLE_END_WIDTH * 2 &&
(inputp->position - 2 + i) < MAPSIZEX;
i++) {
if (i == 0 || i == inputp->pipewidth + 1 + PIPEHOLE_END_WIDTH * 2)
addch_pipe_border(inputp->upheight, inputp->position - 2 + i, PIPEHORI,
inplvl);
else
addch_pipe_body(inputp->upheight, inputp->position - 2 + i, PIPEBODY,
inplvl);
// third/last layer turn border
addch_pipe_border(inputp->upheight + 1, inputp->position - 2 + i, PIPEEND,
inplvl);
}
// RENDER DOWN PIPE
for (int y = 0; y < inputp->downheight && inputp->position < MAPSIZEX; y++) {
addch_pipe_border(MAPSIZEY - 1 - y, inputp->position, PIPEHORI, inplvl);
for (int x = 0;
x < inputp->pipewidth && inputp->position + x + 1 < MAPSIZEX; x++)
addch_pipe_body(MAPSIZEY - 1 - y, inputp->position + x + 1, PIPEBODY,
inplvl);
if ((inputp->position + inputp->pipewidth + 1) < MAPSIZEX)
addch_pipe_border(MAPSIZEY - 1 - y,
inputp->position + inputp->pipewidth + 1, PIPEHORI,
inplvl);
}
// first layer turn border
for (int i = 0; i < PIPEHOLE_END_WIDTH &&
(inputp->position - PIPEHOLE_END_WIDTH + i) < MAPSIZEX;
i++)
addch_pipe_border(MAPSIZEY - 1 - inputp->downheight + 1,
inputp->position - PIPEHOLE_END_WIDTH + i, '-', inplvl);
for (int i = 0; i < PIPEHOLE_END_WIDTH &&
(inputp->position + 1 + inputp->pipewidth + 1 + i) < MAPSIZEX;
i++)
addch_pipe_border(MAPSIZEY - 1 - inputp->downheight + 1,
inputp->position + 1 + inputp->pipewidth + 1 + i, '-',
inplvl);
// second layer turn border and body
for (int i = 0; i < inputp->pipewidth + 2 + PIPEHOLE_END_WIDTH * 2 &&
(inputp->position - 2 + i) < MAPSIZEX;
i++) {
if (i == 0 || i == inputp->pipewidth + 1 + PIPEHOLE_END_WIDTH * 2)
addch_pipe_border(MAPSIZEY - 1 - inputp->downheight,
inputp->position - 2 + i, PIPEHORI, inplvl);
else
addch_pipe_body(MAPSIZEY - 1 - inputp->downheight,
inputp->position - 2 + i, PIPEBODY, inplvl);
// third/last layer turn border
addch_pipe_border(MAPSIZEY - 1 - inputp->downheight - 1,
inputp->position - 2 + i, PIPEEND, inplvl);
}
unsetcolor_bits(inplvl->pipe_color_brd,
bitscolor_bg_to_fg(inplvl->pipe_color_brd));
unsetcolor_bits(inplvl->pipe_color_body,
bitscolor_bg_to_fg(inplvl->pipe_color_body));
return 0;
}