source: trunk/test-map.py @ 5

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

Map rendering test in PyOpenGL.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 4.0 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
13textures = [0,0]
14
15def resize((width, height)):
16    glViewport(0, 0, width, height)
17    glMatrixMode(GL_PROJECTION)
18    glLoadIdentity()
19    glOrtho(0, width, height, 0, 0, 1);
20    glMatrixMode(GL_MODELVIEW)
21    glLoadIdentity()
22
23def init():
24    glEnable(GL_TEXTURE_2D)
25    load_textures()
26    glShadeModel(GL_SMOOTH)
27    glClearColor(0.0, 0.0, 0.0, 0.0)
28    glClearDepth(1.0)
29    glEnable(GL_DEPTH_TEST)
30    glDepthFunc(GL_LEQUAL)
31    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
32
33def load_textures():
34    texturefile = os.path.join('art','test','groundtest.png')
35    textureSurface = pygame.image.load(texturefile)
36    textureData = pygame.image.tostring(textureSurface, "RGBX", 1)
37
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
44def put_map(themap):
45    glBegin(GL_QUADS)
46    y = 0.0
47    for line in themap:
48        x = 0.0
49        for tile in line:
50            sy = .0625 * (15 - tile / 16)
51            sx = .0625 * (tile % 16)
52            glTexCoord2f(sx, sy); glVertex2f(x, y + 32.0);
53            glTexCoord2f(sx + .0625, sy); glVertex2f(x + 32.0, y + 32.0);
54            glTexCoord2f(sx + .0625, sy + .0625); glVertex2f(x + 32.0, y);
55            glTexCoord2f(sx, sy + .0625); glVertex2f(x, y);
56            x += 32.0
57        y += 32.0
58    glEnd();                           
59
60def draw():
61    #glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
62    glLoadIdentity()
63
64    themap = [
65        [ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 ],
66        [ 17,  0,  1,  1,  1, 33,  1,  1,  1,  1,  1,  1,  2, 33,  3, 17, 17, 17, 17, 17 ],
67        [ 17, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 16, 17, 19,  3, 17, 17, 17, 17 ],
68        [ 17, 18, 17, 48, 49, 50, 48, 49, 50, 48, 49, 17, 16, 17, 16, 18, 17, 17, 17, 17 ],
69        [ 17, 16, 17, 48, 49, 50, 48, 49, 50, 48, 49, 17, 16, 17, 16, 16, 17, 17, 17, 17 ],
70        [ 17, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 17, 19, 35, 17, 17, 17, 17 ],
71        [ 17, 32,  1,  1,  1,  1,  1,  1,  1,  1, 33,  1, 34,  1, 35, 17, 17, 17, 17, 17 ],
72        [ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 ],
73        [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
74        [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
75        [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
76        [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
77        [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
78        [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
79        [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
80    ]
81    put_map(themap)
82
83def main():
84
85    video_flags = OPENGL|DOUBLEBUF
86
87    pygame.init()
88    surface = pygame.display.set_mode((640,480), video_flags)
89
90    resize((640,480))
91    init()
92
93    frames = 0
94    ticks = pygame.time.get_ticks()
95    start = ticks
96    while 1:
97        event = pygame.event.poll()
98        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
99            break
100
101        # Enforce 33 fps
102        while pygame.time.get_ticks() < ticks + 33:
103            pygame.time.wait(1)
104        ticks = pygame.time.get_ticks()
105
106        draw()
107        pygame.display.flip()
108        frames = frames+1
109#        if frames > 200:
110#            break
111
112    print "fps:  %d" % ((frames*1000)/(pygame.time.get_ticks()-start))
113
114
115if __name__ == '__main__': main()
116
117#import cProfile
118#cProfile.run('main()')
119
120
Note: See TracBrowser for help on using the repository browser.