Commits

Pierre Surply committed 9f3828c

Optimisation GL

  • Participants
  • Parent commits ee8dc03

Comments (0)

Files changed (3)

File src/display.py

 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>. 
 ## 
 ## Started on  Wed May  9 17:19:46 2012 Pierre Surply
-## Last update Mon Aug 27 13:16:53 2012 Pierre Surply
+## Last update Mon Aug 27 16:01:43 2012 Pierre Surply
 ##
 
 import pygame
         glEnable (GL_BLEND);
         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
-        #self.set_bg()
         self.set_tile()
+        self.set_display_list()
         self.ascii = pygame.image.load("res/img/font.png").convert_alpha()
         self.ascii_wb = surface.cut_surface(self.ascii, (8, 12))
         self.ascii_bw = surface.cut_surface(surface.change_color(self.ascii, \
                                                                      pygame.Color(0, 100, 0, 255),\
                                                                      pygame.Color(255, 255, 255, 255)),\
                                                (8, 12))
+
+    def set_display_list(self):
+        self.list_char = glGenLists(1)
+        glNewList(self.list_char,GL_COMPILE)
+        glBegin(GL_QUADS) 
+        glColor3d(0xFF, 0xFF, 0xFF)
+        glTexCoord2f(0, 0); glVertex2f(0, 12)
+        glTexCoord2f(0, 1); glVertex2f(0, 0)
+        glTexCoord2f(1, 1); glVertex2f(8, 0)
+        glTexCoord2f(1, 0); glVertex2f(8, 12)        
+        glEnd()
+        glEndList()
+
     def set_display(self, size):
         self.window = pygame.display.set_mode(size, pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE)
         glMatrixMode(GL_PROJECTION)
         for y in range(h):
             for x in range(w):
                 if self.tile[y][x] != None:
+                    glPushMatrix()
+                    glTranslated(x*8, y*12, 0)
                     glBindTexture(GL_TEXTURE_2D, self.tile2surface(self.tile[y][x]))
-                    glBegin(GL_QUADS) 
-                    glColor3d(0xFF, 0xFF, 0xFF)
-                    glTexCoord2f(0, 0); glVertex2f(x*8, y*12+12)
-                    glTexCoord2f(0, 1); glVertex2f(x*8, y*12)
-                    glTexCoord2f(1, 1); glVertex2f(x*8+8, y*12)
-                    glTexCoord2f(1, 0); glVertex2f(x*8+8, y*12+12)
-                   
-                    glEnd()
+                    glCallList(self.list_char)
+                    glPopMatrix()
 
     def flip(self):
         pygame.display.flip()
 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>. 
 ##
 ## Started on  Thu Jun 28 13:22:05 2012 Pierre Surply
-## Last update Mon Aug 27 15:28:34 2012 Pierre Surply
+## Last update Mon Aug 27 16:39:56 2012 Pierre Surply
 ##
 
 import os
     size = 64
     
     def __init__(self, path, biome, (x, y), events):
+        self.set_display_list()
         self.init_tile()
         self.robots = []
         self.running_robot = 0
         self.sprite_elts = surface.cut_surface(pygame.image.load("res/img/lvl_elts.png").convert_alpha(), (16, 16))
         self.sprite_robots = surface.cut_surface(pygame.image.load("res/img/robots.png").convert_alpha(), (32, 32))
         self.cursor = surface.surface2texture(pygame.image.load("res/img/cursor.png").convert_alpha())
+
+
+    def set_display_list(self):
+        self.list_floor = glGenLists(2)
+        glNewList(self.list_floor,GL_COMPILE)
+        glBegin(GL_QUADS)
+        glTexCoord2d(0,0); glVertex3d(0, 1, 0)
+        glTexCoord2d(0,1); glVertex3d(0, 0, 0)
+        glTexCoord2d(1,1); glVertex3d(1, 0, 0)
+        glTexCoord2d(1,0); glVertex3d(1, 1, 0)
+        glEnd()
+        glEndList()
+
+        self.list_floor_side = glGenLists(3)
+        glNewList(self.list_floor_side,GL_COMPILE)
+        glBegin(GL_QUADS)
+        glTexCoord2d(0,0); glVertex3d(0, 0, 1)
+        glTexCoord2d(0,1); glVertex3d(0, 0, 0)
+        glTexCoord2d(1,1); glVertex3d(1, 0, 0)
+        glTexCoord2d(1,0); glVertex3d(1, 0, 1)
+        glEnd()
+        glEndList()
+
+        self.list_elt = glGenLists(3)
+        glNewList(self.list_elt,GL_COMPILE)
+        glBegin(GL_QUADS)
+        glTexCoord2d(0,0); glVertex3d(0, 0, 0)
+        glTexCoord2d(0,1); glVertex3d(0, 0, 1.41)
+        glTexCoord2d(1,1); glVertex3d(1, 1, 1.41)
+        glTexCoord2d(1,0); glVertex3d(1, 1, 0)
+        glEnd()
+        glBegin(GL_QUADS)
+        glTexCoord2d(0,0); glVertex3d(0, 1, 0)
+        glTexCoord2d(0,1); glVertex3d(0, 1, 1.41)
+        glTexCoord2d(1,1); glVertex3d(1, 0, 1.41)
+        glTexCoord2d(1,0); glVertex3d(1, 0, 0)
+        glEnd()
+        glEndList()
+
+        self.list_robot = glGenLists(3)
+        glNewList(self.list_robot,GL_COMPILE)
+        glBegin(GL_QUADS)
+        glTexCoord2d(0,0); glVertex3d(-0.5, 0.5, 0)
+        glTexCoord2d(0,1); glVertex3d(-0.5, -0.5, 0)
+        glTexCoord2d(1,1); glVertex3d(0.5, -0.5, 0)
+        glTexCoord2d(1,0); glVertex3d(0.5, 0.5, 0)
+        glEnd()
+        glEndList()
         
     def init_tile(self):
         self.tile = [[0] * self.size for i in range(self.size)]
     def render_case(self, x, y):
         glBindTexture(GL_TEXTURE_2D, self.sprite_floor[self.tile[x][y]])
         z = self.temp_tiles[self.tile[x][y]][3]
-        glBegin(GL_QUADS)
-        glTexCoord2d(0,0); glVertex3d(x, y+1, z)
-        glTexCoord2d(0,1); glVertex3d(x, y, z)
-        glTexCoord2d(1,1); glVertex3d(x+1, y, z)
-        glTexCoord2d(1,0); glVertex3d(x+1, y+1, z)
-        glEnd()
+        glPushMatrix()
+        glTranslated(x, y, z)
+        glCallList(self.list_floor)
         if (not self.tile[x][y] in [2, 4]):
             if (y >= self.size-1 or self.temp_tiles[self.tile[x][y+1]][3] < z):
-                glBegin(GL_QUADS)
-                glTexCoord2d(0,0); glVertex3d(x, y+1, z-1)
-                glTexCoord2d(0,1); glVertex3d(x, y+1, z)
-                glTexCoord2d(1,1); glVertex3d(x+1, y+1, z)
-                glTexCoord2d(1,0); glVertex3d(x+1, y+1, z-1)
-                glEnd()
+                glPushMatrix()
+                glTranslated(0, 1, -1)
+                glCallList(self.list_floor_side)
+                glPopMatrix()
             if (y <= 0 or self.temp_tiles[self.tile[x][y-1]][3] < z):
-                glBegin(GL_QUADS)
-                glTexCoord2d(0,0); glVertex3d(x, y, z-1)
-                glTexCoord2d(0,1); glVertex3d(x, y, z)
-                glTexCoord2d(1,1); glVertex3d(x+1, y, z)
-                glTexCoord2d(1,0); glVertex3d(x+1, y, z-1)
-                glEnd()
+                glPushMatrix()
+                glTranslated(0, 0, -1)
+                glCallList(self.list_floor_side)
+                glPopMatrix()
             if (x <= 0 or self.temp_tiles[self.tile[x-1][y]][3] < z):
-                glBegin(GL_QUADS)
-                glTexCoord2d(0,0); glVertex3d(x, y, z-1)
-                glTexCoord2d(0,1); glVertex3d(x, y, z)
-                glTexCoord2d(1,1); glVertex3d(x, y+1, z)
-                glTexCoord2d(1,0); glVertex3d(x, y+1, z-1)
-                glEnd()
+                glPushMatrix()
+                glTranslated(0, 0, -1)
+                glRotated(90, 0, 0, 1)
+                glCallList(self.list_floor_side)
+                glPopMatrix()
             if (x >= self.size-1 or self.temp_tiles[self.tile[x+1][y]][3] < z):
-                glBegin(GL_QUADS)
-                glTexCoord2d(0,0); glVertex3d(x+1, y, z-1)
-                glTexCoord2d(0,1); glVertex3d(x+1, y, z)
-                glTexCoord2d(1,1); glVertex3d(x+1, y+1, z)
-                glTexCoord2d(1,0); glVertex3d(x+1, y+1, z-1)
-                glEnd()
+                glPushMatrix()
+                glTranslated(1, 0, -1)
+                glRotated(90, 0, 0, 1)
+                glCallList(self.list_floor_side)
+                glPopMatrix()
+        glPopMatrix()
 
     def render_elt(self, x, y): 
         glBindTexture(GL_TEXTURE_2D, self.sprite_elts[self.tile_elts[x][y]])
         z = self.temp_tiles[self.tile[x][y]][3]
-        glBegin(GL_QUADS)
-        glTexCoord2d(0,0); glVertex3d(x, y, z)
-        glTexCoord2d(0,1); glVertex3d(x, y, z+1.41)
-        glTexCoord2d(1,1); glVertex3d(x+1, y+1, z+1.41)
-        glTexCoord2d(1,0); glVertex3d(x+1, y+1, z)
-        glEnd()
-        glBegin(GL_QUADS)
-        glTexCoord2d(0,0); glVertex3d(x, y+1, z)
-        glTexCoord2d(0,1); glVertex3d(x, y+1, z+1.41)
-        glTexCoord2d(1,1); glVertex3d(x+1, y, z+1.41)
-        glTexCoord2d(1,0); glVertex3d(x+1, y, z)
-        glEnd()
+        glPushMatrix()
+        glTranslated(x, y, z)
+        glCallList(self.list_elt)
+        glPopMatrix()
 
     def render_robot(self, r):
         glBindTexture(GL_TEXTURE_2D, self.sprite_robots[r.id_sprite])
         z = self.temp_tiles[self.tile[r.pos_x][r.pos_y]][3] + 0.4
         glPushMatrix()
-        glTranslated(r.pos_x+0.5, r.pos_y+0.5, 0)
+        glTranslated(r.pos_x+0.5, r.pos_y+0.5, z)
         glRotated(90*r.orient, 0, 0, 1)
-        glBegin(GL_QUADS)
-        glTexCoord2d(0,0); glVertex3d(-0.5, 0.5, z)
-        glTexCoord2d(0,1); glVertex3d(-0.5, -0.5, z)
-        glTexCoord2d(1,1); glVertex3d(0.5, -0.5, z)
-        glTexCoord2d(1,0); glVertex3d(0.5, 0.5, z)
-        glEnd()
+        glCallList(self.list_robot)
         glPopMatrix()
 
     def render(self, (select_x, select_y, cam_w, cam_h), (dx, dy)):
 
         z = self.temp_tiles[self.tile[select_x][select_y]][3] + 0.1
         glBindTexture(GL_TEXTURE_2D, self.cursor)
-        glBegin(GL_QUADS)
-        glTexCoord2d(0,0); glVertex3d(-0.5-dx, 0.5-dy, z)
-        glTexCoord2d(0,1); glVertex3d(-0.5-dx, -0.5-dy, z)
-        glTexCoord2d(1,1); glVertex3d(0.5-dx, -0.5-dy, z)
-        glTexCoord2d(1,0); glVertex3d(0.5-dx, 0.5-dy, z)
-        glEnd()
+        glPushMatrix()
+        glTranslated(-dx, -dy, z)
+        glCallList(self.list_robot)
+        glPopMatrix()
         glTranslated(-select_x-0.5-dx, -select_y-0.5-dy, 0);
 
         for y in range(max(select_y-16, 0), min(select_y + 10, self.size)):

File src/worldmap.py

 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ## 
 ## Started on  Thu Jun 28 20:42:21 2012 Pierre Surply
-## Last update Mon Aug 27 10:09:07 2012 Pierre Surply
+## Last update Mon Aug 27 16:06:29 2012 Pierre Surply
 ##
 
 import os
         self.tile = [[0] * self.size for i in range(self.size)]
         self.env = [[None] * self.size for i in range(self.size)]
         
-        
     def render(self, (w, h), (sx, sy)):
         size_biome = min((w/2) / 34, (h/2) / 34)
         size_large_biome = min((w/4)/9, (h/2) / 9)