home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d7xx / d762 / plotmap.lha / PlotMap / source.lha / draw.c < prev    next >
C/C++ Source or Header  |  1992-10-09  |  15KB  |  436 lines

  1. /* drawing routines for PlotMap */
  2.  
  3. #include "PlotMap.h"
  4.  
  5. /********** externals **********/
  6.  
  7. extern struct mapinfo map[];
  8. extern struct point *loadmap_buffer;
  9. extern struct Screen *screen;
  10. extern struct RastPort *rp;
  11. extern struct config config;
  12.  
  13. extern BPTR get_map_fhd(UWORD);
  14.  
  15. /********** Variablen, Daten **********/
  16.  
  17. UWORD old_maptype = ~0;             /* ~0 indicates no map */
  18. WORD detail_level;
  19.  
  20. double act_vfactor, m_vfactor;
  21. UWORD centerx, centery, half_scr_width;
  22. WORD box_x1 = -180*60, box_x2 = 180*60;
  23. WORD box_y1 = 90*60, box_y2 = -90*60;
  24.  
  25. /********** Routinen **********/
  26.  
  27. void get_detail_level(void)
  28. {
  29.    WORD bw, dlm;
  30.  
  31.    if (config.detail_level <= DL_LOW)
  32.       detail_level = config.detail_level;
  33.    else
  34.    {                                   /* auto detail level */
  35.       bw = box_x2 - box_x1;
  36.       dlm = DL_LOW+1;
  37.  
  38.       if (bw < 150*60)
  39.          dlm = DL_LOW;
  40.       if (bw < 70*60)
  41.          dlm = DL_MEDIUMLOW;
  42.       if (bw < 35*60)
  43.          dlm = DL_MEDIUM;
  44.       if (bw < 15*60)
  45.          dlm = DL_MEDIUMHIGH;
  46.  
  47.       detail_level = dlm - (DL_AUTO_LOW - config.detail_level);
  48.  
  49.       if (detail_level > DL_LOW)
  50.          detail_level = DL_LOW;
  51.       if (detail_level < DL_HIGH)
  52.          detail_level = DL_HIGH;
  53.    }
  54. }
  55.  
  56.  
  57.  
  58. void draw_fullmap(UWORD type)
  59. /* type can be MAP_FLAT or MAP_MERCATOR */
  60. {
  61.    WORD x, y, old_x = -1, old_y = -1, width, height, vfac2;
  62.    LONG t2;
  63.    UWORD anz, map_zhl, seg_zhl;
  64.    ULONG first, last, pt_zhl;
  65.    BOOL new_seg, error;
  66.    BPTR fhd = NULL;
  67.    double t;
  68.    struct arc *seg;
  69.    struct point *pt;
  70.    struct IntuiMessage *msg;
  71.    BOOL abort = FALSE;
  72.  
  73.    RMBTRAP_ON;
  74.    IDCMP(IDCMP_MOUSEBUTTONS);
  75.    MOUSE_WAIT;
  76.    TITLE_MSG("Drawing map, press RMB to abort");
  77.  
  78.    box_x1 = -180*60;
  79.    box_x2 = 180*60;
  80.    box_y1 = 90*60;
  81.    box_y2 = -90*60;
  82.  
  83.    get_detail_level();
  84.    width = screen->Width;
  85.    height = screen->Height;
  86.    vfac2 = (WORD)(180.0*60.0 * act_vfactor);
  87.  
  88.    old_maptype = type | MAP_PLANE;
  89.    OnMenu(main_wd, FULLMENUNUM(3,1,NOSUB));     /* Box */
  90.    OffMenu(main_wd, FULLMENUNUM(3,1,3));        /* Box zoom */
  91.    OffMenu(main_wd, FULLMENUNUM(3,1,4));
  92.    OffMenu(main_wd, FULLMENUNUM(3,2,3));        /* Sphere zoom */
  93.    OffMenu(main_wd, FULLMENUNUM(3,2,4));
  94.    OnMenu(main_wd, FULLMENUNUM(3,4,NOSUB));     /* Redraw */
  95.  
  96.    if (config.cls_before_draw)
  97.       SetRast(rp, config.bg_color);                   /* clear screen */
  98.  
  99.    for (map_zhl = 0; map_zhl < NUM_MAPS; map_zhl++)   /* do each map type */
  100.    {
  101.       if (config.plotmap[map_zhl] && !abort)    /* if not requested, skip */
  102.       {
  103.          error = FALSE;
  104.          SetAPen(rp, config.mapcolor[map_zhl]); /* initialize color */
  105.          seg = map[map_zhl].seg;
  106.  
  107.          if (config.loadmap[map_zhl])           /* map loaded? */
  108.             pt = map[map_zhl].pt;
  109.          else
  110.          {
  111.             pt = loadmap_buffer;                /* no, open map */
  112.             if (!(fhd = get_map_fhd(map_zhl)))
  113.             {
  114.                TITLE_ERROR("Can`t open map file!");
  115.                error = TRUE;
  116.             }
  117.          } /* if (config.loadmap[]) */
  118.                                                 /* do each segment */
  119.          for (seg_zhl = 0; seg_zhl < map[map_zhl].nsegs && !error; seg_zhl++)
  120.          {
  121.             /* check for user abort */
  122.             if (msg = (struct IntuiMessage *)GetMsg(main_wd->UserPort))
  123.             {
  124.                if ((msg->Class == IDCMP_MOUSEBUTTONS) && (msg->Code == MENUDOWN))
  125.                {
  126.                   ReplyMsg((struct Message *)msg);    /* user aborted */
  127.                   DisplayBeep(NULL);
  128.                   abort = TRUE;
  129.                   break;
  130.                }
  131.                ReplyMsg((struct Message *)msg);
  132.             } /* if (GetMsg()) */
  133.  
  134.             if (config.loadmap[map_zhl])        /* map loaded? */
  135.             {
  136.                first = seg[seg_zhl].first;      /* somewhere from the */
  137.                last = seg[seg_zhl].last;        /* whole map */
  138.             }
  139.             else
  140.             {                    /* load whole next segment into buffer */
  141.                anz = seg[seg_zhl].last - seg[seg_zhl].first + 1;
  142.                if (Read(fhd, loadmap_buffer, anz*sizeof(struct point)) !=
  143.                      anz * sizeof(struct point))
  144.                {
  145.                   TITLE_ERROR("Can`t read map file!");
  146.                   error = TRUE;
  147.                   break;                        /* exit seg_zhl-loop */
  148.                }
  149.  
  150.                first = 0;              /* from beginning of load-buffer */
  151.                last = anz-1;
  152.             } /* if (config.loadmap[]) */
  153.  
  154.             new_seg = TRUE;            /* we start a new segment */
  155.  
  156.             for (pt_zhl = first; pt_zhl <= last; pt_zhl++)
  157.             {
  158.                if (pt[pt_zhl].code >= detail_level)
  159.                {                                /* filter by detail level */
  160.                   if (type == MAP_FLAT)
  161.                   {
  162.                      t2 = pt[pt_zhl].lat * height;    /* y = lattitude */
  163.                      if (t2 < 0)                /* round to nearest pixel */
  164.                         t2 -= 180*30;
  165.                      else
  166.                         t2 += 180*30;
  167.                      y = centery - t2/vfac2;    /* screen coord. */
  168.                   }
  169.                   else
  170.                   {
  171.                      t = pt[pt_zhl].lat;        /* y = lattitude */
  172.                      t = (t/120.0 + 45.0) * RAD;
  173.                      t = log(tan((FLOAT)t)) * m_vfactor;
  174.                      if (t < 0.0)               /* round to nearest pixel */
  175.                         t -= 0.5;
  176.                      else
  177.                         t += 0.5;
  178.                      y = centery - (WORD)t;     /* screen coord. */
  179.                   } /* if (type == MAP_FLAT) */
  180.  
  181.                   t2 = pt[pt_zhl].lam * width;  /* x = longitude */
  182.                   if (t2 < 0)                   /* round to nearest pixel */
  183.                      t2 -= 360*30;
  184.                   else
  185.                      t2 += 360*30;
  186.                   x = centerx + t2/(360*60);    /* screen coord. */
  187.  
  188.                   if (!new_seg)
  189.                   {
  190.                      if ((x != old_x) || (y != old_y))
  191.                      {                 /* disallow identical adjacent pts */
  192.                         if (ABS(x - old_x) < half_scr_width)
  193.                            Draw(rp, x,y);
  194.                         else
  195.                         {
  196.                            WritePixel(rp, x,y);
  197.                            Move(rp, x,y);
  198.                         }
  199.                      } /* if ((x != old_x) || (y != old_y)) */
  200.                   } /* if (!new_seg) */
  201.                   else
  202.                   {
  203.                      WritePixel(rp, x,y);       /* new segment => complete */
  204.                      Move(rp, x,y);             /* new position! */
  205.                      new_seg = FALSE;
  206.                   } /* else (!new_seg) */
  207.  
  208.                   old_x = x;                    /* remember old position */
  209.                   old_y = y;
  210.                } /* if (detail_level) */
  211.             } /* for (pt_zhl) */
  212.          } /* for (seg_zhl) */
  213.  
  214.          if (fhd)                      /* if we opened a file, close it */
  215.          {
  216.             Close(fhd);
  217.             fhd = NULL;
  218.          }
  219.       } /* if (map[].plot) */
  220.    } /* for (map_zhl) */
  221.  
  222.    TITLE_NORMAL;
  223.    MOUSE_NORMAL;
  224.    IDCMP(NORMAL_IDCMP);
  225.    RMBTRAP_OFF;
  226. }
  227.  
  228.  
  229. void draw_boxmap(UWORD type)
  230. {
  231.    WORD x, y, old_x = -1, old_y = -1, width, height;
  232.    WORD box_w, box_h, box_cx, box_cy;
  233.    UWORD anz, map_zhl, seg_zhl;
  234.    ULONG first, last, pt_zhl;
  235.    BOOL new_seg, error, in_view, prev_in_view;
  236.    BPTR fhd = NULL;
  237.    struct arc *seg;
  238.    struct point *pt;
  239.    struct IntuiMessage *msg;
  240.    BOOL abort = FALSE;
  241.  
  242.    RMBTRAP_ON;
  243.    IDCMP(IDCMP_MOUSEBUTTONS);
  244.    MOUSE_WAIT;
  245.    TITLE_MSG("Drawing map, press RMB to abort");
  246.  
  247.    get_detail_level();                          /* init. vars */
  248.    width = screen->Width;
  249.    height = screen->Height;
  250.    box_w = box_x2 - box_x1;
  251.    box_h = box_y1 - box_y2;
  252.    box_cx = (box_x1 + box_x2) / 2;
  253.    box_cy = (box_y2 + box_y1) / 2;
  254.  
  255.    old_maptype = type | MAP_BOX;
  256.    OnMenu(main_wd, FULLMENUNUM(3,1,3));         /* Box zoom */
  257.    OnMenu(main_wd, FULLMENUNUM(3,1,4));
  258.  
  259.    if (config.cls_before_draw)
  260.       SetRast(rp, config.bg_color);                   /* clear screen */
  261.  
  262.    for (map_zhl = 0; map_zhl < NUM_MAPS; map_zhl++)   /* do each map type */
  263.    {
  264.       if (config.plotmap[map_zhl] && !abort)    /* if not requested, skip */
  265.       {
  266.          error = FALSE;
  267.          SetAPen(rp, config.mapcolor[map_zhl]); /* initialize color */
  268.          seg = map[map_zhl].seg;
  269.  
  270.          if (config.loadmap[map_zhl])
  271.             pt = map[map_zhl].pt;
  272.          else
  273.          {
  274.             pt = loadmap_buffer;
  275.             if (!(fhd = get_map_fhd(map_zhl)))
  276.             {
  277.                TITLE_ERROR("Can`t open map file!");
  278.                error = TRUE;
  279.             }
  280.          } /* if (config.loadmap[]) */
  281.                                                 /* do each segment */
  282.          for (seg_zhl = 0; seg_zhl < map[map_zhl].nsegs && !error; seg_zhl++)
  283.          {
  284.             /* check for user abort */
  285.             if (msg = (struct IntuiMessage *)GetMsg(main_wd->UserPort))
  286.             {
  287.                if ((msg->Class == IDCMP_MOUSEBUTTONS) && (msg->Code == MENUDOWN))
  288.                {
  289.                   ReplyMsg((struct Message *)msg);    /* user aborted */
  290.                   DisplayBeep(NULL);
  291.                   abort = TRUE;
  292.                   break;
  293.                }
  294.                ReplyMsg((struct Message *)msg);
  295.             } /* if (GetMsg()) */
  296.  
  297.             if (config.loadmap[map_zhl])        /* map loaded? */
  298.             {
  299.                first = seg[seg_zhl].first;      /* somewhere from the */
  300.                last = seg[seg_zhl].last;        /* whole map */
  301.             }
  302.             else
  303.             {                    /* load whole next segment into buffer */
  304.                anz = seg[seg_zhl].last - seg[seg_zhl].first + 1;
  305.                if (Read(fhd, loadmap_buffer, anz*sizeof(struct point)) !=
  306.                      anz * sizeof(struct point))
  307.                {
  308.                   TITLE_ERROR("Can`t read map file!");
  309.                   error = TRUE;
  310.                   break;                        /* exit seg_zhl-loop */
  311.                }
  312.  
  313.                first = 0;              /* from beginning of load-buffer */
  314.                last = anz-1;
  315.             } /* if (config.loadmap[]) */
  316.  
  317.             new_seg = TRUE;            /* we start a new segment */
  318.             prev_in_view = FALSE;      /* obsolete, but better */
  319.  
  320.             if (seg[seg_zhl].lam_min <= box_x2 &&
  321.                 seg[seg_zhl].lam_max >= box_x1 &&
  322.                 seg[seg_zhl].lat_min <= box_y1 &&
  323.                 seg[seg_zhl].lat_max >= box_y2)
  324.             {
  325.                for (pt_zhl = first; pt_zhl <= last; pt_zhl++)
  326.                {
  327.                   if (pt[pt_zhl].code >= detail_level)
  328.                   {                             /* filter by detail level */
  329.                      x = pt[pt_zhl].lam;
  330.                      y = pt[pt_zhl].lat;
  331.  
  332.                      /* get status of current point (visable or not) */
  333.                      if ((x >= box_x1 && x <= box_x2) &&
  334.                          (y <= box_y1 && y >= box_y2))
  335.                         in_view = TRUE;
  336.                      else
  337.                         in_view = FALSE;
  338.  
  339.                      /* calculate screen coordinates */
  340.                      x = centerx + (LONG)(x - box_cx) * width / box_w;
  341.                      y = centery - (LONG)(y - box_cy) * height / box_h;
  342.  
  343.                      if (in_view)            /* curr. point in view? */
  344.                      {
  345.                         if (!new_seg)
  346.                         {
  347.                            if ((x != old_x) || (y != old_y))
  348.                            {
  349.                               if (!prev_in_view)
  350.                                  Move(rp, old_x,old_y);
  351.  
  352.                               if (ABS(x - old_x) < half_scr_width)
  353.                                  Draw(rp, x,y);
  354.                               else
  355.                               {
  356.                                  WritePixel(rp, x,y);
  357.                                  Move(rp, x,y);
  358.                               }
  359.                            } /* if ((x != old_x) || (y != old_y)) */
  360.                         } /* if (!new_seg) */
  361.                         else
  362.                         {
  363.                            WritePixel(rp, x,y); /* new segment => complete */
  364.                            Move(rp, x,y);       /* new position */
  365.                            new_seg = FALSE;
  366.                         } /* else (!new_seg) */
  367.                      } /* if (in_view) */
  368.                      else
  369.                      {
  370.                         if (!new_seg)
  371.                         {           /* need not compare x and old_x, this */
  372.                            if (prev_in_view) /* point is out of view, old */
  373.                            {              /* was in view => must differ */
  374.                               if (ABS(x - old_x) < half_scr_width)
  375.                                  Draw(rp, x,y);
  376.                               else
  377.                               {
  378.                                  WritePixel(rp, x,y);
  379.                                  Move(rp, x,y);
  380.                               }
  381.                            } /* if (prev_in_view) */
  382.                         } /* if (!new_seg) */
  383.                         else
  384.                            new_seg = FALSE;
  385.                      } /* else (in_view) */
  386.  
  387.                      old_x = x;
  388.                      old_y = y;
  389.                      prev_in_view = in_view;
  390.                   } /* if (detail_level) */
  391.                } /* for (pt_zhl) */
  392.             } /* if (segment visible) */
  393.          } /* for (seg_zhl) */
  394.  
  395.          if (fhd)                /* close file if necassary */
  396.          {
  397.             Close(fhd);
  398.             fhd = NULL;
  399.          }
  400.       } /* if (map[].plot) */
  401.    } /* for (map_zhl) */
  402.  
  403.    TITLE_NORMAL;
  404.    MOUSE_NORMAL;
  405.    IDCMP(NORMAL_IDCMP);
  406.    RMBTRAP_OFF;
  407. }
  408.  
  409.  
  410. void draw_globemap(UWORD type)
  411. {
  412. //
  413. //   get_detail_level();
  414. //
  415. //   old_maptype = type | MAP_SPHERE;
  416. //   OffMenu(main_wd, FULLMENUNUM(3,1,NOSUB));    /* Box */
  417. //   OnMenu(main_wd, FULLMENUNUM(3,2,3));         /* Sphere zoom */
  418. //   OnMenu(main_wd, FULLMENUNUM(3,2,4));
  419. //   OnMenu(main_wd, FULLMENUNUM(3,4,NOSUB));     /* Redraw */
  420. //
  421. //   RMBTRAP_ON;
  422. //   IDCMP(IDCMP_MOUSEBUTTONS);
  423. //   MOUSE_WAIT;
  424. //   TITLE_MSG("Drawing map, press RMB to abort");
  425. //
  426. //   if (config.cls_before_draw)
  427. //      SetRast(rp, config.bg_color);
  428. //
  429. //
  430. //   TITLE_NORMAL;
  431. //   MOUSE_NORMAL;
  432. //   IDCMP(NORMAL_IDCMP);
  433. //   RMBTRAP_OFF;
  434. }
  435.  
  436.