(libload "libc/constants")
(libload "opengl/opengl")
(libload "sdl/libsdl")

(de sdlgl-demo ()
  (printf "press the up/down and left/right arrows to rotate the triangle\n")
  (printf "press q to quit\n")
  ;; initialize the SDL subsystem. DONT FORGET THIS!!!
  (sdl-initialize)
  (let ((scr (new sdlgl-screen 640 480 "SDL-GL Demo"))
	(event (new sdl-event))
	(xyk (int-matrix 3))
	(theta 0) (rho 0)
	(stop ()))
    (while (not stop)
      (==> event get-arrows xyk)	; read keyboard
      (when (= (xyk 2) @@SDLK_q) (setq stop t))	; stop when q is pressed
      ;; change angles based on keyboard input
      (incr theta (* (xyk 0) 5))
      (incr rho (* (xyk 1) 5))
      ;; setup viewport and such
      (glViewport 80 0 480 480)
      (glMatrixMode @@GL_PROJECTION)
      (glLoadIdentity)
      (glFrustum -1.0 1.0 -1.0 1.0 1.0 100.0)
      ;; make apropriate coordinate transforms
      (glClearColor 0 0 0 0)
      (glMatrixMode @@GL_MODELVIEW)
      (glLoadIdentity)
      (gltranslatef 0 0 -4.0)
      (glrotatef theta 0 0 -1)
      (glrotatef rho 1 0 0)
      ;; draw a triangle
      (glClear @@GL_COLOR_BUFFER_BIT)
      (glBegin @@GL_TRIANGLES)
      (glColor3f 1.0 0 0) (glVertex3f 0.0 1.0 0.0)
      (glColor3f 0 1.0 0) (glVertex3f 1.0 -1.0 0.0)
      (glColor3f 0 0 1.0) (glVertex3f -1.0 -1.0 0.0)
      (glEnd)
      (glFinish)
      ;; slap image on the screen
      (==> scr flip)))
  ;; close window
  (sdl-terminate))

(printf "type (sdlgl-demo) to start the demo\n")



