'
' How to create a program using PROTO and link with external library
'
' Examples of linking:
' ===================
'./bacon.bash -lGL -lglut proto.bac
'./bacon.ksh -lGL -lglut proto.bac
'./bacon -lGL -lglut proto.bac
' ===================
'
' July 2010, PvE - GPL v3
' Adapted at April 2014 - PvE
'---------------------------------------------------------------------------------------------

' Add linker flags
PRAGMA LDFLAGS GL glut

' We must define these, look into gl.h and glut.h
CONST GL_AMBIENT_AND_DIFFUSE = 0x1602
CONST GL_COLOR_BUFFER_BIT = 0x00004000
CONST GL_DEPTH_BUFFER_BIT = 0x00000100
CONST GL_DEPTH_TEST = 0x0B71
CONST GL_FRONT = 0x0404
CONST GL_LIGHT0 = 0x4000
CONST GL_LIGHTING = 0x0B50
CONST GL_MODELVIEW = 0x1700
CONST GL_POSITION = 0x1203
CONST GL_SMOOTH = 0x1D01
CONST GLUT_DOUBLE = 0x0002
CONST GLUT_RGB = 0x0000

' Make sure BaCon recognizes these commands
PROTO glClear, glClearColor, glEnable, glLightfv, glLoadIdentity, glMatrixMode
PROTO glMaterialfv, glPopMatrix, glPushMatrix, glRotated, glShadeModel, glutCreateWindow
PROTO glutDisplayFunc, glutIdleFunc, glutInit, glutInitDisplayMode, glutInitWindowSize
PROTO glutMainLoop, glutPostRedisplay, glutSolidTeapot, glutSwapBuffers

' Some colors
DECLARE lightpos[] = { 2.0, 2.0, -8.0, 0.0 } TYPE float
DECLARE lightamb[] = { 0.15, 0.15, 0.15 } TYPE float
DECLARE teapot_color[] = { 0.1, 0.7, 0.1, 0.5 } TYPE float

' Current position of teapot
DECLARE xpos, ypos TYPE FLOATING

' Draw the teapot
SUB display

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glShadeModel(GL_SMOOTH)
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glLightfv(GL_LIGHT0, GL_POSITION, lightpos)
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, teapot_color)
    glMatrixMode(GL_MODELVIEW)
    glPushMatrix
    glRotated(xpos, 1.0, 0.0, 0.0)
    glRotated(ypos, 0.0, 1.0, 0.0)
    glutSolidTeapot(0.5)
    glLoadIdentity
    glPopMatrix
    glutSwapBuffers

END SUB

' Idle loop
SUB idle

    INCR xpos, 0.5
    INCR ypos, 1.0
    glutPostRedisplay

END SUB

' Main starts here
argc = 0
argv$ = ""

glutInit(ADDRESS(argc), ADDRESS(argv$))
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize(500, 400)
glutCreateWindow("BaCon using PROTO")
glClearColor(0.0, 0.0, 0.0, 1.0)
glutDisplayFunc(ADDRESS(display))
glutIdleFunc(ADDRESS(idle))
glutMainLoop