diff src/gba/Mode3.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/Mode3.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,443 @@
     1.4 +#include "GBAGfx.h"
     1.5 +#include "GBAGlobals.h"
     1.6 +
     1.7 +void mode3RenderLine()
     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 & 0x0400)
    1.22 +	{
    1.23 +		int changed = gfxBG2Changed;
    1.24 +
    1.25 +		if (gfxLastVCOUNT > VCOUNT)
    1.26 +			changed = 3;
    1.27 +
    1.28 +		gfxDrawRotScreen16Bit(BG2CNT, BG2X_L, BG2X_H,
    1.29 +		                      BG2Y_L, BG2Y_H, BG2PA, BG2PB,
    1.30 +		                      BG2PC, BG2PD,
    1.31 +		                      gfxBG2X, gfxBG2Y, changed,
    1.32 +		                      line2);
    1.33 +	}
    1.34 +
    1.35 +	gfxDrawSprites(lineOBJ);
    1.36 +
    1.37 +	u32 background = (READ16LE(&palette[0]) | 0x30000000);
    1.38 +
    1.39 +	for (int x = 0; x < 240; x++)
    1.40 +	{
    1.41 +		u32 color = background;
    1.42 +		u8  top   = 0x20;
    1.43 +
    1.44 +		if (line2[x] < color)
    1.45 +		{
    1.46 +			color = line2[x];
    1.47 +			top   = 0x04;
    1.48 +		}
    1.49 +
    1.50 +		if ((u8)(lineOBJ[x]>>24) < (u8)(color >>24))
    1.51 +		{
    1.52 +			color = lineOBJ[x];
    1.53 +			top   = 0x10;
    1.54 +		}
    1.55 +
    1.56 +		if ((top & 0x10) && (color & 0x00010000))
    1.57 +		{
    1.58 +			// semi-transparent OBJ
    1.59 +			u32 back = background;
    1.60 +			u8  top2 = 0x20;
    1.61 +
    1.62 +			if (line2[x] < back)
    1.63 +			{
    1.64 +				back = line2[x];
    1.65 +				top2 = 0x04;
    1.66 +			}
    1.67 +
    1.68 +			if (top2 & (BLDMOD>>8))
    1.69 +				color = gfxAlphaBlend(color, back,
    1.70 +				                      coeff[COLEV & 0x1F],
    1.71 +				                      coeff[(COLEV >> 8) & 0x1F]);
    1.72 +			else
    1.73 +			{
    1.74 +				switch ((BLDMOD >> 6) & 3)
    1.75 +				{
    1.76 +				case 2:
    1.77 +					if (BLDMOD & top)
    1.78 +						color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
    1.79 +					break;
    1.80 +				case 3:
    1.81 +					if (BLDMOD & top)
    1.82 +						color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
    1.83 +					break;
    1.84 +				}
    1.85 +			}
    1.86 +		}
    1.87 +
    1.88 +		lineMix[x] = color;
    1.89 +	}
    1.90 +	gfxBG2Changed = 0;
    1.91 +	gfxLastVCOUNT = VCOUNT;
    1.92 +}
    1.93 +
    1.94 +void mode3RenderLineNoWindow()
    1.95 +{
    1.96 +	u16 *palette = (u16 *)paletteRAM;
    1.97 +
    1.98 +	if (DISPCNT & 0x80)
    1.99 +	{
   1.100 +		for (int x = 0; x < 240; x++)
   1.101 +		{
   1.102 +			lineMix[x] = 0x7fff;
   1.103 +		}
   1.104 +		gfxLastVCOUNT = VCOUNT;
   1.105 +		return;
   1.106 +	}
   1.107 +
   1.108 +	if (layerEnable & 0x0400)
   1.109 +	{
   1.110 +		int changed = gfxBG2Changed;
   1.111 +
   1.112 +		if (gfxLastVCOUNT > VCOUNT)
   1.113 +			changed = 3;
   1.114 +
   1.115 +		gfxDrawRotScreen16Bit(BG2CNT, BG2X_L, BG2X_H,
   1.116 +		                      BG2Y_L, BG2Y_H, BG2PA, BG2PB,
   1.117 +		                      BG2PC, BG2PD,
   1.118 +		                      gfxBG2X, gfxBG2Y, changed,
   1.119 +		                      line2);
   1.120 +	}
   1.121 +
   1.122 +	gfxDrawSprites(lineOBJ);
   1.123 +
   1.124 +	u32 background = (READ16LE(&palette[0]) | 0x30000000);
   1.125 +
   1.126 +	for (int x = 0; x < 240; x++)
   1.127 +	{
   1.128 +		u32 color = background;
   1.129 +		u8  top   = 0x20;
   1.130 +
   1.131 +		if (line2[x] < color)
   1.132 +		{
   1.133 +			color = line2[x];
   1.134 +			top   = 0x04;
   1.135 +		}
   1.136 +
   1.137 +		if ((u8)(lineOBJ[x]>>24) < (u8)(color >>24))
   1.138 +		{
   1.139 +			color = lineOBJ[x];
   1.140 +			top   = 0x10;
   1.141 +		}
   1.142 +
   1.143 +		if (!(color & 0x00010000))
   1.144 +		{
   1.145 +			switch ((BLDMOD >> 6) & 3)
   1.146 +			{
   1.147 +			case 0:
   1.148 +				break;
   1.149 +			case 1:
   1.150 +			{
   1.151 +				if (top & BLDMOD)
   1.152 +				{
   1.153 +					u32 back = background;
   1.154 +					u8  top2 = 0x20;
   1.155 +
   1.156 +					if (line2[x] < back)
   1.157 +					{
   1.158 +						if (top != 0x04)
   1.159 +						{
   1.160 +							back = line2[x];
   1.161 +							top2 = 0x04;
   1.162 +						}
   1.163 +					}
   1.164 +
   1.165 +					if ((u8)(lineOBJ[x]>>24) < (u8)(back >> 24))
   1.166 +					{
   1.167 +						if (top != 0x10)
   1.168 +						{
   1.169 +							back = lineOBJ[x];
   1.170 +							top2 = 0x10;
   1.171 +						}
   1.172 +					}
   1.173 +
   1.174 +					if (top2 & (BLDMOD>>8))
   1.175 +						color = gfxAlphaBlend(color, back,
   1.176 +						                      coeff[COLEV & 0x1F],
   1.177 +						                      coeff[(COLEV >> 8) & 0x1F]);
   1.178 +				}
   1.179 +				break;
   1.180 +			}
   1.181 +			case 2:
   1.182 +				if (BLDMOD & top)
   1.183 +					color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
   1.184 +				break;
   1.185 +			case 3:
   1.186 +				if (BLDMOD & top)
   1.187 +					color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
   1.188 +				break;
   1.189 +			}
   1.190 +		}
   1.191 +		else
   1.192 +		{
   1.193 +			// semi-transparent OBJ
   1.194 +			u32 back = background;
   1.195 +			u8  top2 = 0x20;
   1.196 +
   1.197 +			if (line2[x] < back)
   1.198 +			{
   1.199 +				back = line2[x];
   1.200 +				top2 = 0x04;
   1.201 +			}
   1.202 +
   1.203 +			if (top2 & (BLDMOD>>8))
   1.204 +				color = gfxAlphaBlend(color, back,
   1.205 +				                      coeff[COLEV & 0x1F],
   1.206 +				                      coeff[(COLEV >> 8) & 0x1F]);
   1.207 +			else
   1.208 +			{
   1.209 +				switch ((BLDMOD >> 6) & 3)
   1.210 +				{
   1.211 +				case 2:
   1.212 +					if (BLDMOD & top)
   1.213 +						color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
   1.214 +					break;
   1.215 +				case 3:
   1.216 +					if (BLDMOD & top)
   1.217 +						color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
   1.218 +					break;
   1.219 +				}
   1.220 +			}
   1.221 +		}
   1.222 +
   1.223 +		lineMix[x] = color;
   1.224 +	}
   1.225 +	gfxBG2Changed = 0;
   1.226 +	gfxLastVCOUNT = VCOUNT;
   1.227 +}
   1.228 +
   1.229 +void mode3RenderLineAll()
   1.230 +{
   1.231 +	u16 *palette = (u16 *)paletteRAM;
   1.232 +
   1.233 +	if (DISPCNT & 0x80)
   1.234 +	{
   1.235 +		for (int x = 0; x < 240; x++)
   1.236 +		{
   1.237 +			lineMix[x] = 0x7fff;
   1.238 +		}
   1.239 +		gfxLastVCOUNT = VCOUNT;
   1.240 +		return;
   1.241 +	}
   1.242 +
   1.243 +	bool inWindow0 = false;
   1.244 +	bool inWindow1 = false;
   1.245 +
   1.246 +	if (layerEnable & 0x2000)
   1.247 +	{
   1.248 +		u8 v0 = WIN0V >> 8;
   1.249 +		u8 v1 = WIN0V & 255;
   1.250 +		inWindow0 = ((v0 == v1) && (v0 >= 0xe8));
   1.251 +		if (v1 >= v0)
   1.252 +			inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1);
   1.253 +		else
   1.254 +			inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1);
   1.255 +	}
   1.256 +	if (layerEnable & 0x4000)
   1.257 +	{
   1.258 +		u8 v0 = WIN1V >> 8;
   1.259 +		u8 v1 = WIN1V & 255;
   1.260 +		inWindow1 = ((v0 == v1) && (v0 >= 0xe8));
   1.261 +		if (v1 >= v0)
   1.262 +			inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1);
   1.263 +		else
   1.264 +			inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1);
   1.265 +	}
   1.266 +
   1.267 +	if (layerEnable & 0x0400)
   1.268 +	{
   1.269 +		int changed = gfxBG2Changed;
   1.270 +
   1.271 +		if (gfxLastVCOUNT > VCOUNT)
   1.272 +			changed = 3;
   1.273 +
   1.274 +		gfxDrawRotScreen16Bit(BG2CNT, BG2X_L, BG2X_H,
   1.275 +		                      BG2Y_L, BG2Y_H, BG2PA, BG2PB,
   1.276 +		                      BG2PC, BG2PD,
   1.277 +		                      gfxBG2X, gfxBG2Y, changed,
   1.278 +		                      line2);
   1.279 +	}
   1.280 +
   1.281 +	gfxDrawSprites(lineOBJ);
   1.282 +	gfxDrawOBJWin(lineOBJWin);
   1.283 +
   1.284 +	u8 inWin0Mask = WININ & 0xFF;
   1.285 +	u8 inWin1Mask = WININ >> 8;
   1.286 +	u8 outMask    = WINOUT & 0xFF;
   1.287 +
   1.288 +	u32 background = (READ16LE(&palette[0]) | 0x30000000);
   1.289 +
   1.290 +	for (int x = 0; x < 240; x++)
   1.291 +	{
   1.292 +		u32 color = background;
   1.293 +		u8  top   = 0x20;
   1.294 +		u8  mask  = outMask;
   1.295 +
   1.296 +		if (!(lineOBJWin[x] & 0x80000000))
   1.297 +		{
   1.298 +			mask = WINOUT >> 8;
   1.299 +		}
   1.300 +
   1.301 +		if (inWindow1)
   1.302 +		{
   1.303 +			if (gfxInWin1[x])
   1.304 +				mask = inWin1Mask;
   1.305 +		}
   1.306 +
   1.307 +		if (inWindow0)
   1.308 +		{
   1.309 +			if (gfxInWin0[x])
   1.310 +			{
   1.311 +				mask = inWin0Mask;
   1.312 +			}
   1.313 +		}
   1.314 +
   1.315 +		if ((mask & 4) && (line2[x] < color))
   1.316 +		{
   1.317 +			color = line2[x];
   1.318 +			top   = 0x04;
   1.319 +		}
   1.320 +
   1.321 +		if ((mask & 16) && ((u8)(lineOBJ[x]>>24) < (u8)(color >>24)))
   1.322 +		{
   1.323 +			color = lineOBJ[x];
   1.324 +			top   = 0x10;
   1.325 +		}
   1.326 +
   1.327 +		if (mask & 32)
   1.328 +		{
   1.329 +			if (!(color & 0x00010000))
   1.330 +			{
   1.331 +				switch ((BLDMOD >> 6) & 3)
   1.332 +				{
   1.333 +				case 0:
   1.334 +					break;
   1.335 +				case 1:
   1.336 +				{
   1.337 +					if (top & BLDMOD)
   1.338 +					{
   1.339 +						u32 back = background;
   1.340 +						u8  top2 = 0x20;
   1.341 +
   1.342 +						if ((mask & 4) && line2[x] < back)
   1.343 +						{
   1.344 +							if (top != 0x04)
   1.345 +							{
   1.346 +								back = line2[x];
   1.347 +								top2 = 0x04;
   1.348 +							}
   1.349 +						}
   1.350 +
   1.351 +						if ((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24))
   1.352 +						{
   1.353 +							if (top != 0x10)
   1.354 +							{
   1.355 +								back = lineOBJ[x];
   1.356 +								top2 = 0x10;
   1.357 +							}
   1.358 +						}
   1.359 +
   1.360 +						if (top2 & (BLDMOD>>8))
   1.361 +							color = gfxAlphaBlend(color, back,
   1.362 +							                      coeff[COLEV & 0x1F],
   1.363 +							                      coeff[(COLEV >> 8) & 0x1F]);
   1.364 +					}
   1.365 +					break;
   1.366 +				}
   1.367 +				case 2:
   1.368 +					if (BLDMOD & top)
   1.369 +						color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
   1.370 +					break;
   1.371 +				case 3:
   1.372 +					if (BLDMOD & top)
   1.373 +						color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
   1.374 +					break;
   1.375 +				}
   1.376 +			}
   1.377 +			else
   1.378 +			{
   1.379 +				// semi-transparent OBJ
   1.380 +				u32 back = background;
   1.381 +				u8  top2 = 0x20;
   1.382 +
   1.383 +				if ((mask & 4) && line2[x] < back)
   1.384 +				{
   1.385 +					back = line2[x];
   1.386 +					top2 = 0x04;
   1.387 +				}
   1.388 +
   1.389 +				if (top2 & (BLDMOD>>8))
   1.390 +					color = gfxAlphaBlend(color, back,
   1.391 +					                      coeff[COLEV & 0x1F],
   1.392 +					                      coeff[(COLEV >> 8) & 0x1F]);
   1.393 +				else
   1.394 +				{
   1.395 +					switch ((BLDMOD >> 6) & 3)
   1.396 +					{
   1.397 +					case 2:
   1.398 +						if (BLDMOD & top)
   1.399 +							color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
   1.400 +						break;
   1.401 +					case 3:
   1.402 +						if (BLDMOD & top)
   1.403 +							color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
   1.404 +						break;
   1.405 +					}
   1.406 +				}
   1.407 +			}
   1.408 +		}
   1.409 +		else if (color & 0x00010000)
   1.410 +		{
   1.411 +			// semi-transparent OBJ
   1.412 +			u32 back = background;
   1.413 +			u8  top2 = 0x20;
   1.414 +
   1.415 +			if ((mask & 4) && line2[x] < back)
   1.416 +			{
   1.417 +				back = line2[x];
   1.418 +				top2 = 0x04;
   1.419 +			}
   1.420 +
   1.421 +			if (top2 & (BLDMOD>>8))
   1.422 +				color = gfxAlphaBlend(color, back,
   1.423 +				                      coeff[COLEV & 0x1F],
   1.424 +				                      coeff[(COLEV >> 8) & 0x1F]);
   1.425 +			else
   1.426 +			{
   1.427 +				switch ((BLDMOD >> 6) & 3)
   1.428 +				{
   1.429 +				case 2:
   1.430 +					if (BLDMOD & top)
   1.431 +						color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]);
   1.432 +					break;
   1.433 +				case 3:
   1.434 +					if (BLDMOD & top)
   1.435 +						color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]);
   1.436 +					break;
   1.437 +				}
   1.438 +			}
   1.439 +		}
   1.440 +
   1.441 +		lineMix[x] = color;
   1.442 +	}
   1.443 +	gfxBG2Changed = 0;
   1.444 +	gfxLastVCOUNT = VCOUNT;
   1.445 +}
   1.446 +