home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d157 / animballs.lha / AnimBalls / screen.c < prev   
C/C++ Source or Header  |  1988-10-02  |  5KB  |  212 lines

  1. /* screen.c -- code to open a screen and then call render */
  2. #include <exec/types.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <intuition/intuition.h>
  6.  
  7. #include "globals.h"
  8.  
  9. #include <proto/exec.h>
  10. #include <proto/graphics.h>
  11. #include <proto/intuition.h>
  12. #include <proto/layers.h>
  13.  
  14. struct IntuitionBase *IntuitionBase = NULL;
  15. struct GfxBase *GfxBase = NULL;
  16. struct LayersBase *LayersBase = NULL;
  17.  
  18. #define IREV 29
  19. #define GREV 29
  20.  
  21. char defaultfile[] = "balls.default.dat";
  22.  
  23. #define DEPTH 6
  24. int depth = DEPTH;
  25.  
  26. struct BitMap pbitmap = {    /* picture (screen) bitmap (*sbitmap) */
  27.     0, 0, 0, 0, 0,
  28.     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  29.  
  30. struct BitMap tbitmap = {    /* temp (exact) bitmap */
  31.     0, 0, 0, 0, 0,
  32.     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  33.  
  34. struct BitMap ibitmap = {    /* image bitmap */
  35.     0, 0, 0, 0, 0,
  36.     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  37.  
  38. struct BitMap mbitmap = {    /* mask bitmap */
  39.     0, 0, 0, 0, 0,
  40.     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  41.  
  42. struct NewScreen mynewscreen = {
  43.     0, 0, WIDTH, 200, DEPTH,
  44.     0, 15,
  45.     HAM,
  46.     CUSTOMSCREEN | CUSTOMBITMAP,
  47.     NULL,
  48.     "B & W   Low Res Screen",
  49.     NULL,
  50.     &pbitmap,
  51.     };
  52.  
  53. struct Screen *myscreen = NULL;
  54. struct ViewPort *vp;
  55. struct BitMap *sbitmap = NULL;
  56.  
  57. struct NewWindow mynewwindow = {
  58.     0, 1, WIDTH, 199,
  59.     0, 15,
  60.     MOUSEBUTTONS | MOUSEMOVE | CLOSEWINDOW,
  61.     WINDOWCLOSE | SIMPLE_REFRESH | /* BACKDROP | */ BORDERLESS | 
  62.         REPORTMOUSE | ACTIVATE,
  63.     NULL,
  64.     NULL,
  65.     "   Please Wait . . . ",
  66.     NULL,
  67.     NULL,
  68.     0, 0, 0, 0,
  69.     CUSTOMSCREEN,
  70.     };
  71.  
  72. struct Window *mywindow = NULL;
  73.  
  74. int bw = 0;
  75.  
  76. UWORD bwcolortable[16] = {
  77.     0x000, 0x111, 0x222, 0x333, 0x444, 0x555, 0x666, 0x777,
  78.     0x888, 0x999, 0xaaa, 0xbbb, 0xccc, 0xddd, 0xeee, 0xfff};
  79.  
  80. void
  81. main(argc,argv)
  82. int argc;
  83. char *argv[];
  84. {
  85.     int i;
  86.     void cleanup();
  87.  
  88.     switch (argc) {
  89.         case 0: readballs(defaultfile);
  90.             break;
  91.         case 2:    readballs(argv[1]);
  92.             break;
  93.         case 3:    if (strcmp(argv[1],"-bw") == 0) {
  94.                 setbw();
  95.                 readballs(argv[2]);
  96.                 break;
  97.             }
  98.         default:dodefault();
  99.     }
  100.  
  101.     onbreak(&cleanup);
  102.  
  103.     initrender();
  104.  
  105.     IntuitionBase = (struct IntuitionBase *)
  106.         OpenLibrary("intuition.library",IREV);
  107.     if (IntuitionBase == NULL) panic("Can't open intuition");
  108.     GfxBase = (struct GfxBase *)
  109.         OpenLibrary("graphics.library",GREV);
  110.     if (GfxBase == NULL) panic("Can't open GfxBase");
  111.  
  112.     InitBitMap(&pbitmap,depth,WIDTH,HEIGHT);
  113.     for (i=0; i < depth; i++) {
  114.         pbitmap.Planes[i] = (PLANEPTR)AllocRaster(WIDTH,HEIGHT);
  115.         if (pbitmap.Planes[i] == NULL)
  116.             panic("Not enough memory for screen");
  117.         BltClear(pbitmap.Planes[i],RASSIZE(WIDTH,HEIGHT),0);
  118.     }
  119.     if (!bw) {
  120.         InitBitMap(&tbitmap,depth,WIDTH,SMHEIGHT);
  121.         for (i=0; i < depth; i++) {
  122.         tbitmap.Planes[i] = (PLANEPTR)AllocRaster(WIDTH,SMHEIGHT);
  123.         if (tbitmap.Planes[i] == NULL)
  124.             panic("Not enough memory for temp bit map");
  125.         BltClear(tbitmap.Planes[i],RASSIZE(WIDTH,SMHEIGHT),0);
  126.         }
  127.     }
  128.     InitBitMap(&ibitmap,depth,maskw,maskh);
  129.     for (i=0; i < depth; i++) {
  130.         ibitmap.Planes[i] = (PLANEPTR)AllocRaster(maskw,maskh);
  131.         if (ibitmap.Planes[i] == NULL)
  132.             panic("Not enough memory for image bit map");
  133.         BltClear(ibitmap.Planes[i],RASSIZE(maskw,maskh),0);
  134.     }
  135.     InitBitMap(&mbitmap,1,maskw,maskh);
  136.     mbitmap.Planes[0] = (PLANEPTR)AllocRaster(maskw,maskh);
  137.     if (mbitmap.Planes[0] == NULL)
  138.         panic("Not enough memory for mask bit map");
  139.     BltClear(mbitmap.Planes[0],RASSIZE(maskw,maskh),0);
  140.  
  141.     myscreen = (struct Screen *) OpenScreen(&mynewscreen);
  142.     if (myscreen == NULL) panic("Can't open screen");
  143.     vp = &(myscreen->ViewPort);
  144.     sbitmap = &(myscreen->BitMap);
  145.  
  146.     mynewwindow.Screen = myscreen;
  147.     mywindow = (struct Window *) OpenWindow(&mynewwindow);
  148.     if (mywindow == NULL) panic("Can't open window");
  149.  
  150.     if (bw) LoadRGB4(&myscreen->ViewPort,bwcolortable,16);
  151.     else    LoadRGB4(&myscreen->ViewPort,colortable,16);
  152.     RethinkDisplay();
  153.  
  154.     render();
  155.  
  156.     cleanup();
  157. }
  158.  
  159. void
  160. panic(str)
  161. char *str;
  162. {
  163.     fprintf(stderr,"%s\n",str);
  164.     fflush(stderr);
  165.     cleanup();
  166. }
  167.  
  168. void
  169. cleanup()
  170. {
  171.     int i;
  172.  
  173.     if (mywindow) CloseWindow(mywindow);
  174.     if (myscreen) CloseScreen(myscreen);
  175.     if (mbitmap.Planes[0]) FreeRaster(mbitmap.Planes[0],maskw,maskh);
  176.     for (i = depth-1; i >= 0; i--) if (ibitmap.Planes[i])
  177.         FreeRaster(ibitmap.Planes[i],maskw,maskh);
  178.     for (i = depth-1; i >= 0; i--) if (tbitmap.Planes[i])
  179.         FreeRaster(tbitmap.Planes[i],WIDTH,SMHEIGHT);
  180.     for (i = depth-1; i >= 0; i--) if (pbitmap.Planes[i])
  181.         FreeRaster(pbitmap.Planes[i],WIDTH,HEIGHT);
  182.     if (GfxBase)  CloseLibrary((struct Library *)GfxBase);
  183.     if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  184.     exit(0);
  185. }
  186.  
  187. void
  188. setbw()
  189. {
  190.     bw = TRUE;
  191.     mynewscreen.Depth = depth = 4;
  192.     mynewscreen.ViewModes &= ~HAM;
  193. }
  194.  
  195. void
  196. dodefault()
  197. {
  198.   printf("  %c33manimballs%c31m - An animation hack by JimG\n\n",0x9b,0x9b);
  199.   printf("Usage: animballs [-bw] ballsfile\n\n");
  200.   printf("  You have invoked animballs without parameters. I will use\n");
  201.   printf("a default balls file (balls.default.dat). You should read the\n");
  202.   printf("README file to see how to run this program. As a quick intro,\n");
  203.   printf("there will be a delay and then you will see some balls appear.\n");
  204.   printf("Press and hold the left mouse button, then drag the mouse to\n");
  205.   printf("rotate the balls in 3-space. Click on the close gadget to end.\n\n");
  206.   printf("Press <Return> to continue:");
  207.  
  208.   (void) getc(stdin);
  209.   readballs(defaultfile);
  210. }
  211.  
  212.