home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Venus 3.5 / main.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-02  |  6.1 KB  |  208 lines  |  [TEXT/ALFA]

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *                           Venus main module
  6.  *                 that controls the whole flight
  7.  *
  8.  * It is this module that runs the event loop, dispatching orders from
  9.  * the custom event queue. The module also implements a few orders (
  10.  * creating, resizing and zooming the main window), some of them depositing
  11.  * new orders into the queue. The module also installs AEHandlers to
  12.  * relay the high-level events into our queue.
  13.  *
  14.  * $Id$
  15.  *
  16.  ************************************************************************
  17.  */
  18.  
  19. #include "ElevationMap.h"
  20. #include "EventHandlers.h"
  21. #include "FlightWindow.h"
  22. #include "myAEvents.h"
  23.  
  24.  
  25.                     // The simplest window to display an image view
  26. class DisplayMapView : public ScreenWindow
  27. {
  28.   OffScreenBuffer& view_buffer;
  29.   void draw(void)            // It is this function that really draws smth
  30.   { view_buffer.draw(q_bounds()); }
  31. public:
  32.   DisplayMapView(OffScreenBuffer& pixel_buffer, const char * title)
  33.     : ScreenWindow(pixel_buffer.q_bounds(),title),
  34.       view_buffer(pixel_buffer) {}
  35.   void init(void)        { set_private_palette(view_buffer.q_clut()); show(); }            
  36. };
  37.  
  38.  
  39.  
  40.  
  41.                                         // Making elevation maps to fly through
  42.                                         
  43. class MakeTestImage : public LazyImage    // this one makes a couple of pyramids
  44. {
  45.   void fill_in(IMAGE& im) const
  46.   {
  47.     const int ncols = im.q_ncols();
  48.     for(register int j=-20; j<20; j++)
  49.       for(register int i=-5; i<5; i++)
  50.         im(64+i,j+ncols/2) = 128 - 5*abs(i+j),
  51.         im(64+40+i,j+ncols/2) = 180 - 7*abs(i+j);
  52.   }
  53. public:
  54.   MakeTestImage(void) : LazyImage(256,256,8) {}
  55. };
  56.  
  57.                                 // So does the OpenDoc event. We process only
  58.                                 // the first document of all of them dropped...
  59. class VenusOpenDoc : public AppleEventGenericHandler
  60. {
  61.  
  62.   OSErr operator() (void)    
  63. {
  64.   AEDescList desc_opened_doc;            // Decsriptor of a list of open docs
  65.   long no_opened_doc;                    // Number of documents to open
  66.   
  67.   get_direct_list(desc_opened_doc);
  68.   assert( got_all_params() );
  69.   do_well( AECountItems(&desc_opened_doc, &no_opened_doc) );
  70.   assert( no_opened_doc == 1 );
  71.   FSSpec& file_to_open = * new FSSpec;
  72.  
  73.   Size actual_size;
  74.   AEKeyword keyword;
  75.   DescType type_code;
  76.   
  77.   do_well( AEGetNthPtr(&desc_opened_doc, 1, typeFSS, &keyword, &type_code,
  78.                     (Ptr)&file_to_open, sizeof(file_to_open), &actual_size) );
  79.     //assert( actual_size < alias_record_prealloc );    // Make sure we got the whole
  80.   assert( actual_size == sizeof(file_to_open) );    // record
  81.   do_well( AEDisposeDesc(&desc_opened_doc) );
  82.   
  83.   EventHandler::put_back_my_event(EventHandler::open_picture,
  84.                                     (unsigned long)&file_to_open);
  85.   return noErr;
  86. }
  87.  public:
  88.   VenusOpenDoc(void) : AppleEventGenericHandler(kAEOpenDocuments) {}
  89. };
  90.  
  91.  
  92.  
  93. void main(void)
  94. {
  95.   Initialize_MAC();
  96.  
  97.   GetDateTime((unsigned long *)&qd.randSeed);
  98.   Random(); Random(); Random();                     // Just randomizing
  99.   srand(qd.randSeed);
  100.   
  101.                           // Install AEEventHandlers
  102.                           
  103.                                   // OpenApplication event is tacitly ignored....
  104.   class Open_appl : AppleEventOpenAppl
  105.   {
  106.     OSErr operator() (void)    { return noErr; }
  107.   };
  108.   Open_appl open_appl;
  109.  
  110.                                 // Quit event is relayed to our event queue
  111.   struct Quit_appl : public AppleEventGenericHandler
  112.   {
  113.     OSErr operator() (void)
  114.         {   EventHandler::put_back_my_event(EventHandler::quit_requested);
  115.              return noErr; }
  116.     Quit_appl(void) : AppleEventGenericHandler(kAEQuitApplication) {}
  117.   };
  118.   Quit_appl quit_appl;
  119.  
  120.   VenusOpenDoc open_doc;
  121.   
  122. #if 1    
  123.   ElevationMap elev_map(8);
  124. //  ElevationMap elev_map = GaussClouds(8);                    // Get gaussian clouds and smooth them a bit
  125. //  filter(elev_map).nondet_average(30);
  126. #else
  127.   ElevationMap elev_map = MakeTestImage();
  128. #endif
  129.   
  130. #if 0
  131.   ImageView image_2d_view(elev_map);
  132.   DisplayMapView map(image_2d_view,"Map");
  133.   map.init();
  134.   EventHandler event_handler(map,0);
  135.   event_handler.loop();
  136. #else
  137.  
  138.   const int wind_id = 128;
  139.   const int full_wind_id = 129;
  140.  
  141.   EventHandler::put_back_my_event(EventHandler::start_requested);
  142.   
  143.   for(;;)  
  144.   {
  145.     EventRecord the_event;
  146.     if( !EventHandler::wait_next_event(the_event,0) || 
  147.         the_event.what != EventHandler::mykind )
  148.       break;
  149.     switch(the_event.modifiers)
  150.     {
  151.       case EventHandler::start_requested:
  152.            {
  153.              FlightWindow map(elev_map,wind_id);
  154.              map.init();
  155.              EventHandler(map,0).loop();
  156.            }                            // make sure the map (Window) gets totally
  157.            continue;                    // destroyed before proceeding!
  158.         
  159.       case EventHandler::resize_requested:
  160.            {
  161.             Handle wind_handle = GetResource('WIND',wind_id);
  162.             assert( wind_handle != nil );
  163.             Rect new_dim_rect;
  164.             new_dim_rect.top = the_event.where.v;
  165.             new_dim_rect.left = the_event.where.h;
  166.             new_dim_rect.bottom = new_dim_rect.top + (the_event.message >> 16);
  167.             new_dim_rect.right = new_dim_rect.left + (the_event.message & 0xffff);
  168.             *((Rect *)(*wind_handle)) = new_dim_rect;
  169.             const int title_size = ((char *)(*wind_handle))[18];
  170.                                 // override auto-positioning in the WIND resource
  171.                                 // Position the
  172.                                 // window as told by new_dim_rect above!
  173.             ((char *)(*wind_handle))[19+title_size] = 0;
  174.             ((char *)(*wind_handle))[20+title_size] = 0;
  175.               EventHandler::put_back_my_event(EventHandler::start_requested);
  176.            }
  177.            continue;
  178.            
  179.       case EventHandler::zoom_requested:
  180.             {
  181.                FullFlightWindow map(elev_map,full_wind_id);
  182.                map.init();
  183.                EventHandler(map,0).loop();
  184.             }
  185.             break;
  186.             
  187.                                     // Fly through a custom elevation map
  188.                                     // The map is loaded from a file whose
  189.                                     // (heap!) FSSpec pointer is given in this
  190.                                     // event's message
  191.       case EventHandler::open_picture:
  192.             {
  193.             FSSpec& file_to_open = *((FSSpec*)the_event.message);
  194.             elev_map.load(file_to_open);
  195.             delete &file_to_open;
  196.               EventHandler::put_back_my_event(EventHandler::start_requested);
  197.             }
  198.           continue;
  199.             
  200.         default:
  201.           break;
  202.     }
  203.     break;
  204.  
  205.   }
  206. #endif
  207. }
  208.