aboutsummaryrefslogtreecommitdiffstats
path: root/graphics.c
blob: 155d628dcf5455541c9045ca8b8a923a6c693c65 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * graphics.c
 *
 * Graphics function emulation
 * This pretty much attempts to emulate the QuickBasic-style text screen
 * in 25x40 or 25x80 mode with no scrolling...
 */

#include <stdarg.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "SDL.h"
#include "grv.h"
#include "graphics.h"

#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))

#define TS_WIDTH  80
#define TS_HEIGHT 25

#define FONT_XSIZE 8
#define FONT_YSIZE 14

#define PX_WIDTH  (TS_WIDTH*FONT_XSIZE)
#define PX_HEIGHT (TS_HEIGHT*FONT_YSIZE)

typedef uint8_t font_t;
extern font_t grv_font[256][FONT_YSIZE];

#define NCOLORS 16

static uint32_t colors[NCOLORS];

static struct rgba { uint8_t a, r, g, b; } rgbcolors[NCOLORS] = {
  {0x00,0x00,0x00,0x00},	/* black */
  {0x00,0x00,0x00,0xaa},	/* dark blue */
  {0x00,0x00,0xaa,0x00},	/* dark green */
  {0x00,0x00,0xaa,0xaa},	/* dark cyan */
  {0x00,0xaa,0x00,0x00},	/* dark red */
  {0x00,0xaa,0x00,0xaa},	/* dark purple */
  {0x00,0xaa,0x55,0x00},	/* brown */
  {0x00,0xaa,0xaa,0xaa},	/* light grey */
  {0x00,0x55,0x55,0x55},	/* dark grey */
  {0x00,0x55,0x55,0xff},	/* light blue */
  {0x00,0x55,0xff,0x55},	/* light green */
  {0x00,0x55,0xff,0xff},	/* light cyan */
  {0x00,0xff,0x55,0x55},	/* light red */
  {0x00,0xff,0x55,0xff},	/* light purple */
  {0x00,0xff,0xff,0x55},	/* yellow */
  {0x00,0xff,0xff,0xff},	/* white */
};

struct screen_char {
  uint8_t sym;			/* Symbol code */
  uint8_t attr;			/* Attribute code */
};

static struct screen_char screendata[TS_HEIGHT][TS_WIDTH];

static struct current_screen {
  int x;
  int y;
  uint8_t attr;
  uint8_t dwidth;		/* Double width characters, must be 0 or 1 */
} current = {0, 0, 7, 1};	/* Current screen parameters */

static SDL_Surface *rscreen;
static volatile uint8_t blink_mask = 0;

/*
 * Set the attribute: fg, bg
 * For compatiblity's sake the flashing attribute is in fg, not bg
 */
void color(int fg, int bg)
{
  current.attr = (fg & 15) | ((bg & 7) << 4) | ((fg & 16) << 3);
}

/*
 * Set the cursor position (1-based for compatiblity)
 */
void locate(int r, int c)
{
  current.y = r-1;
  current.x = (c-1) << current.dwidth;
}

/*
 * Get the symbol at a specific screen coordinate
 */
int screen(int r, int c)
{
  return screendata[r-1][(c-1) << current.dwidth].sym;
}

/*
 * Get the attribute at a specific screen coordinate
 */
int screen1(int r, int c)
{
  return screendata[r-1][(c-1) << current.dwidth].attr;
}

/*
 * Prepare screen for modification
 */
static void lock_screen(void)
{
  SDL_LockSurface(rscreen);
}

/*
 * Update the on-screen structure to match the screendata[]
 * for character (tx,ty), but don't refresh the rectangle just
 * yet...
 *
 * tx,ty is zero-based and must be adjusted for doublewidth mode
 */
static void put_screen(int tx, int ty)
{
  font_t *fontp;
  font_t v;
  uint32_t *pixelp, fgp, bgp;
  int x, y, z;
  int attr;
  int pxwid = current.dwidth + 1;
  int bmask = blink_mask;
  
  attr  = screendata[ty][tx].attr;
  fontp = grv_font[screendata[ty][tx].sym];

  bgp = colors[(attr >> 4) & 7];
  fgp = (bmask & attr) ? bgp : colors[attr & 15];
  
  pixelp = ((uint32_t *) rscreen->pixels) +
    ty*(FONT_XSIZE*FONT_YSIZE*TS_WIDTH) +
    tx*FONT_XSIZE;
  
  for ( y = 0 ; y < FONT_YSIZE ; y++ ) {
    v = fontp[y];
    for ( x = 0 ; x < FONT_XSIZE ; x++ ) {
      for ( z = 0 ; z < pxwid ; z++ )
	*pixelp++ = v & ((font_t)1 << (FONT_XSIZE-1)) ? fgp : bgp;
      v <<= 1;
    }
    pixelp += PX_WIDTH-pxwid*FONT_XSIZE;
  }
}

/*
 * This routine switches the blink status, then goes around the screen
 * and updates all characters which has the blink attribute set.
 */
void update_blink(void)
{
  int x, y, xs;
  int gx, gy, gw, gh;
  SDL_Rect rects[TS_WIDTH*TS_HEIGHT]; /* Absolute maximum needed */
  SDL_Rect *rect = rects-1;
  int nrects = 0;

  blink_mask ^= 0x80;

  xs = current.dwidth+1;

  SDL_LockSurface(rscreen);

  gw = FONT_XSIZE << current.dwidth;
  gh = FONT_YSIZE;

  for ( y = 0, gy = 0 ; y < TS_HEIGHT ; y++, gy += gh ) {
    for ( x = 0, gx = 0 ; x < TS_WIDTH ; x += xs, gx += gw ) {
      if ( screendata[y][x].attr & 0x80 ) {
	put_screen(x,y);
	if ( !nrects || rect->y != gy || rect->x+rect->w != gx ) {
	  nrects++;
	  rect++;
	  rect->x = gx; rect->y = gy;
	  rect->w = gw; rect->h = gh;
	} else {
	  rect->w += gw;
	}
      }
    }
  }

  SDL_UnlockSurface(rscreen);

  if ( nrects )
    SDL_UpdateRects(rscreen, nrects, rects);
}

/*
 * Refresh rectangle and unlock screen
 * Coordinates are inclusive and must be adjusted for double-pixel mode
 */
static void update_screen(int x0, int y0, int x1, int y1)
{
  SDL_UnlockSurface(rscreen);

  SDL_UpdateRect(rscreen, x0*FONT_XSIZE, y0*FONT_YSIZE,
		 (x1-x0+1)*FONT_XSIZE, (y1-y0+1)*FONT_YSIZE);
}

/*
 * Display a string of characters and advance cursor
 */
void print(const char *str)
{
  const unsigned char *p = (const unsigned char *)str;
  unsigned char ch;
  int x = current.x;
  int y = current.y;
  uint8_t attr = current.attr;
  int x0 = x; int y0 = y;
  int x1 = x; int y1 = y;
  int xwid;

  if ( !*str )
    return;

  xwid = current.dwidth+1;

  lock_screen();

  while ( (ch = *p++) ) {
    screendata[y][x].sym  = ch;
    screendata[y][x].attr = attr;
    put_screen(x,y);
    x0 = min(x0,x); y0 = min(y0,y);
    x1 = max(x1,x+xwid-1); y1 = max(y1,y);
    x += xwid;
    if ( x >= TS_WIDTH ) {
      x = 0; y++;
      if ( y >= TS_HEIGHT ) {
	y = 0;
      }
    }
  }
  update_screen(x0,y0,x1,y1);

  current.x = x;  current.y = y;
}

/*
 * Same except with printf-style formatting
 */
void gprintf(const char *fmt, ...)
{
  unsigned char buffer[4096];
  va_list ap;

  va_start(ap, fmt);
  vsprintf(buffer, fmt, ap);
  va_end(ap);
  print(buffer);
}

/*
 * locate() and print() as a combined function
 */
void lprint(int r, int c, const char *str)
{
  locate(r,c);
  print(str);
}

/*
 * locate() and gprintf() as a combined function
 */
void lprintf(int r, int c, const char *fmt, ...)
{
  unsigned char buffer[4096];
  va_list ap;

  locate(r,c);
  va_start(ap, fmt);
  vsprintf(buffer, fmt, ap);
  va_end(ap);
  print(buffer);
}

/*
 * Set 40/80 character mode.  Unlike hardware this doesn't change the
 * text currently on the screen, but funny things can happen; cls() is
 * highly recommended.
 */
void width(int wid)
{
  switch(wid) {
  case 40:
    current.dwidth = 1;
    current.x &= ~1;
    break;
  case 80:
    current.dwidth = 0;
    break;
  default:
    abort();
  }
}

/*
 * Clear the screen, set all characters to ' ' and set all pixels to the
 * current background
 */
void cls(void)
{
  int i;
  struct screen_char *scp = &screendata[0][0];
  struct screen_char empty;
  uint32_t pixelval;
  SDL_Rect rect;

  lock_screen();
  
  empty.sym  = ' ';
  empty.attr = current.attr;

  pixelval = colors[(current.attr >> 4) & 7];

  for ( i = TS_WIDTH*TS_HEIGHT ; i ; i-- )
    *scp++ = empty;

  rect.x = rect.y = 0;
  rect.w = PX_WIDTH;
  rect.h = PX_HEIGHT;

  SDL_FillRect(rscreen, &rect, pixelval);

  update_screen(0, 0, TS_WIDTH-1, TS_HEIGHT-1);

  current.x = current.y = 0;
}


/*
 * Save the screen as well as "current" in a malloc'd buffer
 */
struct saved_screen
{
  struct current_screen current;
  struct screen_char screendata[TS_HEIGHT][TS_WIDTH];
};
  
struct saved_screen *save_screen(void)
{
  struct saved_screen *save = malloc(sizeof(struct saved_screen));
  
  if ( !save )
    return NULL;

  memcpy(&save->current, &current, sizeof(current));
  memcpy(&save->screendata, &screendata, sizeof(screendata));
  
  return save;
}

void restore_screen(struct saved_screen *save)
{
  int x, y, xs;

  memcpy(&current, &save->current, sizeof(current));
  memcpy(&screendata, &save->screendata, sizeof(screendata));

  xs = current.dwidth+1;

  lock_screen();

  for ( y = 0 ; y < TS_HEIGHT ; y++ ) {
    for ( x = 0 ; x < TS_WIDTH ; x += xs ) {
      put_screen(x,y);
    }
  }

  update_screen(0, 0, TS_WIDTH-1, TS_HEIGHT-1);
}

/*
 * Initialize SDL and the data structures
 */
int screen_init(void)
{
  int i;

  if ( SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO) )
    return -1;

  if ( !(rscreen = SDL_SetVideoMode(PX_WIDTH, PX_HEIGHT, 32, SDL_SWSURFACE)) ) {
    SDL_Quit();
    return -1;
  }

  atexit(SDL_Quit);

  /* Convert colors to preferred machine representation */
  for ( i = 0 ; i < NCOLORS ; i++ ) {
    colors[i] = SDL_MapRGB(rscreen->format,
			   rgbcolors[i].r, rgbcolors[i].g, rgbcolors[i].b);
  }

  cls();

  SDL_AddTimer(400, post_periodic, (void *)event_blink);

  return 0;
}