graphics.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import os
  2. from OpenGL.GL import *
  3. from OpenGL.GLU import *
  4. import pygame, pygame.image
  5. from pygame.locals import *
  6. import sys
  7. from common import Directions
  8. # TODO: Implement OBJ loading
  9. def loadModel(name):
  10. fullpath = os.path.join('models', name)
  11. f = open(fullpath)
  12. drawtable = []
  13. for line in f.readlines():
  14. if line[0] != "#":
  15. drawtable.append(map(float, line.strip().split()))
  16. f.close()
  17. return drawtable
  18. # TODO: About eight billion optimizations!
  19. def drawModel(name, xoff, yoff, (r, g, b)):
  20. xoff *= 2
  21. yoff *= 2
  22. table = loadModel(name)
  23. glBegin(GL_QUADS)
  24. glColor3f(r, g, b)
  25. for point in table:
  26. glVertex3f(point[0]+xoff, point[1]+yoff, point[2])
  27. glEnd()
  28. def init():
  29. glShadeModel(GL_SMOOTH)
  30. glClearColor(0.0, 0.0, 0.0, 0.0)
  31. glClearDepth(1.0)
  32. glEnable(GL_DEPTH_TEST)
  33. glDepthFunc(GL_LEQUAL)
  34. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
  35. rtri = 0.0
  36. rquad = 0.0
  37. def resize((width, height)):
  38. if height==0:
  39. height=1
  40. glViewport(0, 0, width, height)
  41. glMatrixMode(GL_PROJECTION)
  42. glLoadIdentity()
  43. gluPerspective(45, 1.0*width/height, 0.1, 100.0)
  44. glMatrixMode(GL_MODELVIEW)
  45. glLoadIdentity()
  46. def draw(gameState):
  47. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  48. glLoadIdentity()
  49. glTranslatef(-30.0, -30.0, -90.0)
  50. y = 0
  51. for a in gameState.board.passData:
  52. x = 0
  53. for b in a:
  54. if gameState.board.passData[y][x] != ' ':
  55. drawModel('block.pmd', x, y, (0.4, 0.4, 0.4))
  56. else:
  57. drawModel(gameState.board.getObject(x, y).getModelName(), x, y,
  58. gameState.board.getObject(x, y).getColors())
  59. x += 1
  60. y += 1
  61. def graphicsloop(gameState):
  62. video_flags = OPENGL|DOUBLEBUF
  63. pygame.init()
  64. pygame.display.set_mode((640,480), video_flags)
  65. resize((640,480))
  66. init()
  67. frames = 0
  68. ticks = pygame.time.get_ticks()
  69. while 1:
  70. event = pygame.event.poll()
  71. if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
  72. break
  73. if event.type == KEYDOWN:
  74. manageKeys(event.key, gameState)
  75. draw(gameState)
  76. pygame.display.flip()
  77. frames = frames+1
  78. print "fps: %d" % ((frames*1000)/(pygame.time.get_ticks()-ticks))
  79. def manageKeys(key, game):
  80. if key in command_lookup:
  81. command_lookup[key](game)
  82. else:
  83. print "Key not bound!"
  84. def up(gameState):
  85. gameState.move(Directions.UP)
  86. def down(gameState):
  87. gameState.move(Directions.DOWN)
  88. def left(gameState):
  89. gameState.move(Directions.LEFT)
  90. def right(gameState):
  91. gameState.move(Directions.RIGHT)
  92. def iact(gameState):
  93. gameState.interact()
  94. def errorCommand(gameState):
  95. print "Not a valid command!"
  96. def quit(gameState):
  97. print "End!"
  98. sys.exit()
  99. command_lookup = {K_w: up,
  100. K_a: left,
  101. K_s: down,
  102. K_d: right,
  103. K_f: iact,
  104. K_q: quit}