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