home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine 1996 / ARCHIVE_96.iso / discs / shareware / share_43 / source / c / AMIGA next >
Text File  |  1991-08-22  |  7KB  |  265 lines

  1. /*****************************************************************************
  2. *
  3. *                                   amiga.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This module handles all of the Amiga-specific code for the raytracer.
  8. *
  9. *
  10. * This software is freely distributable. The source and/or object code may be
  11. * copied or uploaded to communications services so long as this notice remains
  12. * at the top of each file.  If any changes are made to the program, you must
  13. * clearly indicate in the documentation and in the programs startup message
  14. * who it was who made the changes. The documentation should also describe what
  15. * those changes were. This software may not be included in whole or in
  16. * part into any commercial package without the express written consent of the
  17. * author.  It may, however, be included in other public domain or freely
  18. * distributed software so long as the proper credit for the software is given.
  19. *
  20. * This software is provided as is without any guarantees or warranty. Although
  21. * the author has attempted to find and correct any bugs in the software, he
  22. * is not responsible for any damage caused by the use of the software.  The
  23. * author is under no obligation to provide service, corrections, or upgrades
  24. * to this package.
  25. *
  26. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  27. * about them.  Also, if you have any comments or questions, you may contact me
  28. * at the following address:
  29. *
  30. *     David Buck
  31. *     22C Sonnet Cres.
  32. *     Nepean Ontario
  33. *     Canada, K2H 8W7
  34. *
  35. *  I can also be reached on the following bulleton boards:
  36. *
  37. *     ATX              (613) 526-4141
  38. *     OMX              (613) 731-3419
  39. *     Mystic           (613) 731-0088 or (613) 731-6698
  40. *
  41. *  Fidonet:   1:163/109.9
  42. *  Internet:  David_Buck@Carleton.CA
  43. *
  44. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  45. *
  46. *     Lattice BBS                      (708) 916-1200
  47. *     The Information Exchange BBS     (708) 945-5575
  48. *     Stillwaters BBS                  (708) 403-2826
  49. *
  50. *****************************************************************************/
  51.  
  52. #include "frame.h"
  53. #include "dkbproto.h"
  54.  
  55. #include <proto/exec.h>
  56. #include <proto/intuition.h>
  57. #include <proto/graphics.h>
  58. #include <proto/dos.h>
  59. #include <exec/types.h>
  60. #include <intuition/intuition.h>
  61. #include <graphics/display.h>
  62.  
  63. void geta4(void);
  64. void Requestor_Handler(void);
  65. void Amiga_open(void);
  66. void Amiga_close(void);
  67. void open_requestor(void);
  68. void close_requestor(void);
  69. extern int Options;
  70.  
  71. #define INT_REV 29L
  72. #define GR_REV 29L
  73.  
  74. struct IntuitionBase *IntuitionBase;
  75. struct GfxBase *GfxBase;
  76. struct Screen *s;
  77. struct Window *w;
  78. struct Task *Requestor_Task;
  79.  
  80. volatile int Requestor_Running;
  81. volatile extern int Stop_Flag;
  82.  
  83. #define SCREEN_WIDTH 320
  84. #define SCREEN_HEIGHT 400
  85.  
  86. struct NewScreen MyScreen =
  87.    {
  88.    0, 0,
  89.    SCREEN_WIDTH, SCREEN_HEIGHT,
  90.    6,
  91.    0, 1,
  92.    INTERLACE | HAM,
  93.    SCREENQUIET,
  94.    NULL,
  95.    (UBYTE *) "DKB Ray Trace",
  96.    NULL,
  97.    NULL
  98.    };
  99.  
  100. struct Window *Requestor_Window;
  101. struct MsgPort *Requestor_Port;
  102.  
  103. struct IntuiText Body_Text =
  104.    {0, 1, JAM1, 5, 10, NULL, (UBYTE *) "Click to abort the picture", NULL};
  105.  
  106. struct IntuiText Abort_Text =
  107.    {0, 1, JAM1, 5, 3, NULL, (UBYTE *) "Abort", NULL};
  108.  
  109. UWORD ColorTbl[16] = { 0x000, 0x111, 0x222, 0x333, 0x444, 0x555, 0x666,
  110.                        0x777, 0x888, 0x999, 0xaaa, 0xbbb, 0xccc, 0xddd,
  111.                        0xeee, 0xfff };
  112.  
  113. LONG last_red = 0, last_green = 0, last_blue = 0, last_y = -1;
  114.  
  115. void Requestor_Handler ()
  116.    {
  117.    Requestor_Port = CreatePort ("ray trace port", 0L);
  118.    Requestor_Window = BuildSysRequest
  119.              (NULL, &Body_Text, NULL, &Abort_Text, GADGETUP, 280L, 60L);
  120.    Wait ((1 << Requestor_Port -> mp_SigBit)
  121.           | (1 << Requestor_Window -> UserPort -> mp_SigBit));
  122.  
  123.    Requestor_Running = FALSE;
  124.    Stop_Flag = TRUE;
  125.    }
  126.  
  127. void Amiga_open()
  128.    {
  129.    IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library",INT_REV);
  130.    if (IntuitionBase == NULL)
  131.      exit(FALSE);
  132.  
  133.    GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", GR_REV);
  134.    if (GfxBase == NULL)
  135.      exit(FALSE);
  136.    Requestor_Running = FALSE;
  137.    }
  138.  
  139. void Amiga_close()
  140.    {
  141.    if (Requestor_Running) {
  142.       Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  143.       Delay (2L);
  144.       }
  145.  
  146.    if (Requestor_Window)
  147.       FreeSysRequest (Requestor_Window);
  148.  
  149.    Requestor_Window = NULL;
  150.  
  151.    CloseLibrary (GfxBase) ;
  152.    CloseLibrary (IntuitionBase) ;
  153.    }
  154.  
  155. void open_requestor()
  156.    {
  157.    Requestor_Window = NULL;
  158.    Stop_Flag = FALSE;
  159.    Requestor_Running = TRUE;
  160.    Requestor_Task = CreateTask ("Raytrace Requestor", 2L,
  161.                                 (APTR) Requestor_Handler, 20000L);
  162.    }
  163.  
  164. void display_finished ()
  165.    {
  166.    if (Requestor_Running) {
  167.      Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  168.      Delay (2L);
  169.      }
  170.  
  171.    if (Requestor_Window)
  172.       FreeSysRequest (Requestor_Window);
  173.  
  174.    Requestor_Window = NULL;
  175.    if (Options & PROMPTEXIT)
  176.       {
  177.       printf ("Finished.\nPress CR to quit.\n");
  178.       getchar();
  179.       }
  180.    }
  181.  
  182. void display_init ()
  183.    {
  184.    Amiga_open();
  185.    open_requestor();
  186.  
  187.    Delay (10);
  188.    if ((s = (struct Screen *) OpenScreen (&MyScreen)) == NULL)
  189.       exit (FALSE);
  190.  
  191.    ShowTitle (s, FALSE);
  192.  
  193.    LoadRGB4 (&(s->ViewPort), ColorTbl, 16L);
  194.    SetAPen (&(s->RastPort), 7L);
  195.    RectFill (&(s -> RastPort), 0L, 0L, 319L, 399L);
  196.    }
  197.  
  198. void display_close ()
  199.    {
  200.    if (Requestor_Running) {
  201.       Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  202.       Delay (2L);
  203.       }
  204.  
  205.    if (Requestor_Window)
  206.       FreeSysRequest (Requestor_Window);
  207.  
  208.    Requestor_Window = NULL;
  209.  
  210.    CloseScreen (s);
  211.    }
  212.  
  213. #define absdif(x,y) ((x > y) ? (x - y) : (y - x))
  214. #define max3(x,y,z) ((x>y)?((x>z)?1:3):((y>z)?2:3))
  215.  
  216. void display_plot (x, y, Red, Green, Blue)
  217.    int x, y;
  218.    char Red, Green, Blue;
  219.    {
  220.    register LONG delta_red, delta_green, delta_blue, colour;
  221.    static int Last_Colour = 0;
  222.  
  223.    if ((x >= SCREEN_WIDTH-1 )  || (y >= SCREEN_HEIGHT))
  224.       return;
  225.  
  226.    Red = (Red >> 4) & 0x0F;
  227.    Green = (Green >> 4) & 0x0F;
  228.    Blue = (Blue >> 4) & 0x0F;
  229.  
  230.    if (last_y != y) {
  231.       last_y = y;
  232.       last_red = last_green = last_blue = 0;
  233.       Last_Colour = 0;
  234.       SetAPen (&(s -> RastPort), 0);
  235.       WritePixel (&(s -> RastPort), 0, y);
  236.       }
  237.  
  238.    delta_red = absdif (Red, last_red);
  239.    delta_green = absdif (Green, last_green);
  240.    delta_blue = absdif (Blue, last_blue);
  241.  
  242.    switch (max3(delta_red, delta_green, delta_blue)) {
  243.       case 1:
  244.          last_red = Red;
  245.          colour = 0x20 + Red;
  246.          break;
  247.       case 2:
  248.          last_green = Green;
  249.          colour = 0x30 + Green;
  250.          break;
  251.       case 3:
  252.          last_blue = Blue;
  253.          colour = 0x10 + Blue;
  254.          break;
  255.       }
  256.  
  257.    if (colour != Last_Colour)
  258.       {
  259.       SetAPen (&(s -> RastPort), colour);
  260.       Last_Colour = colour;
  261.       }
  262.    WritePixel (&(s -> RastPort), x+1, y);
  263.    }
  264.  
  265.