When launching a file from the finder, add the new file into the command line at...
[chocolate-doom.git] / textscreen / txt_io.c
blob1ecc7bd639b327deeef15ef0a30e58d3a73803d8
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 2005,2006 Simon Howard
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 // 02111-1307, USA.
21 //-----------------------------------------------------------------------------
23 // Text mode I/O functions, similar to C stdio
25 //-----------------------------------------------------------------------------
27 #include <stdlib.h>
28 #include <string.h>
30 #include "txt_io.h"
31 #include "txt_main.h"
33 static struct
35 txt_color_t color;
36 const char *name;
37 } colors[] = {
38 {TXT_COLOR_BLACK, "black"},
39 {TXT_COLOR_BLUE, "blue"},
40 {TXT_COLOR_GREEN, "green"},
41 {TXT_COLOR_CYAN, "cyan"},
42 {TXT_COLOR_RED, "red"},
43 {TXT_COLOR_MAGENTA, "magenta"},
44 {TXT_COLOR_BROWN, "brown"},
45 {TXT_COLOR_GREY, "grey"},
46 {TXT_COLOR_DARK_GREY, "darkgrey"},
47 {TXT_COLOR_BRIGHT_BLUE, "brightblue"},
48 {TXT_COLOR_BRIGHT_GREEN, "brightgreen"},
49 {TXT_COLOR_BRIGHT_CYAN, "brightcyan"},
50 {TXT_COLOR_BRIGHT_RED, "brightred"},
51 {TXT_COLOR_BRIGHT_MAGENTA, "brightmagenta"},
52 {TXT_COLOR_YELLOW, "yellow"},
53 {TXT_COLOR_BRIGHT_WHITE, "brightwhite"},
56 static int cur_x = 0, cur_y = 0;
57 static txt_color_t fgcolor = TXT_COLOR_GREY;
58 static txt_color_t bgcolor = TXT_COLOR_BLACK;
60 static int GetColorForName(char *s)
62 size_t i;
64 for (i=0; i<sizeof(colors) / sizeof(*colors); ++i)
66 if (!strcmp(s, colors[i].name))
68 return colors[i].color;
72 return -1;
75 static void NewLine(unsigned char *screendata)
77 int i;
78 unsigned char *p;
80 cur_x = 0;
81 ++cur_y;
83 if (cur_y >= TXT_SCREEN_H)
85 // Scroll the screen up
87 cur_y = TXT_SCREEN_H - 1;
89 memcpy(screendata, screendata + TXT_SCREEN_W * 2,
90 TXT_SCREEN_W * 2 * (TXT_SCREEN_H -1));
92 // Clear the bottom line
94 p = screendata + (TXT_SCREEN_H - 1) * 2 * TXT_SCREEN_W;
96 for (i=0; i<TXT_SCREEN_W; ++i)
98 *p++ = ' ';
99 *p++ = fgcolor | (bgcolor << 4);
104 static void PutChar(unsigned char *screendata, int c)
106 unsigned char *p;
108 p = screendata + cur_y * TXT_SCREEN_W * 2 + cur_x * 2;
110 switch (c)
112 case '\n':
113 NewLine(screendata);
114 break;
116 case '\b':
117 // backspace
118 --cur_x;
119 if (cur_x < 0)
120 cur_x = 0;
121 break;
123 default:
125 // Add a new character to the buffer
127 p[0] = c;
128 p[1] = fgcolor | (bgcolor << 4);
130 ++cur_x;
132 if (cur_x >= TXT_SCREEN_W)
134 NewLine(screendata);
137 break;
141 void TXT_PutChar(int c)
143 unsigned char *screen;
145 screen = TXT_GetScreenData();
147 PutChar(screen, c);
150 void TXT_Puts(const char *s)
152 int previous_color = TXT_COLOR_BLACK;
153 unsigned char *screen;
154 const char *p;
155 char colorname_buf[20];
156 char *ending;
157 int col;
159 screen = TXT_GetScreenData();
161 for (p=s; *p != '\0'; ++p)
163 if (*p == '<')
165 ++p;
167 if (*p == '<')
169 PutChar(screen, '<');
171 else
173 ending = strchr(p, '>');
175 if (ending == NULL)
177 return;
180 strncpy(colorname_buf, p, 19);
181 colorname_buf[ending-p] = '\0';
183 if (!strcmp(colorname_buf, "/"))
185 // End of color block
187 col = previous_color;
189 else
191 col = GetColorForName(colorname_buf);
193 if (col < 0)
195 return;
198 // Save the color for the ending marker
200 previous_color = fgcolor;
203 TXT_FGColor(col);
205 p = ending;
208 else
210 PutChar(screen, *p);
214 PutChar(screen, '\n');
217 void TXT_GotoXY(int x, int y)
219 cur_x = x;
220 cur_y = y;
223 void TXT_GetXY(int *x, int *y)
225 *x = cur_x;
226 *y = cur_y;
229 void TXT_FGColor(txt_color_t color)
231 fgcolor = color;
234 void TXT_BGColor(int color, int blinking)
236 bgcolor = color;
237 if (blinking)
238 bgcolor |= TXT_COLOR_BLINKING;
241 void TXT_ClearScreen(void)
243 unsigned char *screen;
244 int i;
246 screen = TXT_GetScreenData();
248 for (i=0; i<TXT_SCREEN_W * TXT_SCREEN_H; ++i)
250 screen[i * 2] = ' ';
251 screen[i * 2 + 1] = (bgcolor << 4) | fgcolor;
254 cur_x = 0;
255 cur_y = 0;