Option _Explicit
DefInt I
DefSng S
DefLng L
Const True = -1, False = Not True

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

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

'          STANDARD SYSTEM COHORDINATES
' 0,0      TopLeft-------------TopRight    799,0
'             |                   |
'             |                   |
'             |                   |
'             |                   |
' 0, 599  BottomLeft------------BottomRight     799, 599

'                     OPENGL SYSTEM COHORDINATES
' -1,1            TopLeft-------------TopRight    1,1
'                     |                   |
'                     |                   |
'                     |                   |
'                     |                   |
' -1, -1         BottomLeft------------BottomRight   -1,1

_Title "OpenGl 2D demo: step 1 setting the OpenGl system"
Print "press a key to quit"
Sleep ' to see OpenGl results you must wait at least the first second after window of program has been displayed
System

'OpenGl area-----------------------------
Sub _GL ()
    0 'this sub runs about 60 times for second
    1 ' here you must put all OpenGl statements
    2 ' here you can put standard QB64/QB statements
    3 ' putting here statements that change or stop the flow of program is a BAD PRACTICE, deprecated!
    4 ' it is useful to have an initialization routine called only when we need it

    If iGlInit Then GlInit ' only if we need initialization these instructions (in the sub GlInit) will be executed

    _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
    _glPointSize 8 'it defines the dimension in pixel of the single point drawn

    _glBegin _GL_POINTS 'it starts a graphic primitive, in this case the primitive is a point of size of 8 pixels

    ' _glVertex2f passes 2 float digits (x,y) to _GL_POINTS for drawing a point with specified color
    _glVertex2f -1, -1 'Bottom Left point
    _glVertex2f 1, -1 'Top Left point
    _glVertex2f -1, 1 ' Bottom Right point
    _glVertex2f 1, 1 'Top Right point

    _glEnd ' it closes the instructions for the above graphic primitive invoked

End Sub

Sub GlInit ()
    Shared iMaxScreenX, iMaxScreenY, iMinScreenX, iMinScreenY

    _glViewport iMinScreenX, iMinScreenY, iMaxScreenX + 1, iMaxScreenY + 1 'it sets the viewport for whole screen of window of program
    iGlInit = False

End Sub
