Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

121 linhas
4.0 KiB

  1. #!/usr/bin/env python
  2. import os
  3. import OpenGL
  4. OpenGL.ERROR_CHECKING = False
  5. from OpenGL.GL import *
  6. from OpenGL.GLU import *
  7. import pygame, pygame.image
  8. from pygame.locals import *
  9. textures = [0,0]
  10. def resize((width, height)):
  11. glViewport(0, 0, width, height)
  12. glMatrixMode(GL_PROJECTION)
  13. glLoadIdentity()
  14. glOrtho(0, width, height, 0, 0, 1);
  15. glMatrixMode(GL_MODELVIEW)
  16. glLoadIdentity()
  17. def init():
  18. glEnable(GL_TEXTURE_2D)
  19. load_textures()
  20. glShadeModel(GL_SMOOTH)
  21. glClearColor(0.0, 0.0, 0.0, 0.0)
  22. glClearDepth(1.0)
  23. glEnable(GL_DEPTH_TEST)
  24. glDepthFunc(GL_LEQUAL)
  25. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
  26. def load_textures():
  27. texturefile = os.path.join('art','test','groundtest.png')
  28. textureSurface = pygame.image.load(texturefile)
  29. textureData = pygame.image.tostring(textureSurface, "RGBX", 1)
  30. glBindTexture(GL_TEXTURE_2D, textures[0])
  31. glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0,
  32. GL_RGBA, GL_UNSIGNED_BYTE, textureData );
  33. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  34. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  35. def put_map(themap):
  36. glBegin(GL_QUADS)
  37. y = 0.0
  38. for line in themap:
  39. x = 0.0
  40. for tile in line:
  41. sy = .0625 * (15 - tile / 16)
  42. sx = .0625 * (tile % 16)
  43. glTexCoord2f(sx, sy); glVertex2f(x, y + 32.0);
  44. glTexCoord2f(sx + .0625, sy); glVertex2f(x + 32.0, y + 32.0);
  45. glTexCoord2f(sx + .0625, sy + .0625); glVertex2f(x + 32.0, y);
  46. glTexCoord2f(sx, sy + .0625); glVertex2f(x, y);
  47. x += 32.0
  48. y += 32.0
  49. glEnd();
  50. def draw():
  51. #glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  52. glLoadIdentity()
  53. themap = [
  54. [ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 ],
  55. [ 17, 0, 1, 1, 1, 33, 1, 1, 1, 1, 1, 1, 2, 33, 3, 17, 17, 17, 17, 17 ],
  56. [ 17, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 16, 17, 19, 3, 17, 17, 17, 17 ],
  57. [ 17, 18, 17, 48, 49, 50, 48, 49, 50, 48, 49, 17, 16, 17, 16, 18, 17, 17, 17, 17 ],
  58. [ 17, 16, 17, 48, 49, 50, 48, 49, 50, 48, 49, 17, 16, 17, 16, 16, 17, 17, 17, 17 ],
  59. [ 17, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 17, 19, 35, 17, 17, 17, 17 ],
  60. [ 17, 32, 1, 1, 1, 1, 1, 1, 1, 1, 33, 1, 34, 1, 35, 17, 17, 17, 17, 17 ],
  61. [ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 ],
  62. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  63. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  64. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  65. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  66. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  67. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  68. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  69. ]
  70. put_map(themap)
  71. def main():
  72. video_flags = OPENGL|DOUBLEBUF
  73. pygame.init()
  74. surface = pygame.display.set_mode((640,480), video_flags)
  75. resize((640,480))
  76. init()
  77. frames = 0
  78. ticks = pygame.time.get_ticks()
  79. start = ticks
  80. while 1:
  81. event = pygame.event.poll()
  82. if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
  83. break
  84. # Enforce 33 fps
  85. while pygame.time.get_ticks() < ticks + 33:
  86. pygame.time.wait(1)
  87. ticks = pygame.time.get_ticks()
  88. draw()
  89. pygame.display.flip()
  90. frames = frames+1
  91. # if frames > 200:
  92. # break
  93. print "fps: %d" % ((frames*1000)/(pygame.time.get_ticks()-start))
  94. if __name__ == '__main__': main()
  95. #import cProfile
  96. #cProfile.run('main()')