' this Demo5 uses all _GlMethods without own functions built from scratch
' using _Gl_Triangle_Fan we draw a circle
' method 1 is on fly building
' method 2 is matrix precalculation
Dim lMyScreen
, iMaxScreenX
, iMaxScreenY
, iMinScreenX
, iMinScreenY
' variables for managing screen visible Dim Shared iBallState
, sMyBallX
, sMyBallY
, sMyBallSpeedX
, sMyBallSpeedY
'variables for managing ball Dim lColor
, iRed
, iGreen
, iBlue
, iAlpha
, sRed
, sGreen
, sBlue
, sAlpha
' variables for managing colors Dim iKey
, icircles
(0 To 60, 0 To 1)
' initialization
iMaxScreenX = 799
iMaxScreenY = 599
iMinScreenX = 0
iMinScreenY = 0
lColor
= _RGBA32(255, 0, 0, 255) 'color is REDiBallState = True
iGlInit = True
lMyScreen
= _NewImage(iMaxScreenX
+ 1, iMaxScreenY
+ 1, 32) ' it creates a graphic surface of 800x600 pixels (0-799,0-599)If lMyScreen
< -1 Then Screen lMyScreen
Else Print "Error creating screen" 'it gives feedback if there is a graphic error
_Title "OpenGl 2D demo: step 5 Bouncing Ball and transformation cohordinates: ScaleF method" Locate 2, 1:
Print "press Enter key to quit, Spacebar to change shape of ball"
While 1 ' main loop to manage the flow of application 'area input
If iKey
= 32 Then iBallState
= Not iBallState
'area calculation
If sMyBallX
>= iMaxScreenX
Or sMyBallX
<= iMinScreenX
Then sMyBallSpeedX = -sMyBallSpeedX
sMyBallSpeedX
= sMyBallSpeedX
+ (Rnd * 2) If sMyBallSpeedX
> 12 Then sMyBallSpeedX
= 6 If sMyBallSpeedX
< -12 Then sMyBallSpeedX
= -6 If sMyBallX
< iMinScreenX
Then sMyBallX
= iMinScreenX
+ 1 Else sMyBallX
= iMaxScreenX
- 1 sMyBallX = sMyBallX + sMyBallSpeedX
If sMyBallY
>= iMaxScreenY
Or sMyBallY
<= iMinScreenY
Then sMyBallSpeedY = -sMyBallSpeedY
sMyBallSpeedY
= sMyBallSpeedY
+ (Rnd * 2) If sMyBallSpeedY
> 12 Then sMyBallSpeedY
= 6 If sMyBallSpeedY
< -12 Then sMyBallSpeedY
= -6
If sMyBallY
< iMinScreenY
Then sMyBallY
= iMinScreenY
+ 1 Else sMyBallY
= iMaxScreenY
- 1 sMyBallY = sMyBallY + sMyBallSpeedY
' to see OpenGl results you must wait at least the first second after window of program has been displayed
_Limit 15 ' it is useful to limit the activity of CPU while we use the GPU (openGl)
'OpenGl area-----------------------------
Shared iMaxScreenX
, iMaxScreenY
, iMinScreenX
, iMinScreenY
' variables for managing screen visible Shared iRed
, iGreen
, iBlue
, iAlpha
, icircles
() ' every time _GL runs it must set values for screen
' 2D graphic primitives area-----------------------
If iGlInit
Then GlInit
' only if we need initialization these instructions (in the sub GlInit) will be executed _glClear (_GL_COLOR_BUFFER_BIT
) ' it clears buffer of color
' Red, Green, Blue
_glColor4ub iRed
, iGreen
, iBlue
, iAlpha
' here it defines the color to use for drawing like in _RGB32 with value from 0 to 255
For e
= 1 To (_Pi(2)) Step (2 * _Pi / 10) ' <-- increasing the number we get a more suitable ball
_glVertex2i icircles
(e
, 0) + sMyBallX
, icircles
(e
, 1) + sMyBallY
Shared sRed
, sBlue
, sGreen
, sAlpha
, icircles
(), iKey
Shared iMaxScreenX
, iMaxScreenY
, iMinScreenX
, iMinScreenY
_glViewport iMinScreenX
, iMinScreenY
, iMaxScreenX
+ 1, iMaxScreenY
+ 1 _glClearColor 0, 0, 0, 1 ' it set color for CLS,color and color is defined in RGBA mode 0-1
sMyBallX = 400
sMyBallY = 300
sMyBallSpeedX = 5
sMyBallSpeedY = 5
' precalculation matrix values for circle
' we use temporary iKey, sRed, SGreen
sRed = 4.1445 * 2.0 / 60.0
sGreen = 0.0
icircles
(iKey
, 0) = Cos(sGreen
) * 7 icircles
(iKey
, 1) = Sin(sGreen
) * 7 sGreen = sGreen + sRed
sRed = 0: sGreen = 0: iKey = 0 ' we restore these variable to 0
iGlInit = False