xTetris-Game
Loading...
Searching...
No Matches
gameMenu.c
Go to the documentation of this file.
1#include <locale.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <wchar.h>
6
7#include "gameMenu.h"
8#include "common.h"
9#include "definitions.h"
10#include "struct.h"
11
12void welcome(void){
13 clearCLI();
14 heightSpacing(13);
15 widthSpacing(62);
16 wprintf(L" ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ\r\n");
17 widthSpacing(62);
18 wprintf(L"๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ\r\n");
19 widthSpacing(62);
20 wprintf(L" ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ\r\n");
21 widthSpacing(62);
22 wprintf(L"๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ\r\n");
23 widthSpacing(62);
24 wprintf(L"๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ\r\n");
25 wprintf(L"\r\n");
26 wprintf(L"\r\n");
27 wprintf(L"\r\n");
28 wprintf(L"\r\n");
29 printCentered(L"๐ŸŽฎ Controls: W๐Ÿ”„, Aโช, S๐Ÿ”ƒ, Dโฉ, Spacebar๐Ÿ‘ฝ, Enterโœ…, EscโŽ");
30 wprintf(L"\r\n");
31 wprintf(L"\r\n");
32 printCentered(L"๐Ÿ•น Press ENTER to continue...");
33}
34
35void goodbye(void){
36 clearCLI();
37 heightSpacing(10);
38 widthSpacing(30);
39 wprintf(L" โœจ\r\n");
40 widthSpacing(30);
41 wprintf(L"๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ\r\n");
42 widthSpacing(30);
43 wprintf(L"๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ\r\n");
44 widthSpacing(30);
45 wprintf(L"๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ\r\n");
46 widthSpacing(30);
47 wprintf(L"๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ\r\n");
48 widthSpacing(30);
49 wprintf(L"๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ ๐Ÿ”ฒ ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ๐Ÿ”ฒ\r\n");
50 wprintf(L"\r\n");
51 delayTimer(1);
52}
53
54void printMenuItems(int start, int end, int selected){
55 int i;
56 wchar_t *item[9];
57 item[0]=L"-->";
58 item[1]=L"๐ŸŽฎ Start Game";
59 item[2]=L"๐Ÿ†˜ Guide";
60 item[3]=L"๐Ÿ’ณ Credits";
61 item[4]=L"๐Ÿต Exit";
62 item[5]=L"๐Ÿบ Singleplayer";
63 item[6]=L"๐Ÿป Multiplayer";
64 item[7]=L"๐Ÿ”™ Return";
65 item[8]=L"<--";
66 for(i=start; i<=end; i++){
67 if(i==selected){
68 widthSpacing(wcslen(item[0]) + wcslen(item[i]) + wcslen(item[8]));
69 wprintf(L"%ls%ls%ls\r\n", item[0], item[i], item[8]);
70 }
71 else
72 printCentered(item[i]);
73 }
74}
75
76void printMainMenu(int *opt){
78 printMenuItems(1, 4, (*opt)-1);
79}
80
81void printPlayerMode(int* opt){
83 printMenuItems(1, 1, 0);
84 wprintf(L"\r\n");
85 printMenuItems(5, 7, (*opt)-1);
86}
87
88void printGuide(void){
89 heightSpacing(16);
90 printMenuItems(2, 2, 0);
91 wprintf(L"\r\n");
92 wprintf(L"- Goal: Put your organizational skills and endurance to the test by clearing as many lines as possible.\r\n");
93 wprintf(L"- Clear lines: Maneuver the falling Tetriminos to fit them together within the Matrix. To clear a line, fill every square within a single row.\r\n");
94 wprintf(L"- Score points: Earn points by clearing lines. Clear multiple lines at once to increase your scoring opportunities.\r\n");
95 wprintf(L"- Ghost piece: Use the Ghost Piece to determine the best fit for the falling Tetrimino. This helpful guide appears directly below the falling Tetrimino and displays possible placements.\r\n");
96 wprintf(L"- Next queue: Preview the upcoming Tetrimino in the Next Queue to plan ahead and increase your scoring opportunities.\r\n");
97 wprintf(L"- Hold queue: Store a falling Tetrimino in the Hold Queue for later use.\r\n");
98 wprintf(L"- Game over: Stack the Tetriminos too high and the game is over!\r\n");
99 wprintf(L"\r\n");
100 printMenuItems(7, 7, 7);
101}
102
103void printCredits(void){
104 heightSpacing(6);
105 printMenuItems(3, 3, 0);
106 wprintf(L"\r\n");
107 printCentered(L"Edoardo Tosin");
108 wprintf(L"\r\n");
109 wprintf(L"\r\n");
110 printMenuItems(7, 7, 7);
111}
112
113void printCurrentMenu(int *menu, int *opt){
114 clearCLI();
115 /*sound();*/
116 if(*menu==1)
117 printMainMenu(opt);
118 else if(*menu==2)
119 printPlayerMode(opt);
120 else if(*menu==3)
121 printGuide();
122 else if(*menu==4)
123 printCredits();
124}
125
126int choiceCtrl(int menu, int opt, int min, int max){
127 int key=RESET;
128 printCurrentMenu(&menu, &opt);
129 while(1){
130 if(u_kbhit()){
131 key=u_getchar();
132 if(key==LOWER_W || key==UPPER_W || key==LOWER_S || key==UPPER_S){
133 if(key==LOWER_W || key==UPPER_W){
134 (opt)--;
135 if(opt<min)
136 opt=max;
137 }
138 else if(key==LOWER_S || key==UPPER_S){
139 (opt)++;
140 if(opt>max)
141 opt=min;
142 }
143 printCurrentMenu(&menu, &opt);
144 }
145 else if(key==CARRIAGE_RETURN){
146 return opt;
147 }
148 else if(key==ESCAPE){
149 if(opt>=6 && opt<=8)
150 return 8;
151 else
152 return 1;
153 }
154 else if(key==CTRL_C)
155 exitFailure();
156 }
157 usleep(NSEC_TO_SLEEP);
158 }
159 exitFailure();
160}
void printCentered(wchar_t *text)
Print text centered horizontally based on string length.
Definition common.c:71
void delayTimer(int time)
Call the system sleep.
Definition common.c:41
void heightSpacing(int filledHeight)
Apply a vertical spacing based on window heigth and what need to be printed.
Definition common.c:76
void widthSpacing(int filledWidth)
Apply an horizontal spacing based on window width and what need to be printed.
Definition common.c:62
void exitFailure(void)
Exit the program after cleaning everything on screen.
Definition common.c:36
void clearCLI(void)
Call the system function to clear the cli.
Definition common.c:25
int u_getchar(void)
Definition common.c:118
int u_kbhit(void)
Definition common.c:91
#define LOWER_W
Definition definitions.h:30
#define CARRIAGE_RETURN
Definition definitions.h:20
#define UPPER_S
Definition definitions.h:27
#define CTRL_C
Definition definitions.h:19
#define RESET
Definition definitions.h:22
#define UPPER_W
Definition definitions.h:25
#define LOWER_S
Definition definitions.h:32
#define NSEC_TO_SLEEP
Definition definitions.h:43
#define ESCAPE
Definition definitions.h:21
void printCurrentMenu(int *menu, int *opt)
Call the print of the current menu.
Definition gameMenu.c:113
void printMainMenu(int *opt)
Print main menu.
Definition gameMenu.c:76
void goodbye(void)
Print the end screen.
Definition gameMenu.c:35
void welcome(void)
Print the intro screen.
Definition gameMenu.c:12
void printMenuItems(int start, int end, int selected)
Print main menu.
Definition gameMenu.c:54
int choiceCtrl(int menu, int opt, int min, int max)
Create the board.
Definition gameMenu.c:126
void printCredits(void)
Print the credits menu.
Definition gameMenu.c:103
void printPlayerMode(int *opt)
Print player mode menu.
Definition gameMenu.c:81
void printGuide(void)
Print the rules menu.
Definition gameMenu.c:88