Option _Explicit
DefInt I
DefSng S
DefLng L
Const True = -1, False = Not True
Randomize Timer
Dim Shared iGlInit
Dim lMyScreen, iMaxScreenX, iMaxScreenY, iMinScreenX, iMinScreenY ' variables for managing screen visible
Dim sMyBallX, sMyBallY, sMyBallSpeedX, sMyBallSpeedY 'variables for managing ball
Dim iRed, iGreen, iBlue, iAlpha, sRed, sGreen, sBlue, sAlpha ' variables for managing colors
Dim iKey, iBallState

iMaxScreenX = 799
iMaxScreenY = 599
iMinScreenX = 0
iMinScreenY = 0
iGlInit = True
iBallState = False
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 4  Draw bouncing ball: WINDOW method"
Locate 2, 1: Print "press spacebar to switch ball shape, Enter to quit"


While 1 ' main loop to manage the flow of application
    ' area INPUT manager
    iKey = _KeyHit
    If iKey = 13 Then End
    If iKey = 32 Then iBallState = Not iBallState

    'area calculations
    If sMyBallX >= 1 Or sMyBallX <= -1 Then
        sMyBallSpeedX = -(sMyBallSpeedX + (Rnd * (1 / 10)))
        If sMyBallSpeedX > .2 Then sMyBallSpeedX = .1
        If sMyBallSpeedX < -.2 Then sMyBallSpeedX = -.1
        If sMyBallX < -1 Then sMyBallX = -.99 Else sMyBallX = .99
    End If
    sMyBallX = sMyBallX + sMyBallSpeedX

    If sMyBallY >= 1 Or sMyBallY <= -1 Then
        sMyBallSpeedY = -(sMyBallSpeedY + (Rnd * (1 / 10)))
        If sMyBallSpeedY > .2 Then sMyBallSpeedY = .1
        If sMyBallSpeedY < -.2 Then sMyBallSpeedY = -.1

        If sMyBallY < -1 Then sMyBallY = -.99 Else sMyBallY = .99
    End If
    sMyBallY = sMyBallY + sMyBallSpeedY

    ' to see OpenGl results you must wait at least the first second after window of program has been displayed
    _Limit 10 ' it is useful to limit the activity of CPU while we use the GPU (openGl)
Wend
System

'OpenGl area-----------------------------
Sub _GL ()
    Shared iBallState, sMyBallX, sMyBallY, sMyBallSpeedX, sMyBallSpeedY 'variables for managing ball
    ' 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

    If iBallState Then
        '          Red, Green, Blue
        _glColor3f 1.0, 0.0, 0.0 ' here it defines the color to use for drawing like in _RGB32 with value from 0.0 to 1.0
        _glBegin _GL_QUADS 'it starts a graphic primitive
        ' _glVertex2f passes 2 float digits (x,y) to _GL_LINES_loop for drawing a point with specified color
        _glVertex2f sMyBallX - .01, sMyBallY - .01 'Bottom Left point
        _glVertex2f sMyBallX - .01, sMyBallY + .01 ' Top Left point
        _glVertex2f sMyBallX + .01, sMyBallY + .01 'Top Right point
        _glVertex2f sMyBallX + .01, sMyBallY - .01 'Bottom Right point
        _glEnd

    Else
        _glPointSize 12
        _glColor3f Proportion!(0, 255, 0, 1, 127), Proportion!(0, 255, 0, 1, 10), Proportion!(0, 255, 0, 1, 227)
        _glBegin _GL_POINTS
        _glVertex2f sMyBallX, sMyBallY
        _glEnd
    End If
End Sub

Sub GlInit ()
    Shared iMaxScreenX, iMaxScreenY, iMinScreenX, iMinScreenY
    Shared sMyBallX, sMyBallY, sMyBallSpeedX, sMyBallSpeedY 'variables for managing ball
    _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
    _GLRender _Behind
    sMyBallX = 0.0 ' the ball starts at centre of screen
    sMyBallY = 0.0
    sMyBallSpeedX = 0.01
    sMyBallSpeedY = 0.01
    iGlInit = False
End Sub

Function Proportion! (min1 As Integer, max1 As Integer, min2 As Integer, max2 As Integer, Value As Single)
    Proportion! = ((Abs(max2 - min2) * Value) / Abs(max1 - min1)) + min2
End Function
