summaryrefslogtreecommitdiff
path: root/source/framebuffer.c
blob: 6d165291345687699419ade192a9965329074b22 (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
/*
   ds81 - Nintendo DS ZX81 emulator.

   Copyright (C) 2006  Ian Cowburn <ianc@noddybox.co.uk>
   
   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.
  
   $Id$
*/

#include <nds.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "framebuffer.h"

/* ---------------------------------------- STATIC DATA
*/
#define WIDTH	256
#define SCAN	128
#define HEIGHT	192

static uint16	*buff;
static uint16	*pal;

static uint8	font[]=
{
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x08, 0x08, 0x08, 0x08, 0x00, 0x08, 0x00,
    0x00, 0x24, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x24, 0x7e, 0x24, 0x24, 0x7e, 0x24, 0x00,
    0x00, 0x10, 0x7c, 0x14, 0x7c, 0x50, 0x7c, 0x10,
    0x00, 0x46, 0x26, 0x10, 0x08, 0x64, 0x62, 0x00,
    0x00, 0x08, 0x14, 0x08, 0x54, 0x22, 0x5c, 0x00,
    0x00, 0x10, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x20, 0x10, 0x10, 0x10, 0x10, 0x20, 0x00,
    0x00, 0x04, 0x08, 0x08, 0x08, 0x08, 0x04, 0x00,
    0x00, 0x00, 0x28, 0x10, 0x7c, 0x10, 0x28, 0x00,
    0x00, 0x00, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x08,
    0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00,
    0x00, 0x00, 0x40, 0x20, 0x10, 0x08, 0x04, 0x00,
    0x00, 0x3c, 0x62, 0x52, 0x4a, 0x46, 0x3c, 0x00,
    0x00, 0x18, 0x14, 0x10, 0x10, 0x10, 0x7c, 0x00,
    0x00, 0x3c, 0x42, 0x40, 0x3c, 0x02, 0x7e, 0x00,
    0x00, 0x3c, 0x42, 0x30, 0x40, 0x42, 0x3c, 0x00,
    0x00, 0x10, 0x18, 0x14, 0x12, 0x7e, 0x10, 0x00,
    0x00, 0x7e, 0x02, 0x3e, 0x40, 0x42, 0x3c, 0x00,
    0x00, 0x3c, 0x02, 0x3e, 0x42, 0x42, 0x3c, 0x00,
    0x00, 0x7e, 0x40, 0x20, 0x10, 0x08, 0x08, 0x00,
    0x00, 0x3c, 0x42, 0x3c, 0x42, 0x42, 0x3c, 0x00,
    0x00, 0x3c, 0x42, 0x42, 0x7c, 0x40, 0x3c, 0x00,
    0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x00,
    0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x08, 0x04,
    0x00, 0x00, 0x20, 0x10, 0x08, 0x10, 0x20, 0x00,
    0x00, 0x00, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0x00,
    0x00, 0x00, 0x08, 0x10, 0x20, 0x10, 0x08, 0x00,
    0x00, 0x3c, 0x42, 0x20, 0x10, 0x00, 0x10, 0x00,
    0x00, 0x3c, 0x52, 0x6a, 0x7a, 0x02, 0x3c, 0x00,
    0x00, 0x3c, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x00,
    0x00, 0x3e, 0x42, 0x3e, 0x42, 0x42, 0x3e, 0x00,
    0x00, 0x3c, 0x42, 0x02, 0x02, 0x42, 0x3c, 0x00,
    0x00, 0x1e, 0x22, 0x42, 0x42, 0x22, 0x1e, 0x00,
    0x00, 0x7e, 0x02, 0x3e, 0x02, 0x02, 0x7e, 0x00,
    0x00, 0x7e, 0x02, 0x3e, 0x02, 0x02, 0x02, 0x00,
    0x00, 0x3c, 0x42, 0x02, 0x72, 0x42, 0x3c, 0x00,
    0x00, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42, 0x00,
    0x00, 0x7c, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x00,
    0x00, 0x40, 0x40, 0x40, 0x42, 0x42, 0x3c, 0x00,
    0x00, 0x22, 0x12, 0x0e, 0x12, 0x22, 0x42, 0x00,
    0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x7e, 0x00,
    0x00, 0x42, 0x66, 0x5a, 0x42, 0x42, 0x42, 0x00,
    0x00, 0x42, 0x46, 0x4a, 0x52, 0x62, 0x42, 0x00,
    0x00, 0x3c, 0x42, 0x42, 0x42, 0x42, 0x3c, 0x00,
    0x00, 0x3e, 0x42, 0x42, 0x3e, 0x02, 0x02, 0x00,
    0x00, 0x3c, 0x42, 0x42, 0x4a, 0x52, 0x3c, 0x00,
    0x00, 0x3e, 0x42, 0x42, 0x3e, 0x22, 0x42, 0x00,
    0x00, 0x3c, 0x02, 0x3c, 0x40, 0x42, 0x3c, 0x00,
    0x00, 0x7f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00,
    0x00, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c, 0x00,
    0x00, 0x42, 0x42, 0x42, 0x42, 0x24, 0x18, 0x00,
    0x00, 0x42, 0x42, 0x42, 0x42, 0x5a, 0x24, 0x00,
    0x00, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x00,
    0x00, 0x41, 0x22, 0x14, 0x08, 0x08, 0x08, 0x00,
    0x00, 0x7e, 0x20, 0x10, 0x08, 0x04, 0x7e, 0x00,
    0x00, 0x70, 0x10, 0x10, 0x10, 0x10, 0x70, 0x00,
    0x00, 0x00, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00,
    0x00, 0x0e, 0x08, 0x08, 0x08, 0x08, 0x0e, 0x00,
    0x00, 0x08, 0x1c, 0x2a, 0x08, 0x08, 0x08, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
    0x00, 0x38, 0x44, 0x1e, 0x04, 0x04, 0x7e, 0x00,
    0x00, 0x00, 0x1c, 0x20, 0x3c, 0x22, 0x3c, 0x00,
    0x00, 0x04, 0x04, 0x3c, 0x44, 0x44, 0x3c, 0x00,
    0x00, 0x00, 0x38, 0x04, 0x04, 0x04, 0x38, 0x00,
    0x00, 0x20, 0x20, 0x3c, 0x22, 0x22, 0x3c, 0x00,
    0x00, 0x00, 0x1c, 0x22, 0x1e, 0x02, 0x3c, 0x00,
    0x00, 0x30, 0x08, 0x18, 0x08, 0x08, 0x08, 0x00,
    0x00, 0x00, 0x3c, 0x22, 0x22, 0x3c, 0x20, 0x1c,
    0x00, 0x02, 0x02, 0x1e, 0x22, 0x22, 0x22, 0x00,
    0x00, 0x08, 0x00, 0x0c, 0x08, 0x08, 0x1c, 0x00,
    0x00, 0x20, 0x00, 0x20, 0x20, 0x20, 0x24, 0x18,
    0x00, 0x04, 0x14, 0x0c, 0x0c, 0x14, 0x24, 0x00,
    0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x30, 0x00,
    0x00, 0x00, 0x16, 0x2a, 0x2a, 0x2a, 0x2a, 0x00,
    0x00, 0x00, 0x1e, 0x22, 0x22, 0x22, 0x22, 0x00,
    0x00, 0x00, 0x1c, 0x22, 0x22, 0x22, 0x1c, 0x00,
    0x00, 0x00, 0x1e, 0x22, 0x22, 0x1e, 0x02, 0x02,
    0x00, 0x00, 0x3c, 0x22, 0x22, 0x3c, 0x20, 0x60,
    0x00, 0x00, 0x38, 0x04, 0x04, 0x04, 0x04, 0x00,
    0x00, 0x00, 0x1c, 0x02, 0x1c, 0x20, 0x1e, 0x00,
    0x00, 0x08, 0x1c, 0x08, 0x08, 0x08, 0x30, 0x00,
    0x00, 0x00, 0x22, 0x22, 0x22, 0x22, 0x1c, 0x00,
    0x00, 0x00, 0x22, 0x22, 0x14, 0x14, 0x08, 0x00,
    0x00, 0x00, 0x22, 0x2a, 0x2a, 0x2a, 0x14, 0x00,
    0x00, 0x00, 0x22, 0x14, 0x08, 0x14, 0x22, 0x00,
    0x00, 0x00, 0x22, 0x22, 0x22, 0x3c, 0x20, 0x1c,
    0x00, 0x00, 0x3e, 0x10, 0x08, 0x04, 0x3e, 0x00,
    0x00, 0x70, 0x10, 0x0c, 0x10, 0x10, 0x70, 0x00,
    0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00,
    0x00, 0x0e, 0x08, 0x30, 0x08, 0x08, 0x0e, 0x00,
    0x00, 0x28, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x3c, 0x42, 0x99, 0x85, 0x85, 0x99, 0x42, 0x3c
};


/* ---------------------------------------- PRIVATE INTERFACES
*/
static inline void Plot(int x, int y, int col)
{
    uint16 *base;
    uint16 cur;
    int odd;

    if (col == -1)
    	return;

    odd = x&1;
    x /= 2;

    base = buff+x+y*SCAN;
    cur = *base;

    if (odd)
    {
    	cur = (cur & 0xff) | (col<<8);
    }
    else
    {
    	cur = (cur & 0xff00) | col;
    }

    *base = cur;
}


/* ---------------------------------------- PUBLIC INTERFACES
*/
void FB_Init(uint16 *vram, uint16 *palette)
{
    buff = vram;
    pal = palette;

    pal[COL_BLACK] = RGB15(0,0,0);
    pal[COL_WHITE] = RGB15(31,31,31);
    pal[COL_RED] = RGB15(31,0,0);
    pal[COL_GREEN] = RGB15(0,31,0);
    pal[COL_BLUE] = RGB15(0,0,31);
    pal[COL_GUISELECT] = RGB15(8,8,31);
    pal[COL_GREY] = RGB15(15,15,15);
    pal[COL_LIGHTGREY] = RGB15(22,22,22);
    pal[COL_DARKGREY] = RGB15(8,8,8);
    pal[COL_YELLOW] = RGB15(31,31,0);
}


void FB_LoadASCIITiles(uint16 *tiles)
{
    uint8 *src;
    int c;
    int row;
    int val;
    int mask;

    src = font;

    for(c = 32; c < 127; c++)
    {
	for(row = 0; row < 8; row++)
	{
	    for(mask = 1; mask < 0xff; mask<<=1)
	    {
		if (*src & mask)
		{
		    val = COL_WHITE;
		}
		else
		{
		    val = 0;
		}

		mask <<= 1;

		if (*src & mask)
		{
		    val |= COL_WHITE << 8;
		}

		*tiles++ = val;
	    }

	src++;
	}
    }
}


void FB_Print(const char *text, int x, int y, FB_Colour colour, FB_Colour paper)
{
    int cx,cy;
    int ch;

    while(*text)
    {
	ch=((*text)-32)*8;

	for(cy=0;cy<8;cy++)
	{
	    for(cx=0;cx<8;cx++)
	    {
		if (font[ch]&(1<<cx))
		{
		    Plot(x+cx, y+cy, colour);
		}
		else
		{
		    Plot(x+cx, y+cy, paper);
		}
	    }

	    ch++;
	}

	x+=8;
	text++;
    }
}


void FB_Centre(const char *text, int y, FB_Colour colour, FB_Colour paper)
{
    FB_Print(text,WIDTH/2-strlen(text)*4,y,colour,paper);
}


void FB_printf(int x, int y, FB_Colour colour, FB_Colour paper,
	       const char *format, ...)
{
    char buff[80];
    va_list va;

    va_start(va,format);
    vsnprintf(buff,sizeof buff,format,va);
    va_end(va);

    FB_Print(buff,x,y,colour,paper);
}


void FB_HLine(int x1, int x2, int y, FB_Colour colour)
{
    uint16 *line;

    line=buff+y*WIDTH+x1;

    while(x1<=x2)
    {
    	Plot(x1++,y,colour);
    }
}


void FB_VLine(int x, int y1, int y2, FB_Colour colour)
{
    while(y1<=y2)
    {
	Plot(x,y1++,colour);
    }
}


void FB_Box(int x, int y, int w, int h, FB_Colour colour)
{
    FB_HLine(x,x+w-1,y,colour);
    FB_HLine(x,x+w-1,y+h-1,colour);
    FB_VLine(x,y,y+h-1,colour);
    FB_VLine(x+w-1,y,y+h-1,colour);
}


void FB_FillBox(int x, int y, int w, int h, FB_Colour colour)
{
    while(h--)
    {
    	FB_HLine(x,x+w-1,y++,colour);
    }
}


void FB_Clear(void)
{
    uint16 *p;
    int f;

    f=WIDTH*HEIGHT/2;
    p=buff;

    while(f--)
    {
    	*p++=0;
    }
}


void FB_Blit(sImage *img, int x, int y, int offset)
{
    uint16 *dest;
    uint16 *row;
    uint8 *src;
    uint16 pix;
    int hww;
    int ht;
    int f;

    x /= 2;

    ht = img->height;
    hww = img->width / 2;
    dest = buff+x+y*SCAN;
    src = img->image.data8;

    for(f=0;f<16;f++)
    {
    	pal[offset+f] = img->palette[f];
    }

    while(ht--)
    {
    	row = dest;

    	for(f=0;f<hww;f++)
	{
	    pix = *src++ + offset;
	    pix |= (*src++ + offset) << 8;

	    *row++ = pix;
	}

	dest += SCAN;
    }
}