home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / Mesa-aux / src-aux.aos / glaux.c < prev    next >
C/C++ Source or Header  |  1999-07-12  |  9KB  |  424 lines

  1. /*
  2.  * aux.c 
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <GL/gl.h>
  9. #include <GL/gltk.h>
  10. #include <GL/glaux.h>
  11.  
  12. static struct {
  13.   int keyField;
  14.   void (*KeyFunc) (void);
  15. } keyTable[200];
  16.  
  17. static struct {
  18.   int mouseField;
  19.   void (*MouseFunc) (AUX_EVENTREC *);
  20. } mouseDownTable[20], mouseUpTable[20], mouseLocTable[20];
  21.  
  22. static int keyTableCount = 0;
  23. static int mouseDownTableCount = 0;
  24. static int mouseUpTableCount = 0;
  25. static int mouseLocTableCount = 0;
  26. static GLenum displayModeType = 0;
  27. static GLenum displayModePolicy = AUX_MINIMUM_CRITERIA;
  28. static int displayModeID = 0;
  29.  
  30. static int animate = 0;
  31.  
  32. #define NCOLORS 8
  33. float auxRGBMap[NCOLORS][3] =
  34. {
  35.   {0, 0, 0},
  36.   {1, 0, 0},
  37.   {0, 1, 0},
  38.   {1, 1, 0},
  39.   {0, 0, 1},
  40.   {1, 0, 1},
  41.   {0, 1, 1},
  42.   {1, 1, 1}
  43. };
  44.  
  45. static void DefaultHandleReshape(int w, int h)
  46. {
  47.   glViewport(0, 0, w, h);
  48.   glMatrixMode(GL_PROJECTION);
  49.   glLoadIdentity();
  50.   glOrtho(0.0, (GLdouble) w, 0.0, (GLdouble) h, -1.0, 1.0);
  51.   glMatrixMode(GL_MODELVIEW);
  52.   glLoadIdentity();
  53. }
  54.  
  55. static void DefaultHandleExpose(int w, int h)
  56. {
  57. }
  58.  
  59. static GLenum MouseLoc(int x, int y, GLenum button)
  60. {
  61.   AUX_EVENTREC info;
  62.   GLenum flag;
  63.   int i;
  64.  
  65.   flag = GL_FALSE;
  66.   for (i = 0; i < mouseLocTableCount; i++) {
  67.     if ((button & AUX_LEFTBUTTON) == mouseLocTable[i].mouseField) {
  68.       info.event = AUX_MOUSELOC;
  69.       info.data[AUX_MOUSEX] = x;
  70.       info.data[AUX_MOUSEY] = y;
  71.       info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  72.       (*mouseLocTable[i].MouseFunc) (&info);
  73.       flag |= GL_TRUE;
  74.     }
  75.     if ((button & AUX_RIGHTBUTTON) == mouseLocTable[i].mouseField) {
  76.       info.event = AUX_MOUSELOC;
  77.       info.data[AUX_MOUSEX] = x;
  78.       info.data[AUX_MOUSEY] = y;
  79.       info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  80.       (*mouseLocTable[i].MouseFunc) (&info);
  81.       flag |= GL_TRUE;
  82.     }
  83.     if ((button & AUX_MIDDLEBUTTON) == mouseLocTable[i].mouseField) {
  84.       info.event = AUX_MOUSELOC;
  85.       info.data[AUX_MOUSEX] = x;
  86.       info.data[AUX_MOUSEY] = y;
  87.       info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  88.       (*mouseLocTable[i].MouseFunc) (&info);
  89.       flag |= GL_TRUE;
  90.     }
  91.   }
  92.   return flag;
  93. }
  94.  
  95. static GLenum MouseUp(int x, int y, GLenum button)
  96. {
  97.   AUX_EVENTREC info;
  98.   GLenum flag;
  99.   int i;
  100.  
  101.   flag = GL_FALSE;
  102.   for (i = 0; i < mouseUpTableCount; i++) {
  103.     if ((button & AUX_LEFTBUTTON) == mouseUpTable[i].mouseField) {
  104.       info.event = AUX_MOUSEUP;
  105.       info.data[AUX_MOUSEX] = x;
  106.       info.data[AUX_MOUSEY] = y;
  107.       info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  108.       (*mouseUpTable[i].MouseFunc) (&info);
  109.       flag |= GL_TRUE;
  110.     }
  111.     if ((button & AUX_RIGHTBUTTON) == mouseUpTable[i].mouseField) {
  112.       info.event = AUX_MOUSEUP;
  113.       info.data[AUX_MOUSEX] = x;
  114.       info.data[AUX_MOUSEY] = y;
  115.       info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  116.       (*mouseUpTable[i].MouseFunc) (&info);
  117.       flag |= GL_TRUE;
  118.     }
  119.     if ((button & AUX_MIDDLEBUTTON) == mouseUpTable[i].mouseField) {
  120.       info.event = AUX_MOUSEUP;
  121.       info.data[AUX_MOUSEX] = x;
  122.       info.data[AUX_MOUSEY] = y;
  123.       info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  124.       (*mouseUpTable[i].MouseFunc) (&info);
  125.       flag |= GL_TRUE;
  126.     }
  127.   }
  128.   return flag;
  129. }
  130.  
  131. static GLenum MouseDown(int x, int y, GLenum button)
  132. {
  133.   AUX_EVENTREC info;
  134.   GLenum flag;
  135.   int i;
  136.  
  137.   flag = GL_FALSE;
  138.   for (i = 0; i < mouseDownTableCount; i++) {
  139.     if ((button & AUX_LEFTBUTTON) == mouseDownTable[i].mouseField) {
  140.       info.event = AUX_MOUSEDOWN;
  141.       info.data[AUX_MOUSEX] = x;
  142.       info.data[AUX_MOUSEY] = y;
  143.       info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  144.       (*mouseDownTable[i].MouseFunc) (&info);
  145.       flag |= GL_TRUE;
  146.     }
  147.     if ((button & AUX_RIGHTBUTTON) == mouseDownTable[i].mouseField) {
  148.       info.event = AUX_MOUSEDOWN;
  149.       info.data[AUX_MOUSEX] = x;
  150.       info.data[AUX_MOUSEY] = y;
  151.       info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  152.       (*mouseDownTable[i].MouseFunc) (&info);
  153.       flag |= GL_TRUE;
  154.     }
  155.     if ((button & AUX_MIDDLEBUTTON) == mouseDownTable[i].mouseField) {
  156.       info.event = AUX_MOUSEDOWN;
  157.       info.data[AUX_MOUSEX] = x;
  158.       info.data[AUX_MOUSEY] = y;
  159.       info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  160.       (*mouseDownTable[i].MouseFunc) (&info);
  161.       flag |= GL_TRUE;
  162.     }
  163.   }
  164.   return flag;
  165. }
  166.  
  167. static GLenum KeyDown(int key, GLenum status)
  168. {
  169.   GLenum flag;
  170.   int i;
  171.  
  172.   flag = GL_FALSE;
  173.   if (keyTableCount) {
  174.     for (i = 0; i < keyTableCount; i++) {
  175.       if (key == keyTable[i].keyField) {
  176.     (*keyTable[i].KeyFunc) ();
  177.     flag |= GL_TRUE;
  178.       }
  179.     }
  180.   }
  181.   return flag;
  182. }
  183.  
  184. void auxExposeFunc(void (*Func) (int, int))
  185. {
  186.   tkExposeFunc(Func);
  187. }
  188.  
  189. void auxReshapeFunc(void (*Func) (int, int))
  190. {
  191.   tkExposeFunc(Func);
  192.   tkReshapeFunc(Func);
  193. }
  194.  
  195. void auxIdleFunc(void (*Func) (void))
  196. {
  197.   tkIdleFunc(Func);
  198. }
  199.  
  200. void auxKeyFunc(int key, void (*Func) (void))
  201. {
  202.   keyTable[keyTableCount].keyField = key;
  203.   keyTable[keyTableCount++].KeyFunc = Func;
  204. }
  205.  
  206. void auxMouseFunc(int mouse, int mode, void (*Func) (AUX_EVENTREC *))
  207. {
  208.   if (mode == AUX_MOUSEDOWN) {
  209.     mouseDownTable[mouseDownTableCount].mouseField = mouse;
  210.     mouseDownTable[mouseDownTableCount++].MouseFunc = Func;
  211.   }
  212.   else if (mode == AUX_MOUSEUP) {
  213.     mouseUpTable[mouseUpTableCount].mouseField = mouse;
  214.     mouseUpTable[mouseUpTableCount++].MouseFunc = Func;
  215.   }
  216.   else if (mode == AUX_MOUSELOC) {
  217.     mouseLocTable[mouseLocTableCount].mouseField = mouse;
  218.     mouseLocTable[mouseLocTableCount++].MouseFunc = Func;
  219.   }
  220. }
  221.  
  222. void auxDeleteMouseFunc(int mouse, int mode, void (*Func) (AUX_EVENTREC *))
  223. {
  224.   int i, j;
  225.  
  226.   for (i = 0; i < mouseLocTableCount; i++) {
  227.     if (mouseLocTable[i].MouseFunc == Func) {
  228.       /*
  229.        * delete this one 
  230.        */
  231.       for (j = i + 1; j < mouseLocTableCount; j++) {
  232.     mouseLocTable[j - 1].MouseFunc = mouseLocTable[j].MouseFunc;
  233.     mouseLocTable[j - 1].mouseField = mouseLocTable[j].mouseField;
  234.       }
  235.       mouseLocTableCount--;
  236.       break;
  237.     }
  238.   }
  239.  
  240. }
  241.  
  242. static void idle(void)
  243. {
  244.   /* do nothing */
  245. }
  246.  
  247. void auxMainLoop(void (*Func) (void))
  248. {
  249.   if (animate)
  250.     auxIdleFunc(idle);
  251.  
  252.   tkDisplayFunc(Func);
  253.   tkExec();
  254. }
  255.  
  256. void auxInitPosition(int x, int y, int width, int height)
  257. {
  258.   tkInitPosition(x, y, width, height);
  259. }
  260.  
  261. void auxInitDisplayMode(GLbitfield type)
  262. {
  263.   displayModeType = type;
  264.   tkInitDisplayMode(type);
  265. }
  266.  
  267. void auxInitDisplayModePolicy(GLenum type)
  268. {
  269.  
  270.   displayModePolicy = type;
  271.   tkInitDisplayModePolicy(type);
  272. }
  273.  
  274. GLenum auxInitDisplayModeID(GLint id)
  275. {
  276.   displayModeID = id;
  277.   return tkInitDisplayModeID(id);
  278. }
  279.  
  280. GLenum auxInitWindow(char *title)
  281. {
  282.   int useDoubleAsSingle = 0;
  283.  
  284.   if (tkInitWindow(title) == GL_FALSE) {
  285.     if (AUX_WIND_IS_SINGLE(displayModeType)) {
  286.       tkInitDisplayMode(displayModeType | AUX_DOUBLE);
  287.       if (!tkInitWindow(title))
  288.     return GL_FALSE;
  289.       fprintf(stderr, "Can't initialize a single buffer visual.\n");
  290.       fprintf(stderr, "Will use a double buffer visual instead,");
  291.       fprintf(stderr, "only drawing into the front buffer.\n");
  292.       displayModeType = displayModeType | AUX_DOUBLE;
  293.       useDoubleAsSingle = 1;
  294.     }
  295.     else
  296.       return GL_FALSE;
  297.   }
  298.   tkReshapeFunc(DefaultHandleReshape);
  299.   tkExposeFunc(DefaultHandleExpose);
  300.   tkMouseUpFunc(MouseUp);
  301.   tkMouseDownFunc(MouseDown);
  302.   tkMouseMoveFunc(MouseLoc);
  303.   tkKeyDownFunc(KeyDown);
  304.   auxKeyFunc(AUX_ESCAPE, auxQuit);
  305.   glClearColor(0.0, 0.0, 0.0, 1.0);
  306.   glClearIndex(0);
  307.   glLoadIdentity();
  308.   if (useDoubleAsSingle) {
  309.     glReadBuffer(GL_FRONT);
  310.     glDrawBuffer(GL_FRONT);
  311.   }
  312.   return GL_TRUE;
  313. }
  314.  
  315. void auxCloseWindow(void)
  316. {
  317.   tkCloseWindow();
  318.   keyTableCount = 0;
  319.   mouseDownTableCount = 0;
  320.   mouseUpTableCount = 0;
  321.   mouseLocTableCount = 0;
  322. }
  323.  
  324. void auxQuit(void)
  325. {
  326.   tkQuit();
  327. }
  328.  
  329. void auxSwapBuffers(void)
  330. {
  331.   tkSwapBuffers();
  332. }
  333.  
  334. void *auxAOSScreen(void)
  335. {
  336.   void *ptr;
  337.  
  338.   tkGetSystem(TK_AOS_SCREEN, (void *)&ptr);
  339.   return ptr;
  340. }
  341.  
  342. void *auxAOSWindow(void)
  343. {
  344.   void *ptr;
  345.  
  346.   tkGetSystem(TK_AOS_WINDOW, (void *)&ptr);
  347.   return ptr;
  348. }
  349.  
  350. void *auxAOSContext(void)
  351. {
  352.   void *ptr;
  353.  
  354.   tkGetSystem(TK_AOS_CONTEXT, (void *)&ptr);
  355.   return ptr;
  356. }
  357.  
  358. GLenum auxGetDisplayModePolicy(void)
  359. {
  360.   return tkGetDisplayModePolicy();
  361. }
  362.  
  363. GLint auxGetDisplayModeID(void)
  364. {
  365.   return tkGetDisplayModeID();
  366. }
  367.  
  368. GLenum auxGetDisplayMode(void)
  369. {
  370.   return tkGetDisplayMode();
  371. }
  372.  
  373. void auxSetOneColor(int index, float r, float g, float b)
  374. {
  375.   tkSetOneColor(index, r, g, b);
  376. }
  377.  
  378. void auxSetFogRamp(int density, int startIndex)
  379. {
  380.   tkSetFogRamp(density, startIndex);
  381. }
  382.  
  383. void auxSetGreyRamp(void)
  384. {
  385.   tkSetGreyRamp();
  386. }
  387.  
  388. void auxSetRGBMap(int size, float *rgb)
  389. {
  390.   tkSetRGBMap(size, rgb);
  391. }
  392.  
  393. int auxGetColorMapSize(void)
  394. {
  395.   return tkGetColorMapSize();;
  396. }
  397.  
  398. void auxGetMouseLoc(int *x, int *y)
  399. {
  400.   tkGetMouseLoc(x, y);
  401. }
  402.  
  403. #include <intuition/screens.h>
  404. void auxGetScreenSize(GLint * width, GLint * height)
  405. {
  406.   struct Screen *ptr;
  407.  
  408.   tkGetSystem(TK_AOS_SCREEN, (void *)&ptr);
  409.  
  410.   if(ptr) {
  411.     *width = ptr->Width;
  412.     *height = ptr->Height;
  413.   }
  414.   else {
  415.     *width = 0;
  416.     *height = 0;
  417.   }
  418. }
  419.  
  420. void auxAnimation(GLint state)
  421. {
  422.   animate = state;
  423. }
  424.