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 lColor, iRed, iGreen, iBlue, iAlpha, sRed, sGreen, sBlue, sAlpha ' variables for managing colors
Dim iKey
iMaxScreenX = 799
iMaxScreenY = 599
iMinScreenX = 0
iMinScreenY = 0
lColor = _RGBA32(255, 0, 0, 255) 'color is RED
iRed = _Red32(lColor)
iBlue = _Blue32(lColor)
iGreen = _Green32(lColor)
iAlpha = _Alpha32(lColor)
iBallState = 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 4  Bouncing 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
    iKey = _KeyHit
    If iKey = 13 Then Exit While
    If iKey = 32 Then iBallState = Not iBallState
    'area calculation
    If sMyBallX >= iMaxScreenX Or sMyBallX <= iMinScreenX Then
        sMyBallSpeedX = -(sMyBallSpeedX + (Rnd * 10))
        If sMyBallSpeedX > 12 Then sMyBallSpeedX = 6
        If sMyBallSpeedX < -12 Then sMyBallSpeedX = -6
        If sMyBallX < iMinScreenX Then sMyBallX = iMinScreenX + 1 Else sMyBallX = iMaxScreenX - 1
    End If
    sMyBallX = sMyBallX + sMyBallSpeedX

    If sMyBallY >= iMaxScreenY Or sMyBallY <= iMinScreenY Then
        sMyBallSpeedY = -(sMyBallSpeedY + (Rnd * 10))
        If sMyBallSpeedY > 12 Then sMyBallSpeedY = 6
        If sMyBallSpeedY < -12 Then sMyBallSpeedY = -6

        If sMyBallY < iMinScreenY Then sMyBallY = iMinScreenY + 1 Else sMyBallY = iMaxScreenY - 1
    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 ()
    Shared iMaxScreenX, iMaxScreenY, iMinScreenX, iMinScreenY ' variables for managing screen visible
    Shared sRed, sGreen, sBlue, sAlpha
    ' every time _GL runs it must set values for screen
    _glMatrixMode _GL_MODELVIEW
    _glTranslatef -1, 1, 0
    _glScalef (1 / 400), (-1 / 300), 1

    ' 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
        _glColor4f sRed, sGreen, sBlue, sAlpha ' 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 - 5, sMyBallY - 5 'Bottom Left point
        _glVertex2f sMyBallX - 5, sMyBallY + 5 ' Top Left point
        _glVertex2f sMyBallX + 5, sMyBallY + 5 'Top Right point
        _glVertex2f sMyBallX + 5, sMyBallY - 5 'Bottom Right point
        _glEnd
    Else
        _glPointSize 14
        _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 sRed, sBlue, sGreen, sAlpha, iRed, iBlue, iGreen, iAlpha
    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
    _GLRender _Behind

    sRed = Proportion(0, 255, 0, 1, iRed)
    sBlue = Proportion(0, 255, 0, 1, iBlue)
    sGreen = Proportion(0, 255, 0, 1, iGreen)
    sAlpha = Proportion(0, 255, 0, 1, iAlpha)

    sMyBallX = 400
    sMyBallY = 300
    sMyBallSpeedX = 5
    sMyBallSpeedY = 5
    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
