home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / New Venus / src / main.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-07  |  7.3 KB  |  231 lines  |  [TEXT/CWIE]

  1. /*
  2.  ************************************************************************
  3.  *
  4.  *                               Root Module
  5.  * Note on animation.
  6.  * We don't use null events to do animation: all animation is done in the
  7.  * drawing function itself. To activate animation, we invalidate regions
  8.  * covering both 2d and 3d views, and set is_flying to TRUE. Invalidating regions
  9.  * guarantees that visRgn of our on-screen window is set to include both views,
  10.  * and it makes sure that the next event would be an Update event. This event
  11.  * is eventually relayed to our 2d_view's and 3d_view's drawing functions.
  12.  * When is_flying is set to TRUE, these functions not only draw a window, but
  13.  * perform a few cycles of animation as well. Before they return, they invalidate
  14.  * the regions again, thus making sure that a new Update event would be generated,
  15.  * and they'll be called again.
  16.  *
  17.  ************************************************************************
  18.  */
  19.  
  20. #include "fractal_map.h"
  21. #include "mymenv.h"
  22. #include "EventHandlers.h"
  23. #include "ValueControl.h"
  24. #include "ImageViews.h"
  25. #include "filter.h"
  26.  
  27. class VenusDialog : public ModelessDialog
  28. {
  29.   enum { item_Go = 1, item_Cancel = 2, item_2dview = 3, item_3dview = 4,
  30.            item_ze=5,   item_ze_val=6,   item_ze_text=7,
  31.            item_beta=8, item_beta_val=9, item_beta_text=10,
  32.            item_Ou=11,  item_Ou_val=12,  item_Ou_text=13,
  33.            item_Ov=14,  item_Ov_val=15,  item_Ov_text=16,
  34.            item_zc=17,  item_zc_val=18,  item_zc_text=19,
  35.            item_zf=20,  item_zf_val=21,  item_zf_text=22 };
  36.   enum { dlog_id = 128 };
  37.  
  38.   ProjectionParameters projection_parms;
  39.   Boolean handle_item_hit(const int item_no);
  40.   Boolean handle_activate(Boolean onoff);    // Handle suspend/resume events
  41.  
  42.   ValueControl eyepoint_elevation;
  43.   ValueControl beta;                    // Local coordinates scaling factor
  44.   ValueControl Ou;                        // Origin of the local coordinate ref Pt
  45.   ValueControl Ov;
  46.   ValueControl z_cutoff;                // Coefficients to determine elevation from
  47.   ValueControl elevation_factor;        // the map's "elevation code"
  48.   
  49.   ImageView image_2d_view;
  50.   ThreeDView image_3d_view;
  51.   void draw_user_item(const int item_no);
  52.   
  53.   Boolean is_flying;                    // Is our plane moving?
  54.  
  55. public:
  56.   VenusDialog(const IMAGE& image);
  57. };
  58.  
  59. VenusDialog::VenusDialog(const IMAGE& image)
  60.     : ModelessDialog(dlog_id), image_2d_view(image), 
  61.       image_3d_view(image,image_2d_view.where_is_viewer(),projection_parms),
  62.       beta(4,1), is_flying(FALSE)
  63. {
  64.                                         // Late construction of items, after the dialog itself
  65.                                         // has been initialized
  66.   image_2d_view.bind(*this,item_2dview);
  67.   eyepoint_elevation.bind(ControlItem(*this,item_ze),TextItem(*this,item_ze_val));
  68.   beta.bind(ControlItem(*this,item_beta),TextItem(*this,item_beta_val));
  69.   Ou.bind(ControlItem(*this,item_Ou),TextItem(*this,item_Ou_val));
  70.   Ov.bind(ControlItem(*this,item_Ov),TextItem(*this,item_Ov_val));
  71.   z_cutoff.bind(ControlItem(*this,item_zc),TextItem(*this,item_zc_val));
  72.   elevation_factor.bind(ControlItem(*this,item_zf),TextItem(*this,item_zf_val));
  73.   
  74.   projection_parms.ze = eyepoint_elevation;
  75.   projection_parms.beta = beta;
  76.   projection_parms.Ou   = Ou;
  77.   projection_parms.Ov   = Ov;
  78.   projection_parms.z_cutoff = z_cutoff;
  79.   projection_parms.elevation_factor = elevation_factor;
  80.  
  81.   image_3d_view.bind(*this,item_3dview);
  82.   
  83.   show();
  84. }
  85.  
  86.  
  87. Boolean VenusDialog::handle_item_hit(const int item_no)
  88. {
  89.   SetNewGrafPtr dialog_grafport(our_window());
  90.   switch(item_no)
  91.   {
  92.     case item_Cancel:
  93.          return FALSE;
  94.  
  95.     case item_ze:
  96.          projection_parms.ze = eyepoint_elevation;
  97.          image_3d_view.re_project();
  98.          return TRUE;
  99.          
  100.     case item_beta:
  101.          projection_parms.beta = beta;
  102.          image_3d_view.re_project();
  103.          return TRUE;
  104.          
  105.     case item_Ou:
  106.          projection_parms.Ou = Ou;
  107.          image_3d_view.re_project();
  108.          return TRUE;
  109.          
  110.     case item_Ov:
  111.          projection_parms.Ov = Ov;
  112.          image_3d_view.re_project();
  113.          return TRUE;
  114.          
  115.     case item_zc:
  116.          projection_parms.z_cutoff = z_cutoff;
  117.          image_3d_view.re_project();
  118.          return TRUE;
  119.          
  120.     case item_zf:
  121.          projection_parms.elevation_factor = elevation_factor;
  122.          image_3d_view.re_project();
  123.          return TRUE;
  124.          
  125.     case item_Go:
  126.          if( is_flying )
  127.            is_flying = FALSE, ModelessDialog::ControlItem(*this,item_Go).set_title("\pGo");
  128.          else
  129.            is_flying = TRUE,  ModelessDialog::ControlItem(*this,item_Go).set_title("\pStop");
  130.           image_2d_view.force_redraw();
  131.           image_3d_view.force_redraw();
  132.          return TRUE;
  133.          
  134.     default:
  135.          return TRUE;
  136.   }
  137. }
  138.  
  139.                                         // A relay to draw specific user items
  140.                                         // See a note on animation above
  141. void VenusDialog::draw_user_item(const int item_no)
  142. {
  143.   const int turns_per_cycle = 30;
  144.   switch(item_no)
  145.   {
  146.     case item_2dview:
  147.          if( is_flying )
  148.          {
  149.            for(register int i=0; i<turns_per_cycle; i++)
  150.                   image_2d_view.do_one_turn(),
  151.                image_3d_view.re_project();
  152.             image_2d_view.force_redraw();        // That would make sure that the next call
  153.             image_3d_view.force_redraw();        // to WaitNextEvent would get another update
  154.           }                                        // event, and this function would be called again
  155.          else
  156.           image_2d_view.draw();
  157.          break;
  158.     case item_3dview:
  159.          if( !is_flying )                        // when flying, the window has been
  160.            image_3d_view.draw();                // already redrawn, see case above
  161.          break;
  162.     default:
  163.          _die("couldn't draw user item %d",item_no);
  164.   }
  165. }
  166.  
  167.  
  168.                                         // Handle suspend/resume events: stop flying on
  169.                                         // a suspend event (flying takes too much time,
  170.                                         // it's unreasonable to take so much when
  171.                                         // running in background)
  172. Boolean VenusDialog::handle_activate(Boolean onoff)
  173. {
  174.   if( !onoff && is_flying )
  175.     handle_item_hit(item_Go);
  176.   return TRUE; 
  177. }
  178.  
  179.                                         // Making elevation maps to fly around to
  180.                                         
  181. class MakeTestImage : public LazyImage    // this one makes a couple of pyramids
  182. {
  183.   void fill_in(IMAGE& im) const
  184.   {
  185.     const int ncols = im.q_ncols();
  186.     for(register int j=-20; j<20; j++)
  187.       for(register int i=-5; i<5; i++)
  188.         im(64+i,j+ncols/2) = 128 - 5*abs(i+j),
  189.         im(64+40+i,j+ncols/2) = 180 - 7*abs(i+j);
  190.   }
  191. public:
  192.   MakeTestImage(void) : LazyImage(256,256,8) {}
  193. };
  194.  
  195. class GaussClouds : public FractalMap
  196. {
  197.   public:
  198.     GaussClouds(const card order)
  199.       : FractalMap(order) {}
  200.                 // Well-known Gaussian random number generator
  201.                 // Note rand() returns a number [0,2^15-1],
  202.                 // that is, within [0,1] with a 15-bit implicit
  203.                 // fraction
  204.     inline int get_noise(const card scale) const {
  205.       long sum = 0;
  206.       for(register int i=0; i<12; i++)
  207.          sum += rand();            // keep the result within 
  208.       return (scale * (sum-(6<<15)))>>17; }    // [-scale/2,scale/2]
  209. };
  210.  
  211. void main(void)
  212. {
  213.   Initialize_MAC();
  214.  
  215.   GetDateTime((unsigned long *)&qd.randSeed);
  216.   Random(); Random(); Random();                     // Just randomizing
  217.   srand(qd.randSeed);
  218. #if 1    
  219.   IMAGE image = FractalMap(8);                    // Get gaussian clouds and smooth them a bit
  220. //  IMAGE image = GaussClouds(8);                    // Get gaussian clouds and smooth them a bit
  221.   FilterIt(image).conv(conv_kernel(1,1,1,CommonDenom(3))).clip_to_intensity_range();
  222. #else
  223. //  IMAGE image = FractalMap(8);  
  224.   IMAGE image = MakeTestImage();
  225. #endif
  226.   VenusDialog venus_dialog(image);
  227.   EventHandler event_handler(venus_dialog,0);
  228.   event_handler.loop();
  229. //  alert("Done");
  230. }
  231.