home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 310.lha / Icon2c_v1.0 / icon2c.c < prev    next >
C/C++ Source or Header  |  1980-12-06  |  8KB  |  298 lines

  1. ;/*
  2. FailAt 1
  3. LC -v -iINCLUDE:CompactH/ icon2c.c
  4. Blink FROM lib:c.o icon2c.o TO Icon2c LIB lib:lc.lib SC SD ND
  5. Quit
  6.  
  7.  
  8.     Icon2c - created by Ray Lambert on 10-23-89
  9.  
  10.  
  11.     Purpose: read an icon file (Workbench DiskObject) and convert it to
  12.               c labguage constant data statements for inclusion in programs
  13.               that would like to save an icon with data files.
  14.  
  15.     Usage (CLI only): Icon2c <iconfile[.info]> <cfile[.c]>
  16.  
  17. */
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <dos.h>
  24. #include <intuition/intuition.h>
  25. #include <graphics/gfx.h> /* for RASSIZE macro definition */
  26. #include <workbench/workbench.h>
  27. #include <workbench/icon.h>
  28. #include <proto/exec.h>
  29. #include <proto/icon.h>
  30.  
  31.  
  32. struct Library *IconBase  = NULL;
  33. struct DiskObject *dO     = NULL;
  34. FILE *file                = NULL;
  35.  
  36. struct DrawerData *dd;
  37. struct Gadget *g;
  38. char *c;
  39. char **cp;
  40. int i;
  41.  
  42.  
  43. void bustit(void)
  44. {
  45.   if (file) fclose(file);
  46.   if (dO) FreeDiskObject(dO);
  47.   if (IconBase) CloseLibrary(IconBase);
  48.   exit(0);
  49. }
  50.  
  51. #define WORDSPERROW 10
  52.  
  53. void dumpimg(struct Image *img, char *namepfx)
  54. {
  55.   int imgwords = ( RASSIZE(img->Width,img->Height) * img->Depth );
  56.   int rows, last, cnt;
  57.   USHORT *s = img->ImageData;
  58.  
  59. /* dump the image data array */
  60.   fprintf(file,"USHORT %sImageData[] =\n\t{\n",namepfx);
  61.   rows = (imgwords / WORDSPERROW); /* number of rows of hex words to dump */
  62.   last = (imgwords % WORDSPERROW); /* number of hex words on the last line*/
  63.   for(; rows>0; rows--)
  64.     {
  65.       fprintf(file,"\t\t");
  66.       for(cnt = 0; cnt < WORDSPERROW; cnt++)
  67.         {
  68.           if (cnt > 0) fprintf(file,",");
  69.           fprintf(file,"0x%04X", *s);
  70.           s++;
  71.         }
  72.       if (rows > 1)
  73.         {
  74.           fprintf(file,",\n");
  75.         }
  76.       else
  77.         {
  78.           if (last != 0) fprintf(file,",");
  79.           fprintf(file,"\n");
  80.         }
  81.     }
  82.   if (last > 0)
  83.     {
  84.       fprintf(file,"\t\t");
  85.       for(; last>0; last--, s++)
  86.         {
  87.           fprintf(file,"0x%04X", *s);
  88.           if (last > 1) fprintf(file,",");
  89.         }
  90.       fprintf(file,"\n");
  91.     }
  92.   fprintf(file,"\t};\n\n");
  93.  
  94. /* dump the struct Image */
  95.   fprintf(file,"struct Image %sImage =\n\t{\n", namepfx);
  96.   fprintf(file,"\t\t%hd, %hd, %hd, %hd, %hd,\n",
  97.       img->LeftEdge, img->TopEdge, img->Width, img->Height, img->Depth );
  98.   fprintf(file,"\t\t%sImageData,\n", namepfx);
  99.   fprintf(file,"\t\t%hd, %hd,\n", (USHORT)img->PlanePick,
  100.                                   (USHORT)img->PlaneOnOff );
  101.   fprintf(file,"\t\tNULL\n\t};\n\n");
  102. }
  103.  
  104.  
  105. void main(int argc, char *argv[])
  106. {
  107.  
  108. /* check for Workbench */
  109.   if (argc == 0)  /* Workbench! */
  110.     {
  111.       printf("\nCLI ONLY!!\n"); /* just in case someone's listening */
  112.       exit(0);
  113.     }
  114.  
  115.   printf("\n›33;40mIcon2c Version 1.0 - Created by Ray Lambert›0m\n\n");
  116.  
  117. /* check args */
  118.   if (argc != 3)
  119.     {
  120.       printf("Usage: Icon2c <iconfile[.info]> <c file>\n\n");
  121.       exit(5);
  122.     }
  123.  
  124. /* open icon library */
  125.   IconBase = (struct Library *)OpenLibrary("icon.library",0);
  126.   if (!IconBase)
  127.     {
  128.       printf("\"icon.library\" is on vacation!\n\n");
  129.       exit(20);
  130.     }
  131.  
  132. /* read the icon */
  133.   /* check for & remove file extension (.info) */
  134.   c = (char *)(argv[1] + strlen(argv[1]) - 5);
  135.   if (stricmp(c,".info") == 0) *c = '\0';
  136.   /* ok now, go get it */
  137.   dO = GetDiskObject(argv[1]);
  138.   if (!dO)
  139.     {
  140.       printf("Sorry, that icon (\"%s\") isn't around...\n\n",argv[1]);
  141.       bustit();
  142.     }
  143.  
  144. /* open the output file */
  145.   file = fopen(argv[2],"wt");
  146.   if (!file)
  147.     {
  148.       printf("Can't open your c file (\"%s\")\n\n",argv[2]);
  149.       bustit();
  150.     }
  151.  
  152. /* ok, got everything at our fingertips now */
  153.  
  154.   printf("Working...");
  155.  
  156.   /* say hello... */
  157.   fprintf(file,"/*\n");
  158.   fprintf(file,"** \"%s.info\" - dumped by Icon2c!\n",argv[1]);
  159.   fprintf(file,"*/\n\n");
  160.  
  161.   /* some headers they may need... */
  162.   fprintf(file,"#include <workbench/workbench.h>\n");
  163.   fprintf(file,"#include <intuition/intuition.h>\n\n");
  164.  
  165. /* start with the loose stuff... */
  166.  
  167. /* Render imagery */
  168.   g = &dO->do_Gadget;
  169.   if (g->GadgetRender)
  170.     {
  171.       dumpimg((struct Image *)g->GadgetRender,"Render");
  172.     }
  173.  
  174. /* Select imagery */
  175.   if (g->SelectRender)
  176.     {
  177.       dumpimg((struct Image *)g->SelectRender,"Select");
  178.     }
  179.  
  180. /* the ToolTypes array */
  181.   cp = dO->do_ToolTypes;
  182.   if ( (cp) && (*cp) )
  183.     {
  184.       fprintf(file,"char *ToolTypes[] = \n\t{\n");
  185.       while(*cp)
  186.         {
  187.           fprintf(file,"\t\t\"%s\",\n",*cp);
  188.           cp++;
  189.         }
  190.       fprintf(file,"\t\tNULL\n\t};\n\n");
  191.     }
  192.  
  193. /* the DrawerData structure (for drawers only) */
  194.   dd = dO->do_DrawerData;
  195.   if (dd)
  196.     {
  197.       register struct NewWindow *nw = &dd->dd_NewWindow;
  198.       fprintf(file,"struct DrawerData drawerData =\n\t{\n");
  199.       fprintf(file,"\t\t{  /* struct NewWindow */\n");
  200.       fprintf(file,"\t\t\t%hd, %hd, %hd, %hd,\n", nw->LeftEdge, nw->TopEdge,
  201.                                                   nw->Width,    nw->Height );
  202.       fprintf(file,"\t\t\t%hd, %hd,\n", (USHORT)nw->DetailPen,
  203.                                         (USHORT)nw->BlockPen  );
  204.       fprintf(file,"\t\t\t0x%08X,\n", nw->IDCMPFlags );
  205.       fprintf(file,"\t\t\t0x%08X,\n", nw->Flags );
  206.       fprintf(file,"\t\t\tNULL,\n");
  207.       fprintf(file,"\t\t\tNULL,\n");
  208.       fprintf(file,"\t\t\tNULL,\n");
  209.       fprintf(file,"\t\t\tNULL,\n");
  210.       fprintf(file,"\t\t\tNULL,\n");
  211.       fprintf(file,"\t\t\t%hd, %hd, %hd, %hd,\n",
  212.                             nw->MinWidth, nw->MinHeight,
  213.                             nw->MaxWidth, nw->MaxHeight );
  214.       fprintf(file,"\t\t\t%hd\n\t\t},\n", nw->Type);
  215.  
  216.       fprintf(file,"\t\t%ld,\n", dd->dd_CurrentX);
  217.       fprintf(file,"\t\t%ld\n\t};\n\n", dd->dd_CurrentY);
  218.     }
  219.  
  220. /* ok, on to the big guy! */
  221.  
  222.   /* figure a name for the struct based on icon's name */
  223.   c = (char *)(argv[1] + strlen(argv[1]) - 1);
  224.   while( (c > argv[1]) && (*c != '/') && (*c != ':') )
  225.     {
  226.       if (*c == '.') *c = '_';
  227.       c--;
  228.     }
  229.   if ( (*c == '/') || (*c == ':') ) c++;
  230.   fprintf(file,"struct DiskObject do_%s =\n\t{\n", c);
  231.   fprintf(file,"\t\t0x%04X,\n", (ULONG)dO->do_Magic);
  232.   fprintf(file,"\t\t0x%04X,\n", (ULONG)dO->do_Version);
  233.  
  234.   fprintf(file,"\t\t{ /* struct Gadget */\n");
  235.   fprintf(file,"\t\t\tNULL,\n");
  236.   fprintf(file,"\t\t\t%hd, %hd, %hd, %hd,\n", g->LeftEdge, g->TopEdge,
  237.                                               g->Width,    g->Height  );
  238.   fprintf(file,"\t\t\t0x%04X,\n", (ULONG)g->Flags);
  239.   fprintf(file,"\t\t\t0x%04X,\n", (ULONG)g->Activation);
  240.   fprintf(file,"\t\t\t0x%04X,\n", (ULONG)g->GadgetType);
  241.   fprintf(file,"\t\t\t");
  242.   if (g->GadgetRender != NULL)
  243.     {
  244.       fprintf(file,"(APTR)&RenderImage,\n");
  245.     }
  246.   else
  247.     {
  248.       fprintf(file,"NULL,\n");
  249.     }
  250.   fprintf(file,"\t\t\t");
  251.   if (g->SelectRender != NULL)
  252.     {
  253.       fprintf(file,"(APTR)&SelectImage,\n");
  254.     }
  255.   else
  256.     {
  257.       fprintf(file,"NULL,\n");
  258.     }
  259.   fprintf(file,"\t\t\tNULL,\n");
  260.   fprintf(file,"\t\t\t0x%08X,\n", g->MutualExclude);
  261.   fprintf(file,"\t\t\tNULL,\n");
  262.   fprintf(file,"\t\t\t0x%04X,\n", (ULONG)g->GadgetID);
  263.   fprintf(file,"\t\t\tNULL\n\t\t},\n");
  264.  
  265.   fprintf(file,"\t\t%d,\n", dO->do_Type);
  266.   fprintf(file,"\t\t\"%s\",\n", dO->do_DefaultTool);
  267.   fprintf(file,"\t\t");
  268.   if ( (dO->do_ToolTypes != NULL) && (*dO->do_ToolTypes != NULL) )
  269.     {
  270.       fprintf(file,"ToolTypes,\n");
  271.     }
  272.   else
  273.     {
  274.       fprintf(file,"NULL,\n");
  275.     }
  276.   fprintf(file,"\t\t%ld,\n", dO->do_CurrentX);
  277.   fprintf(file,"\t\t%ld,\n", dO->do_CurrentY);
  278.   fprintf(file,"\t\t");
  279.   if (dO->do_DrawerData != NULL)
  280.     {
  281.       fprintf(file,"&drawerData,\n");
  282.     }
  283.   else
  284.     {
  285.       fprintf(file,"NULL,\n");
  286.     }
  287.   fprintf(file,"\t\t\"%s\",\n", dO->do_ToolWindow);
  288.   fprintf(file,"\t\t%ld\n\t};\n\n", dO->do_StackSize);
  289.  
  290. /* welp, guess that's it! */
  291.   printf(" ok dude!  you got it...\n\n");
  292.   bustit();
  293. }
  294.  
  295.  
  296.  
  297.  
  298.