home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / os2 / hpgl312.zip / TO_AMIGA.C < prev    next >
C/C++ Source or Header  |  1993-04-18  |  12KB  |  431 lines

  1. /*
  2.    Copyright (c) 1992, 1993 Claus Langhans.  All rights reserved.
  3.    Distributed by Free Software Foundation, Inc.
  4.  
  5. This file is part of HP2xx.
  6.  
  7. HP2xx is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  9. to anyone for the consequences of using it or for whether it serves any
  10. particular purpose or works at all, unless he says so in writing.  Refer
  11. to the GNU General Public License, Version 2 or later, for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. HP2xx, but only under the conditions described in the GNU General Public
  15. License.  A copy of this license is supposed to have been
  16. given to you along with HP2xx so you can know your rights and
  17. responsibilities.  It should be in a file named COPYING.  Among other
  18. things, the copyright notice and this notice must be preserved on all
  19. copies.
  20.  
  21. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. */
  23.  
  24. /**
  25.  ** to_amiga2.c: GNU-C++ (AMIGA) preview part of project "hp2xx"
  26.  **
  27.  ** 92/04/10  V 1.00  CHL  Some ideas taken from to_gnu.c and from the RKMs
  28.  ** 92/04/11  V 1.01  CHL  Preview finished
  29.  ** 92/04/12  V 1.02  CHL  Stop ^C
  30.  ** 92/04/13  V 1.03  CHL  Too small windows bug cleaned
  31.  ** 92/12/09  V 2.00  CHL  Added the color preview.
  32.  **/
  33.  
  34. /* Standard includes */
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <ctype.h>
  39. #include <signal.h>
  40.  
  41.  
  42. /* Project includes */
  43.  
  44. #include "bresnham.h"
  45. #include "hp2xx.h"
  46.  
  47. /* Amiga includes */
  48.  
  49. #include <exec/types.h>
  50. #include <exec/memory.h>
  51. #include <intuition/intuition.h>
  52.  
  53. UBYTE           vers[] = "$VER: hp2xx 2.00 (16.12.92)\0\n";
  54.  
  55. WORD            width_super = 800;
  56. WORD            height_super = 600;
  57.  
  58. #define LEFTBORDERSIZE     4
  59. #define RIGHTBORDERSIZE    18
  60. #define TOPBORDERSIZE    11
  61. #define BOTTOMBORDERSIZE    9
  62. #define OPENWIDTH    425
  63. #define OPENHEIGHT    178
  64. #define MINWINWIDTH    90
  65. #define MINWINHEIGHT    40
  66. #define MAXWINWIDTH    32000 - LEFTBORDERSIZE + RIGHTBORDERSIZE
  67. #define MAXWINHEIGHT    32000 - TOPBORDERSIZE + BOTTOMBORDERSIZE
  68. #define    DEPTH_SUPER    3
  69.  
  70. #ifndef __GNUC__
  71. extern struct GfxBase *GfxBase;
  72. extern struct IntuitionBase *IntuitionBase;
  73. #else
  74. struct GfxBase *GfxBase;
  75. struct IntuitionBase *IntuitionBase;
  76. #endif                /* __GNUC__ */
  77.  
  78. struct LayersBase *LayersBase;
  79.  
  80. #define GetGadgetID(x) (((struct Gadget *)(msg->IAddress))->GadgetID)
  81.  
  82. #define GetLayerXOffset(x) (x->RPort->Layer->Scroll_X)
  83. #define GetLayerYOffset(x) (x->RPort->Layer->Scroll_Y)
  84.  
  85. #define UP_DOWN_GADGET    0
  86. #define LEFT_RIGHT_GADGET 1
  87. #define NO_GADGET         2
  88.  
  89. #define MAXVAL 0xFFFFL
  90.  
  91. struct Image    Images[2];
  92. struct Screen  *PreviewScreen;
  93.  
  94. /* The special data needed for the two proportional gadgets */
  95. struct PropInfo GadgetsSInfo[2] =
  96. {
  97.     {FREEVERT | AUTOKNOB, 0, 0, -1, -1,},
  98.     {FREEHORIZ | AUTOKNOB, 0, 0, -1, -1,}
  99. };
  100.  
  101. /* The usual data needed for any gadget */
  102. struct Gadget   Gadgets[2] =
  103. {
  104.  
  105. /* Gadgets[0] */
  106.     {&Gadgets[1], -15, 10, 16, -18,
  107.      GRELRIGHT | GRELHEIGHT,
  108.      RELVERIFY | GADGIMMEDIATE | RIGHTBORDER, PROPGADGET | GZZGADGET,
  109.      (APTR) & Images[0], NULL, NULL, NULL,
  110.      (APTR) & GadgetsSInfo[0], UP_DOWN_GADGET, NULL},
  111.  
  112. /* Gadgets[1] */
  113.     {NULL, 0, -8, -14, 9,
  114.      GRELBOTTOM | GRELWIDTH,
  115.      RELVERIFY | GADGIMMEDIATE | BOTTOMBORDER, PROPGADGET | GZZGADGET,
  116.      (APTR) & Images[1], NULL, NULL, NULL,
  117.      (APTR) & GadgetsSInfo[1], LEFT_RIGHT_GADGET, NULL}
  118. };
  119.  
  120. struct NewScreen NewPreviewScreen =
  121. {
  122. /* LeftEdge, TopEdge, Width, Height, Depth, */
  123.     0, 0, STDSCREENHEIGHT, STDSCREENWIDTH, DEPTH_SUPER,
  124.  
  125. /* DetailPen, BlockPen, */
  126.     0, 1,
  127.  
  128. /* ViewModes, */
  129.     HIRES | LACE,
  130.  
  131. /* Type, */
  132.     CUSTOMSCREEN,
  133.  
  134. /* Font */
  135.     NULL,
  136.  
  137. /* DefaultTitle */
  138.     NULL,
  139.  
  140. /* Gadgets */
  141.     NULL,
  142.  
  143. /* CustomBitMap */
  144.     NULL
  145. };
  146.  
  147.  
  148. static struct NewWindow NewPreviewWindow =
  149. {
  150.     0, 12,            /* window XY origin relative to TopLeft of
  151.                  * screen */
  152.     OPENWIDTH, OPENHEIGHT,    /* window width and height */
  153.     0, 1,            /* detail and block pens */
  154.  
  155.     GADGETUP |
  156.     GADGETDOWN |
  157.     NEWSIZE |
  158.     INTUITICKS |
  159.     CLOSEWINDOW,        /* IDCMP flags */
  160.  
  161.     WINDOWSIZING |
  162.     WINDOWDRAG |
  163.     WINDOWDEPTH |
  164.     WINDOWCLOSE |
  165.     SUPER_BITMAP |
  166.     GIMMEZEROZERO |
  167.     NOCAREREFRESH,        /* other window flags */
  168.  
  169.     Gadgets,            /* first gadget in gadget list */
  170.     NULL,            /* custom CHECKMARK imagery */
  171.     "hp2xx",            /* window title */
  172.     NULL,            /* custom screen pointer */
  173.     NULL,            /* custom bitmap */
  174.     MINWINWIDTH, MINWINHEIGHT,    /* minimum width and height */
  175.     MAXWINWIDTH, MAXWINHEIGHT,    /* maximum width and height */
  176.     CUSTOMSCREEN        /* destination screen type */
  177. };
  178.  
  179.  
  180. void
  181. brkfunc(int signo)
  182. {
  183.     fprintf(stderr, "Don't send a BREAK, click the close gadget!\n");
  184. }
  185.  
  186.  
  187.  
  188. /* This function provides a simple interface to ScrollLayer */
  189. VOID
  190. Slide_BitMap(struct Window * window, SHORT Dx, SHORT Dy)
  191. {
  192.     ScrollLayer(0, window->RPort->Layer, Dx, Dy);
  193. }
  194.  
  195. VOID
  196. Do_NewSize(struct Window * window)
  197. {
  198.     ULONG           tmp;
  199.  
  200.     tmp = GetLayerXOffset(window) + window->GZZWidth;
  201.     if (tmp >= width_super)
  202.     Slide_BitMap(window, width_super - tmp, 0);
  203.  
  204.     NewModifyProp(&Gadgets[LEFT_RIGHT_GADGET], window, NULL, AUTOKNOB | FREEHORIZ,
  205.           ((GetLayerXOffset(window) * MAXVAL) /
  206.            (width_super - window->GZZWidth)),
  207.           NULL,
  208.           ((window->GZZWidth * MAXVAL) / width_super),
  209.           MAXVAL,
  210.           1);
  211.  
  212.     tmp = GetLayerYOffset(window) + window->GZZHeight;
  213.     if (tmp >= height_super)
  214.     Slide_BitMap(window, 0, height_super - tmp);
  215.  
  216.     NewModifyProp(&Gadgets[UP_DOWN_GADGET], window, NULL, AUTOKNOB | FREEVERT,
  217.           NULL,
  218.           ((GetLayerYOffset(window) * MAXVAL) /
  219.            (height_super - window->GZZHeight)),
  220.           MAXVAL,
  221.           ((window->GZZHeight * MAXVAL) / height_super),
  222.           1);
  223. }
  224.  
  225. VOID
  226. Check_Gadget(struct Window * window, USHORT gadgetID)
  227. {
  228.     ULONG           tmp;
  229.     SHORT           dX = 0;
  230.     SHORT           dY = 0;
  231.     switch (gadgetID) {
  232.     case UP_DOWN_GADGET:
  233.     tmp = height_super - window->GZZHeight;
  234.     tmp = tmp * GadgetsSInfo[UP_DOWN_GADGET].VertPot;
  235.     tmp = tmp / MAXVAL;
  236.     dY = tmp - GetLayerYOffset(window);
  237.     break;
  238.     case LEFT_RIGHT_GADGET:
  239.     tmp = width_super - window->GZZWidth;
  240.     tmp = tmp * GadgetsSInfo[LEFT_RIGHT_GADGET].HorizPot;
  241.     tmp = tmp / MAXVAL;
  242.     dX = tmp - GetLayerXOffset(window);
  243.     break;
  244.     }
  245.     if (dX || dY)
  246.     Slide_BitMap(window, dX, dY);
  247. }
  248.  
  249. VOID
  250. Do_MainLoop(struct Window * window)
  251. {
  252.     struct IntuiMessage *msg;
  253.     SHORT           flag = TRUE;
  254.     USHORT          CurrentGadget = NO_GADGET;
  255.  
  256.     SetDrMd(window->RPort, JAM1);
  257.     Do_NewSize(window);
  258.     while (flag) {
  259.  
  260.     /* Whenever you want to wait on just one message port */
  261.     /* you can use WaitPort(). WaitPort() doesn't require */
  262.     /* the setting of a signal bit. The only argument it  */
  263.     /* requires is the pointer to the window's UserPort   */
  264.     WaitPort(window->UserPort);
  265.     while (msg = (struct IntuiMessage *) GetMsg(window->UserPort)) {
  266.         switch (msg->Class) {
  267.         case CLOSEWINDOW:
  268.         flag = FALSE;
  269.         break;
  270.         case NEWSIZE:
  271.         Do_NewSize(window);
  272.         break;
  273.         case GADGETDOWN:
  274.         CurrentGadget = GetGadgetID(msg);
  275.         break;
  276.         case GADGETUP:
  277.         Check_Gadget(window, CurrentGadget);
  278.         CurrentGadget = NO_GADGET;
  279.         break;
  280.         }
  281.         ReplyMsg((struct Message *) msg);
  282.     }
  283.     }
  284. }
  285.  
  286. void
  287. PicBuf_to_AMIGA(PicBuf * picbuf, PAR * p)
  288. {
  289.     int             row_c, x, y, xoff, yoff, color_index;
  290.     int             NoScreen = FALSE;
  291.     RowBuf         *row;
  292.  
  293.     struct BitMap  *BigOne;
  294.     struct Window  *window;
  295.     ULONG           RasterSize;
  296.     SHORT           Loop;
  297.     SHORT           Flag;
  298.  
  299.     if (picbuf->depth > 1)
  300.     NoScreen = TRUE;
  301.  
  302.     if (!p->quiet)
  303.     fprintf(stderr, "\nAMIGA preview follows.\n");
  304.  
  305.     xoff = p->xoff * p->dpi_x / 25.4;
  306.     yoff = p->yoff * p->dpi_y / 25.4;
  307.  
  308.  
  309.  
  310.     if (!p->quiet)
  311.     fprintf(stderr, "Current range: (%d..%d) x (%d..%d) pels\n",
  312.         xoff, (picbuf->nb << 3) + xoff, yoff, picbuf->nr + yoff);
  313.     if ((!p->quiet) &&
  314.     (((picbuf->nb << 3) + xoff > MAXWINWIDTH) || (picbuf->nr + yoff > MAXWINHEIGHT))) {
  315.     fprintf(stderr, "\n\007WARNING: Picture ist too large!\n");
  316.     fprintf(stderr, "Continue anyway (^C): ");
  317.     getchar();        /* Simple: Chance for ^C */
  318.     }
  319.     width_super = (picbuf->nb << 3) + xoff + LEFTBORDERSIZE + RIGHTBORDERSIZE;
  320.     height_super = picbuf->nr + yoff + TOPBORDERSIZE + BOTTOMBORDERSIZE;
  321.     if (width_super < MINWINWIDTH)
  322.     width_super = MINWINWIDTH;
  323.     if (height_super < MINWINHEIGHT)
  324.     height_super = MINWINHEIGHT;
  325.     if (width_super > MAXWINWIDTH) {
  326.     width_super = MAXWINWIDTH;
  327.     fprintf(stderr, "Error: Window width too big:%d\n", width_super);
  328.     exit(ERROR);
  329.     }
  330.     if (height_super > MAXWINHEIGHT) {
  331.     height_super = MAXWINHEIGHT;
  332.     fprintf(stderr, "Error: Window height too big:%d\n", height_super);
  333.     exit(ERROR);
  334.     }
  335.     NewPreviewWindow.Width = width_super;
  336.     NewPreviewWindow.Height = height_super;
  337.     NewPreviewWindow.MaxWidth = width_super;
  338.     NewPreviewWindow.MaxHeight = height_super;
  339.  
  340.     if (NewPreviewWindow.Width > NewPreviewWindow.MaxWidth)
  341.     NewPreviewWindow.Width = NewPreviewWindow.MaxWidth;
  342.  
  343.     if (NewPreviewWindow.Height > NewPreviewWindow.MaxHeight)
  344.     NewPreviewWindow.Height = NewPreviewWindow.MaxHeight;
  345.  
  346.     if (NewPreviewWindow.Width < NewPreviewWindow.MinWidth)
  347.     NewPreviewWindow.Width = NewPreviewWindow.MinWidth;
  348.  
  349.     if (NewPreviewWindow.Height < NewPreviewWindow.MinHeight)
  350.     NewPreviewWindow.Height = NewPreviewWindow.MinHeight;
  351.  
  352.     /*
  353.      * We don 't like a BREAK a beyond this point, the window would stay open
  354.      * and the memory allocated !
  355.      */
  356.     signal(SIGINT, brkfunc);
  357.  
  358.     if (IntuitionBase = (struct IntuitionBase *)
  359.     OpenLibrary("intuition.library", 33L)) {
  360.     if (GfxBase = (struct GfxBase *)
  361.         OpenLibrary("graphics.library", 33L)) {
  362.         if (LayersBase = (struct LayersBase *)
  363.         OpenLibrary("layers.library", 33L)) {
  364.         if (PreviewScreen = (struct Screen *) OpenScreen(&NewPreviewScreen)) {
  365.             if (BigOne = (struct BitMap *) AllocMem(sizeof(struct BitMap), MEMF_PUBLIC | MEMF_CLEAR)) {
  366.             InitBitMap(BigOne, DEPTH_SUPER, width_super, height_super);
  367.             RasterSize = BigOne->BytesPerRow * BigOne->Rows;
  368.             Flag = TRUE;
  369.             for (Loop = 0; Loop < DEPTH_SUPER; Loop++) {
  370.                 BigOne->Planes[Loop] = (PLANEPTR) AllocMem(RasterSize,
  371.                       MEMF_CHIP | MEMF_CLEAR | MEMF_PUBLIC);
  372.                 if (!BigOne->Planes[Loop])
  373.                 Flag = FALSE;
  374.             }
  375.             if (Flag) {
  376.                 NewPreviewWindow.BitMap = BigOne;
  377.                 NewPreviewWindow.Screen = PreviewScreen;
  378.                 if (window = (struct Window *) OpenWindow(&NewPreviewWindow)) {
  379.                 for (row_c = 0, y = picbuf->nr + yoff - 1; row_c < picbuf->nr; row_c++, y--) {
  380.                     if ((!p->quiet) && (row_c % 10 == 0))
  381.                     putc('.', stderr);
  382.  
  383.                     row = get_RowBuf(picbuf, picbuf->nr - row_c - 1);
  384.                     for (x = 0; x < picbuf->nc; x++) {
  385.                     color_index = index_from_RowBuf(row, x, picbuf);
  386.                     if (color_index != xxBackground) {
  387.                         SetAPen(window->RPort, color_index);
  388.                         WritePixel(window->RPort, x + xoff, row_c + yoff);
  389.                     }
  390.                     }
  391.                 }
  392.                 if ((!p->quiet))
  393.                     fprintf(stderr, "\nClick Close Gadget to continue!\n");
  394.  
  395.                 Do_MainLoop(window);
  396.  
  397.                 CloseWindow(window);
  398.                 }
  399.             } else {
  400.                 fprintf(stderr, "Error: Not enough graphics memory to allocate BitPlanes!\n");
  401.             }
  402.             for (Loop = 0; Loop < DEPTH_SUPER; Loop++) {
  403.                 if (BigOne->Planes[Loop]) {
  404.                 FreeMem(BigOne->Planes[Loop], RasterSize);
  405.                 }
  406.             }
  407.             FreeMem(BigOne, sizeof(struct BitMap));
  408.             } else {
  409.             fprintf(stderr, "Error: Not enough graphics memory to allocate BitPlane Structure!\n");
  410.             }
  411.             CloseScreen(PreviewScreen);
  412.         } else {
  413.             fprintf(stderr, "Oh! Couldn't open Screen!\n");
  414.         }
  415.         CloseLibrary((struct Library *) LayersBase);
  416.         } else {
  417.         fprintf(stderr, "Oh! Couldn't open Layers Library!\n");
  418.         }
  419.  
  420.         CloseLibrary((struct Library *) GfxBase);
  421.     } else {
  422.         fprintf(stderr, "Oh! Couldn't open Graphics Library!\n");
  423.     }
  424.     CloseLibrary((struct Library *) IntuitionBase);
  425.     } else {
  426.     fprintf(stderr, "Oh! Couldn't open Intuition Library!\n");
  427.     }
  428.     signal(SIGINT, SIG_DFL);
  429.   ERROR_EXIT_2:;
  430. }
  431.