#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/device.h>

#define TRUE  1
#define FALSE 0

/*
   Testbed for low level gl programming
   Compile as 
      cc -o gltest gltest.c -lm -lgl
*/

/* Prototypes */
int CreateWindow(char *);
int UpdateWindow(int);
int CreateMenus(char *);
int HandleMenus(int);
void LetsQuit(int);

#define REDRAWMENU 3
#define SAVEMENU 11
#define OPENMENU 12
#define NEWMENU  13
int mainmenu;
int filemenu;

int main(int argc,char **argv)
{
   int wid;
   short data;

   /* Attempt to open the window */
   if ((wid = CreateWindow(argv[0])) == -1) {
      fprintf(stderr,"Unable to create GL window\n");
      exit(-1);
   }
   UpdateWindow(wid);

   /* Attempt to create the menus */
   CreateMenus(argv[0]);

   /* Enter the event loop */
   while (TRUE) {

      switch (qread(&data)) {
      case ESCKEY:
      case QKEY:
         LetsQuit(wid);
         break;
      case RKEY:
      case REDRAWMENU:
         UpdateWindow(wid);
         break;
      case LEFTMOUSE:
         break;
      case MIDDLEMOUSE:
      case RIGHTMOUSE:
         HandleMenus(wid);
         break;
      }

      qreset();
   }

}

/*
   Exit handler
*/
void LetsQuit(int id)
{
   winclose(id);
   exit(0);
}

/*
   Create the window 
*/
int CreateWindow(char *name)
{
   int id;

   /* Prefered size */
   prefsize(500,400);

   /* Create the window */
   id = winopen(name);
   RGBmode();
   gconfig();

   /* Queue up event types */
   qdevice(LEFTMOUSE);
   qdevice(MIDDLEMOUSE);
   qdevice(RIGHTMOUSE);
   qdevice(RKEY);
   qdevice(QKEY);
   qdevice(ESCKEY);

   return(id);
}

/*
   Create the menus
*/
int CreateMenus(char *name)
{
   char title[64];

   filemenu = newpup();
   addtopup(filemenu,"New  %x13");
   addtopup(filemenu,"Save %x11");
   addtopup(filemenu,"Open %x12");

   mainmenu = newpup();
   sprintf(title,"%s %%t",name);
   addtopup(mainmenu,title);                  /* Menu title */
   addtopup(mainmenu,"File %m",filemenu);
   addtopup(mainmenu,"Redraw %l %x3");         /* REDRAWMENU == 3 */
   addtopup(mainmenu,"Quit %f",LetsQuit);

   return(TRUE);
}

/*
   Deal with the menu items 
*/
int HandleMenus(int id)
{
   switch (dopup(mainmenu)) {
   case REDRAWMENU:
      UpdateWindow(id);
      break;
   case SAVEMENU:
      break;
   case OPENMENU:
      break;
   case NEWMENU:
      break;
   }
   return(TRUE);
}

/*
   Perform a complete redraw of the window
*/
int UpdateWindow(int id)
{
   long v[2];

   RGBcolor(255,255,255);
   clear();

   /* Draw a point */
   RGBcolor(0,0,0);
   pnt2i(10,10);

   /* Write some text */
   cmov2i(10,380);
   charstr("Some text");

   /* Draw some lines */
   RGBcolor(255,0,0);
   move2i(100,100);
   draw2i(200,100);
   draw2i(200,200);
   draw2i(100,200);
   draw2i(100,100);

   /* Draw a polyline */
   bgnline();
   v[0] = 300; v[1] = 300;
   v2i(v);
   v[0] = 400; v[1] = 300;
   v2i(v);
   v[0] = 350; v[1] = 350;
   v2i(v);
   v[0] = 300; v[1] = 300;
   v2i(v);
   endline();

   /* Draw a polygon */
   concave(TRUE);
   RGBcolor(0,200,0);
   bgnpolygon();
   v[0] = 200; v[1] = 300;
   v2i(v);
   v[0] = 300; v[1] = 300;
   v2i(v);
   v[0] = 250; v[1] = 350;
   v2i(v);
   v[0] = 200; v[1] = 300;
   v2i(v);
   endpolygon();

   /* Draw an arc, angles are 1/10 of a degree! */
   RGBcolor(0,0,255);
   arci(300,200,20,0,1800);

   /* Draw a circle */
   RGBcolor(0,0,125);
   circi(300,196,15);

   return(TRUE);
}


