Skip to content

Commit e8abf2e

Browse files
committed
tiletool: support for Neo Geo CD SPR file format
1 parent 28fedd6 commit e8abf2e

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

Diff for: tools/tiletool.py

+108
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,110 @@ def write_tile_to_rom(self, t):
328328
surf_off += 8
329329

330330

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+
331435
def main():
332436
pygame.display.init()
333437

@@ -346,6 +450,8 @@ def main():
346450
help='8x8 fix tile mode')
347451
ptype.add_argument('--sprite', action='store_true',
348452
help='16x16 sprite tile mode [default]')
453+
ptype.add_argument('--cd-sprite', action='store_true',
454+
help='16x16 CD sprite tile mode')
349455

350456
parser.add_argument('FILE', nargs='+', help='file to process')
351457
parser.add_argument('-o', '--output', nargs='+',
@@ -363,6 +469,8 @@ def main():
363469

364470
if arguments.fix:
365471
conv = fix_converter(arguments)
472+
elif arguments.cd_sprite:
473+
conv = cd_sprite_converter(arguments)
366474
else:
367475
conv = sprite_converter(arguments)
368476

0 commit comments

Comments
 (0)