#INCLUDE "occSDL.module"
#INCLUDE "occGL.module"
#INCLUDE "course.module"

PROC display(CHAN BYTE keyboard?, out!)
  TIMER tim:
  INT t, fpstimer, fpsholder:
  INITIAL INT fpscounter IS 0:
  VAL INT timeout IS 3000:
  INITIAL BOOL running IS TRUE:
  INITIAL BOOL quit IS FALSE:
  INITIAL REAL64 spin IS 0.0:
  INITIAL REAL32 boxsize IS 25.0:
  INITIAL REAL32 change IS 0.5:
  SEQ
    tim ? fpstimer
    fpsholder := fpstimer PLUS 1000000
    WHILE NOT quit
      PRI ALT
        BYTE dummy:
        keyboard ? dummy
          IF
            dummy = 'q'
              quit := TRUE
            running
              running := FALSE
            TRUE
              running := TRUE
        TRUE & SKIP
          SEQ
            glClear(GL.COLOR.BUFFER.BIT)
            glPushMatrix()
            glRotated(spin, 0.0, 0.0, 1.0)
            glColor3f(0.0, 0.5, 1.0)
            glRectf(-boxsize, -boxsize, boxsize, boxsize)
            glPopMatrix()
            SDL.GL.SwapBuffers()
            PAR
              SEQ
                tim ? fpstimer
                IF
                  fpstimer > fpsholder
                    SEQ
                      fpsholder := fpstimer PLUS 1000000
                      out.string("FPS: ",0,out!)
                      out.int(fpscounter,0,out!)
                      out.string("*c*n",0,out!)
                      fpscounter := 0
                  TRUE
                    fpscounter := fpscounter + 1
              SEQ
                tim ? t
                tim ? AFTER t PLUS timeout
            IF 
              running
                SEQ
                  boxsize := boxsize + change
                  spin := spin + 3.0 
                  IF
                    spin > 360.0
                      spin := 0.0
                    TRUE
                      SKIP
                  IF 
                    boxsize < 2.0
                      change := 0.5
                    boxsize > 25.0
                      change := -0.5
                    TRUE
                      SKIP
              TRUE
                SKIP
:

PROC keyevent(CHAN BYTE scr!, keyboard!)
  SDL.Event event:
  SDL.KeyboardEvent keyevent:
  TIMER tim:
  INT wait:
  VAL INT delay IS 1000:
  SDL.keysym keyinfo:
  BYTE type:
  INT re:
  INITIAL BOOL quit IS FALSE:
  SEQ
    create.SDL.Event(event)
    WHILE NOT quit
      SEQ 
        
        tim ? wait
        tim ? AFTER wait PLUS delay
        tim ? wait
        SDL.PollEvent(re, event)
        tim ? AFTER wait PLUS delay
        IF
          re > 0
            SEQ
              get.SDL.Event.type(type, event)
              IF 
                (INT type) = SDL.KEYDOWN
                  INT key:
                  SEQ
                    keyevent := (SDL.KeyboardEvent event)
                    get.SDL.KeyboardEvent.keysym(keyinfo, keyevent)
                    get.SDL.keysym.sym(key, keyinfo)
                    scr ! (BYTE key)
                    keyboard ! (BYTE key)
                    IF
                      (BYTE key) = 'q'
                        quit := TRUE
                      TRUE
                        SKIP
                  --out.string("SDL.KEYDOWN detected!!*n", 0, scr)
                (INT type) = SDL.KEYUP
                  SKIP 
                  --out.string("SDL.KEYUP detected!!*n", 0, scr)
                (INT type) = SDL.MOUSEBUTTONDOWN
                  out.string("mouse click!*c*n",0,scr)
                (INT type) = SDL.QUIT
                  SEQ
                    quit := TRUE
                    keyboard ! 'q'
                TRUE
                  SKIP
                  --out.string("no keystrokes", 0, scr)
          TRUE 
            SKIP
    delete.SDL.Event(event)
:

PROC simple.gl(CHAN BYTE kyb?, scr!, err!)
  CHAN BYTE quit:
  INT result:
  SDL.Surface surface:
  VAL INT err.const IS 100:
  INITIAL INT width, height IS 500, 500:
  SEQ
    out.string("SDL SIMPLE_GL: from occam*c*n", 0, scr!)
    out.string("(c) 2003 Christian L. Jacobsen*c*n", 0, scr!)
    out.string("(c) 2005 Damian Dimmich*c*n*n", 0, scr!)
    occ.SDL.Init(result, SDL.INIT.VIDEO)
    out.string("sdl init ok*c*n", 0, scr!)
    IF
      result <> 0
        [err.const]BYTE err.str:
        INT err.len:
        SEQ
          out.string("SDL initialisation failed: ", 0, scr!)
          occ.SDL.GetError(err.str, err.const, err.len)
          out.string([err.str FOR err.len], 0, scr!)
          out.string("*c*n", 0, scr!)
      TRUE
        INT re:
        SEQ
          SDL.GL.SetAttribute(re, SDL.GL.RED.SIZE, 5)
          SDL.GL.SetAttribute(re, SDL.GL.GREEN.SIZE, 5)
          SDL.GL.SetAttribute(re, SDL.GL.BLUE.SIZE, 5)
          SDL.GL.SetAttribute(re, SDL.GL.DOUBLEBUFFER, 1)
          SDL.SetVideoMode(surface, width, height, 32, SDL.OPENGL)
          IF
            (INT surface) = 0
              [100]BYTE err.str:
              INT err.len:
              SEQ
                out.string("GL initialisation failed: ", 0, scr!)
                occ.SDL.GetError(err.str, err.const, err.len)
                out.string([err.str FOR err.len], 0, scr!)
                out.string("*c*n", 0, scr!)
                out.string("surface pointer is: ", 0, scr!)
                out.hex((INT surface), 0, scr!)
                out.string("*c*n", 0, scr!)
            TRUE
              SEQ
                --out.string("surface is: ", 0, scr!)
                --out.int((INT surface), 0, scr!)
                --scr ! '*n'
                --scr ! FLUSH
                glShadeModel(GL.FLAT)
                glViewport(0, 0, width, height)
                glMatrixMode(GL.PROJECTION)
                glLoadIdentity()
                glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0)
                glMatrixMode(GL.MODELVIEW)
                glLoadIdentity()
                glClearColor(0.1, 0.3, 0.2, 0.0)
                glClear(GL.COLOR.BUFFER.BIT)
                glFlush()
                out.string("Press q in the window to quit, any key to pause*c*n", 0, scr!)
                PAR
                  display(quit?, scr!)
                  keyevent(err!, quit!)
    SDL.Quit()
:
