summaryrefslogtreecommitdiff
path: root/textscreen/txt_io.c
blob: e0fdae43a83bbfb5aefe175465da6ae42e9afb8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// $Id: txt_io.c 292 2006-01-14 00:10:54Z fraggle $
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 2005 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// $Log$
// Revision 1.2  2006/01/14 00:10:53  fraggle
// Change the format of color commands.  Reorganise the waiting dialog.
//
// Revision 1.1  2006/01/13 23:56:00  fraggle
// Add text-mode I/O functions.
// Use text-mode screen for the waiting screen.
//
//
//-----------------------------------------------------------------------------
//
// Text mode I/O functions, similar to C stdio
//
//-----------------------------------------------------------------------------

#include <stdlib.h>
#include <string.h>

#include "txt_io.h"
#include "txt_main.h"

static struct 
{
    txt_color_t color;
    char *name;
} colors[] = {
    {TXT_COLOR_BLACK,           "black"},
    {TXT_COLOR_BLUE,            "blue"},
    {TXT_COLOR_GREEN,           "green"},
    {TXT_COLOR_CYAN,            "cyan"},
    {TXT_COLOR_RED,             "red"},
    {TXT_COLOR_MAGENTA,         "magenta"},
    {TXT_COLOR_BROWN,           "brown"},
    {TXT_COLOR_GREY,            "grey"},
    {TXT_COLOR_DARK_GREY,       "darkgrey"},
    {TXT_COLOR_BRIGHT_BLUE,     "brightblue"},
    {TXT_COLOR_BRIGHT_GREEN,    "brightgreen"},
    {TXT_COLOR_BRIGHT_CYAN,     "brightcyan"},
    {TXT_COLOR_BRIGHT_RED,      "brightred"},
    {TXT_COLOR_BRIGHT_MAGENTA,  "brightmagenta"},
    {TXT_COLOR_YELLOW,          "yellow"},
    {TXT_COLOR_BRIGHT_WHITE,    "brightwhite"},
};

static int cur_x = 0, cur_y = 0;
static txt_color_t fgcolor = TXT_COLOR_GREY;
static txt_color_t bgcolor = TXT_COLOR_BLACK;

static int GetColorForName(char *s)
{
    int i;

    for (i=0; i<sizeof(colors) / sizeof(*colors); ++i)
    {
        if (!strcmp(s, colors[i].name))
        {
            return colors[i].color;
        }
    }

    return -1;
}

static void NewLine(unsigned char *screendata)
{
    int i;
    unsigned char *p;

    cur_x = 0;
    ++cur_y;

    if (cur_y >= TXT_SCREEN_H)
    {
        // Scroll the screen up

        cur_y = TXT_SCREEN_H - 1;

        memcpy(screendata, screendata + TXT_SCREEN_W * 2,
               TXT_SCREEN_W * 2 * (TXT_SCREEN_H -1));

        // Clear the bottom line

        p = screendata + (TXT_SCREEN_H - 1) * 2 * TXT_SCREEN_W;

        for (i=0; i<TXT_SCREEN_W; ++i) 
        {
            *p++ = ' ';
            *p++ = fgcolor | (bgcolor << 4);
        }
    }
}

static void PutChar(unsigned char *screendata, int c)
{
    unsigned char *p;
   
    p = screendata + cur_y * TXT_SCREEN_W * 2 +  cur_x * 2;

    switch (c)
    {
        case '\n':
            NewLine(screendata);
            break;

        case '\b':
            // backspace
            --cur_x;
            if (cur_x < 0)
                cur_x = 0;
            break;

        default:

            // Add a new character to the buffer

            p[0] = c;
            p[1] = fgcolor | (bgcolor << 4);

            ++cur_x;

            if (cur_x >= TXT_SCREEN_W) 
            {
                NewLine(screendata);
            }

            break;
    }
}

void TXT_PutChar(int c)
{
    unsigned char *screen;

    screen = TXT_GetScreenData();

    PutChar(screen, c);
}

void TXT_Puts(char *s)
{
    int previous_color = TXT_COLOR_BLACK;
    unsigned char *screen;
    char *p;
    char colorname_buf[20];
    char *ending;
    int col;

    screen = TXT_GetScreenData();

    for (p=s; *p != '\0'; ++p)
    {
        if (*p == '<')
        {
            ++p;

            if (*p == '<')
            {
                PutChar(screen, '<');
            }
            else
            {
                ending = strchr(p, '>');

                if (ending == NULL)
                {
                    return;
                }

                strncpy(colorname_buf, p, 19);
                colorname_buf[ending-p] = '\0';

                if (!strcmp(colorname_buf, "/"))
                {
                    // End of color block

                    col = previous_color;
                }
                else
                {
                    col = GetColorForName(colorname_buf);

                    if (col < 0)
                    {
                        return;
                    }

                    // Save the color for the ending marker

                    previous_color = fgcolor;
                }

                TXT_FGColor(col);
                
                p = ending;
            }
        }
        else
        {
            PutChar(screen, *p);
        }
    }

    PutChar(screen, '\n');
}

void TXT_GotoXY(int x, int y)
{
    cur_x = x;
    cur_y = y;
}

void TXT_FGColor(txt_color_t color)
{
    fgcolor = color;
}

void TXT_BGColor(int color, int blinking)
{
    bgcolor = color;
    if (blinking)
        bgcolor |= TXT_COLOR_BLINKING;
}

void TXT_ClearScreen(void)
{
    unsigned char *screen;
    int i;

    screen = TXT_GetScreenData();

    for (i=0; i<TXT_SCREEN_W * TXT_SCREEN_H; ++i)
    {
        screen[i * 2] = ' ';
        screen[i * 2 +  1] = (bgcolor << 4) | fgcolor;
    }

    cur_x = 0;
    cur_y = 0;
}