-
Notifications
You must be signed in to change notification settings - Fork 1
/
tileset_gen.py
executable file
·66 lines (52 loc) · 2.32 KB
/
tileset_gen.py
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
#!/usr/bin/env python3
from PIL import Image
from os.path import splitext, basename
from glob import glob
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
images = []
images += [ { 'scale_factor': 0.7, 'file_name': f } for f in glob('../gfx/*alien*') ]
images += [ { 'scale_factor': 0.7, 'file_name': f } for f in glob('../gfx/*astronaut*') ]
images += [ { 'scale_factor': 0.5, 'file_name': f } for f in glob('../gfx/*Tile*') ]
images += [ { 'scale_factor': 0.5, 'file_name': f } for f in glob('../gfx/*spaceCraft3*') ]
images += [ { 'scale_factor': 0.5, 'file_name': f } for f in glob('../gfx/*Fence*') ]
tilemap_temp = Image.new('RGBA', (1000, 1000), (255, 0, 0, 0))
curr_x = 0
curr_y = 0
max_image_y_in_line = 0
max_line_width = 0
sprites = []
for image in images:
im = Image.open(image['file_name'])
scaled_im = im.resize((round(im.width * image['scale_factor']), round(im.height * image['scale_factor'])), Image.LANCZOS)
tilemap_temp.paste(scaled_im, (curr_x, curr_y))
sprites.append({ 'name': splitext(basename(image['file_name']))[0], 'posX': curr_x,
'posY': curr_y, 'width': scaled_im.width, 'height': scaled_im.height })
curr_x += scaled_im.width
max_image_y_in_line = max(scaled_im.height, max_image_y_in_line)
if curr_x > 400:
max_line_width = max(max_line_width, curr_x)
curr_x = 0
curr_y += max_image_y_in_line
else:
max_line_width = max(max_line_width, curr_x)
curr_y += max_image_y_in_line
tilemap = tilemap_temp.crop((0, 0, max_line_width, curr_y))
tilemap.save('../dist/assets/tileset.png', optimize=True)
with open('../tileset.hpp', 'w') as f:
f.write('''// generated by tools/tileset_gen.py
#pragma once
#include <SFML/System.hpp>
#include "spriteinfo.hpp"
namespace Tileset {
''')
for sprite in sprites:
f.write('const SpriteInfo %s = { %s, %s, { sf::Vector2f(%s, %s), sf::Vector2f(%s, %s), sf::Vector2f(%s, %s), sf::Vector2f(%s, %s) } };\n' % (
sprite['name'],
sprite['width'], sprite['height'],
sprite['posX'], sprite['posY'],
sprite['posX'] + sprite['width'], sprite['posY'],
sprite['posX'] + sprite['width'], sprite['posY'] + sprite['height'],
sprite['posX'], sprite['posY'] + sprite['height']
))
f.write('\n}\n')