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