-
Notifications
You must be signed in to change notification settings - Fork 17
/
graphics.cpp
1722 lines (1524 loc) · 50.5 KB
/
graphics.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//---------------------------------------------------------------------------
// 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. See also the license.txt file for
// additional informations.
//---------------------------------------------------------------------------
// graphics.cpp: implementation of the graphics class.
//
//////////////////////////////////////////////////////////////////////
//Flavor - Convert from DirectDraw to SDL
//Flavor #define WIN32_LEAN_AND_MEAN
#include "StdAfx.h"
#include "main.h"
#ifndef TARGET_PSP
#include "menu.h"
#include "GP2X.h"
#endif
#include "graphics.h"
#include "memory.h"
#ifdef TARGET_PSP
#include "psp/psplib/video.h"
extern PspImage *Screen;
extern int gfx_hacks;
#endif
#define INITGUID
#ifdef ZOOM_SUPPORT
int zoom=0;
#endif
//SDL_Color SDLpalette[256];
//
// 16 bit graphics buffers
// At the moment there's no system which uses more than 16 bit
// colors.
// A grpaphics buffer holds number referencing to the color from
// the "total palette" for that system.
//
int totalpalette[32*32*32];
#ifdef __GP32__
#define OFFSETX 80
#define OFFSETY 44
unsigned short drawBuffer[NGPC_SIZEX*NGPC_SIZEY];
#else
unsigned short *drawBuffer;
#endif
// NGP specific
// precalculated pattern structures
const unsigned char mypatterns[256*4] =
{
0,0,0,0, 0,0,0,1, 0,0,0,2, 0,0,0,3, 0,0,1,0, 0,0,1,1, 0,0,1,2, 0,0,1,3,
0,0,2,0, 0,0,2,1, 0,0,2,2, 0,0,2,3, 0,0,3,0, 0,0,3,1, 0,0,3,2, 0,0,3,3,
0,1,0,0, 0,1,0,1, 0,1,0,2, 0,1,0,3, 0,1,1,0, 0,1,1,1, 0,1,1,2, 0,1,1,3,
0,1,2,0, 0,1,2,1, 0,1,2,2, 0,1,2,3, 0,1,3,0, 0,1,3,1, 0,1,3,2, 0,1,3,3,
0,2,0,0, 0,2,0,1, 0,2,0,2, 0,2,0,3, 0,2,1,0, 0,2,1,1, 0,2,1,2, 0,2,1,3,
0,2,2,0, 0,2,2,1, 0,2,2,2, 0,2,2,3, 0,2,3,0, 0,2,3,1, 0,2,3,2, 0,2,3,3,
0,3,0,0, 0,3,0,1, 0,3,0,2, 0,3,0,3, 0,3,1,0, 0,3,1,1, 0,3,1,2, 0,3,1,3,
0,3,2,0, 0,3,2,1, 0,3,2,2, 0,3,2,3, 0,3,3,0, 0,3,3,1, 0,3,3,2, 0,3,3,3,
1,0,0,0, 1,0,0,1, 1,0,0,2, 1,0,0,3, 1,0,1,0, 1,0,1,1, 1,0,1,2, 1,0,1,3,
1,0,2,0, 1,0,2,1, 1,0,2,2, 1,0,2,3, 1,0,3,0, 1,0,3,1, 1,0,3,2, 1,0,3,3,
1,1,0,0, 1,1,0,1, 1,1,0,2, 1,1,0,3, 1,1,1,0, 1,1,1,1, 1,1,1,2, 1,1,1,3,
1,1,2,0, 1,1,2,1, 1,1,2,2, 1,1,2,3, 1,1,3,0, 1,1,3,1, 1,1,3,2, 1,1,3,3,
1,2,0,0, 1,2,0,1, 1,2,0,2, 1,2,0,3, 1,2,1,0, 1,2,1,1, 1,2,1,2, 1,2,1,3,
1,2,2,0, 1,2,2,1, 1,2,2,2, 1,2,2,3, 1,2,3,0, 1,2,3,1, 1,2,3,2, 1,2,3,3,
1,3,0,0, 1,3,0,1, 1,3,0,2, 1,3,0,3, 1,3,1,0, 1,3,1,1, 1,3,1,2, 1,3,1,3,
1,3,2,0, 1,3,2,1, 1,3,2,2, 1,3,2,3, 1,3,3,0, 1,3,3,1, 1,3,3,2, 1,3,3,3,
2,0,0,0, 2,0,0,1, 2,0,0,2, 2,0,0,3, 2,0,1,0, 2,0,1,1, 2,0,1,2, 2,0,1,3,
2,0,2,0, 2,0,2,1, 2,0,2,2, 2,0,2,3, 2,0,3,0, 2,0,3,1, 2,0,3,2, 2,0,3,3,
2,1,0,0, 2,1,0,1, 2,1,0,2, 2,1,0,3, 2,1,1,0, 2,1,1,1, 2,1,1,2, 2,1,1,3,
2,1,2,0, 2,1,2,1, 2,1,2,2, 2,1,2,3, 2,1,3,0, 2,1,3,1, 2,1,3,2, 2,1,3,3,
2,2,0,0, 2,2,0,1, 2,2,0,2, 2,2,0,3, 2,2,1,0, 2,2,1,1, 2,2,1,2, 2,2,1,3,
2,2,2,0, 2,2,2,1, 2,2,2,2, 2,2,2,3, 2,2,3,0, 2,2,3,1, 2,2,3,2, 2,2,3,3,
2,3,0,0, 2,3,0,1, 2,3,0,2, 2,3,0,3, 2,3,1,0, 2,3,1,1, 2,3,1,2, 2,3,1,3,
2,3,2,0, 2,3,2,1, 2,3,2,2, 2,3,2,3, 2,3,3,0, 2,3,3,1, 2,3,3,2, 2,3,3,3,
3,0,0,0, 3,0,0,1, 3,0,0,2, 3,0,0,3, 3,0,1,0, 3,0,1,1, 3,0,1,2, 3,0,1,3,
3,0,2,0, 3,0,2,1, 3,0,2,2, 3,0,2,3, 3,0,3,0, 3,0,3,1, 3,0,3,2, 3,0,3,3,
3,1,0,0, 3,1,0,1, 3,1,0,2, 3,1,0,3, 3,1,1,0, 3,1,1,1, 3,1,1,2, 3,1,1,3,
3,1,2,0, 3,1,2,1, 3,1,2,2, 3,1,2,3, 3,1,3,0, 3,1,3,1, 3,1,3,2, 3,1,3,3,
3,2,0,0, 3,2,0,1, 3,2,0,2, 3,2,0,3, 3,2,1,0, 3,2,1,1, 3,2,1,2, 3,2,1,3,
3,2,2,0, 3,2,2,1, 3,2,2,2, 3,2,2,3, 3,2,3,0, 3,2,3,1, 3,2,3,2, 3,2,3,3,
3,3,0,0, 3,3,0,1, 3,3,0,2, 3,3,0,3, 3,3,1,0, 3,3,1,1, 3,3,1,2, 3,3,1,3,
3,3,2,0, 3,3,2,1, 3,3,2,2, 3,3,2,3, 3,3,3,0, 3,3,3,1, 3,3,3,2, 3,3,3,3,
};
// standard VRAM table adresses
unsigned char *sprite_table = get_address(0x00008800);
unsigned char *pattern_table = get_address(0x0000A000);
unsigned short*patterns = (unsigned short*)pattern_table;
unsigned short *tile_table_front = (unsigned short *)get_address(0x00009000);
unsigned short *tile_table_back = (unsigned short *)get_address(0x00009800);
unsigned short *palette_table = (unsigned short *)get_address(0x00008200);
unsigned char *bw_palette_table = get_address(0x00008100);
unsigned char *sprite_palette_numbers = get_address(0x00008C00);
// VDP registers
//
// wher is the vdp rendering now on the lcd display?
//unsigned char *scanlineX = get_address(0x00008008);
unsigned char *scanlineY = get_address(0x00008009);
// frame 0/1 priority registers
unsigned char *frame0Pri = get_address(0x00008000);
unsigned char *frame1Pri = get_address(0x00008030);
// windowing registers
unsigned char *wndTopLeftX = get_address(0x00008002);
unsigned char *wndTopLeftY = get_address(0x00008003);
unsigned char *wndSizeX = get_address(0x00008004);
unsigned char *wndSizeY = get_address(0x00008005);
// scrolling registers
unsigned char *scrollSpriteX = get_address(0x00008020);
unsigned char *scrollSpriteY = get_address(0x00008021);
unsigned char *scrollFrontX = get_address(0x00008032);
unsigned char *scrollFrontY = get_address(0x00008033);
unsigned char *scrollBackX = get_address(0x00008034);
unsigned char *scrollBackY = get_address(0x00008035);
// background color selection register and table
unsigned char *bgSelect = get_address(0x00008118);
unsigned short *bgTable = (unsigned short *)get_address(0x000083E0);
unsigned char *oowSelect = get_address(0x00008012);
unsigned short *oowTable = (unsigned short *)get_address(0x000083F0);
// machine constants
unsigned char *color_switch = get_address(0x00006F91);
// Defines
#define ZeroStruct(x) ZeroMemory(&x, sizeof(x)); x.dwSize = sizeof(x);
#define SafeRelease(x) if(x) { x->Release(); x = NULL; }
// target window
/*
unsigned short p2[16] = {
0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000
};
*/
#define BLIT_X_OFFSET 8
#define BLIT_Y_OFFSET 8
#define BLIT_OFFSET (BLIT_X_OFFSET + (BLIT_Y_OFFSET*SIZEX))
#define BLIT_WIDTH (160)
#define BLIT_HEIGHT (152)
#define SCREEN_X_OFFSET ((screen->w - BLIT_WIDTH)/2)
#define SCREEN_Y_OFFSET ((screen->h - BLIT_HEIGHT)/2)
#define PSP_FUDGE 0//(32) //32 is good for 480x272 -64 is for 320x240 (squish)
#define SCREEN_OFFET (SCREEN_X_OFFSET + (SCREEN_Y_OFFSET*(screen->w+PSP_FUDGE)))//extra fudge factor for PSP?
#ifndef TARGET_PSP
#define DO_PERIODIC_FLASH_SAVES
#define DO_FPS_DISPLAY
#endif
#if defined(DO_FPS_DISPLAY) || defined(DO_PERIODIC_FLASH_SAVES)
static unsigned int frameCount = 0;
#ifdef __GP32__
static char fps[32];
#endif
#endif
//Flavor - For speed testing, you can turn off screen updates
//#define NO_SCREEN_OUTPUT //seems to give about 4-6FPS on GP2X
// flip buffers and indicate that one of the buffers is ready
// for blitting to the screen.
void graphicsBlitEnd()
{
//displayDirty=1;
}
#ifdef TARGET_PSP
void graphics_paint();
#else
// blit the display buffer, if necessary, and put it to the screen
inline void graphics_paint()
{
#ifndef NO_SCREEN_OUTPUT
#ifndef __GP32__
//should update only what changed, but test with one that updates entire screen
#ifndef ZOOM_SUPPORT
SDL_UpdateRect(screen, SCREEN_X_OFFSET, SCREEN_Y_OFFSET, BLIT_WIDTH, BLIT_HEIGHT);
#else
/* static int prevZoom=0;
if(prevZoom!=zoom)
SDL_FillRect(actualScreen, NULL, SDL_MapRGB(screen->format,0,0,0));//fill black
if(zoom<=0)
{
SDL_Rect scrRect = {SCREEN_X_OFFSET, SCREEN_Y_OFFSET, BLIT_WIDTH, BLIT_HEIGHT};
SDL_BlitSurface(screen, &scrRect, actualScreen, &scrRect);
if(prevZoom==zoom)
SDL_UpdateRects(actualScreen, 1, &scrRect);
else
SDL_Flip(actualScreen);
}
else //if(zoom>0)
{
SDL_Rect scrRect = {SCREEN_X_OFFSET, SCREEN_Y_OFFSET, BLIT_WIDTH, BLIT_HEIGHT};
SDL_Rect ascrRect = {scrRect.x-zoom, scrRect.y-zoom, scrRect.w+(zoom*2), scrRect.h+(zoom*2)};
SDL_SoftStretch(screen, &scrRect, actualScreen, &ascrRect);
if(prevZoom==zoom)
SDL_UpdateRects(actualScreen, 1, &ascrRect);
else
SDL_Flip(actualScreen);
}
prevZoom=zoom;*/
//For now, no variable zooming
SDL_Rect scrRect = {SCREEN_X_OFFSET, SCREEN_Y_OFFSET, BLIT_WIDTH, BLIT_HEIGHT};
SDL_BlitSurface(screen, &scrRect, actualScreen, &scrRect);
SDL_UpdateRects(actualScreen, 1, &scrRect);
#endif
//SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
#endif
//SDL_Flip(screen);
//}
#endif
#if defined(DO_FPS_DISPLAY) || defined(DO_PERIODIC_FLASH_SAVES)
static unsigned int startTime = 0;
unsigned int currTime;
if(startTime == 0)
startTime = SDL_GetTicks();
currTime = SDL_GetTicks();
if((currTime - startTime) >= 1000)
{
#ifdef DO_PERIODIC_FLASH_SAVES
//WRITE_SAVEGAME_IF_DIRTY;
if(needToWriteFile && options[PERIODIC_SAVES_OPTION])
{
if(options[FPS_OPTION])
printTTF("S", 10, 10, red, 1, actualScreen, 1);
writeSaveGameFile();//we found a dirty one, so write the file
if(options[FPS_OPTION])
printTTF("S", 10, 10, green, 1, actualScreen, 1);
}
else
{
#endif
#ifdef DO_FPS_DISPLAY
//SDL_Rect numRect =
if(options[FPS_OPTION])
drawNumber(frameCount, 10, 10);
//SDL_UpdateRect(screen, numRect.x, numRect.y, numRect.w, numRect.h);
#endif
#ifdef DO_PERIODIC_FLASH_SAVES
}
#endif
startTime = currTime;
frameCount = 0;
}
#endif
}
#endif
#if defined(DO_FPS_DISPLAY) || defined(DO_PERIODIC_FLASH_SAVES)
inline void incFrameCount()
{
frameCount++;
}
#endif
void write_screenshot(FILE *f)
{
#if 0
png_structp png_ptr;
png_infop info_ptr;
png_color_8 sig_bit;
png_byte row[SIZEX*3];
int x,y;
unsigned short *p, *gb;
png_byte *rowpt;
if (NULL == (png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)))
return;
if (NULL == (info_ptr = png_create_info_struct(png_ptr)))
{
png_destroy_write_struct(&png_ptr, png_infopp_NULL);
return;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_write_struct(&png_ptr, &info_ptr);
return;
}
png_init_io(png_ptr, f);
png_set_IHDR(png_ptr, info_ptr,
paintRect.right - paintRect.left,
paintRect.bottom - paintRect.top,
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
sig_bit.red = 8;
sig_bit.green = 8;
sig_bit.blue = 8;
png_set_sBIT(png_ptr, info_ptr, &sig_bit);
png_write_info(png_ptr, info_ptr);
gb = &displayBuffer[SIZEX * 8];
for(y = paintRect.bottom - paintRect.top; y; y--)
{
rowpt = row;
p = gb + 8;
for(x = paintRect.right - paintRect.left; x; x--)
{
*rowpt = (totalpalette32[*p]>>16) & 0xFF;
rowpt++;
*rowpt = (totalpalette32[*p]>>8) & 0xFF;
rowpt++;
*rowpt = totalpalette32[*p] & 0xFF;
rowpt++;
p++;
}
gb+= SIZEX;
rowpt = row;
png_write_row(png_ptr, rowpt);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
return;
#endif //#if 0
}
//////////////////////////////////////////////////////////////////////////////
//
// Palette Initialization
//
//////////////////////////////////////////////////////////////////////////////
void (*palette_init)(DWORD dwRBitMask, DWORD dwGBitMask, DWORD dwBBitMask);
void palette_init32(DWORD dwRBitMask, DWORD dwGBitMask, DWORD dwBBitMask)
{
/* dbg_print("in palette_init32(0x%X, 0x%X, 0x%X)\n", dwRBitMask, dwGBitMask, dwBBitMask);
char RShiftCount = 0, GShiftCount = 0, BShiftCount = 0;
char RBitCount = 0, GBitCount = 0, BBitCount = 0;
int r,g,b;
DWORD i;
i = dwRBitMask;
while (!(i&1))
{
i = i >> 1;
RShiftCount++;
}
while (i&1)
{
i = i >> 1;
RBitCount++;
}
i = dwGBitMask;
while (!(i&1))
{
i = i >> 1;
GShiftCount++;
}
while (i&1)
{
i = i >> 1;
GBitCount++;
}
i = dwBBitMask;
while (!(i&1))
{
i = i >> 1;
BShiftCount++;
}
while (i&1)
{
i = i >> 1;
BBitCount++;
}
switch(m_emuInfo.machine)
{
case NGP:
case NGPC:
case GAMEGEAR:
case LYNX:
case WONDERSWAN:
case WONDERSWANCOLOR:
case ADVISION:
for (b=0; b<16; b++)
for (g=0; g<16; g++)
for (r=0; r<16; r++)
totalpalette[b*256+g*16+r] =
(((b<<(BBitCount-4))+(b>>(4-(BBitCount-4))))<<BShiftCount) +
(((g<<(GBitCount-4))+(g>>(4-(GBitCount-4))))<<GShiftCount) +
(((r<<(RBitCount-4))+(r>>(4-(RBitCount-4))))<<RShiftCount);
break;
case GAMEBOY:
case GAMEBOYPOCKET:
case GAMEBOYCOLOR:
case SUPERGAMEBOY:
case SUPERVISION:
case NES:
for (b=0; b<32; b++)
for (g=0; g<32; g++)
for (r=0; r<32; r++)
totalpalette[b*32*32+g*32+r] =
(((b<<(BBitCount-5))+(b>>(5-(BBitCount-5))))<<BShiftCount) +
(((g<<(GBitCount-5))+(g>>(5-(GBitCount-5))))<<GShiftCount) +
(((r<<(RBitCount-5))+(r>>(5-(RBitCount-5))))<<RShiftCount);
break;
}*/
}
void palette_init16(DWORD dwRBitMask, DWORD dwGBitMask, DWORD dwBBitMask)
{
//dbg_print("in palette_init16(0x%X, 0x%X, 0x%X)\n", dwRBitMask, dwGBitMask, dwBBitMask);
char RShiftCount = 0, GShiftCount = 0, BShiftCount = 0;
char RBitCount = 0, GBitCount = 0, BBitCount = 0;
int r,g,b;
DWORD i;
i = dwRBitMask;
while (!(i&1))
{
i = i >> 1;
RShiftCount++;
}
while (i&1)
{
i = i >> 1;
RBitCount++;
}
i = dwGBitMask;
while (!(i&1))
{
i = i >> 1;
GShiftCount++;
}
while (i&1)
{
i = i >> 1;
GBitCount++;
}
i = dwBBitMask;
while (!(i&1))
{
i = i >> 1;
BShiftCount++;
}
while (i&1)
{
i = i >> 1;
BBitCount++;
}
switch(m_emuInfo.machine)
{
case NGP:
case NGPC:
for (b=0; b<16; b++)
for (g=0; g<16; g++)
for (r=0; r<16; r++)
totalpalette[b*256+g*16+r] =
(((b<<(BBitCount-4))+(b>>(4-(BBitCount-4))))<<BShiftCount) +
(((g<<(GBitCount-4))+(g>>(4-(GBitCount-4))))<<GShiftCount) +
(((r<<(RBitCount-4))+(r>>(4-(RBitCount-4))))<<RShiftCount);
break;
}
}
void palette_init8(DWORD dwRBitMask, DWORD dwGBitMask, DWORD dwBBitMask)
{
/*
dbg_print("in palette_init8(0x%X, 0x%X, 0x%X)\n", dwRBitMask, dwGBitMask, dwBBitMask);
SDL_Color SDLcolors[256];
unsigned char r, g, b;
//Generic 332 RGB palette
for (b=0; b<0x3; b++)
for (g=0; g<0x7; g++)
for (r=0; r<0x7; r++)
{
SDLcolors[b+(g<<2)+(r<<5)].r = r<<5;
SDLcolors[b+(g<<2)+(r<<5)].g = g<<5;
SDLcolors[b+(g<<2)+(r<<5)].b = b<<6;
}
if(!SDL_SetColors(screen, SDLcolors, 0, 256))
dbg_print("palette_init8: SDL_SetPalette failed\n");
*/
}
void pngpalette_init(void)
{
/* int r,g,b;
switch(m_emuInfo.machine)
{
case NGP:
case NGPC:
case GAMEGEAR:
case LYNX:
case WONDERSWAN:
case WONDERSWANCOLOR:
case ADVISION:
for (b=0; b<16; b++)
for (g=0; g<16; g++)
for (r=0; r<16; r++)
{
totalpalette32[b*256+g*16+r] =
(((b<<(8-4))+(b>>(4-(8-4))))<<0) +
(((g<<(8-4))+(g>>(4-(8-4))))<<8) +
(((r<<(8-4))+(r>>(4-(8-4))))<<16);
}
break;
case GAMEBOY:
case GAMEBOYPOCKET:
case GAMEBOYCOLOR:
case SUPERGAMEBOY:
case SUPERVISION:
case NES:
for (b=0; b<32; b++)
for (g=0; g<32; g++)
for (r=0; r<32; r++)
{
totalpalette32[b*32*32+g*32+r] =
(((b<<(8-5))+(b>>(5-(8-5))))<<0) +
(((g<<(8-5))+(g>>(5-(8-5))))<<8) +
(((r<<(8-5))+(r>>(5-(8-5))))<<16);
}
break;
}*/
}
//////////////////////////////////////////////////////////////////////////////
//
// Neogeo Pocket & Neogeo Pocket color rendering
//
//////////////////////////////////////////////////////////////////////////////
//static const bwTable[8] = { 0x0000, 0x0333, 0x0555, 0x0777, 0x0999, 0x0BBB, 0x0DDD, 0x0FFF };
//NOTA Color para juegos b/n
static const unsigned short bwTable[8] =
{
//NOTA nose,nose,window,nose,nose,nose,nose,nose
// B, G,R
0x0FFF, 0x0DDD, 0x0BBB, 0x0999, 0x0777, 0x0555, 0x0333, 0x0000
};
#ifndef __GP32__
// used for rendering the sprites
typedef struct
{
unsigned short offset; // offset in graphics buffer to blit, Flavor hopes 16bits is good enough
unsigned short pattern; // pattern code including palette number
unsigned short *tilept; // pointer into the tile description
unsigned short *palette; // palette used to render this sprite
}
SPRITE;
typedef struct
{
unsigned short *gbp; // (0,x) base for drawing
unsigned char count[152];
SPRITE sprite[152][64];
}
SPRITEDEFS;
//unsigned int spritesDirty = true;
SPRITEDEFS spriteDefs[3]; // 4 priority levels
// definitions of types and variables that are used internally during
// rendering of a screen
typedef struct
{
unsigned short *gbp; // pointer into graphics buffer
unsigned char oldScrollX; // keep an eye on the old and previous values
unsigned char *newScrollX; // of the scroll values
unsigned char oldScrollY;
unsigned char *newScrollY;
unsigned short *tileBase; // start address of the tile table this structure represents
short tile[21]; // the tile code
unsigned short *palettes[21]; // palettes associated with tiles
unsigned short *tilept[21]; // tile lines to render
unsigned short *palette; // palette for the tiles this VSYNC
}
TILECACHE;
unsigned short palettes[16*4+16*4+16*4]; // placeholder for the converted palette
TILECACHE tCBack; // tile pointer cache for the back buffer
TILECACHE tCFront; // tile pointer cache for the front buffer
//int BGCol; // placeholder for the background color this VSYNC
//int OOWCol; // placeholder for the outside window color this VSYNC
//unsigned char SprPriLo, SprPriHi, SprPri = 0;
inline void set_paletteCol(unsigned short *palette_table,
unsigned short *sprite,
unsigned short *front,
unsigned short *back)
{
int i;
// initialize palette table
//
// initialize back plane palette table
//these should actually be setting the SDL palette
for(i=0;i<16*4;i++)
{
//sprite[i] = *palette_table & 0x0FFF;
sprite[i] = NGPC_TO_SDL16(palette_table[i]);
//SDLpalette[i].r=(palette_table[i]&0xF)<<4;
//SDLpalette[i].g=(palette_table[i]&0xF0);
//SDLpalette[i].b=((palette_table[i]>>4)&0xF0);
}
// initialize front plane palette table
for(i=0;i<16*4;i++)
{
//front[i] = *palette_table & 0x0FFF;
front[i] = NGPC_TO_SDL16(palette_table[i+16*4]);
//SDLpalette[i].r=(palette_table[i+16*4]&0xF)<<4;
//SDLpalette[i].g=(palette_table[i+16*4]&0xF0);
//SDLpalette[i].b=((palette_table[i+16*4]>>4)&0xF0);
}
// initialize sprite palette table (?)
for(i=0;i<16*4;i++)
{
//back[i] = *palette_table & 0x0FFF;
back[i] = NGPC_TO_SDL16(palette_table[i+16*4*2]);
//SDLpalette[i].r=(palette_table[i+16*4*2]&0xF)<<4;
//SDLpalette[i].g=(palette_table[i+16*4*2]&0xF0);
//SDLpalette[i].b=((palette_table[i+16*4*2]>>4)&0xF0);
}
//if(!SDL_SetColors(screen, SDLpalette, 16*4*2, 16*4))
// dbg_print("set_paletteCol: SDL_SetPalette failed\n");
//Flavor
//I could take all the colors from sprite, front, back , BGCol, OOWCol, and set the SDL palette
//or, just take palette_table and set it
}
inline void set_paletteBW(unsigned short *palette_table,
unsigned short *sprite,
unsigned short *front,
unsigned short *back)
{
int i;
unsigned char *pt = ((unsigned char *)palette_table)-0x0100; // get b/w color table
// initialize palette table
//
for(i=0;i<4;i++)
{
// initialize sprite palette table
sprite[i] = NGPC_TO_SDL16(bwTable[pt[i] & 0x07]);
sprite[4+i] = NGPC_TO_SDL16(bwTable[pt[4+i] & 0x07]);
// initialize front plane palette table
front[i] = NGPC_TO_SDL16(bwTable[pt[8+i] & 0x07]);
front[4+i] = NGPC_TO_SDL16(bwTable[pt[8+4+i] & 0x07]);
// initialize back plane palette table
back[i] = NGPC_TO_SDL16(bwTable[pt[16+i] & 0x07]);
back[4+i] = NGPC_TO_SDL16(bwTable[pt[16+4+i] & 0x07]);
}
}
//I think there's something wrong with this on the GP2X, because CFC2's intro screen is all red
inline void lineClear(TILECACHE *tC, unsigned short col)
{
for(int i=0; i<21*8; i++)
{
tC->gbp[i] = col;
}
}
inline void clipLeftRight(SPRITEDEFS *sprDef, unsigned short OOWCol)
{
int i,j;
j = *wndTopLeftX + 8;
if (j > (NGPC_SIZEX+8))
j = NGPC_SIZEX+8;
for(i=8; i<j; i++)
sprDef->gbp[i] = OOWCol;
j = j + *wndSizeX;
if (j > (NGPC_SIZEX+8))
j = NGPC_SIZEX+8;
for(i=j; i < (NGPC_SIZEX+8); i++)
sprDef->gbp[i] = OOWCol;
}
inline void lineFront(TILECACHE *tC)
{
int i;
unsigned char a,b;
const unsigned char *p2;
unsigned short *gb;
//for 8bit SDL, this would set gb to the index of the proper color
//then, we'd set gb to p2[n]+(i*sizeof(palette))
gb = tC->gbp;
for (i=0;i<21;i++)
{
a = *(((unsigned char *)tC->tilept[i])+1);
b = *((unsigned char *)tC->tilept[i]);
if (tC->tile[i]&0x8000)
{
p2 = &mypatterns[b*4];
if (p2[3])
gb[0] = tC->palettes[i][p2[3]];
if (p2[2])
gb[1] = tC->palettes[i][p2[2]];
if (p2[1])
gb[2] = tC->palettes[i][p2[1]];
if (p2[0])
gb[3] = tC->palettes[i][p2[0]];
p2 = &mypatterns[a*4];
if (p2[3])
gb[4] = tC->palettes[i][p2[3]];
if (p2[2])
gb[5] = tC->palettes[i][p2[2]];
if (p2[1])
gb[6] = tC->palettes[i][p2[1]];
if (p2[0])
gb[7] = tC->palettes[i][p2[0]];
}
else
{
p2 = &mypatterns[a*4];
if (p2[0])
gb[0] = tC->palettes[i][p2[0]];
if (p2[1])
gb[1] = tC->palettes[i][p2[1]];
if (p2[2])
gb[2] = tC->palettes[i][p2[2]];
if (p2[3])
gb[3] = tC->palettes[i][p2[3]];
p2 = &mypatterns[b*4];
if (p2[0])
gb[4] = tC->palettes[i][p2[0]];
if (p2[1])
gb[5] = tC->palettes[i][p2[1]];
if (p2[2])
gb[6] = tC->palettes[i][p2[2]];
if (p2[3])
gb[7] = tC->palettes[i][p2[3]];
}
if (tC->tile[i]&0x4000)
tC->tilept[i]-= 1;
else
tC->tilept[i]+= 1;
gb+= 8;
}
}
inline void lineSprite(SPRITEDEFS *sprDefs)
{
SPRITE *spr;
unsigned short *gb;
unsigned char a,b;
const unsigned char *p2;
//for 8bit SDL, this would set gb to the index of the proper color
//then, we'd set gb to p2[n]
for (int i=sprDefs->count[*scanlineY];i>0;i--)
{
spr = &sprDefs->sprite[*scanlineY][i-1];
gb = &sprDefs->gbp[spr->offset];
a = *(((unsigned char *)spr->tilept)+1);
b = *((unsigned char *)spr->tilept);
if (spr->pattern&0x8000)
{
p2 = &mypatterns[b*4];
if (p2[3])
gb[0] = spr->palette[p2[3]];
if (p2[2])
gb[1] = spr->palette[p2[2]];
if (p2[1])
gb[2] = spr->palette[p2[1]];
if (p2[0])
gb[3] = spr->palette[p2[0]];
p2 = &mypatterns[a*4];
if (p2[3])
gb[4] = spr->palette[p2[3]];
if (p2[2])
gb[5] = spr->palette[p2[2]];
if (p2[1])
gb[6] = spr->palette[p2[1]];
if (p2[0])
gb[7] = spr->palette[p2[0]];
}
else
{
p2 = &mypatterns[a*4];
if (p2[0])
gb[0] = spr->palette[p2[0]];
if (p2[1])
gb[1] = spr->palette[p2[1]];
if (p2[2])
gb[2] = spr->palette[p2[2]];
if (p2[3])
gb[3] = spr->palette[p2[3]];
p2 = &mypatterns[b*4];
if (p2[0])
gb[4] = spr->palette[p2[0]];
if (p2[1])
gb[5] = spr->palette[p2[1]];
if (p2[2])
gb[6] = spr->palette[p2[2]];
if (p2[3])
gb[7] = spr->palette[p2[3]];
}
}
}
// sort all the sprites according to their priorities
/*inline void spriteSort(unsigned int bw)
{
unsigned short spriteCode;
unsigned short *pt;
unsigned char x, y, prevx=0, prevy=0;
int i, j;
SPRITEDEFS *SprPri00 = &spriteDefs[0];
SPRITEDEFS *SprPri40 = &spriteDefs[1];
SPRITEDEFS *SprPri80 = &spriteDefs[2];
SPRITEDEFS *SprPriC0 = &spriteDefs[3];
// initialize the number of sprites in each structure
SprPri00->count = 0;
SprPri40->count = 0;
SprPri80->count = 0;
SprPriC0->count = 0;
for (i=0;i<64;i++)
{
spriteCode = *((unsigned short *)(sprite_table+4*i));
if (spriteCode & 0x0400)
prevx+= *(sprite_table+4*i+2);
else
prevx = *(sprite_table+4*i+2) + 8;
x = prevx + *scrollSpriteX;
if (spriteCode & 0x0200)
prevy+= *(sprite_table+4*i+3);
else
prevy = *(sprite_table+4*i+3) + 8;
y = prevy + *scrollSpriteY;
j = *scanlineY+8 - y;
if ((spriteCode>0xff) && (j >= 0) && (j < 8) && (x<168)) //is this sprite visable on the current scanline?
{
// if ((j >= 0) && (j < 8) && (x<168)) {
SPRITE *spr = NULL;
// *(sprite_palette_numbers+i)
pt = (unsigned short *)(pattern_table + 16*(spriteCode & 0x01ff)
+ ((spriteCode&0x4000) ? (7-j)*2 : j*2));
switch (spriteCode & 0x1800)
{
// case order reversed because priority 3 (and 2) sprites occur most of the time
case 0x1800:
spr = &SprPriC0->sprite[SprPriC0->count];
SprPriC0->count++;
break;
case 0x1000:
spr = &SprPri80->sprite[SprPri80->count];
SprPri80->count++;
break;
case 0x0800:
spr = &SprPri40->sprite[SprPri40->count];
SprPri40->count++;
break;
case 0x0000:
spr = &SprPri00->sprite[SprPri00->count];
SprPri00->count++;
break;
}
spr->pattern = spriteCode;
spr->offset = x;
spr->tilept = pt;
spr->palette = ((bw) ? &palettes[(spriteCode>>11)&0x04]
: &palettes[(*(sprite_palette_numbers+i)&0x0F)*4]);
}
}
}*/
inline void spriteSortAll(unsigned int bw)
{
unsigned int spriteCode;
unsigned short *pt;
unsigned char x, y, prevx=0, prevy=0;
unsigned int i, j, k, scanline;
SPRITE *spr;
SPRITEDEFS *SprPri40 = &spriteDefs[0];
SPRITEDEFS *SprPri80 = &spriteDefs[1];
SPRITEDEFS *SprPriC0 = &spriteDefs[2];
// initialize the number of sprites in each structure
memset(SprPri40->count, 0, 152);
memset(SprPri80->count, 0, 152);
memset(SprPriC0->count, 0, 152);
for (i=0;i<64;i++)
{
spriteCode = *((unsigned short *)(sprite_table+4*i));
if ((spriteCode<=0xff) || ((spriteCode & 0x1800)==0))
continue;
if (spriteCode & 0x0400)
prevx+= *(sprite_table+4*i+2);
else
prevx = *(sprite_table+4*i+2) + 8;
x = prevx + *scrollSpriteX;
if (spriteCode & 0x0200)
prevy+= *(sprite_table+4*i+3);
else
prevy = *(sprite_table+4*i+3) + 8;
y = prevy + *scrollSpriteY;
if (x>167 || y>151)
continue;
for(k=0; k<8; k++)
{
scanline = y+k-8;
if(scanline < 0 || scanline >= 152)
continue;
// if ((x<168) && (spriteCode>0xff) && (spriteCode & 0x1800))
// {
switch (spriteCode & 0x1800)
{
// case order reversed because priority 3 (and 2) sprites occur most of the time
case 0x1800:
spr = &SprPriC0->sprite[scanline][SprPriC0->count[scanline]++];
break;
case 0x1000:
spr = &SprPri80->sprite[scanline][SprPri80->count[scanline]++];
break;
case 0x0800:
spr = &SprPri40->sprite[scanline][SprPri40->count[scanline]++];
break;
}
j = scanline+8 - y;
spr->pattern = spriteCode;
spr->offset = x;
spr->tilept = (unsigned short *)(pattern_table + 16*(spriteCode & 0x01ff)
+ ((spriteCode&0x4000) ? (7-j)*2 : j*2));
spr->palette = ((bw) ? &palettes[(spriteCode>>11)&0x04]
: &palettes[(*(sprite_palette_numbers+i)&0x0F)*4]);
// }
}
}
}
// initialize drawing/blitting of a screen
void graphicsBlitInit()
{
// buffers 0 and 1
// definitions for the back frame
tCBack.gbp = &drawBuffer[8*SIZEX + (8-((*scrollBackX)&7))];
tCBack.newScrollX = scrollBackX;
tCBack.newScrollY = scrollBackY;
tCBack.tileBase = tile_table_back;
tCBack.palette = &palettes[16*4+16*4];
// definitions for the front frame
tCFront.gbp = &drawBuffer[8*SIZEX + (8-((*scrollFrontX)&7))];
tCFront.newScrollX = scrollFrontX;
tCFront.newScrollY = scrollFrontY;
tCFront.tileBase = tile_table_front;
tCFront.palette = &palettes[16*4];
// definitions for sprite priorities