source: trunk/test-map.py @ 9

Last change on this file since 9 was 8, checked in by sam, 11 years ago

Testing vertex buffers. They're really, really faster.

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