summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/decospr.c
blob: dae7f78726bf1446d971f8f55e5b1fdd10802d67 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/* Data East Sprite Chip
   DECO 52

   note, we have pri callbacks and checks to drop back to plain drawgfx because not all drivers are using pdrawgfx yet, they probably should be...
   some games have different visible areas, but are confirmed as the same sprite chip.

   games with alpha aren't supported here yet, in most cases they need better mixing anyway, probably rendering to screen buffers and manual mixing.
   see m_sprite_bitmap... (note, if using that you must also use BITMAP_FORMAT_RGB32 in the machine config)

   used by:

   dblewing.c
   tumblep.c
   dietgo.c
   supbtime.c
   simpl156.c
   deco156.c
   pktgaldx.c
   backfire.c
   darkseal.c
   sshangha.c (could probably use pdrawgfx, not m_sprite_bitmap)
   cbuster.c (could probably use pdrawgfx, not m_sprite_bitmap)
   mirage.c (could probably use pdrawgfx, not m_sprite_bitmap)
   cninja.c

   partially converted:
   deco32.c - video mixing

   difficult to convert:
   rohga.c - complex video mixing, 6bpp gfx..
   lemmings.c - priority stuff (plus sprites seem somewhat different anyway??)
   dassault.c - complex video mixing
   boogwing.c - complex video mixing

   notes:
   does the chip natively support 5bpp (tattass / nslasher) in hw, or is it done with doubled up chips?
   the information in deco16ic lists 3x sprite chips on those games, but there are only 2 spritelists.

*/


/*

STANDARD FORMAT

offs +0
-------- --------
efFbSssy yyyyyyyy

s = size (height)
S = size (width) (double wings)
f = flipy
F = flipx
b = flash/blink
y = ypos
e = extra priority bit (or at least externally detectable by mixer circuits)

offs +1
-------- --------
tttttttt tttttttt

t = sprite tile

offs +2
-------- --------
ppcccccx xxxxxxxx

c = colour palette
p = priority
x = xpos

offs +3
-------- -------- (unused)

in reality all the colour / priority (and extra priority) bits are externally detectable and can be used for
priority and mixing, the sprite palette in twocrude for example isn't in a single block (there is a gap for the tilemaps)
but the mixing circuits take care of this

ALTERNATE FORMAT (used by Mutant Fighter, Captain America)

offs +0
-------- --------
fFbyyyyy yyyyyyyy

f = flipy
F = flipx
b = flash/blink
y = ypos

offs +1
-------- --------
xxxxxxxx xxxxxxxx

x = xpos

offs +2
-------- --------
ssssSSSS pppccccc

s = size (height)
S = size (width) (double wings)

offs +3
-------- --------
tttttttt tttttttt

t = sprite tile

todo: the priotity callback for using pdrawgfx should really pack those 8 bits, and pass them instead of currently just
passing offs+2 which lacks the extra priority bit

todo: basic blend mixing

*/

#include "emu.h"
#include "decospr.h"


decospr_device_config::decospr_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
	: device_config(mconfig, static_alloc_device_config, "decospr_device", tag, owner, clock)
{
	m_gfxregion = 0;
	m_pricallback = NULL;
}

device_config *decospr_device_config::static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
{
	return global_alloc(decospr_device_config(mconfig, tag, owner, clock));
}

device_t *decospr_device_config::alloc_device(running_machine &machine) const
{
	return auto_alloc(&machine, decospr_device(machine, *this));
}

void decospr_device_config::set_gfx_region(device_config *device, int gfxregion)
{
	decospr_device_config *dev = downcast<decospr_device_config *>(device);
	dev->m_gfxregion = gfxregion;
//  printf("decospr_device_config::set_gfx_region()\n");
}

void decospr_device_config::set_pri_callback(device_config *device, decospr_priority_callback_func callback)
{
	decospr_device_config *dev = downcast<decospr_device_config *>(device);
	dev->m_pricallback = callback;
//  printf("decospr_device_config::set_pri_callback()\n");
}

decospr_device::decospr_device(running_machine &_machine, const decospr_device_config &config)
	: device_t(_machine, config),
	  m_config(config),
	  m_gfxregion(m_config.m_gfxregion),
	  m_pricallback(m_config.m_pricallback)
{
}

void decospr_device::device_start()
{
//  sprite_kludge_x = sprite_kludge_y = 0;
//  printf("decospr_device::device_start()\n");
	m_sprite_bitmap = 0;
	m_alt_format = 0;
	m_pixmask = 0xf;
	m_raw_shift = 4; // set to 8 on tattass / nslashers for the custom mixing (because they have 5bpp sprites, and shifting by 4 isn't good enough)
}

void decospr_device::device_reset()
{
	//printf("decospr_device::device_reset()\n");
}

/*
void decospr_device::decospr_sprite_kludge(int x, int y)
{
    sprite_kludge_x = x;
    sprite_kludge_y = y;
}
*/

void decospr_device::alloc_sprite_bitmap(running_machine* machine)
{
	m_sprite_bitmap =  auto_bitmap_alloc(machine, machine->primary_screen->width(), machine->primary_screen->height(), BITMAP_FORMAT_INDEXED16);
}

void decospr_device::set_pri_callback(decospr_priority_callback_func callback)
{
	m_pricallback = callback;
}




void decospr_device::draw_sprites( running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect, UINT16* spriteram, int sizewords, bool invert_flip )
{
	//printf("cliprect %04x, %04x\n", cliprect->min_y, cliprect->max_y);

	if (m_sprite_bitmap && m_pricallback)
		fatalerror("m_sprite_bitmap && m_pricallback is invalid");

	if (m_sprite_bitmap)
		bitmap_fill(m_sprite_bitmap, cliprect, 0);


	int offs, end, incr;

	int flipscreen = flip_screen_get(machine);

	if (invert_flip)
		flipscreen = !flipscreen;


	if (m_pricallback)
	{
		offs = sizewords-4;
		end = -4;
		incr = -4;
	}
	else
	{
		offs = 0;
		end = sizewords;
		incr = 4;
	}

	while (offs!=end)
	{
		int x, y, sprite, colour, multi, mult2, fx, fy, inc, flash, mult, h, w, pri;

		if (!m_alt_format)
		{

			sprite = spriteram[offs + 1];
			y = spriteram[offs];
			flash = y & 0x1000;
			w = y & 0x0800;


			if (!(flash && (machine->primary_screen->frame_number() & 1)))
			{

				x = spriteram[offs + 2];

				if (!m_sprite_bitmap)
					colour = (x >> 9) & 0x1f;
				else
				{
					colour = (x >> 9) & 0x7f;
					if (y&0x8000) colour |= 0x80; // fghthist uses this to mark priority
				}


				if (m_pricallback)
					pri = m_pricallback(x);
				else
					pri = 0;

				fx = y & 0x2000;
				fy = y & 0x4000;
				multi = (1 << ((y & 0x0600) >> 9)) - 1;	/* 1x, 2x, 4x, 8x height */

				if (cliprect->max_x>256)
				{
					x = x & 0x01ff;
					y = y & 0x01ff;
					if (x >= 320) x -= 512;
					if (y >= 256) y -= 512;
					y = 240 - y;
					x = 304 - x;
				}
				else
				{

					x = x & 0x01ff;
					y = y & 0x01ff;
					if (x >= 256) x -= 512;
					if (y >= 256) y -= 512;
					y = 240 - y;
					x = 240 - x;
				}

				//if (x <= 320)
				{

					sprite &= ~multi;
					if (fy)
						inc = -1;
					else
					{
						sprite += multi;
						inc = 1;
					}

					if (flipscreen)
					{
						y = 240 - y;

						if (cliprect->max_x>256)
							x = 304 - x;
						else
							x = 240 - x;

						if (fx) fx = 0; else fx = 1;
						if (fy) fy = 0; else fy = 1;
						mult = 16;
					}
					else
						mult = -16;

					mult2 = multi + 1;

					while (multi >= 0)
					{
						int ypos;
						ypos = y + mult * multi;
						if ((ypos<=cliprect->max_y) && (ypos>=(cliprect->min_y)-16))
						{
							if(!m_sprite_bitmap)
							{
								if ((ypos<=cliprect->max_y) && (ypos>=(cliprect->min_y)-16))
								{
									if (m_pricallback)
										pdrawgfx_transpen(bitmap,cliprect,machine->gfx[m_gfxregion],
											sprite - multi * inc,
											colour,
											fx,fy,
											x,ypos,
											machine->priority_bitmap,pri,0);
									else
										drawgfx_transpen(bitmap,cliprect,machine->gfx[m_gfxregion],
											sprite - multi * inc,
											colour,
											fx,fy,
											x,ypos,
											0);
								}

								// double wing uses this flag
								if (w)
								{
									if (m_pricallback)
										pdrawgfx_transpen(bitmap,cliprect,machine->gfx[m_gfxregion],
												(sprite - multi * inc)-mult2,
												colour,
												fx,fy,
												x-16,ypos,
												machine->priority_bitmap,pri,0);
									else
										drawgfx_transpen(bitmap,cliprect,machine->gfx[m_gfxregion],
												(sprite - multi * inc)-mult2,
												colour,
												fx,fy,
												x-16,ypos,
												0);
								}
							}
							else
							{
								// if we have a sprite bitmap draw raw data to it for manual mixing
								drawgfx_transpen_raw(m_sprite_bitmap,cliprect,machine->gfx[m_gfxregion],
									sprite - multi * inc,
									colour<<m_raw_shift,
									fx,fy,
									x,ypos,
									0);
								if (w)
								{
									drawgfx_transpen_raw(m_sprite_bitmap,cliprect,machine->gfx[m_gfxregion],
										(sprite - multi * inc)-mult2,
										colour<<m_raw_shift,
										fx,fy,
										x-16,ypos,
										0);
								}

							}
						}

						multi--;
					}
				}
			}
		}
		else // m_alt_format
		{
			y = spriteram[offs+0];
			sprite = spriteram[offs+3] & 0xffff;


			if (m_pricallback)
				pri = m_pricallback(spriteram[offs+2]&0x00ff);
			else
				pri = 0;

			x = spriteram[offs+1];

			if (!((y&0x2000) && (machine->primary_screen->frame_number() & 1)));
			{
				if (!m_sprite_bitmap)
					colour = (spriteram[offs+2] >>0) & 0x1f;
				else
					colour = (spriteram[offs+2] >>0) & 0xff; // store all bits for manual mixing


				h = (spriteram[offs+2]&0xf000)>>12;
				w = (spriteram[offs+2]&0x0f00)>> 8;
				fx = !(spriteram[offs+0]&0x4000);
				fy = !(spriteram[offs+0]&0x8000);

				if (!flipscreen) {
					x = x & 0x01ff;
					y = y & 0x01ff;
					if (x>0x180) x=-(0x200 - x);
					if (y>0x180) y=-(0x200 - y);

					if (fx) { mult=-16; x+=16*w; } else { mult=16; x-=16; }
					if (fy) { mult2=-16; y+=16*h; } else { mult2=16; y-=16; }
				} else {
					if (fx) fx=0; else fx=1;
					if (fy) fy=0; else fy=1;

					x = x & 0x01ff;
					y = y & 0x01ff;
					if (x&0x100) x=-(0x100 - (x&0xff));
					if (y&0x100) y=-(0x100 - (y&0xff));
					x = 304 - x;
					y = 240 - y;
					if (x >= 432) x -= 512;
					if (y >= 384) y -= 512;
					if (fx) { mult=-16; x+=16; } else { mult=16; x-=16*w; }
					if (fy) { mult2=-16; y+=16; } else { mult2=16; y-=16*h; }
				}
				int ypos;

				for (int xx=0; xx<w; xx++)
				{
					for (int yy=0; yy<h; yy++)
					{

						if(!m_sprite_bitmap)
						{
							if (m_pricallback)
							{
								ypos = y + mult2 * (h-yy);

								if ((ypos<=cliprect->max_y) && (ypos>=(cliprect->min_y)-16))
								{
									pdrawgfx_transpen(bitmap,cliprect,machine->gfx[m_gfxregion],
											sprite + yy + h * xx,
											colour,
											fx,fy,
											x + mult * (w-xx),ypos,
											machine->priority_bitmap,pri,0);
								}

								ypos -= 512; // wrap-around y

								if ((ypos<=cliprect->max_y) && (ypos>=(cliprect->min_y-16)))
								{
									pdrawgfx_transpen(bitmap,cliprect,machine->gfx[m_gfxregion],
											sprite + yy + h * xx,
											colour,
											fx,fy,
											x + mult * (w-xx),ypos,
											machine->priority_bitmap,pri,0);
								}

							}
							else
							{
								ypos = y + mult2 * (h-yy);

								if ((ypos<=cliprect->max_y) && (ypos>=(cliprect->min_y)-16))
								{
									drawgfx_transpen(bitmap,cliprect,machine->gfx[m_gfxregion],
											sprite + yy + h * xx,
											colour,
											fx,fy,
											x + mult * (w-xx),ypos,
											0);
								}

								ypos -= 512; // wrap-around y

								if ((ypos<=cliprect->max_y) && (ypos>=(cliprect->min_y-16)))
								{
									drawgfx_transpen(bitmap,cliprect,machine->gfx[m_gfxregion],
											sprite + yy + h * xx,
											colour,
											fx,fy,
											x + mult * (w-xx),ypos,
											0);
								}
							}
						}
						else
						{
							ypos = y + mult2 * (h-yy);

							if ((ypos<=cliprect->max_y) && (ypos>=(cliprect->min_y)-16))
							{
								drawgfx_transpen_raw(m_sprite_bitmap,cliprect,machine->gfx[m_gfxregion],
										sprite + yy + h * xx,
										colour<<m_raw_shift,
										fx,fy,
										x + mult * (w-xx),ypos,
										0);
							}

							ypos -= 512; // wrap-around y

							if ((ypos<=cliprect->max_y) && (ypos>=(cliprect->min_y-16)))
							{
								drawgfx_transpen_raw(m_sprite_bitmap,cliprect,machine->gfx[m_gfxregion],
										sprite + yy + h * xx,
										colour<<m_raw_shift,
										fx,fy,
										x + mult * (w-xx),ypos,
										0);
							}
						}
					}
				}
			}
		}

		offs+=incr;
	}
}


// inefficient, we should be able to mix in a single pass by comparing the existing priority bitmap from the tilemaps
void decospr_device::inefficient_copy_sprite_bitmap(running_machine* machine, bitmap_t *bitmap, const rectangle *cliprect, UINT16 pri, UINT16 priority_mask, UINT16 colbase, UINT16 palmask, UINT8 alpha)
{
	if (!m_sprite_bitmap)
		fatalerror("decospr_device::inefficient_copy_sprite_bitmap with no m_sprite_bitmap");

	int y, x;
	const pen_t *paldata = machine->pens;

	UINT16* srcline;
	UINT32* dstline;

	for (y=cliprect->min_y;y<=cliprect->max_y;y++)
	{
		srcline= BITMAP_ADDR16(m_sprite_bitmap, y, 0);
		dstline= BITMAP_ADDR32(bitmap, y, 0);

		if (alpha==0xff)
		{
			for (x=cliprect->min_x;x<=cliprect->max_x;x++)
			{
				UINT16 pix = srcline[x];

				if (pix&0xf)
				{
					if ((pix & priority_mask) ==pri )
					{
						dstline[x] = paldata[(pix&palmask) + colbase];
					}
				}
			}
		}
		else
		{
			for (x=cliprect->min_x;x<=cliprect->max_x;x++)
			{
				UINT16 pix = srcline[x];

				if (pix&m_pixmask)
				{
					if ((pix & priority_mask) ==pri )
					{
						UINT32 pal1 = paldata[(pix&palmask) + colbase];
						UINT32 pal2 = dstline[x];
						dstline[x] = alpha_blend_r32(pal2, pal1, alpha);
					}
				}
			}
		}
	}
}