@@ -328,6 +328,110 @@ def write_tile_to_rom(self, t):
328
328
surf_off += 8
329
329
330
330
331
+ class cd_sprite_converter (converter ):
332
+ """Specialization for sprite tile SPR format (CD)
333
+
334
+ A 16x16 sprite tile takes 16 * 16 * 4bits = 1024bits = 128bytes.
335
+ It's split into 4 8x8 blocks, stored as sequences of horizontal rows.
336
+ Each row is encoded in 4 successive bitplanes of 8 bits
337
+ (plane 2; plane 1; plane 4; plane 3).
338
+ """
339
+
340
+ def __init__ (self , args ):
341
+ self .args = args
342
+ self .multiple = 128
343
+ self .edge = 16
344
+
345
+ def open_rom (self , mode ):
346
+ """I/O for ROM file xxx.SPR"""
347
+ self .fd1 = open (self .in1 if mode == 'rb' else self .out1 , mode )
348
+
349
+ def close_rom (self ):
350
+ if self .size :
351
+ padding = self .size - self .fd1 .tell ();
352
+ if padding > 0 :
353
+ self .fd1 .write ('\0 ' * padding )
354
+ self .fd1 .close ()
355
+
356
+ def validate_extract (self ):
357
+ """SPR tile checks for extract command"""
358
+ if len (self .args .FILE ) != 1 :
359
+ sys .exit ("error: expected one SPR file, given: %s" %
360
+ " " .join (self .args .FILE ))
361
+ self .in1 = self .args .FILE [0 ]
362
+
363
+ self .size = self .args .size
364
+ if not self .size :
365
+ sizein1 = os .path .getsize (self .in1 )
366
+ self .size = sizein1
367
+
368
+ super (cd_sprite_converter , self ).validate_extract ()
369
+
370
+ def validate_create (self ):
371
+ """SPR tile checks for create command"""
372
+ if not self .args .output :
373
+ self .args .output = ['none' ]
374
+ if len (self .args .output ) != 1 :
375
+ sys .exit ("error: expected one SPR file, given: %s" %
376
+ " " .join (self .args .output ))
377
+ self .out1 = self .args .output [0 ]
378
+
379
+ super (cd_sprite_converter , self ).validate_create ()
380
+
381
+ def read_tile_from_rom (self ):
382
+ """SPR tile loader"""
383
+ surf_buf = bytearray (256 )
384
+
385
+ for tile8x8_off in (8 , 136 , 0 , 128 ):
386
+ surf_off = tile8x8_off
387
+
388
+ for y in range (8 ):
389
+ row_bitplane1 = ord (self .fd1 .read (1 ))
390
+ row_bitplane2 = ord (self .fd1 .read (1 ))
391
+ row_bitplane3 = ord (self .fd2 .read (1 ))
392
+ row_bitplane4 = ord (self .fd2 .read (1 ))
393
+
394
+ for x in range (8 ):
395
+ bp1 = (row_bitplane1 >> x ) & 1
396
+ bp2 = (row_bitplane2 >> x ) & 1
397
+ bp3 = (row_bitplane3 >> x ) & 1
398
+ bp4 = (row_bitplane4 >> x ) & 1
399
+ col = (bp4 << 3 ) + (bp3 << 2 ) + (bp2 << 1 ) + bp1
400
+ surf_buf [surf_off ] = col
401
+
402
+ surf_off += 1
403
+ surf_off += 8
404
+ t = pygame .image .fromstring (bytes (surf_buf ), (16 , 16 ), "P" )
405
+ return t
406
+
407
+ def write_tile_to_rom (self , t ):
408
+ """SPR tile writer"""
409
+ surf_buf = t .get_buffer ().raw
410
+ for tile8x8_off in (8 , 136 , 0 , 128 ):
411
+ surf_off = tile8x8_off
412
+
413
+ for y in range (8 ):
414
+ row_bitplane1 = 0
415
+ row_bitplane2 = 0
416
+ row_bitplane3 = 0
417
+ row_bitplane4 = 0
418
+
419
+ for x in range (8 ):
420
+ col = surf_buf [surf_off ]
421
+ row_bitplane1 += ((col >> 0 ) & 1 ) << x
422
+ row_bitplane2 += ((col >> 1 ) & 1 ) << x
423
+ row_bitplane3 += ((col >> 2 ) & 1 ) << x
424
+ row_bitplane4 += ((col >> 3 ) & 1 ) << x
425
+ surf_off += 1
426
+
427
+ self .fd1 .write (struct .pack ('4B' ,
428
+ row_bitplane2 ,
429
+ row_bitplane1 ,
430
+ row_bitplane4 ,
431
+ row_bitplane3 ))
432
+ surf_off += 8
433
+
434
+
331
435
def main ():
332
436
pygame .display .init ()
333
437
@@ -346,6 +450,8 @@ def main():
346
450
help = '8x8 fix tile mode' )
347
451
ptype .add_argument ('--sprite' , action = 'store_true' ,
348
452
help = '16x16 sprite tile mode [default]' )
453
+ ptype .add_argument ('--cd-sprite' , action = 'store_true' ,
454
+ help = '16x16 CD sprite tile mode' )
349
455
350
456
parser .add_argument ('FILE' , nargs = '+' , help = 'file to process' )
351
457
parser .add_argument ('-o' , '--output' , nargs = '+' ,
@@ -363,6 +469,8 @@ def main():
363
469
364
470
if arguments .fix :
365
471
conv = fix_converter (arguments )
472
+ elif arguments .cd_sprite :
473
+ conv = cd_sprite_converter (arguments )
366
474
else :
367
475
conv = sprite_converter (arguments )
368
476
0 commit comments