]> icculus.org git repositories - btb/d2x.git/blob - main/console.c
fix mem leak
[btb/d2x.git] / main / console.c
1 /* $Id: console.c,v 1.9 2003-03-17 09:33:49 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 #include "pstypes.h"
19 #include "u_mem.h"
20 #include "error.h"
21 #include "console.h"
22 #include "cmd.h"
23 #include "gr.h"
24
25 #ifndef __MSDOS__
26 int text_console_enabled = 1;
27 #else
28 int isvga();
29 #define text_console_enabled (!isvga())
30 #endif
31
32 cvar_t *cvar_vars = NULL;
33
34 /* Console specific cvars */
35 /* How discriminating we are about which messages are displayed */
36 cvar_t con_threshold = {"con_threshold", "0",};
37
38 /* Private console stuff */
39 #define CON_NUM_LINES 40
40 #define CON_LINE_LEN 40
41 static char con_display[40][40];
42 static int  con_line; /* Current display line */
43
44 /* ======
45  * con_init - Initialise the console.
46  * ======
47  */
48 int con_init(void)
49 {
50         /* Make sure the output is unbuffered */
51         if (text_console_enabled) {
52                 setbuf (stdout, NULL);
53                 setbuf (stderr, NULL);
54         }
55
56         memset(con_display, ' ', sizeof(con_display));
57         con_line = 0;
58
59         /* Initialise the cvars */
60         cvar_registervariable (&con_threshold);
61         return 0;
62 }
63
64 /* ======
65  * con_printf - Print a message to the console.
66  * ======
67  */
68 void con_printf(int priority, char *fmt, ...)
69 {
70         va_list arglist;
71         char buffer[2048];
72
73         if (priority <= ((int)con_threshold.value))
74         {
75                 va_start (arglist, fmt);
76                 vsprintf (buffer,  fmt, arglist);
77                 va_end (arglist);
78                 if (text_console_enabled) {
79                         va_start (arglist, fmt);
80                         vprintf(fmt, arglist);
81                         va_end (arglist);
82                 }
83
84 /*              for (i=0; i<l; i+=CON_LINE_LEN,con_line++)
85                 {
86                         memcpy(con_display, &buffer[i], min(80, l-i));
87                 }*/
88         }
89 }
90
91 /* ======
92  * con_update - Check for new console input. If it's there, use it.
93  * ======
94  */
95 void con_update(void)
96 {
97         char buffer[CMD_MAX_LENGTH], *t;
98
99         /* Check for new input */
100         t = fgets(buffer, sizeof(buffer), stdin);
101         if (t == NULL) return;
102
103         cmd_parse(buffer);
104         con_draw();
105 }
106
107 /* ======
108  * cvar_registervariable - Register a CVar
109  * ======
110  */
111 void cvar_registervariable (cvar_t *cvar)
112 {
113         cvar_t *ptr;
114
115         Assert(cvar != NULL);
116
117         cvar->next = NULL;
118         cvar->value = strtod(cvar->string, (char **) NULL);
119
120         if (cvar_vars == NULL)
121         {
122                 cvar_vars = cvar;
123         } else
124         {
125                 for (ptr = cvar_vars; ptr->next != NULL; ptr = ptr->next) ;
126                 ptr->next = cvar;
127         }
128 }
129
130 /* ======
131  * cvar_set - Set a CVar's value
132  * ======
133  */
134 void cvar_set (char *cvar_name, char *value)
135 {
136         cvar_t *ptr;
137
138         for (ptr = cvar_vars; ptr != NULL; ptr = ptr->next)
139                 if (!strcmp(cvar_name, ptr->name)) break;
140
141         if (ptr == NULL) return; // If we didn't find the cvar, give up
142
143         ptr->value = strtod(value, (char **) NULL);
144 }
145
146 /* ======
147  * cvar() - Get a CVar's value
148  * ======
149  */
150 float cvar (char *cvar_name)
151 {
152         cvar_t *ptr;
153
154         for (ptr = cvar_vars; ptr != NULL; ptr = ptr->next)
155                 if (!strcmp(cvar_name, ptr->name)) break;
156
157         if (ptr == NULL) return 0.0; // If we didn't find the cvar, give up
158
159         return ptr->value;
160 }
161
162
163 /* ==========================================================================
164  * DRAWING
165  * ==========================================================================
166  */
167 void con_draw(void)
168 {
169         char buffer[CON_LINE_LEN+1];
170         int i,j;
171         for (i = con_line, j=0; j < 20; i = (i+1) % CON_NUM_LINES, j++)
172         {
173                 memcpy(buffer, con_display[i], CON_LINE_LEN);
174                 buffer[CON_LINE_LEN] = 0;
175                 gr_string(1,j*10,buffer);
176         }
177 }