]> icculus.org git repositories - btb/d2x.git/blob - main/console.c
set console background, fix whitespace
[btb/d2x.git] / main / console.c
1 /* $Id: console.c,v 1.13 2003-06-02 20:45:32 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
31 #ifndef __MSDOS__
32 int text_console_enabled = 1;
33 #else
34 int isvga();
35 #define text_console_enabled (!isvga())
36 #endif
37
38 cvar_t *cvar_vars = NULL;
39
40 /* Console specific cvars */
41 /* How discriminating we are about which messages are displayed */
42 cvar_t con_threshold = {"con_threshold", "0",};
43
44 /* Private console stuff */
45 #define CON_NUM_LINES 40
46 #define CON_LINE_LEN 40
47 #if 0
48 static char con_display[40][40];
49 static int  con_line; /* Current display line */
50 #endif
51
52 #ifdef CONSOLE
53 static int con_initialized;
54
55 ConsoleInformation *Console;
56 extern SDL_Surface *screen;
57
58 void con_parse(ConsoleInformation *console, char *command);
59 #endif
60
61
62 /* ======
63  * con_init - Initialise the console.
64  * ======
65  */
66 int con_init(void)
67 {
68         /* Initialise the cvars */
69         cvar_registervariable (&con_threshold);
70         return 0;
71 }
72
73 #ifdef CONSOLE
74 void real_con_init(void)
75 {
76         SDL_Rect Con_rect;
77
78         Con_rect.x = Con_rect.y = 0;
79         Con_rect.w = 320;
80         Con_rect.h = 200;
81
82         Console = CON_Init("ConsoleFont.png", screen, CON_NUM_LINES, Con_rect);
83
84         Assert(Console);
85
86         CON_SetExecuteFunction(Console, con_parse);
87         CON_Background(Console, "scores.pcx", 0, 0);
88
89         con_initialized = 1;
90 }
91 #endif
92
93 /* ======
94  * con_printf - Print a message to the console.
95  * ======
96  */
97 void con_printf(int priority, char *fmt, ...)
98 {
99         va_list arglist;
100         char buffer[2048];
101
102         if (priority <= ((int)con_threshold.value))
103         {
104                 va_start (arglist, fmt);
105                 vsprintf (buffer,  fmt, arglist);
106                 va_end (arglist);
107                 if (text_console_enabled) {
108                         va_start (arglist, fmt);
109                         vprintf(fmt, arglist);
110                         va_end (arglist);
111                 }
112
113 #ifdef CONSOLE
114                 CON_Out(Console, buffer);
115 #endif
116
117 /*              for (i=0; i<l; i+=CON_LINE_LEN,con_line++)
118                 {
119                         memcpy(con_display, &buffer[i], min(80, l-i));
120                 }*/
121         }
122 }
123
124 /* ======
125  * con_update - Check for new console input. If it's there, use it.
126  * ======
127  */
128 void con_update(void)
129 {
130 #if 0
131         char buffer[CMD_MAX_LENGTH], *t;
132
133         /* Check for new input */
134         t = fgets(buffer, sizeof(buffer), stdin);
135         if (t == NULL) return;
136
137         cmd_parse(buffer);
138 #endif
139         con_draw();
140 }
141
142 /* ======
143  * cvar_registervariable - Register a CVar
144  * ======
145  */
146 void cvar_registervariable (cvar_t *cvar)
147 {
148         cvar_t *ptr;
149
150         Assert(cvar != NULL);
151
152         cvar->next = NULL;
153         cvar->value = strtod(cvar->string, (char **) NULL);
154
155         if (cvar_vars == NULL)
156         {
157                 cvar_vars = cvar;
158         } else
159         {
160                 for (ptr = cvar_vars; ptr->next != NULL; ptr = ptr->next) ;
161                 ptr->next = cvar;
162         }
163 }
164
165 /* ======
166  * cvar_set - Set a CVar's value
167  * ======
168  */
169 void cvar_set (char *cvar_name, char *value)
170 {
171         cvar_t *ptr;
172
173         for (ptr = cvar_vars; ptr != NULL; ptr = ptr->next)
174                 if (!strcmp(cvar_name, ptr->name)) break;
175
176         if (ptr == NULL) return; // If we didn't find the cvar, give up
177
178         ptr->value = strtod(value, (char **) NULL);
179 }
180
181 /* ======
182  * cvar() - Get a CVar's value
183  * ======
184  */
185 float cvar (char *cvar_name)
186 {
187         cvar_t *ptr;
188
189         for (ptr = cvar_vars; ptr != NULL; ptr = ptr->next)
190                 if (!strcmp(cvar_name, ptr->name)) break;
191
192         if (ptr == NULL) return 0.0; // If we didn't find the cvar, give up
193
194         return ptr->value;
195 }
196
197
198 /* ==========================================================================
199  * DRAWING
200  * ==========================================================================
201  */
202 void con_draw(void)
203 {
204 #ifdef CONSOLE
205         CON_DrawConsole(Console);
206 #else
207 #if 0
208         char buffer[CON_LINE_LEN+1];
209         int i,j;
210         for (i = con_line, j=0; j < 20; i = (i+1) % CON_NUM_LINES, j++)
211         {
212                 memcpy(buffer, con_display[i], CON_LINE_LEN);
213                 buffer[CON_LINE_LEN] = 0;
214                 gr_string(1,j*10,buffer);
215         }
216 #endif
217 #endif
218 }
219
220 void con_show(void)
221 {
222 #ifdef CONSOLE
223         if (!con_initialized)
224                 real_con_init();
225
226         CON_Show(Console);
227         CON_Topmost(Console);
228 #endif
229 }
230
231 #ifdef CONSOLE
232 void con_parse(ConsoleInformation *console, char *command)
233 {
234         cmd_parse(command);
235 }
236 #endif