Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

166 wiersze
5.5 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. import numpy
  10. textures = [0,0]
  11. buflist = False
  12. def resize((width, height)):
  13. glViewport(0, 0, width, height)
  14. glMatrixMode(GL_PROJECTION)
  15. glLoadIdentity()
  16. glOrtho(0, width, height, 0, -1, 10);
  17. glMatrixMode(GL_MODELVIEW)
  18. glLoadIdentity()
  19. def init():
  20. glEnable(GL_TEXTURE_2D)
  21. load_textures()
  22. make_vbo()
  23. glShadeModel(GL_SMOOTH)
  24. glClearColor(0.0, 0.0, 0.0, 0.0)
  25. glClearDepth(1.0)
  26. glEnable(GL_DEPTH_TEST)
  27. glDepthFunc(GL_LEQUAL)
  28. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
  29. def load_textures():
  30. texturefile = os.path.join('art','test','groundtest.png')
  31. textureSurface = pygame.image.load(texturefile)
  32. textureData = pygame.image.tostring(textureSurface, "RGBX", 1)
  33. glBindTexture(GL_TEXTURE_2D, textures[0])
  34. glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0,
  35. GL_RGBA, GL_UNSIGNED_BYTE, textureData );
  36. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  37. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  38. def make_vbo():
  39. global buflist
  40. buflist = glGenBuffers(3)
  41. vertices = [False] * (20 * 15)
  42. for y in range(15):
  43. for x in range(20):
  44. ty = y * 32
  45. tx = x * 32
  46. # Z coord is used for blit order
  47. vertices[x + y * 20] = [tx, ty, y * 0.01,
  48. tx + 32, ty, y * 0.01,
  49. tx + 32, ty + 32, y * 0.01,
  50. tx, ty + 32, y * 0.01]
  51. vertices = numpy.array(vertices, dtype=numpy.float32)
  52. glBindBuffer(GL_ARRAY_BUFFER, buflist[0])
  53. glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW)
  54. indices = numpy.array([x for x in range(4 * 20 * 15)], dtype=numpy.int)
  55. glBindBuffer(GL_ARRAY_BUFFER, buflist[2])
  56. glBufferData(GL_ARRAY_BUFFER, indices, GL_STATIC_DRAW)
  57. def put_map(themap):
  58. uvs = [False] * (20 * 15)
  59. index = 0
  60. for line in themap:
  61. for tile in line:
  62. ty = .0625 * (15 - tile / 16)
  63. tx = .0625 * (tile % 16)
  64. uvs[index] = [tx, ty + .0625,
  65. tx + .0625, ty + .0625,
  66. tx + .0625, ty,
  67. tx, ty]
  68. index += 1
  69. uvs = numpy.array(uvs, dtype=numpy.float32)
  70. glBindBuffer(GL_ARRAY_BUFFER, buflist[1])
  71. glBufferData(GL_ARRAY_BUFFER, uvs, GL_STATIC_DRAW)
  72. glEnableClientState(GL_VERTEX_ARRAY)
  73. glEnableClientState(GL_TEXTURE_COORD_ARRAY)
  74. glEnableClientState(GL_INDEX_ARRAY)
  75. glBindTexture(GL_TEXTURE_2D, textures[0])
  76. glBindBuffer(GL_ARRAY_BUFFER, buflist[0])
  77. glVertexPointer(3, GL_FLOAT, 0, None)
  78. glBindBuffer(GL_ARRAY_BUFFER, buflist[1])
  79. glTexCoordPointer(2, GL_FLOAT, 0, None)
  80. glBindBuffer(GL_ARRAY_BUFFER, buflist[2])
  81. glIndexPointer(GL_INT, 0, None)
  82. glDrawArrays(GL_QUADS, 0, 4 * 20 * 15)
  83. glDisableClientState(GL_VERTEX_ARRAY)
  84. glDisableClientState(GL_TEXTURE_COORD_ARRAY)
  85. glDisableClientState(GL_INDEX_ARRAY)
  86. def draw():
  87. glClear(GL_DEPTH_BUFFER_BIT) # Full redraw: no need to clear color buffer
  88. glLoadIdentity()
  89. themap = [
  90. [ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 ],
  91. [ 17, 0, 1, 1, 1, 33, 1, 1, 1, 1, 1, 1, 2, 33, 3, 17, 17, 17, 17, 17 ],
  92. [ 17, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 16, 17, 19, 3, 17, 17, 17, 17 ],
  93. [ 17, 18, 17, 48, 49, 50, 48, 49, 50, 48, 49, 17, 16, 17, 16, 18, 17, 17, 17, 17 ],
  94. [ 17, 16, 17, 48, 49, 50, 48, 49, 50, 48, 49, 17, 16, 17, 16, 16, 17, 17, 17, 17 ],
  95. [ 17, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 17, 19, 35, 17, 17, 17, 17 ],
  96. [ 17, 32, 1, 1, 1, 1, 1, 1, 1, 1, 33, 1, 34, 1, 35, 17, 17, 17, 17, 17 ],
  97. [ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 ],
  98. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  99. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  100. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  101. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  102. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  103. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  104. [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
  105. ]
  106. put_map(themap)
  107. def main():
  108. video_flags = OPENGL|DOUBLEBUF
  109. pygame.init()
  110. surface = pygame.display.set_mode((640,480), video_flags)
  111. resize((640,480))
  112. init()
  113. frames = 0
  114. ticks = pygame.time.get_ticks()
  115. start = ticks
  116. while 1:
  117. event = pygame.event.poll()
  118. if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
  119. break
  120. # Enforce 33 fps
  121. while pygame.time.get_ticks() < ticks + 33:
  122. pygame.time.wait(1)
  123. ticks = pygame.time.get_ticks()
  124. draw()
  125. pygame.display.flip()
  126. frames = frames+1
  127. #if frames > 200:
  128. # break
  129. print "fps: %d" % ((frames*1000)/(pygame.time.get_ticks()-start))
  130. if __name__ == '__main__': main()
  131. #import cProfile
  132. #cProfile.run('main()')