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 lColor, iRed, iGreen, iBlue, iAlpha, sRed, sGreen, sBlue, sAlpha ' variables for managing colors

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)
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 3  LineLoop and transformation cohordinates: ScaleF method"
Locate 2, 1: Print "press a key to quit"
_FullScreen

While InKey$ = "" ' main loop to manage the flow of application
    ' to see OpenGl results you must wait at least the first second after window of program has been displayed
    _Limit 5 ' 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-----------------------
    ' 1 defining color
    ' 2 _glBEGIN primitives name
    ' 3 _glvertex2f needed from graphic primitives
    ' 4 _glEND

    ' graphic primitives available
    'GL_POINTS   Draws points on screen. Every vertex specified is a point.
    'GL_LINES    Draws lines on screen. Every two vertices specified compose a line.
    'GL_LINE_STRIP   Draws connected lines on screen. Every vertex specified after first two are connected.
    'GL_LINE_LOOP    Draws connected lines on screen. The last vertex specified is connected to first vertex.
    'GL_TRIANGLES    Draws triangles on screen. Every three vertices specified compose a triangle.
    'GL_TRIANGLE_STRIP   Draws connected triangles on screen. Every vertex specified after first three vertices creates a triangle.
    'GL_TRIANGLE_FAN Draws connected triangles like GL_TRIANGLE_STRIP, except draws triangles in fan shape.
    'GL_QUADS    Draws quadrilaterals (4 – sided shapes) on screen. Every four vertices specified compose a quadrilateral.
    'GL_QUAD_STRIP   Draws connected quadrilaterals on screen. Every two vertices specified after first four compose a connected quadrilateral.
    'GL_POLYGON  Draws a polygon on screen. Polygon can be composed of as many sides as you want.
    'https://en.wikibooks.org/wiki/OpenGL_Programming/GLStart/Tut3

    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
    _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_LINE_LOOP 'it starts a graphic primitive
    ' _glVertex2f passes 2 float digits (x,y) to _GL_LINES_loop for drawing a point with specified color
    _glVertex2f iMinScreenX, (iMinScreenY + 1) 'Bottom Left point
    _glVertex2f iMinScreenX, (iMaxScreenY - 1) ' Top Left point
    _glVertex2f iMaxScreenX, iMaxScreenY 'Top Right point
    _glVertex2f iMaxScreenX, iMinScreenY + 1 'Bottom Right point
    _glEnd

    _glColor3f 0.0, 1.0, 0.0
    _glBegin _GL_LINES
    _glVertex2f iMaxScreenX / 2, iMaxScreenY / 3 ' Top  1/3 Left point
    _glVertex2f iMaxScreenX / 4, iMinScreenY + 100 'Bottom Right point
    _glEnd ' it closes the instructions for the above graphic primitive invoked

    _glColor3f 1.0, 0.0, 0.0
    _glBegin _GL_LINES
    _glVertex2f iMaxScreenX / 4, iMaxScreenY / 4 'Bottom Left point
    _glVertex2f iMaxScreenX * (3 / 4), iMaxScreenY * (3 / 4) 'Top Rightpoint
    _glEnd

    _glPointSize 4
    _glColor3f Proportion!(0, 255, 0, 1, 127), Proportion!(0, 255, 0, 1, 10), Proportion!(0, 255, 0, 1, 227)
    _glBegin _GL_POINTS
    _glVertex2f 400, 300
    _glEnd
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)
    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
