]> icculus.org git repositories - btb/d2x.git/blob - main/console.c
add callback for console hide. disable controls when open.
[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 /* ======
67  * con_free - Free the console.
68  * ======
69  */
70 void con_free(void)
71 {
72         CON_Free(Console);
73 }
74 #endif
75
76
77 /* ======
78  * con_init - Initialise the console.
79  * ======
80  */
81 int con_init(void)
82 {
83         grs_screen fake_screen;
84         grs_font   fake_font;
85
86         fake_screen.sc_w = 320;
87         fake_screen.sc_h = 200;
88         fake_font.ft_w = 5;
89         fake_font.ft_h = 5;
90
91         Console = CON_Init(&fake_font, &fake_screen, CON_NUM_LINES, 0, 0, 320, 200);
92
93         CON_SetExecuteFunction(Console, con_parse);
94         CON_SetHideFunction(Console, con_hide);
95
96         /* Initialise the cvars */
97         cvar_registervariable (&con_threshold);
98
99         atexit(con_free);
100
101         return 0;
102 }
103
104 #ifdef CONSOLE
105
106 #define CON_BG_HIRES (cfexist("scoresb.pcx")?"scoresb.pcx":"scores.pcx")
107 #define CON_BG_LORES (cfexist("scores.pcx")?"scores.pcx":"scoresb.pcx") // Mac datafiles only have scoresb.pcx
108 #define CON_BG ((SWIDTH>=640)?CON_BG_HIRES:CON_BG_LORES)
109
110 void con_background(char *filename)
111 {
112         int pcx_error;
113         grs_bitmap bmp;
114         ubyte pal[256*3];
115
116         gr_init_bitmap_data(&bmp);
117         pcx_error = pcx_read_bitmap(filename, &bmp, BM_LINEAR, pal);
118         Assert(pcx_error == PCX_ERROR_NONE);
119         gr_remap_bitmap_good(&bmp, pal, -1, -1);
120         CON_Background(Console, &bmp);
121         gr_free_bitmap_data(&bmp);
122 }
123
124
125 void con_init_gfx(void)
126 {
127         CON_Font(Console, SMALL_FONT, gr_getcolor(63, 63, 63), -1);
128         CON_Transfer(Console, grd_curscreen, 0, 0, SWIDTH, SHEIGHT / 2);
129
130         con_background(CON_BG);
131 }
132 #endif
133
134
135 void con_resize(void)
136 {
137 #ifdef CONSOLE
138         CON_Font(Console, SMALL_FONT, gr_getcolor(63, 63, 63), -1);
139         CON_Resize(Console, 0, 0, SWIDTH, SHEIGHT / 2);
140         con_background(CON_BG);
141 #endif
142 }
143
144 /* ======
145  * con_printf - Print a message to the console.
146  * ======
147  */
148 void con_printf(int priority, char *fmt, ...)
149 {
150         va_list arglist;
151         char buffer[2048];
152
153         if (priority <= ((int)con_threshold.value))
154         {
155                 va_start (arglist, fmt);
156                 vsprintf (buffer,  fmt, arglist);
157                 va_end (arglist);
158
159 #ifdef CONSOLE
160                 CON_Out(Console, buffer);
161 #endif
162
163 /*              for (i=0; i<l; i+=CON_LINE_LEN,con_line++)
164                 {
165                         memcpy(con_display, &buffer[i], min(80, l-i));
166                 }*/
167
168                 if (text_console_enabled)
169                 {
170                         /* Produce a sanitised version and send it to the console */
171                         char *p1, *p2;
172
173                         p1 = p2 = buffer;
174                         do
175                                 switch (*p1)
176                                 {
177                                 case CC_COLOR:
178                                 case CC_LSPACING:
179                                         p1++;
180                                 case CC_UNDERLINE:
181                                         p1++;
182                                         break;
183                                 default:
184                                         *p2++ = *p1++;
185                                 }
186                         while (*p1);
187                         *p2 = 0;
188
189                         printf("%s", buffer);
190                 }
191         }
192 }
193
194 /* ======
195  * con_update - Check for new console input. If it's there, use it.
196  * ======
197  */
198 void con_update(void)
199 {
200 #if 0
201         char buffer[CMD_MAX_LENGTH], *t;
202
203         /* Check for new input */
204         t = fgets(buffer, sizeof(buffer), stdin);
205         if (t == NULL) return;
206
207         cmd_parse(buffer);
208 #endif
209         con_draw();
210 }
211
212
213 int con_events(int key)
214 {
215 #ifdef CONSOLE
216         return CON_Events(key);
217 #else
218         return key;
219 #endif
220 }
221
222
223 /* ======
224  * cvar_registervariable - Register a CVar
225  * ======
226  */
227 void cvar_registervariable (cvar_t *cvar)
228 {
229         cvar_t *ptr;
230
231         Assert(cvar != NULL);
232
233         cvar->next = NULL;
234         cvar->value = strtod(cvar->string, (char **) NULL);
235
236         if (cvar_vars == NULL)
237         {
238                 cvar_vars = cvar;
239         } else
240         {
241                 for (ptr = cvar_vars; ptr->next != NULL; ptr = ptr->next) ;
242                 ptr->next = cvar;
243         }
244 }
245
246 /* ======
247  * cvar_set - Set a CVar's value
248  * ======
249  */
250 void cvar_set (char *cvar_name, char *value)
251 {
252         cvar_t *ptr;
253
254         for (ptr = cvar_vars; ptr != NULL; ptr = ptr->next)
255                 if (!strcmp(cvar_name, ptr->name)) break;
256
257         if (ptr == NULL) return; // If we didn't find the cvar, give up
258
259         ptr->value = strtod(value, (char **) NULL);
260 }
261
262 /* ======
263  * cvar() - Get a CVar's value
264  * ======
265  */
266 float cvar (char *cvar_name)
267 {
268         cvar_t *ptr;
269
270         for (ptr = cvar_vars; ptr != NULL; ptr = ptr->next)
271                 if (!strcmp(cvar_name, ptr->name)) break;
272
273         if (ptr == NULL) return 0.0; // If we didn't find the cvar, give up
274
275         return ptr->value;
276 }
277
278
279 /* ==========================================================================
280  * DRAWING
281  * ==========================================================================
282  */
283 void con_draw(void)
284 {
285 #ifdef CONSOLE
286         CON_DrawConsole(Console);
287 #else
288 #if 0
289         char buffer[CON_LINE_LEN+1];
290         int i,j;
291         for (i = con_line, j=0; j < 20; i = (i+1) % CON_NUM_LINES, j++)
292         {
293                 memcpy(buffer, con_display[i], CON_LINE_LEN);
294                 buffer[CON_LINE_LEN] = 0;
295                 gr_string(1,j*10,buffer);
296         }
297 #endif
298 #endif
299 }
300
301 void con_show(void)
302 {
303         Console_open = 1;
304 #ifdef CONSOLE
305         CON_Show(Console);
306         CON_Topmost(Console);
307 #endif
308 }
309
310 void con_hide(void)
311 {
312         Console_open = 0;
313 }
314
315 #ifdef CONSOLE
316 void con_parse(ConsoleInformation *console, char *command)
317 {
318         cmd_parse(command);
319 }
320 #endif