"""graphics class, helps everyone to draw""" import pygame, pygame.image from pygame.locals import * import game, stars #the accessable screen surface and size surface = None background = None rect = Rect(0, 0, 0, 0) #the accessable dirty rectangles dirtyrects = [] starobj = None def initialize(size, fullscreen): global surface, rect, background, starobj try: flags = 0 if fullscreen: flags |= FULLSCREEN depth = pygame.display.mode_ok(size, flags, 16) surface = pygame.display.set_mode(size, flags, depth) rect = surface.get_rect() pygame.mouse.set_visible(0) if surface.get_bytesize() == 1: print "loading palette" loadpalette() background = pygame.Surface((rect.width,rect.height)).convert() background.fill(0) except pygame.error, msg: import messagebox messagebox.error('Cannot Initialize Graphics', msg.args[0]) starobj = stars.Stars(game.starrect) def dirty(rect): dirtyrects.append(rect) def dirty2(rect1, rect2): if not rect2: dirtyrects.append(rect1) elif rect.colliderect(rect2): dirtyrects.append(rect1.union(rect2)) else: dirtyrects.append(rect1) dirtyrects.append(rect2) def updatestars(bgd, gfx): starobj.erase_tick_draw(bgd, gfx) def update(): global dirtyrects pygame.display.update(dirtyrects) dirtyrects = [] def text(font, color, text, center=None, pos='center'): bgd = 0, 0, 0 try: if surface.get_bytesize()>1: img = font.render(text, 1, color, bgd) img.set_colorkey(bgd, RLEACCEL) else: img = font.render(text, 0, color) img = img.convert() except pygame.error: print 'TEXTFAILED', text, color img = pygame.Surface((10, 10)) r = img.get_rect() if center: setattr(r, pos, center) return [img, r] def load(name): file = game.get_resource(name) img = pygame.image.load(file) #use rle acceleration if no hardware accel if not surface.get_flags() & HWSURFACE: clear = img.get_colorkey() if not clear: clear = img.get_at((0,0)) img.set_colorkey(clear, RLEACCEL) return img.convert() def loadpalette(): file = game.get_resource('starblazer.pal') pal = [] for line in file.readlines()[2:]: vals = [int(x) for x in line.split()] pal.append(vals) surface.set_palette(pal) def clearscreen(): """clear the screen- effect shows on the next update()""" surface.set_clip() surface.fill(0) dirty(surface.get_rect()) background.fill(0)