home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff384.lzh / NorthC / Example1.LZH / cRender / aif.c < prev    next >
C/C++ Source or Header  |  1990-08-30  |  10KB  |  360 lines

  1. /*
  2.   (c) 1990 S.Hawtin.
  3.   Permission is granted to copy this file provided
  4.    1) It is not used for commercial gain
  5.    2) This notice is included in all copies
  6.    3) Altered copies are marked as such
  7.  
  8.   No liability is accepted for the contents of the file.
  9.  
  10.     aif.c    within        NorthC render
  11. */
  12.  
  13. /*
  14.   The Amiga interface functions
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <types.h>
  19. #include <intuition/intuition.h>
  20. #include <graphics/rastport.h>
  21. #include <graphics/gfx.h>
  22. #include <graphics/view.h>
  23. #include <intuition/screens.h>
  24.  
  25. #include "3d.h"
  26.  
  27. extern struct Screen *OpenScreen();
  28. extern struct Window *OpenWindow();
  29. extern struct ViewPort *ViewPortAddress();
  30. extern long OpenLibrary();
  31. extern struct Message *GetMsg();
  32. extern struct ColorMap *GetColorMap();
  33. extern PLANEPTR AllocRaster();
  34. extern long Request();
  35.  
  36. void shut_screen();
  37.  
  38. extern int solid_fill;
  39. extern double rot1;
  40. extern double rot2;
  41.  
  42. UWORD area1Buffer[25];
  43. UWORD area2Buffer[25];
  44.  
  45. /* Get the menustrip data structures */
  46. #include "menustrip.h"
  47.  
  48. /* Get the requester data structure */
  49. #include "requester.h"
  50.  
  51. PLANEPTR myplane1;
  52. PLANEPTR myplane2;
  53.  
  54. int midx,midy;
  55.  
  56. struct TmpRas myTmpRas1;
  57. struct TmpRas myTmpRas2;
  58. struct NewWindow init_win;
  59. struct Window *mywin;
  60. struct Window *win1,*win2;
  61. struct NewScreen init_scn;
  62. struct Screen *myscreen;
  63. struct AreaInfo myArea1;
  64. struct AreaInfo myArea2;
  65.  
  66. struct ViewPort *myvport;
  67.  
  68. short colours[16] =
  69.    {0x0000,0x0888,0x0222,0x0333,
  70.     0x0444,0x0555,0x0666,0x0777,
  71.     0x0888,0x0999,0x0aaa,0x0bbb,
  72.     0x0ccc,0x0ddd,0x0eee,0x0fff};
  73.  
  74. draw_tri(col,x1,y1,x2,y2,x3,y3)
  75.     int col;
  76.     int x1,y1,x2,y2,x3,y3;
  77.    {/* Draw the triangle */
  78.     long this_shade;
  79.  
  80.     if(solid_fill)
  81.        {/* Set colour for triangle */
  82.         if(col>15)
  83.             col = 15;
  84.         if(col<2)
  85.             col = 2;
  86.         SetAPen(mywin->RPort,(long)col);
  87.         /* Draw the triangle */
  88.         AreaMove(mywin->RPort,(long)(x1 + midx),(long)(y1 + midy));
  89.         AreaDraw(mywin->RPort,(long)(x2 + midx),(long)(y2 + midy));
  90.         AreaDraw(mywin->RPort,(long)(x3 + midx),(long)(y3 + midy));
  91.         AreaDraw(mywin->RPort,(long)(x1 + midx),(long)(y1 + midy));
  92.         AreaEnd(mywin->RPort);
  93.         }
  94.       else
  95.        {
  96.         SetAPen(mywin->RPort,(long)15);
  97.         /* Draw the triangle */
  98.         Move(mywin->RPort,(long)(x1 + midx),(long)(y1 + midy));
  99.         Draw(mywin->RPort,(long)(x2 + midx),(long)(y2 + midy));
  100.         Draw(mywin->RPort,(long)(x3 + midx),(long)(y3 + midy));
  101.         Draw(mywin->RPort,(long)(x1 + midx),(long)(y1 + midy));
  102.         }
  103.     }
  104.  
  105. open_request()
  106.    {/* Call the requester to change rotation */
  107.     struct IntuiMessage *my_msg;
  108.     ULONG  class;
  109.     USHORT code;
  110.     USHORT qualifier;
  111.  
  112.     /* Set up the requester */
  113.     sprintf(int1_buffer,"%d",(int)(1000.0*rot1));
  114.     sprintf(int2_buffer,"%d",(int)(1000.0*rot2));
  115.  
  116.     class = Request(&my_requester,mywin);
  117.     if(class==0)
  118.        {printf("Requester failed\n");
  119.         exit(EXIT_FAILURE);
  120.         }
  121.     while(-1)
  122.        {/* Wait until we have recieved a message */
  123.         my_msg=(struct IntuiMessage *)GetMsg(mywin->UserPort);
  124.         while(my_msg!=NULL)
  125.            {/* Keep dealing with messages until requester is dismissed */
  126.             class = my_msg->Class;
  127.             code  = my_msg->Code;
  128.             qualifier = my_msg->Qualifier;
  129.             /* OK now free the message */
  130.             ReplyMsg(my_msg);
  131.  
  132.             /* Check which IDCMP flag was sent: */
  133.             switch(class)
  134.                {
  135.                 case GADGETDOWN:
  136.                     /* The user has selected a gadget */
  137.                     break;
  138.                 case GADGETUP:
  139.                     /* The user has released a gadget */
  140.                     break;
  141.                 case REQSET:
  142.                     /* Requester activated */
  143.                     break;
  144.                 case REQCLEAR:
  145.                     /* Requester deactivated */
  146.                     goto all_done;
  147.                 }
  148.             my_msg=(struct IntuiMessage *)GetMsg(mywin->UserPort);
  149.             }
  150.         /* Give everthing else a chance */
  151.         Delay(20L);
  152.         }
  153. all_done:
  154.     /* Now read the requester values */
  155.     rot1 = ((double)int1_info.LongInt)/1000.0;
  156.     rot2 = ((double)int2_info.LongInt)/1000.0;
  157.     }
  158.  
  159. proc_event(class,code,qualifier)
  160.     long  class;
  161.     int   code;
  162.     int   qualifier;
  163.    {/* Process the message, should be a menu select */
  164.  
  165.     /* Now go do what the user wants */
  166.     if(class == MENUPICK)
  167.        {
  168.         if( code != MENUNULL )
  169.            {
  170.             switch(MENUNUM(code))
  171.                {case EDIT_MENU:
  172.                     switch(ITEMNUM(code))
  173.                        {case QUIT_ITEM:
  174.                             exit(0);
  175.                         case SOLID_ITEM:
  176.                             solid_fill = -1;
  177.                             break;
  178.                         case OUTLINE_ITEM:
  179.                             solid_fill = 0;
  180.                             break;
  181.                         case REQ_ITEM:
  182.                             open_request();
  183.                             break;
  184.                         default:
  185.                             printf("Unknown item\n");
  186.                         }
  187.                     break;
  188.                 default:
  189.                     printf("Unknown Menu\n");
  190.                 }
  191.             }
  192.         }
  193.     }
  194.  
  195.  
  196. clear_screen()
  197.    {
  198.     ULONG  class;
  199.     USHORT code;
  200.     USHORT qualifier;
  201.     struct IntuiMessage *my_msg;
  202.  
  203.     /* Swap windows */
  204.     WindowToFront(mywin);
  205.     /* Wait for window to arrive at front */
  206.     Delay(15L);
  207.     /* process all the waiting events */
  208.     my_msg = (struct IntuiMessage *)GetMsg(win2->UserPort);
  209.     while(my_msg!=NULL)
  210.        {
  211.         class = my_msg->Class;
  212.         code  = my_msg->Code;
  213.         qualifier = my_msg->Qualifier;
  214.         /* OK now free the message */
  215.         ReplyMsg(my_msg);
  216.         proc_event(class,(int)code,(int)qualifier);
  217.         my_msg = (struct IntuiMessage *)GetMsg(win2->UserPort);
  218.         }
  219.     my_msg = (struct IntuiMessage *)GetMsg(win1->UserPort);
  220.     while(my_msg!=NULL)
  221.        {
  222.         class = my_msg->Class;
  223.         code  = my_msg->Code;
  224.         qualifier = my_msg->Qualifier;
  225.         /* OK now free the message */
  226.         ReplyMsg(my_msg);
  227.         proc_event(class,(int)code,(int)qualifier);
  228.         my_msg = (struct IntuiMessage *)GetMsg(win1->UserPort);
  229.         }
  230.     /* Swap windows */
  231.     if(mywin==win1)
  232.         mywin=win2;
  233.       else
  234.         mywin=win1;
  235.     /* Clear off window */
  236.     SetAPen(mywin->RPort,0L);
  237.     RectFill(mywin->RPort,0L,0L,(long)SC_WIDTH,(long)(SC_HEIGHT-SC_TOP));
  238.     }
  239.  
  240. init_screen()
  241.    {/* Open up the window */
  242.     int i;
  243.     /* Open the intuition library */
  244.     if(0==OpenLibrary("intuition.library",0L))
  245.        {printf("Unable to open intuition library\n");
  246.     exit(EXIT_FAILURE);
  247.     }
  248.     /* Open the graphics library */
  249.     if(0==OpenLibrary("graphics.library",0L))
  250.        {printf("Unable to open graphics library\n");
  251.     exit(EXIT_FAILURE);
  252.     }
  253.     /* Ensure we tidy up */
  254.     atexit(shut_screen);
  255.     /* Open my own screen */
  256.     init_scn.LeftEdge  = 0;
  257.     init_scn.TopEdge   = 0;
  258.     init_scn.Width     = SC_WIDTH;
  259.     init_scn.Height    = SC_HEIGHT;
  260.     init_scn.Depth     = 4;
  261.     init_scn.Type      = CUSTOMSCREEN;
  262.     init_scn.DefaultTitle = "NorthC CRender";
  263.     init_scn.DetailPen = 15;
  264.     init_scn.BlockPen  = 8;
  265.     myscreen = OpenScreen(&init_scn);
  266.     if(myscreen==NULL)
  267.        {printf("Failed to open screen\n");
  268.     exit(EXIT_FAILURE);
  269.         }
  270.     /* Set up centre of window */
  271.     midx = SC_WIDTH/2;
  272.     midy = (SC_HEIGHT - SC_TOP)/2;
  273.  
  274.     /* Open the window */
  275.     init_win.LeftEdge = 0;
  276.     init_win.TopEdge  = SC_TOP;
  277.     init_win.Width    = SC_WIDTH;
  278.     init_win.Height   = SC_HEIGHT-SC_TOP;
  279.     /* Window cannot resize so just fix it */
  280.     init_win.MaxWidth = init_win.Width;
  281.     init_win.MaxHeight= init_win.Height;
  282.     init_win.MinWidth = init_win.Width;
  283.     init_win.MinHeight= init_win.Height;
  284.  
  285.     init_win.DetailPen= 15;
  286.     init_win.BlockPen = 8;
  287.     init_win.Title    = "";
  288.     init_win.Screen   = myscreen;
  289.     init_win.Type     = CUSTOMSCREEN;
  290.     init_win.Flags    = BORDERLESS | SMART_REFRESH | ACTIVATE;
  291.     init_win.IDCMPFlags = MENUPICK|GADGETDOWN|GADGETUP|REQCLEAR|REQSET;
  292.     /* Now try and open the windows */
  293.     win1 = OpenWindow(&init_win);
  294.     win2 = OpenWindow(&init_win);
  295.     mywin = win1;
  296.  
  297.     if(win1==NULL || win2==NULL)
  298.        {printf("Failed to open window\n");
  299.     exit(EXIT_FAILURE);
  300.         }
  301.  
  302.     /* Set up the menu strip */
  303.     SetMenuStrip(win1, &edit_menu );
  304.     SetMenuStrip(win2, &edit_menu );
  305.  
  306.     /* Set up the areas */
  307.     InitArea(&myArea1,&area1Buffer[0],10L);
  308.     win1->RPort->AreaInfo = &myArea1;
  309.     InitArea(&myArea2,&area2Buffer[0],10L);
  310.     win2->RPort->AreaInfo = &myArea2;
  311.  
  312.     myplane1 = AllocRaster((long)SC_WIDTH,(long)(SC_HEIGHT-SC_TOP));
  313.     myplane2 = AllocRaster((long)SC_WIDTH,(long)(SC_HEIGHT-SC_TOP));
  314.     if(myplane1==NULL || myplane2==NULL)
  315.        {printf("Failed to create plane\n");
  316.         exit(EXIT_FAILURE);
  317.         }
  318.     InitTmpRas(&myTmpRas1,myplane1,(long)RASSIZE(SC_WIDTH,SC_HEIGHT-SC_TOP));
  319.     InitTmpRas(&myTmpRas2,myplane1,(long)RASSIZE(SC_WIDTH,SC_HEIGHT-SC_TOP));
  320. /*    InitTmpRas(&myTmpRas2,myplane1,(long)RASSIZE(SC_WIDTH,SC_HEIGHT-SC_TOP));
  321. */    win1->RPort->TmpRas = &myTmpRas1;
  322.     win2->RPort->TmpRas = &myTmpRas2;
  323.  
  324.     /* Set the colours to grey */
  325.     LoadRGB4(&myscreen->ViewPort,colours,16L);
  326.  
  327.     }
  328.  
  329. void
  330. shut_screen()
  331.    {/* Close down the graphics window, this function is called 
  332.       when exit() runs or with normal program termination */
  333.  
  334.     if(myplane1)
  335.        {
  336.         FreeRaster(myplane1,(long)SC_WIDTH,(long)(SC_HEIGHT-SC_TOP));
  337.         myplane1 = NULL;
  338.         }
  339.     if(myplane2)
  340.        {
  341.         FreeRaster(myplane2,(long)SC_WIDTH,(long)(SC_HEIGHT-SC_TOP));
  342.         myplane2 = NULL;
  343.         }
  344.     if(win1)
  345.        {
  346.         ClearMenuStrip(win1);
  347.         CloseWindow(win1);
  348.         win1 = NULL;
  349.         }
  350.     if(win2)
  351.        {
  352.         ClearMenuStrip(win2);
  353.         CloseWindow(win2);
  354.         win2 = NULL;
  355.         }
  356.     if(myscreen)
  357.         CloseScreen(myscreen);
  358.     myscreen = NULL;
  359.     }
  360.