]> icculus.org git repositories - btb/d2x.git/blob - main/console.c
move cvar to its own module
[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 "cvar.h"
33 #include "gr.h"
34 #include "gamefont.h"
35 #include "pcx.h"
36 #include "cfile.h"
37
38 #ifndef __MSDOS__
39 int text_console_enabled = 1;
40 #else
41 int isvga();
42 #define text_console_enabled (!isvga())
43 #endif
44
45 int Console_open = 0;
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 static int con_initialized;
61
62 ConsoleInformation *Console;
63
64 void con_parse(ConsoleInformation *console, char *command);
65 void con_hide();
66
67
68 /* Free the console */
69 void con_free(void)
70 {
71         if (con_initialized)
72                 CON_Free(Console);
73         con_initialized = 0;
74 }
75 #endif
76
77
78 /* Initialise the console */
79 void con_init(void)
80 {
81         grs_screen fake_screen;
82         grs_font   fake_font;
83
84         fake_screen.sc_w = 320;
85         fake_screen.sc_h = 200;
86         fake_font.ft_w = 5;
87         fake_font.ft_h = 5;
88
89         Console = CON_Init(&fake_font, &fake_screen, CON_NUM_LINES, 0, 0, 320, 200);
90
91         CON_SetExecuteFunction(Console, con_parse);
92         CON_SetHideFunction(Console, con_hide);
93
94
95         cmd_init();
96
97         /* Initialise the cvars */
98         cvar_registervariable (&con_threshold);
99
100         con_initialized = 1;
101
102         atexit(con_free);
103 }
104
105 #ifdef CONSOLE
106
107 #define CON_BG_HIRES (cfexist("scoresb.pcx")?"scoresb.pcx":"scores.pcx")
108 #define CON_BG_LORES (cfexist("scores.pcx")?"scores.pcx":"scoresb.pcx") // Mac datafiles only have scoresb.pcx
109 #define CON_BG ((SWIDTH>=640)?CON_BG_HIRES:CON_BG_LORES)
110
111 void con_background(char *filename)
112 {
113         int pcx_error;
114         grs_bitmap bmp;
115         ubyte pal[256*3];
116
117         gr_init_bitmap_data(&bmp);
118         pcx_error = pcx_read_bitmap(filename, &bmp, BM_LINEAR, pal);
119         Assert(pcx_error == PCX_ERROR_NONE);
120         gr_remap_bitmap_good(&bmp, pal, -1, -1);
121         CON_Background(Console, &bmp);
122         gr_free_bitmap_data(&bmp);
123 }
124
125
126 void con_init_gfx(void)
127 {
128         CON_Font(Console, SMALL_FONT, gr_getcolor(63, 63, 63), -1);
129         CON_Transfer(Console, grd_curscreen, 0, 0, SWIDTH, SHEIGHT / 2);
130
131         con_background(CON_BG);
132 }
133 #endif
134
135
136 void con_resize(void)
137 {
138 #ifdef CONSOLE
139         CON_Font(Console, SMALL_FONT, gr_getcolor(63, 63, 63), -1);
140         CON_Resize(Console, 0, 0, SWIDTH, SHEIGHT / 2);
141         con_background(CON_BG);
142 #endif
143 }
144
145 /* Print a message to the console */
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                 if (con_initialized)
159                         CON_Out(Console, buffer);
160 #endif
161
162 /*              for (i=0; i<l; i+=CON_LINE_LEN,con_line++)
163                 {
164                         memcpy(con_display, &buffer[i], min(80, l-i));
165                 }*/
166
167                 if (text_console_enabled)
168                 {
169                         /* Produce a sanitised version and send it to the standard output */
170                         char *p1, *p2;
171
172                         p1 = p2 = buffer;
173                         do
174                                 switch (*p1)
175                                 {
176                                 case CC_COLOR:
177                                 case CC_LSPACING:
178                                         p1++;
179                                 case CC_UNDERLINE:
180                                         p1++;
181                                         break;
182                                 default:
183                                         *p2++ = *p1++;
184                                 }
185                         while (*p1);
186                         *p2 = 0;
187
188                         printf("%s", buffer);
189                 }
190         }
191 }
192
193 /* Check for new console input. If it's there, use it */
194 void con_update(void)
195 {
196 #if 0
197         char buffer[CMD_MAX_LENGTH], *t;
198
199         /* Check for new input */
200         t = fgets(buffer, sizeof(buffer), stdin);
201         if (t == NULL) return;
202
203         cmd_parse(buffer);
204 #endif
205         con_draw();
206 }
207
208
209 int con_events(int key)
210 {
211 #ifdef CONSOLE
212         return CON_Events(key);
213 #else
214         return key;
215 #endif
216 }
217
218
219 /* Draw the console */
220 void con_draw(void)
221 {
222 #ifdef CONSOLE
223         CON_DrawConsole(Console);
224 #else
225 #if 0
226         char buffer[CON_LINE_LEN+1];
227         int i,j;
228         for (i = con_line, j=0; j < 20; i = (i+1) % CON_NUM_LINES, j++)
229         {
230                 memcpy(buffer, con_display[i], CON_LINE_LEN);
231                 buffer[CON_LINE_LEN] = 0;
232                 gr_string(1,j*10,buffer);
233         }
234 #endif
235 #endif
236 }
237
238 /* Show the console */
239 void con_show(void)
240 {
241         Console_open = 1;
242 #ifdef CONSOLE
243         CON_Show(Console);
244         CON_Topmost(Console);
245 #endif
246 }
247
248 void con_hide(void)
249 {
250         Console_open = 0;
251 }
252
253 #ifdef CONSOLE
254 void con_parse(ConsoleInformation *console, char *command)
255 {
256         cmd_parse(command);
257 }
258 #endif