import os from OpenGL.GL import * from OpenGL.GLU import * import pygame, pygame.image from pygame.locals import * import sys from common import Directions # TODO: Implement OBJ loading def loadModel(name): fullpath = os.path.join('models', name) f = open(fullpath) drawtable = [] for line in f.readlines(): if line[0] != "#": drawtable.append(map(float, line.strip().split())) f.close() return drawtable # TODO: About eight billion optimizations! def drawModel(name, xoff, yoff, (r, g, b)): xoff *= 2 yoff *= 2 table = loadModel(name) glBegin(GL_QUADS) glColor3f(r, g, b) for point in table: glVertex3f(point[0]+xoff, point[1]+yoff, point[2]) glEnd() def init(): glShadeModel(GL_SMOOTH) glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LEQUAL) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) rtri = 0.0 rquad = 0.0 def resize((width, height)): if height==0: height=1 glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45, 1.0*width/height, 0.1, 100.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def draw(gameState): glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) glLoadIdentity() glTranslatef(-30.0, -30.0, -90.0) y = 0 for a in gameState.board.passData: x = 0 for b in a: if gameState.board.passData[y][x] != ' ': drawModel('block.pmd', x, y, (0.4, 0.4, 0.4)) else: drawModel(gameState.board.getObject(x, y).getModelName(), x, y, gameState.board.getObject(x, y).getColors()) x += 1 y += 1 def graphicsloop(gameState): video_flags = OPENGL|DOUBLEBUF pygame.init() pygame.display.set_mode((640,480), video_flags) resize((640,480)) init() frames = 0 ticks = pygame.time.get_ticks() while 1: event = pygame.event.poll() if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): break if event.type == KEYDOWN: manageKeys(event.key, gameState) draw(gameState) pygame.display.flip() frames = frames+1 print "fps: %d" % ((frames*1000)/(pygame.time.get_ticks()-ticks)) def manageKeys(key, game): if key in command_lookup: command_lookup[key](game) else: print "Key not bound!" def up(gameState): gameState.move(Directions.UP) def down(gameState): gameState.move(Directions.DOWN) def left(gameState): gameState.move(Directions.LEFT) def right(gameState): gameState.move(Directions.RIGHT) def iact(gameState): gameState.interact() def errorCommand(gameState): print "Not a valid command!" def quit(gameState): print "End!" sys.exit() command_lookup = {K_w: up, K_a: left, K_s: down, K_d: right, K_f: iact, K_q: quit}