#define DEBUG ' enable dprint(string) macro
#include once "inc/wx.bi"
#include once "GL/gl.bi"
#include once "GL/glu.bi"

const FRAME_WIDTH  = 640
const FRAME_HEIGHT = 480
const FRAME_ID     = 1
const TIMER_ID     = 2
const FRAME_STYLE  = wxSYSTEM_MENU _
                  or wxMINIMIZE_BOX _
                  or wxCAPTION _
                  or wxCLIP_CHILDREN _
                  or wxCLOSE_BOX
                 
dim shared as _wxApp      ptr App
dim shared as _wxFrame    ptr Frame
Dim Shared As _wxTimer    Ptr Ticker
dim shared as _wxGLCanvas ptr GLCanvas

Sub EventListener APICALL (event As _wxEvent Ptr, iListener as wxInt)
  static as wxInt  nTicks = 0
  select case wxEvent_GetEventType(event)
  case wxEvent_EVT_CLOSE_WINDOW()
    dprint("wxTimer_Stop()")
    wxTimer_Stop(Ticker)
    dprint("wxWindow_Destroy()")
    wxWindow_Destroy(Frame)
  case wxEvent_EVT_TIMER()
    ' is it the first frame ?
    if (nTicks=0) then
      dprint("TimerEvent() create GLCanvas")
      GLCanvas = SimpleGLCanvas(Frame,,FRAME_WIDTH,FRAME_HEIGHT)
      wxGLCanvas_SetCurent(GLCanvas)
      glViewport(0, 0, FRAME_WIDTH,FRAME_HEIGHT)
      glMatrixMode(GL_PROJECTION)
      glLoadIdentity()
      gluPerspective(60,FRAME_WIDTH/FRAME_HEIGHT,0.1,1000.0)
      glMatrixMode(GL_MODELVIEW)
      glEnable(GL_DEPTH_TEST)
      glEnable(GL_LIGHTING)
      glEnable(GL_LIGHT0)
      glClearColor(0.0,0.5,0.5,1.0)
    end if
    nticks+=1
    glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    gluLookAt(2,2,-1, 0,0,0, 0,1,0)
    
    glRotatef(nTicks mod 360,1,1,1)
    
    glBegin(GL_QUADS)
      glNormal3f( 0.0, 0.0, 1.0)
      glVertex3f( 0.5, 0.5, 0.5): glVertex3f(-0.5, 0.5, 0.5)
      glVertex3f(-0.5,-0.5, 0.5): glVertex3f( 0.5,-0.5, 0.5)

      glNormal3f( 0.0, 0.0,-1.0)
      glVertex3f(-0.5,-0.5,-0.5): glVertex3f(-0.5, 0.5,-0.5)
      glVertex3f( 0.5, 0.5,-0.5): glVertex3f( 0.5,-0.5,-0.5)

      glNormal3f( 0.0, 1.0, 0.0)
      glVertex3f( 0.5, 0.5, 0.5): glVertex3f( 0.5, 0.5,-0.5)
      glVertex3f(-0.5, 0.5,-0.5): glVertex3f(-0.5, 0.5, 0.5)

      glNormal3f( 0.0,-1.0, 0.0)
      glVertex3f(-0.5,-0.5,-0.5): glVertex3f( 0.5,-0.5,-0.5)
      glVertex3f( 0.5,-0.5, 0.5): glVertex3f(-0.5,-0.5, 0.5)

      glNormal3f( 1.0, 0.0, 0.0)
      glVertex3f( 0.5, 0.5, 0.5): glVertex3f( 0.5,-0.5, 0.5)
      glVertex3f( 0.5,-0.5,-0.5): glVertex3f( 0.5, 0.5,-0.5)

      glNormal3f(-1.0, 0.0, 0.0)
      glVertex3f(-0.5,-0.5,-0.5): glVertex3f(-0.5,-0.5, 0.5)
      glVertex3f(-0.5, 0.5, 0.5): glVertex3f(-0.5, 0.5,-0.5)
    glEnd()
    wxGLCanvas_SwapBuffers(GLCanvas)
  end select
end sub

function OnInit APICALL as wxBool
  dprint("OnInit")
  ' create a wxFrame
  Frame = wxFrame_ctor()
  wxFrame_Create(Frame,,FRAME_ID,wxString("test for wxGLCanvas"),,,_
                 FRAME_WIDTH,FRAME_HEIGHT, FRAME_STYLE)
  wxEvtHandler_proxy(Frame, @EventListener)
  wxEvtHandler_Connect(Frame,wxEvent_EVT_CLOSE_WINDOW())

  ' create wxTimer
  Ticker = wxTimer_ctor(Frame,TIMER_ID)
  wxEvtHandler_proxy(Frame, @EventListener)
  wxEvtHandler_Connect(Frame, wxEvent_EVT_TIMER())

  ' show the wxFrame
  wxWindow_Show(frame)
  ' start the timer
  wxTimer_Start(Ticker,20)
  return wxApp_OnInit(App)
end function  

function OnExit APICALL as wxInt
  dprint("OnExit")
  return wxApp_OnExit(App)
end function

' create our wxApllication
App = wxApp_ctor()
wxApp_RegisterVirtual(App,@OnInit,@OnExit)
wxApp_Run()

