home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / magic.lha / MAGIC / src / mdemo.c < prev    next >
C/C++ Source or Header  |  1993-06-20  |  16KB  |  622 lines

  1. /*
  2.  * MAGIC Image Tester - creates a small public MAGIC image with
  3.  *    a teeny tiny interface.
  4.  *
  5.  * Written by Thomas Krehbiel
  6.  *
  7.  * (This requires 2.0, BTW.)
  8.  *
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <libraries/dos.h>
  14. #include <intuition/intuition.h>
  15. #include <libraries/gadtools.h>
  16. #include <clib/exec_protos.h>
  17. #include <clib/alib_protos.h>
  18. #include <clib/dos_protos.h>
  19. #include <clib/intuition_protos.h>
  20. #include <clib/graphics_protos.h>
  21. #include <clib/gadtools_protos.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26.  
  27. #include <magic/magic.h>
  28. #include <magic/magic_protos.h>
  29. #include <magic/magic_pragmas.h>
  30.  
  31. struct Library *IntuitionBase;
  32. struct Library *GfxBase;
  33. struct Library *GadToolsBase;
  34.  
  35. struct MagicBase *MagicBase;
  36.  
  37. UBYTE *planes[4] = { NULL, NULL, NULL, NULL };
  38. struct MagicImage *mi;
  39. struct MagicHandle *mh;
  40. struct MsgPort *hostport;
  41.  
  42.  
  43. void message (char *, ...);
  44.  
  45.  
  46. #define WIDTH     320
  47. #define HEIGHT    200
  48. #define DEPTH     3        /* color */
  49.  
  50. short real_width, real_height;
  51.  
  52. BOOL alloc_planes (UBYTE **planes, int depth)
  53. {
  54.    int i;
  55.  
  56.    for (i = 0; i < depth; i++) planes[i] = NULL;
  57.  
  58.    for (i = 0; i < depth; i++) {
  59.       if (!(planes[i] = AllocMem(WIDTH * HEIGHT, MEMF_CLEAR | MEMF_PUBLIC))) {
  60.          return(FALSE);
  61.       }
  62.    }
  63.  
  64.    /* gradient from black to light violet */
  65.    for (i = 0; i < HEIGHT; i++) {
  66.       memset(planes[0] + (WIDTH * i), i * 255 / (HEIGHT-1), WIDTH);
  67.       memset(planes[1] + (WIDTH * i), i * 127 / (HEIGHT-1), WIDTH);
  68.       memset(planes[2] + (WIDTH * i), i * 255 / (HEIGHT-1), WIDTH);
  69.    }
  70.  
  71.    real_width = WIDTH;
  72.    real_height = HEIGHT;
  73.  
  74.    return(TRUE);
  75. }
  76.  
  77. void free_planes (UBYTE **planes, int depth)
  78. {
  79.    int i;
  80.  
  81.    for (i = 0; i < depth; i++) {
  82.       if (planes[i]) {
  83.          FreeMem(planes[i], real_width * real_height);
  84.          planes[i] = NULL;
  85.       }
  86.    }
  87. }
  88.  
  89.  
  90. static
  91. BOOL __saveds PutData (struct MagicImage *pi, LONG offset, LONG rows, LONG *tags)
  92. {
  93.    UBYTE *tagdata;
  94.    LONG byte;
  95.    UBYTE *ptr;
  96.    int i;
  97.  
  98.    while (*tags != TAG_END) {
  99.       tagdata = (UBYTE *)tags[1];
  100.       switch (*tags) {
  101.          case TAG_IGNORE :
  102.             break;
  103.          case GMI_Red :
  104.             CopyMem(tagdata, planes[0] + (offset * WIDTH), WIDTH * rows);
  105.             break;
  106.          case GMI_Green :
  107.             CopyMem(tagdata, planes[1] + (offset * WIDTH), WIDTH * rows);
  108.             break;
  109.          case GMI_Blue :
  110.             CopyMem(tagdata, planes[2] + (offset * WIDTH), WIDTH * rows);
  111.             break;
  112.          case GMI_RGB :
  113.             byte = offset * WIDTH;
  114.             ptr = (UBYTE *)tagdata;
  115.             for (i = 0; i < WIDTH; i++) {
  116.                planes[0][byte] = *ptr++;
  117.                planes[1][byte] = *ptr++;
  118.                planes[2][byte++] = *ptr++;
  119.             }
  120.             break;
  121.          case GMI_ARGB :
  122.             byte = offset * WIDTH;
  123.             ptr = (UBYTE *)tagdata;
  124.             for (i = 0; i < WIDTH; i++) {
  125.                ptr++;   /* skip alpha */
  126.                planes[0][byte] = *ptr++;
  127.                planes[1][byte] = *ptr++;
  128.                planes[2][byte++] = *ptr++;
  129.             }
  130.             break;
  131.          default :
  132.             break;
  133.       }
  134.       tags += 2;
  135.    }
  136.    return(TRUE);
  137. }
  138.  
  139. static
  140. BOOL __saveds GetData (struct MagicImage *pi, LONG offset, LONG rows, LONG *tags)
  141. {
  142.    UBYTE *tagdata;
  143.    LONG byte;
  144.    UBYTE *ptr;
  145.    int i;
  146.  
  147.    while (*tags != TAG_END) {
  148.       tagdata = (UBYTE *)tags[1];
  149.       switch (*tags) {
  150.          case TAG_IGNORE :
  151.             break;
  152.          case GMI_Red :
  153.             CopyMem(planes[0] + (offset * WIDTH), tagdata, WIDTH * rows);
  154.             break;
  155.          case GMI_Green :
  156.             CopyMem(planes[1] + (offset * WIDTH), tagdata, WIDTH * rows);
  157.             break;
  158.          case GMI_Blue :
  159.             CopyMem(planes[2] + (offset * WIDTH), tagdata, WIDTH * rows);
  160.             break;
  161.          case GMI_RGB :
  162.             byte = offset * WIDTH;
  163.             ptr = (UBYTE *)tagdata;
  164.             for (i = 0; i < WIDTH; i++) {
  165.                *ptr++ = planes[0][byte];
  166.                *ptr++ = planes[1][byte];
  167.                *ptr++ = planes[2][byte++];
  168.             }
  169.             break;
  170.          case GMI_ARGB :
  171.             byte = offset * WIDTH;
  172.             ptr = (UBYTE *)tagdata;
  173.             for (i = 0; i < WIDTH; i++) {
  174.                *ptr++ = 255;
  175.                *ptr++ = planes[0][byte];
  176.                *ptr++ = planes[1][byte];
  177.                *ptr++ = planes[2][byte++];
  178.             }
  179.             break;
  180.          default :
  181.             break;
  182.       }
  183.       tags += 2;
  184.    }
  185.    return(TRUE);
  186. }
  187.  
  188. BOOL init_magic (void)
  189. {
  190.    if (!(MagicBase = (struct MagicBase *)OpenLibrary(MAGIC_NAME, 34)))
  191.       return(FALSE);
  192.  
  193.    return(TRUE);
  194. }
  195.  
  196. void close_magic (void)
  197. {
  198.    if (mh) {
  199.       CloseMagicImage(mh);
  200.       mh = NULL;
  201.    }
  202.    if (mi) {
  203.       while (!RemMagicImage(mi)) {
  204.          message("Image in use!");
  205.          Delay(50);
  206.       }
  207.       FreeMagicImage(mi);
  208.       free_planes(planes, DEPTH);
  209.       mi = NULL;
  210.    }
  211. }
  212.  
  213. void cleanup_magic (void)
  214. {
  215.    close_magic();
  216.    CloseLibrary((struct Library *)MagicBase);
  217. }
  218.  
  219. void new_magic (void)
  220. {
  221.    close_magic();
  222.  
  223.    if (alloc_planes(planes, DEPTH)) {
  224.       if (mi = AllocMagicImage(AMI_Width, WIDTH,
  225.                                   AMI_Height, HEIGHT,
  226.                                   AMI_Depth, DEPTH,
  227.                                   AMI_Red, planes[0],
  228.                                   AMI_Green, planes[1],
  229.                                   AMI_Blue, planes[2],
  230.                                   AMI_GetDataCode, GetData,
  231.                                   AMI_PutDataCode, PutData,
  232.                                   AMI_OwnerName, "MagicDemo",
  233.                                   TAG_END)) {
  234.          if (AddMagicImage(mi)) {
  235.             /*
  236.              * Even the owner of an image must open it!  Remember to use
  237.              * OMI_OwnerPort instead of OMI_MsgPort.
  238.              */
  239.             if (mh = OpenMagicImage(mi, NULL, OMI_OwnerPort, hostport, TAG_END)) {
  240.                message("Magic image created.");
  241.                return;
  242.             }
  243.             RemMagicImage(mi);
  244.          }
  245.          FreeMagicImage(mi);
  246.       }
  247.       free_planes(planes, DEPTH);
  248.    }
  249.  
  250.    mi = NULL;
  251.    mh = NULL;
  252. }
  253.  
  254. struct NewMenu newMenus[] = {
  255.    { NM_TITLE, "Project",        NULL, 0, 0, 0 },
  256.    { NM_ITEM,  "New",            "N",  0, 0, 0 },
  257.    { NM_ITEM,  "Open...",        "O",  0, 0, 0 },
  258.    { NM_ITEM,  NM_BARLABEL,      NULL, 0, 0, 0 },
  259.    { NM_ITEM,  "Manipulate",     "M",  0, 0, 0 },
  260.    { NM_ITEM,  NM_BARLABEL,      NULL, 0, 0, 0 },
  261.    { NM_ITEM,  "Cycle",          "C",  0, 0, 0 },
  262.    { NM_ITEM,  NM_BARLABEL,      NULL, 0, 0, 0 },
  263.    { NM_ITEM,  "Quit",           "Q",  0, 0, 0 },
  264.    { NM_END }
  265. };
  266.  
  267. struct Window *win;
  268. struct Menu *menus;
  269. APTR vi;
  270.  
  271. BOOL init_gui (void)
  272. {
  273.    struct Screen *scr;
  274.  
  275.    IntuitionBase = OpenLibrary("intuition.library", 37);
  276.    GfxBase = OpenLibrary("graphics.library", 37);
  277.    GadToolsBase = OpenLibrary("gadtools.library", 37);
  278.    /* (yes, I know, but it's just a demo) */
  279.  
  280.    scr = LockPubScreen(NULL);
  281.    if (scr == NULL) return(FALSE);
  282.  
  283.    if (!(win = OpenWindowTags(NULL,
  284.                               WA_Title, "MAGIC Testerosa",
  285.                               WA_InnerWidth, 200,
  286.                               WA_Height, 115 + scr->WBorTop + scr->Font->ta_YSize + 1 + scr->WBorBottom + scr->RastPort.TxHeight + 2,
  287.                               WA_Left, 20,
  288.                               WA_Top, 20,
  289.                               WA_Flags, WFLG_CLOSEGADGET | WFLG_DRAGBAR |
  290.                                           WFLG_DEPTHGADGET |
  291.                                           WFLG_SMART_REFRESH | WFLG_NOCAREREFRESH |
  292.                                           WFLG_ACTIVATE,
  293.                               WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_MENUPICK,
  294.                               WA_NewLookMenus, TRUE,
  295.                               TAG_END))) goto err1;
  296.  
  297.    menus = CreateMenus(newMenus, TAG_END);
  298.    if (menus == NULL) goto err2;
  299.  
  300.    vi = GetVisualInfo(scr, TAG_END);
  301.    if (vi == NULL) goto err3;
  302.  
  303.    LayoutMenus(menus, vi, GTMN_NewLookMenus, TRUE, TAG_END);
  304.  
  305.    SetMenuStrip(win, menus);
  306.  
  307.    UnlockPubScreen