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