]> icculus.org git repositories - btb/d2x.git/blob - main/console.c
implemented execution of console commands
[btb/d2x.git] / main / console.c
1 /* $Id: console.c,v 1.18 2003-11-26 12:39:00 btb Exp $ */
2 /*
3  *
4  * Code for controlling the console
5  *
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <conf.h>
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <string.h>
17 #ifndef _WIN32_WCE
18 #include <fcntl.h>
19 #endif
20 #include <ctype.h>
21
22 #include <SDL.h>
23 #ifdef CONSOLE
24 #include "CON_console.h"
25 #endif
26
27 #include "pstypes.h"
28 #include "u_mem.h"
29 #include "error.h"
30 #include "console.h"
31 #include "cmd.h"
32 #include "gr.h"
33 #include "gamefont.h"
34 #include "pcx.h"
35 #include "cfile.h"
36
37 #ifndef __MSDOS__
38 int text_console_enabled = 1;
39 #else
40 int isvga();
41 #define text_console_enabled (!isvga())
42 #endif
43
44 int Console_open = 0;
45 cvar_t *cvar_vars = NULL;
46
47 /* Console specific cvars */
48 /* How discriminating we are about which messages are displayed */
49 cvar_t con_threshold = {"con_threshold", "0",};
50
51 /* Private console stuff */
52 #define CON_NUM_LINES 40
53 #if 0
54 #define CON_LINE_LEN 40
55 static char con_display[40][40];
56 static int  con_line; /* Current display line */
57 #endif
58
59 #ifdef CONSOLE
60 ConsoleInformation *Console;
61
62 void con_parse(ConsoleInformation *console, char *command);
63 void con_hide();
64
65
66 /* Free the console */
67 void con_free(void)
68 {
69         CON_Free(Console);
70 }
71 #endif
72
73
74 /* Initialise the console */
75 void con_init(void)
76 {
77         grs_screen fake_screen;
78         grs_font   fake_font;
79
80         fake_screen.sc_w = 320;
81         fake_screen.sc_h = 200;
82         fake_font.ft_w = 5;
83         fake_font.ft_h = 5;
84
85         Console = CON_Init(&fake_font, &fake_screen, CON_NUM_LINES, 0, 0, 320, 200);
86
87         CON_SetExecuteFunction(Console, con_parse);
88         CON_SetHideFunction(Console, con_hide);
89
90
91         cmd_init();
92
93         /* Initialise the cvars */
94         cvar_registervariable (&con_threshold);
95
96         atexit(con_free);
97 }
98
99 #ifdef CONSOLE
100
101 #define CON_BG_HIRES (cfexist("scoresb.pcx")?"scoresb.pcx":"scores.pcx")
102 #define CON_BG_LORES (cfexist("scores.pcx")?"scores.pcx":"scoresb.pcx") // Mac datafiles only have scoresb.pcx
103 #define CON_BG ((SWIDTH>=640)?CON_BG_HIRES:CON_BG_LORES)
104
105 void con_background(char *filename)
106 {
107         int pcx_error;
108         grs_bitmap bmp;
109         ubyte pal[256*3];
110
111         gr_init_bitmap_data(&bmp);
112         pcx_error = pcx_read_bitmap(filename, &bmp, BM_LINEAR, pal);
113         Assert(pcx_error == PCX_ERROR_NONE);
114         gr_remap_bitmap_good(&bmp, pal, -1, -1);
115         CON_Background(Console, &bmp);
116         gr_free_bitmap_data(&bmp);
117 }
118
119
120 void con_init_gfx(void)
121 {
122         CON_Font(Console, SMALL_FONT, gr_getcolor(63, 63, 63), -1);
123         CON_Transfer(Console, grd_curscreen, 0, 0, SWIDTH, SHEIGHT / 2);
124
125         con_background(CON_BG);
126 }
127 #endif
128
129
130 void con_resize(void)
131 {
132 #ifdef CONSOLE
133         CON_Font(Console, SMALL_FONT, gr_getcolor(63, 63, 63), -1);
134         CON_Resize(Console, 0, 0, SWIDTH, SHEIGHT / 2);
135         con_background(CON_BG);
136 #endif
137 }
138
139 /* Print a message to the console */
140 void con_printf(int priority, char *fmt, ...)
141 {
142         va_list arglist;
143         char buffer[2048];
144
145         if (priority <= ((int)con_threshold.value))
146         {
147                 va_start (arglist, fmt);
148                 vsprintf (buffer,  fmt, arglist);
149                 va_end (arglist);
150
151 #ifdef CONSOLE
152                 CON_Out(Console, buffer);
153 #endif
154
155 /*              for (i=0; i<l; i+=CON_LINE_LEN,con_line++)
156                 {
157                         memcpy(con_display, &buffer[i], min(80, l-i));
158                 }*/
159
160                 if (text_console_enabled)
161                 {
162                         /* Produce a sanitised version and send it to the standard output */
163                         char *p1, *p2;
164
165                         p1 = p2 = buffer;
166                         do
167                                 switch (*p1)
168                                 {
169                                 case CC_COLOR:
170                                 case CC_LSPACING:
171                                         p1++;
172                                 case CC_UNDERLINE:
173                                         p1++;
174                                         break;
175                                 default:
176                                         *p2++ = *p1++;
177                                 }
178                         while (*p1);
179                         *p2 = 0;
180
181                         printf("%s", buffer);
182                 }
183         }
184 }
185
186 /* Check for new console input. If it's there, use it */
187 void con_update(void)
188 {
189 #if 0
190         char buffer[CMD_MAX_LENGTH], *t;
191
192         /* Check for new input */
193         t = fgets(buffer, sizeof(buffer), stdin);
194         if (t == NULL) return;
195
196         cmd_parse(buffer);
197 #endif
198         con_draw();
199 }
200
201
202 int con_events(int key)
203 {
204 #ifdef CONSOLE
205         return CON_Events(key);
206 #else
207         return key;
208 #endif
209 }
210
211
212 /* Register a CVar */
213 void cvar_registervariable (cvar_t *cvar)
214 {
215         cvar_t *ptr;
216
217         Assert(cvar != NULL);
218
219         cvar->next = NULL;
220         cvar->value = strtod(cvar->string, (char **) NULL);
221
222         if (cvar_vars == NULL)
223         {
224                 cvar_vars = cvar;
225         } else
226         {
227                 for (ptr = cvar_vars; ptr->next != NULL; ptr = ptr->next) ;
228                 ptr->next = cvar;
229         }
230 }
231
232 /* Set a CVar's value */
233 void cvar_set (char *cvar_name, char *value)
234 {
235         cvar_t *ptr;
236
237         for (ptr = cvar_vars; ptr != NULL; ptr = ptr->next)
238                 if (!strcmp(cvar_name, ptr->name)) break;
239
240         if (ptr == NULL) return; // If we didn't find the cvar, give up
241
242         ptr->value = strtod(value, (char **) NULL);
243 }
244
245 /* Get a CVar's value */
246 float cvar (char *cvar_name)
247 {
248         cvar_t *ptr;
249
250         for (ptr = cvar_vars; ptr != NULL; ptr = ptr->next)
251                 if (!strcmp(cvar_name, ptr->name)) break;
252
253         if (ptr == NULL) return 0.0; // If we didn't find the cvar, give up
254
255         return ptr->value;
256 }
257
258
259 /* Draw the console */
260 void con_draw(void)
261 {
262 #ifdef CONSOLE
263         CON_DrawConsole(Console);
264 #else
265 #if 0
266         char buffer[CON_LINE_LEN+1];
267         int i,j;
268         for (i = con_line, j=0; j < 20; i = (i+1) % CON_NUM_LINES, j++)
269         {
270                 memcpy(buffer, con_display[i], CON_LINE_LEN);
271                 buffer[CON_LINE_LEN] = 0;
272                 gr_string(1,j*10,buffer);
273         }
274 #endif
275 #endif
276 }
277
278 /* Show the console */
279 void con_show(void)
280 {
281         Console_open = 1;
282 #ifdef CONSOLE
283         CON_Show(Console);
284         CON_Topmost(Console);
285 #endif
286 }
287
288 void con_hide(void)
289 {
290         Console_open = 0;
291 }
292
293 #ifdef CONSOLE
294 void con_parse(ConsoleInformation *console, char *command)
295 {
296         cmd_parse(command);
297 }
298 #endif