home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 505b.lha / Ham_e_library / RendDemo.c < prev    next >
C/C++ Source or Header  |  1991-05-05  |  8KB  |  347 lines

  1. /*
  2.  * This is demo code to show how to call the renderhame.library
  3.  * for both HAME and REG mode rendering. Save of the resulting IFF
  4.  * image is also provided in the library, and demonstrated here.
  5.  *
  6.  * if 'DOHAM' is defined, then this code will generate a HAME mode output.
  7.  * otherwise, it generates REWG mode output. If you search thru the code
  8.  * to find these two variables, you'll see how simple the render options
  9.  * are.
  10.  */
  11.  
  12. #define DOHAM 1
  13.  
  14. #include <intuition/intuition.h>
  15. #include <exec/types.h>
  16. #include <exec/execbase.h>
  17. #include <graphics/text.h>
  18.  
  19. #include <proto/exec.h>
  20. #include <proto/graphics.h>
  21.  
  22. #include "hame_pragmas.h"
  23. #include "HameStruct.h"
  24. #include "HameProtos.h"
  25. #include "HameRev.h"
  26.  
  27. #include "rend_pragmas.h"
  28. #include "RendStruct.h"
  29. #include "RendProtos.h"
  30. #include "RendRev.h"
  31.  
  32. #include <string.h>
  33. #include <exec/memory.h>
  34. #include <exec/tasks.h>
  35.  
  36. #include <graphics/view.h>
  37. #include <graphics/gfx.h>
  38. #include <graphics/rastport.h>
  39.  
  40. #include <proto/intuition.h>
  41. #include <stdio.h>
  42.  
  43. struct Library *HameBase;
  44. struct Library *RenderBase;
  45. struct HamePort *HamePort;
  46. struct rgb_image rgb;
  47.  
  48. extern struct Screen *OpenScreen();
  49. extern struct Window *OpenWindow();
  50.  
  51. extern struct ExecBase *SysBase;
  52. struct IntuitionBase *IntuitionBase;
  53. struct GfxBase       *GfxBase;
  54.  
  55. struct Screen        *screen;
  56. struct Window        *window;
  57.  
  58. FILE *rf, *gf, *bf;
  59. UBYTE  *rptr, *gptr, *bptr;
  60. long ptsize;
  61.  
  62. #define WIDTH 640
  63. #define HEIGHT 200
  64.  
  65. struct NewScreen my_screen =
  66.   {
  67.     0, 0, WIDTH, HEIGHT,
  68.     4,
  69.     0, 1,
  70.     HIRES,
  71.     CUSTOMSCREEN,
  72.     (struct TextAttr *) NULL,
  73.     NULL,
  74.     NULL, NULL
  75.   };
  76.  
  77. struct NewWindow my_window =
  78.   {
  79.     0, 0, WIDTH, HEIGHT,
  80.     (UBYTE) 0, (UBYTE) 1,
  81.     MOUSEBUTTONS
  82.        | INTUITICKS,
  83.     SMART_REFRESH
  84.        | BORDERLESS
  85.        | BACKDROP
  86.        | ACTIVATE
  87.        | NOCAREREFRESH
  88.        | REPORTMOUSE,
  89.     NULL, NULL,
  90.     NULL,
  91.     NULL, NULL,
  92.     0, 0, 0, 0,
  93.     CUSTOMSCREEN
  94.   };
  95.  
  96.  
  97. /*
  98.   Clean up and exit
  99. */
  100. void CloseStuff()
  101.   {
  102.       if (rf)                 fclose(rf);
  103.       if (gf)                 fclose(gf);
  104.       if (bf)                 fclose(bf);
  105.  
  106.       if (rptr)               FreeMem(rptr, ptsize);
  107.       if (gptr)               FreeMem(gptr, ptsize);
  108.       if (bptr)               FreeMem(bptr, ptsize);
  109.  
  110.       if (window)             CloseWindow(window);
  111.       if (screen)             CloseScreen(screen);
  112.       if (HamePort)           HAME_Dispose(HamePort);
  113.       if (RenderBase)         CloseLibrary(RenderBase);
  114.       if (HameBase)           CloseLibrary(HameBase);
  115.       if (GfxBase)            CloseLibrary(GfxBase);
  116.       if (IntuitionBase)      CloseLibrary(IntuitionBase);
  117.  
  118.       exit(0);
  119.   }
  120.  
  121.  
  122. /*
  123.   Draw a vertical column of pixels at the left margin to prevent hame from
  124.   turning off.
  125. */
  126. void DrawLeftMargin()
  127.   {
  128.   int i;
  129.     HAME_SetAPen(HamePort, 1);
  130.     for ( i=0; i<200; i++ )
  131.       {
  132.         HAME_WritePixel( HamePort, 0, i);
  133.       }
  134.   }
  135.  
  136. void palette1()
  137.   {
  138.   int i, r;
  139.     HAME_SetRGB8(HamePort, 0, 0, 0, 0);
  140.     HAME_SetRGB8(HamePort, 1, 0, 0, 0);
  141.     for ( i = 2; i < 64; i++ )
  142.       {
  143.         r = i << 2;
  144.         HAME_SetRGB8(HamePort, i, r, r, r);
  145.       }
  146.   }
  147.  
  148. /*
  149.  * Main routine
  150.  */
  151. int main(argc, argv)
  152.   int argc;
  153.   char *argv[];
  154.   {
  155.   ULONG  msg_class;
  156.   USHORT msg_code;
  157.   int    finished=0;
  158.   struct IntuiMessage *message;
  159.   struct Gadget *gadget;
  160.  
  161.  
  162.     if (argc < 2)
  163.       {
  164.         printf("USAGE:\n  %s outfile\n", argv[0] );
  165.         exit(0);
  166.       }
  167.  
  168.     if ((IntuitionBase = (struct IntuitionBase *)
  169.          OpenLibrary("intuition.library",33L))==NULL)
  170.       {
  171.         printf("No intuition.library!  GET SOME ROMS!\n");
  172.         CloseStuff();
  173.       }
  174.   
  175.     if ((GfxBase = (struct GfxBase *)
  176.          OpenLibrary("graphics.library" , 0L))==NULL)
  177.       {
  178.         printf("No graphics.library!  GET SOME ROMS!\n");
  179.         CloseStuff();
  180.       }
  181.   
  182.     /* Open the hame library */
  183.     if ((HameBase = (struct Library *) 
  184.          OpenLibrary("hame.library", HAMELIB_VERSION)) == NULL)
  185.       {
  186.         printf("No hame.library!\n");
  187.         CloseStuff();
  188.       }
  189.   
  190.     /* Open the hame library */
  191.     if ((RenderBase = (struct Library *) 
  192.          OpenLibrary(RENDER_LIBNAME, RENDER_LIBVERSION)) == NULL)
  193.       {
  194.         printf("No %s!\n", RENDER_LIBNAME);
  195.         CloseStuff();
  196.       }
  197.   
  198.     /* Get a screen to play with */
  199.     if ((screen = OpenScreen(&my_screen))     ==NULL)
  200.       {
  201.         printf("Couldn't open Screen!\n");
  202.         CloseStuff();
  203.       }
  204.   
  205.     my_window.Screen = screen;
  206.   
  207.     /* Get a window to play with */
  208.     if ((window = OpenWindow(&my_window)) == NULL)
  209.       {
  210.         printf("Couldn't open Window!\n");
  211.         CloseStuff();
  212.       }
  213.   
  214.     /* turn off the title bar */
  215.     ShowTitle(screen, 0L);
  216.   
  217.     /* Tell the library where to put pixels */
  218. #ifdef DOHAM
  219.     /* This is hame mode */
  220.     if ((HamePort = HAME_Init(screen, 320, 200, 10, 1, 0, 1, 199)) == NULL)
  221.       {
  222.         printf("Couldn't get HamePort!\n");
  223.         CloseStuff();
  224.       }
  225. #else
  226.     /* this is spectral mode (256 REG) */
  227.     if ((HamePort = HAME_Init(screen, 320, 200, 10, 0, 0, 4, 196)) == NULL)
  228.       {
  229.         printf("Couldn't get HamePort!\n");
  230.         CloseStuff();
  231.       }
  232. #endif
  233.  
  234.     /* Set the pallette */
  235.     palette1();
  236.   
  237.     /* Keep hame from turning off because of screen wide color 0 */
  238.     DrawLeftMargin();
  239.   
  240.     /* 8 bit buffer is 160 x 100 */
  241.     ptsize = 160 * 100;
  242.   
  243.     /* Get fast memory */
  244.     rptr = AllocMem(ptsize, 0);
  245.     gptr = AllocMem(ptsize, 0);
  246.     bptr = AllocMem(ptsize, 0);
  247.   
  248.     if ( !rptr || ! gptr || !bptr )
  249.       {
  250.         printf("Can't get enough memory for RGB buffers\n");
  251.         CloseStuff();
  252.       }
  253.   
  254.     /* load the buffers */
  255.     rf = fopen( "pic160x100.red", "r" );
  256.     gf = fopen( "pic160x100.grn", "r" );
  257.     bf = fopen( "pic160x100.blu", "r" );
  258.   
  259.     if ( !rf || ! gf || !bf )
  260.       {
  261.         printf("RAW RGB files missing..\n   pic160x100.red, pic160x100.grn, pic160x100.blu\n");
  262.         CloseStuff();
  263.       }
  264.   
  265.     if (fread(rptr, ptsize, 1, rf) != 1 )
  266.       {
  267.         printf("pic160x100.red truncated!  Aborting\n");
  268.         CloseStuff();
  269.       }
  270.     if (fread(gptr, ptsize, 1, gf) != 1 )
  271.       {
  272.         printf("pic160x100.grn truncated!  Aborting\n");
  273.         CloseStuff();
  274.       }
  275.     if (fread(bptr, ptsize, 1, bf) != 1 )
  276.       {
  277.         printf("pic160x100.blu truncated!  Aborting\n");
  278.         CloseStuff();
  279.       }
  280.   
  281.     rgb.rbu = rptr;
  282.     rgb.gbu = gptr;
  283.     rgb.bbu = bptr;
  284.   
  285.     rgb.xsize = 160;
  286.     rgb.ysize = 100;
  287.   
  288. #ifdef DOHAM
  289.     /* HAM mode */
  290.     HAME_RenderHam(HamePort, &rgb, 0, 0, 160, 100, 1);
  291. #else
  292.     /* REG mode */
  293.     HAME_RenderReg(HamePort, &rgb, 0, 0, 160, 100, 1, 250);
  294. #endif
  295.  
  296.     if (!HAME_SaveIFF( HamePort, argv[1] ))
  297.       {
  298.         printf("IFF save unsuccessful!\n");
  299.       }
  300.   
  301.     /* Wait for mouse press */
  302.     while(!finished)
  303.       {
  304.         /* flush all msgs in the port */
  305.         
  306.         /* now wait for a NEW message. */
  307.         message       = (struct IntuiMessage *) WaitPort(window->UserPort);
  308.         while(message = (struct IntuiMessage *) GetMsg(window->UserPort))
  309.           {
  310.             msg_class = message->Class;
  311.             msg_code = message->Code;
  312.             gadget = (struct Gadget *) message->IAddress;
  313.             ReplyMsg((struct Message *)message);
  314.   
  315.             switch( msg_class )
  316.               {
  317.                 case MOUSEBUTTONS:
  318.                   {
  319.                     finished = 1;
  320.                     break;
  321.                   }
  322.   
  323.                 case INTUITICKS:
  324.                   {
  325.                     break;
  326.                   }
  327.                 case CLOSEWINDOW:
  328.                   {
  329.                     finished = 1;
  330.                     break;
  331.                   }
  332.                 default:
  333.                   {
  334.                     printf("?");
  335.                     break;
  336.                   }
  337.               }
  338.           }
  339.       }
  340.     while ((message = (struct IntuiMessage *)
  341.             GetMsg( window->UserPort )) != NULL)
  342.       {
  343.         ReplyMsg((struct Message *)message);
  344.       }
  345.     CloseStuff();
  346.   }
  347.