Debian Codegraph @

mednafen/vb/vip_draw.inc

raw symbols SCIP
1 #include "vb.h"
2
3 #define BGM_AFFINE 0x2
4 #define BGM_OBJ 0x3
5
6
7 static void DrawBG(uint8 *target, uint16 RealY, bool lr, uint8 bgmap_base_raw, bool overplane, uint16 overplane_char, uint32 SourceX, uint32 SourceY, uint32 scx, uint32 scy, uint16 DestX, uint16 DestY, uint16 DestWidth, uint16 DestHeight)
8 {
9 const uint16 *CHR16 = CHR_RAM;
10 const uint16 *BGMap = DRAM;
11 uint32 BGMap_Base = bgmap_base_raw << 12;
12 int32 start_x, final_x;
13 const uint32 bgsc_overplane = DRAM[overplane_char];
14 const uint32 BGMap_XCount = 1 << scx;
15 const uint32 BGMap_YCount = 1 << scy;
16 const uint32 SourceX_Size = 512 * BGMap_XCount;
17 const uint32 SourceY_Size = 512 * BGMap_YCount;
18 const uint32 SourceX_Mask = overplane ? 0x1FFF : (SourceX_Size - 1);
19 const uint32 SourceY_Mask = overplane ? 0x1FFF : (SourceY_Size - 1);
20
21 if((uint16)(RealY - DestY) > DestHeight)
22 return;
23
24 //printf("%d, %d, %d, %d\n", overplane, srcXSize, srcYSize, bgmap_base_raw);
25
26 DestX = sign_10_to_s16(DestX);
27
28 if(DestX & 0x8000)
29 SourceX -= DestX;
30
31 start_x = (int16)DestX;
32 final_x = (int16)DestX + DestWidth;
33
34 if(start_x < 0)
35 start_x = 0;
36
37 if(final_x > 383)
38 final_x = 383;
39
40 if(start_x > final_x)
41 return;
42
43 // Optimization:
44 SourceY &= SourceY_Mask;
45 BGMap_Base |= (((SourceY >> 3) & 0x3F) * 0x40) | (((SourceY << 3) & ~0xFFF) << scx);
46
47 for(int x = start_x; x <= final_x; x++)
48 {
49 uint32 bgsc;
50 uint32 char_no;
51 uint32 palette_selector;
52 uint32 hflip_xor;
53 uint32 vflip_xor;
54
55 SourceX &= SourceX_Mask;
56
57 bgsc = bgsc_overplane;
58
59 if(SourceX < SourceX_Size && SourceY < SourceY_Size)
60 bgsc = BGMap[(BGMap_Base | ((SourceX << 3) & ~0xFFF) | ((SourceX >> 3) & 0x3F)) & 0xFFFF];
61
62 char_no = bgsc & 0x7FF;
63 palette_selector = bgsc >> 14;
64 hflip_xor = (bgsc & 0x2000) ? 7 : 0; //(((int32)bgsc << 18) >> 31) & 0x7;
65 vflip_xor = (bgsc & 0x1000) ? 7 : 0; //(((int32)bgsc << 19) >> 31) & 0x7;
66
67 unsigned int char_sub_y = vflip_xor ^ (SourceY & 0x7);
68
69 if(!(SourceX & 7) && (x + 7) <= final_x)
70 {
71 uint32 pixels = CHR16[char_no * 8 + char_sub_y];
72
73 #if 0
74 unsigned int char_sub_x;
75 uint8 *sub_target = target + x + 8;
76
77 for(int sub_x = -8; sub_x < 0; sub_x++)
78 {
79 if(pixels & 3) sub_target[sub_x] = GPLT_Cache[palette_selector][pixels & 3];
80 pixels >>= 2;
81 }
82 #endif
83
84 if(bgsc & 0x2000)
85 {
86 if((pixels >> 14) & 3) target[0 + x] = GPLT_Cache[palette_selector][(pixels >> 14) & 3];
87 if((pixels >> 12) & 3) target[1 + x] = GPLT_Cache[palette_selector][(pixels >> 12) & 3];
88 if((pixels >> 10) & 3) target[2 + x] = GPLT_Cache[palette_selector][(pixels >> 10) & 3];
89 if((pixels >> 8) & 3) target[3 + x] = GPLT_Cache[palette_selector][(pixels >> 8) & 3];
90 if((pixels >> 6) & 3) target[4 + x] = GPLT_Cache[palette_selector][(pixels >> 6) & 3];
91 if((pixels >> 4) & 3) target[5 + x] = GPLT_Cache[palette_selector][(pixels >> 4) & 3];
92 if((pixels >> 2) & 3) target[6 + x] = GPLT_Cache[palette_selector][(pixels >> 2) & 3];
93 if((pixels >> 0) & 3) target[7 + x] = GPLT_Cache[palette_selector][(pixels >> 0) & 3];
94 }
95 else
96 {
97 if((pixels >> 0) & 3) target[0 + x] = GPLT_Cache[palette_selector][(pixels >> 0) & 3];
98 if((pixels >> 2) & 3) target[1 + x] = GPLT_Cache[palette_selector][(pixels >> 2) & 3];
99 if((pixels >> 4) & 3) target[2 + x] = GPLT_Cache[palette_selector][(pixels >> 4) & 3];
100 if((pixels >> 6) & 3) target[3 + x] = GPLT_Cache[palette_selector][(pixels >> 6) & 3];
101 if((pixels >> 8) & 3) target[4 + x] = GPLT_Cache[palette_selector][(pixels >> 8) & 3];
102 if((pixels >> 10) & 3) target[5 + x] = GPLT_Cache[palette_selector][(pixels >> 10) & 3];
103 if((pixels >> 12) & 3) target[6 + x] = GPLT_Cache[palette_selector][(pixels >> 12) & 3];
104 if((pixels >> 14) & 3) target[7 + x] = GPLT_Cache[palette_selector][(pixels >> 14) & 3];
105 }
106
107 x += 7;
108 SourceX += 8;
109 }
110 else
111 {
112 unsigned int char_sub_x;
113
114 char_sub_x = hflip_xor ^ (SourceX & 0x7);
115
116 uint8 pixel = (CHR16[char_no * 8 + char_sub_y] >> (char_sub_x * 2)) & 0x3;
117
118 if(pixel)
119 target[x] = GPLT_Cache[palette_selector][pixel]; //target[x] = (GPLT[palette_selector] >> (pixel * 2)) & 0x3;
120 SourceX++;
121 }
122 }
123 }
124
125 static void DrawAffine(uint8 *target, uint16 RealY, bool lr, uint32 ParamBase, uint32 BGMap_Base, bool OverplaneMode, uint16 OverplaneChar, uint32 scx, uint32 scy,
126 uint16 DestX, uint16 DestY, uint16 DestWidth, uint16 DestHeight)
127 {
128 const uint16 *CHR16 = CHR_RAM;
129 const uint16 *BGMap = DRAM;
130
131 const uint32 BGMap_XCount = 1 << scx;
132 const uint32 BGMap_YCount = 1 << scy;
133 const uint32 SourceX_Size = 512 * BGMap_XCount;
134 const uint32 SourceY_Size = 512 * BGMap_YCount;
135
136 const uint16 *param_ptr = &DRAM[(ParamBase + 8 * (RealY - DestY)) & 0xFFFF];
137 int16 mx = param_ptr[0], mp = (ParallaxDisabled ? 0 : param_ptr[1]), my = param_ptr[2], dx = param_ptr[3], dy = param_ptr[4];
138
139 uint32 SourceX, SourceY;
140 uint32 SourceX_Mask, SourceY_Mask;
141
142 int32 start_x, final_x;
143 const uint32 bgsc_overplane = DRAM[OverplaneChar];
144
145
146 DestX = sign_10_to_s16(DestX);
147
148 if((uint16)(RealY - DestY) > DestHeight)
149 return;
150
151 SourceX = (int32)mx << 6;
152 SourceY = (int32)my << 6;
153
154 if(DestX & 0x8000)
155 {
156 SourceX += dx * (65536 - DestX);
157 SourceY += dy * (65536 - DestX);
158 }
159
160 if(mp >= 0 && lr)
161 {
162 SourceX += dx * mp;
163 SourceY += dy * mp;
164 }
165 else if(mp < 0 && !lr)
166 {
167 SourceX += dx * -mp;
168 SourceY += dy * -mp;
169 }
170
171 if(OverplaneMode)
172 {
173 SourceX_Mask = 0x3FFFFFF; //(((uint32)SourceX_Size << 9) * 2) - 1;
174 SourceY_Mask = 0x3FFFFFF; //(((uint32)SourceY_Size << 9) * 2) - 1;
175 }
176 else
177 {
178 SourceX_Mask = ((uint32)SourceX_Size << 9) - 1;
179 SourceY_Mask = ((uint32)SourceY_Size << 9) - 1;
180 }
181
182 start_x = (int16)DestX;
183 final_x = (int16)DestX + DestWidth;
184
185 if(start_x < 0)
186 start_x = 0;
187
188 if(final_x > 383)
189 final_x = 383;
190
191 if(dy == 0) // Optimization for no rotation.
192 {
193 SourceY &= SourceY_Mask;
194
195 if(SourceY >= (SourceY_Size << 9))
196 return;
197
198 BGMap_Base |= (((SourceY >> 6) & ~0xFFF) << scx) | (((SourceY >> 12) & 0x3F) * 0x40);
199 for(int x = start_x; x <= final_x; x++)
200 {
201 uint32 bgsc;
202 uint32 hflip_xor;
203 uint32 vflip_xor;
204 uint32 pixel = 0;
205
206 SourceX &= SourceX_Mask;
207
208 bgsc = bgsc_overplane;
209
210 if(SourceX < (SourceX_Size << 9))
211 bgsc = BGMap[(BGMap_Base | ((SourceX >> 6) & ~0xFFF) | ((SourceX >> 12) & 0x3F)) & 0xFFFF];
212
213 //hflip_xor = bgsc & 0x2000 ? 0xE : 0;
214 //vflip_xor = bgsc & 0x1000 ? 0x7 : 0;
215 hflip_xor = ((int32)(bgsc << 18) >> 30) & 0xE;
216 vflip_xor = ((int32)(bgsc << 19) >> 31) & 0x7;
217
218 unsigned int char_sub_y = vflip_xor ^ ((SourceY >> 9) & 0x7);
219 unsigned int char_sub_x = hflip_xor ^ ((SourceX >> 8) & 0xE);
220
221 pixel = (CHR16[((bgsc & 0x7FF) * 8) | char_sub_y] >> char_sub_x) & 0x3;
222
223 if(pixel)
224 target[x] = GPLT_Cache[bgsc >> 14][pixel];
225
226 SourceX += dx;
227 }
228 }
229 else
230 for(int x = start_x; x <= final_x; x++)
231 {
232 uint32 bgsc;
233 uint32 char_no;
234 uint32 palette_selector;
235 uint32 hflip_xor;
236 uint32 vflip_xor;
237 uint8 pixel = 0;
238
239 SourceX &= SourceX_Mask;
240 SourceY &= SourceY_Mask;
241
242 bgsc = bgsc_overplane;
243
244 if(SourceX < (SourceX_Size << 9) && SourceY < (SourceY_Size << 9))
245 {
246 uint32 m_index = ((SourceX >> 6) & ~0xFFF) + (((SourceY >> 6) & ~0xFFF) << scx);
247 uint32 sub_index = ((SourceX >> 12) & 0x3F) + (((SourceY >> 12) & 0x3F) * 0x40);
248
249 bgsc = BGMap[(BGMap_Base | m_index | sub_index) & 0xFFFF];
250
251 //bgsc = BGMap[(BGMapBase + (SourceX >> 12) + (SourceY >> 12) * (SourceX_Size >> 3)) & 0xFFFF ];
252 }
253 char_no = bgsc & 0x7FF;
254 palette_selector = bgsc >> 14;
255 hflip_xor = bgsc & 0x2000 ? 7 : 0; //(((int32)bgsc << 18) >> 31) & 0x7;
256 vflip_xor = bgsc & 0x1000 ? 7 : 0; //(((int32)bgsc << 19) >> 31) & 0x7;
257
258 unsigned int char_sub_y = vflip_xor ^ ((SourceY >> 9) & 0x7);
259 unsigned int char_sub_x = hflip_xor ^ ((SourceX >> 9) & 0x7);
260
261 pixel = (CHR16[char_no * 8 + char_sub_y] >> (char_sub_x * 2)) & 0x3;
262
263 if(pixel)
264 target[x] = GPLT_Cache[palette_selector][pixel];
265
266 SourceX += dx;
267 SourceY += dy;
268 }
269 }
270
271 static int obj_search_which;
272
273 static void DrawOBJ(uint8 *fb[2], uint16 Y, bool lron[2])
274 {
275 const uint16 *CHR16 = CHR_RAM;
276
277 int32 start_oam;
278 int32 end_oam;
279
280 start_oam = SPT[obj_search_which];
281
282 end_oam = 1023;
283 if(obj_search_which)
284 end_oam = SPT[obj_search_which - 1];
285
286 int32 oam = start_oam;
287 do
288 {
289 const uint16 *oam_ptr = &DRAM[(0x1E000 + (oam * 8)) >> 1];
290 const uint32 jy = oam_ptr[2];
291 const uint32 tile_y = (Y - jy) & 0xFF; // I think this mask is right. See: http://www.planetvb.com/modules/newbb/viewtopic.php?topic_id=3797&forum=2
292
293 if(tile_y >= 8)
294 continue;
295
296 uint32 jx = oam_ptr[0];
297 uint32 jp = ParallaxDisabled ? 0 : (oam_ptr[1] & 0x3FFF);
298 uint32 palette_selector = oam_ptr[3] >> 14;
299 uint32 vflip_xor = (oam_ptr[3] & 0x1000) ? 7 : 0;
300 uint32 char_sub_y = vflip_xor ^ tile_y;
301 bool jlron[2] = { (bool)(oam_ptr[1] & 0x8000), (bool)(oam_ptr[1] & 0x4000) };
302 uint32 char_no = oam_ptr[3] & 0x7FF;
303 const uint32 pixels_save = CHR16[char_no * 8 + char_sub_y];
304
305 for(int lr = 0; lr < 2; lr++)
306 {
307 if(!(jlron[lr] & lron[lr]))
308 continue;
309
310 uint32 pixels = pixels_save;
311 int32 x = sign_x_to_s32(10, (jx + (lr ? jp : -jp))); // It may actually be 9, TODO?
312
313 if(x >= -7 && x < 384) // Make sure we always keep the pitch of our 384x8 buffer large enough(with padding before and after the visible space)
314 {
315 uint8 *target = &fb[lr][x];
316
317 if(oam_ptr[3] & 0x2000)
318 {
319 target += 7;
320
321 for(int meow = 8; meow; meow--)
322 {
323 if(pixels & 3)
324 *target = JPLT_Cache[palette_selector][pixels & 3];
325 target--;
326 pixels >>= 2;
327 }
328 }
329 else
330 {
331 for(int meow = 8; meow; meow--)
332 {
333 if(pixels & 3)
334 *target = JPLT_Cache[palette_selector][pixels & 3];
335 target++;
336 pixels >>= 2;
337 }
338 }
339 #if 0
340 if(oam_ptr[3] & 0x2000)
341 {
342 if((pixels >> 14) & 3) fb[lr][0 + x] = JPLT_Cache[palette_selector][(pixels >> 14) & 3];
343 if((pixels >> 12) & 3) fb[lr][1 + x] = JPLT_Cache[palette_selector][(pixels >> 12) & 3];
344 if((pixels >> 10) & 3) fb[lr][2 + x] = JPLT_Cache[palette_selector][(pixels >> 10) & 3];
345 if((pixels >> 8) & 3) fb[lr][3 + x] = JPLT_Cache[palette_selector][(pixels >> 8) & 3];
346 if((pixels >> 6) & 3) fb[lr][4 + x] = JPLT_Cache[palette_selector][(pixels >> 6) & 3];
347 if((pixels >> 4) & 3) fb[lr][5 + x] = JPLT_Cache[palette_selector][(pixels >> 4) & 3];
348 if((pixels >> 2) & 3) fb[lr][6 + x] = JPLT_Cache[palette_selector][(pixels >> 2) & 3];
349 if((pixels >> 0) & 3) fb[lr][7 + x] = JPLT_Cache[palette_selector][(pixels >> 0) & 3];
350 }
351 else
352 {
353 if((pixels >> 0) & 3) fb[lr][0 + x] = JPLT_Cache[palette_selector][(pixels >> 0) & 3];
354 if((pixels >> 2) & 3) fb[lr][1 + x] = JPLT_Cache[palette_selector][(pixels >> 2) & 3];
355 if((pixels >> 4) & 3) fb[lr][2 + x] = JPLT_Cache[palette_selector][(pixels >> 4) & 3];
356 if((pixels >> 6) & 3) fb[lr][3 + x] = JPLT_Cache[palette_selector][(pixels >> 6) & 3];
357 if((pixels >> 8) & 3) fb[lr][4 + x] = JPLT_Cache[palette_selector][(pixels >> 8) & 3];
358 if((pixels >> 10) & 3) fb[lr][5 + x] = JPLT_Cache[palette_selector][(pixels >> 10) & 3];
359 if((pixels >> 12) & 3) fb[lr][6 + x] = JPLT_Cache[palette_selector][(pixels >> 12) & 3];
360 if((pixels >> 14) & 3) fb[lr][7 + x] = JPLT_Cache[palette_selector][(pixels >> 14) & 3];
361 }
362 #endif
363
364 }
365
366 }
367 } while( (oam = (oam - 1) & 1023) != end_oam);
368
369 }
370
371
372 void VIP_DrawBlock(uint8 block_no, uint8 *fb_l, uint8 *fb_r)
373 {
374 for(int y = 0; y < 8; y++)
375 {
376 memset(fb_l + y * 512, BKCOL, 384);
377 memset(fb_r + y * 512, BKCOL, 384);
378 }
379
380 obj_search_which = 3;
381
382 for(int world = 31; world >= 0; world--)
383 {
384 const uint16 *world_ptr = &DRAM[(0x1D800 + world * 0x20) >> 1];
385
386 uint32 bgmap_base = world_ptr[0] & 0xF;
387 bool end = world_ptr[0] & 0x40;
388 bool over = world_ptr[0] & 0x80;
389 uint32 scy = (world_ptr[0] >> 8) & 3;
390 uint32 scx = (world_ptr[0] >> 10) & 3;
391 uint32 bgm = (world_ptr[0] >> 12) & 3;
392 bool lron[2] = { (bool)(world_ptr[0] & 0x8000), (bool)(world_ptr[0] & 0x4000) };
393
394 uint16 gx = sign_11_to_s16(world_ptr[1]);
395 uint16 gp = ParallaxDisabled ? 0 : sign_9_to_s16(world_ptr[2]);
396 uint16 gy = sign_11_to_s16(world_ptr[3]);
397 uint16 mx = world_ptr[4];
398 uint16 mp = ParallaxDisabled ? 0 : sign_9_to_s16(world_ptr[5]);
399 uint16 my = world_ptr[6];
400 uint16 window_width = sign_11_to_s16(world_ptr[7]);
401 uint16 window_height = (world_ptr[8] & 0x3FF);
402 uint32 param_base = (world_ptr[9] & 0xFFF0);
403 uint16 overplane_char = world_ptr[10];
404
405 if(end)
406 break;
407
408 if(((512 << scx) + (512 << scy)) > 4096)
409 {
410 printf("BG Size too large for world: %d(scx=%d, scy=%d)\n", world, scx, scy);
411 }
412
413 // if(world != 2)
414 // continue;
415
416 // if(block_no == 8)
417 // printf("World: %d; gx: %d, gp: %d, gy: %d, mx: %d, mp: %d, my: %d, window_width: %d, window_height: %d\n", world, gx, gp, gy, mx, mp, my, window_width, window_height);
418
419 for(int y = 0; y < 8; y++)
420 {
421 uint8 *fb[2] = { &fb_l[y * 512], &fb_r[y * 512] };
422
423 if(bgm == BGM_OBJ)
424 {
425 if(!lron[0] || !lron[1])
426 printf("Bad OBJ World? %d(%d/%d) %d~%d\n", world, lron[0], lron[1], SPT[obj_search_which], obj_search_which ? (SPT[obj_search_which - 1] + 1) : 0);
427
428 DrawOBJ(fb, (block_no * 8) + y, lron);
429 }
430 else if(bgm == BGM_AFFINE)
431 {
432 //if(((block_no * 8) + y) == 128)
433 // printf("Draw affine: %d %d\n", gx, gp);
434 for(int lr = 0; lr < 2; lr++)
435 {
436 if(lron[lr])
437 {
438 DrawAffine(fb[lr], (block_no * 8) + y, lr, param_base, bgmap_base * 4096, over, overplane_char, scx, scy,
439 gx + (lr ? gp : -gp), gy, window_width, window_height);
440 }
441 }
442 }
443 else
444 for(int lr = 0; lr < 2; lr++)
445 {
446 uint16 srcX, srcY;
447 uint16 RealY = (block_no * 8) + y;
448 uint16 DestX;
449 uint16 DestY;
450
451 srcX = mx + (lr ? mp : -mp);
452 srcY = my + (RealY - gy);
453
454 DestX = gx + (lr ? gp : -gp);
455 DestY = gy;
456
457 if(lron[lr])
458 {
459 if(bgm == 1) // HBias
460 srcX += (int16)DRAM[(param_base + (((RealY - DestY) * 2) | lr)) & 0xFFFF];
461
462 DrawBG(fb[lr], RealY, lr, bgmap_base, over, overplane_char, (int32)(int16)srcX, (int32)(int16)srcY, scx, scy, DestX, DestY, window_width, window_height);
463 }
464 }
465 }
466
467 if(bgm == BGM_OBJ)
468 if(obj_search_which)
469 obj_search_which--;
470
471 }
472
473
474 }