source: trunk/PARPG/utilities/gfxsplit.py @ 49

Revision 49, 4.6 KB checked in by maximinus_parpg, 11 years ago (diff)

/utilities/gfxsplit.py now writes xml files as well.
Adjusted file structure for building objects.
Converted building1 to use split tile gfxs.
Looks ugly but it's a start.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>
17
18import sys,pygame
19
20# place defines here
21
22TILE_WIDTH  =   70
23
24# this is very much a simple routine, but we still have a simple class
25
26class TileImage:
27    def __init__(self,picture,name):
28        self.image=picture
29        self.filename=name
30
31def writeXML(name):
32    """Write the XML file as well
33       Always the same small file so we do it automatically"""
34    # we need to strip off the entire path up to the last
35    # TODO: this code will not work on windows
36    # strip off the png part and replace with the XML
37    filename=name.split('/')[-1]
38    x_file=open(name[:-4]+".xml","wt")
39    x_file.write('''<?fife type="object"?>\n''')
40    x_file.write('''<object id="''')
41    x_file.write(filename[:-4])
42    x_file.write('''" namespace="PARPG" blocking="1" static="1">\n''')
43    x_file.write('''    <image source="''')
44    x_file.write(filename)
45    x_file.write('''" direction="0" />\n''')
46    # the \n\n is ESSENTIAL otherwise the XML parser in FIFE craps out!
47    x_file.write('''</object>\n\n''')
48    x_file.close
49
50def saveFiles(files):
51    """Given a list of TileImages, output them as seperate files
52       Returns True if it worked"""
53    # files is a list of TileImages
54    complete=[]
55    for i in files:
56        try:
57            pygame.image.save(i.image,i.filename)
58            # output the XML file as well
59            writeXML(i.filename)
60        except:
61            print "Error: Failed to save",filename
62            # if we saved some anyway, then tell the user
63            if(complete!=[]):
64                print "  Managed to save",
65                for name in complete:
66                    print name,
67                print "\n"
68            return False
69        complete.append(i.filename)
70    # seems like all was ok
71    return True
72           
73def splitImage(image,filename):
74    """Quite complex this, as there are many differing layouts on the
75       hexes that we could be dealing with. However, for now we assume
76       that we blit from left to right, with the image x position increasing
77       by one and the y value staying the same (on the grid map)"""
78    xpos=0
79    file_counter=0
80    tiles=[]
81    height=image.get_height()
82    while(xpos<image.get_width()):
83        # create a new surface the same height as the original but
84        # with a width of TILE_WIDTH, and with per-pixel alpha
85        new_surface=pygame.Surface((TILE_WIDTH,height),pygame.SRCALPHA,32)
86        # now blit a strip of the image across
87        if(xpos==0):
88            new_surface.blit(image,(0,0),pygame.Rect(0,0,TILE_WIDTH,height))
89            # on the first time around, move ahead by the width of a tile
90            xpos+=TILE_WIDTH
91        else:
92            # we need to offset into halfway through the tile on other blits
93            new_surface.blit(image,((TILE_WIDTH/2)-1,0),
94                pygame.Rect(xpos,0,TILE_WIDTH/2,height))
95            xpos+=(TILE_WIDTH/2)
96        # store the image for later
97        tiles.append(TileImage(new_surface,
98            filename+chr(ord('a')+file_counter)+".png"))
99        file_counter+=1
100    return tiles
101           
102def convertFiles(filename):
103    """Take a file, slice into seperate images and then save these new images
104       as filename0, filename1 ... filenameN
105       Returns True if everything worked"""
106    # first off, load the file
107    try:
108        image=pygame.image.load(filename)
109    except(pygame.error):
110        print "Error: Couldn't load",filename
111        return False
112    # split into seperate files
113    # the [:-4] is used to split off the .png from the filename
114    images=splitImage(image,filename[:-4])
115    # save it and we are done
116    if(images==[]):
117        # something funny happened
118        print "Error: Couldn't splice given image file"
119        return False
120    return(saveFiles(images))
121
122if __name__=="__main__":
123    # check we have some options
124    if(len(sys.argv)<2):
125        sys.stderr.write("Error: No image given!\n")
126        sys.exit(False)
127    # ok, so init pygame and do it
128    pygame.init()
129    sys.exit(convertFiles(sys.argv[1]))
130
Note: See TracBrowser for help on using the repository browser.