home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / agl103p.lha / src / agl / window.c < prev   
Encoding:
C/C++ Source or Header  |  1994-12-09  |  23.7 KB  |  1,113 lines

  1. /******************************************************************************
  2.  
  3. Copyright © 1994 Jason Weber
  4. All Rights Reserved
  5.  
  6. $Id: window.c,v 1.2.1.6 1994/12/09 05:29:56 jason Exp $
  7.  
  8. $Log: window.c,v $
  9.  * Revision 1.2.1.6  1994/12/09  05:29:56  jason
  10.  * fixed bitmap crashes
  11.  *
  12.  * Revision 1.2.1.5  1994/11/18  07:49:22  jason
  13.  * added foreground() check
  14.  *
  15.  * Revision 1.2.1.4  1994/11/16  06:31:09  jason
  16.  * added border support
  17.  * fixed double-buffering
  18.  *
  19.  * Revision 1.2.1.3  1994/09/13  03:53:54  jason
  20.  * use true double-buffering for single window
  21.  * added prefsize
  22.  * added minsize(), maxsize()
  23.  * fixed border corrections
  24.  *
  25.  * Revision 1.2.1.2  1994/04/06  02:42:55  jason
  26.  * winset() calls reset_matrix_pointers()
  27.  *
  28.  * Revision 1.2.1.1  1994/03/29  05:41:32  jason
  29.  * Added RCS Header
  30.  *
  31.  * Revision 1.2.1.1  2002/03/26  22:04:26  jason
  32.  * Added RCS Header
  33.  *
  34.  * Revision 1.2.1.1  2002/03/26  22:00:51  jason
  35.  * RCS/agl.h,v
  36.  *
  37.  
  38. ******************************************************************************/
  39.  
  40.  
  41. #define NOT_EXTERN
  42. #include"agl.h"
  43.  
  44. #define COOL_BORDERS    TRUE
  45.  
  46. #define BORDERSIZEX    4        /* displace lower left for border */
  47. #define BORDERSIZEY    11
  48.  
  49. #define BORDERSIZE_TITLEY    12
  50.  
  51. short NextWindow_Initialized=FALSE;
  52. short NextBordered=TRUE;
  53. short NextSizeable=TRUE;
  54. short Gfx_Initialized=FALSE;
  55. short NumberWindows=0;
  56.  
  57. short Foregrounded=FALSE;
  58.  
  59. struct Library *ConsoleDevice,*DiskfontBase,*LayersBase;
  60.  
  61.  
  62. /*******************************************************************************
  63. void    gversion(char *string)
  64.  
  65. *******************************************************************************/
  66. /*PROTOTYPE*/
  67. void gversion(char *string)
  68.     {
  69.     sprintf(string,"GLAMECS-1.0");
  70.     }
  71.  
  72.  
  73. /*******************************************************************************
  74. long    getgdesc(long inquiry)
  75.  
  76. *******************************************************************************/
  77. /*PROTOTYPE*/
  78. long getgdesc(long inquiry)
  79.     {
  80.     long value=0;
  81.  
  82.     switch(inquiry)
  83.         {
  84.         case GD_XPMAX:
  85.             value=ScreenWidth;
  86.             break;
  87.         case GD_YPMAX:
  88.             value=ScreenHeight;
  89.             break;
  90.         case GD_BITS_NORM_SNG_CMODE:
  91.             value=ScreenDeep;
  92.             break;
  93.         case GD_BITS_NORM_DBL_CMODE:
  94.             value=ScreenDeep;
  95.             break;
  96.         case GD_NVERTEX_POLY:
  97.             value=MAX_POLY_VERTS;
  98.             break;
  99.          }
  100.  
  101.     return value;
  102.     }
  103.  
  104.  
  105. /******************************************************************************
  106. void    foreground(void)
  107.  
  108. ******************************************************************************/
  109. /*PROTOTYPE*/
  110. void foreground(void)
  111.     {
  112.     Foregrounded=TRUE;
  113.     }
  114.  
  115.  
  116. /******************************************************************************
  117. void    cmode(void)
  118.  
  119. ******************************************************************************/
  120. /*PROTOTYPE*/
  121. void cmode(void)
  122.     {
  123.     RGBmodeOn[CurrentWid]=FALSE;
  124.     }
  125.  
  126.  
  127. /******************************************************************************
  128. void    RGBmode(void)
  129.  
  130. ******************************************************************************/
  131. /*PROTOTYPE*/
  132. void RGBmode(void)
  133.     {
  134.     RGBmodeOn[CurrentWid]=TRUE;
  135.     }
  136.  
  137.  
  138. /******************************************************************************
  139. void    doublebuffer(void)
  140.  
  141. ******************************************************************************/
  142. /*PROTOTYPE*/
  143. void doublebuffer(void)
  144.     {
  145.     DoubleBuffered[CurrentWid]=TRUE;
  146.     }
  147.  
  148.  
  149. /******************************************************************************
  150. void    singlebuffer(void)
  151.  
  152. ******************************************************************************/
  153. /*PROTOTYPE*/
  154. void singlebuffer(void)
  155.     {
  156.     DoubleBuffered[CurrentWid]=FALSE;
  157.     }
  158.  
  159.  
  160. /******************************************************************************
  161. long    getdisplaymode(void)
  162.  
  163.     0 = RGB single
  164.     1 = single
  165.     2 = double
  166.     5 = RGB double
  167. ******************************************************************************/
  168. /*PROTOTYPE*/
  169. long getdisplaymode(void)
  170.     {
  171.     return 1+DoubleBuffered[CurrentWid];
  172.     }
  173.  
  174.  
  175. /******************************************************************************
  176. void    clone_new_bitmap(void)
  177.  
  178.     if true double-buffing (single window), copy visible bitmap to drawbuffer
  179.     intended to update window borders and screen background in backbuffer
  180. ******************************************************************************/
  181. /*PROTOTYPE*/
  182. void clone_new_bitmap(void)
  183.     {
  184.     struct BitMap *old_bitmap;
  185.  
  186.     if(NumberWindows==1 && DoubleBufferSet[CurrentWid])
  187.         {
  188.  
  189. #if FALSE
  190.         BltBitMap(
  191.                     &BackBitMap[CurrentWid],0,0,
  192.                     GLScreen->RastPort.BitMap,0,0,
  193.                                             (long)ScreenWidth,(long)ScreenHeight,0xC0,0xFF,NULL);
  194.  
  195.         old_bitmap=GLWindow[CurrentWid]->RPort->BitMap;
  196.         memcpy(GLWindow[CurrentWid]->RPort,&BackRPort[CurrentWid],sizeof(struct RastPort));
  197.  
  198.         GLWindow[CurrentWid]->RPort->BitMap=old_bitmap;
  199. #endif
  200.  
  201. #if FALSE
  202.         BltBitMap(
  203.                     GLScreen->RastPort.BitMap,0,0,
  204.                     &BackBitMap[CurrentWid],0,0,
  205.                                         (long)ScreenWidth,(long)ScreenHeight,0xC0,0xFF,NULL);
  206.  
  207.         old_bitmap=BackRPort[CurrentWid].BitMap;
  208.         memcpy(&BackRPort[CurrentWid],GLWindow[CurrentWid]->RPort,sizeof(struct RastPort));
  209.         BackRPort[CurrentWid].BitMap=old_bitmap;
  210. #endif
  211.  
  212. #if TRUE
  213.         BltBitMap(
  214.                     VisibleRPort->BitMap,0,0,
  215.                     DrawRPort->BitMap,0,0,
  216.                                         (long)ScreenWidth,(long)ScreenHeight,0xC0,0xFF,NULL);
  217.  
  218. /*
  219.         old_bitmap=DrawRPort->BitMap;
  220.         memcpy(DrawRPort,VisibleRPort,sizeof(struct RastPort));
  221.         DrawRPort->BitMap=old_bitmap;
  222. */
  223. #endif
  224.         }
  225.     }
  226.  
  227.  
  228. /*******************************************************************************
  229. void    gconfig(void)
  230.  
  231. *******************************************************************************/
  232. /*PROTOTYPE*/
  233. void gconfig(void)
  234.     {
  235.     long d,dd,abort=FALSE;
  236.  
  237.     if(DoubleBuffered[CurrentWid] && !DoubleBufferSet[CurrentWid])
  238.         {
  239.         memcpy(&BackRPort[CurrentWid],GLWindow[CurrentWid]->RPort,sizeof(struct RastPort));
  240.  
  241.         InitBitMap(&BackBitMap[CurrentWid],(long)ScreenDeep,(long)ScreenWidth,(long)ScreenHeight);
  242.         for(d=0;d<ScreenDeep && !abort;d++)
  243.             {
  244.             if((BackBitMap[CurrentWid].Planes[d]=(PLANEPTR)AllocRaster((ULONG)ScreenWidth,
  245.                                                                     (ULONG)ScreenHeight))==NULL)
  246.                 abort=TRUE;
  247.             }
  248.  
  249.         if(abort)
  250.             {
  251.             GL_error("Could not allocate backbuffer");
  252.  
  253.             for(dd=0;dd<d-1;dd++)
  254.                 FreeRaster(BackBitMap[CurrentWid].Planes[dd],
  255.                                                         (ULONG)ScreenWidth,(ULONG)ScreenHeight);
  256.             }
  257.         else
  258.             {
  259. /*             clone_new_bitmap(); */
  260.  
  261.             BltBitMap(
  262.                         GLScreen->RastPort.BitMap,0,0,
  263.                         &BackBitMap[CurrentWid],0,0,
  264.                                             (long)ScreenWidth,(long)ScreenHeight,0xC0,0xFF,NULL);
  265.  
  266. /*             memcpy(&BackRPort[CurrentWid],GLWindow[CurrentWid]->RPort,sizeof(struct RastPort)); */
  267.  
  268.             BackRPort[CurrentWid].BitMap= &BackBitMap[CurrentWid];
  269.             DoubleBufferSet[CurrentWid]=TRUE;
  270.             }
  271.         }
  272.  
  273.     if(!DoubleBuffered[CurrentWid] && DoubleBufferSet[CurrentWid])
  274.         {
  275.         for(d=0;d<ScreenDeep;d++)
  276.             FreeRaster(BackBitMap[CurrentWid].Planes[d],(ULONG)ScreenWidth,(ULONG)ScreenHeight);
  277.  
  278.         DoubleBufferSet[CurrentWid]=FALSE;
  279.         }
  280.  
  281.     RGBmodeSet[CurrentWid]=RGBmodeOn[CurrentWid];
  282.  
  283.     set_rasterport();
  284.     }
  285.  
  286.  
  287. /*******************************************************************************
  288. void    swapbuffers(void)
  289.  
  290. *******************************************************************************/
  291. /*PROTOTYPE*/
  292. void swapbuffers(void)
  293.     {
  294.     struct BitMap *swap;
  295.  
  296.     long x,y;
  297.  
  298.     if(DoubleBuffered[CurrentWid])
  299.         {
  300.         if(NumberWindows==1)
  301.             {
  302.             /* make sure any window movements/sizing taken care of */
  303.             update_queue(-1);
  304.  
  305.             /* update to make visible */
  306.             MakeScreen(GLScreen);
  307.             RethinkDisplay();
  308.  
  309.             /* swap bitmaps */
  310.             swap=VisibleRPort->BitMap;
  311.             VisibleRPort->BitMap=DrawRPort->BitMap;
  312.  
  313.             if(DrawRPort->BitMap==swap)
  314.                 GL_error("swapbuffers() software error, bitmaps are the same");
  315.  
  316.             DrawRPort->BitMap=swap;
  317.  
  318.             GLScreen->RastPort.BitMap=swap;
  319.             GLScreen->ViewPort.RasInfo->BitMap=swap;
  320.  
  321. /*             GLWindow[CurrentWid]->RPort->BitMap=VisibleRPort->BitMap; */
  322.  
  323.             if(RedoBorder[CurrentWid])
  324.                 {
  325.                 drawborder(CurrentWid,0);
  326.                 RedoBorder[CurrentWid]=FALSE;
  327.                 }
  328.             }
  329.         else
  330.             {
  331.             deactivate_clipping(CurrentWid);
  332. /*
  333.             OwnBlitter();
  334.             WaitBlit();
  335.             BltBitMap(DrawRPort->BitMap,0,0,VisibleRPort->BitMap,0,0,CurrentWidth,CurrentHeight,
  336.                                                                                     0xC0,0xFF,NULL);
  337.             DisownBlitter();
  338. */
  339.             if(COOL_BORDERS && Bordered[CurrentWid])
  340.                 {
  341.                 x=BorderWidth;
  342.                 y=BorderWidth+BorderHeight;
  343.                 }
  344.             else
  345.                 {
  346.                 x=0;
  347.                 y=0;
  348.                 }
  349.  
  350.             ClipBlit(DrawRPort,x,y,VisibleRPort,x,y,CurrentWidth,CurrentHeight,0xC0);
  351.  
  352.             activate_clipping(CurrentWid);
  353.             }
  354.         }
  355.     else
  356.         GL_error("swapbuffers() called in single buffer mode");
  357.     }
  358.  
  359.  
  360. /*******************************************************************************
  361. void    set_rasterport(void)
  362.  
  363. *******************************************************************************/
  364. /*PROTOTYPE*/
  365. void set_rasterport(void)
  366.     {
  367.     VisibleRPort=GLWindow[CurrentWid]->RPort;
  368.  
  369.     if(DoubleBuffered[CurrentWid])
  370.         DrawRPort= &BackRPort[CurrentWid];
  371.     else
  372.         DrawRPort=VisibleRPort;
  373.     }
  374.  
  375.  
  376. /*******************************************************************************
  377. void    winpush(void)
  378.  
  379. *******************************************************************************/
  380. /*PROTOTYPE*/
  381. void winpush(void)
  382.     {
  383.     WindowToBack(GLWindow[CurrentWid]);
  384.     }
  385.  
  386.  
  387. /*******************************************************************************
  388. void    winpop(void)
  389.  
  390. *******************************************************************************/
  391. /*PROTOTYPE*/
  392. void winpop(void)
  393.     {
  394.     WindowToFront(GLWindow[CurrentWid]);
  395.     }
  396.  
  397.  
  398. /*******************************************************************************
  399. long    winget(void)
  400.  
  401. *******************************************************************************/
  402. /*PROTOTYPE*/
  403. long winget(void)
  404.     {
  405.     return CurrentWid;
  406.     }
  407.  
  408.  
  409. /*******************************************************************************
  410. void    winset(long wid)
  411.  
  412. *******************************************************************************/
  413. /*PROTOTYPE*/
  414. void winset(long wid)
  415.     {
  416.     static char string[100];
  417.  
  418.     if(GLWindow[wid]==NULL)
  419.         {
  420.         if(wid)
  421.             {
  422.             sprintf(string,"winset(%d): window not open",wid);
  423.             GL_error(string);
  424.             }
  425.  
  426.         CurrentWid=0;
  427.         }
  428.     else
  429.         {
  430.         CurrentWid=wid;
  431.  
  432.         set_rasterport();
  433.         get_dimensions(CurrentWid,FALSE,&CurrentPosX,&CurrentPosY,&CurrentWidth,&CurrentHeight);
  434.  
  435.         reset_matrix_pointers();
  436.         }
  437.     }
  438.  
  439.  
  440. /******************************************************************************
  441. short    get_dimensions(long wid,long whole,long *x,long *y,long *lenx,long *leny)
  442.  
  443.     if whole==TRUE, get whole window dimensions including border
  444.  
  445.     returns FALSE if window is not opened
  446. ******************************************************************************/
  447. /*PROTOTYPE*/
  448. short get_dimensions(long wid,long whole,long *x,long *y,long *lenx,long *leny)
  449.     {
  450.     if(GLWindow[wid]==NULL)
  451.         return FALSE;
  452.  
  453.     if(COOL_BORDERS || !Bordered[wid])
  454.         {
  455.         *lenx=GLWindow[wid]->Width;
  456.         *leny=GLWindow[wid]->Height;
  457.         }
  458.     else
  459.         {
  460.         *lenx=GLWindow[wid]->GZZWidth;
  461.         *leny=GLWindow[wid]->GZZHeight;
  462.         }
  463.  
  464.     *x=GLWindow[wid]->LeftEdge+GLWindow[wid]->BorderLeft;
  465.     *y=GLWindow[wid]->TopEdge+GLWindow[wid]->BorderTop;
  466.  
  467.     *y=ScreenHeight-(*y)- *leny;
  468.  
  469. #if COOL_BORDERS
  470.  
  471.     if(Bordered[wid] && !whole)
  472.         {
  473.         *x+=BorderWidth;
  474.         *y+=BorderWidth;
  475.  
  476.         *lenx-=2*BorderWidth;
  477.         *leny-=2*BorderWidth+BorderHeight;
  478.         }
  479.  
  480. #endif
  481.  
  482.     return TRUE;
  483.     }
  484.  
  485.  
  486. /*******************************************************************************
  487. void    sleep(long seconds)
  488.  
  489. *******************************************************************************/
  490. /*PROTOTYPE*/
  491. void sleep(long seconds)
  492.     {
  493.     Delay(60*seconds);
  494.     }
  495.  
  496.  
  497. /******************************************************************************
  498. void    minsize(long x,long y)
  499.  
  500. ******************************************************************************/
  501. /*PROTOTYPE*/
  502. void minsize(long x,long y)
  503.     {
  504.     NextWindow.MinWidth=x;
  505.     NextWindow.MinHeight=y;
  506.     }
  507.  
  508.  
  509. /******************************************************************************
  510. void    maxsize(long x,long y)
  511.  
  512. ******************************************************************************/
  513. /*PROTOTYPE*/
  514. void maxsize(long x,long y)
  515.     {
  516.     NextWindow.MaxWidth=x;
  517.     NextWindow.MaxHeight=y;
  518.     }
  519.  
  520.  
  521. /*******************************************************************************
  522. void    prefposition(long x1,long x2,long y1,long y2)
  523.  
  524. *******************************************************************************/
  525. /*PROTOTYPE*/
  526. void prefposition(long x1,long x2,long y1,long y2)
  527.     {
  528.     if(!NextWindow_Initialized)
  529.         initialize_nextwindow();
  530.  
  531.     NextWindow.LeftEdge=x1;
  532.     NextWindow.Width=    x2-x1+1;
  533.  
  534.     NextWindow.TopEdge=    ScreenHeight-1-y2;
  535.     NextWindow.Height=    y2-y1+1;
  536.  
  537.     if(NextWindow.Flags&WINDOWSIZING)
  538.         NextWindow.Flags^=WINDOWSIZING;
  539.  
  540. /*
  541.     printf("X %3d   %3d  Y %3d   %3d\n",x1,x2,y1,y2);
  542.     printf("L %3d W %3d  T %3d H %3d\n\n",NextWindow.LeftEdge,NextWindow.Width,
  543.                                                             NextWindow.TopEdge,NextWindow.Height);
  544. */
  545.     NextSizeable=FALSE;
  546.     }
  547.  
  548.  
  549. /******************************************************************************
  550. void    prefsize(long x,long y)
  551.  
  552. ******************************************************************************/
  553. /*PROTOTYPE*/
  554. void prefsize(long x,long y)
  555.     {
  556.     if(!NextWindow_Initialized)
  557.         initialize_nextwindow();
  558.  
  559. #if COOL_BORDERS
  560.  
  561.     prefposition(BorderWidth,BorderWidth+x-1,BorderWidth,BorderWidth+BorderHeight+y-1);
  562.  
  563. #else
  564.  
  565.     prefposition(BORDERSIZEX,BORDERSIZEX+x-1,1,1+y-1);
  566.  
  567.     NextWindow.Flags|=WINDOWSIZING;
  568.  
  569. #endif
  570.  
  571.     NextSizeable=TRUE;
  572.     }
  573.  
  574.  
  575. /*******************************************************************************
  576. void    noborder(void)
  577.  
  578. *******************************************************************************/
  579. /*PROTOTYPE*/
  580. void noborder(void)
  581.     {
  582.     if(!NextWindow_Initialized)
  583.         initialize_nextwindow();
  584.  
  585.     NextBordered=FALSE;
  586.  
  587. #if !COOL_BORDERS
  588.  
  589.     NextWindow.Flags|=BORDERLESS;
  590.  
  591. #endif
  592.     }
  593.  
  594.  
  595. /******************************************************************************
  596. void    winposition(long x1,long x2,long y1,long y2)
  597.  
  598. ******************************************************************************/
  599. /*PROTOTYPE*/
  600. void winposition(long x1,long x2,long y1,long y2)
  601.     {
  602.     long posx,posy,lenx,leny;
  603.     long sizex,sizey;
  604.  
  605. /*     printf("\nwinposition(%d,%d,%d,%d)\n",x1,x2,y1,y2); */
  606.  
  607.     get_dimensions(CurrentWid,FALSE,&posx,&posy,&lenx,&leny);
  608.     posy=ScreenHeight-posy-leny;
  609.  
  610. /*     printf(" %d,%d %d,%d\n",posx,posy,lenx,leny); */
  611.  
  612.     sizex=abs(x2-x1)+1-lenx;
  613.     sizey=abs(y2-y1)+1-leny;
  614.  
  615.     lenx+=sizex;
  616.     leny+=sizey;
  617.  
  618.     y1=ScreenHeight-y1-leny;
  619.     y2=ScreenHeight-y2-leny;
  620.  
  621.     if(x1>x2)
  622.         x1=x2;
  623.     if(y1<y2)
  624.         y1=y2;
  625.  
  626.     x1-=posx;
  627.     y1-=posy;
  628.  
  629. /*     printf(" %d,%d+%d,%d %d,%d+%d,%d\n",posx,posy,x1,y1,lenx,leny,sizex,sizey); */
  630.  
  631.     /* in two passes to prevent possible crossing of screen borders */
  632.     do_move_and_resize(CurrentWid,x1<0,x1,0,sizex,0);
  633.     do_move_and_resize(CurrentWid,y1<0,0,y1,0,sizey);
  634.  
  635.     winset(CurrentWid);
  636.  
  637.     get_dimensions(CurrentWid,FALSE,&posx,&posy,&lenx,&leny);
  638. /*     printf(" %d,%d %d,%d ->",posx,posy,lenx,leny); */
  639.  
  640.     posy=ScreenHeight-posy-leny;
  641.  
  642. /*     printf("%d\n",posy); */
  643.     }
  644.  
  645.  
  646. /******************************************************************************
  647. void    winmove(long orgx,long orgy)
  648.  
  649. ******************************************************************************/
  650. /*PROTOTYPE*/
  651. void winmove(long orgx,long orgy)
  652.     {
  653.     long dx,dy;
  654.  
  655.     dx=orgx-CurrentPosX;
  656.     dy=orgy-CurrentPosY;
  657.  
  658.     do_move_and_resize(CurrentWid,FALSE,dx,-dy,0,0);
  659.  
  660.     winset(CurrentWid);
  661.  
  662. /*     MoveWindow(GLWindow[CurrentWid],dx,-dy); */
  663.     }
  664.  
  665.  
  666. /******************************************************************************
  667. void    wintitle(char *name)
  668.  
  669. ******************************************************************************/
  670. /*PROTOTYPE*/
  671. void wintitle(char *name)
  672.     {
  673.     strcpy(TitleList[CurrentWid],name);
  674.  
  675.     drawborder(CurrentWid,0);
  676.  
  677.     if(NumberWindows==1 && DoubleBufferSet[CurrentWid])
  678.         RedoBorder[CurrentWid]=TRUE;
  679.     }
  680.  
  681.  
  682. /******************************************************************************
  683. void    getsize(long *x,long *y)
  684.  
  685. ******************************************************************************/
  686. /*PROTOTYPE*/
  687. void getsize(long *x,long *y)
  688.     {
  689.     *x=CurrentWidth;
  690.     *y=CurrentHeight;
  691.     }
  692.  
  693.  
  694. /******************************************************************************
  695. void    getorigin(long *x,long *y)
  696.  
  697. ******************************************************************************/
  698. /*PROTOTYPE*/
  699. void getorigin(long *x,long *y)
  700.     {
  701.     *x=CurrentPosX;
  702.     *y=CurrentPosY;
  703.     }
  704.  
  705.  
  706. /*******************************************************************************
  707. long    winopen(char *title)
  708.  
  709. *******************************************************************************/
  710. /*PROTOTYPE*/
  711. long winopen(char *title)
  712.     {
  713.     long wid=1;
  714.  
  715.     if(!Foregrounded)
  716.         {
  717.         GL_error("AGL cannot background process: foreground() assumed");
  718.  
  719.         Foregrounded=TRUE;
  720.         }
  721.  
  722.     if(!NextWindow_Initialized)
  723.         initialize_nextwindow();
  724.  
  725.     while(wid<MAX_WINDOWS && GLWindow[wid])
  726.         wid++;
  727.  
  728.     if(wid==MAX_WINDOWS)
  729.         {
  730.         GL_error("Too many windows");
  731.         return -1;
  732.         }
  733.  
  734.     if(!Gfx_Initialized)
  735.         gfxinit();
  736.  
  737.     DefaultWindow.Screen=GLScreen;
  738.     NextWindow.Screen=GLScreen;
  739.  
  740.     Sizeable[wid]=NextSizeable;
  741.     Bordered[wid]=NextBordered;
  742.     NextSizeable=TRUE;
  743.     NextBordered=TRUE;
  744.  
  745.     Maximization[wid][0]=BorderWidth;
  746.     Maximization[wid][1]=BorderWidth;
  747.     Maximization[wid][2]=ScreenWidth-2*BorderWidth;
  748.     Maximization[wid][3]=ScreenHeight-2*BorderWidth-BorderHeight;
  749.     
  750. #if COOL_BORDERS
  751.  
  752.     NextWindow.Flags|=BORDERLESS;
  753.  
  754. #endif
  755.  
  756.     if(Bordered[wid])
  757.         {
  758. #if COOL_BORDERS
  759.  
  760.         NextWindow.LeftEdge-=BorderWidth;
  761.         NextWindow.TopEdge-=BorderWidth+BorderHeight;
  762.  
  763.         NextWindow.Width+=BorderWidth*2;
  764.         NextWindow.Height+=BorderHeight+BorderWidth*2;
  765.  
  766. #else
  767.  
  768.         NextWindow.Flags|= ACTIVATE | WINDOWDRAG | WINDOWCLOSE | GIMMEZEROZERO;
  769.  
  770.         NextWindow.LeftEdge-=BORDERSIZEX;
  771.         NextWindow.TopEdge-=BORDERSIZEY;
  772.  
  773.         NextWindow.Height+=BORDERSIZE_TITLEY;
  774.         NextWindow.Width+=BORDERSIZEX*2;
  775.  
  776.         NextWindow.Title=TitleList[wid];
  777.  
  778. #endif
  779.  
  780.         strcpy(TitleList[wid],title);
  781.         }
  782.  
  783.     GLWindow[wid]=(struct Window *)OpenWindow(&NextWindow);
  784.     if(GLWindow[wid]==NULL)
  785.          {
  786.          printf("Window won't open: \"%s\"\n",title);
  787.          CloseScreen(GLScreen);
  788.          CloseLibrary((void *)IntuitionBase);
  789.          CloseLibrary((void *)GfxBase);
  790.          exit(400);
  791.          }
  792.  
  793.     winset(wid);
  794.  
  795.     InitArea(&AInfo[wid],AreaBuffer[wid],MAX_POLY_VERTS);
  796.     DrawRPort->AreaInfo= &AInfo[wid];
  797.     TempBuffer[wid]=(PLANEPTR)AllocRaster((ULONG)ScreenWidth,(ULONG)ScreenHeight);
  798.     if(TempBuffer[wid]==NULL)
  799.         {
  800.         GL_error("Error alocating Poly Space");
  801.         return -1;
  802.         }
  803.  
  804.     DrawRPort->TmpRas=(struct TmpRas *)
  805.                 InitTmpRas(&TempRaster[wid],TempBuffer[wid],RASSIZE(ScreenWidth,ScreenHeight));
  806.  
  807.     SetDrMd(DrawRPort,JAM1);
  808.  
  809.     Dimensions[wid]=2;
  810.  
  811.     ortho2(-0.5,CurrentWidth-0.5,-0.5,CurrentHeight-0.5);
  812.  
  813.     if(Bordered[wid])
  814.         drawborder(wid,0);
  815.  
  816.     viewport(0,CurrentWidth-1,0,CurrentHeight-1);
  817.  
  818.     initialize_nextwindow();
  819.  
  820.     NumberWindows++;
  821.  
  822.     return wid;
  823.     }
  824.  
  825.  
  826. /******************************************************************************
  827. void    gexit(void)
  828.  
  829.     set as trap to occur on completion of execution
  830.  
  831.     closes all opened windows
  832.     should result in complete release of all of AGL's resources
  833. ******************************************************************************/
  834. /*PROTOTYPE*/
  835. void gexit(void)
  836.     {
  837.     long m;
  838.  
  839.     printf("KILL AGL\n");
  840.  
  841.     for(m=1;m<MAX_WINDOWS;m++)
  842.         if(GLWindow[m])
  843.             winclose(m);
  844.     }
  845.  
  846.  
  847. /*******************************************************************************
  848. void    winclose(long wid)
  849.  
  850. *******************************************************************************/
  851. /*PROTOTYPE*/
  852. void winclose(long wid)
  853.     {
  854.     char string[100];
  855.     long temp_wid;
  856.     short m,on;
  857.  
  858.     NumberWindows--;
  859.  
  860.     if(GLWindow[wid]==NULL)
  861.         {
  862.         sprintf(string,"cannot close unopened window %d",wid);
  863.         GL_error(string);
  864.         }
  865.     else
  866.         {
  867.         /* remove backbuffer */
  868.         temp_wid=CurrentWid;
  869.  
  870.         winset(wid);
  871.         singlebuffer();
  872.         gconfig();
  873.  
  874.         if(GLWindow[temp_wid])
  875.             winset(temp_wid);
  876.  
  877.         FreeRaster(TempBuffer[wid],(ULONG)ScreenWidth,(ULONG)ScreenHeight);
  878.  
  879.         CloseWindow(GLWindow[wid]);
  880.         GLWindow[wid]=NULL;
  881.         }
  882.  
  883.     on=0;
  884.     for(m=1;m<MAX_WINDOWS;m++)
  885.         if(GLWindow[m])
  886.             on=1;
  887.  
  888.     if(!on)
  889.         {
  890. #if MICE
  891.         free_mousesprite();
  892.         stop_gameport();
  893. #endif
  894.  
  895.         if(FontPtr)
  896.             CloseFont(FontPtr);
  897.          CloseLibrary((void *)DiskfontBase);
  898.  
  899.         CloseScreen(GLScreen);
  900.          CloseLibrary((void *)IntuitionBase);
  901.          CloseLibrary((void *)GfxBase);
  902.         }
  903.     }
  904.  
  905.  
  906. /*******************************************************************************
  907. void    initialize_nextwindow(void)
  908.  
  909. *******************************************************************************/
  910. /*PROTOTYPE*/
  911. void initialize_nextwindow(void)
  912.     {
  913.     static short first=1;
  914.     short m;
  915.  
  916.     if(first)
  917.         {
  918.         BorderWidth=4;
  919.         BorderHeight=11;
  920.  
  921.         for(m=0;m<MAX_WINDOWS;m++)
  922.             {
  923.             GLWindow[m]=NULL;
  924.             Bordered[m]=FALSE;
  925.             RedoBorder[m]=FALSE;
  926.             DoubleBuffered[m]=FALSE;
  927.             DoubleBufferSet[m]=FALSE;
  928.             Clipped[m]=FALSE;
  929.             }
  930.  
  931.         first=FALSE;
  932.         }
  933.  
  934.     memcpy(&NextWindow,&DefaultWindow,sizeof(struct NewWindow));
  935.  
  936.     NextWindow_Initialized=TRUE;
  937.     }
  938.  
  939.  
  940. /******************************************************************************
  941. long    AGLconfig(short screenx,short screeny,short bitplanes)
  942.  
  943.     this the only AGL specific command
  944.  
  945.     it configures the screen for all windows to use
  946.  
  947.     returns TRUE if sucessful, ie. no GL command had been previously issued
  948.  
  949. ******************************************************************************/
  950. /*PROTOTYPE*/
  951. long AGLconfig(short screenx,short screeny,short bitplanes)
  952.     {
  953.     if(Gfx_Initialized)
  954.         {
  955.         GL_error("AGLconfig(): must be called before all GL commands\n");
  956.         return FALSE;
  957.         }
  958.     else
  959.         {
  960.         ScreenDef.ViewModes=NULL;
  961.  
  962.         if(screeny>225)
  963.             {
  964. /*             printf("HiRes Lace\n"); */
  965.  
  966.             if(screenx>720)
  967.                 screenx=720;
  968.  
  969.             if(screeny>450)
  970.                 screeny=450;
  971.  
  972.             if(bitplanes>4)
  973.                 bitplanes=4;
  974.  
  975.             ScreenDef.ViewModes|=HIRES;
  976.             ScreenDef.ViewModes|=LACE;
  977.             }
  978.         else
  979.             {
  980. /*             printf("LoRes\n"); */
  981.  
  982.             if(screenx>360)
  983.                 screenx=360;
  984.  
  985.             if(screeny>225)
  986.                 screeny=225;
  987.  
  988.             if(bitplanes>5)
  989.                 bitplanes=5;
  990.             }
  991.  
  992.         ScreenDef.Width=screenx;
  993.         ScreenDef.Height=screeny;
  994.         ScreenDef.Depth=bitplanes;
  995.  
  996.         ScreenWidth=ScreenDef.Width;
  997.         ScreenHeight=ScreenDef.Height;
  998.         ScreenDeep=ScreenDef.Depth;
  999.  
  1000.         return TRUE;
  1001.         }
  1002.     }
  1003.  
  1004.  
  1005. /*******************************************************************************
  1006. void    gfxinit(void)
  1007.  
  1008.     startup AGL and initialize all it's resources
  1009.  
  1010. *******************************************************************************/
  1011. /*PROTOTYPE*/
  1012. void gfxinit(void)
  1013.     {
  1014.     long colors;
  1015.  
  1016.     colors=pow(2.0,(float)ScreenDeep);
  1017.  
  1018.     GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0L);
  1019.     if(GfxBase==NULL)
  1020.          {
  1021.         GL_error("Error opening GraFiX Library\n");
  1022.  
  1023.          exit(1);
  1024.          }
  1025.  
  1026.     IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L);
  1027.     if(IntuitionBase==NULL)
  1028.          {
  1029.         GL_error("Error opening Intuition Library\n");
  1030.  
  1031.          CloseLibrary((void *)GfxBase);
  1032.          exit(2);
  1033.          }
  1034.  
  1035.     LayersBase=OpenLibrary("layers.library",0L);
  1036.     if(LayersBase==NULL)
  1037.          {
  1038.         GL_error("Error opening Intuition Library\n");
  1039.  
  1040.          CloseLibrary((void *)IntuitionBase);
  1041.          CloseLibrary((void *)GfxBase);
  1042.          exit(3);
  1043.          }
  1044.  
  1045.     GLScreen=(struct Screen *)OpenScreen(&ScreenDef);
  1046.     if(GLScreen==NULL)
  1047.          {
  1048.         GL_error("Error opening Screen\n");
  1049.  
  1050.          CloseLibrary((void *)LayersBase);
  1051.          CloseLibrary((void *)IntuitionBase);
  1052.          CloseLibrary((void *)GfxBase);
  1053.          exit(4);
  1054.          }
  1055.  
  1056.     if(!(OpenDevice("console.device",-1L,(struct IORequest *)&IOStandardRequest,0L)))
  1057.         ConsoleDevice=(struct Library *)IOStandardRequest.io_Device;
  1058.     else
  1059.         {
  1060.         GL_error("Error Opening Console\n");
  1061.  
  1062.          CloseScreen(GLScreen);
  1063.          CloseLibrary((void *)LayersBase);
  1064.          CloseLibrary((void *)IntuitionBase);
  1065.          CloseLibrary((void *)GfxBase);
  1066.          exit(5);
  1067.         }
  1068.  
  1069.     DiskfontBase=OpenLibrary("diskfont.library",0L);
  1070.     if(DiskfontBase==NULL)
  1071.         GL_error("Error opening Diskfont Library\n");
  1072.  
  1073.     FontPtr=OpenDiskFont(&StdFont);
  1074.     if(FontPtr==NULL)
  1075.         GL_error("Error Opening Font\n");
  1076.  
  1077. /*     SetRast(&GLScreen->RastPort,BLUEGREEN); */
  1078.  
  1079.     ScreenWidth=GLScreen->Width;
  1080.     ScreenHeight=GLScreen->Height;
  1081.  
  1082.     GLView= &GLScreen->ViewPort;
  1083.     LoadRGB4(GLView,ColorMap,colors);
  1084.     DrawType=FALSE;
  1085.     CurrentColor=0;
  1086.     init_matrices();
  1087.     initialize_RGB();
  1088.  
  1089. #if MICE
  1090.     start_gameport();
  1091.     create_mousesprite();
  1092. #endif
  1093.  
  1094.     qinit();
  1095.  
  1096.     Gfx_Initialized=TRUE;
  1097.  
  1098.     /* set an exit trap */
  1099.     if(atexit(gexit))
  1100.         GL_error("Error setting exit trap\n");
  1101.     }
  1102.  
  1103.  
  1104. /*******************************************************************************
  1105. void    GL_error(char *message)
  1106.  
  1107. *******************************************************************************/
  1108. /*PROTOTYPE*/
  1109. void GL_error(char *message)
  1110.     {
  1111.     printf("Amiga GL Error: %s\n",message);
  1112.     }
  1113.