You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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