home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 597b.lha / WorldDataBank_v2.2 / source / wdb.c < prev    next >
C/C++ Source or Header  |  1991-12-18  |  8KB  |  311 lines

  1. /***************** Display World Data Bank ********************/
  2. #include    <clib/macros.h>
  3. #include    <functions.h>
  4. #include    <rjd/rjd.h>
  5. #include    <intuition/intuition.h>
  6. #include    <libraries/dosextens.h>
  7. #include    <exec/memory.h>
  8. #include    <wdb/req.h>
  9.  
  10. #define    QSPAN    90*6000
  11. #define HSPAN    QSPAN*2
  12. #define WSPAN    QSPAN*4
  13. #define gg_ix   gg_addr->GadgetID
  14. #define False 0
  15.  
  16. struct IntuitionBase *IntuitionBase;
  17. struct GfxBase    *GfxBase;
  18. struct Screen    *s;
  19. struct Window    *w,*Contr,*win;
  20. struct RastPort *rp;
  21. struct Gadget    *gg_addr;
  22.  
  23. typedef struct { short Code, Lat, Lon; } PNT ;
  24. #define PNTSiz    sizeof(PNT)
  25. #define PCount    1024
  26.  
  27. PNT    *p;
  28. long    Latitude, Longitude,
  29.         X_Scale, Y_Scale,
  30.         val,
  31.         ymax,ymin,xmax,xmin,xspan,yspan;
  32. double zoom,atof();
  33. char    *infile;
  34. short    dbug=1;
  35. ULONG    class;
  36.  
  37. short CTable[] = {
  38.     0x0000, 0x0ddd, 0x00a0, 0x0d00, 0x0ff0, 0x05f5, 0x0aaf, 0x033f
  39. };
  40.  
  41. long colr[] = { 2, 2, 3, 0, 4, 5, 6, 7 };    /* pen = line type DIV 1000 */
  42.  
  43. struct TextAttr ta = {
  44.     (STRPTR)"topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT
  45. };
  46.  
  47. struct NewScreen ns = {
  48.     0,0,640,400,3,3,1,HIRES|LACE,CUSTOMSCREEN,&ta,
  49.     (UBYTE *)"Micro WorldDataBank-II",NULL,NULL
  50. };
  51.  
  52. struct NewWindow nw = {
  53.     0,0,640,400,-1,-1,MENUPICK,NULL,
  54.     NULL,NULL,NULL,NULL,NULL,0,0,0,0,CUSTOMSCREEN
  55. };
  56.  
  57. struct IntuiText quitIT = { 3,2,JAM1,2,1,&ta,(UBYTE *)"Quit" ,NULL },
  58.          prntIT = { 3,2,JAM1,2,1,&ta,(UBYTE *)"Print",NULL },
  59.          loadIT = { 3,2,JAM1,2,1,&ta,(UBYTE *)"Load" ,NULL },
  60.          titlIT = { 3,2,JAM1,2,1,&ta,(UBYTE *)"Title",NULL },
  61.          specIT = { 3,2,JAM1,2,1,&ta,(UBYTE *)"Specs",NULL };
  62.  
  63. #define MenuFlags  ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP
  64.  
  65. struct MenuItem
  66.         quitMI = {    NULL,0,40,99,11,MenuFlags,NULL,
  67.                     (APTR)&quitIT,NULL,(BYTE)'Q',NULL },
  68.         prntMI = { &quitMI,0,30,99,11,MenuFlags,NULL,
  69.                     (APTR)&prntIT,NULL,(BYTE)'P',NULL },
  70.         loadMI = { &prntMI,0,20,99,11,MenuFlags,NULL,
  71.                     (APTR)&loadIT,NULL,(BYTE)'L',NULL },
  72.         titlMI = { &loadMI,0,10,99,11,MenuFlags,NULL,
  73.                     (APTR)&titlIT,NULL,(BYTE)'T',NULL },
  74.         specMI = { &titlMI,0, 0,99,11,MenuFlags,NULL,
  75.                     (APTR)&specIT,NULL,(BYTE)'S',NULL };
  76.  
  77. struct Menu m = { NULL,0,0,64,11,MENUENABLED,(BYTE *)"Control",&specMI };
  78.  
  79. /************************** clean up **********************/
  80. void quit()
  81. {
  82.     if (w) {
  83.         ClearMenuStrip(w);
  84.         CloseWindow(w);
  85.     }
  86.     if(Contr) CloseWindow(Contr);
  87.     FreeMem(p,PCount*PNTSiz);
  88.     if (s) CloseScreen(s);
  89.     CloseLibrary(GfxBase);
  90.     CloseLibrary(IntuitionBase);
  91.     exit(0);
  92. }
  93.  
  94. /************************* initialization ***********************/
  95. void init_gfx()
  96. {
  97.     IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",0L);
  98.     GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",0L);
  99.  
  100.     if (!(s = OpenScreen(&ns)))
  101.         quit();
  102.     LoadRGB4(&s->ViewPort, &CTable, 8L);
  103.     nw.Screen = s;
  104.     if (!(w = OpenWindow(&nw)))
  105.         quit();
  106.     SetMenuStrip(w,&m);
  107.     rp = w->RPort;
  108. }
  109.  
  110. /***********************************************************/
  111. SetSpecs()
  112. {
  113.  
  114.     Latitude  = -((((CrossGgtSInfo.VertPot  + 1) * 180) >> 16) - 90 ) * 6000;
  115.     Longitude = ((((CrossGgtSInfo.HorizPot + 1) * 360) >> 16) - 180 ) * 6000;
  116.     zoom      = atof(StrGgtSIBuff);
  117.     val = zoom * 10.0;
  118.     X_Scale   = zoom * 120.0 / 33.75;    /* 33.75 = 10800 / 320 */
  119.     Y_Scale   = zoom * 100.0 / 26.00;    /* 27.00 =  5400 / 200 */
  120.     ymax = QSPAN/zoom;
  121.     ymin = -ymax;
  122.     xmax = HSPAN / (zoom * 1.2);
  123.     xmin = -xmax;
  124.     xspan = xmax - xmin;
  125.     yspan = ymax - ymin;
  126. }
  127.  
  128.  
  129. _wb_parse() {}
  130.  
  131. OpenContrWindow()
  132. {
  133. nw.Flags = SMART_REFRESH | ACTIVATE | WINDOWDRAG | WINDOWDEPTH;
  134. nw.IDCMPFlags = GADGETUP | VANILLAKEY;
  135. nw.TopEdge = 200;
  136. nw.LeftEdge = 10;
  137. nw.Width = 250;
  138. nw.Height = 120;
  139. nw.FirstGadget = &CrossGgt;
  140. Contr = (struct Window *)OpenWindow(&nw);
  141. }
  142.  
  143.  
  144. /***************************** main routine ********************************/
  145. main(argc, argv)
  146. int argc;
  147. char *argv[];
  148. {
  149.     void load();
  150.     struct IntuiMessage *im;
  151.     long title = FALSE,
  152.         MsgBits,MainMsgBit, ContrMsgBit;
  153.     USHORT code;
  154.  
  155.     if (argv[1][1] == '?')
  156.       {
  157.         Write(Output(), "Usage: WDB data_file\n", 14L);
  158.         exit(0);
  159.       }
  160.     if (argc == 2)
  161.         infile = argv[1];
  162.     else
  163.         infile = "wdb.3.all";
  164.  
  165.     if ((p = (USHORT *)AllocMem(PCount*PNTSiz,MEMF_FAST)) == NULL)
  166.     {
  167.         Write(Output(), "Insufficient memory for buffer\n", 31L);
  168.         exit(0);
  169.     }
  170.     init_gfx();
  171.     OpenContrWindow();
  172.     MainMsgBit = 1 << w->UserPort->mp_SigBit;
  173.     ContrMsgBit = 1 << Contr->UserPort->mp_SigBit;
  174.     FOREVER
  175.       { MsgBits = Wait(MainMsgBit | ContrMsgBit);
  176.           if(MsgBits & MainMsgBit)
  177.             win = w;
  178.         else if (MsgBits & ContrMsgBit)
  179.             win = Contr;
  180.         while (im = (struct IntuiMessage *) GetMsg(win->UserPort))
  181.           { code = im->Code;
  182.             class = im->Class;
  183.             gg_addr = im->IAddress;
  184.             ReplyMsg(im);
  185.             switch(class)
  186.               {    case GADGETUP:
  187.                       switch(gg_ix)
  188.                       {    case gidOK:
  189. draw_map:                    SetSpecs();                 /* redo parameters    */
  190.                             WindowToBack(Contr);
  191.                             SetAPen(rp,0);     /* clear screen    */
  192.                             RectFill(rp,0,0,639,399);
  193.                             SetAPen(rp,1);
  194.                             load(infile);         /* redisplay map    */
  195.                             ActivateWindow(Contr);
  196.                             WindowToFront(Contr);
  197.                               break;
  198.                         case gidQUIT:
  199.                             goto exit;
  200.                         case gidCROSS:
  201.                             break;
  202.                           case gidMAG:
  203.                             zoom = ((MagGgtSInfo.HorizPot+1) * 30.0) / 0xffff;
  204.                             val = 10.0 * zoom;
  205.                             if(zoom < .5) 
  206.                                 zoom = .833;
  207.                             sprintf(StrGgtSIBuff,"%5.2f",zoom);
  208.                             RefreshGadgets(&StrGgt,Contr,NULL,1);
  209.                               break;
  210.                         case gidSTR:
  211.                             zoom = atof(StrGgtSIBuff);
  212.                             val = 10.0 * zoom;
  213.                             ModifyProp(&MagGgt,Contr,NULL,MagGgtSInfo.Flags,
  214.                              (short)((zoom * 0xffff)/30.0 - 1.0),
  215.                              MagGgtSInfo.VertPot,
  216.                              MagGgtSInfo.HorizBody,
  217.                              MagGgtSInfo.VertBody);
  218.                             break;
  219.                       }
  220.                     break;
  221.                 case VANILLAKEY:
  222.                     switch(code)
  223.                       {
  224.                         case 'c':
  225.                             ActivateGadget(&StrGgt,Contr,NULL);
  226.                             break;
  227.                         case 'o':
  228.                             goto draw_map;
  229.                         case 'q':
  230.                             goto exit;
  231.                         default:
  232.                             break;
  233.                       }
  234.                     break;
  235.                 case MENUPICK:
  236.                     switch (ITEMNUM(code))
  237.                       {    case 0:                     /* new specs    */
  238.                             WindowToFront(Contr);
  239.                             break;
  240.                         case 1:
  241.                             title ^= TRUE;
  242.                             ShowTitle(s, title);
  243.                             break;
  244.                         case 3:                        /* print */
  245.                             break;
  246.                         case 4:
  247. exit:                        quit();
  248.                             break;
  249.                       }
  250.               }
  251.           }
  252.       }
  253. }
  254.  
  255. /***************************** nitty gritty **********************************/
  256. void load(fn)
  257.     char *fn;
  258. {
  259.     register long x, y;
  260.     register PNT    *pp;
  261.     PNT    *pend;
  262.     struct FileHandle *in;
  263.     long    z,LonPrv=0,LatPrv=0,xprv=0,yprv;
  264.     short    newseg=0,n;
  265.  
  266.     if (in = Open(fn, MODE_OLDFILE)) {
  267.         while (n = (short) Read(in, p, (long)PCount*PNTSiz))
  268.         {
  269.             for (pp = p,pend = p+n/6; pp < pend; pp++)
  270.             {                    /* do displacement    */
  271.                 x = pp->Lon*100 - Longitude;
  272.                 y = pp->Lat*100 - Latitude;
  273.                                 /* wrap around for East-West    */
  274.                 if (x < -HSPAN)
  275.                     x += WSPAN;
  276.                 else if (x > HSPAN)
  277.                     x -= WSPAN;
  278.                                 /* ignore pts outside magnified area    */
  279.                 if((x < xmin || x > xmax || y < ymin || y > ymax))
  280.                   {    newseg = 1;
  281.                     continue;
  282.                   }
  283.                 if(ABS(x - LonPrv) >= xspan || ABS(y - LatPrv) >= yspan)
  284.                     newseg = 1;
  285.                 LonPrv = x;
  286.                 LatPrv = y;
  287.                                 /* scale pts w/in area to interlace screen    */
  288.                 x = 320 + (x * X_Scale)/10000;
  289.                 y = 200 - (y * Y_Scale)/10000;
  290.                                 /* ignore duplicates    */
  291.                 if (x == xprv && y == yprv)
  292.                     continue;
  293.                                 /* if over edge, set for return    */
  294.  
  295.                 if (newseg || pp->Code > 5 || ABS(z - pp->Lon) > 10800)
  296.                   {    SetAPen(rp, colr[pp->Code / 1000]);
  297.                     Move(rp, x, y);
  298.                     WritePixel(rp, x, y);
  299.                     newseg = 0;
  300.                   }
  301.                 else
  302.                     Draw(rp, x, y);
  303.                 xprv = x;
  304.                 yprv = y;
  305.                 z = pp->Lon;
  306.             }
  307.         }
  308.         Close(in);
  309.     }
  310. }
  311.