home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / WalkDemo.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  5.5 KB  |  169 lines

  1. ' WalkDemo
  2. '
  3. ' Written by Tom Mulgrew and Scott Brosious
  4. '
  5. ' A simple wolfenstein like engine
  6.  
  7. const squareSize# = 10      ' Size of each individual square (cube)
  8.  
  9. data 10, 10                 ' Width and height
  10. data 1,1,1,1,1,1,1,1,1,1
  11. data 1,0,0,0,0,1,0,1,0,3
  12. data 1,0,1,0,0,1,0,1,0,3
  13. data 1,0,0,0,0,0,0,0,0,3
  14. data 1,0,0,0,0,0,0,0,0,3
  15. data 1,0,1,0,1,0,0,0,0,3
  16. data 1,0,0,0,0,0,0,1,0,3
  17. data 1,0,1,0,1,0,0,1,0,3
  18. data 1,0,0,0,0,0,0,1,0,3
  19. data 1,1,1,1,1,1,1,1,1,1
  20.  
  21. ' Working variables
  22. dim x, y, i, drawWall, wallType
  23.  
  24. ' Read map size
  25. dim xSize, ySize
  26. read xSize, ySize
  27.  
  28. ' Allocate map
  29. const BASE = 0, NORTH = 1, SOUTH = 2, EAST = 3, WEST = 4
  30. dim map(xSize)(ySize)(4)
  31.  
  32. ' Read it in
  33. for y = 1 to ySize
  34.     for x = 1 to xSize
  35.     
  36.         ' Read in the type, then copy it to ALL the walls.
  37.         read wallType
  38.         for i = 0 to 4
  39.             map(x)(y)(i) = wallType
  40.         next
  41.     next
  42. next
  43.  
  44. ' Add in specific texture overrides here
  45.  
  46. map(3)(3)(NORTH) = 1
  47. map(3)(3)(SOUTH) = 4
  48. map(3)(3)(WEST) = 1
  49. map(3)(3)(EAST) = 1
  50.        
  51. ' Load textures first
  52. dim textures(4) 
  53.  
  54. textures(1) = LoadTexture ("textures\wall01.jpg")   
  55. textures(2) = LoadTexture ("textures\ceil01.jpg")
  56. textures(3) = LoadTexture ("textures\floor01.jpg")
  57. textures(4) = LoadTexture ("data\star.bmp") 
  58.  
  59.  
  60. ' Enable texturing.
  61. ' (Note: We use GL_TEXTURE_2D, because the textures are 2D images.)
  62. glEnable (GL_TEXTURE_2D)
  63.  
  64. dim camX#, camY#, camZ#, camAng#    ' Camera position and direction
  65. camX# = xSize * squareSize# * .5 + 5
  66. camY# = 5
  67. camZ# = ySize * squareSize# * .5
  68.  
  69. while true
  70.     
  71.     ' Draw scene
  72.     glClear (GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT) 
  73.     glLoadIdentity ()
  74.     glRotatef (-camAng#, 0, 1, 0)
  75.     glTranslatef (-camX#, -camY#, -camZ#)
  76.     glScalef (squareSize#, squareSize#, squareSize#)
  77.                                     
  78.     ' Ceiling texture
  79.                 glBindTexture (GL_TEXTURE_2D, textures(2))
  80.                
  81.                 glBegin (GL_QUADS)
  82.                     glTexCoord2f ( 0,         0): glVertex3f (0,      1, 0)
  83.                     glTexCoord2f ( xSize,     0): glVertex3f (xSize,  1, 0)
  84.                     glTexCoord2f ( XSize, ySize): glVertex3f (xSize,  1, ySize)
  85.                     glTexCoord2f ( 0,     ySize): glVertex3f (0,      1, ySize)
  86.                 glEnd ()      
  87.      
  88.      ' Floor texture
  89.                 glBindTexture (GL_TEXTURE_2D, textures(3))
  90.                
  91.                 glBegin (GL_QUADS)
  92.                     glTexCoord2f ( 0,         0): glVertex3f (0,      0, 0)
  93.                     glTexCoord2f ( xSize,     0): glVertex3f (xSize,  0, 0)
  94.                     glTexCoord2f ( XSize, ySize): glVertex3f (xSize,  0, ySize)
  95.                     glTexCoord2f ( 0,     ySize): glVertex3f (0,      0, ySize)
  96.                 glEnd ()                                          
  97.           
  98.     for y = 2 to ySize
  99.         for x = 2 to xSize                  
  100.                          
  101.             ' Look at the cell, and it's left hand neighbour
  102.             ' If one is 0 and the other isn't, we need to draw a wall
  103.             if map(x)(y)(BASE) = 0 and map(x-1)(y)(BASE) <> 0 then
  104.                 drawWall = true
  105.                 wallType = map(x-1)(y)(EAST)
  106.             else
  107.                 if map(x)(y)(BASE) <> 0 and map(x-1)(y)(BASE) = 0 then
  108.                     drawWall = true
  109.                     wallType = map(x)(y)(WEST)
  110.                 else
  111.                     drawWall = false
  112.                 endif
  113.             endif              
  114.             if drawWall then
  115.                
  116.                 glBindTexture (GL_TEXTURE_2D, textures(wallType))
  117.                
  118.                 glBegin (GL_QUADS)
  119.                     glTexCoord2f ( 0, 0): glVertex3f ( x,  0, y)
  120.                     glTexCoord2f ( 1, 0): glVertex3f ( x,  0, y+1)
  121.                     glTexCoord2f ( 1, 1): glVertex3f ( x,  1, y+1)
  122.                     glTexCoord2f ( 0, 1): glVertex3f ( x,  1, y)
  123.                 glEnd ()
  124.              
  125.             endif
  126.             
  127.             ' Compare the cell and it's above neighbour
  128.             ' Same again
  129.             if map(x)(y)(BASE) = 0 and map(x)(y-1)(BASE) <> 0 then
  130.                 drawWall = true
  131.                 wallType = map(x)(y-1)(SOUTH)
  132.             else
  133.                 if map(x)(y)(BASE) <> 0 and map(x)(y-1)(BASE) = 0 then
  134.                     drawWall = true
  135.                     wallType = map(x)(y)(NORTH)
  136.                 else
  137.                     drawWall = false
  138.                 endif
  139.             endif              
  140.             if drawWall then
  141.                 
  142.                 glBindTexture (GL_TEXTURE_2D, textures(wallType))
  143.                             
  144.                 glBegin (GL_QUADS)
  145.                     glTexCoord2f ( 0, 0): glVertex3f ( x,   0, y)
  146.                     glTexCoord2f ( 1, 0): glVertex3f ( x+1, 0, y)
  147.                     glTexCoord2f ( 1, 1): glVertex3f ( x+1, 1, y)
  148.                     glTexCoord2f ( 0, 1): glVertex3f ( x,   1, y)
  149.                 glEnd ()
  150.            
  151.            endif
  152.         next
  153.     next  
  154.     SwapBuffers ()
  155.  
  156.     ' Move camera
  157.     while SyncTimer (10)
  158.         if ScanKeyDown (VK_LEFT)  then camAng# = camAng# + 1: endif
  159.         if ScanKeyDown (VK_RIGHT) then camAng# = camAng# - 1: endif
  160.         if ScanKeyDown (VK_UP)    then
  161.             camX# = camX# - sind (camAng#) * .2
  162.             camZ# = camZ# - cosd (camAng#) * .2
  163.         endif
  164.         if ScanKeyDown (VK_DOWN)  then
  165.             camX# = camX# + sind (camAng#) * .2
  166.             camZ# = camZ# + cosd (camAng#) * .2
  167.         endif
  168.     wend
  169. wend