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