From: <Blu...@us...> - 2010-09-01 15:37:28
|
Revision: 385 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=385&view=rev Author: BlueWolf_ Date: 2010-09-01 15:37:21 +0000 (Wed, 01 Sep 2010) Log Message: ----------- * Download-threads will now stop when closing VP. * The playground-test-walk now works with the timer to test the performance. * Also moved the FPS to 100 because I suspect a bug in the updater which halves the FPS in some situations. In the future 50FPS will probably be enough Modified Paths: -------------- trunk/client/VP.py trunk/client/downloader.py trunk/client/functions.py trunk/client/playground.py Modified: trunk/client/VP.py =================================================================== --- trunk/client/VP.py 2010-09-01 14:24:10 UTC (rev 384) +++ trunk/client/VP.py 2010-09-01 15:37:21 UTC (rev 385) @@ -103,6 +103,7 @@ # Someone decided to close VP (Why??) try: sh['client'].close() except: pass + sh['downloader'].stop() sys.exit() @@ -125,7 +126,7 @@ # When there are more updates from here one, they will be saved without # being updated immediately. We will do that once tick stops blocking self.updatewaiting = True - self.clock.tick(50) + self.clock.tick(UPDATE_FPS) # Get all the rects to update if len(self.updaterect) == 1: @@ -154,7 +155,7 @@ This timer will update every 33ms (30fps) ones called """ def __init__(self): - self.speed = 0.033 + self.speed = 1/float(UPDATE_FPS) self.callers = {} self.timer = None Modified: trunk/client/downloader.py =================================================================== --- trunk/client/downloader.py 2010-09-01 14:24:10 UTC (rev 384) +++ trunk/client/downloader.py 2010-09-01 15:37:21 UTC (rev 385) @@ -15,7 +15,7 @@ ## along with this program; if not, write to the Free Software from functions import * -from Queue import Queue +import Queue import urllib2, shutil DOWNLOAD_THREADS = 2 @@ -25,17 +25,17 @@ This class will download and check all the stuff that is needed """ def __init__(self): - self.notify = threading.Condition() + self.queue = Queue.PriorityQueue() - self.tasks = [] # Dict within a list with the following things: - # filename, filetype, callbacks, usages + self.tasks = {} # Dict with the key being the filename and value being a + # dict with: filetype, callbacks, usages self.loaded_files = {} # Dict with the following things: # filetype, cache, usages self.workers = [] for i in range(DOWNLOAD_THREADS): - self.workers.append(Worker(self.tasks, self.notify, self.error, \ + self.workers.append(Worker(self.tasks, self.queue, self.error, \ self.loaded_files)) @@ -51,23 +51,20 @@ if filename in sh['filetable']: # We know where it lives. Do we already have a task for this? - task = self.find_task(filename) - if task: + if self.tasks.has_key(filename): # Append our callback + task = self.tasks[filename] task['usages'] += 1 task['callbacks'].append((callback, arg)) else: # Create a new task - self.tasks.append({ - "filename": filename, + self.tasks[filename] = { "filetype": filetype, "callbacks": [(callback, arg),], "usages": 1, - }) - self.notify.acquire() - self.notify.notify() - self.notify.release() + } + self.queue.put((1, filename)) else: print "File '" + filename + "' is not in the repository!" @@ -87,53 +84,50 @@ def remove_loaded_file(self, filename): try: del self.loaded_files[filename] except: pass - - - def find_task(self, filename): - for task in self.tasks: - if task['filename'] == filename: - return task - return None def error(self, filetype, error): if filetype == "img": # [TODO] Maybe show a little error-image? return pygame.Surface((1,1)) + + def stop(self): + # Stop the download-threads + for i in range(DOWNLOAD_THREADS): + self.queue.put((0, "STOP")) + class Worker(threading.Thread): - def __init__(self, tasks, notify, error, loaded_files): + def __init__(self, tasks, queue, error, loaded_files): self.tasks = tasks - self.notify = notify + self.queue = queue self.error = error self.loaded_files = loaded_files + self.quiting = False threading.Thread.__init__(self) self.start() def run(self): - self.notify.acquire() - while 1: - try: - task = self.tasks.pop(0) - except IndexError: - self.notify.wait() - continue + task = self.queue.get()[1] + if task == "STOP": return - + filename = task + task = self.tasks[filename] + # Setting some variables destination = real_path("downloads", task['filetype'], \ - task['filename']) + filename) - if not task['filename'] in sh['filetable']: + if not filename in sh['filetable']: print "File '" + filename + "' is not in the repository!" - self.callbacks(task, self.error(task['filetype'], \ - "not in repos")) + self.callbacks(filename, task['callbacks'], \ + self.error(task['filetype'], "not in repos")) return - filetable = sh['filetable'][task['filename']] + filetable = sh['filetable'][filename] url = filetable['url'] checksum = filetable['checksum'] @@ -144,7 +138,7 @@ if os.path.exists(destination): # Check if we need to download the file filehash = file_checksum("downloads", task['filetype'], \ - task['filename']) + filename) if checksum != filehash: need_download = True @@ -163,8 +157,8 @@ try: f = urllib2.urlopen(request) except urllib2.URLError, e: print "Url '" + url + "' could not be found!" - self.callbacks(task, self.error(task['filetype'], \ - "not found")) + self.callbacks(filename, task['callbacks'], \ + self.error(filename, "not found")) return target = file(destination + ".tmp", "wb") @@ -181,12 +175,13 @@ # Check the checksum filehash = file_checksum("downloads", task['filetype'], \ - task['filename'] + ".tmp") + filename + ".tmp") if checksum != filehash: - print "Checksum mismatch for '" + task['filename'] + "! " + \ + print "Checksum mismatch for '" + filename + "! " + \ filehash + " > " + checksum - self.callbacks(task, self.error(task['filetype'], "checksum")) + self.callbacks(filename, task['callbacks'], \ + self.error(task['filetype'], "checksum")) # Remove tmp file os.remove(destination + ".tmp") @@ -198,7 +193,6 @@ # Everything went good! Now load the file load = { - "filename": task['filename'], "filetype": task['filetype'], "cache": None, "usages": task['usages'] @@ -207,15 +201,12 @@ if task['filetype'] == "img": # Load image load['cache'] = load_image(True, "downloads", task['filetype'],\ - task['filename']) + filename) - self.loaded_files[task['filename']] = load - self.callbacks(task, load['cache']) + self.loaded_files[filename] = load + self.callbacks(filename, task['callbacks'], load['cache']) - - self.notify.release() - - def callbacks(self, task, cache): - for callback in task['callbacks']: - callback[0](task['filename'], cache, *callback[1]) + def callbacks(self, filename, callbacks, cache): + for callback in callbacks: + callback[0](filename, cache, *callback[1]) Modified: trunk/client/functions.py =================================================================== --- trunk/client/functions.py 2010-09-01 14:24:10 UTC (rev 384) +++ trunk/client/functions.py 2010-09-01 15:37:21 UTC (rev 385) @@ -22,8 +22,10 @@ VERSION = "0.0.1" -SERVER = ("localhost", 6653) +SERVER = ("vp.bluewolf.nl", 6653) +UPDATE_FPS = 100 + # This will be used to [sh]are all variables, functions and classes across the # code sh = {} Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-09-01 14:24:10 UTC (rev 384) +++ trunk/client/playground.py 2010-09-01 15:37:21 UTC (rev 385) @@ -38,6 +38,7 @@ # Playground stuff self.backgrounds = {} self.viewport = None + self.direction_pressed = None def event(self, ev): @@ -58,18 +59,27 @@ elif self.status == "playground": if ev.type == KEYDOWN: - if ev.key == K_UP: - self.viewport.move_ip(0, -10) - self.playground_render() - if ev.key == K_DOWN: - self.viewport.move_ip(0, 10) - self.playground_render() - if ev.key == K_LEFT: - self.viewport.move_ip(-10, 0) - self.playground_render() - if ev.key == K_RIGHT: - self.viewport.move_ip(10, 0) - self.playground_render() + if ev.key == K_UP and self.direction_pressed != 0: + self.direction_pressed = 0 + sh['timer'].start("playground_move", self.playground_move, \ + time.time(), self.viewport.top, 0) + elif ev.key == K_DOWN and self.direction_pressed != 1: + self.direction_pressed = 1 + sh['timer'].start("playground_move", self.playground_move, \ + time.time(), self.viewport.top, 1) + elif ev.key == K_LEFT and self.direction_pressed != 2: + self.direction_pressed = 2 + sh['timer'].start("playground_move", self.playground_move, \ + time.time(), self.viewport.left, 2) + elif ev.key == K_RIGHT and self.direction_pressed != 3: + self.direction_pressed = 3 + sh['timer'].start("playground_move", self.playground_move, \ + time.time(), self.viewport.left, 3) + + elif ev.type == KEYUP: + if ev.key in [K_UP, K_DOWN, K_LEFT, K_RIGHT]: + self.direction_pressed = None + sh['timer'].stop("playground_move") def call(self, name, data): @@ -281,8 +291,25 @@ self.transit = 1 - timeline self.playground_render() + + + def playground_move(self, name, startime, startpos, direction): + # Temporary test function to walk + + timeline = time.time() - startime + speed = 175 + move = timeline*speed + + if direction == 0: + self.viewport.top = startpos - move + elif direction == 1: + self.viewport.top = startpos + move + elif direction == 2: + self.viewport.left = startpos - move + elif direction == 3: + self.viewport.left = startpos + move + self.playground_render() - class LoginFeedback(gui.Feedback): def __init__(self, parent): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |