1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
4 // Copyright(C) 2005,2006 Simon Howard
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
21 //-----------------------------------------------------------------------------
23 // Text mode I/O functions, similar to C stdio
25 //-----------------------------------------------------------------------------
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
)
64 for (i
=0; i
<sizeof(colors
) / sizeof(*colors
); ++i
)
66 if (!strcmp(s
, colors
[i
].name
))
68 return colors
[i
].color
;
75 static void NewLine(unsigned char *screendata
)
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
)
99 *p
++ = fgcolor
| (bgcolor
<< 4);
104 static void PutChar(unsigned char *screendata
, int c
)
108 p
= screendata
+ cur_y
* TXT_SCREEN_W
* 2 + cur_x
* 2;
125 // Add a new character to the buffer
128 p
[1] = fgcolor
| (bgcolor
<< 4);
132 if (cur_x
>= TXT_SCREEN_W
)
141 void TXT_PutChar(int c
)
143 unsigned char *screen
;
145 screen
= TXT_GetScreenData();
150 void TXT_Puts(const char *s
)
152 int previous_color
= TXT_COLOR_BLACK
;
153 unsigned char *screen
;
155 char colorname_buf
[20];
159 screen
= TXT_GetScreenData();
161 for (p
=s
; *p
!= '\0'; ++p
)
169 PutChar(screen
, '<');
173 ending
= strchr(p
, '>');
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
;
191 col
= GetColorForName(colorname_buf
);
198 // Save the color for the ending marker
200 previous_color
= fgcolor
;
214 PutChar(screen
, '\n');
217 void TXT_GotoXY(int x
, int y
)
223 void TXT_GetXY(int *x
, int *y
)
229 void TXT_FGColor(txt_color_t color
)
234 void TXT_BGColor(int color
, int blinking
)
238 bgcolor
|= TXT_COLOR_BLINKING
;
241 void TXT_ClearScreen(void)
243 unsigned char *screen
;
246 screen
= TXT_GetScreenData();
248 for (i
=0; i
<TXT_SCREEN_W
* TXT_SCREEN_H
; ++i
)
251 screen
[i
* 2 + 1] = (bgcolor
<< 4) | fgcolor
;