junglejourney

view tools/atom/makesprites.py @ 235:b74ffb65fb88

Added more sprites for the Atom and a script to convert them to screen data.
author David Boddie <david@boddie.org.uk>
date Sun Aug 26 00:18:50 2012 +0200
parents
children
line source
1 #!/usr/bin/env python
3 """
4 Copyright (C) 2012 David Boddie <david@boddie.org.uk>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 """
20 """ Information provided by Kees van Oss:
22 BBC ATOM
23 TILES 16 x 24 -> 12 x 16
24 PLAYER 8 x 24 -> 8 x 16
25 PROJECTILE 8 x 8 -> 8 x 8
26 ENEMIES 16 x 16 -> 12 x 12
27 WEAPONS 16 x 16 -> 12 x 12
28 TREASURE 16 x 16 -> 12 x 12
29 EXIT 16 x 24 -> 12 x 16
31 Hex: $a0 , $80 , $aa
32 Bin:%10 10 00 00, %10 00 00 00, %10 10 10 10
33 Col: BL BL GR GR BL GR GR GR BL BL BL BL
35 00 = GR = GREEN, 01 = YE = YELLOW, 10 = BL = BLUE, 11 = RE = RED
36 """
38 import os, sys
39 import Image
41 # Map RGB values to the appropriate values for the Atom palette.
42 palette = {"\x00\x00\x00": 2, "\xff\x00\x00": 3,
43 "\x00\x80\x00": 0, "\xff\xff\x00": 2}
45 if __name__ == "__main__":
47 this_dir = os.path.split(os.path.abspath(__file__))[0]
48 image_dir = os.path.join(this_dir, os.path.pardir, os.path.pardir, "images", "atom")
50 sprites = ""
52 for file_name in os.listdir(image_dir):
54 if file_name.endswith(".png"):
56 im = Image.open(os.path.join(image_dir, file_name))
57 im = im.convert("RGB")
58 data = im.tostring()
59 k = 0
61 sprite = ""
63 for i in range(im.size[1]):
65 # Initialise the byte value for insertion into the sprite data.
66 byte = 0
67 # The offset into the byte for each pixel starts at the highest
68 # two bits.
69 offset = 6
71 for j in range(im.size[0]):
73 rgb = data[k:k+3]
74 v = palette[rgb]
75 byte = byte | (v << offset)
77 if offset == 0:
78 # For the last pixel in a byte, insert it into the sprite
79 # data, and reset the offset and byte values.
80 sprite += chr(byte)
81 offset = 6
82 else:
83 # Otherwise, decrease the offset into the byte to accept
84 # data for the next pixel.
85 offset -= 2
87 k += 3
89 if offset != 6:
90 # If the current byte has not been added to the sprite data,
91 # add it now.
92 sprite += chr(byte)
94 # Add the sprite data to the collection.
95 sprites += sprite
97 # Write the sprite data to a file.
98 open("sprites.data", "w").write(sprites)
100 sys.exit()