-
Notifications
You must be signed in to change notification settings - Fork 3
/
id_vl.cpp
651 lines (548 loc) · 14.9 KB
/
id_vl.cpp
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
// ID_VL.C
//#include <string.h>
#include "wl_def.h"
// Uncomment the following line, if you get destination out of bounds
// assertion errors and want to ignore them during debugging
#define IGNORE_BAD_DEST
#ifdef IGNORE_BAD_DEST
//#undef assert
//#define assert(x) if(!(x)) { slPrint((char *)"asset test failed0", slLocate(10,20));return;}
#define assert1(x) if(!(x)) { slPrint((char *)"asset failed1", slLocate(10,20));return;}
#define assert2(x) if(!(x)) { slPrint((char *)"asset failed2", slLocate(10,20));return;}
#define assert3(x) if(!(x)) { slPrint((char *)"asset failed3", slLocate(10,20));return;}
#define assert4(x) if(!(x)) { slPrint((char *)"asset failed4", slLocate(10,20));return;}
#define assert5(x) if(!(x)) { slPrint((char *)"asset failed5", slLocate(10,20));return;}
#define assert6(x) if(!(x)) { slPrint((char *)"asset failed6", slLocate(10,20));return;}
//#define assert7(x) if(!(x)) { slPrint((char *)"asset test failed7", slLocate(10,20));return;}
#define assert8(x) if(!(x)) { slPrint((char *)"asset failed8", slLocate(10,20));return;}
#define assert_ret(x) if(!(x)) return 0
#else
#define assert_ret(x) assert(x)
#endif
//boolean fullscreen = true;
#if defined(_arch_dreamcast)
unsigned screenWidth = 320;
unsigned screenHeight = 200;
unsigned screenBits = 8;
#elif defined(GP2X)
unsigned screenWidth = 320;
unsigned screenHeight = 240;
unsigned screenBits = 8;
#else
unsigned screenWidth = SATURN_WIDTH;
unsigned screenHeight = 240;
unsigned screenBits = 8; // use "best" color depth according to libSDL
#endif
SDL_Surface *screen = NULL;
SDL_Surface *screenBuffer = NULL;
SDL_Surface *curSurface = NULL;
unsigned curPitch;
unsigned scaleFactor;
boolean screenfaded;
SDL_Color curpal[256];
#define CASSERT(x) extern int ASSERT_COMPILE[((x) != 0) * 2 - 1];
#undef RGB
#define RGB(r, g, b) {(r)*255/63, (g)*255/63, (b)*255/63}
//#define RGB(r, g, b) (r<<2,g<<2,b<<2,0)
SDL_Color gamepal[]={
#ifdef SPEAR
#include "sodpal.inc"
#else
#include "wolfpal.inc"
#endif
};
//CASSERT(lengthof(gamepal) == 256)
//===========================================================================
/*
=======================
=
= VL_Shutdown
=
=======================
*/
void VL_Shutdown (void)
{
//VL_SetTextMode ();
}
/*
=======================
=
= VL_SetVGAPlaneMode
=
=======================
*/
void VL_SetVGAPlaneMode (void)
{
if(screenBits == 0)
{
const SDL_VideoInfo *vidInfo = SDL_GetVideoInfo();
screenBits = vidInfo->vfmt->BitsPerPixel;
}
if(screen==NULL)
{
screen = SDL_SetVideoMode(screenWidth, screenHeight, screenBits,
SDL_SWSURFACE | (screenBits == 8 ? SDL_HWPALETTE : 0) );
}
if(!screen)
{
// printf("Unable to set %ix%ix%i video mode: %s\n", screenWidth,
// screenHeight, screenBits, SDL_GetError());
SYS_Exit(1);
}
if(screenBuffer==NULL)
{
screenBuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, screenWidth, screenHeight, 8, 0, 0, 0, 0);
if(!screenBuffer)
{
// printf("Unable to create screen buffer surface: %s\n", SDL_GetError());
SYS_Exit(1);
}
// SDL_ShowCursor(SDL_DISABLE);
curSurface = screenBuffer;
curPitch = screenBuffer->pitch;
}
// SDL_SetColors(screen, gamepal, 0, 256);
SDL_SetColors(curSurface, gamepal, 0, 256);
memcpyl(curpal, gamepal, sizeof(SDL_Color) * 256);
scaleFactor = screenWidth/SATURN_WIDTH;
if(screenHeight/200 < scaleFactor) scaleFactor = screenHeight/200;
if(pixelangle==NULL) pixelangle = (short *) malloc(screenWidth * sizeof(short));
CHECKMALLOCRESULT(pixelangle);
// if(wallheight==NULL) wallheight = (short *) malloc(screenWidth * sizeof(short));
// CHECKMALLOCRESULT(wallheight);
}
/*
=============================================================================
PALETTE OPS
To avoid snow, do a WaitVBL BEFORE calling these
=============================================================================
*/
/*
=================
=
= VL_ConvertPalette
=
=================
*/
void VL_ConvertPalette(byte *srcpal, SDL_Color *destpal, int numColors)
{
for(int i=0; i<numColors; i++)
{
destpal[i].r = *srcpal++ * 255 / 63;
destpal[i].g = *srcpal++ * 255 / 63;
destpal[i].b = *srcpal++ * 255 / 63;
}
}
/*
=================
=
= VL_FillPalette
=
=================
*/
void VL_FillPalette (int red, int green, int blue)
{
unsigned short i;
SDL_Color palo[256];
for(i=0; i<256; i++)
{
palo[i].r = red;
palo[i].g = green;
palo[i].b = blue;
}
VL_SetPalette(palo);
}
/*
=================
=
= VL_SetPalette
=
=================
*/
void VL_SetPalette (SDL_Color *palette)
{
memcpyl(curpal, palette, sizeof(SDL_Color) * 256);
// SDL_SetPalette(screen, SDL_PHYSPAL, palette, 0, 256);
// SDL_SetPalette(curSurface, SDL_PHYSPAL, palette, 0, 256);
}
//===========================================================================
/*
=================
=
= VL_GetPalette
=
=================
*/
void VL_GetPalette (SDL_Color *palette)
{
memcpyl(palette, curpal, sizeof(SDL_Color) * 256);
}
//===========================================================================
/*
=================
=
= VL_FadeOut
=
= Fades the current palette to the given color in the given number of steps
=
=================
*/
void VL_FadeOut (int start, int end, int red, int green, int blue, int steps)
{
int i,j,orig,delta;
SDL_Color palette1[256], palette2[256];
SDL_Color *origptr, *newptr;
red = red * 255 / 63;
green = green * 255 / 63;
blue = blue * 255 / 63;
VL_GetPalette(palette1);
VL_WaitVBL(1);
memcpyl(palette2, palette1, sizeof(SDL_Color) * 256);
//
// fade through intermediate frames
//
for (i=0;i<steps;i++)
{
origptr = &palette1[start];
newptr = &palette2[start];
for (j=start;j<=end;j++)
{
orig = origptr->r;
delta = red-orig;
newptr->r = orig + delta * i / steps;
orig = origptr->g;
delta = green-orig;
newptr->g = orig + delta * i / steps;
orig = origptr->b;
delta = blue-orig;
newptr->b = orig + delta * i / steps;
origptr++;
newptr++;
}
VL_SetPalette (palette2);
VL_WaitVBL(1);
// xxx VGAClearScreen(); // vbt : maj du fond d'écran
}
//
// final color
//
VL_FillPalette (red,green,blue);
screenfaded = true;
}
/*
=================
=
= VL_FadeIn
=
=================
*/
void VL_FadeIn (int start, int end, SDL_Color *palette, int steps)
{
SDL_Color palette1[256], palette2[256];
int i,j,delta;
VL_GetPalette(palette1);
VL_WaitVBL(1);
memcpyl(palette2, palette1, sizeof(SDL_Color) * 256);
//
// fade through intermediate frames
//
for (i=0;i<steps;i++)
{
for (j=start;j<=end;j++)
{
delta = palette[j].r-palette1[j].r;
palette2[j].r = palette1[j].r + delta * i / steps;
delta = palette[j].g-palette1[j].g;
palette2[j].g = palette1[j].g + delta * i / steps;
delta = palette[j].b-palette1[j].b;
palette2[j].b = palette1[j].b + delta * i / steps;
}
VL_SetPalette(palette2);
VL_WaitVBL(1);
// xxx VGAClearScreen(); // vbt : maj du fond d'écran
}
//
// final color
//
VL_SetPalette (palette);
screenfaded = false;
}
/*
=============================================================================
PIXEL OPS
=============================================================================
*/
byte *VL_LockSurface(SDL_Surface *surface)
{
return (byte *) surface->pixels;
}
/*
=================
=
= VL_GetPixel
=
=================
*/
byte VL_GetPixel (int x, int y)
{
assert_ret(x >= 0 && (unsigned) x < screenWidth
&& y >= 0 && (unsigned) y < screenHeight
&& "VL_GetPixel: Pixel out of bounds!");
return ((byte *) curSurface->pixels)[y * curPitch + x];
// return col;
}
/*
=================
=
= VL_Hlin
=
=================
*/
void VL_Hlin (unsigned x, unsigned y, unsigned width, int color)
{
assert1(x + width <= screenWidth
&& y < screenHeight
&& "VL_Hlin: Destination rectangle out of bounds!");
Uint8 *dest = ((byte *) curSurface->pixels) + y * curPitch + x;
memset(dest, color, width);
}
/*
=================
=
= VL_Vlin
=
=================
*/
void VL_Vlin (int x, int y, int height, int color)
{
assert2(x >= 0 && (unsigned) x < screenWidth
&& y >= 0 && (unsigned) y + height <= screenHeight
&& "VL_Vlin: Destination rectangle out of bounds!");
Uint8 *dest = ((byte *) curSurface->pixels) + y * curPitch + x;
while (height--)
{
*dest = color;
dest += curPitch;
}
}
/*
=================
=
= VL_Bar
=
=================
*/
void VL_BarScaledCoord (int scx, int scy, int scwidth, int scheight, int color)
{
if (scy<0 || scy>screenHeight)
scy=0;
/*
assert3(scx >= 0 && (unsigned) scx + scwidth <= screenWidth
&& scy >= 0 && (unsigned) scy + scheight <= screenHeight
&& "VL_BarScaledCoord: Destination rectangle out of bounds!");
*/
// VL_LockSurface(curSurface);
Uint8 *dest = ((byte *) curSurface->pixels) + scy * curPitch + scx;
while (scheight--)
{
memset(dest, color, scwidth);
dest += curPitch;
}
VL_UnlockSurface(curSurface);// vbt utile
}
void VL_BarScaledCoordNBG (int scx, int scy, int scwidth, int scheight, int color)
{
// if (scy<0 || scy>screenHeight)
// scy=0;
/*
assert3(scx >= 0 && (unsigned) scx + scwidth <= screenWidth
&& scy >= 0 && (unsigned) scy + scheight <= screenHeight
&& "VL_BarScaledCoord: Destination rectangle out of bounds!");
*/
// VL_LockSurface(curSurface);
// Uint8 *dest = ((byte *) curSurface->pixels) + scy * curPitch + scx;
/*
while (scheight--)
{
memset(dest, color, scwidth);
dest += curPitch;
}
VL_UnlockSurface(curSurface);// vbt utile
*/
// unsigned char *surfacePtr = (unsigned char*)src->pixels + ((srcrect->y) * src->pitch) + srcrect->x;
// unsigned char *surfacePtrD = (unsigned char*)dst->pixels + (dstrect->y* dst->pitch)+ dstrect->x;
unsigned int *dest = (unsigned int*)(VDP2_VRAM_A0 + (scy<<9)+ scx);
while (scheight--)
{
memset(dest, color, scwidth);
dest+=128;
}
}
/*
============================================================================
MEMORY OPS
============================================================================
*/
/*
=================
=
= VL_MemToLatch
=
=================
*/
void VL_MemToLatch(byte *source, int width, int height,
SDL_Surface *destSurface, int x, int y)
{
assert4(x >= 0 && (unsigned) x + width <= screenWidth
&& y >= 0 && (unsigned) y + height <= screenHeight
&& "VL_MemToLatch: Destination rectangle out of bounds!");
// VL_LockSurface(destSurface);
int pitch = destSurface->pitch;
byte *dest = (byte *) destSurface->pixels + y * pitch + x;
for(int ysrc = 0; ysrc < height; ysrc++)
{
for(int xsrc = 0; xsrc < width; xsrc++)
{
dest[xsrc] = source[(ysrc * (width >> 2) + (xsrc >> 2))
+ (xsrc & 3) * (width >> 2) * height];
}
dest+=pitch;
}
}
//===========================================================================
/*
=================
=
= VL_MemToScreenScaledCoord
=
= Draws a block of data to the screen with scaling according to scaleFactor.
=
=================
*/
// vbt : à améliorer
void VL_MemToScreenScaledCoord (byte *source, int width, int height, int destx, int desty)
{
// assert5(destx >= 0 && destx + width * scaleFactor <= screenWidth
// && desty >= 0 && desty + height * scaleFactor <= screenHeight
// && "VL_MemToScreenScaledCoord: Destination rectangle out of bounds!");
#if 1
// VL_LockSurface(curSurface);
byte *vbuf = (byte *)curSurface->pixels+(desty*curPitch)+destx;
unsigned char w2 = width>>2;
unsigned int mul = w2*height;
// if(scaleFactor == 1)
{
for(unsigned int j=0; j<height; j++)
{
for(unsigned int i=0; i<width; i++)
{
vbuf[i] = source[(i>>2)+(i&3)*mul];
}
vbuf+=curPitch;
source+=w2;
}
}
/* else
{
for(unsigned int j=0,scj=0; j<height; j++, scj+=scaleFactor)
{
for(unsigned int i=0,sci=0; i<width; i++, sci+=scaleFactor)
{
byte col = source[(j*w2+(i>>2))+(i&3)*mul];
for(unsigned m=0; m<scaleFactor; m++)
{
for(unsigned n=0; n<scaleFactor; n++)
{
vbuf[(scj+m)*curPitch+sci+n] = col;
}
}
}
}
}*/
VL_UnlockSurface(curSurface); // vbt utile pour signon screen
#endif
}
//==========================================================================
/*
=================
=
= VL_LatchToScreen
=
=================
*/
// vbt à améliorer
void VL_LatchToScreenScaledCoord(SDL_Surface *source, int xsrc, int ysrc,
int width, int height, int scxdest, int scydest)
{
assert(scxdest >= 0 && scxdest + width * scaleFactor <= screenWidth
&& scydest >= 0 && scydest + height * scaleFactor <= screenHeight
&& "VL_LatchToScreenScaledCoord: Destination rectangle out of bounds!");
if(scaleFactor == 1)
{
SDL_Rect srcrect = { xsrc, ysrc, width, height };
SDL_Rect destrect = { scxdest, scydest, 0, 0 }; // width and height are ignored
SDL_BlitSurface(source, &srcrect, curSurface, &destrect);
}
else
{
// VL_LockSurface(source);
byte *src = (byte *) source->pixels;
unsigned srcPitch = source->pitch;
// VL_LockSurface(curSurface);
byte *vbuf = (byte *) curSurface->pixels;
for(int j=0,scj=0; j<height; j++, scj+=scaleFactor)
{
for(int i=0,sci=0; i<width; i++, sci+=scaleFactor)
{
byte col = src[(ysrc + j)*srcPitch + xsrc + i];
for(unsigned m=0; m<scaleFactor; m++)
{
for(unsigned n=0; n<scaleFactor; n++)
{
vbuf[(scydest+scj+m)*curPitch+scxdest+sci+n] = col;
}
}
}
}
}
//VL_UnlockSurface(curSurface); // pas de cas de scale
}
/*
typedef struct TC_TRANSFER {
Uint32 size;
void *target;
void *source;
} TC_transfer __attribute__ ((aligned (8)));
Bool slTransferEntry2(void *src , void *dest , Uint16 size)
{
extern Uint8 TransRequest;
if(TransRequest>0xF0)TransRequest=0;
TC_transfer *tc = (TC_transfer *)(TransList + sizeof(TC_transfer)*TransRequest);
tc->source = (void *)src;
tc->target = (void *)dest;
tc->size = size;
TransRequest++;
(tc + TransRequest)->source += 1 << 31;
return true;
}
*/
//extern int nb_unlock;
//0,0,latchpics[2+picnum-LATCHPICS_LUMP_START]->w,latchpics[2+picnum-LATCHPICS_LUMP_START]->h,
void VL_LatchToScreenScaledCoordIndirect(SDL_Surface *source, int scxdest, int scydest)
{
// assert(scxdest >= 0 && scxdest + width * scaleFactor <= screenWidth
// && scydest >= 0 && scydest + height * scaleFactor <= screenHeight
// && "VL_LatchToScreenScaledCoord: Destination rectangle out of bounds!");
unsigned char *surfacePtr = (unsigned char*)source->pixels; // + ((0) * source->pitch) + 0;
unsigned int *nbg1Ptr = (unsigned int*)(VDP2_VRAM_A0 + (scydest<<9)+ scxdest);
//if(TransCount!=0)
// slPrintHex(TransCount,slLocate(10,4));
for( Sint16 i=0;i<source->h;i++)
{
slDMACopy((void*)surfacePtr,(void *)nbg1Ptr,source->w);
// slTransferEntry((void *)surfacePtr,(void *)nbg1Ptr,source->w);
surfacePtr+=source->pitch;
// nb_unlock+=source->w;
nbg1Ptr+=128;
}
}