-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterminal_rpg_options.c
326 lines (297 loc) · 9.06 KB
/
terminal_rpg_options.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
/*
* =====================================================================================
*
* Filename: terminal_rpg_options.c
*
* Description:
*
* Version: 1.0
* Created: 02/05/2019 08:30:15 PM
* Revision: none
* Compiler: gcc
*
* Author: Miguel Fernandes
* Organization:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include "terminal_rpg_options.h"
#include "terminal_rpg_window_size.h"
#include "terminal_rpg_music.h"
//CONTROLS - Strings
char * controls_msg [] = {
"CONTROLS",
"- ARROWS : For movement and selection like usual;",
"- ENTER : To confirm selection and interact;",
"- BACKSPACE : To return, when option is not available;",
"- M : To show the map (just a placeholder);",
"Press any key to return."
};
//CONTROLS - Variables
int controls_msg_size = 6;
//OPTIONS - Strings
char * options_msg [] = {
"OPTIONS",
"- MUSIC VOLUME -",
"- Return -",
};
char * options_music_volume [] = {
"<",
" ",
"[",
"!", // 10 of these for 100% / music_volume = 10; [3]
".", // or 10 of these for 0% / music_volume = 0; [4]
"]",
" ",
">"
};
//OPTIONS - Variables
int options_msg_size = 3;
int options_longest_str = 0;
int options_music_volume_size = 8;
//QUIT - Strings
char * quit_msg = "Do you really want to quit?";
char * error_msg = "An error occurred !!! - quit() returned -1 ";
char * quit_choice [] = {
"YES",
"NO"
};
int controls () {
int row, col;
getmaxyx(stdscr, row, col);
clear();
mvprintw((row / 4), (col - strlen(controls_msg[0])) / 2, "%s", controls_msg[0]); //title
int longest_str = 0;
for (int i = 1; i < controls_msg_size; i++) {
if(strlen(controls_msg[i]) > longest_str)
longest_str = strlen(controls_msg[i]);
}
for(int i = 1; i < controls_msg_size; i++){
if(i == controls_msg_size - 1 )
mvprintw((row / 4) + 3 + i, (col - strlen(controls_msg[i])) / 2, "%s", controls_msg[i]);
else
mvprintw((row / 4) + 2 + i, (col - longest_str) / 2, "%s", controls_msg[i]);
}
refresh();
getch();
return 0;
}
int options () {
while(1) {
int c = options_handling(options_msg_size, 1);
switch(c) {
case 1:
// first arg is the starting highlight inside the function, second is equal to the switch case;
options_handling_music(options_msg_size, 0, 1);
break;
case 2:
return 0;
break;
default:
break;
}
}
return 0;
}
int options_def_longest_str () {
for(int i = 1; i < options_msg_size; i++) {
if(strlen(options_msg[i]) > options_longest_str)
options_longest_str = strlen(options_msg[i]);
}
return 0;
}
int options_handling (int argc, int highlight) {
while(1) {
if(options_write(argc, highlight) == 1)
continue;
int c = getch();
switch(c) {
case KEY_UP:
if(highlight == 1)
highlight = argc - 1;
else
highlight--;
break;
case KEY_DOWN:
if(highlight == argc - 1)
highlight = 1;
else
highlight++;
break;
case 10:
return highlight;
default:
break;
}
}
return -1;
}
int options_handling_music (int argc, int highlight, int option) {
int music_volume = music_return_music_volume();
while(1) {
options_write_music(argc, highlight, option);
int c = getch();
switch(c) {
case KEY_LEFT:
if(highlight == 0)
highlight = 1;
else
highlight = 0;
break;
case KEY_RIGHT:
if(highlight == 1)
highlight = 0;
else
highlight = 1;
break;
case 10:
if(highlight == 0 && music_volume > 0) {
music_volume--;
music_dec_music_volume();
}
if(highlight == 1 && music_volume < 10) {
music_volume++;
music_inc_music_volume();
}
break;
case 127:
return 0;
default:
break;
}
}
return -1;
}
int options_write (int argc, const int highlight) {
int row, col;
getmaxyx(stdscr, row, col);
if(window_size_check(row, col, 25, 80) == 1) {
return 1;
}
clear();
mvprintw((row / 4), (col - strlen(options_msg[0])) / 2, "%s", options_msg[0]); //title
for(int i = 1; i < argc; i++){
if(highlight == i) {
attron(A_REVERSE);
mvprintw((row / 4) + 2 + i, (col - options_longest_str) / 3, "%s", options_msg[i]);
attroff(A_REVERSE);
}
else
mvprintw((row / 4) + 2 + i, (col - options_longest_str) / 3, "%s", options_msg[i]);
if(i == 1)
options_write_music_volume(2, row, col);
}
return 0;
}
int options_write_music (int argc, const int highlight, int option) {
int row, col;
getmaxyx(stdscr, row, col);
if(window_size_check(row, col, 25, 80) == 1) {
return 1;
}
clear();
mvprintw((row / 4), (col - strlen(options_msg[0])) / 2, "%s", options_msg[0]); //title
for(int i = 1; i < argc; i++){
mvprintw((row / 4) + 2 + i, (col - options_longest_str) / 3, "%s", options_msg[i]);
}
if(highlight == 0) {
options_write_music_volume(0, row, col);
}
else {
options_write_music_volume(1, row, col);
}
return 0;
}
int options_write_music_volume (const int highlight, int row, int col) {
int music_volume = music_return_music_volume();
mvprintw((row / 4) + 3, col / 2, " ");
for(int i = 0; i < options_music_volume_size; i++) {
if(highlight == 0 && i == 0) {
attron(A_REVERSE);
printw("%s", options_music_volume[i]);
attroff(A_REVERSE);
continue;
}
if(highlight == 1 && i == options_music_volume_size - 1) {
attron(A_REVERSE);
printw("%s", options_music_volume[i]);
attroff(A_REVERSE);
continue;
}
if(i == (options_music_volume_size / 2) - 1) {
for(int c = 0; c < music_volume; c++) {
printw("%s", options_music_volume[i]);
}
continue;
}
if(i == (options_music_volume_size / 2) ) {
for(int c = 10; c > music_volume; c--) {
printw("%s", options_music_volume[i]);
}
continue;
}
printw("%s", options_music_volume[i]);
}
return 0;
}
int quit () {
int highlight = quit_handling(0);
if(highlight == 0)
return 0;
if(highlight == 1)
return 1;
if(highlight == -1) {
clear();
int row, col;
getmaxyx(stdscr, row, col);
mvprintw(row /2, (col - strlen(error_msg)) / 2, "%s", error_msg);
}
return -1;
}
int quit_handling (int highlight) {
while(1) {
int c;
quit_write(highlight);
switch(c = getch()) {
case KEY_LEFT:
if(highlight == 0)
highlight = 1;
else
highlight = 0;
break;
case KEY_RIGHT:
if(highlight == 1)
highlight = 0;
else
highlight = 1;
break;
case 10:
return highlight;
default:
break;
}
}
return -1;
}
int quit_write (const int highlight) {
clear();
int row, col;
getmaxyx(stdscr, row, col);
mvprintw(row / 2 - 1, (col - strlen(quit_msg)) / 2, "%s", quit_msg);
if(highlight == 0) {
attron(A_REVERSE);
mvprintw( (row / 2) + 1, (col - 8) / 2, "%s", quit_choice[0]);
attroff(A_REVERSE);
printw(" %s", quit_choice[1]);
} else {
mvprintw( (row / 2) + 1, (col - 8) / 2, "%s", quit_choice[0]);
attron(A_REVERSE);
printw("%s", quit_choice[1]);
attroff(A_REVERSE);
}
return 0;
}
//eof