Commits

fabe  committed 99faacf

some code cleaning

  • Participants
  • Parent commits b1b9187

Comments (0)

Files changed (4)

File gfx_engine.py

 ################################################################################
 
 
+"""
+TODO docu and example code
+
+"""
+
+
 import pygame
 from Queue import Queue
+from model import Image
 from pygame.locals import *
 
 
-class Image:
+__img_queue__ = None
+__jobs__ = []
+__filters__ = []
 
-    def __init__(self, surface):
-        self.surface = surface
 
-    def draw_to(self, screen):
-        screen_offset = (0, 0)       
-        surface_area = self.surface.get_rect()
-        screen.blit(self.surface, screen_offset, surface_area)
+def init(resolution = (800, 600), fullscreen = True, title = "odium"):
+    global __img_queue__
+    __img_queue__ = Queue() 
+    pygame.display.set_mode(resolution)
+    pygame.display.set_caption(title)
+    pygame.mouse.set_visible(0)
 
 
-class Renderer(object):
-# TODO use OpenGL and maby implement in c
+def render():
+    global __jobs__
+    global __img_queue__
+    global __filters__
+    screen = pygame.display.get_surface()
+    # run jobs
+    for job in __jobs__:
+        job()
+    # process img queue
+    while not __img_queue__.empty():
+        image = __img_queue__.get()
+        image.draw_to(screen)
+        __img_queue__.task_done()
+    # apply filters
+    for filter in __filters__:
+        filter(screen)
+    pygame.display.flip()
 
-    queue = None
-    __pipeline__ = []
 
-    @classmethod
-    def init(cls, size = (800, 600), fullscreen = True, title = "odium"):
-        cls.queue = Queue() 
-        pygame.display.set_mode(size)
-        pygame.display.set_caption(title)
-        pygame.mouse.set_visible(0)
+def add_job(func):
+    global __jobs__
+    __jobs__.append(func)
 
-    @classmethod
-    def resize_screen(cls, size = (800, 600), fullscreen = True):
-        pass # TODO
+def remove_job(func):
+    global __jobs__
+    __jobs__.remove(func)
 
-    @classmethod
-    def toggle_fullscreen():
-        pass # TODO
+def add_filter(func):
+    global __filters__
+    __filters__.append(func)
 
-    @classmethod
-    def add_filter(cls, func):
-        """ Note: Functions are called in the order they were added. """
-        cls.__pipeline__.append(func)
+def remove_filter(func):
+    global __filters__
+    __filters__.remove(func)
 
-    @classmethod
-    def remove_filter(cls, func):
-        cls.__pipeline__.remove(func)
+def queue_img(img):
+    global __img_queue__
+    __img_queue__.put(img)
 
-    @classmethod
-    def render(cls):
-        for filter in cls.__pipeline__:
-            filter()
-        screen = pygame.display.get_surface()
-        while not cls.queue.empty():
-            image = cls.queue.get()
-            image.draw_to(screen)
-            cls.queue.task_done()
-        pygame.display.flip()
 
+def screenshot():
+    pass # TODO
 
+def resize_screen(resolution = (800, 600), fullscreen = True):
+    pass # TODO
+
+def toggle_fullscreen():
+    pass # TODO
+
+
+################################################################################
+#                                                                              #
+#	License:       GPL Version 3, see LICENSE file                             #
+#                                                                              #
+#	Authors:       Odium team, See AUTHORS file                                #
+#                                                                              #
+#	Maintainer:    Fabian Barkhau (fabian.barkhau at gmail dot com)            #
+#                                                                              #
+################################################################################
+
+
+import pygame
+from pygame.locals import *
+
+
+class Image:
+
+    def __init__(self, surface, offset = (0,0)):
+        self.surface = surface
+        self.offset = offset
+
+    def draw_to(self, screen):
+        area = self.surface.get_rect()
+        screen.blit(self.surface, self.offset, area)
+
+
 ################################################################################
 
 
-import os, sys
 import pygame
-from gfx_engine import *
-from system_io import *
+import gfx_engine
+import system_io
+from gfx_engine import Image
 from pygame.locals import *
 
 
-class Resource(Image):
-    
-    def __call__(self): 
-        Renderer.queue.put(self)
-
-
 def exit():
     for event in pygame.event.get():
         if event.type == QUIT:
     return False
 
 
+class TestRenderJob:
+
+    def __init__(self, img):
+        self.img = img
+
+    def __call__(self):
+        gfx_engine.queue_img(self.img)
+    
+
 def main():
     pygame.init()
-    Renderer.init() 
-    img = Resource(load_image("test.bmp"))
-    Renderer.add_filter(img)
+    gfx_engine.init()
+    img = Image(system_io.load_surface("data/test.bmp"))
+    job = TestRenderJob(img)
+    gfx_engine.add_job(job)
     clock = pygame.time.Clock()
     while not exit():
-        Renderer.render()
+        gfx_engine.render()
         clock.tick()
     fps = clock.get_fps()
     print fps

File system_io.py

+################################################################################
+#                                                                              #
+#	License:       GPL Version 3, see LICENSE file                             #
+#                                                                              #
+#	Authors:       Odium team, See AUTHORS file                                #
+#                                                                              #
+#	Maintainer:    Fabian Barkhau (fabian.barkhau at gmail dot com)            #
+#                                                                              #
+################################################################################
+
+
+import os, sys
+import pygame
+from pygame.locals import *
+
+
+def load_surface(file, convert = True, per_pixel_alpha = False, 
+                 colorkey = None, surface_alpha = None):
+    """
+    convert must be enbled for per_pixel_alpha.
+    colorkey and surface_alpha will be ignored if per_pixel_alpha is used.
+    """
+    try:
+        surface = pygame.image.load(file)
+    except pygame.error, message:
+        # TODO log error, sys exit going to far?
+        raise SystemExit, message
+    if convert:
+        if per_pixel_alpha:
+            surface = surface.convert_alpha()
+        else: 
+            surface = surface.convert()
+    if not per_pixel_alpha:
+        surface.set_alpha(surface_alpha, RLEACCEL)
+        surface.set_colorkey(colorkey, RLEACCEL)
+    return surface
+
+