-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
946 lines (781 loc) · 20.2 KB
/
main.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
/* Bricktick brick breaker
* This software is public domain.
* See the file UNLICENSE.TXT for more information.
* Created in 2014 by Subsentient
*/
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
#include "bricktick.h"
#define BRICKTICK_NUMLEVELS 7
#define BRICKTICK_NUMLIVES 3
#ifdef WIN32
#define BT_MKDIR(a, b) mkdir(a)
#define HOME_ENV "USERPROFILE"
#else
#define BT_MKDIR(a, b) mkdir(a, b)
#define HOME_ENV "HOME"
#endif /*WIN32*/
/*Types*/
struct LEVEL
{
int BrickNumLines;
int BricksPerLine;
int HeightFromPaddle;
};
/*Globals*/
static int Lives = BRICKTICK_NUMLIVES;
static int Level = 1;
static int Score = 0;
Bool UseColor = true;
const struct LEVEL Levels[BRICKTICK_NUMLEVELS] =
{
{ BRICK_DEFAULT_NUMLINES - 2, BRICK_DEFAULT_PERLINE, BRICK_DEFAULT_HEIGHT },
{ BRICK_DEFAULT_NUMLINES, BRICK_DEFAULT_PERLINE, BRICK_DEFAULT_HEIGHT },
{ BRICK_DEFAULT_NUMLINES + 3, BRICK_DEFAULT_PERLINE, BRICK_DEFAULT_HEIGHT },
{ BRICK_DEFAULT_NUMLINES + 4, BRICK_DEFAULT_PERLINE, BRICK_DEFAULT_HEIGHT + 2 },
{ BRICK_MAX_NUMLINES, BRICK_DEFAULT_PERLINE, BRICK_DEFAULT_HEIGHT + 3 },
{ BRICK_DEFAULT_NUMLINES + 5, BRICK_MAX_PERLINE, BRICK_DEFAULT_HEIGHT + 4 },
{ BRICK_MAX_NUMLINES, BRICK_MAX_PERLINE, BRICK_DEFAULT_HEIGHT + 5 },
};
static const unsigned SaveGameVersion = 0x001;
/*Prototypes for static functions.*/
static void DrawMessage(const char *const Message);
static void DeleteMessage(void);
static void WaitForUserLaunch(void);
static Bool SetLevel(const int Level_);
static void ProcessGameOver(struct BALL *Ball, struct PADDLE *Paddle);
static void GameLoop(struct BALL *const Ball, struct PADDLE *const Paddle);
static void DrawStats(void);
static void DrawGreeting(void);
static Bool LoadGame(struct BALL *OutBall, struct PADDLE *OutPaddle);
static Bool SaveGame(const struct BALL *Ball, const struct PADDLE *Paddle);
static void DrawBorders(void);
/*Functions*/
static void GameLoop(struct BALL *const Ball, struct PADDLE *const Paddle)
{ /*Primary loop where most events get processed.*/
struct BRICKSTRIKE Strike;
int Key = 0;
int SecTick = 0;
Bool PaddleMovedLastTick;
DirectionX PaddleMoveDir;
int Inc = 0;
Bool Flip = false;
int SlowBallTicks = 0, BallNukeTicks = 0;
while ((Key = getch()) != 27) /*27 is ESC*/
{
if (SecTick == 10)
{ /*We get score every second for just surviving.*/
Score += 2;
DrawStats();
SecTick = 0;
}
++SecTick;
if (Ball->Y == 1)
{ /*We hit the ceiling.*/
BounceBallY(Ball, DOWN);
}
else if (Ball->Y >= BRICKTICK_MAX_Y - 2)
{ /*More happens when we hit the floor.*/
if (!CheckBallHitPaddle(Ball, Paddle))
{
DeleteBall(Ball);
Ball->Y = BRICKTICK_MAX_Y - 1;
DrawBall(Ball);
if (Lives == 1)
{ /*We ran out of lives.*/
ProcessGameOver(Ball, Paddle);
}
else
{
--Lives;
DrawStats();
WaitForUserLaunch();
DeleteBall(Ball);
DeletePaddle(Paddle);
ResetBall(Ball);
ResetPaddle(Paddle);
DrawPaddle(Paddle);
DrawBall(Ball);
/*Redraw but don't reset.*/
DrawAllBricks();
}
}
else
{
BounceBallY(Ball, UP);
if (PaddleMovedLastTick)
{ /*We can "whack" the ball with our Paddle->*/
Ball->DirX = PaddleMoveDir;
}
else
{ /*We cut the paddle into thirds for the X direction after bounce.*/
#define PADDLE_THIRD (Paddle->Length / 3)
if (Ball->X <= Paddle->X + PADDLE_THIRD)
{
Ball->DirX = LEFT;
}
else if (Ball->X > Paddle->X + PADDLE_THIRD && Ball->X <= Paddle->X + (PADDLE_THIRD * 2))
{
/*Make whether we hit up or not as a chance.*/
Bool StraightUp = rand() & 1;
if (StraightUp) Ball->DirX = X_NEUTRAL;
}
else
{
Ball->DirX = RIGHT;
}
}
}
}
PaddleMovedLastTick = false;
/*Bounce off left and right walls.*/
if (Ball->X >= BRICKTICK_MAX_X - 1)
{
Ball->X = BRICKTICK_MAX_X - 1;
BounceBallX(Ball, LEFT);
}
else if (Ball->X <= 0)
{
Ball->X = 0;
BounceBallX(Ball, RIGHT);
}
/*Check if a charm hit the paddle.*/
for (Inc = 0; Inc < BRICK_MAX_NUMLINES * BRICK_MAX_PERLINE; ++Inc)
{
if (Charms[Inc].Type == CHARM_NONE || !Charms[Inc].Dropped || Charms[Inc].Y != BRICKTICK_MAX_Y - 2) continue;
if (CheckCharmHitPaddle(Paddle, Charms + Inc))
{
void *Ptr = NULL;
const char *const Strings[] = { "+1,000 Score", "+1 Lives",
"10 Second Slow Ball", "3 second nuke mode" };
switch (Charms[Inc].Type)
{
case CHARM_SCORE:
Ptr = &Score;
break;
case CHARM_LIFE:
Ptr = &Lives;
break;
case CHARM_SLOW:
Ptr = &SlowBallTicks;
break;
case CHARM_NUKE:
Ptr = &BallNukeTicks;
break;
default:
break;
}
if (Charms[Inc].Type != CHARM_NONE)
{ /*Show a message on what type of charm we have here.*/
DrawMessage(Strings[Charms[Inc].Type - 1]); fflush(NULL);
usleep(500000);
DeleteMessage();
DrawAllBricks();
DrawStats();
}
/*Do the thing the charm does.*/
ProcessCharmAction(Charms + Inc, Ptr);
}
/*In any case, we're done with it.*/
DeleteCharm(Charms + Inc);
Charms[Inc].Type = CHARM_NONE;
}
/*We hit a brick.*/
if (BallStruckBrick(Ball, &Strike))
{
if (BallNukeTicks == 0) /*Nuclear ball passes through.*/
{
switch (Strike.StrikeV)
{
case STRIKE_TOP:
Ball->DirY = UP;
break;
case STRIKE_BOTTOM:
Ball->DirY = DOWN;
break;
default:
break;
}
switch (Strike.StrikeH)
{
case STRIKE_LEFT:
Ball->DirX = LEFT;
break;
case STRIKE_RIGHT:
Ball->DirX = RIGHT;
break;
default:
{
if (Ball->DirX != X_NEUTRAL) break;
else
{
Bool Dir = rand() & 1;
Ball->DirX = (DirectionX)Dir;
}
break;
}
}
}
DeleteBrick(Strike.Brick);
Score += 100;
DrawStats();
if (!BricksLeft())
{ /*Move to next level.*/
if (SetLevel(Level + 1))
{ /*We have more levels to go before we win.*/
DeleteAllBricks();
DeleteAllCharms();
DeleteBall(Ball);
DeletePaddle(Paddle);
DrawStats();
Score += 1000; /*Reward for making it this far.*/
Lives = BRICKTICK_NUMLIVES;
ResetBall(Ball);
ResetPaddle(Paddle);
ResetBricks();
DrawAllBricks();
DrawPaddle(Paddle);
WaitForUserLaunch();
DrawBall(Ball);
DrawAllBricks(); /*Redraw to fix WaitForUserLaunch() goofing.*/
}
else
{ /*WE WON!!!!*/
char WonBuf[256];
snprintf(WonBuf, sizeof WonBuf, "You Won! Score is %d! Hit ESC to exit or space to play again.", Score);
DrawMessage(WonBuf);
WinRegetch:
switch (getch())
{
case 27: /*27 is ESC*/
endwin();
exit(0);
break;
case ' ':
{
DeleteMessage();
SetLevel(1);
Lives = BRICKTICK_NUMLIVES;
Score = 0;
DeleteAllBricks();
DeleteAllCharms();
DeleteBall(Ball);
DeletePaddle(Paddle);
DrawStats();
ResetBall(Ball);
ResetPaddle(Paddle);
ResetBricks();
DrawAllBricks();
DrawPaddle(Paddle);
WaitForUserLaunch();
DrawBall(Ball);
DrawAllBricks(); /*Redraw to fix WaitForUserLaunch() goofing.*/
continue; /*For the loop we are in.*/
}
default:
goto WinRegetch;
}
}
continue;
}
else
{ /*Charm drops.*/
struct CHARM *Charm = GetCharmByBrick(Strike.Brick);
if (Charm)
{ /*We DO have a charm for this brick.*/
PerformCharmDrop(Charm); /*Mark it dropped.*/
/*Now draw the charm.*/
DrawCharm(Charm);
}
}
}
switch (Key)
{ /*Paddle movement.*/
case KEY_LEFT:
MovePaddle(Paddle, LEFT);
PaddleMovedLastTick = true;
PaddleMoveDir = LEFT;
break;
case KEY_RIGHT:
MovePaddle(Paddle, RIGHT);
PaddleMovedLastTick = true;
PaddleMoveDir = RIGHT;
break;
case 's': /*They want to save the game.*/
if (SaveGame(Ball, Paddle))
{
DrawMessage("Game saved.");
}
else
{
DrawMessage("Failed to save game.");
}
fflush(NULL);
usleep(500000);
DeleteMessage();
DrawAllBricks(); /*Redraw bricks if damaged.*/
break;
case 'o': /*They want us to load a game.*/
{
const Bool LoadedOk = LoadGame(Ball, Paddle);
if (LoadedOk)
{
/*Restore the state.*/
clear();
DrawBorders(); /*Need to redraw these after clearing the screen.*/
DrawBall(Ball);
DrawPaddle(Paddle);
DrawStats();
DrawAllBricks();
DrawMessage("Game loaded.");
}
else
{
DrawMessage("Failed to load game.");
}
fflush(NULL);
usleep(500000);
DeleteMessage();
DrawAllBricks();
break;
}
case ' ':
{
DrawMessage("PAUSED");
cbreak();
PauseRegetch:
switch (getch())
{
case ' ':
DeleteMessage();
DrawAllBricks(); /*Redraw to fix what deleting the message messed up.*/
halfdelay(1);
break;
case 27: /*27 is ESC*/
endwin();
exit(0);
break;
default:
goto PauseRegetch;
}
}
default:
break;
}
Flip = !Flip;
/*Ball movement, obviously. Flip is used to keep it at half speed if we got a 'slow' charm.*/
if (Flip || SlowBallTicks == 0) MoveBall(Ball);
/*Decrement slow ball ticks until zero, then the ball goes fast again.*/
if (SlowBallTicks > 0) --SlowBallTicks;
/*Decrement nuclear ball until ticks hit zero.*/
if (BallNukeTicks > 0) --BallNukeTicks;
/*Charm movement.*/
for (Inc = 0; Inc < BRICK_MAX_NUMLINES * BRICK_MAX_PERLINE; ++Inc)
{
if (Charms[Inc].Type == CHARM_NONE || !Charms[Inc].Dropped) continue;
if (Flip) MoveCharm(Charms + Inc);
}
/*Redraw borders in case of terminal resize.*/
DrawBorders();
}
}
static void ProcessGameOver(struct BALL *Ball, struct PADDLE *Paddle)
{
char OutBuf[255];
snprintf(OutBuf, sizeof OutBuf, "GAME OVER! Score is %d. Hit ESC to exit or space to play again.", Score);
Lives = 0;
DrawStats();
Lives = 3;
Score = 0;
DrawMessage(OutBuf);
cbreak();
AskAgain:
switch (getch())
{
case 27: /*27 is ESC.*/
endwin();
exit(0);
break;
case ' ':
break;
default:
goto AskAgain;
}
halfdelay(1);
DeleteMessage();
DeleteBall(Ball);
DeletePaddle(Paddle);
DeleteAllBricks();
DeleteAllCharms();
SetLevel(1); /*Set back to the default level again.*/
ResetPaddle(Paddle);
ResetBricks();
DrawPaddle(Paddle);
DrawAllBricks();
DrawStats();
/*Assume they want to play again.*/
WaitForUserLaunch();
ResetBall(Ball);
DrawBall(Ball);
DrawAllBricks(); /*Redraw to fix WaitForUserLaunch() goofing.*/
}
static Bool SetLevel(const int Level_)
{
if (Level_ > BRICKTICK_NUMLEVELS) return false;
Level = Level_;
HeightFromPaddle = Levels[Level - 1].HeightFromPaddle;
BrickNumLines = Levels[Level - 1].BrickNumLines;
BricksPerLine = Levels[Level - 1].BricksPerLine;
return true;
}
int main(int argc, char **argv)
{
int Inc = 1;
struct BALL Ball = { 0 };
struct PADDLE Paddle = { 0 };
Bool DoLoadGame = false;
int GotoLevel = 0;
for (; Inc < argc; ++Inc)
{ /*Argument parsing.*/
if (!strcmp("--nocolor", argv[Inc]))
{
UseColor = false;
}
else if (!strcmp("--version", argv[Inc]))
{
printf("Bricktick brick breaker v" BRICKTICK_VERSION "\n"
"By Subsentient.\n");
fflush(NULL);
exit(0);
}
else if (!strcmp("--help", argv[Inc]))
{
printf("Command line options:\n\t"
"--help: Show this help.\n\t"
"--nocolor: Run Bricktick with no color.\n\t"
"--version: Display version and exit.\n");
fflush(NULL);
exit(0);
}
else if (!strncmp("--level=", argv[Inc], sizeof "--level=" - 1))
{
GotoLevel = atoi(argv[Inc] + sizeof "--level=" - 1);
if (GotoLevel > sizeof Levels / sizeof *Levels)
{
fprintf(stderr, "The maximum level is %u.\n", (unsigned)(sizeof Levels / sizeof *Levels)); fflush(NULL);
exit(1);
}
}
else
{
fprintf(stderr, "Bad command line argument \"%s\".\n"
"Use --help for command line options.\n", argv[Inc]);
exit(1);
}
}
initscr(); /*Fire up ncurses.*/
if (COLS < BRICKTICK_MAX_X || LINES < BRICKTICK_MAX_Y)
{
endwin();
fprintf(stderr, "Please use a console with a resolution of at least %dx%d.\n", BRICKTICK_MAX_X, BRICKTICK_MAX_Y);
exit(1);
}
/*Color not supported.*/
if (!has_colors()) UseColor = false;
/*Various ncurses things.*/
noecho();
if (UseColor)
{ /*Color fireup.*/
start_color();
init_pair(1, COLOR_CYAN, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLACK, COLOR_WHITE);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_GREEN, COLOR_BLACK);
init_pair(6, COLOR_YELLOW, COLOR_BLACK);
init_pair(7, COLOR_RED, COLOR_BLACK);
/*Fire up colors for the bricks.*/
init_pair(BRICK_COLORS_START, COLOR_GREEN, COLOR_BLACK);
init_pair(BRICK_COLORS_START + 1, COLOR_BLUE, COLOR_BLACK);
init_pair(BRICK_COLORS_START + 2, COLOR_YELLOW, COLOR_BLACK);
init_pair(BRICK_COLORS_START + 3, COLOR_MAGENTA, COLOR_BLACK);
init_pair(BRICK_COLORS_START + 4, COLOR_CYAN, COLOR_BLACK);
init_pair(BRICK_COLORS_END, COLOR_RED, COLOR_BLACK);
}
/*Draw borders for consoles bigger than our 80x24 playing area.*/
DrawBorders();
/*Greeting.*/
GreetAsk:
if (!GotoLevel)
{ /*If they specified a level let them go straigth to game start.*/
DrawGreeting();
switch (getch())
{
case 27: /*27 is ESC key*/
endwin();
exit(0);
break;
case ' ':
clear(); /*Wipe the message from the screen.*/
DrawBorders(); /*Redraw borders.*/
break;
case 'o':
clear();
DrawBorders();
DoLoadGame = true;
LoadGame(&Ball, &Paddle);
break;
default:
goto GreetAsk;
}
}
else
{
clear();
DrawBorders();
}
halfdelay(1);
keypad(stdscr, true);
#if NCURSES_VERSION_MAJOR >= 5 && NCURSES_VERSION_MINOR >=4
set_escdelay(25);
#endif
curs_set(0);
if (DoLoadGame)
{
DrawStats();
DrawBall(&Ball);
DrawPaddle(&Paddle);
DrawAllBricks();
}
else
{
/*Show our initial lives count.*/
DrawStats();
/*Reset to level 1 if we haven't specified a level.*/
SetLevel(GotoLevel ? GotoLevel : 1);
ResetBall(&Ball);
ResetPaddle(&Paddle);
DrawBall(&Ball);
DrawPaddle(&Paddle);
ResetBricks();
DrawAllBricks();
}
/*Wait for L key.*/
WaitForUserLaunch();
DrawAllBricks(); /*Redraw to fix goofing by WaitForUserLaunch().*/
/*Set up the number generator.*/
srand(time(NULL));
/**
* Main loop for the game!
**/
Reloop:
GameLoop(&Ball, &Paddle);
DrawMessage("Really quit Bricktick? y/n");
cbreak();
/*When the game loop exits.*/
switch (getch())
{
case 'y':
case 'Y':
break;
default:
DeleteMessage();
refresh();
halfdelay(1);
DrawAllBricks(); /*Fix damage to brick display resulting from the message.*/
goto Reloop;
break;
}
DeleteBall(&Ball);
DeletePaddle(&Paddle);
endwin();
return 0;
}
static void DrawMessage(const char *const Message)
{
move(BRICKTICK_MAX_Y / 2, (BRICKTICK_MAX_X - strlen(Message)) / 2);
addstr(Message);
refresh();
}
static void DrawGreeting(void)
{
int Inc = 0;
const struct
{
const char *Msg;
int Color;
} Greeting[] = {
{ "Welcome to Bricktick v" BRICKTICK_VERSION "!", COLOR_PAIR(1) },
{ "Charms:", COLOR_PAIR(1) },
{ "% is +1,000 score.", COLOR_PAIR(5) },
{ "@ is +1 lives,", COLOR_PAIR(6) },
{ "# is 10 second slow ball.", COLOR_PAIR(7) },
{ "^ is 3 second nuke mode.", COLOR_PAIR(4) },
{ "Ingame, press 's' to save a game and 'o' to load it.", COLOR_PAIR(1) },
{ "Press space to start a game, 'o' to load one, or ESC to exit.", COLOR_PAIR(1) },
{ NULL }
};
for (; Greeting[Inc].Msg != NULL; ++Inc)
{
move(BRICKTICK_MAX_Y / 2 + Inc - (sizeof Greeting / sizeof *Greeting / 2),
(BRICKTICK_MAX_X - strlen(Greeting[Inc].Msg)) / 2);
if (UseColor) attron(Greeting[Inc].Color);
addch(*Greeting[Inc].Msg); /*We do the first character so only the first charm is colored.*/
if (UseColor) attroff(Greeting[Inc].Color), attrset(COLOR_PAIR(1));
addstr(Greeting[Inc].Msg + 1);
}
refresh();
}
static void DeleteMessage(void)
{
int Inc = 0;
move(BRICKTICK_MAX_Y / 2, 0);
for (; Inc < BRICKTICK_MAX_X - 1; ++Inc)
{
addch(' ');
}
refresh();
}
static void DrawStats(void)
{
int Inc = 0;
const char *const Formats[] = { "Lives: %d", "Score: %d", "Level: %d" };
const int *const Ptrs[sizeof Formats / sizeof *Formats] = { &Lives, &Score, &Level };
/*Move to our starting position.*/
move(0, 2);
/*Clear any old.*/
for (; Inc < BRICKTICK_MAX_X - 1; ++Inc)
{
addch(' ');
}
/*Print the new.*/
move(0, 2);
for (Inc = 0; Inc < sizeof Formats / sizeof *Formats; ++Inc)
{
if (UseColor) attron(COLOR_PAIR(3));
printw(Formats[Inc], *Ptrs[Inc]);
if (UseColor)
{
/*For us.*/
attroff(COLOR_PAIR(3));
/*For everyone else.*/
attrset(COLOR_PAIR(1));
}
/*Padding.*/
if (Inc + 1 != sizeof Formats / sizeof *Formats) addch('\t'), addch('\t'), addch('\t');
}
refresh();
}
static void WaitForUserLaunch(void)
{
int Key;
DrawMessage("Hit L to launch.");
while ((Key = getch()) != 'l' && Key != 'L' && Key != 27);
DeleteMessage();
if (Key == 27) /*ESC*/
{
endwin();
exit(0);
}
}
static Bool SaveGame(const struct BALL *Ball, const struct PADDLE *Paddle)
{ /*Writes the current game, score, etc to our config dir.*/
FILE *Desc = NULL;
char SaveFile[2048];
struct stat FileStat;
char ConfigDir[1024];
snprintf(ConfigDir, sizeof SaveFile, "%s/.bricktick", getenv(HOME_ENV));
snprintf(SaveFile, sizeof SaveFile, "%s/savegame.bin", ConfigDir);
/*Create config directory if it does not exist.*/
if (stat(ConfigDir, &FileStat) != 0) BT_MKDIR(ConfigDir, 0755);
if (!(Desc = fopen(SaveFile, "wb")))
{
return false;
}
/*write savegame version.*/
fwrite(&SaveGameVersion, 1, sizeof(int), Desc);
/*write bricks.*/
fwrite(&Bricks, 1, sizeof Bricks, Desc);
/*write charms.*/
fwrite(&Charms, 1, sizeof Charms, Desc);
/*write paddle.*/
fwrite(Paddle, 1, sizeof(struct PADDLE), Desc);
/*write ball.*/
fwrite(Ball, 1, sizeof(struct BALL), Desc);
/*write some extra brick data.*/
fwrite(&BrickNumLines, 1, sizeof(int), Desc);
fwrite(&BricksPerLine, 1, sizeof(int), Desc);
fwrite(&HeightFromPaddle, 1, sizeof(int), Desc);
/*write score, lives, and level.*/
fwrite(&Score, 1, sizeof(int), Desc);
fwrite(&Lives, 1, sizeof(int), Desc);
fwrite(&Level, 1, sizeof(int), Desc);
/*We're done.*/
fclose(Desc);
return true;
}
static Bool LoadGame(struct BALL *OutBall, struct PADDLE *OutPaddle)
{
FILE *Desc = NULL;
char SaveFile[1024];
unsigned Ver;
snprintf(SaveFile, sizeof SaveFile, "%s/.bricktick/savegame.bin", getenv(HOME_ENV));
if (!(Desc = fopen(SaveFile, "rb")))
{
return false;
}
/**Check savegame version!**/
fread(&Ver, 1, sizeof Ver, Desc);
if (Ver != SaveGameVersion)
{
fclose(Desc);
return false;
}
/*restore bricks.*/
fread(&Bricks, 1, sizeof Bricks, Desc);
/*restore charms.*/
fread(&Charms, 1, sizeof Charms, Desc);
/*restore paddle.*/
fread(OutPaddle, 1, sizeof(struct PADDLE), Desc);
/*restore ball.*/
fread(OutBall, 1, sizeof(struct BALL), Desc);
/*restore some extra brick data.*/
fread(&BrickNumLines, 1, sizeof(int), Desc);
fread(&BricksPerLine, 1, sizeof(int), Desc);
fread(&HeightFromPaddle, 1, sizeof(int), Desc);
/*restore score, lives, and level.*/
fread(&Score, 1, sizeof(int), Desc);
fread(&Lives, 1, sizeof(int), Desc);
fread(&Level, 1, sizeof(int), Desc);
/*We're done.*/
fclose(Desc);
return true;
}
static void DrawBorders(void)
{ /*If we're greater than 80x24, draw borders for us so it doesn't look like the ball will fly out into nowhere.*/
unsigned Inc = 0;
if (LINES > BRICKTICK_MAX_Y)
{
move(BRICKTICK_MAX_Y, 0);
for (; Inc < BRICKTICK_MAX_X + 1; ++Inc)
{
if (UseColor) addch('-' | COLOR_PAIR(1));
else addch('-');
}
}
if (COLS > BRICKTICK_MAX_X)
{
for (Inc = 0; Inc < BRICKTICK_MAX_Y + 1; ++Inc)
{
move(Inc, BRICKTICK_MAX_X);
if (UseColor) addch('|' | COLOR_PAIR(1));
else addch('|');
}
}
refresh();
}