diff src/gba/Mode1.cpp @ 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/Mode1.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,580 @@
     1.4 +#include "GBAGfx.h"
     1.5 +#include "GBAGlobals.h"
     1.6 +
     1.7 +void mode1RenderLine()
     1.8 +{
     1.9 +	u16 *palette = (u16 *)paletteRAM;
    1.10 +
    1.11 +	if (DISPCNT & 0x80)
    1.12 +	{
    1.13 +		for (int x = 0; x < 240; x++)
    1.14 +		{
    1.15 +			lineMix[x] = 0x7fff;
    1.16 +		}
    1.17 +		gfxLastVCOUNT = VCOUNT;
    1.18 +		return;
    1.19 +	}
    1.20 +
    1.21 +	if (layerEnable & 0x0100)
    1.22 +	{
    1.23 +		gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0);
    1.24 +	}
    1.25 +
    1.26 +	if (layerEnable & 0x0200)
    1.27 +	{
    1.28 +		gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1);
    1.29 +	}
    1.30 +
    1.31 +	if (layerEnable & 0x0400)
    1.32 +	{
    1.33 +		int changed = gfxBG2Changed;
    1.34 +		if (gfxLastVCOUNT > VCOUNT)
    1.35 +			changed = 3;
    1.36 +		gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H,
    1.37 +		                 BG2PA, BG2PB, BG2PC, BG2PD,
    1.38 +		                 gfxBG2X, gfxBG2Y, changed, line2);
    1.39 +	}
    1.40 +
    1.41 +	gfxDrawSprites(lineOBJ);
    1.42 +
    1.43 +	u32 backdrop = (READ16LE(&palette[0]) | 0x30000000);
    1.44 +
    1.45 +	for (int x = 0; x < 240; x++)
    1.46 +	{
    1.47 +		u32 color = backdrop;
    1.48 +		u8  top   = 0x20;
    1.49 +
    1.50 +		if (line0[x] < color)
    1.51 +		{
    1.52 +			color = line0[x];
    1.53 +			top   = 0x01;
    1.54 +		}
    1.55 +
    1.56 +		if ((u8)(line1[x]>>24) < (u8)(color >> 24))
    1.57 +		{
    1.58 +			color = line1[x];
    1.59 +			top   = 0x02;
    1.60 +		}
    1.61 +
    1.62 +		if ((u8)(line2[x]>>24) < (u8)(color >> 24))
    1.63 +		{
    1.64 +			color = line2[x];
    1.65 +			top   = 0x04;
    1.66 +		}
    1.67 +
    1.68 +		if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24))
    1.69 +		{
    1.70 +			color = lineOBJ[x];
    1.71 +			top   = 0x10;
    1.72 +		}
    1.73 +
    1.74 +		if ((top & 0x10) && (color & 0x00010000))
    1.75 +		{
    1.76 +			// semi-transparent OBJ
    1.77 +			u32 back = backdrop;
    1.78 +			u8  top2 = 0x20;
    1.79 +
    1.80 +			if ((u8)(line0[x]>>24) < (u8)(back >> 24))
    1.81 +			{
    1.82 +				back = line0[x];
    1.83 +				top2 = 0x01;
    1.84 +			}
    1.85 +
    1.86 +			if ((u8)(line1[x]>>24) < (u8)(back >> 24))
    1.87 +			{
    1.88 +				back = line1[x];
    1.89 +				top2 = 0x02;
    1.90 +			}
    1.91 +
    1.92 +			if ((u8)(line2[x]>>24) < (u8)(back >> 24))
    1.93 +			{
    1.94 +				back = line2[x];
    1.95 +				top2 = 0x04;
    1.96 +			}
    1.97 +
    1.98 +			if (top2 & (BLDMOD>>8))
    1.99 +				color = gfxAlphaBlend(color, back,
   1.100 +				                      coeff[COLEV & 0x1F],
   1.101 +				                      coeff[(COLEV >> 8) & 0x1F]);
   1.102 +			else
   1.103 +			{
   1.104 +				switch ((BLDMOD >> 6) & 3)
   1.105 +				{
   1.106 +				case 2:
   1.107 +					if (BLDMOD & top)
   1.108 +						color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
   1.109 +					break;
   1.110 +				case 3:
   1.111 +					if (BLDMOD & top)
   1.112 +						color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
   1.113 +					break;
   1.114 +				}
   1.115 +			}
   1.116 +		}
   1.117 +
   1.118 +		lineMix[x] = color;
   1.119 +	}
   1.120 +	gfxBG2Changed = 0;
   1.121 +	gfxLastVCOUNT = VCOUNT;
   1.122 +}
   1.123 +
   1.124 +void mode1RenderLineNoWindow()
   1.125 +{
   1.126 +	u16 *palette = (u16 *)paletteRAM;
   1.127 +
   1.128 +	if (DISPCNT & 0x80)
   1.129 +	{
   1.130 +		for (int x = 0; x < 240; x++)
   1.131 +		{
   1.132 +			lineMix[x] = 0x7fff;
   1.133 +		}
   1.134 +		gfxLastVCOUNT = VCOUNT;
   1.135 +		return;
   1.136 +	}
   1.137 +
   1.138 +	if (layerEnable & 0x0100)
   1.139 +	{
   1.140 +		gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0);
   1.141 +	}
   1.142 +
   1.143 +	if (layerEnable & 0x0200)
   1.144 +	{
   1.145 +		gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1);
   1.146 +	}
   1.147 +
   1.148 +	if (layerEnable & 0x0400)
   1.149 +	{
   1.150 +		int changed = gfxBG2Changed;
   1.151 +		if (gfxLastVCOUNT > VCOUNT)
   1.152 +			changed = 3;
   1.153 +		gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H,
   1.154 +		                 BG2PA, BG2PB, BG2PC, BG2PD,
   1.155 +		                 gfxBG2X, gfxBG2Y, changed, line2);
   1.156 +	}
   1.157 +
   1.158 +	gfxDrawSprites(lineOBJ);
   1.159 +
   1.160 +	u32 backdrop = (READ16LE(&palette[0]) | 0x30000000);
   1.161 +
   1.162 +	for (int x = 0; x < 240; x++)
   1.163 +	{
   1.164 +		u32 color = backdrop;
   1.165 +		u8  top   = 0x20;
   1.166 +
   1.167 +		if (line0[x] < color)
   1.168 +		{
   1.169 +			color = line0[x];
   1.170 +			top   = 0x01;
   1.171 +		}
   1.172 +
   1.173 +		if ((u8)(line1[x]>>24) < (u8)(color >> 24))
   1.174 +		{
   1.175 +			color = line1[x];
   1.176 +			top   = 0x02;
   1.177 +		}
   1.178 +
   1.179 +		if ((u8)(line2[x]>>24) < (u8)(color >> 24))
   1.180 +		{
   1.181 +			color = line2[x];
   1.182 +			top   = 0x04;
   1.183 +		}
   1.184 +
   1.185 +		if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24))
   1.186 +		{
   1.187 +			color = lineOBJ[x];
   1.188 +			top   = 0x10;
   1.189 +		}
   1.190 +
   1.191 +		if (!(color & 0x00010000))
   1.192 +		{
   1.193 +			switch ((BLDMOD >> 6) & 3)
   1.194 +			{
   1.195 +			case 0:
   1.196 +				break;
   1.197 +			case 1:
   1.198 +			{
   1.199 +				if (top & BLDMOD)
   1.200 +				{
   1.201 +					u32 back = backdrop;
   1.202 +					u8  top2 = 0x20;
   1.203 +					if ((u8)(line0[x]>>24) < (u8)(back >> 24))
   1.204 +					{
   1.205 +						if (top != 0x01)
   1.206 +						{
   1.207 +							back = line0[x];
   1.208 +							top2 = 0x01;
   1.209 +						}
   1.210 +					}
   1.211 +
   1.212 +					if ((u8)(line1[x]>>24) < (u8)(back >> 24))
   1.213 +					{
   1.214 +						if (top != 0x02)
   1.215 +						{
   1.216 +							back = line1[x];
   1.217 +							top2 = 0x02;
   1.218 +						}
   1.219 +					}
   1.220 +
   1.221 +					if ((u8)(line2[x]>>24) < (u8)(back >> 24))
   1.222 +					{
   1.223 +						if (top != 0x04)
   1.224 +						{
   1.225 +							back = line2[x];
   1.226 +							top2 = 0x04;
   1.227 +						}
   1.228 +					}
   1.229 +
   1.230 +					if ((u8)(lineOBJ[x]>>24) < (u8)(back >> 24))
   1.231 +					{
   1.232 +						if (top != 0x10)
   1.233 +						{
   1.234 +							back = lineOBJ[x];
   1.235 +							top2 = 0x10;
   1.236 +						}
   1.237 +					}
   1.238 +
   1.239 +					if (top2 & (BLDMOD>>8))
   1.240 +						color = gfxAlphaBlend(color, back,
   1.241 +						                      coeff[COLEV & 0x1F],
   1.242 +						                      coeff[(COLEV >> 8) & 0x1F]);
   1.243 +				}
   1.244 +				break;
   1.245 +			}
   1.246 +			case 2:
   1.247 +				if (BLDMOD & top)
   1.248 +					color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
   1.249 +				break;
   1.250 +			case 3:
   1.251 +				if (BLDMOD & top)
   1.252 +					color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
   1.253 +				break;
   1.254 +			}
   1.255 +		}
   1.256 +		else
   1.257 +		{
   1.258 +			// semi-transparent OBJ
   1.259 +			u32 back = backdrop;
   1.260 +			u8  top2 = 0x20;
   1.261 +
   1.262 +			if ((u8)(line0[x]>>24) < (u8)(back >> 24))
   1.263 +			{
   1.264 +				back = line0[x];
   1.265 +				top2 = 0x01;
   1.266 +			}
   1.267 +
   1.268 +			if ((u8)(line1[x]>>24) < (u8)(back >> 24))
   1.269 +			{
   1.270 +				back = line1[x];
   1.271 +				top2 = 0x02;
   1.272 +			}
   1.273 +
   1.274 +			if ((u8)(line2[x]>>24) < (u8)(back >> 24))
   1.275 +			{
   1.276 +				back = line2[x];
   1.277 +				top2 = 0x04;
   1.278 +			}
   1.279 +
   1.280 +			if (top2 & (BLDMOD>>8))
   1.281 +				color = gfxAlphaBlend(color, back,
   1.282 +				                      coeff[COLEV & 0x1F],
   1.283 +				                      coeff[(COLEV >> 8) & 0x1F]);
   1.284 +			else
   1.285 +			{
   1.286 +				switch ((BLDMOD >> 6) & 3)
   1.287 +				{
   1.288 +				case 2:
   1.289 +					if (BLDMOD & top)
   1.290 +						color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
   1.291 +					break;
   1.292 +				case 3:
   1.293 +					if (BLDMOD & top)
   1.294 +						color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
   1.295 +					break;
   1.296 +				}
   1.297 +			}
   1.298 +		}
   1.299 +
   1.300 +		lineMix[x] = color;
   1.301 +	}
   1.302 +	gfxBG2Changed = 0;
   1.303 +	gfxLastVCOUNT = VCOUNT;
   1.304 +}
   1.305 +
   1.306 +void mode1RenderLineAll()
   1.307 +{
   1.308 +	u16 *palette = (u16 *)paletteRAM;
   1.309 +
   1.310 +	if (DISPCNT & 0x80)
   1.311 +	{
   1.312 +		for (int x = 0; x < 240; x++)
   1.313 +		{
   1.314 +			lineMix[x] = 0x7fff;
   1.315 +		}
   1.316 +		gfxLastVCOUNT = VCOUNT;
   1.317 +		return;
   1.318 +	}
   1.319 +
   1.320 +	bool inWindow0 = false;
   1.321 +	bool inWindow1 = false;
   1.322 +
   1.323 +	if (layerEnable & 0x2000)
   1.324 +	{
   1.325 +		u8 v0 = WIN0V >> 8;
   1.326 +		u8 v1 = WIN0V & 255;
   1.327 +		inWindow0 = ((v0 == v1) && (v0 >= 0xe8));
   1.328 +		if (v1 >= v0)
   1.329 +			inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1);
   1.330 +		else
   1.331 +			inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1);
   1.332 +	}
   1.333 +	if (layerEnable & 0x4000)
   1.334 +	{
   1.335 +		u8 v0 = WIN1V >> 8;
   1.336 +		u8 v1 = WIN1V & 255;
   1.337 +		inWindow1 = ((v0 == v1) && (v0 >= 0xe8));
   1.338 +		if (v1 >= v0)
   1.339 +			inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1);
   1.340 +		else
   1.341 +			inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1);
   1.342 +	}
   1.343 +
   1.344 +	if (layerEnable & 0x0100)
   1.345 +	{
   1.346 +		gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0);
   1.347 +	}
   1.348 +
   1.349 +	if (layerEnable & 0x0200)
   1.350 +	{
   1.351 +		gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1);
   1.352 +	}
   1.353 +
   1.354 +	if (layerEnable & 0x0400)
   1.355 +	{
   1.356 +		int changed = gfxBG2Changed;
   1.357 +		if (gfxLastVCOUNT > VCOUNT)
   1.358 +			changed = 3;
   1.359 +		gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H,
   1.360 +		                 BG2PA, BG2PB, BG2PC, BG2PD,
   1.361 +		                 gfxBG2X, gfxBG2Y, changed, line2);
   1.362 +	}
   1.363 +
   1.364 +	gfxDrawSprites(lineOBJ);
   1.365 +	gfxDrawOBJWin(lineOBJWin);
   1.366 +
   1.367 +	u32 backdrop = (READ16LE(&palette[0]) | 0x30000000);
   1.368 +
   1.369 +	u8 inWin0Mask = WININ & 0xFF;
   1.370 +	u8 inWin1Mask = WININ >> 8;
   1.371 +	u8 outMask    = WINOUT & 0xFF;
   1.372 +
   1.373 +	for (int x = 0; x < 240; x++)
   1.374 +	{
   1.375 +		u32 color = backdrop;
   1.376 +		u8  top   = 0x20;
   1.377 +		u8  mask  = outMask;
   1.378 +
   1.379 +		if (!(lineOBJWin[x] & 0x80000000))
   1.380 +		{
   1.381 +			mask = WINOUT >> 8;
   1.382 +		}
   1.383 +
   1.384 +		if (inWindow1)
   1.385 +		{
   1.386 +			if (gfxInWin1[x])
   1.387 +				mask = inWin1Mask;
   1.388 +		}
   1.389 +
   1.390 +		if (inWindow0)
   1.391 +		{
   1.392 +			if (gfxInWin0[x])
   1.393 +			{
   1.394 +				mask = inWin0Mask;
   1.395 +			}
   1.396 +		}
   1.397 +
   1.398 +		if (line0[x] < color && (mask & 1))
   1.399 +		{
   1.400 +			color = line0[x];
   1.401 +			top   = 0x01;
   1.402 +		}
   1.403 +
   1.404 +		if ((u8)(line1[x]>>24) < (u8)(color >> 24) && (mask & 2))
   1.405 +		{
   1.406 +			color = line1[x];
   1.407 +			top   = 0x02;
   1.408 +		}
   1.409 +
   1.410 +		if ((u8)(line2[x]>>24) < (u8)(color >> 24) && (mask & 4))
   1.411 +		{
   1.412 +			color = line2[x];
   1.413 +			top   = 0x04;
   1.414 +		}
   1.415 +
   1.416 +		if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24) && (mask & 16))
   1.417 +		{
   1.418 +			color = lineOBJ[x];
   1.419 +			top   = 0x10;
   1.420 +		}
   1.421 +
   1.422 +		// special FX on the window
   1.423 +		if (mask & 32)
   1.424 +		{
   1.425 +			if (!(color & 0x00010000))
   1.426 +			{
   1.427 +				switch ((BLDMOD >> 6) & 3)
   1.428 +				{
   1.429 +				case 0:
   1.430 +					break;
   1.431 +				case 1:
   1.432 +				{
   1.433 +					if (top & BLDMOD)
   1.434 +					{
   1.435 +						u32 back = backdrop;
   1.436 +						u8  top2 = 0x20;
   1.437 +						if ((mask & 1) && (u8)(line0[x]>>24) < (u8)(back >> 24))
   1.438 +						{
   1.439 +							if (top != 0x01)
   1.440 +							{
   1.441 +								back = line0[x];
   1.442 +								top2 = 0x01;
   1.443 +							}
   1.444 +						}
   1.445 +
   1.446 +						if ((mask & 2) && (u8)(line1[x]>>24) < (u8)(back >> 24))
   1.447 +						{
   1.448 +							if (top != 0x02)
   1.449 +							{
   1.450 +								back = line1[x];
   1.451 +								top2 = 0x02;
   1.452 +							}
   1.453 +						}
   1.454 +
   1.455 +						if ((mask & 4) && (u8)(line2[x]>>24) < (u8)(back >> 24))
   1.456 +						{
   1.457 +							if (top != 0x04)
   1.458 +							{
   1.459 +								back = line2[x];
   1.460 +								top2 = 0x04;
   1.461 +							}
   1.462 +						}
   1.463 +
   1.464 +						if ((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24))
   1.465 +						{
   1.466 +							if (top != 0x10)
   1.467 +							{
   1.468 +								back = lineOBJ[x];
   1.469 +								top2 = 0x10;
   1.470 +							}
   1.471 +						}
   1.472 +
   1.473 +						if (top2 & (BLDMOD>>8))
   1.474 +							color = gfxAlphaBlend(color, back,
   1.475 +							                      coeff[COLEV & 0x1F],
   1.476 +							                      coeff[(COLEV >> 8) & 0x1F]);
   1.477 +					}
   1.478 +					break;
   1.479 +				}
   1.480 +				case 2:
   1.481 +					if (BLDMOD & top)
   1.482 +						color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
   1.483 +					break;
   1.484 +				case 3:
   1.485 +					if (BLDMOD & top)
   1.486 +						color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
   1.487 +					break;
   1.488 +				}
   1.489 +			}
   1.490 +			else
   1.491 +			{
   1.492 +				// semi-transparent OBJ
   1.493 +				u32 back = backdrop;
   1.494 +				u8  top2 = 0x20;
   1.495 +
   1.496 +				if ((mask & 1) && (u8)(line0[x]>>24) < (u8)(back >> 24))
   1.497 +				{
   1.498 +					back = line0[x];
   1.499 +					top2 = 0x01;
   1.500 +				}
   1.501 +
   1.502 +				if ((mask & 2) && (u8)(line1[x]>>24) < (u8)(back >> 24))
   1.503 +				{
   1.504 +					back = line1[x];
   1.505 +					top2 = 0x02;
   1.506 +				}
   1.507 +
   1.508 +				if ((mask & 4) && (u8)(line2[x]>>24) < (u8)(back >> 24))
   1.509 +				{
   1.510 +					back = line2[x];
   1.511 +					top2 = 0x04;
   1.512 +				}
   1.513 +
   1.514 +				if (top2 & (BLDMOD>>8))
   1.515 +					color = gfxAlphaBlend(color, back,
   1.516 +					                      coeff[COLEV & 0x1F],
   1.517 +					                      coeff[(COLEV >> 8) & 0x1F]);
   1.518 +				else
   1.519 +				{
   1.520 +					switch ((BLDMOD >> 6) & 3)
   1.521 +					{
   1.522 +					case 2:
   1.523 +						if (BLDMOD & top)
   1.524 +							color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
   1.525 +						break;
   1.526 +					case 3:
   1.527 +						if (BLDMOD & top)
   1.528 +							color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
   1.529 +						break;
   1.530 +					}
   1.531 +				}
   1.532 +			}
   1.533 +		}
   1.534 +		else if (color & 0x00010000)
   1.535 +		{
   1.536 +			// semi-transparent OBJ
   1.537 +			u32 back = backdrop;
   1.538 +			u8  top2 = 0x20;
   1.539 +
   1.540 +			if ((mask & 1) && (u8)(line0[x]>>24) < (u8)(back >> 24))
   1.541 +			{
   1.542 +				back = line0[x];
   1.543 +				top2 = 0x01;
   1.544 +			}
   1.545 +
   1.546 +			if ((mask & 2) && (u8)(line1[x]>>24) < (u8)(back >> 24))
   1.547 +			{
   1.548 +				back = line1[x];
   1.549 +				top2 = 0x02;
   1.550 +			}
   1.551 +
   1.552 +			if ((mask & 4) && (u8)(line2[x]>>24) < (u8)(back >> 24))
   1.553 +			{
   1.554 +				back = line2[x];
   1.555 +				top2 = 0x04;
   1.556 +			}
   1.557 +
   1.558 +			if (top2 & (BLDMOD>>8))
   1.559 +				color = gfxAlphaBlend(color, back,
   1.560 +				                      coeff[COLEV & 0x1F],
   1.561 +				                      coeff[(COLEV >> 8) & 0x1F]);
   1.562 +			else
   1.563 +			{
   1.564 +				switch ((BLDMOD >> 6) & 3)
   1.565 +				{
   1.566 +				case 2:
   1.567 +					if (BLDMOD & top)
   1.568 +						color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
   1.569 +					break;
   1.570 +				case 3:
   1.571 +					if (BLDMOD & top)
   1.572 +						color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
   1.573 +					break;
   1.574 +				}
   1.575 +			}
   1.576 +		}
   1.577 +
   1.578 +		lineMix[x] = color;
   1.579 +	}
   1.580 +	gfxBG2Changed = 0;
   1.581 +	gfxLastVCOUNT = VCOUNT;
   1.582 +}
   1.583 +