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 Shared iBallState, sMyBallX, sMyBallY, sMyBallSpeedX, sMyBallSpeedY 'variables for managing ball
Dim iRed, iGreen, iBlue, iAlpha, sRed, sGreen, sBlue, sAlpha ' variables for managing colors
Dim ikey
iMaxScreenX = 799
iMaxScreenY = 599
iMinScreenX = 0
iMinScreenY = 0
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 4  Bouncing ball and transformation cohordinates"
Locate 2, 1: Print "press Enter key to quit, SpaceBar key to change shape of ball"
_FullScreen

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
    ' here to avoid massive translations from SCREEN cohordinates
    ' and Opengl Viewport cohordinate we calculates values using
    ' directly the Opengl values for cohordinates
    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 15 ' it is useful to limit the activity of CPU while we use the GPU (openGl)
Wend
System

'OpenGl area-----------------------------
Sub _GL ()

    ' 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
    ' using the default colorclean

    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 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 ' it closes the instructions for the above graphic primitive invoked
    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
    _glViewport iMinScreenX, iMinScreenY, iMaxScreenX + 1, iMaxScreenY + 1
    _glClearColor 0, 0, .4, 1 ' it sets color _glclear() that is like CLS,color
    'and color is defined in RGBA mode with range 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
