source: trunk/test-map.py @ 12

Last change on this file since 12 was 12, checked in by sam, 13 years ago

Alpha test.

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