Python as a Game Dev tool on GBA

Gianluca Cancelmi cancelmi at cli.di.unipi.it
Thu Jun 6 18:51:21 EDT 2002


Hi,

I've been using python for a lot of little tools for Gameboy Advance.

At the moment I'm using it to convert multiple bitmaps into a large shared
tile set and multiple maps.

I'm using PIL to load the bitmaps and generate the tiles.

At the moment I'm having a little bit of trouble with memory usage.

I'm cutting the 8x8 pixels tiles using Image.crop().
I'm creating 3 other versions of the current tile for Horizontal, Vertical
and H+V flipping.
I convert the tiles in strings and use this string as a key for the tile
dictionary. The value for that key is an object (TileImage).

That's the source code:

class TileImage:
    def __init__(self, image, tile_num, ref_counter = 0):
        # Init the members
        self.image = image
        self.tile_num = tile_num
        self.ref_counter = ref_counter

    def inc_reference(self):
        # Increase the reference number
        self.ref_counter += 1

    def dec_reference(self):
        # Decrease the reference number

        # Check if it's 0
        if (self.ref_counter > 0):
            self.ref_counter -= 1

    .................................
    .................................
    # Main part of the loop

    # We have a unique tile set for all layers but a separate map (a map for
each layer)
    tile_set = {}
    corr_num_tile = {}
    maps = []
    total_tile_counter = 0
    tile_unique_counter = 0

    # Process each image (img_list is a list of images loaded with PIL)
    for img in img_list:
        # Map is the current map
        map = []
        tile_counter = 0

        s_x = img.size[0]/8
        s_y = img.size[1]/8

        # Generate tile set and map
        for y in range(0, s_y):
            for x in range(0, s_x):
                # Generate the tile
                # Normal version of the tile
                tile = img.crop((x*8, y*8, x*8+8, y*8+8))

                # Convert to string for fast compare
                tile_string = tile.tostring()
                tile_v_string =
tile.transpose(Image.FLIP_TOP_BOTTOM).tostring()
                tile_h_string =
tile.transpose(Image.FLIP_LEFT_RIGHT).tostring()
                tile_vh_string =
tile.transpose(Image.FLIP_TOP_BOTTOM).transpose(Image.FLIP_LEFT_RIGHT).tostr
ing()

                # Convert to 16x16 colors mode for palette reduction
                tile_16_string = module_string_fast(tile_string)
                tile_16_v_string = module_string_fast(tile_v_string)
                tile_16_h_string = module_string_fast(tile_h_string)
                tile_16_vh_string = module_string_fast(tile_vh_string)

                # Add the tile in the tile set (check for H,V,H+V)
                if (tile_set.has_key(tile_16_string)):
                    # Set current value for map
                    current_key = [tile_set[tile_16_string].tile_num,
get_pal_idx(tile_string, 16), 0, 0]
                elif (tile_set.has_key(tile_16_v_string)):
                    # Set current value for map
                    current_key = [tile_set[tile_16_v_string].tile_num,
get_pal_idx(tile_v_string, 16), 1, 0]
                elif (tile_set.has_key(tile_16_h_string)):
                    # Set current value for map
                    current_key = [tile_set[tile_16_h_string].tile_num,
get_pal_idx(tile_h_string, 16), 0, 1]
                elif (tile_set.has_key(tile_16_vh_string)):
                    # Set current value for map
                    current_key = [tile_set[tile_16_vh_string].tile_num,
get_pal_idx(tile_vh_string, 16), 1, 1]
                else:
                    # Add a new tile into the dictionary
                    tile_set[tile_16_string] = TileImage(tile,
tile_unique_counter)
                    corr_num_tile[tile_unique_counter] = tile_16_string

                    # Set current value
                    current_key = [tile_unique_counter,
get_pal_idx(tile_string, 16), 0, 0]

                    # Increment unique tile counter because we found a
unique tile
                    tile_unique_counter += 1

                # Add the tile into the map
                map.append(current_key)

                # Increment tile counter
                tile_counter += 1

        # Save the map in the map array and add the tiles to the
total_tile_counter
        total_tile_counter += tile_counter

        # Save the map for this layer
        maps.append(map)

    .................................
    .................................

The problem is that is taking too much memory and I get something like
memory error in python.
The test images are 9600x512 pixels each (4 images).

My PC has 512M of RAM and I'm running the script in win2000.

Is there any way to debug memory usage?

Anyone has an idea of what I'm doing wrong?

Any help would be really appreciated.

Cheers,

Gianluca





More information about the Python-list mailing list