diff src/gba/GBAGfx.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/gba/GBAGfx.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,1791 @@
     1.4 +#ifndef VBA_GBA_GFX_H
     1.5 +#define VBA_GBA_GFX_H
     1.6 +
     1.7 +#if _MSC_VER > 1000
     1.8 +#pragma once
     1.9 +#endif // _MSC_VER > 1000
    1.10 +
    1.11 +#include "../Port.h"
    1.12 +#include "GBA.h"
    1.13 +#include "GBAGlobals.h"
    1.14 +
    1.15 +//#define SPRITE_DEBUG
    1.16 +
    1.17 +void gfxDrawTextScreen(u16, u16, u16, u32 *);
    1.18 +void gfxDrawRotScreen(u16,
    1.19 +                      u16, u16,
    1.20 +                      u16, u16,
    1.21 +                      u16, u16,
    1.22 +                      u16, u16,
    1.23 +                      int&, int&,
    1.24 +                      int,
    1.25 +                      u32 *);
    1.26 +void gfxDrawRotScreen16Bit(u16,
    1.27 +                           u16, u16,
    1.28 +                           u16, u16,
    1.29 +                           u16, u16,
    1.30 +                           u16, u16,
    1.31 +                           int&, int&,
    1.32 +                           int,
    1.33 +                           u32 *);
    1.34 +void gfxDrawRotScreen256(u16,
    1.35 +                         u16, u16,
    1.36 +                         u16, u16,
    1.37 +                         u16, u16,
    1.38 +                         u16, u16,
    1.39 +                         int&, int&,
    1.40 +                         int,
    1.41 +                         u32 *);
    1.42 +void gfxDrawRotScreen16Bit160(u16,
    1.43 +                              u16, u16,
    1.44 +                              u16, u16,
    1.45 +                              u16, u16,
    1.46 +                              u16, u16,
    1.47 +                              int&, int&,
    1.48 +                              int,
    1.49 +                              u32 *);
    1.50 +void gfxDrawSprites(u32 *);
    1.51 +void gfxIncreaseBrightness(u32 *line, int coeff);
    1.52 +void gfxDecreaseBrightness(u32 *line, int coeff);
    1.53 +void gfxAlphaBlend(u32 *ta, u32 *tb, int ca, int cb);
    1.54 +
    1.55 +void mode0RenderLine();
    1.56 +void mode0RenderLineNoWindow();
    1.57 +void mode0RenderLineAll();
    1.58 +
    1.59 +void mode1RenderLine();
    1.60 +void mode1RenderLineNoWindow();
    1.61 +void mode1RenderLineAll();
    1.62 +
    1.63 +void mode2RenderLine();
    1.64 +void mode2RenderLineNoWindow();
    1.65 +void mode2RenderLineAll();
    1.66 +
    1.67 +void mode3RenderLine();
    1.68 +void mode3RenderLineNoWindow();
    1.69 +void mode3RenderLineAll();
    1.70 +
    1.71 +void mode4RenderLine();
    1.72 +void mode4RenderLineNoWindow();
    1.73 +void mode4RenderLineAll();
    1.74 +
    1.75 +void mode5RenderLine();
    1.76 +void mode5RenderLineNoWindow();
    1.77 +void mode5RenderLineAll();
    1.78 +
    1.79 +extern int  coeff[32];
    1.80 +
    1.81 +#define LINE_BUFFER_OVERFLOW_LEEWAY (512-240)
    1.82 +
    1.83 +extern u32  line0[240+LINE_BUFFER_OVERFLOW_LEEWAY];
    1.84 +extern u32  line1[240+LINE_BUFFER_OVERFLOW_LEEWAY];
    1.85 +extern u32  line2[240+LINE_BUFFER_OVERFLOW_LEEWAY];
    1.86 +extern u32  line3[240+LINE_BUFFER_OVERFLOW_LEEWAY];
    1.87 +extern u32  lineOBJ[240+LINE_BUFFER_OVERFLOW_LEEWAY];
    1.88 +extern u32  lineOBJWin[240+LINE_BUFFER_OVERFLOW_LEEWAY];
    1.89 +extern u32  lineMix[240+LINE_BUFFER_OVERFLOW_LEEWAY];
    1.90 +extern bool gfxInWin0[240+LINE_BUFFER_OVERFLOW_LEEWAY];
    1.91 +extern bool gfxInWin1[240+LINE_BUFFER_OVERFLOW_LEEWAY];
    1.92 +
    1.93 +extern int gfxBG2Changed;
    1.94 +extern int gfxBG3Changed;
    1.95 +
    1.96 +extern int gfxBG2X;
    1.97 +extern int gfxBG2Y;
    1.98 +extern int gfxBG2LastX;
    1.99 +extern int gfxBG2LastY;
   1.100 +extern int gfxBG3X;
   1.101 +extern int gfxBG3Y;
   1.102 +extern int gfxBG3LastX;
   1.103 +extern int gfxBG3LastY;
   1.104 +extern int gfxLastVCOUNT;
   1.105 +
   1.106 +inline void gfxClearArray(u32 *array)
   1.107 +{
   1.108 +	for (int i = 0; i < 240; i++)
   1.109 +	{
   1.110 +		*array++ = 0x80000000;
   1.111 +	}
   1.112 +}
   1.113 +
   1.114 +inline void gfxDrawTextScreen(u16 control, u16 hofs, u16 vofs,
   1.115 +                              u32 *line)
   1.116 +{
   1.117 +	u16 *palette    = (u16 *)paletteRAM;
   1.118 +	u8 * charBase   = &vram[((control >> 2) & 0x03) * 0x4000];
   1.119 +	u16 *screenBase = (u16 *)&vram[((control >> 8) & 0x1f) * 0x800];
   1.120 +	u32  prio       = ((control & 3)<<25) + 0x1000000;
   1.121 +	int  sizeX      = 256;
   1.122 +	int  sizeY      = 256;
   1.123 +	switch ((control >> 14) & 3)
   1.124 +	{
   1.125 +	case 0:
   1.126 +		break;
   1.127 +	case 1:
   1.128 +		sizeX = 512;
   1.129 +		break;
   1.130 +	case 2:
   1.131 +		sizeY = 512;
   1.132 +		break;
   1.133 +	case 3:
   1.134 +		sizeX = 512;
   1.135 +		sizeY = 512;
   1.136 +		break;
   1.137 +	}
   1.138 +
   1.139 +	int maskX = sizeX-1;
   1.140 +	int maskY = sizeY-1;
   1.141 +
   1.142 +	bool mosaicOn = (control & 0x40) ? true : false;
   1.143 +
   1.144 +	int xxx     = hofs & maskX;
   1.145 +	int yyy     = (vofs + VCOUNT) & maskY;
   1.146 +	int mosaicX = (MOSAIC & 0x000F)+1;
   1.147 +	int mosaicY = ((MOSAIC & 0x00F0)>>4)+1;
   1.148 +
   1.149 +	if (mosaicOn)
   1.150 +	{
   1.151 +		if ((VCOUNT % mosaicY) != 0)
   1.152 +		{
   1.153 +			mosaicY = (VCOUNT / mosaicY) * mosaicY;
   1.154 +			yyy     = (vofs + mosaicY) & maskY;
   1.155 +		}
   1.156 +	}
   1.157 +
   1.158 +	if (yyy > 255 && sizeY > 256)
   1.159 +	{
   1.160 +		yyy        &= 255;
   1.161 +		screenBase += 0x400;
   1.162 +		if (sizeX > 256)
   1.163 +			screenBase += 0x400;
   1.164 +	}
   1.165 +
   1.166 +	int yshift = ((yyy>>3)<<5);
   1.167 +	if ((control) & 0x80)
   1.168 +	{
   1.169 +		u16 *screenSource = screenBase + 0x400 * (xxx>>8) + ((xxx & 255)>>3) + yshift;
   1.170 +		for (int x = 0; x < 240; x++)
   1.171 +		{
   1.172 +			u16 data = READ16LE(screenSource);
   1.173 +
   1.174 +			int tile  = data & 0x3FF;
   1.175 +			int tileX = (xxx & 7);
   1.176 +			int tileY = yyy & 7;
   1.177 +
   1.178 +			if (data & 0x0400)
   1.179 +				tileX = 7 - tileX;
   1.180 +			if (data & 0x0800)
   1.181 +				tileY = 7 - tileY;
   1.182 +
   1.183 +			u8 color = charBase[tile * 64 + tileY * 8 + tileX];
   1.184 +
   1.185 +			line[x] = color ? (READ16LE(&palette[color]) | prio) : 0x80000000;
   1.186 +
   1.187 +			if (data & 0x0400)
   1.188 +			{
   1.189 +				if (tileX == 0)
   1.190 +					screenSource++;
   1.191 +			}
   1.192 +			else if (tileX == 7)
   1.193 +				screenSource++;
   1.194 +			xxx++;
   1.195 +			if (xxx == 256)
   1.196 +			{
   1.197 +				if (sizeX > 256)
   1.198 +					screenSource = screenBase + 0x400 + yshift;
   1.199 +				else
   1.200 +				{
   1.201 +					screenSource = screenBase + yshift;
   1.202 +					xxx = 0;
   1.203 +				}
   1.204 +			}
   1.205 +			else if (xxx >= sizeX)
   1.206 +			{
   1.207 +				xxx = 0;
   1.208 +				screenSource = screenBase + yshift;
   1.209 +			}
   1.210 +		}
   1.211 +	}
   1.212 +	else
   1.213 +	{
   1.214 +		u16 *screenSource = screenBase + 0x400*(xxx>>8)+((xxx&255)>>3) +
   1.215 +		                    yshift;
   1.216 +		for (int x = 0; x < 240; x++)
   1.217 +		{
   1.218 +			u16 data = READ16LE(screenSource);
   1.219 +
   1.220 +			int tile  = data & 0x3FF;
   1.221 +			int tileX = (xxx & 7);
   1.222 +			int tileY = yyy & 7;
   1.223 +
   1.224 +			if (data & 0x0400)
   1.225 +				tileX = 7 - tileX;
   1.226 +			if (data & 0x0800)
   1.227 +				tileY = 7 - tileY;
   1.228 +
   1.229 +			u8 color = charBase[(tile<<5) + (tileY<<2) + (tileX>>1)];
   1.230 +
   1.231 +			if (tileX & 1)
   1.232 +			{
   1.233 +				color = (color >> 4);
   1.234 +			}
   1.235 +			else
   1.236 +			{
   1.237 +				color &= 0x0F;
   1.238 +			}
   1.239 +
   1.240 +			int pal = (READ16LE(screenSource)>>8) & 0xF0;
   1.241 +			line[x] = color ? (READ16LE(&palette[pal + color])|prio) : 0x80000000;
   1.242 +
   1.243 +			if (data & 0x0400)
   1.244 +			{
   1.245 +				if (tileX == 0)
   1.246 +					screenSource++;
   1.247 +			}
   1.248 +			else if (tileX == 7)
   1.249 +				screenSource++;
   1.250 +			xxx++;
   1.251 +			if (xxx == 256)
   1.252 +			{
   1.253 +				if (sizeX > 256)
   1.254 +					screenSource = screenBase + 0x400 + yshift;
   1.255 +				else
   1.256 +				{
   1.257 +					screenSource = screenBase + yshift;
   1.258 +					xxx = 0;
   1.259 +				}
   1.260 +			}
   1.261 +			else if (xxx >= sizeX)
   1.262 +			{
   1.263 +				xxx = 0;
   1.264 +				screenSource = screenBase + yshift;
   1.265 +			}
   1.266 +		}
   1.267 +	}
   1.268 +	if (mosaicOn)
   1.269 +	{
   1.270 +		if (mosaicX > 1)
   1.271 +		{
   1.272 +			int m = 1;
   1.273 +			for (int i = 0; i < 239; i++)
   1.274 +			{
   1.275 +				line[i+1] = line[i];
   1.276 +				m++;
   1.277 +				if (m == mosaicX)
   1.278 +				{
   1.279 +					m = 1;
   1.280 +					i++;
   1.281 +				}
   1.282 +			}
   1.283 +		}
   1.284 +	}
   1.285 +}
   1.286 +
   1.287 +inline void gfxDrawRotScreen(u16 control,
   1.288 +                             u16 x_l, u16 x_h,
   1.289 +                             u16 y_l, u16 y_h,
   1.290 +                             u16 pa,  u16 pb,
   1.291 +                             u16 pc,  u16 pd,
   1.292 +                             int& currentX, int& currentY,
   1.293 +                             int changed,
   1.294 +                             u32 *line)
   1.295 +{
   1.296 +	u16 *palette    = (u16 *)paletteRAM;
   1.297 +	u8 * charBase   = &vram[((control >> 2) & 0x03) * 0x4000];
   1.298 +	u8 * screenBase = (u8 *)&vram[((control >> 8) & 0x1f) * 0x800];
   1.299 +	int  prio       = ((control & 3) << 25) + 0x1000000;
   1.300 +
   1.301 +	int sizeX = 128;
   1.302 +	int sizeY = 128;
   1.303 +	switch ((control >> 14) & 3)
   1.304 +	{
   1.305 +	case 0:
   1.306 +		break;
   1.307 +	case 1:
   1.308 +		sizeX = sizeY = 256;
   1.309 +		break;
   1.310 +	case 2:
   1.311 +		sizeX = sizeY = 512;
   1.312 +		break;
   1.313 +	case 3:
   1.314 +		sizeX = sizeY = 1024;
   1.315 +		break;
   1.316 +	}
   1.317 +
   1.318 +	int dx = pa & 0x7FFF;
   1.319 +	if (pa & 0x8000)
   1.320 +		dx |= 0xFFFF8000;
   1.321 +	int dmx = pb & 0x7FFF;
   1.322 +	if (pb & 0x8000)
   1.323 +		dmx |= 0xFFFF8000;
   1.324 +	int dy = pc & 0x7FFF;
   1.325 +	if (pc & 0x8000)
   1.326 +		dy |= 0xFFFF8000;
   1.327 +	int dmy = pd & 0x7FFFF;
   1.328 +	if (pd & 0x8000)
   1.329 +		dmy |= 0xFFFF8000;
   1.330 +
   1.331 +	if (VCOUNT == 0)
   1.332 +		changed = 3;
   1.333 +
   1.334 +	if (changed & 1)
   1.335 +	{
   1.336 +		currentX = (x_l) | ((x_h & 0x07FF)<<16);
   1.337 +		if (x_h & 0x0800)
   1.338 +			currentX |= 0xF8000000;
   1.339 +	}
   1.340 +	else
   1.341 +	{
   1.342 +		currentX += dmx;
   1.343 +	}
   1.344 +
   1.345 +	if (changed & 2)
   1.346 +	{
   1.347 +		currentY = (y_l) | ((y_h & 0x07FF)<<16);
   1.348 +		if (y_h & 0x0800)
   1.349 +			currentY |= 0xF8000000;
   1.350 +	}
   1.351 +	else
   1.352 +	{
   1.353 +		currentY += dmy;
   1.354 +	}
   1.355 +
   1.356 +	int realX = currentX;
   1.357 +	int realY = currentY;
   1.358 +
   1.359 +	if (control & 0x40)
   1.360 +	{
   1.361 +		int mosaicY = ((MOSAIC & 0xF0)>>4) + 1;
   1.362 +		int y       = (VCOUNT % mosaicY);
   1.363 +		realX -= y*dmx;
   1.364 +		realY -= y*dmy;
   1.365 +	}
   1.366 +
   1.367 +	int xxx = (realX >> 8);
   1.368 +	int yyy = (realY >> 8);
   1.369 +
   1.370 +	if (control & 0x2000)
   1.371 +	{
   1.372 +		xxx %= sizeX;
   1.373 +		yyy %= sizeY;
   1.374 +		if (xxx < 0)
   1.375 +			xxx += sizeX;
   1.376 +		if (yyy < 0)
   1.377 +			yyy += sizeY;
   1.378 +	}
   1.379 +
   1.380 +	if (control & 0x80)
   1.381 +	{
   1.382 +		for (int x = 0; x < 240; x++)
   1.383 +		{
   1.384 +			if (xxx < 0 ||
   1.385 +			    yyy < 0 ||
   1.386 +			    xxx >= sizeX ||
   1.387 +			    yyy >= sizeY)
   1.388 +			{
   1.389 +				line[x] = 0x80000000;
   1.390 +			}
   1.391 +			else
   1.392 +			{
   1.393 +				int tile = screenBase[(xxx>>3) + (yyy>>3)*(sizeX>>3)];
   1.394 +
   1.395 +				int tileX = (xxx & 7);
   1.396 +				int tileY = yyy & 7;
   1.397 +
   1.398 +				u8 color = charBase[(tile<<6) + (tileY<<3) + tileX];
   1.399 +
   1.400 +				line[x] = color ? (READ16LE(&palette[color])|prio) : 0x80000000;
   1.401 +			}
   1.402 +			realX += dx;
   1.403 +			realY += dy;
   1.404 +
   1.405 +			xxx = (realX >> 8);
   1.406 +			yyy = (realY >> 8);
   1.407 +
   1.408 +			if (control & 0x2000)
   1.409 +			{
   1.410 +				xxx %= sizeX;
   1.411 +				yyy %= sizeY;
   1.412 +				if (xxx < 0)
   1.413 +					xxx += sizeX;
   1.414 +				if (yyy < 0)
   1.415 +					yyy += sizeY;
   1.416 +			}
   1.417 +		}
   1.418 +	}
   1.419 +	else
   1.420 +	{
   1.421 +		for (int x = 0; x < 240; x++)
   1.422 +		{
   1.423 +			if (xxx < 0 ||
   1.424 +			    yyy < 0 ||
   1.425 +			    xxx >= sizeX ||
   1.426 +			    yyy >= sizeY)
   1.427 +			{
   1.428 +				line[x] = 0x80000000;
   1.429 +			}
   1.430 +			else
   1.431 +			{
   1.432 +				int tile = screenBase[(xxx>>3) + (yyy>>3)*(sizeX>>3)];
   1.433 +
   1.434 +				int tileX = (xxx & 7);
   1.435 +				int tileY = yyy & 7;
   1.436 +
   1.437 +				u8 color = charBase[(tile<<6) + (tileY<<3) + tileX];
   1.438 +
   1.439 +				line[x] = color ? (READ16LE(&palette[color])|prio) : 0x80000000;
   1.440 +			}
   1.441 +			realX += dx;
   1.442 +			realY += dy;
   1.443 +
   1.444 +			xxx = (realX >> 8);
   1.445 +			yyy = (realY >> 8);
   1.446 +
   1.447 +			if (control & 0x2000)
   1.448 +			{
   1.449 +				xxx %= sizeX;
   1.450 +				yyy %= sizeY;
   1.451 +				if (xxx < 0)
   1.452 +					xxx += sizeX;
   1.453 +				if (yyy < 0)
   1.454 +					yyy += sizeY;
   1.455 +			}
   1.456 +		}
   1.457 +	}
   1.458 +
   1.459 +	if (control & 0x40)
   1.460 +	{
   1.461 +		int mosaicX = (MOSAIC & 0xF) + 1;
   1.462 +		if (mosaicX > 1)
   1.463 +		{
   1.464 +			int m = 1;
   1.465 +			for (int i = 0; i < 239; i++)
   1.466 +			{
   1.467 +				line[i+1] = line[i];
   1.468 +				m++;
   1.469 +				if (m == mosaicX)
   1.470 +				{
   1.471 +					m = 1;
   1.472 +					i++;
   1.473 +				}
   1.474 +			}
   1.475 +		}
   1.476 +	}
   1.477 +}
   1.478 +
   1.479 +inline void gfxDrawRotScreen16Bit(u16 control,
   1.480 +                                  u16 x_l, u16 x_h,
   1.481 +                                  u16 y_l, u16 y_h,
   1.482 +                                  u16 pa,  u16 pb,
   1.483 +                                  u16 pc,  u16 pd,
   1.484 +                                  int& currentX, int& currentY,
   1.485 +                                  int changed,
   1.486 +                                  u32 *line)
   1.487 +{
   1.488 +	u16 *screenBase = (u16 *)&vram[0];
   1.489 +	int  prio       = ((control & 3) << 25) + 0x1000000;
   1.490 +	int  sizeX      = 240;
   1.491 +	int  sizeY      = 160;
   1.492 +
   1.493 +	int startX = (x_l) | ((x_h & 0x07FF)<<16);
   1.494 +	if (x_h & 0x0800)
   1.495 +		startX |= 0xF8000000;
   1.496 +	int startY = (y_l) | ((y_h & 0x07FF)<<16);
   1.497 +	if (y_h & 0x0800)
   1.498 +		startY |= 0xF8000000;
   1.499 +
   1.500 +	int dx = pa & 0x7FFF;
   1.501 +	if (pa & 0x8000)
   1.502 +		dx |= 0xFFFF8000;
   1.503 +	int dmx = pb & 0x7FFF;
   1.504 +	if (pb & 0x8000)
   1.505 +		dmx |= 0xFFFF8000;
   1.506 +	int dy = pc & 0x7FFF;
   1.507 +	if (pc & 0x8000)
   1.508 +		dy |= 0xFFFF8000;
   1.509 +	int dmy = pd & 0x7FFFF;
   1.510 +	if (pd & 0x8000)
   1.511 +		dmy |= 0xFFFF8000;
   1.512 +
   1.513 +	if (VCOUNT == 0)
   1.514 +		changed = 3;
   1.515 +
   1.516 +	if (changed & 1)
   1.517 +	{
   1.518 +		currentX = (x_l) | ((x_h & 0x07FF)<<16);
   1.519 +		if (x_h & 0x0800)
   1.520 +			currentX |= 0xF8000000;
   1.521 +	}
   1.522 +	else
   1.523 +		currentX += dmx;
   1.524 +
   1.525 +	if (changed & 2)
   1.526 +	{
   1.527 +		currentY = (y_l) | ((y_h & 0x07FF)<<16);
   1.528 +		if (y_h & 0x0800)
   1.529 +			currentY |= 0xF8000000;
   1.530 +	}
   1.531 +	else
   1.532 +	{
   1.533 +		currentY += dmy;
   1.534 +	}
   1.535 +
   1.536 +	int realX = currentX;
   1.537 +	int realY = currentY;
   1.538 +
   1.539 +	if (control & 0x40)
   1.540 +	{
   1.541 +		int mosaicY = ((MOSAIC & 0xF0)>>4) + 1;
   1.542 +		int y       = (VCOUNT % mosaicY);
   1.543 +		realX -= y*dmx;
   1.544 +		realY -= y*dmy;
   1.545 +	}
   1.546 +
   1.547 +	int xxx = (realX >> 8);
   1.548 +	int yyy = (realY >> 8);
   1.549 +
   1.550 +	for (int x = 0; x < 240; x++)
   1.551 +	{
   1.552 +		if (xxx < 0 ||
   1.553 +		    yyy < 0 ||
   1.554 +		    xxx >= sizeX ||
   1.555 +		    yyy >= sizeY)
   1.556 +		{
   1.557 +			line[x] = 0x80000000;
   1.558 +		}
   1.559 +		else
   1.560 +		{
   1.561 +			line[x] = (READ16LE(&screenBase[yyy * sizeX + xxx]) | prio);
   1.562 +		}
   1.563 +		realX += dx;
   1.564 +		realY += dy;
   1.565 +
   1.566 +		xxx = (realX >> 8);
   1.567 +		yyy = (realY >> 8);
   1.568 +	}
   1.569 +
   1.570 +	if (control & 0x40)
   1.571 +	{
   1.572 +		int mosaicX = (MOSAIC & 0xF) + 1;
   1.573 +		if (mosaicX > 1)
   1.574 +		{
   1.575 +			int m = 1;
   1.576 +			for (int i = 0; i < 239; i++)
   1.577 +			{
   1.578 +				line[i+1] = line[i];
   1.579 +				m++;
   1.580 +				if (m == mosaicX)
   1.581 +				{
   1.582 +					m = 1;
   1.583 +					i++;
   1.584 +				}
   1.585 +			}
   1.586 +		}
   1.587 +	}
   1.588 +}
   1.589 +
   1.590 +inline void gfxDrawRotScreen256(u16 control,
   1.591 +                                u16 x_l, u16 x_h,
   1.592 +                                u16 y_l, u16 y_h,
   1.593 +                                u16 pa,  u16 pb,
   1.594 +                                u16 pc,  u16 pd,
   1.595 +                                int &currentX, int& currentY,
   1.596 +                                int changed,
   1.597 +                                u32 *line)
   1.598 +{
   1.599 +	u16 *palette    = (u16 *)paletteRAM;
   1.600 +	u8 * screenBase = (DISPCNT & 0x0010) ? &vram[0xA000] : &vram[0x0000];
   1.601 +	int  prio       = ((control & 3) << 25) + 0x1000000;
   1.602 +	int  sizeX      = 240;
   1.603 +	int  sizeY      = 160;
   1.604 +
   1.605 +	int startX = (x_l) | ((x_h & 0x07FF)<<16);
   1.606 +	if (x_h & 0x0800)
   1.607 +		startX |= 0xF8000000;
   1.608 +	int startY = (y_l) | ((y_h & 0x07FF)<<16);
   1.609 +	if (y_h & 0x0800)
   1.610 +		startY |= 0xF8000000;
   1.611 +
   1.612 +	int dx = pa & 0x7FFF;
   1.613 +	if (pa & 0x8000)
   1.614 +		dx |= 0xFFFF8000;
   1.615 +	int dmx = pb & 0x7FFF;
   1.616 +	if (pb & 0x8000)
   1.617 +		dmx |= 0xFFFF8000;
   1.618 +	int dy = pc & 0x7FFF;
   1.619 +	if (pc & 0x8000)
   1.620 +		dy |= 0xFFFF8000;
   1.621 +	int dmy = pd & 0x7FFFF;
   1.622 +	if (pd & 0x8000)
   1.623 +		dmy |= 0xFFFF8000;
   1.624 +
   1.625 +	if (VCOUNT == 0)
   1.626 +		changed = 3;
   1.627 +
   1.628 +	if (changed & 1)
   1.629 +	{
   1.630 +		currentX = (x_l) | ((x_h & 0x07FF)<<16);
   1.631 +		if (x_h & 0x0800)
   1.632 +			currentX |= 0xF8000000;
   1.633 +	}
   1.634 +	else
   1.635 +	{
   1.636 +		currentX += dmx;
   1.637 +	}
   1.638 +
   1.639 +	if (changed & 2)
   1.640 +	{
   1.641 +		currentY = (y_l) | ((y_h & 0x07FF)<<16);
   1.642 +		if (y_h & 0x0800)
   1.643 +			currentY |= 0xF8000000;
   1.644 +	}
   1.645 +	else
   1.646 +	{
   1.647 +		currentY += dmy;
   1.648 +	}
   1.649 +
   1.650 +	int realX = currentX;
   1.651 +	int realY = currentY;
   1.652 +
   1.653 +	if (control & 0x40)
   1.654 +	{
   1.655 +		int mosaicY = ((MOSAIC & 0xF0)>>4) + 1;
   1.656 +		int y       = (VCOUNT / mosaicY) * mosaicY;
   1.657 +		realX = startX + y*dmx;
   1.658 +		realY = startY + y*dmy;
   1.659 +	}
   1.660 +
   1.661 +	int xxx = (realX >> 8);
   1.662 +	int yyy = (realY >> 8);
   1.663 +
   1.664 +	for (int x = 0; x < 240; x++)
   1.665 +	{
   1.666 +		if (xxx < 0 ||
   1.667 +		    yyy < 0 ||
   1.668 +		    xxx >= sizeX ||
   1.669 +		    yyy >= sizeY)
   1.670 +		{
   1.671 +			line[x] = 0x80000000;
   1.672 +		}
   1.673 +		else
   1.674 +		{
   1.675 +			u8 color = screenBase[yyy * 240 + xxx];
   1.676 +
   1.677 +			line[x] = color ? (READ16LE(&palette[color])|prio) : 0x80000000;
   1.678 +		}
   1.679 +		realX += dx;
   1.680 +		realY += dy;
   1.681 +
   1.682 +		xxx = (realX >> 8);
   1.683 +		yyy = (realY >> 8);
   1.684 +	}
   1.685 +
   1.686 +	if (control & 0x40)
   1.687 +	{
   1.688 +		int mosaicX = (MOSAIC & 0xF) + 1;
   1.689 +		if (mosaicX > 1)
   1.690 +		{
   1.691 +			int m = 1;
   1.692 +			for (int i = 0; i < 239; i++)
   1.693 +			{
   1.694 +				line[i+1] = line[i];
   1.695 +				m++;
   1.696 +				if (m == mosaicX)
   1.697 +				{
   1.698 +					m = 1;
   1.699 +					i++;
   1.700 +				}
   1.701 +			}
   1.702 +		}
   1.703 +	}
   1.704 +}
   1.705 +
   1.706 +inline void gfxDrawRotScreen16Bit160(u16 control,
   1.707 +                                     u16 x_l, u16 x_h,
   1.708 +                                     u16 y_l, u16 y_h,
   1.709 +                                     u16 pa,  u16 pb,
   1.710 +                                     u16 pc,  u16 pd,
   1.711 +                                     int& currentX, int& currentY,
   1.712 +                                     int changed,
   1.713 +                                     u32 *line)
   1.714 +{
   1.715 +	u16 *screenBase = (DISPCNT & 0x0010) ? (u16 *)&vram[0xa000] :
   1.716 +	                  (u16 *)&vram[0];
   1.717 +	int prio  = ((control & 3) << 25) + 0x1000000;
   1.718 +	int sizeX = 160;
   1.719 +	int sizeY = 128;
   1.720 +
   1.721 +	int startX = (x_l) | ((x_h & 0x07FF)<<16);
   1.722 +	if (x_h & 0x0800)
   1.723 +		startX |= 0xF8000000;
   1.724 +	int startY = (y_l) | ((y_h & 0x07FF)<<16);
   1.725 +	if (y_h & 0x0800)
   1.726 +		startY |= 0xF8000000;
   1.727 +
   1.728 +	int dx = pa & 0x7FFF;
   1.729 +	if (pa & 0x8000)
   1.730 +		dx |= 0xFFFF8000;
   1.731 +	int dmx = pb & 0x7FFF;
   1.732 +	if (pb & 0x8000)
   1.733 +		dmx |= 0xFFFF8000;
   1.734 +	int dy = pc & 0x7FFF;
   1.735 +	if (pc & 0x8000)
   1.736 +		dy |= 0xFFFF8000;
   1.737 +	int dmy = pd & 0x7FFFF;
   1.738 +	if (pd & 0x8000)
   1.739 +		dmy |= 0xFFFF8000;
   1.740 +
   1.741 +	if (VCOUNT == 0)
   1.742 +		changed = 3;
   1.743 +
   1.744 +	if (changed & 1)
   1.745 +	{
   1.746 +		currentX = (x_l) | ((x_h & 0x07FF)<<16);
   1.747 +		if (x_h & 0x0800)
   1.748 +			currentX |= 0xF8000000;
   1.749 +	}
   1.750 +	else
   1.751 +	{
   1.752 +		currentX += dmx;
   1.753 +	}
   1.754 +
   1.755 +	if (changed & 2)
   1.756 +	{
   1.757 +		currentY = (y_l) | ((y_h & 0x07FF)<<16);
   1.758 +		if (y_h & 0x0800)
   1.759 +			currentY |= 0xF8000000;
   1.760 +	}
   1.761 +	else
   1.762 +	{
   1.763 +		currentY += dmy;
   1.764 +	}
   1.765 +
   1.766 +	int realX = currentX;
   1.767 +	int realY = currentY;
   1.768 +
   1.769 +	if (control & 0x40)
   1.770 +	{
   1.771 +		int mosaicY = ((MOSAIC & 0xF0)>>4) + 1;
   1.772 +		int y       = (VCOUNT / mosaicY) * mosaicY;
   1.773 +		realX = startX + y*dmx;
   1.774 +		realY = startY + y*dmy;
   1.775 +	}
   1.776 +
   1.777 +	int xxx = (realX >> 8);
   1.778 +	int yyy = (realY >> 8);
   1.779 +
   1.780 +	for (int x = 0; x < 240; x++)
   1.781 +	{
   1.782 +		if (xxx < 0 ||
   1.783 +		    yyy < 0 ||
   1.784 +		    xxx >= sizeX ||
   1.785 +		    yyy >= sizeY)
   1.786 +		{
   1.787 +			line[x] = 0x80000000;
   1.788 +		}
   1.789 +		else
   1.790 +		{
   1.791 +			line[x] = (READ16LE(&screenBase[yyy * sizeX + xxx]) | prio);
   1.792 +		}
   1.793 +		realX += dx;
   1.794 +		realY += dy;
   1.795 +
   1.796 +		xxx = (realX >> 8);
   1.797 +		yyy = (realY >> 8);
   1.798 +	}
   1.799 +
   1.800 +	if (control & 0x40)
   1.801 +	{
   1.802 +		int mosaicX = (MOSAIC & 0xF) + 1;
   1.803 +		if (mosaicX > 1)
   1.804 +		{
   1.805 +			int m = 1;
   1.806 +			for (int i = 0; i < 239; i++)
   1.807 +			{
   1.808 +				line[i+1] = line[i];
   1.809 +				m++;
   1.810 +				if (m == mosaicX)
   1.811 +				{
   1.812 +					m = 1;
   1.813 +					i++;
   1.814 +				}
   1.815 +			}
   1.816 +		}
   1.817 +	}
   1.818 +}
   1.819 +
   1.820 +inline void gfxDrawSprites(u32 *lineOBJ)
   1.821 +{
   1.822 +	int m = 0;
   1.823 +	gfxClearArray(lineOBJ);
   1.824 +	if (layerEnable & 0x1000)
   1.825 +	{
   1.826 +		u16 *sprites       = (u16 *)oam;
   1.827 +		u16 *spritePalette = &((u16 *)paletteRAM)[256];
   1.828 +		int  mosaicY       = ((MOSAIC & 0xF000)>>12) + 1;
   1.829 +		int  mosaicX       = ((MOSAIC & 0xF00)>>8) + 1;
   1.830 +		for (int x = 0; x < 128; x++)
   1.831 +		{
   1.832 +			u16 a0 = READ16LE(sprites++);
   1.833 +			u16 a1 = READ16LE(sprites++);
   1.834 +			u16 a2 = READ16LE(sprites++);
   1.835 +			sprites++;
   1.836 +
   1.837 +			// ignore OBJ-WIN
   1.838 +			if ((a0 & 0x0c00) == 0x0800)
   1.839 +				continue;
   1.840 +
   1.841 +			int sizeY = 8;
   1.842 +			int sizeX = 8;
   1.843 +
   1.844 +			switch (((a0 >>12) & 0x0c)|(a1>>14))
   1.845 +			{
   1.846 +			case 0:
   1.847 +				break;
   1.848 +			case 1:
   1.849 +				sizeX = sizeY = 16;
   1.850 +				break;
   1.851 +			case 2:
   1.852 +				sizeX = sizeY = 32;
   1.853 +				break;
   1.854 +			case 3:
   1.855 +				sizeX = sizeY = 64;
   1.856 +				break;
   1.857 +			case 4:
   1.858 +				sizeX = 16;
   1.859 +				break;
   1.860 +			case 5:
   1.861 +				sizeX = 32;
   1.862 +				break;
   1.863 +			case 6:
   1.864 +				sizeX = 32;
   1.865 +				sizeY = 16;
   1.866 +				break;
   1.867 +			case 7:
   1.868 +				sizeX = 64;
   1.869 +				sizeY = 32;
   1.870 +				break;
   1.871 +			case 8:
   1.872 +				sizeY = 16;
   1.873 +				break;
   1.874 +			case 9:
   1.875 +				sizeY = 32;
   1.876 +				break;
   1.877 +			case 10:
   1.878 +				sizeX = 16;
   1.879 +				sizeY = 32;
   1.880 +				break;
   1.881 +			case 11:
   1.882 +				sizeX = 32;
   1.883 +				sizeY = 64;
   1.884 +				break;
   1.885 +			default:
   1.886 +				continue;
   1.887 +			}
   1.888 +
   1.889 +#ifdef SPRITE_DEBUG
   1.890 +			int maskX = sizeX-1;
   1.891 +			int maskY = sizeY-1;
   1.892 +#endif
   1.893 +
   1.894 +			int sy = (a0 & 255);
   1.895 +
   1.896 +			if (sy > 160)
   1.897 +				sy -= 256;
   1.898 +
   1.899 +			if (a0 & 0x0100)
   1.900 +			{
   1.901 +				int fieldX = sizeX;
   1.902 +				int fieldY = sizeY;
   1.903 +				if (a0 & 0x0200)
   1.904 +				{
   1.905 +					fieldX <<= 1;
   1.906 +					fieldY <<= 1;
   1.907 +				}
   1.908 +
   1.909 +				int t = VCOUNT - sy;
   1.910 +				if ((t >= 0) && (t < fieldY))
   1.911 +				{
   1.912 +					int sx = (a1 & 0x1FF);
   1.913 +					if ((sx < 240) || (((sx + fieldX) & 511) < 240))
   1.914 +					{
   1.915 +						// int t2 = t - (fieldY >> 1);
   1.916 +						int  rot = (a1 >> 9) & 0x1F;
   1.917 +						u16 *OAM = (u16 *)oam;
   1.918 +						int  dx  = READ16LE(&OAM[3 + (rot << 4)]);
   1.919 +						if (dx & 0x8000)
   1.920 +							dx |= 0xFFFF8000;
   1.921 +						int dmx = READ16LE(&OAM[7 + (rot << 4)]);
   1.922 +						if (dmx & 0x8000)
   1.923 +							dmx |= 0xFFFF8000;
   1.924 +						int dy = READ16LE(&OAM[11 + (rot << 4)]);
   1.925 +						if (dy & 0x8000)
   1.926 +							dy |= 0xFFFF8000;
   1.927 +						int dmy = READ16LE(&OAM[15 + (rot << 4)]);
   1.928 +						if (dmy & 0x8000)
   1.929 +							dmy |= 0xFFFF8000;
   1.930 +
   1.931 +						if (a0 & 0x1000)
   1.932 +						{
   1.933 +							t -= (t % mosaicY);
   1.934 +						}
   1.935 +
   1.936 +						int realX = ((sizeX) << 7) - (fieldX >> 1)*dx - (fieldY>>1)*dmx
   1.937 +						            + t * dmx;
   1.938 +						int realY = ((sizeY) << 7) - (fieldX >> 1)*dy - (fieldY>>1)*dmy
   1.939 +						            + t * dmy;
   1.940 +
   1.941 +						u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6);
   1.942 +
   1.943 +						if (a0 & 0x2000)
   1.944 +						{
   1.945 +							int c = (a2 & 0x3FF);
   1.946 +							if ((DISPCNT & 7) > 2 && (c < 512))
   1.947 +								continue;
   1.948 +							int inc = 32;
   1.949 +							if (DISPCNT & 0x40)
   1.950 +								inc = sizeX >> 2;
   1.951 +							else
   1.952 +								c &= 0x3FE;
   1.953 +							for (int x = 0; x < fieldX; x++)
   1.954 +							{
   1.955 +								int xxx = realX >> 8;
   1.956 +								int yyy = realY >> 8;
   1.957 +
   1.958 +								if (xxx < 0 || xxx >= sizeX ||
   1.959 +								    yyy < 0 || yyy >= sizeY ||
   1.960 +								    sx >= 240)
   1.961 +									;
   1.962 +								else
   1.963 +								{
   1.964 +									u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5)
   1.965 +									                             + ((yyy & 7)<<3) + ((xxx >> 3)<<6) +
   1.966 +									                             (xxx & 7))&0x7FFF)];
   1.967 +									if ((color == 0) && (((prio >> 25)&3) <
   1.968 +									                     ((lineOBJ[sx]>>25)&3)))
   1.969 +									{
   1.970 +										lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio;
   1.971 +										if ((a0 & 0x1000) && m)
   1.972 +											lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio;
   1.973 +									}
   1.974 +									else if ((color) && (prio < (lineOBJ[sx]&0xFF000000)))
   1.975 +									{
   1.976 +										lineOBJ[sx] = READ16LE(&spritePalette[color]) | prio;
   1.977 +										if ((a0 & 0x1000) && m)
   1.978 +											lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio;
   1.979 +									}
   1.980 +
   1.981 +									if (a0 & 0x1000)
   1.982 +									{
   1.983 +										m++;
   1.984 +										if (m == mosaicX)
   1.985 +											m = 0;
   1.986 +									}
   1.987 +#ifdef SPRITE_DEBUG
   1.988 +									if (t == 0 || t == maskY || x == 0 || x == maskX)
   1.989 +										lineOBJ[sx] = 0x001F;
   1.990 +#endif
   1.991 +								}
   1.992 +								sx     = (sx+1)&511;;
   1.993 +								realX += dx;
   1.994 +								realY += dy;
   1.995 +							}
   1.996 +						}
   1.997 +						else
   1.998 +						{
   1.999 +							int c = (a2 & 0x3FF);
  1.1000 +							if ((DISPCNT & 7) > 2 && (c < 512))
  1.1001 +								continue;
  1.1002 +
  1.1003 +							int inc = 32;
  1.1004 +							if (DISPCNT & 0x40)
  1.1005 +								inc = sizeX >> 3;
  1.1006 +							int palette = (a2 >> 8) & 0xF0;
  1.1007 +							for (int x = 0; x < fieldX; x++)
  1.1008 +							{
  1.1009 +								int xxx = realX >> 8;
  1.1010 +								int yyy = realY >> 8;
  1.1011 +								if (xxx < 0 || xxx >= sizeX ||
  1.1012 +								    yyy < 0 || yyy >= sizeY ||
  1.1013 +								    sx >= 240)
  1.1014 +									;
  1.1015 +								else
  1.1016 +								{
  1.1017 +									u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5)
  1.1018 +									                             + ((yyy & 7)<<2) + ((xxx >> 3)<<5) +
  1.1019 +									                             ((xxx & 7)>>1))&0x7FFF)];
  1.1020 +									if (xxx & 1)
  1.1021 +										color >>= 4;
  1.1022 +									else
  1.1023 +										color &= 0x0F;
  1.1024 +
  1.1025 +									if ((color == 0) && (((prio >> 25)&3) <
  1.1026 +									                     ((lineOBJ[sx]>>25)&3)))
  1.1027 +									{
  1.1028 +										lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio;
  1.1029 +										if ((a0 & 0x1000) && m)
  1.1030 +											lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio;
  1.1031 +									}
  1.1032 +									else if ((color) && (prio < (lineOBJ[sx]&0xFF000000)))
  1.1033 +									{
  1.1034 +										lineOBJ[sx] = READ16LE(&spritePalette[palette+color]) | prio;
  1.1035 +										if ((a0 & 0x1000) && m)
  1.1036 +											lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio;
  1.1037 +									}
  1.1038 +								}
  1.1039 +								if ((a0 & 0x1000) && m)
  1.1040 +								{
  1.1041 +									m++;
  1.1042 +									if (m == mosaicX)
  1.1043 +										m = 0;
  1.1044 +								}
  1.1045 +
  1.1046 +#ifdef SPRITE_DEBUG
  1.1047 +								if (t == 0 || t == maskY || x == 0 || x == maskX)
  1.1048 +									lineOBJ[sx] = 0x001F;
  1.1049 +#endif
  1.1050 +								sx     = (sx+1)&511;;
  1.1051 +								realX += dx;
  1.1052 +								realY += dy;
  1.1053 +							}
  1.1054 +						}
  1.1055 +					}
  1.1056 +				}
  1.1057 +			}
  1.1058 +			else
  1.1059 +			{
  1.1060 +				int t = VCOUNT - sy;
  1.1061 +				if ((t >= 0) && (t < sizeY))
  1.1062 +				{
  1.1063 +					int sx = (a1 & 0x1FF);
  1.1064 +					if (((sx < 240) || (((sx+sizeX)&511) < 240)) && !(a0 & 0x0200))
  1.1065 +					{
  1.1066 +						if (a0 & 0x2000)
  1.1067 +						{
  1.1068 +							if (a1 & 0x2000)
  1.1069 +								t = sizeY - t - 1;
  1.1070 +							int c = (a2 & 0x3FF);
  1.1071 +							if ((DISPCNT & 7) > 2 && (c < 512))
  1.1072 +								continue;
  1.1073 +
  1.1074 +							int inc = 32;
  1.1075 +							if (DISPCNT & 0x40)
  1.1076 +							{
  1.1077 +								inc = sizeX >> 2;
  1.1078 +							}
  1.1079 +							else
  1.1080 +							{
  1.1081 +								c &= 0x3FE;
  1.1082 +							}
  1.1083 +							int xxx = 0;
  1.1084 +							if (a1 & 0x1000)
  1.1085 +								xxx = sizeX-1;
  1.1086 +
  1.1087 +							if (a0 & 0x1000)
  1.1088 +							{
  1.1089 +								t -= (t % mosaicY);
  1.1090 +							}
  1.1091 +
  1.1092 +							int address = 0x10000 + ((((c+ (t>>3) * inc) << 5)
  1.1093 +							                          + ((t & 7) << 3) + ((xxx>>3)<<6) + (xxx & 7)) & 0x7FFF);
  1.1094 +
  1.1095 +							if (a1 & 0x1000)
  1.1096 +								xxx = 7;
  1.1097 +							u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6);
  1.1098 +
  1.1099 +							for (int xx = 0; xx < sizeX; xx++)
  1.1100 +							{
  1.1101 +								if (sx < 240)
  1.1102 +								{
  1.1103 +									u8 color = vram[address];
  1.1104 +									if ((color == 0) && (((prio >> 25)&3) <
  1.1105 +									                     ((lineOBJ[sx]>>25)&3)))
  1.1106 +									{
  1.1107 +										lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio;
  1.1108 +										if ((a0 & 0x1000) && m)
  1.1109 +											lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio;
  1.1110 +									}
  1.1111 +									else if ((color) && (prio < (lineOBJ[sx]&0xFF000000)))
  1.1112 +									{
  1.1113 +										lineOBJ[sx] = READ16LE(&spritePalette[color]) | prio;
  1.1114 +										if ((a0 & 0x1000) && m)
  1.1115 +											lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio;
  1.1116 +									}
  1.1117 +
  1.1118 +									if (a0 & 0x1000)
  1.1119 +									{
  1.1120 +										m++;
  1.1121 +										if (m == mosaicX)
  1.1122 +											m = 0;
  1.1123 +									}
  1.1124 +
  1.1125 +#ifdef SPRITE_DEBUG
  1.1126 +									if (t == 0 || t == maskY || xx == 0 || xx == maskX)
  1.1127 +										lineOBJ[sx] = 0x001F;
  1.1128 +#endif
  1.1129 +								}
  1.1130 +
  1.1131 +								sx = (sx+1) & 511;
  1.1132 +								if (a1 & 0x1000)
  1.1133 +								{
  1.1134 +									xxx--;
  1.1135 +									address--;
  1.1136 +									if (xxx == -1)
  1.1137 +									{
  1.1138 +										address -= 56;
  1.1139 +										xxx      = 7;
  1.1140 +									}
  1.1141 +									if (address < 0x10000)
  1.1142 +										address += 0x8000;
  1.1143 +								}
  1.1144 +								else
  1.1145 +								{
  1.1146 +									xxx++;
  1.1147 +									address++;
  1.1148 +									if (xxx == 8)
  1.1149 +									{
  1.1150 +										address += 56;
  1.1151 +										xxx      = 0;
  1.1152 +									}
  1.1153 +									if (address > 0x17fff)
  1.1154 +										address -= 0x8000;
  1.1155 +								}
  1.1156 +							}
  1.1157 +						}
  1.1158 +						else
  1.1159 +						{
  1.1160 +							if (a1 & 0x2000)
  1.1161 +								t = sizeY - t - 1;
  1.1162 +							int c = (a2 & 0x3FF);
  1.1163 +							if ((DISPCNT & 7) > 2 && (c < 512))
  1.1164 +								continue;
  1.1165 +
  1.1166 +							int inc = 32;
  1.1167 +							if (DISPCNT & 0x40)
  1.1168 +							{
  1.1169 +								inc = sizeX >> 3;
  1.1170 +							}
  1.1171 +							int xxx = 0;
  1.1172 +							if (a1 & 0x1000)
  1.1173 +								xxx = sizeX - 1;
  1.1174 +
  1.1175 +							if (a0 & 0x1000)
  1.1176 +							{
  1.1177 +								t -= (t % mosaicY);
  1.1178 +							}
  1.1179 +
  1.1180 +							int address = 0x10000 + ((((c + (t>>3) * inc)<<5)
  1.1181 +							                          + ((t & 7)<<2) + ((xxx>>3)<<5) + ((xxx & 7) >> 1))&0x7FFF);
  1.1182 +							u32 prio    = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6);
  1.1183 +							int palette = (a2 >> 8) & 0xF0;
  1.1184 +							if (a1 & 0x1000)
  1.1185 +							{
  1.1186 +								xxx = 7;
  1.1187 +								for (int xx = sizeX - 1; xx >= 0; xx--)
  1.1188 +								{
  1.1189 +									if (sx < 240)
  1.1190 +									{
  1.1191 +										u8 color = vram[address];
  1.1192 +										if (xx & 1)
  1.1193 +										{
  1.1194 +											color = (color >> 4);
  1.1195 +										}
  1.1196 +										else
  1.1197 +											color &= 0x0F;
  1.1198 +
  1.1199 +										if ((color == 0) && (((prio >> 25)&3) <
  1.1200 +										                     ((lineOBJ[sx]>>25)&3)))
  1.1201 +										{
  1.1202 +											lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio;
  1.1203 +											if ((a0 & 0x1000) && m)
  1.1204 +												lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio;
  1.1205 +										}
  1.1206 +										else if ((color) && (prio < (lineOBJ[sx]&0xFF000000)))
  1.1207 +										{
  1.1208 +											lineOBJ[sx] = READ16LE(&spritePalette[palette + color]) | prio;
  1.1209 +											if ((a0 & 0x1000) && m)
  1.1210 +												lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio;
  1.1211 +										}
  1.1212 +									}
  1.1213 +									if (a0 & 0x1000)
  1.1214 +									{
  1.1215 +										m++;
  1.1216 +										if (m == mosaicX)
  1.1217 +											m = 0;
  1.1218 +									}
  1.1219 +#ifdef SPRITE_DEBUG
  1.1220 +									if (t == 0 || t == maskY || xx == 0 || xx == maskX)
  1.1221 +										lineOBJ[sx] = 0x001F;
  1.1222 +#endif
  1.1223 +									sx = (sx+1) & 511;
  1.1224 +									xxx--;
  1.1225 +									if (!(xx & 1))
  1.1226 +										address--;
  1.1227 +									if (xxx == -1)
  1.1228 +									{
  1.1229 +										xxx      = 7;
  1.1230 +										address -= 28;
  1.1231 +									}
  1.1232 +									if (address < 0x10000)
  1.1233 +										address += 0x8000;
  1.1234 +								}
  1.1235 +							}
  1.1236 +							else
  1.1237 +							{
  1.1238 +								for (int xx = 0; xx < sizeX; xx++)
  1.1239 +								{
  1.1240 +									if (sx < 240)
  1.1241 +									{
  1.1242 +										u8 color = vram[address];
  1.1243 +										if (xx & 1)
  1.1244 +										{
  1.1245 +											color = (color >> 4);
  1.1246 +										}
  1.1247 +										else
  1.1248 +											color &= 0x0F;
  1.1249 +
  1.1250 +										if ((color == 0) && (((prio >> 25)&3) <
  1.1251 +										                     ((lineOBJ[sx]>>25)&3)))
  1.1252 +										{
  1.1253 +											lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio;
  1.1254 +											if ((a0 & 0x1000) && m)
  1.1255 +												lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio;
  1.1256 +										}
  1.1257 +										else if ((color) && (prio < (lineOBJ[sx]&0xFF000000)))
  1.1258 +										{
  1.1259 +											lineOBJ[sx] = READ16LE(&spritePalette[palette + color]) | prio;
  1.1260 +											if ((a0 & 0x1000) && m)
  1.1261 +												lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio;
  1.1262 +										}
  1.1263 +									}
  1.1264 +									if (a0 & 0x1000)
  1.1265 +									{
  1.1266 +										m++;
  1.1267 +										if (m == mosaicX)
  1.1268 +											m = 0;
  1.1269 +									}
  1.1270 +#ifdef SPRITE_DEBUG
  1.1271 +									if (t == 0 || t == maskY || xx == 0 || xx == maskX)
  1.1272 +										lineOBJ[sx] = 0x001F;
  1.1273 +#endif
  1.1274 +									sx = (sx+1) & 511;
  1.1275 +									xxx++;
  1.1276 +									if (xx & 1)
  1.1277 +										address++;
  1.1278 +									if (xxx == 8)
  1.1279 +									{
  1.1280 +										address += 28;
  1.1281 +										xxx      = 0;
  1.1282 +									}
  1.1283 +									if (address > 0x17fff)
  1.1284 +										address -= 0x8000;
  1.1285 +								}
  1.1286 +							}
  1.1287 +						}
  1.1288 +					}
  1.1289 +				}
  1.1290 +			}
  1.1291 +		}
  1.1292 +	}
  1.1293 +}
  1.1294 +
  1.1295 +inline void gfxDrawOBJWin(u32 *lineOBJWin)
  1.1296 +{
  1.1297 +	gfxClearArray(lineOBJWin);
  1.1298 +	if (layerEnable & 0x8000)
  1.1299 +	{
  1.1300 +		u16 *sprites = (u16 *)oam;
  1.1301 +		// u16 *spritePalette = &((u16 *)paletteRAM)[256];
  1.1302 +		for (int x = 0; x < 128; x++)
  1.1303 +		{
  1.1304 +			u16 a0 = READ16LE(sprites++);
  1.1305 +			u16 a1 = READ16LE(sprites++);
  1.1306 +			u16 a2 = READ16LE(sprites++);
  1.1307 +			sprites++;
  1.1308 +
  1.1309 +			// ignore non OBJ-WIN
  1.1310 +			if ((a0 & 0x0c00) != 0x0800)
  1.1311 +				continue;
  1.1312 +
  1.1313 +			int sizeY = 8;
  1.1314 +			int sizeX = 8;
  1.1315 +
  1.1316 +			switch (((a0 >>12) & 0x0c)|(a1>>14))
  1.1317 +			{
  1.1318 +			case 0:
  1.1319 +				break;
  1.1320 +			case 1:
  1.1321 +				sizeX = sizeY = 16;
  1.1322 +				break;
  1.1323 +			case 2:
  1.1324 +				sizeX = sizeY = 32;
  1.1325 +				break;
  1.1326 +			case 3:
  1.1327 +				sizeX = sizeY = 64;
  1.1328 +				break;
  1.1329 +			case 4:
  1.1330 +				sizeX = 16;
  1.1331 +				break;
  1.1332 +			case 5:
  1.1333 +				sizeX = 32;
  1.1334 +				break;
  1.1335 +			case 6:
  1.1336 +				sizeX = 32;
  1.1337 +				sizeY = 16;
  1.1338 +				break;
  1.1339 +			case 7:
  1.1340 +				sizeX = 64;
  1.1341 +				sizeY = 32;
  1.1342 +				break;
  1.1343 +			case 8:
  1.1344 +				sizeY = 16;
  1.1345 +				break;
  1.1346 +			case 9:
  1.1347 +				sizeY = 32;
  1.1348 +				break;
  1.1349 +			case 10:
  1.1350 +				sizeX = 16;
  1.1351 +				sizeY = 32;
  1.1352 +				break;
  1.1353 +			case 11:
  1.1354 +				sizeX = 32;
  1.1355 +				sizeY = 64;
  1.1356 +				break;
  1.1357 +			default:
  1.1358 +				continue;
  1.1359 +			}
  1.1360 +
  1.1361 +			int sy = (a0 & 255);
  1.1362 +
  1.1363 +			if (sy > 160)
  1.1364 +				sy -= 256;
  1.1365 +
  1.1366 +			if (a0 & 0x0100)
  1.1367 +			{
  1.1368 +				int fieldX = sizeX;
  1.1369 +				int fieldY = sizeY;
  1.1370 +				if (a0 & 0x0200)
  1.1371 +				{
  1.1372 +					fieldX <<= 1;
  1.1373 +					fieldY <<= 1;
  1.1374 +				}
  1.1375 +
  1.1376 +				int t = VCOUNT - sy;
  1.1377 +				if ((t >= 0) && (t < fieldY))
  1.1378 +				{
  1.1379 +					int sx = (a1 & 0x1FF);
  1.1380 +					if ((sx < 240) || (((sx + fieldX) & 511) < 240))
  1.1381 +					{
  1.1382 +						// int t2 = t - (fieldY >> 1);
  1.1383 +						int  rot = (a1 >> 9) & 0x1F;
  1.1384 +						u16 *OAM = (u16 *)oam;
  1.1385 +						int  dx  = READ16LE(&OAM[3 + (rot << 4)]);
  1.1386 +						if (dx & 0x8000)
  1.1387 +							dx |= 0xFFFF8000;
  1.1388 +						int dmx = READ16LE(&OAM[7 + (rot << 4)]);
  1.1389 +						if (dmx & 0x8000)
  1.1390 +							dmx |= 0xFFFF8000;
  1.1391 +						int dy = READ16LE(&OAM[11 + (rot << 4)]);
  1.1392 +						if (dy & 0x8000)
  1.1393 +							dy |= 0xFFFF8000;
  1.1394 +						int dmy = READ16LE(&OAM[15 + (rot << 4)]);
  1.1395 +						if (dmy & 0x8000)
  1.1396 +							dmy |= 0xFFFF8000;
  1.1397 +
  1.1398 +						int realX = ((sizeX) << 7) - (fieldX >> 1)*dx - (fieldY>>1)*dmx
  1.1399 +						            + t * dmx;
  1.1400 +						int realY = ((sizeY) << 7) - (fieldX >> 1)*dy - (fieldY>>1)*dmy
  1.1401 +						            + t * dmy;
  1.1402 +
  1.1403 +						// u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6);
  1.1404 +
  1.1405 +						if (a0 & 0x2000)
  1.1406 +						{
  1.1407 +							int c = (a2 & 0x3FF);
  1.1408 +							if ((DISPCNT & 7) > 2 && (c < 512))
  1.1409 +								continue;
  1.1410 +							int inc = 32;
  1.1411 +							if (DISPCNT & 0x40)
  1.1412 +								inc = sizeX >> 2;
  1.1413 +							else
  1.1414 +								c &= 0x3FE;
  1.1415 +							for (int x = 0; x < fieldX; x++)
  1.1416 +							{
  1.1417 +								int xxx = realX >> 8;
  1.1418 +								int yyy = realY >> 8;
  1.1419 +
  1.1420 +								if (xxx < 0 || xxx >= sizeX ||
  1.1421 +								    yyy < 0 || yyy >= sizeY)
  1.1422 +								{}
  1.1423 +								else
  1.1424 +								{
  1.1425 +									u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5)
  1.1426 +									                             + ((yyy & 7)<<3) + ((xxx >> 3)<<6) +
  1.1427 +									                             (xxx & 7))&0x7fff)];
  1.1428 +									if (color)
  1.1429 +									{
  1.1430 +										lineOBJWin[sx] = 1;
  1.1431 +									}
  1.1432 +								}
  1.1433 +								sx     = (sx+1)&511;;
  1.1434 +								realX += dx;
  1.1435 +								realY += dy;
  1.1436 +							}
  1.1437 +						}
  1.1438 +						else
  1.1439 +						{
  1.1440 +							int c = (a2 & 0x3FF);
  1.1441 +							if ((DISPCNT & 7) > 2 && (c < 512))
  1.1442 +								continue;
  1.1443 +
  1.1444 +							int inc = 32;
  1.1445 +							if (DISPCNT & 0x40)
  1.1446 +								inc = sizeX >> 3;
  1.1447 +							// int palette = (a2 >> 8) & 0xF0;
  1.1448 +							for (int x = 0; x < fieldX; x++)
  1.1449 +							{
  1.1450 +								int xxx = realX >> 8;
  1.1451 +								int yyy = realY >> 8;
  1.1452 +
  1.1453 +								//              if(x == 0 || x == (sizeX-1) ||
  1.1454 +								//                 t == 0 || t == (sizeY-1)) {
  1.1455 +								//                lineOBJ[sx] = 0x001F | prio;
  1.1456 +								//              } else {
  1.1457 +								if (xxx < 0 || xxx >= sizeX ||
  1.1458 +								    yyy < 0 || yyy >= sizeY)
  1.1459 +								{}
  1.1460 +								else
  1.1461 +								{
  1.1462 +									u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5)
  1.1463 +									                             + ((yyy & 7)<<2) + ((xxx >> 3)<<5) +
  1.1464 +									                             ((xxx & 7)>>1))&0x7fff)];
  1.1465 +									if (xxx & 1)
  1.1466 +										color >>= 4;
  1.1467 +									else
  1.1468 +										color &= 0x0F;
  1.1469 +
  1.1470 +									if (color)
  1.1471 +									{
  1.1472 +										lineOBJWin[sx] = 1;
  1.1473 +									}
  1.1474 +								}
  1.1475 +								//            }
  1.1476 +								sx     = (sx+1)&511;;
  1.1477 +								realX += dx;
  1.1478 +								realY += dy;
  1.1479 +							}
  1.1480 +						}
  1.1481 +					}
  1.1482 +				}
  1.1483 +			}
  1.1484 +			else
  1.1485 +			{
  1.1486 +				int t = VCOUNT - sy;
  1.1487 +				if ((t >= 0) && (t < sizeY))
  1.1488 +				{
  1.1489 +					int sx = (a1 & 0x1FF);
  1.1490 +					if (((sx < 240) || (((sx+sizeX)&511) < 240)) && !(a0 & 0x0200))
  1.1491 +					{
  1.1492 +						if (a0 & 0x2000)
  1.1493 +						{
  1.1494 +							if (a1 & 0x2000)
  1.1495 +								t = sizeY - t - 1;
  1.1496 +							int c = (a2 & 0x3FF);
  1.1497 +							if ((DISPCNT & 7) > 2 && (c < 512))
  1.1498 +								continue;
  1.1499 +
  1.1500 +							int inc = 32;
  1.1501 +							if (DISPCNT & 0x40)
  1.1502 +							{
  1.1503 +								inc = sizeX >> 2;
  1.1504 +							}
  1.1505 +							else
  1.1506 +							{
  1.1507 +								c &= 0x3FE;
  1.1508 +							}
  1.1509 +							int xxx = 0;
  1.1510 +							if (a1 & 0x1000)
  1.1511 +								xxx = sizeX-1;
  1.1512 +							int address = 0x10000 + ((((c+ (t>>3) * inc) << 5)
  1.1513 +							                          + ((t & 7) << 3) + ((xxx>>3)<<6) + (xxx & 7))&0x7fff);
  1.1514 +							if (a1 & 0x1000)
  1.1515 +								xxx = 7;
  1.1516 +							// u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6);
  1.1517 +							for (int xx = 0; xx < sizeX; xx++)
  1.1518 +							{
  1.1519 +								if (sx < 240)
  1.1520 +								{
  1.1521 +									u8 color = vram[address];
  1.1522 +									if (color)
  1.1523 +									{
  1.1524 +										lineOBJWin[sx] = 1;
  1.1525 +									}
  1.1526 +								}
  1.1527 +
  1.1528 +								sx = (sx+1) & 511;
  1.1529 +								if (a1 & 0x1000)
  1.1530 +								{
  1.1531 +									xxx--;
  1.1532 +									address--;
  1.1533 +									if (xxx == -1)
  1.1534 +									{
  1.1535 +										address -= 56;
  1.1536 +										xxx      = 7;
  1.1537 +									}
  1.1538 +									if (address < 0x10000)
  1.1539 +										address += 0x8000;
  1.1540 +								}
  1.1541 +								else
  1.1542 +								{
  1.1543 +									xxx++;
  1.1544 +									address++;
  1.1545 +									if (xxx == 8)
  1.1546 +									{
  1.1547 +										address += 56;
  1.1548 +										xxx      = 0;
  1.1549 +									}
  1.1550 +									if (address > 0x17fff)
  1.1551 +										address -= 0x8000;
  1.1552 +								}
  1.1553 +							}
  1.1554 +						}
  1.1555 +						else
  1.1556 +						{
  1.1557 +							if (a1 & 0x2000)
  1.1558 +								t = sizeY - t - 1;
  1.1559 +							int c = (a2 & 0x3FF);
  1.1560 +							if ((DISPCNT & 7) > 2 && (c < 512))
  1.1561 +								continue;
  1.1562 +
  1.1563 +							int inc = 32;
  1.1564 +							if (DISPCNT & 0x40)
  1.1565 +							{
  1.1566 +								inc = sizeX >> 3;
  1.1567 +							}
  1.1568 +							int xxx = 0;
  1.1569 +							if (a1 & 0x1000)
  1.1570 +								xxx = sizeX - 1;
  1.1571 +							int address = 0x10000 + ((((c + (t>>3) * inc)<<5)
  1.1572 +							                          + ((t & 7)<<2) + ((xxx>>3)<<5) + ((xxx & 7) >> 1))&0x7fff);
  1.1573 +							// u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6);
  1.1574 +							// int palette = (a2 >> 8) & 0xF0;
  1.1575 +							if (a1 & 0x1000)
  1.1576 +							{
  1.1577 +								xxx = 7;
  1.1578 +								for (int xx = sizeX - 1; xx >= 0; xx--)
  1.1579 +								{
  1.1580 +									if (sx < 240)
  1.1581 +									{
  1.1582 +										u8 color = vram[address];
  1.1583 +										if (xx & 1)
  1.1584 +										{
  1.1585 +											color = (color >> 4);
  1.1586 +										}
  1.1587 +										else
  1.1588 +											color &= 0x0F;
  1.1589 +
  1.1590 +										if (color)
  1.1591 +										{
  1.1592 +											lineOBJWin[sx] = 1;
  1.1593 +										}
  1.1594 +									}
  1.1595 +									sx = (sx+1) & 511;
  1.1596 +									xxx--;
  1.1597 +									if (!(xx & 1))
  1.1598 +										address--;
  1.1599 +									if (xxx == -1)
  1.1600 +									{
  1.1601 +										xxx      = 7;
  1.1602 +										address -= 28;
  1.1603 +									}
  1.1604 +									if (address < 0x10000)
  1.1605 +										address += 0x8000;
  1.1606 +								}
  1.1607 +							}
  1.1608 +							else
  1.1609 +							{
  1.1610 +								for (int xx = 0; xx < sizeX; xx++)
  1.1611 +								{
  1.1612 +									if (sx < 240)
  1.1613 +									{
  1.1614 +										u8 color = vram[address];
  1.1615 +										if (xx & 1)
  1.1616 +										{
  1.1617 +											color = (color >> 4);
  1.1618 +										}
  1.1619 +										else
  1.1620 +											color &= 0x0F;
  1.1621 +
  1.1622 +										if (color)
  1.1623 +										{
  1.1624 +											lineOBJWin[sx] = 1;
  1.1625 +										}
  1.1626 +									}
  1.1627 +									sx = (sx+1) & 511;
  1.1628 +									xxx++;
  1.1629 +									if (xx & 1)
  1.1630 +										address++;
  1.1631 +									if (xxx == 8)
  1.1632 +									{
  1.1633 +										address += 28;
  1.1634 +										xxx      = 0;
  1.1635 +									}
  1.1636 +									if (address > 0x17fff)
  1.1637 +										address -= 0x8000;
  1.1638 +								}
  1.1639 +							}
  1.1640 +						}
  1.1641 +					}
  1.1642 +				}
  1.1643 +			}
  1.1644 +		}
  1.1645 +	}
  1.1646 +}
  1.1647 +
  1.1648 +inline u32 gfxIncreaseBrightness(u32 color, int coeff)
  1.1649 +{
  1.1650 +	int r = (color & 0x1F);
  1.1651 +	int g = ((color >> 5) & 0x1F);
  1.1652 +	int b = ((color >> 10) & 0x1F);
  1.1653 +
  1.1654 +	r = r + (((31 - r) * coeff) >> 4);
  1.1655 +	g = g + (((31 - g) * coeff) >> 4);
  1.1656 +	b = b + (((31 - b) * coeff) >> 4);
  1.1657 +	if (r > 31)
  1.1658 +		r = 31;
  1.1659 +	if (g > 31)
  1.1660 +		g = 31;
  1.1661 +	if (b > 31)
  1.1662 +		b = 31;
  1.1663 +	color = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r;
  1.1664 +	return color;
  1.1665 +}
  1.1666 +
  1.1667 +inline void gfxIncreaseBrightness(u32 *line, int coeff)
  1.1668 +{
  1.1669 +	for (int x = 0; x < 240; x++)
  1.1670 +	{
  1.1671 +		u32 color = *line;
  1.1672 +		int r     = (color & 0x1F);
  1.1673 +		int g     = ((color >> 5) & 0x1F);
  1.1674 +		int b     = ((color >> 10) & 0x1F);
  1.1675 +
  1.1676 +		r = r + (((31 - r) * coeff) >> 4);
  1.1677 +		g = g + (((31 - g) * coeff) >> 4);
  1.1678 +		b = b + (((31 - b) * coeff) >> 4);
  1.1679 +		if (r > 31)
  1.1680 +			r = 31;
  1.1681 +		if (g > 31)
  1.1682 +			g = 31;
  1.1683 +		if (b > 31)
  1.1684 +			b = 31;
  1.1685 +		*line++ = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r;
  1.1686 +	}
  1.1687 +}
  1.1688 +
  1.1689 +inline u32 gfxDecreaseBrightness(u32 color, int coeff)
  1.1690 +{
  1.1691 +	int r = (color & 0x1F);
  1.1692 +	int g = ((color >> 5) & 0x1F);
  1.1693 +	int b = ((color >> 10) & 0x1F);
  1.1694 +
  1.1695 +	r = r - ((r * coeff) >> 4);
  1.1696 +	g = g - ((g * coeff) >> 4);
  1.1697 +	b = b - ((b * coeff) >> 4);
  1.1698 +	if (r < 0)
  1.1699 +		r = 0;
  1.1700 +	if (g < 0)
  1.1701 +		g = 0;
  1.1702 +	if (b < 0)
  1.1703 +		b = 0;
  1.1704 +	color = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r;
  1.1705 +
  1.1706 +	return color;
  1.1707 +}
  1.1708 +
  1.1709 +inline void gfxDecreaseBrightness(u32 *line, int coeff)
  1.1710 +{
  1.1711 +	for (int x = 0; x < 240; x++)
  1.1712 +	{
  1.1713 +		u32 color = *line;
  1.1714 +		int r     = (color & 0x1F);
  1.1715 +		int g     = ((color >> 5) & 0x1F);
  1.1716 +		int b     = ((color >> 10) & 0x1F);
  1.1717 +
  1.1718 +		r = r - ((r * coeff) >> 4);
  1.1719 +		g = g - ((g * coeff) >> 4);
  1.1720 +		b = b - ((b * coeff) >> 4);
  1.1721 +		if (r < 0)
  1.1722 +			r = 0;
  1.1723 +		if (g < 0)
  1.1724 +			g = 0;
  1.1725 +		if (b < 0)
  1.1726 +			b = 0;
  1.1727 +		*line++ = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r;
  1.1728 +	}
  1.1729 +}
  1.1730 +
  1.1731 +inline u32 gfxAlphaBlend(u32 color, u32 color2, int ca, int cb)
  1.1732 +{
  1.1733 +	if (color < 0x80000000)
  1.1734 +	{
  1.1735 +		int r  = (color & 0x1F);
  1.1736 +		int g  = ((color >> 5) & 0x1F);
  1.1737 +		int b  = ((color >> 10) & 0x1F);
  1.1738 +		int r0 = (color2 & 0x1F);
  1.1739 +		int g0 = ((color2 >> 5) & 0x1F);
  1.1740 +		int b0 = ((color2 >> 10) & 0x1F);
  1.1741 +
  1.1742 +		r = ((r * ca) >> 4) + ((r0 * cb) >> 4);
  1.1743 +		g = ((g * ca) >> 4) + ((g0 * cb) >> 4);
  1.1744 +		b = ((b * ca) >> 4) + ((b0 * cb) >> 4);
  1.1745 +
  1.1746 +		if (r > 31)
  1.1747 +			r = 31;
  1.1748 +		if (g > 31)
  1.1749 +			g = 31;
  1.1750 +		if (b > 31)
  1.1751 +			b = 31;
  1.1752 +
  1.1753 +		return (color & 0xFFFF0000) | (b << 10) | (g << 5) | r;
  1.1754 +	}
  1.1755 +	return color;
  1.1756 +}
  1.1757 +
  1.1758 +inline void gfxAlphaBlend(u32 *ta, u32 *tb, int ca, int cb)
  1.1759 +{
  1.1760 +	for (int x = 0; x < 240; x++)
  1.1761 +	{
  1.1762 +		u32 color = *ta;
  1.1763 +		if (color < 0x80000000)
  1.1764 +		{
  1.1765 +			int r      = (color & 0x1F);
  1.1766 +			int g      = ((color >> 5) & 0x1F);
  1.1767 +			int b      = ((color >> 10) & 0x1F);
  1.1768 +			u32 color2 = (*tb++);
  1.1769 +			int r0     = (color2 & 0x1F);
  1.1770 +			int g0     = ((color2 >> 5) & 0x1F);
  1.1771 +			int b0     = ((color2 >> 10) & 0x1F);
  1.1772 +
  1.1773 +			r = ((r * ca) >> 4) + ((r0 * cb) >> 4);
  1.1774 +			g = ((g * ca) >> 4) + ((g0 * cb) >> 4);
  1.1775 +			b = ((b * ca) >> 4) + ((b0 * cb) >> 4);
  1.1776 +
  1.1777 +			if (r > 31)
  1.1778 +				r = 31;
  1.1779 +			if (g > 31)
  1.1780 +				g = 31;
  1.1781 +			if (b > 31)
  1.1782 +				b = 31;
  1.1783 +
  1.1784 +			*ta++ = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r;
  1.1785 +		}
  1.1786 +		else
  1.1787 +		{
  1.1788 +			ta++;
  1.1789 +			tb++;
  1.1790 +		}
  1.1791 +	}
  1.1792 +}
  1.1793 +
  1.1794 +#endif // VBA_GBA_GFX_H