From 486f7e60292289eb85ef704b8cb4e69ded0c0a94 Mon Sep 17 00:00:00 2001 From: josch Date: Mon, 24 Nov 2008 09:29:06 +0100 Subject: [PATCH] do some p3k optimizations --- hr.py | 2 +- lib/h3m.py | 80 ++++++++++++++++++------------------ lib/mapset.py | 110 +++++++++++++++++++++++++------------------------- lib/window.py | 11 +++-- 4 files changed, 101 insertions(+), 102 deletions(-) diff --git a/hr.py b/hr.py index fe6e0ff..7805e19 100644 --- a/hr.py +++ b/hr.py @@ -51,7 +51,7 @@ if __name__ == '__main__': pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA) - window = Window() + window = Window(width=1024, height=768) window.push_handlers(LoadScreen(window, sys.argv[1])) img = pyglet.resource.image("data/cursors/cradvntr.def/0.png") window.set_mouse_cursor(pyglet.window.ImageMouseCursor(img, 0, 40)) diff --git a/lib/h3m.py b/lib/h3m.py index 3cb8405..e8c203f 100644 --- a/lib/h3m.py +++ b/lib/h3m.py @@ -61,7 +61,7 @@ def extract(filename): (map_data[color]["heroes_count"], ) = struct.unpack(" 0: map_data[color]["heroes"] = {} - for i in xrange(map_data[color]["heroes_count"]): + for i in range(map_data[color]["heroes_count"]): (map_data[color]["heroes"]["portrait"], ) = struct.unpack(" 0: map_data["free_heroes"] = [] - for i in xrange(map_data["heroes_count"]): + for i in range(map_data["heroes_count"]): (hero_id, ) = struct.unpack(" 0: map_data["rumors"] = [] - for i in xrange(map_data["rumor_count"]): + for i in range(map_data["rumor_count"]): (name_length, ) = struct.unpack(" 0: - for i in xrange(knapsack_count): + for i in range(knapsack_count): (knapsackID, ) = struct.unpack(" 0: - for i in xrange(eventQuantity): + for i in range(eventQuantity): (length, ) = struct.unpack(" 0: - for i in xrange(secSkills): + for i in range(secSkills): (skill_id, skill_lvl) = struct.unpack(" 0: - for i in xrange(artefacts): + for i in range(artefacts): (artID, ) = struct.unpack(" 0: - for i in xrange(spells): + for i in range(spells): (spellID, ) = struct.unpack(" 0: - for i in xrange(monsters): + for i in range(monsters): (guard_id, guard_count) = struct.unpack(" 0: - for i in xrange(secSkills): + for i in range(secSkills): (skill_id, skill_lvl) = struct.unpack(" 0: - for i in xrange(artefacts): + for i in range(artefacts): (artID, ) = struct.unpack(" 0: - for i in xrange(spells): + for i in range(spells): (spellID, ) = struct.unpack(" 0: - for i in xrange(monsters): + for i in range(monsters): (guard_id, guard_count) = struct.unpack(" len(edge_map)-8: line = [] - line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in xrange(len(h3m_data["upper_terrain"][0])+18)]) + line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in range(len(h3m_data["upper_terrain"][0])+18)]) elif num == 7: line = [] - line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in xrange(8)]) + line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in range(8)]) line.append([-1, 16, 0, 0, 0, 0, 0]) - line.extend([[-1, 20+i%4, 0, 0, 0, 0, 0] for i in xrange(len(h3m_data["upper_terrain"][0]))]) + line.extend([[-1, 20+i%4, 0, 0, 0, 0, 0] for i in range(len(h3m_data["upper_terrain"][0]))]) line.append([-1, 17, 0, 0, 0, 0, 0]) - line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in xrange(8)]) + line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in range(8)]) elif num == len(edge_map)-8: line = [] - line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in xrange(8)]) + line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in range(8)]) line.append([-1, 19, 0, 0, 0, 0, 0]) - line.extend([[-1, 28+i%4, 0, 0, 0, 0, 0] for i in xrange(len(h3m_data["upper_terrain"][0]))]) + line.extend([[-1, 28+i%4, 0, 0, 0, 0, 0] for i in range(len(h3m_data["upper_terrain"][0]))]) line.append([-1, 18, 0, 0, 0, 0, 0]) - line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in xrange(8)]) + line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in range(8)]) else: line = [] - line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in xrange(8)]) + line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in range(8)]) line.append([-1, 32+num%4, 0, 0, 0, 0, 0]) line.extend(h3m_data["upper_terrain"][num-8]) line.append([-1, 24+num%4, 0, 0, 0, 0, 0]) - line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in xrange(8)]) + line.extend([[-1, 0+(i-1)%4+4*(num%4), 0, 0, 0, 0, 0] for i in range(8)]) edge_map[num] = line h3m_data["upper_terrain"] = edge_map @@ -154,12 +154,12 @@ class MapSet(object): for y, line in enumerate(h3m_data["upper_terrain"]): for x, tile in enumerate(line): if tile[0] == -1: #edge - if "edg" not in tile_textures.keys(): - tile_textures["edg"] = [self.load_map_object('data/advmap_tiles/edg.def/%d.png'%i, 100) for i in xrange(36)] + if "edg" not in list(tile_textures.keys()): + tile_textures["edg"] = [self.load_map_object('data/advmap_tiles/edg.def/%d.png'%i, 100) for i in range(36)] self.__tiles[x,y] = [tile_textures["edg"][tile[1]]] elif tile[0] == 0: #dirt - if "dirttl" not in tile_textures.keys(): - tile_textures["dirttl"] = [self.load_map_object('data/advmap_tiles/dirttl.def/%d.png'%i, 0) for i in xrange(46)] + if "dirttl" not in list(tile_textures.keys()): + tile_textures["dirttl"] = [self.load_map_object('data/advmap_tiles/dirttl.def/%d.png'%i, 0) for i in range(46)] flip_x = bool(tile[6] & 1) flip_y = bool(tile[6] & 2) if flip_x or flip_y: @@ -169,12 +169,12 @@ class MapSet(object): else: self.__tiles[x,y] = [tile_textures["dirttl"][tile[1]]] elif tile[0] == 1: #sand - if "sandtl" not in tile_textures.keys(): - tile_textures["sandtl"] = [self.load_map_object('data/advmap_tiles/sandtl.def/%d.png'%i, 0) for i in xrange(24)] + if "sandtl" not in list(tile_textures.keys()): + tile_textures["sandtl"] = [self.load_map_object('data/advmap_tiles/sandtl.def/%d.png'%i, 0) for i in range(24)] self.__tiles[x,y] = [tile_textures["sandtl"][tile[1]]] elif tile[0] == 2: #grass - if "grastl" not in tile_textures.keys(): - tile_textures["grastl"] = [self.load_map_object('data/advmap_tiles/grastl.def/%d.png'%i, 0) for i in xrange(79)] + if "grastl" not in list(tile_textures.keys()): + tile_textures["grastl"] = [self.load_map_object('data/advmap_tiles/grastl.def/%d.png'%i, 0) for i in range(79)] flip_x = bool(tile[6] & 1) flip_y = bool(tile[6] & 2) if flip_x or flip_y: @@ -184,8 +184,8 @@ class MapSet(object): else: self.__tiles[x,y] = [tile_textures["grastl"][tile[1]]] elif tile[0] == 3: #snow - if "snowtl" not in tile_textures.keys(): - tile_textures["snowtl"] = [self.load_map_object('data/advmap_tiles/snowtl.def/%d.png'%i, 0) for i in xrange(79)] + if "snowtl" not in list(tile_textures.keys()): + tile_textures["snowtl"] = [self.load_map_object('data/advmap_tiles/snowtl.def/%d.png'%i, 0) for i in range(79)] flip_x = bool(tile[6] & 1) flip_y = bool(tile[6] & 2) if flip_x or flip_y: @@ -195,8 +195,8 @@ class MapSet(object): else: self.__tiles[x,y] = [tile_textures["snowtl"][tile[1]]] elif tile[0] == 4: #swamp - if "swmptl" not in tile_textures.keys(): - tile_textures["swmptl"] = [self.load_map_object('data/advmap_tiles/swmptl.def/%d.png'%i, 0) for i in xrange(79)] + if "swmptl" not in list(tile_textures.keys()): + tile_textures["swmptl"] = [self.load_map_object('data/advmap_tiles/swmptl.def/%d.png'%i, 0) for i in range(79)] flip_x = bool(tile[6] & 1) flip_y = bool(tile[6] & 2) if flip_x or flip_y: @@ -206,8 +206,8 @@ class MapSet(object): else: self.__tiles[x,y] = [tile_textures["swmptl"][tile[1]]] elif tile[0] == 5: #rough - if "rougtl" not in tile_textures.keys(): - tile_textures["rougtl"] = [self.load_map_object('data/advmap_tiles/rougtl.def/%d.png'%i, 0) for i in xrange(79)] + if "rougtl" not in list(tile_textures.keys()): + tile_textures["rougtl"] = [self.load_map_object('data/advmap_tiles/rougtl.def/%d.png'%i, 0) for i in range(79)] flip_x = bool(tile[6] & 1) flip_y = bool(tile[6] & 2) if flip_x or flip_y: @@ -217,8 +217,8 @@ class MapSet(object): else: self.__tiles[x,y] = [tile_textures["rougtl"][tile[1]]] elif tile[0] == 7: #lava - if "lavatl" not in tile_textures.keys(): - tile_textures["lavatl"] = [self.load_map_object('data/advmap_tiles/lavatl.def/%d.png'%i, 0) for i in xrange(79)] + if "lavatl" not in list(tile_textures.keys()): + tile_textures["lavatl"] = [self.load_map_object('data/advmap_tiles/lavatl.def/%d.png'%i, 0) for i in range(79)] flip_x = bool(tile[6] & 1) flip_y = bool(tile[6] & 2) if flip_x or flip_y: @@ -228,9 +228,9 @@ class MapSet(object): else: self.__tiles[x,y] = [tile_textures["lavatl"][tile[1]]] elif tile[0] == 8: #water 12 anims - if "watrtl" not in tile_textures.keys(): - textures = [self.load_map_object('data/advmap_tiles/watrtl.def/%d/0.png'%i, 0) for i in xrange(33)] - images = [[pyglet.image.load(None, file=pyglet.resource.file('data/advmap_tiles/watrtl.def/%d/%d.png'%(i,j))) for j in xrange(12)] for i in xrange(33)] + if "watrtl" not in list(tile_textures.keys()): + textures = [self.load_map_object('data/advmap_tiles/watrtl.def/%d/0.png'%i, 0) for i in range(33)] + images = [[pyglet.image.load(None, file=pyglet.resource.file('data/advmap_tiles/watrtl.def/%d/%d.png'%(i,j))) for j in range(12)] for i in range(33)] tile_textures["watrtl"] = { (0,0):[Animation(texture, images[i]) for i, texture in enumerate(textures)], (1,0):[Animation(texture, images[i], flip_x=True) for i, texture in enumerate(textures)], @@ -241,8 +241,8 @@ class MapSet(object): flip_y = (tile[6]>>1)&1 self.__tiles[x,y] = [tile_textures["watrtl"][flip_x, flip_y][tile[1]]] elif tile[0] == 9: #rock - if "rocktl" not in tile_textures.keys(): - tile_textures["rocktl"] = [self.load_map_object('data/advmap_tiles/rocktl.def/%d.png'%i, 0) for i in xrange(48)] + if "rocktl" not in list(tile_textures.keys()): + tile_textures["rocktl"] = [self.load_map_object('data/advmap_tiles/rocktl.def/%d.png'%i, 0) for i in range(48)] self.__tiles[x,y] = [tile_textures["rocktl"][tile[1]]] else: raise NotImplementedError @@ -250,9 +250,9 @@ class MapSet(object): if tile[2] == 0: #no river pass elif tile[2] == 1: #clrrvr 12 anims - if "clrrvr" not in tile_textures.keys(): - textures = [self.load_map_object('data/advmap_tiles/clrrvr.def/%d/0.png'%i, 1) for i in xrange(13)] - images = [[pyglet.image.load(None, file=pyglet.resource.file('data/advmap_tiles/clrrvr.def/%d/%d.png'%(i,j))) for j in xrange(12)] for i in xrange(13)] + if "clrrvr" not in list(tile_textures.keys()): + textures = [self.load_map_object('data/advmap_tiles/clrrvr.def/%d/0.png'%i, 1) for i in range(13)] + images = [[pyglet.image.load(None, file=pyglet.resource.file('data/advmap_tiles/clrrvr.def/%d/%d.png'%(i,j))) for j in range(12)] for i in range(13)] tile_textures["clrrvr"] = { (0,0):[Animation(texture, images[i]) for i, texture in enumerate(textures)], (1,0):[Animation(texture, images[i], flip_x=True) for i, texture in enumerate(textures)], @@ -263,8 +263,8 @@ class MapSet(object): flip_y = (tile[6]>>3)&1 self.__tiles[x,y].append(tile_textures["clrrvr"][flip_x, flip_y][tile[3]]) elif tile[2] == 2: #icyrvr - if "icyrvr" not in tile_textures.keys(): - tile_textures["icyrvr"] = [self.load_map_object('data/advmap_tiles/icyrvr.def/%d.png'%i, 1) for i in xrange(13)] + if "icyrvr" not in list(tile_textures.keys()): + tile_textures["icyrvr"] = [self.load_map_object('data/advmap_tiles/icyrvr.def/%d.png'%i, 1) for i in range(13)] flip_x = bool(tile[6] & 4) flip_y = bool(tile[6] & 8) if flip_x or flip_y: @@ -274,9 +274,9 @@ class MapSet(object): else: self.__tiles[x, y].append(tile_textures["icyrvr"][tile[3]]) elif tile[2] == 3: #mudrvr - if "mudrvr" not in tile_textures.keys(): - textures = [self.load_map_object('data/advmap_tiles/mudrvr.def/%d/0.png'%i, 1) for i in xrange(13)] - images = [[pyglet.image.load(None, file=pyglet.resource.file('data/advmap_tiles/clrrvr.def/%d/%d.png'%(i,j))) for j in xrange(12)] for i in xrange(13)] + if "mudrvr" not in list(tile_textures.keys()): + textures = [self.load_map_object('data/advmap_tiles/mudrvr.def/%d/0.png'%i, 1) for i in range(13)] + images = [[pyglet.image.load(None, file=pyglet.resource.file('data/advmap_tiles/clrrvr.def/%d/%d.png'%(i,j))) for j in range(12)] for i in range(13)] tile_textures["mudrvr"] = { (0,0):[Animation(texture, images[i]) for i, texture in enumerate(textures)], (1,0):[Animation(texture, images[i], flip_x=True) for i, texture in enumerate(textures)], @@ -287,9 +287,9 @@ class MapSet(object): flip_y = (tile[6]>>3)&1 self.__tiles[x,y].append(tile_textures["mudrvr"][flip_x, flip_y][tile[3]]) elif tile[2] == 4: #lavrvr - if "lavrvr" not in tile_textures.keys(): - textures = [self.load_map_object('data/advmap_tiles/lavrvr.def/%d/0.png'%i, 1) for i in xrange(13)] - images = [[pyglet.image.load(None, file=pyglet.resource.file('data/advmap_tiles/clrrvr.def/%d/%d.png'%(i,j))) for j in xrange(9)] for i in xrange(13)] + if "lavrvr" not in list(tile_textures.keys()): + textures = [self.load_map_object('data/advmap_tiles/lavrvr.def/%d/0.png'%i, 1) for i in range(13)] + images = [[pyglet.image.load(None, file=pyglet.resource.file('data/advmap_tiles/clrrvr.def/%d/%d.png'%(i,j))) for j in range(9)] for i in range(13)] tile_textures["lavrvr"] = { (0,0):[Animation(texture, images[i]) for i, texture in enumerate(textures)], (1,0):[Animation(texture, images[i], flip_x=True) for i, texture in enumerate(textures)], @@ -300,13 +300,13 @@ class MapSet(object): flip_y = (tile[6]>>3)&1 self.__tiles[x,y].append(tile_textures["lavrvr"][flip_x, flip_y][tile[3]]) else: - raise NotImplementedError, tile[2] + raise NotImplementedError(tile[2]) if tile[4] == 0: #no road pass elif tile[4] == 1: #dirtrd - if "dirtrd" not in tile_textures.keys(): - tile_textures["dirtrd"] = [self.load_map_object('data/advmap_tiles/dirtrd.def/%d.png'%i, 1) for i in xrange(17)] + if "dirtrd" not in list(tile_textures.keys()): + tile_textures["dirtrd"] = [self.load_map_object('data/advmap_tiles/dirtrd.def/%d.png'%i, 1) for i in range(17)] flip_x = bool(tile[6] & 16) flip_y = bool(tile[6] & 32) if flip_x or flip_y: @@ -316,8 +316,8 @@ class MapSet(object): else: self.__tiles[x, y].append(tile_textures["dirtrd"][tile[5]]) elif tile[4] == 2: #gravrd - if "gravrd" not in tile_textures.keys(): - tile_textures["gravrd"] = [self.load_map_object('data/advmap_tiles/gravrd.def/%d.png'%i, 1) for i in xrange(17)] + if "gravrd" not in list(tile_textures.keys()): + tile_textures["gravrd"] = [self.load_map_object('data/advmap_tiles/gravrd.def/%d.png'%i, 1) for i in range(17)] flip_x = bool(tile[6] & 16) flip_y = bool(tile[6] & 32) if flip_x or flip_y: @@ -327,8 +327,8 @@ class MapSet(object): else: self.__tiles[x, y].append(tile_textures["gravrd"][tile[5]]) elif tile[4] == 3: #cobbrd - if "cobbrd" not in tile_textures.keys(): - tile_textures["cobbrd"] = [self.load_map_object('data/advmap_tiles/cobbrd.def/%d.png'%i, 1) for i in xrange(17)] + if "cobbrd" not in list(tile_textures.keys()): + tile_textures["cobbrd"] = [self.load_map_object('data/advmap_tiles/cobbrd.def/%d.png'%i, 1) for i in range(17)] flip_x = bool(tile[6] & 16) flip_y = bool(tile[6] & 32) if flip_x or flip_y: @@ -338,7 +338,7 @@ class MapSet(object): else: self.__tiles[x, y].append(tile_textures["cobbrd"][tile[5]]) else: - raise NotImplementedError, tile[4] + raise NotImplementedError(tile[4]) images = [] for order, obj in enumerate(h3m_data["objects"]): @@ -347,7 +347,7 @@ class MapSet(object): while 1: imgs.append(pyglet.image.load(None, file=pyglet.resource.file("data/advmap_objects/" + obj["filename"] + "/%d.png"%i))) i += 1 - if "data/advmap_objects/" + obj["filename"] + "/%d.png" % i not in pyglet.resource._default_loader._index.keys(): + if "data/advmap_objects/" + obj["filename"] + "/%d.png" % i not in list(pyglet.resource._default_loader._index.keys()): break; images.append((imgs, order)) @@ -372,9 +372,9 @@ class MapSet(object): self.__tiles[obj["x"] + 9,obj["y"] + 8].append(self.objects[obj["id"]]) def get_tiles(self, tiles_x, tiles_y, div_x, div_y): - for y in xrange(tiles_y - 1, -6, -1): + for y in range(tiles_y - 1, -6, -1): y1 = y * 32 - for x in xrange(tiles_x + 5 - 1, -1, -1): + for x in range(tiles_x + 5 - 1, -1, -1): for obj in self.__tiles.get((x - div_x, div_y - y), []): x1 = x * 32 - obj.width + 32 x2 = x1 + obj.width diff --git a/lib/window.py b/lib/window.py index bb97b48..9ba7fc4 100644 --- a/lib/window.py +++ b/lib/window.py @@ -21,7 +21,9 @@ import pyglet class Window(pyglet.window.Window): def __init__(self, *args, **kwargs): - super(Window, self).__init__(1280, 1024, resizable=True, vsync=False) + width = kwargs.get("width", 1024) + height = kwargs.get("height", 768) + super(Window, self).__init__(width, height, resizable=True, vsync=False) self.keys = pyglet.window.key.KeyStateHandler() self.push_handlers(self.keys) self.fps = pyglet.clock.ClockDisplay() @@ -45,8 +47,5 @@ class PNGRGBEncoder(pyglet.image.codecs.ImageEncoder): pitch = -(image.width * len(format)) pil_image = Image.fromstring( format, (image.width, image.height), image.get_data(format, pitch)) - try: - #.convert('P', palette=Image.WEB) - pil_image.convert("RGB").save(file) - except Exception, e: - raise ImageEncodeException(e) + #.convert('P', palette=Image.WEB) + pil_image.convert("RGB").save(file)