home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src-tk / awindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  23.8 KB  |  962 lines

  1. /* $Id: Awindow.c 1.8 1997/06/25 19:17:47 StefanZ Exp StefanZ $*/
  2. /*
  3.  * (c) Copyright 1993, Silicon Graphics, Inc.
  4.  * ALL RIGHTS RESERVED
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  27.  */
  28.  
  29. /* Note: this file has been extended related to previous Amiga MESA
  30.    implementations and related to the history above. Refer to the
  31.    external history file for further changes */
  32.  
  33.  
  34. /* Mesa tkAmiga by Stefan Z (d94sz@efd.lth.se)*/
  35.  
  36. /*$Log: Awindow.c $
  37.  * Revision 1.8  1997/06/25  19:17:47  StefanZ
  38.  * bumped to mesa 2.2
  39.  *
  40.  * Revision 1.5  1996/10/07  00:18:11  StefanZ
  41.  * Mesa 2.0 Fixed
  42.  *
  43.  * Revision 1.4  1996/08/14  22:53:16  StefanZ
  44.  * rev 1.3 tk fixes was from George 'Wulf' Krämer
  45.  *
  46.  * Revision 1.3  1996/08/14  22:23:31  StefanZ
  47.  * Modified due to api change in AmigaMesa
  48.  * Implemented div. Input modifier and changed windowhandling
  49.  *
  50.  * Revision 1.2  1996/06/02  00:03:03  StefanZ
  51.  * Started to use RCS to keep track of code.
  52.  *
  53.  */
  54. /*
  55. History:
  56.  
  57. 1.0 960315 Now almost evetything is implemented
  58. 1.1 960425 Fixed problem with double closeclicks (Thanx to Daniel Jönsson)
  59. 1.2 960731 Modified due to api change in AmigaMesa
  60. 1.3 Implemented div. Input modifier and changed windowhandling (Georg 'Wulf' Krämer)
  61.  
  62. TODO:
  63. Exposefunc
  64.  
  65.  
  66. */
  67.  
  68. //#define USE_CLIP_LAYER
  69. #undef USE_CLIP_LAYER
  70.  
  71.  
  72. #include <stdio.h>
  73. #include <stdlib.h>
  74. #include <string.h>
  75.  
  76. #include <intuition/intuition.h>
  77. #include <devices/inputevent.h>
  78. #include <clib/cybergraphics_protos.h>
  79. #include <cybergraphics/cybergraphics.h>
  80. #ifdef __GNUC__
  81. #include <inline/exec.h>
  82. #include <inline/intuition.h>
  83. #include <inline/graphics.h>
  84.  
  85. #ifdef USE_CLIP_LAYER
  86. #include <inline/layers.h>
  87. #endif
  88. #else
  89. #ifndef __PPC__
  90. #include <proto/exec.h>
  91. #include <proto/intuition.h>
  92. #include <proto/graphics.h>
  93. #include <proto/dos.h>
  94. #include <proto/asl.h>
  95. #include <pragma/cybergraphics_lib.h>
  96. #else
  97. #include <clib/exec_protos.h>
  98. #include <clib/intuition_protos.h>
  99. #include <clib/graphics_protos.h>
  100. #include <clib/dos_protos.h>
  101. #include <clib/asl_protos.h>
  102. #endif
  103.  
  104.  
  105. #ifdef USE_CLIP_LAYER
  106. #include <proto/layers.h>
  107. #endif
  108. #endif
  109.  
  110. #include "gltk.h"
  111. #include "GL/AmigaMesa.h"
  112.  
  113.  
  114.  
  115. #define static
  116.  
  117. #if defined(__cplusplus) || defined(c_plusplus)
  118. #define class c_class
  119. #endif
  120.  
  121. #if DBG
  122. #define TKASSERT(x)                                     \
  123. if ( !(x) ) {                                           \
  124.     PrintMessage("%s(%d) Assertion failed %s\n",        \
  125.         __FILE__, __LINE__, #x);                        \
  126. }
  127. #else
  128. #define TKASSERT(x)
  129. #endif  /* DBG */
  130.  
  131. /* Some Bitmasks for Amigaevents */
  132. #define ControlMask         (IEQUALIFIER_CONTROL)
  133. #define ShiftMask           (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT)
  134.  
  135. /******************************************************************************/
  136.  
  137. struct wnd 
  138.     {
  139.     int x, y, width, height;
  140.     GLenum type;
  141.     struct amigamesa_context *context;
  142.     };
  143.  
  144. struct wnd win={0,0,100,100,TK_SINGLE,NULL};
  145.  
  146.  
  147. struct Library *CyberGfxBase = NULL;
  148. struct Library *AslBase = NULL;
  149. struct Screen *screen = NULL;
  150. struct Screen *customscreen = NULL;
  151. struct Window *tkhwnd = NULL;
  152.  
  153. int SwapBuffersCalled = 0;
  154. int FullscreenEnable = 0;
  155.  
  156.  
  157.  
  158. #ifdef USE_CLIP_LAYER
  159. struct Region *tkclipreg = NULL;
  160. #endif
  161.  
  162. /* Local prototypes */
  163. struct Region *clipWindow(struct Window *win,
  164.     LONG minX, LONG minY, LONG maxX, LONG maxY);
  165. struct Region *clipWindowToBorders(struct Window *win);
  166.  
  167.  
  168.  
  169. GLboolean tkPopupEnable = TRUE;
  170.  
  171. /* Fixed palette support.  */
  172. /*
  173. #define BLACK   PALETTERGB(0,0,0)
  174. #define WHITE   PALETTERGB(255,255,255)
  175. #define NUM_STATIC_COLORS   (COLOR_BTNHIGHLIGHT - COLOR_SCROLLBAR + 1)
  176. */
  177. static void (*ExposeFunc)(int, int)              = NULL;
  178. static void (*ReshapeFunc)(GLsizei, GLsizei)     = NULL;
  179. static void (*DisplayFunc)(void)                 = NULL;
  180. static GLenum (*KeyDownFunc)(int, GLenum)        = NULL;
  181. static GLenum (*MouseDownFunc)(int, int, GLenum) = NULL;
  182. static GLenum (*MouseUpFunc)(int, int, GLenum)   = NULL;
  183. static GLenum (*MouseMoveFunc)(int, int, GLenum) = NULL;
  184. static void (*IdleFunc)(void)                    = NULL;
  185.  
  186. /*
  187.  *  Prototypes for the debugging functions go here
  188.  */
  189.  
  190. #define DBGFUNC 0
  191. #if DBGFUNC
  192.  
  193. static void DbgPrintf( const char *Format, ... );
  194. static void pwi( void );
  195. static void pwr(RECT *pr);
  196. /*static void ShowPixelFormat(HDC hdc);*/
  197.  
  198. #endif
  199.  
  200. #ifdef USE_CLIP_LAYER
  201. /* Get from clipping example of the RKM */
  202. /*
  203. ** clipWindow()
  204. ** Clip a window to a specified rectangle (given by upper left and
  205. ** lower right corner.)  the removed region is returned so that it
  206. ** may be re-installed later.
  207. */
  208. struct Region *clipWindow(struct Window *win,
  209.     LONG minX, LONG minY, LONG maxX, LONG maxY)
  210. {
  211. struct Region    *new_region;
  212. struct Rectangle  my_rectangle;
  213.  
  214. /* set up the limits for the clip */
  215. my_rectangle.MinX = minX;
  216. my_rectangle.MinY = minY;
  217. my_rectangle.MaxX = maxX;
  218. my_rectangle.MaxY = maxY;
  219.  
  220. /* get a new region and OR in the limits. */
  221. if (NULL != (new_region = NewRegion()))
  222.     {
  223.     if (FALSE == OrRectRegion(new_region, &my_rectangle))
  224.         {
  225.         DisposeRegion(new_region);
  226.         new_region = NULL;
  227.         }
  228.     }
  229.  
  230. /* Install the new region, and return any existing region.
  231. ** If the above allocation and region processing failed, then
  232. ** new_region will be NULL and no clip region will be installed.
  233. */
  234. return(InstallClipRegion(win->WLayer, new_region));
  235. }
  236.  
  237. /*
  238. ** clipWindowToBorders()
  239. ** clip a window to its borders.
  240. ** The removed region is returned so that it may be re-installed later.
  241. */
  242. struct Region *clipWindowToBorders(struct Window *win)
  243. {
  244. return(clipWindow(win, win->BorderLeft, win->BorderTop,
  245.     win->Width - win->BorderRight - 1, win->Height - win->BorderBottom - 1));
  246. }
  247. #endif
  248.  
  249. float tkRGBMap[8][3] = 
  250.     {
  251.     {   0, 0, 0 },
  252.     {   1, 0, 0 },
  253.     {   0, 1, 0 },
  254.     {   1, 1, 0 },
  255.     {   0, 0, 1 },
  256.     {   1, 0, 1 },
  257.     {   0, 1, 1 },
  258.     {   1, 1, 1 }
  259.     };
  260.  
  261. int atk_setIDCMPs(void)
  262.     {
  263.     int mask;
  264.     mask=IDCMP_CLOSEWINDOW | IDCMP_NEWSIZE;
  265.     if (ReshapeFunc)
  266.         mask |=IDCMP_NEWSIZE;
  267.     if (KeyDownFunc)
  268.         mask |=(IDCMP_RAWKEY | IDCMP_VANILLAKEY);
  269.     if (MouseDownFunc||MouseUpFunc)
  270.         mask |=IDCMP_MOUSEBUTTONS;
  271.     if (MouseMoveFunc)
  272.         mask |= IDCMP_MOUSEMOVE;
  273.  
  274.     return(mask);
  275.     }
  276.  
  277. /* TODO (*ExposeFunc)(int, int)   */
  278.  
  279. void atk_modifyIDCMP(void)
  280.     {
  281.     if(tkhwnd)
  282.         {
  283.         ModifyIDCMP( tkhwnd, atk_setIDCMPs() );
  284.         }
  285.     }
  286.  
  287.  
  288. int atk_FixKeyRAW(char c)
  289.     {
  290.     int key;
  291.     switch (c) 
  292.         {
  293.         /*
  294.       case XK_Return:   key = TK_RETURN;    break;
  295.       case XK_Escape:   key = TK_ESCAPE;    break;
  296.         */
  297.       case CURSORLEFT:   key = TK_LEFT;      break;
  298.       case CURSORUP:     key = TK_UP;        break;
  299.       case CURSORRIGHT:  key = TK_RIGHT;     break;
  300.       case CURSORDOWN:   key = TK_DOWN;      break;
  301.       default:      key = GL_FALSE;     break;
  302.     }
  303.     return(key);
  304.     }
  305.  
  306.  
  307. /***************************************************************
  308.  *                                                             *
  309.  *  Exported Functions go here                                 *
  310.  *                                                             *
  311.  ***************************************************************/
  312.  
  313.  
  314. void tkNewCursor(GLint id, GLubyte *shapeBuf, GLubyte *maskBuf, GLenum fgColor,
  315.      GLenum bgColor, GLint hotX, GLint hotY)
  316. {
  317. /* cursor.c */ 
  318. }
  319.  
  320. void tkSetCursor(GLint id)
  321. {
  322. /* cursor.s*/
  323. }
  324.  
  325. void tkErrorPopups(GLboolean bEnable)
  326. {
  327.     tkPopupEnable = bEnable;
  328. }
  329.  
  330. void tkCloseWindow(void)
  331.     {
  332.     if (win.context) 
  333.         {
  334.         AmigaMesaDestroyContext(win.context);
  335.         win.context=NULL;
  336.         }
  337. #ifdef USE_CLIP_LAYER
  338.     if (tkclipreg)
  339.         {
  340.         tkclipreg = InstallClipRegion(tkhwnd->WLayer, tkclipreg);
  341.         DisposeRegion(tkclipreg);
  342.         tkclipreg = NULL;
  343.         }
  344. #endif
  345.     if (tkhwnd)
  346.         {
  347.         CloseWindow(tkhwnd);
  348.         tkhwnd=NULL;
  349.         }
  350.     if (customscreen)
  351.         {
  352.         CloseScreen(customscreen);
  353.         customscreen = NULL;
  354.         }
  355.     if (CyberGfxBase)
  356.         {
  357.         CloseLibrary(CyberGfxBase);
  358.         }
  359.     if (AslBase)
  360.         {
  361.         CloseLibrary(AslBase);
  362.         }
  363.     }
  364.  
  365.  
  366.  
  367. //#define PL printf("%d\n",__LINE__);
  368.  
  369. void tkExec(void)
  370.     {
  371.     struct IntuiMessage *msg;
  372.     BOOL done = FALSE;
  373.     BOOL press=FALSE;
  374.     int key,id;
  375.     /* Redraw handling changed by Wulf 11. Aug. 96 to ensure the
  376.        same handling under all systems */
  377.     GLenum Redraw=GL_FALSE;
  378.  
  379. //printf("tkExec\n");
  380.  
  381.     if (ReshapeFunc)
  382.         {
  383.         (*ReshapeFunc)(tkhwnd->Width-tkhwnd->BorderLeft-tkhwnd->BorderRight, tkhwnd->Height-tkhwnd->BorderTop-tkhwnd->BorderBottom);
  384.         }
  385.  
  386.     if (DisplayFunc)
  387.         {
  388.         //printf("DisplayFunk\n");
  389.         SwapBuffersCalled = 0;
  390.         (*DisplayFunc)();
  391.         if ((SwapBuffersCalled == 0) && (win.type & TK_DOUBLE))
  392.             AmigaMesaSwapBuffers(win.context);
  393.         }
  394.  
  395.  
  396.     while (!done)
  397.         {
  398.         if (msg = (struct IntuiMessage *)GetMsg(tkhwnd->UserPort))
  399.             {
  400.             id=msg->Class;
  401.                                     /*  printf("msg->Class=0x%x\n",id);*/
  402.             // after ReplyMsg, you are nolonger allowed to use msg !
  403.             switch (id)
  404.                 {
  405.                 case IDCMP_NEWSIZE:
  406.                                 /* Sizes should be adjusted by border, but painting isn't adjusted :-( */
  407.                         win.width=  tkhwnd->Width-tkhwnd->BorderLeft-tkhwnd->BorderRight;
  408.                         win.height= tkhwnd->Height-tkhwnd->BorderTop-tkhwnd->BorderBottom;
  409. #ifdef USE_CLIP_LAYER
  410.                         if (tkclipreg) // Get rid of last clipping region
  411.                             {
  412.                             DisposeRegion(InstallClipRegion(msg->IDCMPWindow->WLayer,NULL));
  413.                             }
  414.                         clipWindowToBorders(msg->IDCMPWindow);
  415. #endif
  416.                         ReplyMsg((struct Message *)msg);
  417.                         if (ReshapeFunc)
  418.                             (*ReshapeFunc)(win.width, win.height);
  419.                         Redraw = GL_TRUE;
  420.                         break;
  421.                 case IDCMP_RAWKEY:
  422.                         key= atk_FixKeyRAW(msg->Code);
  423.                         if (key && KeyDownFunc)
  424.                             {
  425.                             GLenum mask;
  426.                             mask = 0;
  427.                             if (msg->Qualifier & ControlMask)
  428.                                 mask |= TK_CONTROL;
  429.                             if (msg->Qualifier & ShiftMask)
  430.                                 mask |= TK_SHIFT;
  431.                             ReplyMsg((struct Message *)msg);
  432.                             Redraw = (*KeyDownFunc)(key, mask);
  433.                             } else
  434.                                 ReplyMsg((struct Message *)msg);
  435.                         break;
  436.                 case IDCMP_VANILLAKEY:
  437.                         if (KeyDownFunc)
  438.                             {
  439.                             GLenum mask;
  440.                             mask = 0;
  441.                             if (msg->Qualifier & ControlMask)
  442.                                 mask |= TK_CONTROL;
  443.                             if (msg->Qualifier & ShiftMask)
  444.                                 mask |= TK_SHIFT;
  445.                             key = msg->Code;
  446.                             ReplyMsg((struct Message *)msg);
  447.                             Redraw = (*KeyDownFunc)(key, mask);
  448.                             } else
  449.                                 ReplyMsg((struct Message *)msg);
  450.                         break;
  451.                 case IDCMP_MOUSEMOVE:
  452.                         if (MouseMoveFunc) {
  453.                             int x = msg->MouseX-tkhwnd->BorderLeft,y = msg->MouseY-tkhwnd->BorderTop;
  454.                                 ReplyMsg((struct Message *)msg);
  455.                                 Redraw = (*MouseMoveFunc)(x,y,press);
  456.                         } else
  457.                             ReplyMsg((struct Message *)msg);
  458.                         break;                  
  459.                 case IDCMP_MOUSEBUTTONS:
  460.                      /* Buttonhandling changed Wulf 11.08.96 */
  461.                      /* Buttonhandling changed by Sam Jordan 03.01.98 */
  462.                 /* printf("code=%x\n",(WORD)msg->Code); */
  463.                         {
  464.                         GLenum mask = 0;
  465.                         int x = msg->MouseX-tkhwnd->BorderLeft, y = msg->MouseY-tkhwnd->BorderTop;
  466.                         switch (msg->Code)
  467.                             {
  468.                             case SELECTDOWN: case SELECTUP: mask |= TK_LEFTBUTTON; break;
  469.                             case MIDDLEDOWN: case MIDDLEUP: mask |= TK_MIDDLEBUTTON; break;
  470.                             case MENUDOWN: case MENUUP: mask |= TK_RIGHTBUTTON; break;
  471.                             }
  472.                         if (msg->Qualifier & ControlMask)
  473.                             mask |= TK_CONTROL;
  474.                         if (msg->Qualifier & ShiftMask)
  475.                             mask |= TK_SHIFT;
  476.                         if (!(msg->Code & IECODE_UP_PREFIX))
  477.                             {
  478.                             press=mask;
  479.                             if (MouseDownFunc) {
  480.                                 ReplyMsg((struct Message *)msg);
  481.                                 Redraw = (*MouseDownFunc)(x,y,mask);
  482.                             } else
  483.                                 ReplyMsg((struct Message *)msg);
  484.                             }
  485.                         else
  486.                             {
  487.                             if (MouseUpFunc)
  488.                                 {
  489.                                 ReplyMsg((struct Message *)msg);
  490.                                 Redraw = (*MouseUpFunc)(x,y,mask);
  491.                                 } else
  492.                                 ReplyMsg((struct Message *)msg);
  493.                             press=0;
  494.                             }
  495.                         }
  496.                         break;
  497.                 case IDCMP_REFRESHWINDOW:   /* Not entierly tested! Wulf 11.08.96 */
  498.                         BeginRefresh(msg->IDCMPWindow);  /* Refresh Gadgets */
  499.                         EndRefresh(msg->IDCMPWindow, TRUE);
  500.                         ReplyMsg((struct Message *)msg);
  501.                         if (ExposeFunc)
  502.                             (*ExposeFunc)(win.width, win.height);
  503.                         Redraw = GL_TRUE;
  504.                         break;
  505.                 case IDCMP_CLOSEWINDOW:
  506.                         ReplyMsg((struct Message *)msg);
  507.                         done = TRUE;
  508.                         break;
  509.                 default:
  510.                         ReplyMsg((struct Message *)msg);
  511.                         break;
  512.                 }
  513.             }
  514.                                 /* TODO Fill this with tests and call apropriate functions */
  515.             if (IdleFunc)
  516.                 {
  517.                 SwapBuffersCalled = 0;
  518.                 (*IdleFunc)();
  519.                 if ((SwapBuffersCalled == 0) && (win.type & TK_DOUBLE))
  520.                     AmigaMesaSwapBuffers(win.context);
  521.                 Redraw = GL_TRUE;
  522.                 }
  523.             if ((Redraw==GL_TRUE) && DisplayFunc) /* Redraw handling changed by Wulf 11. Aug. 96 */
  524.                 {
  525.                 SwapBuffersCalled = 0;
  526.                 (*DisplayFunc)();
  527.                 if ((SwapBuffersCalled == 0) && (win.type & TK_DOUBLE))
  528.                     AmigaMesaSwapBuffers(win.context);
  529.                 Redraw = GL_FALSE;
  530.                 }
  531.             if(!done && !IdleFunc)
  532.                 {
  533.                 WaitPort(tkhwnd->UserPort);
  534.                 }
  535.         }
  536.     tkQuit();      /* do not return after quit */
  537. }
  538.  
  539.  
  540. void tkExposeFunc(void (*Func)(int, int))
  541.     {
  542. /*  printf("ExposeFunc\n");*/
  543.     ExposeFunc = Func;
  544.     atk_modifyIDCMP();
  545.     }
  546.  
  547. void tkReshapeFunc(void (*Func)(GLsizei, GLsizei))
  548.     {
  549. /*  printf("ReshapeFunc\n");*/
  550.     ReshapeFunc = Func;
  551.     atk_modifyIDCMP();
  552.     }
  553.  
  554. void tkDisplayFunc(void (*Func)(void))
  555.     {
  556. /*  printf("DisplayFunc\n");*/
  557.     DisplayFunc = Func;
  558.     }
  559.  
  560. void tkKeyDownFunc(GLenum (*Func)(int, GLenum))
  561.     {
  562. /*  printf("KeyDownFunc\n");*/
  563.     KeyDownFunc = Func;
  564.     atk_modifyIDCMP();
  565.     }
  566.  
  567. void tkMouseDownFunc(GLenum (*Func)(int, int, GLenum))
  568.     {
  569. /*  printf("MouseDownFunc\n");*/
  570.     MouseDownFunc = Func;
  571.     atk_modifyIDCMP();
  572.     }
  573.  
  574. void tkMouseUpFunc(GLenum (*Func)(int, int, GLenum))
  575.     {
  576. /*  printf("MouseUpFunc\n");*/
  577.     MouseUpFunc = Func;
  578.     atk_modifyIDCMP();
  579.     }
  580.  
  581. void tkMouseMoveFunc(GLenum (*Func)(int, int, GLenum))
  582.     {
  583. /*  printf("MouseMoveFunc\n");*/
  584.     MouseMoveFunc = Func;
  585.     atk_modifyIDCMP();
  586.     }
  587.  
  588. void tkIdleFunc(void (*Func)(void))
  589.     {
  590. /*  printf("IdleFunc\n");*/
  591.     IdleFunc = Func;
  592.     }
  593.  
  594. void tkInitPosition(int x, int y, int width, int height)
  595.     {
  596.     win.x=x;
  597.     win.y=y;
  598.     win.width=width;
  599.     win.height=height;
  600.     }
  601.  
  602. void tkInitDisplayMode(GLenum type)
  603.     {
  604.     char varbuffer[256];
  605.  
  606.     win.type = type;
  607.     if (GetVar("MESA/FORCE_DB",varbuffer,256,0) != -1)
  608.         if (strcmp("1",varbuffer) == 0)
  609.         win.type |= TK_DOUBLE;
  610.     if (GetVar("MESA/FORCE_IR",varbuffer,256,0) != -1)
  611.         if (strcmp("1",varbuffer) == 0)
  612.         win.type |= TK_INDIRECT;
  613.     if (GetVar("MESA/FORCE_FS",varbuffer,256,0) != -1)
  614.         if (strcmp("1",varbuffer) == 0)
  615.         FullscreenEnable = -1;
  616.     if (!(win.type & TK_INDIRECT))
  617.         win.type |= TK_DOUBLE;
  618.     }
  619.  
  620. GLenum tkInitWindow(char *title)
  621.     {
  622.     GLenum   Result =GL_FALSE,
  623.                 RGB_Flag    =GL_TRUE,
  624.                 DB_Flag =GL_FALSE,
  625.                 Alpha_Flag = GL_FALSE,
  626.                 DR_Flag = GL_TRUE;
  627.  
  628.     if (!FullscreenEnable)
  629.     {
  630.  
  631.             if (!(screen = LockPubScreen("MESA")))
  632.                 screen = LockPubScreen(NULL);
  633.             if (screen)
  634.                 {
  635.                                             /* open the window on the public screen */
  636.  
  637.  
  638.                 tkhwnd = OpenWindowTags(NULL,
  639.                             WA_Left,  win.x,            WA_Top,    win.x,
  640.                             WA_InnerWidth, win.width,   WA_InnerHeight, win.height,
  641.                             WA_DragBar,                 TRUE,
  642.                             WA_CloseGadget,         TRUE,
  643.                             WA_SizeGadget,          TRUE,
  644.                             WA_DepthGadget,         TRUE,
  645.                             WA_SmartRefresh,            TRUE,
  646.                             WA_ReportMouse,         TRUE,
  647.                             WA_NoCareRefresh,           TRUE,
  648.                             WA_RMBTrap,                 TRUE, /* Detect right mouse events, no Menus */
  649.                             WA_SizeBBottom,         TRUE,
  650.                             WA_MinWidth,100,        WA_MinHeight,30,
  651.                             WA_MaxWidth,-1,         WA_MaxHeight,-1,
  652.                             WA_IDCMP,               atk_setIDCMPs(),
  653.                             WA_Flags,               WFLG_SIZEGADGET | WFLG_DRAGBAR | WFLG_ACTIVATE,
  654.                             WA_Title,               title,
  655.                             WA_PubScreen,               screen,
  656.                             TAG_END);
  657.                                             /* Unlock the screen.  The window now acts as a lock on
  658.                                             ** the screen, and we do not need the screen after the
  659.                                             ** window has been closed.  */
  660.                 UnlockPubScreen(NULL, screen);
  661.                 if (tkhwnd)
  662.                     {
  663. #ifdef USE_CLIP_LAYER
  664.                     tkclipreg = clipWindowToBorders(tkhwnd);
  665. #endif
  666.                     if (win.type & TK_INDEX)
  667.                         {
  668.                         RGB_Flag=GL_FALSE;
  669.                         }
  670.                     if (win.type & TK_DOUBLE)
  671.                         {
  672.                         DB_Flag=GL_TRUE;
  673.                         }
  674.                     if (win.type & TK_ALPHA)
  675.                         {
  676.                         Alpha_Flag=GL_TRUE;
  677.                         }
  678.                     if (win.type & TK_INDIRECT)
  679.                         {
  680.                         DR_Flag=GL_FALSE;
  681.                         }
  682.                     win.context=AmigaMesaCreateContextTags(
  683.                             AMA_Window,(unsigned long)      tkhwnd,
  684.                             AMA_RastPort,(unsigned long)    tkhwnd->RPort,
  685.                             AMA_Screen,(unsigned long)      tkhwnd->WScreen,
  686.                             AMA_DoubleBuf,                      DB_Flag,
  687.                             AMA_RGBMode,                        RGB_Flag,
  688.                             AMA_AlphaFlag,                      Alpha_Flag,
  689.                             AMA_DirectRender,                   DR_Flag,
  690.                             AMA_Left,                           tkhwnd->BorderLeft,
  691.                             AMA_Bottom,                         tkhwnd->BorderBottom,
  692.                             AMA_Width,      win.width,
  693.                             AMA_Height,     win.height,
  694.                             TAG_DONE,0);
  695.  
  696.                     if (win.context == 0)
  697.                         tkQuit();
  698.                     AmigaMesaMakeCurrent(win.context,win.context->buffer);
  699.                     return GL_TRUE;
  700.                     }
  701.                 else
  702.                     {
  703.                     printf("Failed to open window.\n");
  704.                     return(Result);
  705.                     }
  706.                 }
  707.     }
  708.     else            /* full screen mode */
  709.     {
  710.         int dispID;
  711.         int width,height,depth;
  712.  
  713.         struct ScreenModeRequester* sreq;
  714.  
  715.         if (!(AslBase = OpenLibrary("asl.library",0)))
  716.         {
  717.         printf("Can't open asl.library\n");
  718.         tkQuit();
  719.         }
  720.         sreq = AllocAslRequestTags(ASL_ScreenModeRequest,ASLSM_InitialDisplayDepth,8,TAG_DONE,0);
  721.         if (!(AslRequestTags(sreq,TAG_DONE,0)))
  722.         tkQuit();
  723.         width = sreq->sm_DisplayWidth;
  724.         height = sreq->sm_DisplayHeight;
  725.         depth = sreq->sm_DisplayDepth;
  726.         dispID = sreq->sm_DisplayID;
  727.         FreeAslRequest(sreq);
  728.  
  729.         if (CyberGfxBase = OpenLibrary("cybergraphics.library",0))
  730.         {
  731.         int currentID;
  732.         if (IsCyberModeID(dispID))
  733.         {
  734.             currentID = GetCyberIDAttr(CYBRIDATTR_PIXFMT,dispID);
  735.             if ( (currentID != PIXFMT_LUT8) && (currentID != PIXFMT_RGB16) &&
  736.              (currentID != PIXFMT_RGB16PC) && (currentID != PIXFMT_ARGB32) &&
  737.              (currentID != PIXFMT_BGRA32) )
  738.             {
  739.              printf("Colormodel not supported in full screen mode\n");
  740.              tkQuit();
  741.             }
  742.         }
  743.         }
  744.  
  745. /*
  746.         if (CyberGfxBase = OpenLibrary("cybergraphics.library",0))
  747.         {
  748.         UWORD CModels[] = {PIXFMT_LUT8,
  749.                    PIXFMT_RGB16,
  750.                    PIXFMT_RGB16PC,
  751.                    PIXFMT_ARGB32,
  752.                    PIXFMT_BGRA32,
  753.                    -1};
  754.  
  755.         dispID = CModeRequestTags(NULL,CYBRMREQ_MinWidth,100,
  756.                            CYBRMREQ_MinHeight,100,
  757.                            CYBRMREQ_CModelArray,(ULONG)CModels,
  758.                            TAG_DONE,0);
  759.         if ((dispID == 0) || (dispID == -1))
  760.             tkQuit();
  761.         width = GetCyberIDAttr(CYBRIDATTR_WIDTH,dispID);
  762.         height = GetCyberIDAttr(CYBRIDATTR_HEIGHT,dispID);
  763.         depth = GetCyberIDAttr(CYBRIDATTR_DEPTH,dispID);
  764.         }
  765.         else
  766.         {
  767.         struct ScreenModeRequester* sreq;
  768.  
  769.         if (!(AslBase = OpenLibrary("asl.library",0)))
  770.         {
  771.             printf("Can't open asl.library\n");
  772.             tkQuit();
  773.         }
  774.         sreq = AllocAslRequestTags(ASL_ScreenModeRequest,TAG_DONE,0);
  775.         if (!(AslRequestTags(sreq,ASLSM_MinDepth,8,ASLSM_MaxDepth,8,TAG_DONE,0)))
  776.             tkQuit();
  777.         width = sreq->sm_DisplayWidth;
  778.         height = sreq->sm_DisplayHeight;
  779.         depth = sreq->sm_DisplayDepth;
  780.         dispID = sreq->sm_DisplayID;
  781.         FreeAslRequest(sreq);
  782.         }
  783. */
  784.  
  785.         if (!(customscreen = screen = OpenScreenTags(NULL,
  786.                       SA_Width,width,
  787.                       SA_Height,height,
  788.                       SA_Depth,depth,
  789.                       SA_DisplayID,dispID,
  790.                       SA_SharePens,TRUE,
  791.                       SA_Quiet,TRUE,
  792.                       TAG_DONE,0)))
  793.                 {
  794.                     printf("Failed to open the screen\n");
  795.                     tkQuit();
  796.                 }
  797.         SetRGB32(&screen->ViewPort,0,0,0,0);
  798.         tkhwnd = OpenWindowTags(NULL,
  799.                     WA_Left,0,
  800.                     WA_Top,0,
  801.                     WA_Width,width,
  802.                     WA_Height,height,
  803.                     WA_CloseGadget,TRUE,
  804.                     WA_DepthGadget,TRUE,
  805.                     WA_ReportMouse,TRUE,
  806.                     WA_RMBTrap,TRUE,
  807.                     WA_IDCMP,atk_setIDCMPs(),
  808.                     WA_Flags,WFLG_ACTIVATE,
  809.                     WA_Title,title,
  810.                     WA_PubScreen,screen,
  811.                     TAG_END);
  812.         if (tkhwnd == NULL)
  813.         {
  814.         printf("Failed to open the window\n");
  815.         tkQuit();
  816.         }
  817.         DB_Flag = GL_TRUE;
  818.         DR_Flag = GL_FALSE;
  819.         if (win.type & TK_INDEX)
  820.             {
  821.             RGB_Flag=GL_FALSE;
  822.             }
  823.         if (win.type & TK_ALPHA)
  824.             {
  825.             Alpha_Flag=GL_TRUE;
  826.             }
  827.         win.context=AmigaMesaCreateContextTags(
  828.                 AMA_Window,(unsigned long)      tkhwnd,
  829.                 AMA_RastPort,(unsigned long)    tkhwnd->RPort,
  830.                 AMA_Screen,(unsigned long)      tkhwnd->WScreen,
  831.                 AMA_DoubleBuf,                      DB_Flag,
  832.                 AMA_RGBMode,                        RGB_Flag,
  833.                 AMA_AlphaFlag,                      Alpha_Flag,
  834.                 AMA_DirectRender,                   DR_Flag,
  835.                 AMA_Fullscreen,                     GL_TRUE,
  836.                 AMA_Left,                           0,
  837.                 AMA_Bottom,                         0,
  838.                 AMA_Width,      win.width,
  839.                 AMA_Height,     win.height,
  840.                 TAG_DONE,0);
  841.  
  842.         if (win.context == 0)
  843.         tkQuit();
  844.         AmigaMesaMakeCurrent(win.context,win.context->buffer);
  845.         return GL_TRUE;
  846.     }
  847. }
  848.  
  849.  
  850.  
  851. /******************************************************************************/
  852.  
  853. /*
  854.  * You cannot just call DestroyWindow() here.  The programs do not expect
  855.  * tkQuit() to return;  DestroyWindow() just sends a WM_DESTROY message
  856.  */
  857.  
  858. void tkQuit(void)
  859. {
  860.     tkCloseWindow();
  861.    exit(0);    /*TODO*/
  862. }
  863.  
  864. /******************************************************************************/
  865.  
  866. void tkSetOneColor(int index, float r, float g, float b)
  867. {
  868.     AmigaMesaSetOneColor(win.context,index,r,g,b);
  869. }
  870.  
  871. void tkSetFogRamp(int density, int startIndex)
  872. {
  873.     int i, j, k, intensity, fogValues, colorValues;
  874.  
  875.     fogValues = 1 << density ;
  876.     colorValues = 1 << startIndex ;
  877.     for( i = 0 ; i < colorValues; i++ ) {
  878.         for( j = 0 ; j < fogValues; j++ ) {
  879.             k = i * fogValues + j;
  880.  
  881.             intensity = i * fogValues + j * colorValues;
  882.  
  883.             if( intensity > 0xff )
  884.                 intensity = 0xff;
  885.  
  886.             tkSetOneColor(k,(float)intensity/255,(float)intensity/255,(float)intensity/255);
  887.         }
  888.     }
  889. }
  890.  
  891. void tkSetGreyRamp(void)
  892. {
  893.     int Count,i;
  894.     float intensity;
  895.     Count = tkGetColorMapSize();
  896.     for( i = 0 ; i < Count ; i++ )
  897.     {
  898.         intensity = (float)(((double)i / (double)(Count-1)) * (double)255.0 + (double)0.5);
  899.         tkSetOneColor(i,(float)intensity/255,(float)intensity/255,(float)intensity/255);
  900.     }
  901. }
  902.  
  903. void tkSetRGBMap( int Size, float *Values )
  904. {
  905.     int i;
  906.  
  907.     for (i=0; i<Size; i++) tkSetOneColor(i,Values[i*3],Values[i*3+1],Values[i*3+2]);
  908. }
  909.  
  910.  
  911. void tkSetOverlayMap(int size, float *rgb)
  912. {
  913. printf("tkSetOverlayMap(%d,%f) TODO\n",size,rgb);
  914. }
  915. /******************************************************************************/
  916.  
  917. void tkSwapBuffers(void)
  918. {
  919.   SwapBuffersCalled = -1;
  920.   AmigaMesaSwapBuffers(win.context);
  921. }
  922.  
  923. /******************************************************************************/
  924.  
  925. GLint tkGetColorMapSize(void)
  926. {
  927.     return(256);
  928. }
  929.  
  930. void tkGetMouseLoc(int *x, int *y)
  931. {
  932.     *x = tkhwnd->MouseX-tkhwnd->BorderLeft;
  933.     *y = tkhwnd->MouseY-tkhwnd->BorderTop;
  934. }
  935.  
  936.  
  937. GLenum tkGetDisplayMode(void)
  938. {
  939.     return win.type;
  940. }
  941.  
  942. GLenum tkSetWindowLevel(GLenum level)
  943. {
  944. printf("tkSetWindowLevel(%d) TODO\n",level);
  945. return GL_FALSE;
  946. }
  947.  
  948. /*
  949. TK_RGBImageRec *tkRGBImageLoad(char *fileName)
  950. {
  951. printf("tkRGBImageLoad(%s) TODO\n",fileName);
  952. }
  953. */
  954.  
  955. void tkGetSystem(TKenum type, void *ptr)
  956. {
  957. printf("tkGetSystem(%d,*ptr) TODO\n",type);
  958. /* getset.c */
  959. }
  960. #undef USE_CLIP_LAYER
  961.  
  962.