src/gb/renderers/software.c (view raw)
1/* Copyright (c) 2013-2016 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 "software.h"
7
8#include "core/tile-cache.h"
9#include "gb/io.h"
10#include "util/memory.h"
11
12static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer, enum GBModel model);
13static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer);
14static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
15static void GBVideoSoftwareRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value);
16static void GBVideoSoftwareRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address);
17static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax);
18static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
19static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer);
20static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
21static void GBVideoSoftwareRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
22
23static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int sx, int sy);
24static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y);
25
26static void _clearScreen(struct GBVideoSoftwareRenderer* renderer) {
27 // TODO: Dynamic from dmgPalette
28#ifdef COLOR_16_BIT
29#ifdef COLOR_5_6_5
30 color_t palette0 = 0xFFDF;
31#else
32 color_t palette0 = 0x7FFF;
33#endif
34#else
35 color_t palette0 = 0xF8F8F8;
36#endif
37
38 int y;
39 for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS; ++y) {
40 color_t* row = &renderer->outputBuffer[renderer->outputBufferStride * y];
41 int x;
42 for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; x += 4) {
43 row[x + 0] = palette0;
44 row[x + 1] = palette0;
45 row[x + 2] = palette0;
46 row[x + 3] = palette0;
47 }
48 }
49}
50
51void GBVideoSoftwareRendererCreate(struct GBVideoSoftwareRenderer* renderer) {
52 renderer->d.init = GBVideoSoftwareRendererInit;
53 renderer->d.deinit = GBVideoSoftwareRendererDeinit;
54 renderer->d.writeVideoRegister = GBVideoSoftwareRendererWriteVideoRegister;
55 renderer->d.writePalette = GBVideoSoftwareRendererWritePalette;
56 renderer->d.writeVRAM = GBVideoSoftwareRendererWriteVRAM;
57 renderer->d.drawRange = GBVideoSoftwareRendererDrawRange;
58 renderer->d.finishScanline = GBVideoSoftwareRendererFinishScanline;
59 renderer->d.finishFrame = GBVideoSoftwareRendererFinishFrame;
60 renderer->d.getPixels = GBVideoSoftwareRendererGetPixels;
61 renderer->d.putPixels = GBVideoSoftwareRendererPutPixels;
62
63 renderer->temporaryBuffer = 0;
64}
65
66static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
67 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
68 softwareRenderer->scy = 0;
69 softwareRenderer->scx = 0;
70 softwareRenderer->wy = 0;
71 softwareRenderer->currentWy = 0;
72 softwareRenderer->wx = 0;
73 softwareRenderer->model = model;
74
75 _clearScreen(softwareRenderer);
76}
77
78static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer) {
79 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
80 UNUSED(softwareRenderer);
81}
82
83static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
84 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
85 switch (address) {
86 case REG_LCDC:
87 if (GBRegisterLCDCIsEnable(softwareRenderer->lcdc) && !GBRegisterLCDCIsEnable(value)) {
88 _clearScreen(softwareRenderer);
89 }
90 softwareRenderer->lcdc = value;
91 break;
92 case REG_SCY:
93 softwareRenderer->scy = value;
94 break;
95 case REG_SCX:
96 softwareRenderer->scx = value;
97 break;
98 case REG_WY:
99 softwareRenderer->wy = value;
100 break;
101 case REG_WX:
102 softwareRenderer->wx = value;
103 break;
104 }
105 return value;
106}
107
108static void GBVideoSoftwareRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
109 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
110#ifdef COLOR_16_BIT
111#ifdef COLOR_5_6_5
112 color_t color = 0;
113 color |= (value & 0x001F) << 11;
114 color |= (value & 0x03E0) << 1;
115 color |= (value & 0x7C00) >> 10;
116#else
117 color_t color = value;
118#endif
119#else
120 color_t color = 0;
121 color |= (value << 3) & 0xF8;
122 color |= (value << 6) & 0xF800;
123 color |= (value << 9) & 0xF80000;
124#endif
125 softwareRenderer->palette[index] = color;
126 if (renderer->cache) {
127 mTileCacheWritePalette(renderer->cache, index << 1);
128 }
129}
130
131static void GBVideoSoftwareRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
132 if (renderer->cache) {
133 mTileCacheWriteVRAM(renderer->cache, address);
134 }
135}
136
137static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
138 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
139 uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
140 if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
141 maps += GB_SIZE_MAP;
142 }
143 if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc) || softwareRenderer->model >= GB_MODEL_CGB) {
144 if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && endX >= softwareRenderer->wx - 7) {
145 if (softwareRenderer->wx - 7 > 0) {
146 GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, softwareRenderer->wx - 7, softwareRenderer->scx, softwareRenderer->scy + y);
147 }
148
149 maps = &softwareRenderer->d.vram[GB_BASE_MAP];
150 if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
151 maps += GB_SIZE_MAP;
152 }
153 GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, softwareRenderer->wx - 7, endX, 7 - softwareRenderer->wx, softwareRenderer->currentWy);
154 } else {
155 GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, endX, softwareRenderer->scx, softwareRenderer->scy + y);
156 }
157 } else {
158 memset(&softwareRenderer->row[startX], 0, endX - startX);
159 }
160
161 if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc)) {
162 size_t i;
163 for (i = 0; i < oamMax; ++i) {
164 GBVideoSoftwareRendererDrawObj(softwareRenderer, &obj[i], startX, endX, y);
165 }
166 }
167 color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
168 int x;
169 for (x = startX; x + 7 < (endX & ~7); x += 8) {
170 row[x] = softwareRenderer->palette[softwareRenderer->row[x] & 0x7F];
171 row[x + 1] = softwareRenderer->palette[softwareRenderer->row[x + 1] & 0x7F];
172 row[x + 2] = softwareRenderer->palette[softwareRenderer->row[x + 2] & 0x7F];
173 row[x + 3] = softwareRenderer->palette[softwareRenderer->row[x + 3] & 0x7F];
174 row[x + 4] = softwareRenderer->palette[softwareRenderer->row[x + 4] & 0x7F];
175 row[x + 5] = softwareRenderer->palette[softwareRenderer->row[x + 5] & 0x7F];
176 row[x + 6] = softwareRenderer->palette[softwareRenderer->row[x + 6] & 0x7F];
177 row[x + 7] = softwareRenderer->palette[softwareRenderer->row[x + 7] & 0x7F];
178 }
179 for (; x < endX; ++x) {
180 row[x] = softwareRenderer->palette[softwareRenderer->row[x] & 0x7F];
181 }
182}
183
184static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
185 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
186 if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && softwareRenderer->wx - 7 < GB_VIDEO_HORIZONTAL_PIXELS) {
187 ++softwareRenderer->currentWy;
188 }
189}
190
191static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
192 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
193
194 if (softwareRenderer->temporaryBuffer) {
195 mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
196 softwareRenderer->temporaryBuffer = 0;
197 }
198 softwareRenderer->currentWy = 0;
199}
200
201static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int sx, int sy) {
202 uint8_t* data = renderer->d.vram;
203 uint8_t* attr = &maps[GB_SIZE_VRAM_BANK0];
204 if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
205 data += 0x1000;
206 }
207 int topY = ((sy >> 3) & 0x1F) * 0x20;
208 int bottomY = sy & 7;
209 if (startX < 0) {
210 startX = 0;
211 }
212 int x;
213 if ((startX + sx) & 7) {
214 int startX2 = startX + 8 - ((startX + sx) & 7);
215 for (x = startX; x < startX2; ++x) {
216 uint8_t* localData = data;
217 int localY = bottomY;
218 int topX = ((x + sx) >> 3) & 0x1F;
219 int bottomX = 7 - ((x + sx) & 7);
220 int bgTile;
221 if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
222 bgTile = maps[topX + topY];
223 } else {
224 bgTile = ((int8_t*) maps)[topX + topY];
225 }
226 int p = 0;
227 if (renderer->model >= GB_MODEL_CGB) {
228 GBObjAttributes attrs = attr[topX + topY];
229 p = GBObjAttributesGetCGBPalette(attrs) * 4;
230 if (GBObjAttributesIsPriority(attrs) && GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
231 p |= 0x80;
232 }
233 if (GBObjAttributesIsBank(attrs)) {
234 localData += GB_SIZE_VRAM_BANK0;
235 }
236 if (GBObjAttributesIsYFlip(attrs)) {
237 localY = 7 - bottomY;
238 }
239 if (GBObjAttributesIsXFlip(attrs)) {
240 bottomX = 7 - bottomX;
241 }
242 }
243 uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
244 uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
245 tileDataUpper >>= bottomX;
246 tileDataLower >>= bottomX;
247 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
248 }
249 startX = startX2;
250 }
251 for (x = startX; x < endX; x += 8) {
252 uint8_t* localData = data;
253 int localY = bottomY;
254 int topX = ((x + sx) >> 3) & 0x1F;
255 int bgTile;
256 if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
257 bgTile = maps[topX + topY];
258 } else {
259 bgTile = ((int8_t*) maps)[topX + topY];
260 }
261 int p = 0;
262 if (renderer->model >= GB_MODEL_CGB) {
263 GBObjAttributes attrs = attr[topX + topY];
264 p = GBObjAttributesGetCGBPalette(attrs) * 4;
265 if (GBObjAttributesIsPriority(attrs) && GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
266 p |= 0x80;
267 }
268 if (GBObjAttributesIsBank(attrs)) {
269 localData += GB_SIZE_VRAM_BANK0;
270 }
271 if (GBObjAttributesIsYFlip(attrs)) {
272 localY = 7 - bottomY;
273 }
274 if (GBObjAttributesIsXFlip(attrs)) {
275 uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
276 uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
277 renderer->row[x + 0] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
278 renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
279 renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
280 renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
281 renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
282 renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
283 renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
284 renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
285 continue;
286 }
287 }
288 uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
289 uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
290 renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
291 renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
292 renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
293 renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
294 renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
295 renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
296 renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
297 renderer->row[x + 0] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
298 }
299}
300
301static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y) {
302 int ix = obj->x - 8;
303 if (endX < ix || startX >= ix + 8) {
304 return;
305 }
306 if (obj->x < endX) {
307 endX = obj->x;
308 }
309 if (obj->x - 8 > startX) {
310 startX = obj->x - 8;
311 }
312 if (startX < 0) {
313 startX = 0;
314 }
315 uint8_t* data = renderer->d.vram;
316 int tileOffset = 0;
317 int bottomY;
318 if (GBObjAttributesIsYFlip(obj->attr)) {
319 bottomY = 7 - ((y - obj->y - 16) & 7);
320 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
321 ++tileOffset;
322 }
323 } else {
324 bottomY = (y - obj->y - 16) & 7;
325 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
326 ++tileOffset;
327 }
328 }
329 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && obj->tile & 1) {
330 --tileOffset;
331 }
332 uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0x63 : 0x60;
333 uint8_t mask2 = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x83;
334 int p;
335 if (renderer->model >= GB_MODEL_CGB) {
336 p = (GBObjAttributesGetCGBPalette(obj->attr) + 8) * 4;
337 if (GBObjAttributesIsBank(obj->attr)) {
338 data += GB_SIZE_VRAM_BANK0;
339 }
340 if (!GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
341 mask = 0x60;
342 mask2 = 0x83;
343 }
344 } else {
345 p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
346 }
347 int bottomX;
348 int x = startX;
349 if ((x - obj->x) & 7) {
350 for (; x < endX; ++x) {
351 if (GBObjAttributesIsXFlip(obj->attr)) {
352 bottomX = (x - obj->x) & 7;
353 } else {
354 bottomX = 7 - ((x - obj->x) & 7);
355 }
356 int objTile = obj->tile + tileOffset;
357 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
358 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
359 tileDataUpper >>= bottomX;
360 tileDataLower >>= bottomX;
361 color_t current = renderer->row[x];
362 if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
363 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
364 }
365 }
366 } else if (GBObjAttributesIsXFlip(obj->attr)) {
367 int objTile = obj->tile + tileOffset;
368 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
369 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
370 color_t current;
371 current = renderer->row[x];
372 if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
373 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
374 }
375 current = renderer->row[x + 1];
376 if (((tileDataUpper | tileDataLower) & 2) && !(current & mask) && (current & mask2) <= 0x80) {
377 renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
378 }
379 current = renderer->row[x + 2];
380 if (((tileDataUpper | tileDataLower) & 4) && !(current & mask) && (current & mask2) <= 0x80) {
381 renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
382 }
383 current = renderer->row[x + 3];
384 if (((tileDataUpper | tileDataLower) & 8) && !(current & mask) && (current & mask2) <= 0x80) {
385 renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
386 }
387 current = renderer->row[x + 4];
388 if (((tileDataUpper | tileDataLower) & 16) && !(current & mask) && (current & mask2) <= 0x80) {
389 renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
390 }
391 current = renderer->row[x + 5];
392 if (((tileDataUpper | tileDataLower) & 32) && !(current & mask) && (current & mask2) <= 0x80) {
393 renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
394 }
395 current = renderer->row[x + 6];
396 if (((tileDataUpper | tileDataLower) & 64) && !(current & mask) && (current & mask2) <= 0x80) {
397 renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
398 }
399 current = renderer->row[x + 7];
400 if (((tileDataUpper | tileDataLower) & 128) && !(current & mask) && (current & mask2) <= 0x80) {
401 renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
402 }
403 } else {
404 int objTile = obj->tile + tileOffset;
405 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
406 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
407 color_t current;
408 current = renderer->row[x + 7];
409 if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
410 renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
411 }
412 current = renderer->row[x + 6];
413 if (((tileDataUpper | tileDataLower) & 2) && !(current & mask) && (current & mask2) <= 0x80) {
414 renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
415 }
416 current = renderer->row[x + 5];
417 if (((tileDataUpper | tileDataLower) & 4) && !(current & mask) && (current & mask2) <= 0x80) {
418 renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
419 }
420 current = renderer->row[x + 4];
421 if (((tileDataUpper | tileDataLower) & 8) && !(current & mask) && (current & mask2) <= 0x80) {
422 renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
423 }
424 current = renderer->row[x + 3];
425 if (((tileDataUpper | tileDataLower) & 16) && !(current & mask) && (current & mask2) <= 0x80) {
426 renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
427 }
428 current = renderer->row[x + 2];
429 if (((tileDataUpper | tileDataLower) & 32) && !(current & mask) && (current & mask2) <= 0x80) {
430 renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
431 }
432 current = renderer->row[x + 1];
433 if (((tileDataUpper | tileDataLower) & 64) && !(current & mask) && (current & mask2) <= 0x80) {
434 renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
435 }
436 current = renderer->row[x];
437 if (((tileDataUpper | tileDataLower) & 128) && !(current & mask) && (current & mask2) <= 0x80) {
438 renderer->row[x] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
439 }
440 }
441}
442
443static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
444 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
445 // TODO: Share with GBAVideoSoftwareRendererGetPixels
446#ifdef COLOR_16_BIT
447 *stride = GB_VIDEO_HORIZONTAL_PIXELS;
448 if (!softwareRenderer->temporaryBuffer) {
449 softwareRenderer->temporaryBuffer = anonymousMemoryMap(GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
450 }
451 *pixels = softwareRenderer->temporaryBuffer;
452 unsigned y, x;
453 for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS; ++y) {
454 for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
455 color_t inColor = softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y + x];
456 uint32_t outColor;
457#ifdef COLOR_5_6_5
458 outColor = (inColor & 0x1F) << 19;
459 outColor |= (inColor & 0x7C0) << 5;
460 outColor |= (inColor & 0xF800) >> 8;
461#else
462 outColor = (inColor & 0x1F) << 3;
463 outColor |= (inColor & 0x3E0) << 6;
464 outColor |= (inColor & 0x7C00) << 9;
465#endif
466 softwareRenderer->temporaryBuffer[GB_VIDEO_HORIZONTAL_PIXELS * y + x] = outColor;
467 }
468 }
469#else
470 *stride = softwareRenderer->outputBufferStride;
471 *pixels = softwareRenderer->outputBuffer;
472#endif
473}
474
475
476static void GBVideoSoftwareRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
477 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
478 // TODO: Share with GBAVideoSoftwareRendererGetPixels
479
480 const color_t* colorPixels = pixels;
481 unsigned i;
482 for (i = 0; i < GB_VIDEO_VERTICAL_PIXELS; ++i) {
483 memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], GB_VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
484 }
485}