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