Author Topic: Rotate (OpenGL version)  (Read 3592 times)

0 Members and 1 Guest are viewing this topic.

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Rotate (OpenGL version)
« on: May 07, 2020, 03:19:30 pm »
Hi Folks,

Here's a small demo of a game i've had on the back burner for the last 8 years or so! I was'nt happy with the results i was getting from using _maptriangle to rotate the image (it worked but was way to grainy), so i decided to make an GL version instead.

Using the arrow keys to rotate the grid in either direction, try to get the ball on top of the target square (the one with the circle on it), do this whilst rying to avoid being squished by the blocks.

Code: QB64: [Select]
  1. '// Rotate GL v.02 \\
  2. '// By John Onyon a.k.a Unseen Machine \\
  3.  
  4. CHDIR "Unseen Demos\Rotate\"
  5. '/////////////////////////////////////////////////////////////////////////////////
  6. REM $INCLUDE:'GDK_GL\GDK_GL.bi'
  7. '/////////////////////////////////////////////////////////////////////////////////
  8.  
  9. DIM SHARED Mouse(1) AS MouseState, Kb(1) AS KeyBoardState '// For Input capture
  10. DIM SHARED CamVec AS VECTOR_GL, RadHelp#, Init_GL, Allow_GL AS _BYTE '// For Camera, Movement and GL initialisation
  11. DIM SHARED LevelBlock AS LONG, Level(11, 11) AS _BYTE, BallTexture AS LONG, TargetTexture AS LONG, Enemyblock AS LONG
  12. DIM SHARED LevelPar AS _BYTE, CurrentLevel AS INTEGER, LevelMax AS _BYTE
  13. DIM SHARED Ball_IsFalling AS _BYTE, DropTimer#, LvlBckUp(11, 11) AS _BYTE
  14. DIM SHARED IsRotating AS _BYTE, Rotation AS INTEGER, Left AS _BYTE
  15.  
  16. '// Initial Setup \\
  17. SCREEN _NEWIMAGE(800, 600, 32) '// Create a 32 bit screen
  18. _DISPLAY '// Turn off auto display
  19. _DISPLAYORDER _GLRENDER , _SOFTWARE '// Set the display to render GL stuff then Software stuff
  20. _FPS 30 '    // Set maximium frames per second
  21.  
  22. '// Allow SUB _GL and initialise it \\
  23. Init_GL = True
  24. Allow_GL = True
  25. DropTimer# = TIMER(.001)
  26.  
  27.  
  28.  _LIMIT 30
  29.  
  30.  GT# = TIMER(.001)
  31.  
  32.  IF NOT IsRotating THEN
  33.  
  34.   GDK_Keyboard_GetState Kb(0)
  35.  
  36.   IF Kb(0).Left AND NOT Kb(1).Left THEN
  37.    Left = True
  38.    IsRotating = True
  39.   ELSEIF Kb(0).Right AND NOT Kb(1).Right THEN
  40.    Left = False
  41.    IsRotating = True
  42.   END IF
  43.  
  44.  
  45.   IF Left = True THEN
  46.    IF Rotation > -90 THEN
  47.     Rotation = Rotation - 2
  48.    ELSE
  49.     Rotation = 0
  50.     IsRotating = False
  51.     RotateArray
  52.    END IF
  53.   ELSEIF Left = False THEN
  54.    IF Rotation < 90 THEN
  55.     Rotation = Rotation + 2
  56.    ELSE
  57.     Rotation = 0
  58.     IsRotating = False
  59.     RotateArray
  60.     RotateArray
  61.     RotateArray
  62.    END IF
  63.   END IF
  64.  
  65.  IF GT# - DropTimer# >= .05 THEN
  66.   FOR i% = 1 TO 10
  67.    FOR j% = 10 TO 1 STEP -1
  68.  
  69.     IF Level(i%, j%) = 3 THEN '// Player ball
  70.      IF Level(i%, j% + 1) = 0 THEN
  71.       Level(i%, j%) = 0
  72.       Level(i%, j% + 1) = 3
  73.       Ball_IsFalling = True
  74.      ELSEIF Level(i%, j% + 1) = 2 THEN '// Hit target square
  75.       IF CurrentLevel < LevelMax THEN
  76.        LoadLevel
  77.        CurrentLevel = CurrentLevel + 1
  78.        Ball_IsFalling = False
  79.        GOTO SkipLoops
  80.       ELSE
  81.        SYSTEM
  82.       END IF
  83.      END IF
  84.     ELSEIF Level(i%, j%) = 4 THEN
  85.      IF Level(i%, j% + 1) = 0 THEN
  86.       Level(i%, j%) = 0
  87.       Level(i%, j% + 1) = 4
  88.      ELSE
  89.       IF Level(i%, j% + 1) = 3 THEN '// gonna crush the ball!!!!
  90.  
  91.        '// Do a loose a life thingy
  92.        '// Restart the level
  93.        FOR i2% = 0 TO 11
  94.         FOR j2% = 0 TO 11
  95.          Level(i2%, j2%) = LvlBckUp(i2%, j2%)
  96.         NEXT
  97.        NEXT
  98.        Ball_IsFalling = False
  99.        GOTO SkipLoops
  100.       END IF
  101.  
  102.      END IF
  103.     END IF
  104.  
  105.    NEXT
  106.   NEXT
  107.   SkipLoops:
  108.   DropTimer# = GT#
  109.  
  110.  
  111.  Kb(1) = Kb(0)
  112.  
  113.  
  114.  
  115. '###############################################################################################################################
  116.  
  117. LvlData:
  118. DATA 0,0,0,0,0,0,0,0,1,1,1,1
  119. DATA 0,1,1,1,1,1,1,1,1,0,0,1
  120. DATA 0,1,0,0,0,0,1,0,0,0,0,1
  121. DATA 0,1,0,0,0,0,0,0,0,0,1,1
  122. DATA 0,1,1,0,1,0,1,1,0,1,1,0
  123. DATA 0,0,1,0,1,2,1,1,0,1,0,0
  124. DATA 0,0,1,0,1,1,1,1,0,1,0,0
  125. DATA 0,0,1,0,1,0,0,1,3,1,0,0
  126. DATA 0,0,1,4,1,0,0,1,1,1,0,0
  127. DATA 0,0,1,1,1,0,0,0,0,0,0,0
  128. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  129. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  130.  
  131. DATA 0,0,1,1,1,1,1,0,0,0,0,0
  132. DATA 0,0,1,3,0,0,1,0,0,0,0,0
  133. DATA 0,0,1,1,1,0,1,0,0,0,0,0
  134. DATA 0,0,0,0,1,0,1,0,0,0,0,0
  135. DATA 0,0,0,0,1,0,1,1,1,1,0,0
  136. DATA 1,1,1,1,1,0,0,0,0,1,0,0
  137. DATA 1,0,0,0,0,0,0,1,0,1,0,0
  138. DATA 1,0,1,1,1,1,1,1,0,1,0,0
  139. DATA 1,0,1,1,0,0,2,0,0,1,0,0
  140. DATA 1,0,4,0,0,1,1,1,1,1,0,0
  141. DATA 1,1,1,1,1,1,0,0,0,0,0,0
  142. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  143.  
  144. DATA 1,1,1,1,1,1,1,1,1,1,1,1
  145. DATA 1,0,2,0,1,0,0,0,0,0,0,1
  146. DATA 1,0,1,0,1,0,1,0,1,0,0,1
  147. DATA 1,0,1,0,1,0,1,0,1,1,0,1
  148. DATA 1,0,1,0,0,0,1,0,0,1,0,1
  149. DATA 1,0,1,1,1,1,1,1,0,1,0,1
  150. DATA 1,0,1,0,0,3,0,0,0,1,0,1
  151. DATA 1,0,1,0,1,1,1,1,1,1,0,1
  152. DATA 1,0,1,0,0,0,0,0,0,0,0,1
  153. DATA 1,0,1,1,1,1,1,1,1,1,0,1
  154. DATA 1,4,0,0,0,0,0,0,0,0,0,1
  155. DATA 1,1,1,1,1,1,1,1,1,1,1,1
  156.  
  157. DATA 1,1,1,1,1,1,1,0,0,0,0,0
  158. DATA 1,0,0,0,0,0,0,1,0,0,0,0
  159. DATA 1,0,1,1,1,1,0,0,1,0,0,0
  160. DATA 1,0,0,0,0,0,0,0,0,1,1,0
  161. DATA 1,0,0,0,1,0,3,1,0,0,0,1
  162. DATA 0,1,0,0,1,1,1,1,0,0,0,1
  163. DATA 0,1,0,0,1,0,0,0,0,0,0,1
  164. DATA 0,1,0,1,1,0,1,1,1,1,1,0
  165. DATA 1,0,0,0,4,0,1,0,0,0,0,0
  166. DATA 1,0,1,1,1,1,0,0,0,0,0,0
  167. DATA 1,0,2,1,0,0,0,0,0,0,0,0
  168. DATA 1,1,1,1,0,0,0,0,0,0,0,0
  169.  
  170. DATA 1,1,1,1,1,1,1,1,1,0,0,0
  171. DATA 1,3,0,0,1,0,0,0,1,0,0,0
  172. DATA 1,1,1,0,0,0,1,0,1,0,0,0
  173. DATA 0,0,1,1,1,0,1,0,1,0,0,0
  174. DATA 0,0,1,0,0,0,1,0,0,1,1,1
  175. DATA 0,0,1,0,1,1,1,1,0,0,0,1
  176. DATA 0,0,1,0,0,0,0,0,0,1,0,1
  177. DATA 0,0,1,0,1,1,1,1,1,1,2,1
  178. DATA 0,0,1,4,0,0,0,0,0,1,0,1
  179. DATA 0,0,1,1,1,1,1,1,0,1,0,1
  180. DATA 0,0,0,0,0,0,0,1,0,0,0,1
  181. DATA 0,0,0,0,0,0,0,1,1,1,1,1
  182.  
  183. DATA 1,1,1,1,1,0,1,1,1,1,0,0
  184. DATA 1,0,0,4,1,0,1,0,0,3,0,0
  185. DATA 1,0,1,1,1,0,1,0,1,1,0,0
  186. DATA 1,0,0,0,0,0,1,0,1,1,1,1
  187. DATA 1,0,1,1,1,1,1,0,0,0,0,1
  188. DATA 1,0,0,0,0,0,0,0,0,0,0,1
  189. DATA 1,0,0,0,0,0,0,0,0,0,0,1
  190. DATA 1,1,1,0,0,0,0,0,0,1,0,1
  191. DATA 0,1,2,1,1,1,1,1,1,1,0,1
  192. DATA 0,1,0,0,0,0,0,0,0,0,0,1
  193. DATA 0,1,1,1,1,1,1,1,1,1,1,1
  194. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  195.  
  196. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  197. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  198. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  199. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  200. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  201. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  202. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  203. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  204. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  205. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  206. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  207. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  208.  
  209. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  210. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  211. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  212. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  213. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  214. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  215. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  216. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  217. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  218. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  219. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  220. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  221.  
  222.  
  223.  
  224.  
  225. '###################################################################################################################################
  226.  
  227. SUB _GL
  228.  
  229.  IF Allow_GL THEN
  230.  
  231.   _glClearColor 0, 0, 0, 1 '// Set background (clear) color to black
  232.   _glClearDepth 1 '                 // Set clear depth value to farthest
  233.   _glEnable _GL_DEPTH_TEST '         // Enables depth buffer for hidden surface removal
  234.   _glEnable _GL_TEXTURE_2D
  235.  
  236.   '// Setup a perspective view
  237.   _glMatrixMode _GL_PROJECTION
  238.   _gluPerspective 80, 800 / 600, 1, 400 '// Field of view, Aspect ratio, near depth, far depth
  239.   _glMatrixMode _GL_MODELVIEW
  240.  
  241.   IF Init_GL = True THEN
  242.  
  243.    '// Load models, terrain and other bits here
  244.    LevelBlock = GDK_GL_LOAD_TEXTURE("Block.PNG")
  245.    BallTexture = GDK_GL_LOAD_TEXTURE("Ball.PNG")
  246.    TargetTexture = GDK_GL_LOAD_TEXTURE("Target.PNG")
  247.    Enemyblock = GDK_GL_LOAD_TEXTURE("YellowBlock.png")
  248.  
  249.    '// Load level data
  250.    RESTORE LvlData:
  251.    LoadLevel
  252.    CurrentLevel = 1
  253.    LevelMax = 6
  254.  
  255.    '// Switch to rendering mode
  256.    Init_GL = False
  257.  
  258.   ELSE
  259.  
  260.    x% = -60
  261.    y% = 60
  262.    z% = -100
  263.    '// Clear the depth buffer and reset the view matrix (effectively CLS)
  264.    GDK_GL_CLS
  265.  
  266.    _glRotatef Rotation, 0, 0, 1
  267.  
  268.    EnemyCount = 0
  269.    FOR j% = 0 TO 11 'Row
  270.     x% = -60
  271.     FOR i% = 0 TO 11 'Column
  272.  
  273.      SELECT CASE Level(i%, j%)
  274.       CASE 1 '// Wall
  275.        GDK_GL_SET_TEXTURE LevelBlock
  276.        GDK_GL_CUBE x%, y%, z%, 10
  277.  
  278.       CASE 2 '// Target
  279.        GDK_GL_SET_TEXTURE TargetTexture
  280.        GDK_GL_CUBE x%, y%, z%, 10
  281.  
  282.       CASE 3 '// Player
  283.        GDK_GL_SET_TEXTURE BallTexture
  284.        _glPushMatrix
  285.        _glEnable GL_LIGHTING
  286.        _glEnable GL_LIGHT0
  287.        _glTranslatef x% + 5, y% - 5, -95
  288.        GDK_GL_SPHERE 5, 16, 16, -1
  289.        _glDisable GL_LIGHTING
  290.        _glPopMatrix
  291.  
  292.       CASE 4 '// Enemy
  293.        GDK_GL_SET_TEXTURE Enemyblock
  294.        GDK_GL_CUBE x%, y%, z%, 10
  295.  
  296.      END SELECT
  297.  
  298.      x% = x% + 10
  299.     NEXT
  300.     y% = y% - 10
  301.    NEXT
  302.   END IF
  303.  
  304.  
  305. '###################################################################################################################################
  306.  
  307. '// library inclusions
  308. REM $INCLUDE:'GDK_GL\GDK_GL.BM'
  309. REM $INCLUDE:'UnseenGDK.bm'
  310.  
  311. '###################################################################################################################################
  312.  
  313. SUB RotateArray
  314.  DIM CopyArray(11, 11) AS INTEGER
  315.  FOR Y% = 0 TO 11
  316.   FOR X% = 0 TO 11
  317.    CopyArray(X%, Y%) = Level(X%, Y%)
  318.   NEXT
  319.  FOR Y% = 0 TO 11
  320.   FOR X% = 0 TO 11
  321.    Level(11 - Y%, X%) = CopyArray(X%, Y%)
  322.   NEXT
  323.  
  324. '###################################################################################################################################
  325.  
  326. SUB LoadLevel
  327.  FOR j% = 0 TO 11
  328.   FOR i% = 0 TO 11
  329.    READ LevelBit%
  330.    IF LevelBit% = 9 THEN SYSTEM
  331.    Level(i%, j%) = LevelBit%
  332.    LvlBckUp(i%, j%) = LevelBit%
  333.   NEXT
  334.  
  335. '###################################################################################################################################
  336.  
  337.  

Extract the files to your Qb64 directory.

Enjoy and happy coding,

Unseen
* RotateGL.zip (Filesize: 22.61 KB, Downloads: 225)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Rotate (OpenGL version)
« Reply #1 on: August 22, 2020, 12:49:14 pm »
I get error message of missing library on line 9

Never mind, I don't want to put more stuff in QB folder. It's hell when QB64 gets updated.
« Last Edit: August 22, 2020, 01:00:21 pm by bplus »