-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.c
More file actions
312 lines (255 loc) · 7.06 KB
/
console.c
File metadata and controls
312 lines (255 loc) · 7.06 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "engine.h"
#include "world.h"
#include "player.h"
#include "gfx.h"
#include "input.h"
#include "console.h"
#include "console_commands.h"
Console console;
//Actions allowed to happen while console is open
ActionCode console_enabled_actions[] =
{
ACTION_QUIT,
ACTION_TOGGLE_CONSOLE,
ACTION_CONFIRM_CONSOLE,
ACTION_MOUSE_MOVE,
ACTION_MOUSE_DRAG_RIGHT,
ACTION_SCROLL_WHEEL
};
const static uint32_t console_enabled_actions_size = sizeof(console_enabled_actions)/sizeof(ActionCode);
//Initializes all console state variables.
void init_console()
{
console.open = false;
memset(console.history, '\0', CONSOLE_CHAR_LIMIT * CONSOLE_HISTORY_SIZE);
set_console_font(0);
console.background_color = color(64, 35, 0, 170);
console.entry_color = color(80, 50, 0, 170);
console.history_text_color = color(128, 70, 40, 170);
console.entry_text_color = color(200, 160, 100, 170);
//Creates the rainbow effect holder for the console rainbow strip(tm)
console.rainbow_effect = create_effect_rainbow(0.25, 200);
}
//Draws the console
void draw_console()
{
//Updates the console rainbow effect, a rainbow effect doesnt do anything on update
//So NULL for second argument
update_effect(&console.rainbow_effect, NULL);
//Gets the current installed font at the console font_ID
Font font = GFX_get_font(console.font);
//Draws console background
for(int i = 0; i < CONSOLE_HISTORY_SIZE; i++)
{
//Calculates the offset of each line in relation to the font height.
Point2 offset = point2(0, (font.char_h + 1)*i);
//Gets whats the current background color is
//If its the last line, gets entry color instead of background color
Color back_color;
if(i == CONSOLE_HISTORY_SIZE-1) back_color = console.entry_color;
else back_color = console.background_color;
//Fills the background rectangle for each line.
GFX_fill_rectangle( offset_rect(rect(0, 0, gfx.screen_res_x, font.char_h+1),
offset.x, offset.y),
back_color);
}
//Draws the rainbow strip at the bottom of the console
Color rainbow_strip_color;
for(int i = 0; i < gfx.screen_res_x; i++)
{
//temp is the angle offset of the effect,
float temp = (i*2.0*M_PI)/gfx.screen_res_x;
//gets the current state of the effect offset by temp and puts it in rainbow_strip_color
get_effect(&console.rainbow_effect, &temp, &rainbow_strip_color);
//Draw current pixel of the rainbow strip
GFX_set_pixel(i, (font.char_h+1)*CONSOLE_HISTORY_SIZE, rainbow_strip_color);
}
//Flush pixels to GPU (And screen)
GFX_update_pixels();
//Draws console text
for(int i = 0; i < CONSOLE_HISTORY_SIZE; i++)
{
//Calculates the offset of each line in relation to the font height.
Point2 offset = point2(0, (font.char_h+1)*i);
Color text_color;
if(i == CONSOLE_HISTORY_SIZE-1) text_color = console.entry_text_color;
else text_color = console.history_text_color;
//Draws the actual Console text history
GFX_draw_string_color( offset, console.font, get_console_history(CONSOLE_HISTORY_SIZE-1-i),
text_color);
}
}
void set_console_font(uint32_t font)
{
console.font = font;
}
void toggle_console(void * console_)
{
Console* console = (Console*)console_;
set_console_open(!console->open);
}
void set_console_open(bool value)
{
if(value)
{
set_enabled_actions(console_enabled_actions, console_enabled_actions_size);
start_text_input(console.history[0], CONSOLE_CHAR_LIMIT);
console.open = true;
}
else
{
set_enabled_actions(NULL, 0);
end_text_input();
console.open = false;
}
}
bool is_console_open()
{
return console.open;
}
char* get_console_history(int history_index)
{
if(history_index >= 0 && history_index < CONSOLE_HISTORY_SIZE)
{
return (char*)(console.history + history_index);
}
else
{
return NULL;
}
}
void scroll_console(int lines)
{
char buffer[CONSOLE_CHAR_LIMIT * (CONSOLE_HISTORY_SIZE - 1)];
for(int i = 0; i < lines; i ++)
{
memcpy(buffer, console.history, CONSOLE_CHAR_LIMIT * (CONSOLE_HISTORY_SIZE - 1));
memcpy(console.history + 1, buffer, CONSOLE_CHAR_LIMIT * (CONSOLE_HISTORY_SIZE - 1));
memset(console.history, '\0', CONSOLE_CHAR_LIMIT);
}
}
void printf_console(const char * char_string, ...)
{
char buffer[CONSOLE_CHAR_LIMIT] = {0};
va_list args;
va_start(args, char_string);
vsnprintf(buffer, CONSOLE_CHAR_LIMIT, char_string, args);
va_end(args);
int cursor_location = 0;
for(int i = 0; i < CONSOLE_CHAR_LIMIT && buffer[i] != '\0'; i++)
{
char character = buffer[i];
if(character == '\n')
{
scroll_console(1);
cursor_location = 0;
}
else
{
*(console.history[0] + cursor_location) = character;
cursor_location++;
}
}
}
bool command_check(char * command, char** token, int token_number)
{
if(strcmp(command, *(token + token_number)) == 0 )
{
return true;
}
else
{
return false;
}
}
char* get_token_value(char** token, int index)
{
return *(token + index);
}
ConsoleCommand console_commands[] =
{
{"ver", 0, command_ver},
{"intro", 0, command_intro},
{"help", 1, command_help},
{"vertex", 1, command_vertex},
{"vertexlist", 1, command_vertex_list},
{"delvertex", 1, command_remove_vertex},
{"mode", 1, command_mode},
/* {"sector_show", 1, COMMAND_sector_show},
{"vertex_list", 0, COMMAND_vertex_list},
{"noclip", 0, COMMAND_vertex_list},
{"set_tint", 3, COMMAND_set_tint},
{"set", 2, COMMAND_set},
// {"save_level", 1, COMMAND_save_level},
// {"load_level", 1, COMMAND_load_level},
// {"edit", 0, COMMAND_edit}*/
};
static uint32_t console_commands_size = sizeof(console_commands)/sizeof(ConsoleCommand);
void parse_tokens(char** tokens, int args_num)
{
ConsoleCommand command;
for(int i = 0; i < console_commands_size; i++)
{
if(strcmp(console_commands[i].command, tokens[0]) == 0)
{
if((args_num-1) >= console_commands[i].arg_num)
{
console_commands[i].function(tokens + 1);
return;
}
else
{
printf_console( "The %s command takes %d arguments.",
console_commands[i].command,
console_commands[i].arg_num);
}
}
}
scroll_console(1);
}
#define MAX_ARGSIZE 32
void parse_console(const char* text_input)
{
char** tokens;
int text_location = 0;
int args_num = 0;
tokens = malloc(1 * sizeof(char*));
while(text_location < CONSOLE_CHAR_LIMIT)
{
while(text_input[text_location] == ' ')
{
text_location++;
}
if(text_input[text_location] == '\0') break;
args_num++;
tokens = realloc(tokens, args_num * sizeof(char*));
tokens[args_num-1] = malloc(MAX_ARGSIZE * sizeof(char));
memset(tokens[args_num-1], '\0', MAX_ARGSIZE);
for(int parser_location = 0;
parser_location < CONSOLE_CHAR_LIMIT &&
text_input[text_location] != '\0' &&
text_input[text_location] != ' ';
parser_location++)
{
if(parser_location < MAX_ARGSIZE-1)
{
tokens[args_num-1][parser_location] = text_input[text_location];
}
text_location++;
}
}
parse_tokens(tokens, args_num);
for(int i = 0; i < args_num; i++)
{
free(tokens[i]);
}
free(tokens);
}
void enter_console(void* console_cursor_location)
{
*(uint32_t*)console_cursor_location = 0;
parse_console(get_console_history(0));
}