]> icculus.org git repositories - btb/d2x.git/blob - main/console.c
d2faa476dd437470d77a0ad6d7f8285202ea8d4e
[btb/d2x.git] / main / console.c
1 /* $Id: console.c,v 1.12 2003-06-02 06:15:41 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 = Con_rect.h = 300;
80
81         Console = CON_Init("ConsoleFont.png", screen, CON_NUM_LINES, Con_rect);
82
83         Assert(Console);
84
85         CON_SetExecuteFunction(Console, con_parse);
86
87         con_initialized = 1;
88 }
89 #endif
90
91 /* ======
92  * con_printf - Print a message to the console.
93  * ======
94  */
95 void con_printf(int priority, char *fmt, ...)
96 {
97         va_list arglist;
98         char buffer[2048];
99
100         if (priority <= ((int)con_threshold.value))
101         {
102                 va_start (arglist, fmt);
103                 vsprintf (buffer,  fmt, arglist);
104                 va_end (arglist);
105                 if (text_console_enabled) {
106                         va_start (arglist, fmt);
107                         vprintf(fmt, arglist);
108                         va_end (arglist);
109                 }
110
111 #ifdef CONSOLE
112                 CON_Out(Console, buffer);
113 #endif
114
115 /*              for (i=0; i<l; i+=CON_LINE_LEN,con_line++)
116                 {
117                         memcpy(con_display, &buffer[i], min(80, l-i));
118                 }*/
119         }
120 }
121
122 /* ======
123  * con_update - Check for new console input. If it's there, use it.
124  * ======
125  */
126 void con_update(void)
127 {
128 #if 0
129         char buffer[CMD_MAX_LENGTH], *t;
130
131         /* Check for new input */
132         t = fgets(buffer, sizeof(buffer), stdin);
133         if (t == NULL) return;
134
135         cmd_parse(buffer);
136 #endif
137         con_draw();
138 }
139
140 /* ======
141  * cvar_registervariable - Register a CVar
142  * ======
143  */
144 void cvar_registervariable (cvar_t *cvar)
145 {
146         cvar_t *ptr;
147
148         Assert(cvar != NULL);
149
150         cvar->next = NULL;
151         cvar->value = strtod(cvar->string, (char **) NULL);
152
153         if (cvar_vars == NULL)
154         {
155                 cvar_vars = cvar;
156         } else
157         {
158                 for (ptr = cvar_vars; ptr->next != NULL; ptr = ptr->next) ;
159                 ptr->next = cvar;
160         }
161 }
162
163 /* ======
164  * cvar_set - Set a CVar's value
165  * ======
166  */
167 void cvar_set (char *cvar_name, char *value)
168 {
169         cvar_t *ptr;
170
171         for (ptr = cvar_vars; ptr != NULL; ptr = ptr->next)
172                 if (!strcmp(cvar_name, ptr->name)) break;
173
174         if (ptr == NULL) return; // If we didn't find the cvar, give up
175
176         ptr->value = strtod(value, (char **) NULL);
177 }
178
179 /* ======
180  * cvar() - Get a CVar's value
181  * ======
182  */
183 float cvar (char *cvar_name)
184 {
185         cvar_t *ptr;
186
187         for (ptr = cvar_vars; ptr != NULL; ptr = ptr->next)
188                 if (!strcmp(cvar_name, ptr->name)) break;
189
190         if (ptr == NULL) return 0.0; // If we didn't find the cvar, give up
191
192         return ptr->value;
193 }
194
195
196 /* ==========================================================================
197  * DRAWING
198  * ==========================================================================
199  */
200 void con_draw(void)
201 {
202 #ifdef CONSOLE
203         CON_DrawConsole(Console);
204 #else
205 #if 0
206         char buffer[CON_LINE_LEN+1];
207         int i,j;
208         for (i = con_line, j=0; j < 20; i = (i+1) % CON_NUM_LINES, j++)
209         {
210                 memcpy(buffer, con_display[i], CON_LINE_LEN);
211                 buffer[CON_LINE_LEN] = 0;
212                 gr_string(1,j*10,buffer);
213         }
214 #endif
215 #endif
216 }
217
218 void con_show(void)
219 {
220 #ifdef CONSOLE
221         if (!con_initialized)
222                 real_con_init();
223
224         CON_Show(Console);
225         CON_Topmost(Console);
226 #endif
227 }
228
229 #ifdef CONSOLE
230 void con_parse(ConsoleInformation *console, char *command)
231 {
232         cmd_parse(command);
233 }
234 #endif