all repos — mgba @ 871c21fb6cf9041b4ac7c7f7074e0b2eb77484fe

mGBA Game Boy Advance Emulator

src/gba/renderers/software-bg.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "gba/renderers/software-private.h"
  7
  8#include <mgba/core/interface.h>
  9#include <mgba/internal/gba/gba.h>
 10
 11#define MODE_2_COORD_OVERFLOW \
 12	localX = x & (sizeAdjusted - 1); \
 13	localY = y & (sizeAdjusted - 1); \
 14
 15#define MODE_2_COORD_NO_OVERFLOW \
 16	if ((x | y) & ~(sizeAdjusted - 1)) { \
 17		continue; \
 18	} \
 19	localX = x; \
 20	localY = y;
 21
 22#define MODE_2_MOSAIC(COORD) \
 23		if (!mosaicWait) { \
 24			COORD \
 25			mapData = screenBase[(localX >> 11) + (((localY >> 7) & 0x7F0) << background->size)]; \
 26			pixelData = charBase[(mapData << 6) + ((localY & 0x700) >> 5) + ((localX & 0x700) >> 8)]; \
 27			\
 28			mosaicWait = mosaicH; \
 29		} else { \
 30			--mosaicWait; \
 31		}
 32
 33#define MODE_2_NO_MOSAIC(COORD) \
 34	COORD \
 35	mapData = screenBase[(localX >> 11) + (((localY >> 7) & 0x7F0) << background->size)]; \
 36	pixelData = charBase[(mapData << 6) + ((localY & 0x700) >> 5) + ((localX & 0x700) >> 8)];
 37
 38#define MODE_2_LOOP(MOSAIC, COORD, BLEND, OBJWIN) \
 39	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) { \
 40		x += background->dx; \
 41		y += background->dy; \
 42		\
 43		uint32_t current = *pixel; \
 44		MOSAIC(COORD) \
 45		if (pixelData) { \
 46			COMPOSITE_256_ ## OBJWIN (BLEND, 0); \
 47		} \
 48	}
 49
 50#define DRAW_BACKGROUND_MODE_2(BLEND, OBJWIN) \
 51	if (background->overflow) { \
 52		if (mosaicH > 1) { \
 53			MODE_2_LOOP(MODE_2_MOSAIC, MODE_2_COORD_OVERFLOW, BLEND, OBJWIN); \
 54		} else { \
 55			MODE_2_LOOP(MODE_2_NO_MOSAIC, MODE_2_COORD_OVERFLOW, BLEND, OBJWIN); \
 56		} \
 57	} else { \
 58		if (mosaicH > 1) { \
 59			MODE_2_LOOP(MODE_2_MOSAIC, MODE_2_COORD_NO_OVERFLOW, BLEND, OBJWIN); \
 60		} else { \
 61			MODE_2_LOOP(MODE_2_NO_MOSAIC, MODE_2_COORD_NO_OVERFLOW, BLEND, OBJWIN); \
 62		} \
 63	}
 64
 65void GBAVideoSoftwareRendererDrawBackgroundMode2(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int inY) {
 66	int sizeAdjusted = 0x8000 << background->size;
 67
 68	BACKGROUND_BITMAP_INIT;
 69
 70	uint8_t* screenBase = &((uint8_t*) renderer->d.vram)[background->screenBase];
 71	uint8_t* charBase = &((uint8_t*) renderer->d.vram)[background->charBase];
 72	uint8_t mapData;
 73	uint8_t pixelData = 0;
 74
 75	int outX;
 76	uint32_t* pixel;
 77
 78	if (!objwinSlowPath) {
 79		if (!(flags & FLAG_TARGET_2)) {
 80			DRAW_BACKGROUND_MODE_2(NoBlend, NO_OBJWIN);
 81		} else {
 82			DRAW_BACKGROUND_MODE_2(Blend, NO_OBJWIN);
 83		}
 84	} else {
 85		if (!(flags & FLAG_TARGET_2)) {
 86			DRAW_BACKGROUND_MODE_2(NoBlend, OBJWIN);
 87		} else {
 88			DRAW_BACKGROUND_MODE_2(Blend, OBJWIN);
 89		}
 90	}
 91}
 92
 93void GBAVideoSoftwareRendererDrawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int inY) {
 94	BACKGROUND_BITMAP_INIT;
 95
 96	uint32_t color = renderer->normalPalette[0];
 97
 98	int outX;
 99	uint32_t* pixel;
100	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
101		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
102
103		if (!mosaicWait) {
104			LOAD_16(color, ((localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS) << 1, renderer->d.vram);
105			color = mColorFrom555(color);
106#ifndef COLOR_16_BIT
107			unsigned color32;
108			color32 = 0;
109			color32 |= (color << 3) & 0xF8;
110			color32 |= (color << 6) & 0xF800;
111			color32 |= (color << 9) & 0xF80000;
112			color32 |= (color32 >> 5) & 0x070707;
113			color = color32;
114#elif COLOR_5_6_5
115			uint16_t color16 = 0;
116			color16 |= (color & 0x001F) << 11;
117			color16 |= (color & 0x03E0) << 1;
118			color16 |= (color & 0x7C00) >> 10;
119			color = color16;
120#endif
121			mosaicWait = mosaicH;
122		} else {
123			--mosaicWait;
124		}
125
126		uint32_t current = *pixel;
127		if (!objwinSlowPath || (!(current & FLAG_OBJWIN)) != objwinOnly) {
128			unsigned mergedFlags = flags;
129			if (current & FLAG_OBJWIN) {
130				mergedFlags = objwinFlags;
131			}
132			if (!variant) {
133				_compositeBlendObjwin(renderer, pixel, color | mergedFlags, current);
134			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
135				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | mergedFlags, current);
136			} else if (renderer->blendEffect == BLEND_DARKEN) {
137				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | mergedFlags, current);
138			}
139		}
140	}
141}
142
143void GBAVideoSoftwareRendererDrawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int inY) {
144	BACKGROUND_BITMAP_INIT;
145
146	uint16_t color = renderer->normalPalette[0];
147	uint32_t offset = 0;
148	if (GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt)) {
149		offset = 0xA000;
150	}
151
152	int outX;
153	uint32_t* pixel;
154	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
155		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
156
157		if (!mosaicWait) {
158			color = ((uint8_t*)renderer->d.vram)[offset + (localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS];
159
160			mosaicWait = mosaicH;
161		} else {
162			--mosaicWait;
163		}
164
165		uint32_t current = *pixel;
166		if (color && IS_WRITABLE(current)) {
167			if (!objwinSlowPath) {
168				_compositeBlendNoObjwin(renderer, pixel, palette[color] | flags, current);
169			} else if (objwinForceEnable || (!(current & FLAG_OBJWIN)) == objwinOnly) {
170				color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
171				unsigned mergedFlags = flags;
172				if (current & FLAG_OBJWIN) {
173					mergedFlags = objwinFlags;
174				}
175				_compositeBlendObjwin(renderer, pixel, currentPalette[color] | mergedFlags, current);
176			}
177		}
178	}
179}
180
181void GBAVideoSoftwareRendererDrawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int inY) {
182	BACKGROUND_BITMAP_INIT;
183
184	uint32_t color = renderer->normalPalette[0];
185	uint32_t offset = 0;
186	if (GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt)) {
187		offset = 0xA000;
188	}
189
190	int outX;
191	uint32_t* pixel;
192	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
193		BACKGROUND_BITMAP_ITERATE(160, 128);
194
195		if (!mosaicWait) {
196			LOAD_16(color, offset + (localX >> 8) * 2 + (localY >> 8) * 320, renderer->d.vram);
197#ifndef COLOR_16_BIT
198			unsigned color32 = 0;
199			color32 |= (color << 9) & 0xF80000;
200			color32 |= (color << 3) & 0xF8;
201			color32 |= (color << 6) & 0xF800;
202			color32 |= (color32 >> 5) & 0x070707;
203			color = color32;
204#elif COLOR_5_6_5
205			uint16_t color16 = 0;
206			color16 |= (color & 0x001F) << 11;
207			color16 |= (color & 0x03E0) << 1;
208			color16 |= (color & 0x7C00) >> 10;
209			color = color16;
210#endif
211			mosaicWait = mosaicH;
212		} else {
213			--mosaicWait;
214		}
215
216		uint32_t current = *pixel;
217		if (!objwinSlowPath || (!(current & FLAG_OBJWIN)) != objwinOnly) {
218			unsigned mergedFlags = flags;
219			if (current & FLAG_OBJWIN) {
220				mergedFlags = objwinFlags;
221			}
222			if (!variant) {
223				_compositeBlendObjwin(renderer, pixel, color | mergedFlags, current);
224			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
225				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | mergedFlags, current);
226			} else if (renderer->blendEffect == BLEND_DARKEN) {
227				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | mergedFlags, current);
228			}
229		}
230	}
231}