# HG changeset patch # User Simon Cross # Date 1259190956 0 # Node ID bf90a2948e34dcc58008f80a0f9ee49993077420 # Parent d2ca4af92c794135874859327407bd8ac850e44a Basic snapshot support in save games added. diff -r d2ca4af92c79 -r bf90a2948e34 gamelib/gameboard.py --- a/gamelib/gameboard.py Wed Nov 25 22:36:48 2009 +0000 +++ b/gamelib/gameboard.py Wed Nov 25 23:15:56 2009 +0000 @@ -970,6 +970,13 @@ buy_price = sell_price*(1.1) self.wood_sell_price, self.wood_buy_price = int(sell_price), int(buy_price) + def snapshot(self, scale=0.25): + """Return a snapshot of the gameboard.""" + w, h = self.disp.screen.get_size() + w, h = int(w * scale), int(h * scale) + snapshot = pygame.transform.smoothscale(self.disp.screen, (w, h)) + return snapshot + def save_game(self): # clear selected animals and tool states before saving self.reset_states() diff -r d2ca4af92c79 -r bf90a2948e34 gamelib/savegame.py --- a/gamelib/savegame.py Wed Nov 25 22:36:48 2009 +0000 +++ b/gamelib/savegame.py Wed Nov 25 23:15:56 2009 +0000 @@ -2,8 +2,11 @@ import xmlrpclib import os +import StringIO +import base64 from pgu import gui +import pygame import config import version @@ -19,11 +22,33 @@ save_version = params[0] if save_version != version.SAVE_GAME_VERSION: raise SaveGameError("Incompatible save game version.") + data = params[1] + + try: + snapshot = decode_snapshot(params[2]) + except Exception, e: + snapshot = None + except Exception, e: raise SaveGameError("Failed to load game: %s" % (e,)) - return data, None + return data, snapshot + +def encode_snapshot(snapshot): + """Encode a snapshot.""" + snapshot_file = StringIO.StringIO() + pygame.image.save(snapshot, snapshot_file) + data = snapshot_file.getvalue() + data = base64.standard_b64encode(data) + return data + +def decode_snapshot(data): + """Decode a snapshot.""" + data = base64.standard_b64decode(data) + snapshot_file = StringIO.StringIO(data) + snapshot = pygame.image.load(snapshot_file, "snapshot.tga") + return snapshot class SaveGameError(Exception): @@ -149,8 +174,13 @@ filename = self.get_fullpath() if filename is None: return + data = gameboard.save_game() - params = (version.SAVE_GAME_VERSION, data) + + snapshot = gameboard.snapshot() + snapshot_data = encode_snapshot(snapshot) + + params = (version.SAVE_GAME_VERSION, data, snapshot_data) xml = xmlrpclib.dumps(params, "foxassault") try: open(filename, "wb").write(xml)