graphics.cpp 49 KB
Newer Older
Libretro-Admin's avatar
Libretro-Admin committed
1
2
3
4
5
6
7
8
/*---------------------------------------------------------------------------
 *	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.
 *---------------------------------------------------------------------------
 */
Ed Mandy's avatar
Ed Mandy committed
9

Libretro-Admin's avatar
Libretro-Admin committed
10
/* graphics.cpp: implementation of the graphics class. */
Ed Mandy's avatar
Ed Mandy committed
11

Libretro-Admin's avatar
Libretro-Admin committed
12
/* Flavor - Convert from DirectDraw to SDL */
Ed Mandy's avatar
Ed Mandy committed
13

Libretro-Admin's avatar
Libretro-Admin committed
14
/* Flavor #define WIN32_LEAN_AND_MEAN */
Ed Mandy's avatar
Ed Mandy committed
15
16
17
18
19
20

#include "StdAfx.h"
#include "main.h"
#include "graphics.h"
#include "memory.h"

's avatar
committed
21
extern ngp_screen* screen;
Ed Mandy's avatar
Ed Mandy committed
22
23
24
25
extern int gfx_hacks;

#define INITGUID

Libretro-Admin's avatar
Libretro-Admin committed
26
27
28
29
30
31
32
/*
 * 16 bit graphics buffers
 * At the moment there's no system which uses more than 16 bit
 * colors.
 * A graphics buffer holds number referencing to the color from
 * the "total palette" for that system.
 */
Ed Mandy's avatar
Ed Mandy committed
33
34
int    totalpalette[32*32*32];

Libretro-Admin's avatar
Libretro-Admin committed
35
36
37
38
39
40
41
/* Allows application of a 'dark filter' to reduce the
 * glare of white backgrounds when viewing NGP content
 * on a backlit screen
 * Note: This code has no consistency regarding variable
 * names - so we just use the normal libretro standard of
 * delimiter-separated words...
 */
jdgleaver's avatar
jdgleaver committed
42
43
static unsigned dark_filter_level = 0;

negativeExponent's avatar
negativeExponent committed
44
static unsigned short *drawBuffer;
Ed Mandy's avatar
Ed Mandy committed
45

Libretro-Admin's avatar
Libretro-Admin committed
46
47
/* NGP specific
 * precalculated pattern structures */
Ed Mandy's avatar
Ed Mandy committed
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
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,
    };

Libretro-Admin's avatar
Libretro-Admin committed
84
/* standard VRAM table adresses */
Ed Mandy's avatar
Ed Mandy committed
85
86
87
88
89
90
91
92
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);
Libretro-Admin's avatar
Libretro-Admin committed
93
94
95
96
97
98
99
100
101

/* VDP registers
 *
 * where is the vdp rendering now on the lcd display?
 */

#if 0
unsigned char *scanlineX  = get_address(0x00008008);
#endif
Ed Mandy's avatar
Ed Mandy committed
102
unsigned char *scanlineY  = get_address(0x00008009);
Libretro-Admin's avatar
Libretro-Admin committed
103
/* frame 0/1 priority registers */
Ed Mandy's avatar
Ed Mandy committed
104
105
unsigned char *frame0Pri  = get_address(0x00008000);
unsigned char *frame1Pri  = get_address(0x00008030);
Libretro-Admin's avatar
Libretro-Admin committed
106
/* windowing registers */
Ed Mandy's avatar
Ed Mandy committed
107
108
109
110
unsigned char *wndTopLeftX = get_address(0x00008002);
unsigned char *wndTopLeftY = get_address(0x00008003);
unsigned char *wndSizeX  = get_address(0x00008004);
unsigned char *wndSizeY  = get_address(0x00008005);
Libretro-Admin's avatar
Libretro-Admin committed
111
/* scrolling registers */
Ed Mandy's avatar
Ed Mandy committed
112
113
114
115
116
117
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);
Libretro-Admin's avatar
Libretro-Admin committed
118
/* background color selection register and table */
Ed Mandy's avatar
Ed Mandy committed
119
120
121
122
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);
Libretro-Admin's avatar
Libretro-Admin committed
123
/* machine constants */
Ed Mandy's avatar
Ed Mandy committed
124
125
unsigned char *color_switch = get_address(0x00006F91);

Libretro-Admin's avatar
Libretro-Admin committed
126
/* Defines */
Ed Mandy's avatar
Ed Mandy committed
127
128
129
130
#define ZeroStruct(x) ZeroMemory(&x, sizeof(x)); x.dwSize = sizeof(x);
#define SafeRelease(x) if(x) { x->Release(); x = NULL; }


Libretro-Admin's avatar
Libretro-Admin committed
131
/* target window */
Ed Mandy's avatar
Ed Mandy committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
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)

Libretro-Admin's avatar
Libretro-Admin committed
147
148
149
150
/* (32)  32 is good for 480x272  -64 is for 320x240 (squish) */
#define PSP_FUDGE 0
/* extra fudge factor for PSP? */
#define SCREEN_OFFET (SCREEN_X_OFFSET + (SCREEN_Y_OFFSET*(screen->w+PSP_FUDGE)))
Ed Mandy's avatar
Ed Mandy committed
151

's avatar
committed
152
#ifndef __LIBRETRO__
Ed Mandy's avatar
Ed Mandy committed
153
154
#define DO_PERIODIC_FLASH_SAVES
#endif
155
#if defined(DO_PERIODIC_FLASH_SAVES)
Ed Mandy's avatar
Ed Mandy committed
156
157
158
static unsigned int frameCount = 0;
#endif

Libretro-Admin's avatar
Libretro-Admin committed
159
/* Flavor - For speed testing, you can turn off screen updates */
Ed Mandy's avatar
Ed Mandy committed
160

Libretro-Admin's avatar
Libretro-Admin committed
161
162
163
164
165
166
167
#if 0
#define NO_SCREEN_OUTPUT  /* seems to give about 4-6FPS on GP2X */
#endif

/* flip buffers and indicate that one of the buffers is ready
 * for blitting to the screen. */
void graphicsBlitEnd(void)
Ed Mandy's avatar
Ed Mandy committed
168
{
Libretro-Admin's avatar
Libretro-Admin committed
169
170
171
#if 0
    displayDirty=1;
#endif
Ed Mandy's avatar
Ed Mandy committed
172
173
174
}


175
void graphics_paint(void);
Ed Mandy's avatar
Ed Mandy committed
176

177
#if defined(DO_PERIODIC_FLASH_SAVES)
Ed Mandy's avatar
Ed Mandy committed
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
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;
Libretro-Admin's avatar
Libretro-Admin committed
240
#endif /* #if 0 */
Ed Mandy's avatar
Ed Mandy committed
241
242
}

Libretro-Admin's avatar
Libretro-Admin committed
243
244
245
246
247
/*
 *
 * Palette Initialization
 *
 */
Ed Mandy's avatar
Ed Mandy committed
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

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;
    }*/
}

Libretro-Admin's avatar
Libretro-Admin committed
327
/* RGB range: [0,1] */
jdgleaver's avatar
jdgleaver committed
328
329
void darken_rgb(float &r, float &g, float &b)
{
Libretro-Admin's avatar
Libretro-Admin committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    /* Note: This is *very* approximate...
     * - Should be done in linear colour space. It isn't.
     * - Should alter brightness by performing an RGB->HSL->RGB
     *   conversion. We just do simple linear scaling instead.
     * Basically, this is intended for use on devices that are
     8 too weak to run shaders (i.e. why would you want a 'dark filter'
     * if your device supports proper LCD shaders?). We therefore
     * cut corners for the sake of performance...
     *
     * Constants
     * > Luminosity factors: photometric/digital ITU BT.709
     *static const float luma_r = 0.2126f;
     *static const float luma_g = 0.7152f;
     *static const float luma_b = 0.0722f;
     * > Luminosity factors: Digital ITU BT.601
     *   (seems to produce better results than ITU BT.709)
     */
jdgleaver's avatar
jdgleaver committed
347
348
349
    static const float luma_r = 0.299f;
    static const float luma_g = 0.587f;
    static const float luma_b = 0.114f;
Libretro-Admin's avatar
Libretro-Admin committed
350
    /* Calculate luminosity */
jdgleaver's avatar
jdgleaver committed
351
    float luma = (luma_r * r) + (luma_g * g) + (luma_b * b);
Libretro-Admin's avatar
Libretro-Admin committed
352
353
354
355
    /* Get 'darkness' scaling factor
     * > User set 'dark filter' level scaled by current luminosity
     *   (i.e. lighter colours affected more than darker colours)
     */
jdgleaver's avatar
jdgleaver committed
356
357
    float dark_factor = 1.0f - ((static_cast<float>(dark_filter_level) * 0.01f) * luma);
    dark_factor = dark_factor < 0.0f ? 0.0f : dark_factor;
Libretro-Admin's avatar
Libretro-Admin committed
358
    /* Perform scaling... */
jdgleaver's avatar
jdgleaver committed
359
360
361
362
363
    r = r * dark_factor;
    g = g * dark_factor;
    b = b * dark_factor;
}

Ed Mandy's avatar
Ed Mandy committed
364
365
void palette_init16(DWORD dwRBitMask, DWORD dwGBitMask, DWORD dwBBitMask)
{
Libretro-Admin's avatar
Libretro-Admin committed
366
367
368
#if 0
    dbg_print("in palette_init16(0x%X, 0x%X, 0x%X)\n", dwRBitMask, dwGBitMask, dwBBitMask);
#endif
Ed Mandy's avatar
Ed Mandy committed
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

    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:
jdgleaver's avatar
jdgleaver committed
412
413
414
415
416
417
418
            if (dark_filter_level > 0)
            {
                static const float rgb_max = 15.0f;
                static const float rgb_max_inv = 1.0f / rgb_max;
                float r_float, g_float, b_float;
                int r_final, g_final, b_final;

Libretro-Admin's avatar
Libretro-Admin committed
419
                /* Perform RGB assignment with colour conversion */
jdgleaver's avatar
jdgleaver committed
420
421
422
423
424
425
                for (b=0; b<16; b++)
                {
                    for (g=0; g<16; g++)
                    {
                        for (r=0; r<16; r++)
                        {
Libretro-Admin's avatar
Libretro-Admin committed
426
                            /* Convert colour range from [0,0xF] to [0,1] */
jdgleaver's avatar
jdgleaver committed
427
428
429
                            r_float = static_cast<float>(r) * rgb_max_inv;
                            g_float = static_cast<float>(g) * rgb_max_inv;
                            b_float = static_cast<float>(b) * rgb_max_inv;
Libretro-Admin's avatar
Libretro-Admin committed
430
                            /* Perform image darkening */
jdgleaver's avatar
jdgleaver committed
431
                            darken_rgb(r_float, g_float, b_float);
Libretro-Admin's avatar
Libretro-Admin committed
432
                            /* Convert back to 4bit unsigned */
jdgleaver's avatar
jdgleaver committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
                            r_final = static_cast<int>((r_float * rgb_max) + 0.5f) & 0xF;
                            g_final = static_cast<int>((g_float * rgb_max) + 0.5f) & 0xF;
                            b_final = static_cast<int>((b_float * rgb_max) + 0.5f) & 0xF;

                            totalpalette[b*256+g*16+r] =
                                (((b_final<<(BBitCount-4))+(b_final>>(4-(BBitCount-4))))<<BShiftCount) +
                                (((g_final<<(GBitCount-4))+(g_final>>(4-(GBitCount-4))))<<GShiftCount) +
                                (((r_final<<(RBitCount-4))+(r_final>>(4-(RBitCount-4))))<<RShiftCount);
                        }
                    }
                }
            }
            else
            {
Libretro-Admin's avatar
Libretro-Admin committed
447
                /* Use fast RGB assignment */
jdgleaver's avatar
jdgleaver committed
448
449
450
451
452
453
454
455
456
                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;
Ed Mandy's avatar
Ed Mandy committed
457
458
459
    }
}

Libretro-Admin's avatar
Libretro-Admin committed
460
461
462
463
/* Again, there is no consistency in naming...
 * Most interface functions seem to use camel case,
 * so do the same here...
 */
jdgleaver's avatar
jdgleaver committed
464
465
void graphicsSetDarkFilterLevel(unsigned filterLevel)
{
466
   unsigned prev_dark_filter_level = dark_filter_level;
jdgleaver's avatar
jdgleaver committed
467

468
469
   dark_filter_level = filterLevel;
   dark_filter_level = (dark_filter_level > 100) ? 100 : dark_filter_level;
jdgleaver's avatar
jdgleaver committed
470

471
472
   if (dark_filter_level != prev_dark_filter_level)
      palette_init16(0xf800,0x7e0,0x1f);
jdgleaver's avatar
jdgleaver committed
473
474
}

Ed Mandy's avatar
Ed Mandy committed
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
void palette_init8(DWORD dwRBitMask, DWORD dwGBitMask, DWORD dwBBitMask)
{
}

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;
    }*/
}

Libretro-Admin's avatar
Libretro-Admin committed
521
522
523
524
525
/*
 *
 * Neogeo Pocket & Neogeo Pocket color rendering
 *
 */
Ed Mandy's avatar
Ed Mandy committed
526

Libretro-Admin's avatar
Libretro-Admin committed
527
528
529
530
531
#if 0
static const bwTable[8] = { 0x0000, 0x0333, 0x0555, 0x0777, 0x0999, 0x0BBB, 0x0DDD, 0x0FFF };
#endif

/* NOTA Color para juegos b/n */
Ed Mandy's avatar
Ed Mandy committed
532
533
static const unsigned short bwTable[8] =
    {
Libretro-Admin's avatar
Libretro-Admin committed
534
535
        /* NOTA  nose,nose,window,nose,nose,nose,nose,nose
         * B, G,R */
Ed Mandy's avatar
Ed Mandy committed
536
537
538
        0x0FFF, 0x0DDD, 0x0BBB, 0x0999, 0x0777, 0x0555, 0x0333, 0x0000
    };

Libretro-Admin's avatar
Libretro-Admin committed
539
/* used for rendering the sprites */
Ed Mandy's avatar
Ed Mandy committed
540
541
542

typedef struct
{
Libretro-Admin's avatar
Libretro-Admin committed
543
544
545
546
    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 */
Ed Mandy's avatar
Ed Mandy committed
547
548
549
550
551
}
SPRITE;

typedef struct
{
Libretro-Admin's avatar
Libretro-Admin committed
552
    unsigned short *gbp;    /* (0,x) base for drawing */
Ed Mandy's avatar
Ed Mandy committed
553
554
555
556
557
    unsigned char count[152];
    SPRITE   sprite[152][64];
}
SPRITEDEFS;

Libretro-Admin's avatar
Libretro-Admin committed
558
559
560
#if 0
unsigned int spritesDirty = true;
#endif
Ed Mandy's avatar
Ed Mandy committed
561

Libretro-Admin's avatar
Libretro-Admin committed
562
SPRITEDEFS spriteDefs[3];    /* 4 priority levels */
Ed Mandy's avatar
Ed Mandy committed
563

Libretro-Admin's avatar
Libretro-Admin committed
564
565
/* definitions of types and variables that are used internally during
 * rendering of a screen */
Ed Mandy's avatar
Ed Mandy committed
566
567
568

typedef struct
{
Libretro-Admin's avatar
Libretro-Admin committed
569
570
571
    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 */
Ed Mandy's avatar
Ed Mandy committed
572
573
    unsigned char oldScrollY;
    unsigned char *newScrollY;
Libretro-Admin's avatar
Libretro-Admin committed
574
575
576
577
578
    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 */
Ed Mandy's avatar
Ed Mandy committed
579
580
581
}
TILECACHE;

Libretro-Admin's avatar
Libretro-Admin committed
582
unsigned short palettes[16*4+16*4+16*4]; /* placeholder for the converted palette */
Ed Mandy's avatar
Ed Mandy committed
583
584


Libretro-Admin's avatar
Libretro-Admin committed
585
586
TILECACHE  tCBack;      /* tile pointer cache for the back buffer */
TILECACHE  tCFront;     /* tile pointer cache for the front buffer */
Ed Mandy's avatar
Ed Mandy committed
587

Libretro-Admin's avatar
Libretro-Admin committed
588
589
590
591
592
#if 0
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;
#endif
Ed Mandy's avatar
Ed Mandy committed
593
594
595
596
597
598

inline void set_paletteCol(unsigned short *palette_table,
                    unsigned short *sprite,
                    unsigned short *front,
                    unsigned short *back)
{
599
600
601
602
   int i;
   /* initialize palette table
    *
    * initialize back plane palette table
Ed Mandy's avatar
Ed Mandy committed
603

604
605
    * these should actually be setting the SDL palette
    */
Ed Mandy's avatar
Ed Mandy committed
606

607
608
   for(i=0;i<16*4;i++)
   {
Libretro-Admin's avatar
Libretro-Admin committed
609
#if 0
610
      sprite[i] = *palette_table & 0x0FFF;
Libretro-Admin's avatar
Libretro-Admin committed
611
#endif
612
613
      sprite[i] = NGPC_TO_SDL16(palette_table[i]);
   }
Ed Mandy's avatar
Ed Mandy committed
614

615
616
617
   /* initialize front plane palette table */
   for(i=0;i<16*4;i++)
   {
Libretro-Admin's avatar
Libretro-Admin committed
618
#if 0
619
      front[i] = *palette_table & 0x0FFF;
Libretro-Admin's avatar
Libretro-Admin committed
620
#endif
621
622
      front[i] = NGPC_TO_SDL16(palette_table[i+16*4]);
   }
Ed Mandy's avatar
Ed Mandy committed
623

624
625
626
   /* initialize sprite palette table (?) */
   for(i=0;i<16*4;i++)
   {
Libretro-Admin's avatar
Libretro-Admin committed
627
#if 0
628
      back[i] = *palette_table & 0x0FFF;
Libretro-Admin's avatar
Libretro-Admin committed
629
#endif
630
631
      back[i] = NGPC_TO_SDL16(palette_table[i+16*4*2]);
   }
Ed Mandy's avatar
Ed Mandy committed
632
633
634
635
636
637
638
639
}

inline void set_paletteBW(unsigned short *palette_table,
                   unsigned short *sprite,
                   unsigned short *front,
                   unsigned short *back)
{
    int i;
Libretro-Admin's avatar
Libretro-Admin committed
640
641
642
    unsigned char *pt = ((unsigned char *)palette_table)-0x0100; /* get b/w color table */

    /* initialize palette table */
Ed Mandy's avatar
Ed Mandy committed
643
644
    for(i=0;i<4;i++)
    {
Libretro-Admin's avatar
Libretro-Admin committed
645
        /* initialize sprite palette table */
Ed Mandy's avatar
Ed Mandy committed
646
647
        sprite[i] = NGPC_TO_SDL16(bwTable[pt[i] & 0x07]);
        sprite[4+i] = NGPC_TO_SDL16(bwTable[pt[4+i] & 0x07]);
Libretro-Admin's avatar
Libretro-Admin committed
648
        /* initialize front plane palette table */
Ed Mandy's avatar
Ed Mandy committed
649
650
        front[i] = NGPC_TO_SDL16(bwTable[pt[8+i] & 0x07]);
        front[4+i] = NGPC_TO_SDL16(bwTable[pt[8+4+i] & 0x07]);
Libretro-Admin's avatar
Libretro-Admin committed
651
        /* initialize back plane palette table */
Ed Mandy's avatar
Ed Mandy committed
652
653
654
655
656
        back[i] = NGPC_TO_SDL16(bwTable[pt[16+i] & 0x07]);
        back[4+i] = NGPC_TO_SDL16(bwTable[pt[16+4+i] & 0x07]);
    }
}

Libretro-Admin's avatar
Libretro-Admin committed
657
/* I think there's something wrong with this on the GP2X, because CFC2's intro screen is all red */
Ed Mandy's avatar
Ed Mandy committed
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
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;

Libretro-Admin's avatar
Libretro-Admin committed
687
688
    /* 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)) */
Ed Mandy's avatar
Ed Mandy committed
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

    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;

Libretro-Admin's avatar
Libretro-Admin committed
752
753
    /* for 8bit SDL, this would set gb to the index of the proper color
     * then, we'd set gb to p2[n] */
Ed Mandy's avatar
Ed Mandy committed
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

    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]];
        }
    }
}

Libretro-Admin's avatar
Libretro-Admin committed
806
807
808
/* sort all the sprites according to their priorities */
#if 0
inline void spriteSort(unsigned int bw)
Ed Mandy's avatar
Ed Mandy committed
809
{
Libretro-Admin's avatar
Libretro-Admin committed
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
   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]);
      }
      }
   }
#endif
Ed Mandy's avatar
Ed Mandy committed
875
876
877
878
879
880
881
882
883
884
885
886
887

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];

Libretro-Admin's avatar
Libretro-Admin committed
888
    /* initialize the number of sprites in each structure */
Ed Mandy's avatar
Ed Mandy committed
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
    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;

Libretro-Admin's avatar
Libretro-Admin committed
920
921
922
923
#if 0
            if ((x<168) && (spriteCode>0xff) && (spriteCode & 0x1800))
            {
#endif
Ed Mandy's avatar
Ed Mandy committed
924
925
                switch (spriteCode & 0x1800)
                {
Libretro-Admin's avatar
Libretro-Admin committed
926
                    /* case order reversed because priority 3 (and 2) sprites occur most of the time */
Ed Mandy's avatar
Ed Mandy committed
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
                    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]);
Libretro-Admin's avatar
Libretro-Admin committed
944
945
946
#if 0
            }
#endif
Ed Mandy's avatar
Ed Mandy committed
947
948
949
950
951
        }
    }
}


Libretro-Admin's avatar
Libretro-Admin committed
952
953
/* initialize drawing/blitting of a screen */
void graphicsBlitInit(void)
Ed Mandy's avatar
Ed Mandy committed
954
{
Libretro-Admin's avatar
Libretro-Admin committed
955
956
    /* buffers 0 and 1
     * definitions for the back frame */
Ed Mandy's avatar
Ed Mandy committed
957
958
959
960
961
    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];
Libretro-Admin's avatar
Libretro-Admin committed
962
    /* definitions for the front frame */
Ed Mandy's avatar
Ed Mandy committed
963
964
965
966
967
    tCFront.gbp   = &drawBuffer[8*SIZEX + (8-((*scrollFrontX)&7))];
    tCFront.newScrollX = scrollFrontX;
    tCFront.newScrollY = scrollFrontY;
    tCFront.tileBase = tile_table_front;
    tCFront.palette  = &palettes[16*4];
Libretro-Admin's avatar
Libretro-Admin committed
968
969
970
971
972
    /* definitions for sprite priorities */
#if 0
    SprPriLo = *frame1Pri>>6;
    SprPriHi = *frame0Pri>>6; /* ? */
#endif
Ed Mandy's avatar
Ed Mandy committed
973
974
975
    spriteDefs[0].gbp = &drawBuffer[8*SIZEX];
    spriteDefs[1].gbp = &drawBuffer[8*SIZEX];
    spriteDefs[2].gbp = &drawBuffer[8*SIZEX];
Libretro-Admin's avatar
Libretro-Admin committed
976
977
978
979
#if 0
    spriteDefs[3].gbp = &drawBuffer[8*SIZEX];
#endif
    /* force recalculations for first line */
Ed Mandy's avatar
Ed Mandy committed
980
    tCBack.oldScrollX = *tCBack.newScrollX;
Libretro-Admin's avatar
Libretro-Admin committed
981
    tCBack.oldScrollY = *tCBack.newScrollY+1;   /* force calculation of structure data */
Ed Mandy's avatar
Ed Mandy committed
982
    tCFront.oldScrollX = *tCFront.newScrollX;
Libretro-Admin's avatar
Libretro-Admin committed
983
984
985
986
987
    tCFront.oldScrollY = *tCFront.newScrollY+1;  /* force calculation of structure data */
    /* start drawing at line 0 */
#if 0
     *scanlineY = 0;
#endif
Ed Mandy's avatar
Ed Mandy committed
988
989
990
991
992
993
994
995
996
997
998
999
}

inline void RenderTileCache(TILECACHE *tC, unsigned int bw)
{
    int    i;
    unsigned char line;
    unsigned short *temp;

    if ((tC->oldScrollX != *tC->newScrollX) ||
            (tC->oldScrollY != *tC->newScrollY) ||
            (((*tC->newScrollY + *scanlineY)&7) == 0))
    {
Libretro-Admin's avatar
Libretro-Admin committed
1000
1001
        /* update the structure to reflect the changed scroll registers
         * first update pointer into render buffer */
Ed Mandy's avatar
Ed Mandy committed
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
        tC->gbp = tC->gbp + (tC->oldScrollX&7) - (*tC->newScrollX&7);

        tC->oldScrollX = *tC->newScrollX;
        tC->oldScrollY = *tC->newScrollY;

        temp = tC->tileBase + (((tC->oldScrollY + *scanlineY)&0xf8)<<2);
        for (i=0;i<21;i++)
        {
            tC->tile[i] = *(temp + (((tC->oldScrollX>>3) + i)&31));
            line = (*tC->newScrollY + *scanlineY)&7;
            tC->palettes[i] = ((bw) ? tC->palette + ((tC->tile[i] & 0x2000) ? 4 : 0)
                                       : tC->palette + ((tC->tile[i]>>7) & 0x3C));
            tC->tilept[i] = (unsigned short *)(pattern_table + ((tC->tile[i] & 0x01ff)<<4));
            tC->tilept[i]+= ((tC->tile[i]&0x4000) ? 7-line : line);
        }
    }
}

void graphicsBlitLine(unsigned char render)
{
Libretro-Admin's avatar
Libretro-Admin committed
1022
1023
1024
#if 0
    dbg_print("in graphicsBlitLine *scanlineY = %d\n", *scanlineY);
#endif
Ed Mandy's avatar
Ed Mandy committed
1025
1026
1027
1028

    if (*scanlineY < 152)
    {
        //if(*scanlineY == 0)  //Flavor moved set_palette off of every line.  Helps speed, hurts hi-color apps
Libretro-Admin's avatar
Libretro-Admin committed
1029
        /* Flavor now only changes palette when dirty */
Ed Mandy's avatar
Ed Mandy committed
1030

Libretro-Admin's avatar
Libretro-Admin committed
1031
        /* this is currently broken for BW games */
Ed Mandy's avatar
Ed Mandy committed
1032
1033
1034
        //if(*scanlineY == 0)


Libretro-Admin's avatar
Libretro-Admin committed
1035
1036
1037
        /* set the palettes, background color, and outside window color
         * Flavor moving to scanline 0 set_palette(palette_table,&palettes[0],&palettes[16*4],&palettes[16*4+16*4]);
         * sort sprites by priority */
Ed Mandy's avatar
Ed Mandy committed
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
        if(render)
        {
            unsigned int bw = (m_emuInfo.machine == NGP);
            unsigned short OOWCol = NGPC_TO_SDL16(oowTable[*oowSelect & 0x07]);

            if(*scanlineY == 0)
            {
                if(bw)
                    set_paletteBW(palette_table,&palettes[0],&palettes[16*4],&palettes[16*4+16*4]);
                else
                    set_paletteCol(palette_table,&palettes[0],&palettes[16*4],&palettes[16*4+16*4]);

                /*if(spritesDirty)
                {
                    spriteSortAll(bw);
                    spritesDirty = false;
                }*/
                spriteSortAll(bw);
            }

Libretro-Admin's avatar
Libretro-Admin committed
1058
1059
1060
#if 0
            spriteSort(bw);  /* this needs to be re-done faster.  We shouldn't need to sort them every scanline */
#endif
Ed Mandy's avatar
Ed Mandy committed
1061

Libretro-Admin's avatar
Libretro-Admin committed
1062
            /* change the tile caches if needed */
Ed Mandy's avatar
Ed Mandy committed
1063
1064
1065
            RenderTileCache(&tCBack, bw);
            RenderTileCache(&tCFront, bw);

Libretro-Admin's avatar
Libretro-Admin committed
1066
            /* blit the planes, take priority registers into account */
Ed Mandy's avatar
Ed Mandy committed
1067
1068
1069
1070

            if(*bgSelect & 0x80) /*== 0x80)*/
                lineClear(&tCBack, NGPC_TO_SDL16(bgTable[*bgSelect & 0x07]));
            else if(bw)
Libretro-Admin's avatar
Libretro-Admin committed
1071
                lineClear(&tCBack, NGPC_TO_SDL16(bwTable[0]));  /* in 8-bit mode, this would be the index of BGCol in the SDL palette */
Ed Mandy's avatar
Ed Mandy committed
1072
            else
Libretro-Admin's avatar
Libretro-Admin committed
1073
                lineClear(&tCBack, 0);  /* in 8-bit mode, this would be the index of BGCol in the SDL palette */
Ed Mandy's avatar
Ed Mandy committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093


            lineSprite(&spriteDefs[0]);

            if (*frame1Pri & 0x80)
            {
                lineFront(&tCFront);
                lineSprite(&spriteDefs[1]);
                lineFront(&tCBack);
            }
            else
            {
                lineFront(&tCBack);
                lineSprite(&spriteDefs[1]);
                lineFront(&tCFront);
            }

            lineSprite(&spriteDefs[2]);


Libretro-Admin's avatar
Libretro-Admin committed
1094
            /* clip left and right sides of screen if necessary */
Ed Mandy's avatar
Ed Mandy committed
1095
1096
1097
1098
            clipLeftRight(&spriteDefs[2], OOWCol);

            if (*wndTopLeftY > *scanlineY || *scanlineY >= (*wndTopLeftY + *wndSizeY))
            {
Libretro-Admin's avatar
Libretro-Admin committed
1099
1100
1101
1102
1103
1104
1105
#if 0
                tCBack.gbp  -= SIZEX;  /* Flavor, I don't get why these were here */
#endif
                lineClear(&tCBack, OOWCol);  /* in 8-bit mode, this would be the index of OOWCol in the SDL palette */
#if 0
                tCBack.gbp  += SIZEX;  /* Flavor, I don't get why these were here */
#endif
Ed Mandy's avatar
Ed Mandy committed
1106
1107
1108
            }
        }

Libretro-Admin's avatar
Libretro-Admin committed
1109
        /* increase scanline count */
Ed Mandy's avatar
Ed Mandy committed
1110
1111
1112
1113
1114
        tCFront.gbp+= SIZEX;
        tCBack.gbp+= SIZEX;
        spriteDefs[0].gbp+= SIZEX;
        spriteDefs[1].gbp+= SIZEX;
        spriteDefs[2].gbp+= SIZEX;
Libretro-Admin's avatar
Libretro-Admin committed
1115
1116
1117
#if 0
        spriteDefs[3].gbp+= SIZEX;
#endif
Ed Mandy's avatar
Ed Mandy committed
1118
1119
1120

        if (*scanlineY == 151)
        {
Libretro-Admin's avatar
Libretro-Admin committed
1121
            /* start VBlank period */
Ed Mandy's avatar
Ed Mandy committed
1122
1123
            tlcsMemWriteB(0x00008010,tlcsMemReadB(0x00008010) | 0x40);
            if(render)
Libretro-Admin's avatar
Libretro-Admin committed
1124
                graphics_paint(); /* displayDirty = 1; */
Ed Mandy's avatar
Ed Mandy committed
1125
1126
1127
1128
1129
1130
        }

        *scanlineY+= 1;
    }
    else if (*scanlineY == 198)
    {
Libretro-Admin's avatar
Libretro-Admin committed
1131
        /* stop VBlank period */
Ed Mandy's avatar
Ed Mandy committed
1132
1133
1134
        tlcsMemWriteB(0x00008010,tlcsMemReadB(0x00008010) & ~0x40);
        graphicsBlitInit();

1135
#if defined(DO_PERIODIC_FLASH_SAVES)
Ed Mandy's avatar
Ed Mandy committed
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
        incFrameCount();
#endif

        *scanlineY = 0;
    }
    else
        *scanlineY+= 1;

}

Libretro-Admin's avatar
Libretro-Admin committed
1146
1147
1148
/*
 * THOR'S GRAPHIC CORE
 */
Ed Mandy's avatar
Ed Mandy committed
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180

typedef struct
{
	unsigned char flip;
	unsigned char x;
	unsigned char pal;
} MYSPRITE;

typedef struct
{
	unsigned short tile;
	unsigned char id;
} MYSPRITEREF;

typedef struct
{
	unsigned char count[152];
	MYSPRITEREF refs[152][64];
} MYSPRITEDEF;

MYSPRITEDEF mySprPri40,mySprPri80,mySprPriC0;
MYSPRITE mySprites[64];
unsigned short myPalettes[192];

void sortSprites(unsigned int bw)
{
    unsigned int spriteCode;
    unsigned char x, y, prevx=0, prevy=0;
    unsigned int i, j;
    unsigned short tile;
    MYSPRITEREF *spr;

Libretro-Admin's avatar
Libretro-Admin committed
1181
    /* initialize the number of sprites in each structure */
Ed Mandy's avatar
Ed Mandy committed
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
    memset(mySprPri40.count, 0, 152);
    memset(mySprPri80.count, 0, 152);
    memset(mySprPriC0.count, 0, 152);

    for (i=0;i<64;i++)
    {
        spriteCode = *((unsigned short *)(sprite_table+4*i));

        prevx = (spriteCode & 0x0400 ? prevx : 0) + *(sprite_table+4*i+2);
        x = prevx + *scrollSpriteX;

        prevy = (spriteCode & 0x0200 ? prevy : 0) + *(sprite_table+4*i+3);
        y = prevy + *scrollSpriteY;

        if ((x>167 && x<249) || (y>151 && y<249) || (spriteCode<=0xff) || ((spriteCode & 0x1800)==0))
            continue;

		mySprites[i].x = x;
		mySprites[i].pal = bw ? ((spriteCode>>11)&0x04) : ((sprite_palette_numbers[i]&0xf)<<2);
		mySprites[i].flip = spriteCode>>8;
		tile = (spriteCode & 0x01ff)<<3;

        for (j = 0; j < 8; ++j,++y)
        {
        	if (y>151)
        		continue;
            switch (spriteCode & 0x1800)
            {
                case 0x1800:
                spr = &mySprPriC0.refs[y][mySprPriC0.count[y]++];
                break;
                case 0x1000:
                spr = &mySprPri80.refs[y][mySprPri80.count[y]++];
                break;
                case 0x0800:
                spr = &mySprPri40.refs[y][mySprPri40.count[y]++];
                break;
                default: continue;
            }
            spr->id = i;
            spr->tile = tile + (spriteCode&0x4000 ? 7-j : j);
        }
    }
}

void drawSprites(unsigned short* draw,
				 MYSPRITEREF *sprites,int count,
				 int x0,int x1)
{
	unsigned short*pal;
	unsigned int pattern,pix,cnt;
	MYSPRITE *spr;
	int i,cx;

	for (i=count-1;i>=0;--i)
	{
		pattern = patterns[sprites[i].tile];
		if (pattern==0)
			continue;

		spr = &mySprites[sprites[i].id];

		if (spr->x>248)
			cx = spr->x-256;
        else
			cx = spr->x;

		if (cx+8<=x0 || cx>=x1)
			continue;

		pal = &myPalettes[spr->pal];

        if (cx<x0)
        {
			cnt = 8-(x0-cx);
			if (spr->flip&0x80)
			{
                pattern>>=((8-cnt)<<1);
                for (cx=x0;pattern;++cx)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[cx] = pal[pix];
                    pattern>>=2;
                }
			}
			else
			{
                pattern &= (0xffff>>((8-cnt)<<1));
                for (cx = x0+cnt-1;pattern;--cx)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[cx] = pal[pix];
                    pattern>>=2;
                }
			}
        }
        else if (cx+7<x1)
        {
			if (spr->flip&0x80)
			{
                for (;pattern;++cx)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[cx] = pal[pix];
                    pattern>>=2;
                }
			}
			else
			{
                for (cx+=7;pattern;--cx)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[cx] = pal[pix];
                    pattern>>=2;
                }
			}
        }
        else
        {
			cnt = x1-cx;
			if (spr->flip&0x80)
			{
                pattern &= (0xffff>>((8-cnt)<<1));
                for (;pattern;++cx)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[cx] = pal[pix];
                    pattern>>=2;
                }
			}
			else
			{
                pattern>>=((8-cnt)<<1);
                for (cx+=cnt-1;pattern;--cx)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[cx] = pal[pix];
                    pattern>>=2;
                }
			}
        }
	}
}

void drawScrollPlane(unsigned short* draw,
					 unsigned short* tile_table,int scrpal,
					 unsigned char dx,unsigned char dy,
					 int x0,int x1,unsigned int bw)
{
	unsigned short*tiles;
	unsigned short*pal;
	unsigned int pattern;
	unsigned int j,count,pix,idy,tile;
	int i,x2;

	dx+=x0;
	tiles = tile_table+((dy>>3)<<5)+(dx>>3);

	count = 8-(dx&0x7);
	dx &= 0xf8;
	dy &= 0x7;
	idy = 7-dy;

	i = x0;

    if (count<8)
    {
		tile = *(tiles++);
		pattern = patterns[(((tile&0x1ff))<<3) + (tile&0x4000 ? idy:dy)];
		if (pattern)
		{
			pal = &myPalettes[scrpal + (bw ? (tile&0x2000 ? 4 : 0) : ((tile>>7)&0x3c))];
			if (tile&0x8000)
			{
                pattern>>=((8-count)<<1);
                for (j=i;pattern;++j)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[j] = pal[pix];
                    pattern>>=2;
                }
			}
			else
			{
                pattern &= (0xffff>>((8-count)<<1));
                for (j=i+count-1;pattern;--j)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[j] = pal[pix];
                    pattern>>=2;
                }
			}
		}
        i+=count;
		dx+=8;
		if (dx==0)
			tiles-=32;
    }

    x2 = i+((x1-i)&0xf8);

	for (;i<x2;i+=8)
	{
		tile = *(tiles++);
		pattern = patterns[(((tile&0x1ff))<<3) + (tile&0x4000 ? idy:dy)];
		if (pattern)
		{
			pal = &myPalettes[scrpal + (bw ? (tile&0x2000 ? 4 : 0) : ((tile>>7)&0x3c))];
			if (tile&0x8000)
			{
                for (j=i;pattern;++j)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[j] = pal[pix];
                    pattern>>=2;
                }
			}
			else
			{
                for (j=i+7;pattern;--j)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[j] = pal[pix];
                    pattern>>=2;
                }
			}
		}
		dx+=8;
		if (dx==0)
			tiles-=32;
	}

	if (x2!=x1)
	{
        count = x1-x2;
		tile = *(tiles++);
		pattern = patterns[(((tile&0x1ff))<<3) + (tile&0x4000 ? idy:dy)];
		if (pattern)
		{
			pal = &myPalettes[scrpal + (bw ? (tile&0x2000 ? 4 : 0) : ((tile>>7)&0x3c))];
			if (tile&0x8000)
			{
                pattern &= (0xffff>>((8-count)<<1));
                for (j=i;pattern;++j)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[j] = pal[pix];
                    pattern>>=2;
                }
			}
			else
			{
			    pattern>>=((8-count)<<1);
                for (j=i+count-1;pattern;--j)
                {
                    pix = pattern & 0x3;
                    if (pix)
                        draw[j] = pal[pix];
                    pattern>>=2;
                }
			}
		}
	}
}

Libretro-Admin's avatar
Libretro-Admin committed
1458
void myGraphicsBlitLine(unsigned char render)  /* NOTA */
Ed Mandy's avatar
Ed Mandy committed
1459
1460
1461
1462
1463
1464
{
	int i,x0,x1;
    if (*scanlineY < 152)
    {
        if(render)
        {
Libretro-Admin's avatar
Libretro-Admin committed
1465
#if __LIBRETRO__
's avatar
committed
1466
			unsigned short* draw = &drawBuffer[(*scanlineY)*(screen->w)];
Ed Mandy's avatar
Ed Mandy committed
1467
#else
Libretro-Admin's avatar
Libretro-Admin committed
1468
			unsigned short* draw = &drawBuffer[(*scanlineY)*(screen->w+PSP_FUDGE)];/* extra fudge factor for PSP??? */
Ed Mandy's avatar
Ed Mandy committed
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
#endif
			unsigned short bgcol;
            unsigned int bw = (m_emuInfo.machine == NGP);
            unsigned short OOWCol = NGPC_TO_SDL16(oowTable[*oowSelect & 0x07]);
            unsigned short* pal;
            unsigned short* mempal;

			if (*scanlineY==0)
				sortSprites(bw);
			if (*scanlineY<*wndTopLeftY || *scanlineY>*wndTopLeftY+*wndSizeY || *wndSizeX==0 || *wndSizeY==0)
			{

				for (i=0;i<NGPC_SIZEX;i++)
					draw[i] = OOWCol;
			}
			else
			{

's avatar
committed
1487
#ifdef __LIBRETRO__
Ed Mandy's avatar
Ed Mandy committed
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
				if (((*scanlineY)&7) == 0)
#else
				if (options[HICOLOR_OPTION] || (!options[HICOLOR_OPTION] && ((*scanlineY)&7)==0))
#endif
				{
		            if (bw)
    		        {
        		        for(i=0;i<4;i++)
            		    {
	                	    myPalettes[i]     = NGPC_TO_SDL16(bwTable[bw_palette_table[i]    & 0x07]);
		                    myPalettes[4+i]   = NGPC_TO_SDL16(bwTable[bw_palette_table[4+i]  & 0x07]);
    		                myPalettes[64+i]  = NGPC_TO_SDL16(bwTable[bw_palette_table[8+i]  & 0x07]);
        		            myPalettes[68+i]  = NGPC_TO_SDL16(bwTable[bw_palette_table[12+i] & 0x07]);
            		        myPalettes[128+i] = NGPC_TO_SDL16(bwTable[bw_palette_table[16+i] & 0x07]);
                		    myPalettes[132+i] = NGPC_TO_SDL16(bwTable[bw_palette_table[20+i] & 0x07]);
		                }
    		        }
        		    else
					{
						pal = myPalettes;
						mempal = palette_table;

						for (i=0;i<192;i++)
							*(pal++) = NGPC_TO_SDL16(*(mempal++));
        		    }
				}


	            if(*bgSelect & 0x80)
    	            bgcol = NGPC_TO_SDL16(bgTable[*bgSelect & 0x07]);
        	    else if(bw)
	                bgcol = NGPC_TO_SDL16(bwTable[0]);
    	        else
Libretro-Admin's avatar
Libretro-Admin committed
1521
        	        bgcol = NGPC_TO_SDL16(bgTable[0]); /* maybe 0xFFF? */
Ed Mandy's avatar
Ed Mandy committed
1522
        	        
Libretro-Admin's avatar
Libretro-Admin committed
1523
        	    /* NOTA arregla algo en m pure tlcsMemWriteB(0x83E0, 0xFF);     */
Ed Mandy's avatar
Ed Mandy committed
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
        	        

				x0 = *wndTopLeftX;
				x1 = x0+*wndSizeX;
				if (x1>NGPC_SIZEX)
					x1 = NGPC_SIZEX;

				for (i=x0;i<x1;i++)
					draw[i] = bgcol;

				if (mySprPri40.count[*scanlineY])
					drawSprites(draw,mySprPri40.refs[*scanlineY],mySprPri40.count[*scanlineY],x0,x1);

	            if (*frame1Pri & 0x80)
	            {
        			drawScrollPlane(draw,tile_table_front,64,*scrollFrontX,*scrollFrontY+*scanlineY,x0,x1,bw);
	            	if (mySprPri80.count[*scanlineY])
						drawSprites(draw,mySprPri80.refs[*scanlineY],mySprPri80.count[*scanlineY],x0,x1);
		        	 
Libretro-Admin's avatar
Libretro-Admin committed
1543
		        	/* NOTA  Wrestling Madness && Big Bang Pro Wrestling */
Ed Mandy's avatar
Ed Mandy committed
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
		        	if (mainrom[0x000020] != 0x66)
		        	drawScrollPlane(draw,tile_table_back,128,*scrollBackX,*scrollBackY+*scanlineY,x0,x1,bw);
		        	
		        	else{
		        	if (*scrollBackY > 0)
		        	drawScrollPlane(draw,tile_table_back,128,*scrollBackX,*scrollBackY+*scanlineY,x0,x1,bw);
		        	else 
		        	drawScrollPlane(draw,tile_table_back,128,1,*scrollBackY+*scanlineY,x0,x1,bw);}
	            
	            }
	            else
	            {
		        	drawScrollPlane(draw,tile_table_back,128,*scrollBackX,*scrollBackY+*scanlineY,x0,x1,bw);
					if (mySprPri80.count[*scanlineY])
						drawSprites(draw,mySprPri80.refs[*scanlineY],mySprPri80.count[*scanlineY],x0,x1);
	    	    	drawScrollPlane(draw,tile_table_front,64,*scrollFrontX,*scrollFrontY+*scanlineY,x0,x1,bw);
	            }

				if (mySprPriC0.count[*scanlineY])
					drawSprites(draw,mySprPriC0.refs[*scanlineY],mySprPriC0.count[*scanlineY],x0,x1);

				for (i=0;i<x0;i++)
					draw[i] = OOWCol;
				for (i=x1;i<NGPC_SIZEX;i++)
					draw[i] = OOWCol;

	        }
        }
        if (*scanlineY == 151)
        {
Libretro-Admin's avatar
Libretro-Admin committed
1574
            /* start VBlank period */
Ed Mandy's avatar
Ed Mandy committed
1575
1576
1577
1578
1579
1580
1581
1582
            tlcsMemWriteB(0x00008010,tlcsMemReadB(0x00008010) | 0x40);
            if (render)
                graphics_paint();
        }
        *scanlineY+= 1;
    }
    else if (*scanlineY == 198)
    {
Libretro-Admin's avatar
Libretro-Admin committed
1583
        /* stop VBlank period */
Ed Mandy's avatar
Ed Mandy committed
1584
1585
1586
1587
1588
1589
1590
1591
1592
        tlcsMemWriteB(0x00008010,tlcsMemReadB(0x00008010) & ~0x40);

        *scanlineY = 0;
    }
    else
        *scanlineY+= 1;
}


Libretro-Admin's avatar
Libretro-Admin committed
1593
1594
1595
1596
1597
/*
 *
 * General Routines
 *
 */
Ed Mandy's avatar
Ed Mandy committed
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607

const char ErrDirectDrawCreate[] = "DirectDrawCreate Failed";
const char ErrQueryInterface[]  = "QueryInterface Failed";
const char ErrSetCooperativeLevel[] = "SetCooperativeLevel Failed";
const char ErrCreateSurface[]  = "CreateSurface Failed";

#define DDOK(x) if (hRet != DD_OK) { LastErr = x; return FALSE; }

BOOL graphics_init(HWND phWnd)
{
's avatar
committed
1608
#ifdef __LIBRETRO__
Ed Mandy's avatar
Ed Mandy committed
1609
    palette_init = palette_init16;
frangarcj's avatar
frangarcj committed
1610
    palette_init(0xf800,0x7e0,0x1f);
's avatar
committed
1611
    drawBuffer = (unsigned short*)screen->pixels;
Ed Mandy's avatar
Ed Mandy committed
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
#else
    dbg_print("in graphics_init\n");


    switch (screen->format->BitsPerPixel)
    {
        case 8:
        palette_init = palette_init8;
        break;
        case 16:
        palette_init = palette_init16;
        break;
        case 32:
        palette_init = palette_init32;
        break;
    }

Libretro-Admin's avatar
Libretro-Admin committed
1629
1630
1631
#if 0
    palettes = palette_table;
#endif
Ed Mandy's avatar
Ed Mandy committed
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
    drawBuffer = ((unsigned short*) screen->pixels) + SCREEN_OFFET;

    palette_init(screen->format->Rmask,
                 screen->format->Gmask, screen->format->Bmask);

    pngpalette_init();
#endif

    switch(m_emuInfo.machine)
    {
        case NGP:
            bgTable  = (unsigned short *)bwTable;
            oowTable = (unsigned short *)bwTable;
Libretro-Admin's avatar
Libretro-Admin committed
1645
1646
1647
#if 0
            set_palette = set_paletteBW;
#endif
Ed Mandy's avatar
Ed Mandy committed
1648
1649
1650
1651
1652
1653
            graphicsBlitInit();
            *scanlineY = 0;
            break;
        case NGPC:
			bgTable  = (unsigned short *)get_address(0x000083E0);
			oowTable  = (unsigned short *)get_address(0x000083F0);
Libretro-Admin's avatar
Libretro-Admin committed
1654
1655
1656
1657
1658
1659
#if 0
         set_palette = set_paletteCol;
#endif
         graphicsBlitInit();
         *scanlineY = 0;
         break;
Ed Mandy's avatar
Ed Mandy committed
1660
1661
1662
1663
    }
    return TRUE;
}

1664
1665
1666
void graphics_cleanup(void)
{
}
Ed Mandy's avatar
Ed Mandy committed
1667