-
Notifications
You must be signed in to change notification settings - Fork 25
/
render_sw8.c
477 lines (387 loc) · 11.7 KB
/
render_sw8.c
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
** Oricutron
** Copyright (C) 2009-2014 Peter Gordon
**
** 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, version 2
** of the License.
**
** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
** Software rendering
*/
#include <stdlib.h>
#include <string.h>
#include "system.h"
#include "6502.h"
#include "via.h"
#include "8912.h"
#include "disk.h"
#include "gui.h"
#include "monitor.h"
#include "6551.h"
#include "machine.h"
#include "render_sw8.h"
#include "ula.h"
// this *should* be 16, but on AmigaOS that messes
// with the pointer colours..
#define GPAL_FIRSTPEN 20
static struct SDL_Surface *screen;
static Uint32 offset_top;
static Uint32 qpen[16*256];
extern SDL_bool fullscreen, hwsurface;
static SDL_bool needclr;
extern struct textzone *tz[NUM_TZ];
extern unsigned char sgpal[];
extern Uint8 oricpalette[];
extern struct guiimg gimgs[NUM_GIMG];
extern SDL_bool refreshstatus;
static Uint8 *mgimg[NUM_GIMG];
static int next_gimgcol;
static SDL_Color colours[256];
// Our "lovely" hand-coded font
extern unsigned char thefont[];
// --- printchar template function --------------------------------------------
// Print a char onto a X-bit framebuffer, X being a power of 2
// ptr = where to draw it
// ch = which char to draw
// fcol = foreground colour
// bcol = background colour
// solidfont = use background colour
static void printchar( Uint8* ptr, unsigned char ch, Uint8 fcol, Uint8 bcol, SDL_bool solidfont )
{
int x, y, mask;
const Uint8 *src_byte;
Uint8 *dst_pixel;
Uint8 *dst_scanline;
if( ch > 127 ) return;
src_byte = &thefont[ch*12];
dst_scanline = ptr;
for( y=12; y!=0; --y, ++src_byte, dst_scanline+=screen->pitch )
{
dst_pixel = dst_scanline;
for( mask=0x80, x=8; x!=0; --x, ++dst_pixel, mask>>=1 )
{
if( (*src_byte)&mask )
*dst_pixel = fcol;
else if( solidfont )
*dst_pixel = bcol;
}
}
}
void render_begin_sw8( struct machine *oric )
{
int y;
Uint8 *dst_scanline;
if( SDL_MUSTLOCK( screen ) )
SDL_LockSurface( screen );
if( oric->newpopupstr )
{
dst_scanline = (Uint8*)screen->pixels;
dst_scanline += 320;
for( y=12; y!=0; --y, dst_scanline += screen->pitch)
memset(dst_scanline, GPAL_FIRSTPEN, 320);
oric->newpopupstr = SDL_FALSE;
}
if( oric->newstatusstr )
{
refreshstatus = SDL_TRUE;
oric->newstatusstr = SDL_FALSE;
}
}
void render_end_sw8( struct machine *oric )
{
int i;
Uint32 char_pitch;
Uint8 *dst_pixel;
char_pitch = 8;
if( oric->emu_mode == EM_RUNNING )
{
if( oric->popupstr[0] )
{
dst_pixel = (Uint8*)screen->pixels;
dst_pixel += 320;
for( i=0; oric->popupstr[i]; i++, dst_pixel += char_pitch )
printchar( dst_pixel, oric->popupstr[i], GPAL_FIRSTPEN+1, GPAL_FIRSTPEN, SDL_TRUE );
}
if( oric->statusstr[0] )
{
dst_pixel = (Uint8*)screen->pixels;
dst_pixel += 466 * screen->pitch;
for( i=0; oric->statusstr[i]; i++, dst_pixel += char_pitch )
printchar( dst_pixel, oric->statusstr[i], GPAL_FIRSTPEN+1, 0, SDL_FALSE );
}
}
if( SDL_MUSTLOCK( screen ) )
SDL_UnlockSurface( screen );
SDL_COMPAT_Flip( screen );
}
void render_textzone_alloc_sw8( struct machine *oric, int i )
{
}
void render_textzone_free_sw8( struct machine *oric, int i )
{
}
void render_textzone_sw8( struct machine *oric, int i )
{
int x, y, o;
struct textzone *ptz = tz[i];
Uint8 *dst_scanline, *dst_pixel;
dst_scanline = (Uint8 *)screen->pixels;
dst_scanline += screen->pitch * ptz->y + ptz->x;
o = 0;
for( y=ptz->h; y!=0; --y, dst_scanline+=12*screen->pitch )
{
dst_pixel = dst_scanline;
for( x=ptz->w; x!=0; --x, ++o, dst_pixel+=8 )
printchar( dst_pixel, ptz->tx[o], GPAL_FIRSTPEN+ptz->fc[o], GPAL_FIRSTPEN+ptz->bc[o], SDL_TRUE );
}
}
// Clear an area to background
void render_clear_area_sw8( int x, int y, int w, int h )
{
Uint8 *dst_scanline;
Sint32 i;
dst_scanline = (Uint8 *)screen->pixels;
dst_scanline += screen->pitch * y + x;
for( i=0; i<h; i++, dst_scanline+=screen->pitch )
memset( dst_scanline, 0, w );
}
// Draw a GUI image at X,Y
void render_gimg_sw8( int img_id, Sint32 xp, Sint32 yp )
{
struct guiimg *gi = &gimgs[img_id];
Uint8 *src_scanline, *dst_scanline;
Sint32 i;
src_scanline = mgimg[img_id];
dst_scanline = (Uint8 *)screen->pixels;
dst_scanline += screen->pitch * yp + xp;
for( i=gi->h; i!=0; --i, src_scanline+=gi->w, dst_scanline+=screen->pitch )
memcpy( dst_scanline, src_scanline, gi->w );
}
// Draw part of an image (xp,yp = screen location, ox, oy = offset into image, w, h = dimensions)
void render_gimgpart_sw8( int img_id, Sint32 xp, Sint32 yp, Sint32 ox, Sint32 oy, Sint32 w, Sint32 h )
{
struct guiimg *gi = &gimgs[img_id];
Uint8 *src_scanline, *dst_scanline;
Sint32 i;
src_scanline = mgimg[img_id];
src_scanline += gi->w * oy + ox;
dst_scanline = (Uint8 *)screen->pixels;
dst_scanline += screen->pitch * yp + xp;
for( i=h; i!=0; --i, src_scanline+=gi->w, dst_scanline+=screen->pitch )
memcpy( dst_scanline, src_scanline, w );
}
// Copy the video output buffer to the SDL surface, assuming 16bpp video mode
void render_video_sw8( struct machine *oric, SDL_bool doublesize )
{
int x, y;
Uint16 *src_pixel;
Sint32 dst_pitch_x2;
Uint32 c;
Uint8 *dst_scanline, *dst_even_scanline, *dst_odd_scanline;
Uint32 *dst_even_pixel, *dst_odd_pixel;
if( !oric->scr )
return;
if( doublesize )
{
if( needclr )
{
SDL_FillRect(screen, NULL, GPAL_FIRSTPEN);
needclr = SDL_FALSE;
}
src_pixel = (Uint16 *)oric->scr;
dst_pitch_x2 = 2 * screen->pitch;
dst_even_scanline = ((Uint8*)screen->pixels) + offset_top;
dst_odd_scanline = dst_even_scanline;
dst_odd_scanline += screen->pitch;
if( oric->scanlines )
{
for( y=0; y<224; y++, dst_even_scanline+=dst_pitch_x2, dst_odd_scanline+=dst_pitch_x2 )
{
if (!oric->vid_dirty[y])
{
src_pixel += 120;
continue;
}
dst_even_pixel = (Uint32*)dst_even_scanline;
dst_odd_pixel = (Uint32*)dst_odd_scanline;
for( x=0; x<120; x++ )
{
c = *(src_pixel++);
*(dst_even_pixel++) = qpen[c];
*(dst_odd_pixel++) = qpen[c+8*257];
}
oric->vid_dirty[y] = SDL_FALSE;
}
} else {
for( y=0; y<224; y++, dst_even_scanline+=dst_pitch_x2, dst_odd_scanline+=dst_pitch_x2 )
{
if (!oric->vid_dirty[y])
{
src_pixel += 120;
continue;
}
dst_even_pixel = (Uint32*)dst_even_scanline;
dst_odd_pixel = (Uint32*)dst_odd_scanline;
for( x=0; x<120; x++ )
{
c = qpen[*(src_pixel++)];
*(dst_even_pixel++) = c;
*(dst_odd_pixel++) = c;
}
oric->vid_dirty[y] = SDL_FALSE;
}
}
return;
}
needclr = SDL_TRUE;
src_pixel = (Uint16 *)oric->scr;
dst_scanline = (Uint8*)screen->pixels;
for( y=0; y<4; y++ )
{
memset( dst_scanline, 0, 240 );
dst_scanline += screen->pitch;
}
for( ; y<228; y++, dst_scanline+=screen->pitch )
{
memcpy(dst_scanline, src_pixel, 240);
src_pixel += 240;
}
}
void preinit_render_sw8( struct machine *oric )
{
// Screen surface is not set yet
screen = NULL;
}
SDL_bool render_togglefullscreen_sw8( struct machine *oric )
{
#if defined(__amigaos4__) || defined(__linux__)
// Use SDL_WM_ToggleFullScreen on systems where it is supported
if( SDL_COMPAT_WM_ToggleFullScreen( screen ) )
{
fullscreen = !fullscreen;
return SDL_TRUE;
}
return SDL_FALSE;
#else
oric->shut_render( oric );
fullscreen = !fullscreen;
if( oric->init_render( oric ) ) return SDL_TRUE;
set_render_mode( oric, RENDERMODE_NULL );
oric->emu_mode = EM_PLEASEQUIT;
return SDL_FALSE;
#endif
}
static int similar_colour(int i, int r, int g, int b)
{
return abs(r-(int)colours[i].r)+abs(g-(int)colours[i].g)+abs(b-(int)colours[i].b);
}
static int find_best_pen(Uint8 r, int g, int b)
{
int i;
int closest_pen=-1, closest_distance=8000;
/* First, look for an exact match */
for( i=0; i<next_gimgcol; i++ )
{
if( (colours[i].r == r) &&
(colours[i].g == g) &&
(colours[i].b == b) )
return i;
if (similar_colour(i, r, g, b) < closest_distance)
{
closest_pen = i;
closest_distance = similar_colour(i, r, g, b);
}
}
/* Any really close? */
if (closest_distance < 3)
return closest_pen;
/* Can we make a new pen? */
if (next_gimgcol >= 256)
return closest_pen;
colours[next_gimgcol].r = r;
colours[next_gimgcol].g = g;
colours[next_gimgcol].b = b;
return next_gimgcol++;
}
static SDL_bool guiimg_to_img(Uint8** dst, const struct guiimg* src)
{
size_t i;
const Uint8 *src_pixel;
Uint8 *dst_pixel;
(*dst) = (Uint8 *)malloc( src->w*src->h );
if ((*dst) == NULL)
return SDL_FALSE;
src_pixel = src->buf;
dst_pixel = *dst;
for( i=src->w*src->h; i!=0; --i, src_pixel += 3, ++dst_pixel )
(*dst_pixel) = find_best_pen(src_pixel[0], src_pixel[1], src_pixel[2]);
return SDL_TRUE;
}
SDL_bool init_render_sw8( struct machine *oric )
{
int i, j;
Sint32 surfacemode;
surfacemode = SDL_COMPAT_HWPALETTE;
if( fullscreen ) surfacemode |= SDL_COMPAT_FULLSCREEN;
if( hwsurface ) surfacemode |= SDL_COMPAT_HWSURFACE;
// Try to setup the video display
screen = SDL_COMPAT_SetVideoMode( 640, 480, 8, surfacemode );
if (oric->show_keyboard) {
screen = SDL_COMPAT_SetVideoMode( 640, 480+240, 8, surfacemode );
} else {
screen = SDL_COMPAT_SetVideoMode( 640, 480, 8, surfacemode );
}
if( !screen )
{
printf( "SDL video failed\n" );
return SDL_FALSE;
}
SDL_COMPAT_WM_SetCaption( APP_NAME_FULL, APP_NAME_FULL );
for( i=0; i<8; i++ )
{
colours[i].r = oricpalette[i*3 ];
colours[i+8].r = oricpalette[i*3 ]/2;
colours[i].g = oricpalette[i*3+1];
colours[i+8].g = oricpalette[i*3+1]/2;
colours[i].b = oricpalette[i*3+2];
colours[i+8].b = oricpalette[i*3+2]/2;
}
for( i=0; i<NUM_GUI_COLS; i++ )
{
colours[i+GPAL_FIRSTPEN].r = sgpal[i*3 ];
colours[i+GPAL_FIRSTPEN].g = sgpal[i*3+1];
colours[i+GPAL_FIRSTPEN].b = sgpal[i*3+2];
}
next_gimgcol = GPAL_FIRSTPEN+NUM_GUI_COLS;
// Get the images for the GUI
for( i=0; i<NUM_GIMG; i++ )
if (!guiimg_to_img(mgimg + i, gimgs + i))
return SDL_FALSE;
SDL_COMPAT_SetPalette( screen, SDL_COMPAT_LOGPAL|SDL_COMPAT_PHYSPAL, colours, 0, next_gimgcol);
// Precompute pixel quads for efficient rendering double size
for( i=0; i<8*2; i++ )
for( j=0; j<8*2; j++ )
qpen[(j<<8)|i] = (j<<24)|(j<<16)|(i<<8)|i;
// For the first frame rendered, we need to clean the screen
needclr = SDL_TRUE;
refreshstatus = SDL_TRUE;
// Calculate the offset to render the screen
offset_top = (240 - 226) * screen->pitch + 80;
ula_set_dirty( oric );
// Job done
return SDL_TRUE;
}
void shut_render_sw8( struct machine *oric )
{
// The surface will be freed by SDL_Quit, or a call to SDL_COMPAT_SetVideoMode from a different render module
}