home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 1: Collection A / 17Bit_Collection_A.iso / files / 223.dms / 223.adf / Source / star1.c < prev    next >
C/C++ Source or Header  |  1988-07-25  |  14KB  |  429 lines

  1. /*=========================================================================
  2.   Star1.c -- This is the root module of the Star Chart program.
  3.   It opens up all windows and initializes all the required tables.
  4.   This module also includes the main IDCMP loop for the program. In
  5.   that loop other modules are called to do the actual processing for
  6.   searching and identifying stars and constellations.
  7.  
  8.   Credits for Star Chart:
  9.        Robert L. Hill of the Orange County, CA. Amiga Friends User Group
  10.                       wrote the original version of StarChart in AmigaBasic
  11.                       The star data and many of the main functions of this
  12.                       version are derived from that program.
  13.  
  14.        Ray R. Larson  wrote the c version 1.0 of StarChart, 'intuitionizing'
  15.                       and enhancing the speed and functions of the original.
  16.               Version 1.2 (the present version) includes color
  17.               controls, requesters for parameter file loading
  18.               and saving, and use of the system clock in determining
  19.               the time of day and date during startup. 1.2 also uses
  20.                       a sixteen color screen instead of the 8 colors used
  21.                       in 1.0 and 1.1
  22.                       
  23.   Copyright (c) 1986 by Ray R. Larson
  24.   
  25.   This program may be freely distributed and copied, but may not be sold
  26.   without the permission of the author. If you modify or enhance it, 
  27.   please include the above credits (and please send me a copy!).
  28.  
  29. Ray R. Larson
  30. 6425 Central Ave. #304
  31. El Cerrito, CA 94530
  32.  
  33. BitNet  LARSON@UCBCMSA
  34. =========================================================================*/
  35. /*------------Header file for all of the standard stuff----*/ 
  36. /*-------------plus definitions of global structures-------*/
  37. #include "star.h" 
  38.  
  39. /* macros for checking userport and timerport bits */
  40. #define USERBIT  1L << w->UserPort->mp_SigBit
  41. #define TIMERBIT 1L << timerport->mp_SigBit
  42. /* macros for timer referencing */
  43. #define SECONDS tr_time.tv_secs
  44. #define MICROSECONDS tr_time.tv_micro
  45.  
  46.  
  47. /*------------ External function declarations ------------*/
  48.  
  49. extern void *OpenLibrary();
  50. extern struct Screen *OpenScreen();
  51. extern struct Window *OpenWindow();
  52. extern struct IntuiMessage *GetMsg();
  53. extern struct MsgPort *CreatePort();
  54. extern struct IORequest *CreateExtIO();
  55.  
  56. /*------------------ extern vars ------------------*/
  57.  
  58. extern struct star_rec StarTable[];
  59. extern char *Greek[]; 
  60. extern struct cons_rec Constel[];
  61. extern struct Menu Titles[];
  62. extern SHORT ErrorMessCount;
  63.  
  64. /*------------------- global variables -------------------*/
  65.  
  66. struct GfxBase *GfxBase;
  67. struct IntuitionBase *IntuitionBase;
  68. LONG IconBase;
  69. LONG MathBase;
  70. LONG MathTransBase;
  71. struct RastPort *rp;
  72. struct Gadget *SelectedGadget;
  73. struct Window *w;
  74. struct Screen *scr;      /* pointer to the custom screen    - RRL */
  75. struct MsgPort *timerport; /* for timing signals */
  76. struct timerequest timermsg;
  77. UBYTE DefaultStartFile[140] = "DEFAULT.STAR";
  78. long mask;
  79.  
  80. /*------------ Main Screen and Window Definitions ------------*/
  81.  
  82. /* The main screen  */
  83. struct NewScreen newscr =
  84.    { 0,             /* LeftEdge at zero           */
  85.      0,             /* TopEdge      "             */
  86.      640,           /* Width - set later          */
  87.      200,           /* Height - non interlace     */
  88.      4,             /* depth - set later          */
  89.      0, 1,          /* DetailPen and BlockPen     */
  90.      HIRES,         /* special display mode       */
  91.      CUSTOMSCREEN,  /* The screen type            */
  92.      NULL,          /* use the default font       */
  93.      (UBYTE *)"Star Chart: Version 1.2 ", /* Screen title       */
  94.      NULL,          /* No special gadgets         */
  95.      NULL,          /* Not a custom bitmap        */
  96.    };
  97.  
  98. UBYTE WinTitle[] = "Star Chart : Version 1.2";  /* Window title */
  99.  
  100. /* new window structure */
  101. struct NewWindow nw = {
  102.    0, 0, 640, 200, 0, 1,
  103.  
  104. /* IDCMP Flags */
  105.  
  106.    MENUPICK
  107. |  MOUSEMOVE
  108. |  MOUSEBUTTONS
  109. |  CLOSEWINDOW
  110. |  GADGETDOWN
  111. |  GADGETUP,
  112.  
  113. /* Flags */
  114.   WINDOWCLOSE
  115. | WINDOWDEPTH
  116. | ACTIVATE
  117. | REPORTMOUSE
  118. | RMBTRAP,
  119.  
  120.   NULL,               /* First gadget */
  121.   NULL,               /* Checkmark */
  122.   &WinTitle[0],  /* Window title */
  123.   NULL,               /* No custom streen */
  124.   NULL,               /* Not a super bitmap window */
  125.   0, 0, 640, 200,     /* Not used, but set up anyway */
  126.   CUSTOMSCREEN
  127. };
  128.  
  129.  
  130. /*------------------- color table definitions ------------------*/
  131.  
  132. UWORD defaultCT[32], CTable[32], *ctabptr;
  133. struct ColorMap *cmap;
  134.  
  135. extern struct ViewPort *ViewPortAddress();
  136. struct ViewPort *vport;
  137. extern struct ColorMap *GetColorMap();
  138. extern struct Preferences *GetPrefs();
  139.  
  140. extern struct WBStartup *WBenchMsg;
  141.  
  142.  
  143. /*=========================================================================
  144.  Main routine of the StarChart program.
  145. ========================================================================*/
  146. main(argc,argv)
  147.   int argc;
  148.   char **argv;
  149. {
  150.     /*---------------- local vars ---------------*/
  151.     struct IntuiMessage *mes;          /* Message pointer */
  152.     ULONG class;                       /* Message class */
  153.     USHORT code;                       /* Message code */
  154.     SHORT mx, my;                      /* Mouse X and Y */
  155.     APTR Iadr, adr;                    /* IAddress from Intuimessage */
  156.     ULONG flgs;                        /* temp IDCMP flags */
  157.     SHORT i;
  158.     char *clarg;
  159.     SHORT N_Bitplanes;
  160.     BOOL menus_on, timeout, othermsg, fromWB, UpDateTime();
  161.     struct FileLock *LastDir, *CurrentDir();
  162.     
  163.     /*--------------- Check for WB or command line args -------------*/
  164.     
  165.     fromWB = (argc == 0);
  166.     if (fromWB)
  167.       { if(WBenchMsg->sm_NumArgs > 1)
  168.           strcpy(DefaultStartFile,WBenchMsg->sm_ArgList[1].wa_Name);
  169.       }
  170.     else /* started from CLI */
  171.       { if (argc >= 2)
  172.            strcpy(DefaultStartFile, argv[1]);
  173.       }
  174.  
  175.  
  176.     /*----------- Open up graphics library and Intuition ------------*/
  177.     if ((GfxBase = (struct GfxBase *)
  178.        OpenLibrary("graphics.library", NULL)) == NULL) {
  179.          printf("no graphics library on disk\n");
  180.          CloseThings();
  181.          exit(100);
  182.     }
  183.     mask |= GRAPHICS;
  184.  
  185.     if ((IntuitionBase = (struct IntuitionBase *)
  186.       OpenLibrary("intuition.library", NULL)) == NULL) {
  187.         printf("No intuition library\n");
  188.         CloseThings();
  189.         exit(100);
  190.     }
  191.     mask |= INTUITION;
  192.  
  193.     if ((IconBase = (LONG)OpenLibrary(ICONNAME,NULL))==NULL) {
  194.        printf("No Icon library!");
  195.        CloseThings();
  196.        exit(100);
  197.        }
  198.     mask |= ICONLIB;
  199.  
  200.     if ((MathBase = (LONG)OpenLibrary("mathffp.library",NULL))==NULL) {
  201.        printf("No mathffp library!");
  202.        CloseThings();
  203.        exit(100);
  204.        }
  205.     mask |= MATHLIB;
  206.    
  207.     if ((MathTransBase = (LONG)OpenLibrary("mathtrans.library",NULL))==NULL) {
  208.        printf("No mathtrans library!");
  209.        CloseThings();
  210.        exit(100);
  211.        }
  212.     mask |= MATHTRANS;
  213.  
  214.     if ((timerport = CreatePort(0L,0L)) == NULL) {
  215.        printf("Can't create the timer port\n");
  216.        CloseThings();
  217.        exit(200);
  218.       }
  219.     mask |= TIMEPORT;
  220.  
  221.     /* open up the custom screen */
  222.     if((scr = (struct Screen *)OpenScreen(&newscr)) == NULL)
  223.          { printf("Can't open the custom screen!!\n");
  224.            CloseThings();
  225.            exit (3);
  226.           }
  227.      mask |= SCREEN;
  228.        
  229.     /* Set window pointer to the custom screen */
  230.  
  231.     nw.Screen = scr ;
  232.  
  233.  
  234.     /*----------- open window ----------*/
  235.     if ((w = OpenWindow(&nw)) == NULL) {
  236.         printf("Cannot open window\n");
  237.         CloseThings();
  238.     }
  239.     mask |= WINDOW;
  240.  
  241.     /* ------------- Initialize some Globals, Menus, etc. -------------*/
  242.     /* set the raster port */
  243.     rp = w->RPort;
  244.  
  245.     /* set the viewport address  */
  246.     vport = ViewPortAddress(w);
  247.  
  248.     /* copy the default colortable */
  249.     cmap = vport->ColorMap;
  250.     ctabptr = (UWORD *) cmap->ColorTable;
  251.     for (i=0; i < 32; i++)
  252.         defaultCT[i] = CTable[i] = ctabptr[i];
  253.     CTable[0] = 0x05A;     /* register 0 is dark blue                   */
  254.     CTable[1] = 0xFFF;     /* register 1 is white                       */
  255.     CTable[2] = 0x002;     /* register 2 is Black (almost)              */
  256.     CTable[3] = 0xFFF;     /* register 4 is white                       */
  257.     CTable[4] = 0xEEE;     /* register 5 is darker                      */
  258.     CTable[5] = 0xDDD;     /* register 6 is darker                      */
  259.     CTable[6] = 0xCCC;     /* register 7 is darrker                     */
  260.     CTable[7] = 0xBBB;     /* register 8 is darker                      */
  261.     CTable[8] = 0xAAA;     /* register 9 is darker                      */
  262.     CTable[9] = 0x999;     /* register 10 is darker                     */
  263.     CTable[10] = 0x888;    /* register 11 is medium gray                */
  264.     CTable[11] = 0xF11;     /* reqister 3 is bright red -                */
  265.     CTable[12] = 0x00B;    /* register 12 is dark blue                */
  266.     CTable[13] = 0xF0F;    /* register 13 is magenta                    */
  267.     CTable[14] = 0xFFF;    /* register 14 is also white                 */
  268.     CTable[15] = 0xF80;    /* register 15 is Orange                     */
  269.  
  270.  
  271.     /* change the registers to the new  colors */
  272.     LoadRGB4(vport,&CTable,32L);
  273.     
  274.     mask |= COLORMAP;
  275.  
  276.     SetMenuStrip(w,Titles);              /* Init the Menus */
  277.     mask |= MENU;
  278.     menus_on = FALSE;  /* flag for interpreting mousemoves */
  279.  
  280.     /* initialize the timer port */
  281.     init_timer();
  282.  
  283.     /* set up some other globals(in starglb.c) and display the default stars */
  284.     initialize();
  285.     
  286.     /* build the constellations menu */
  287.     InitConMenu();
  288.  
  289.     /* change to the star pointer sprite */
  290.     do_StarSprite(w);
  291.  
  292.     /* set the timer to go off in 1 second */
  293.     set_timer(&timermsg,1L,0L);
  294.     timeout = FALSE;
  295.  
  296.     /* --------------- main loop -----------------*/
  297.     for (;;)
  298.     {  
  299.         othermsg = FALSE;
  300.        
  301.         if (timeout == TRUE)
  302.            { 
  303.              timeout = FALSE;
  304.              /* increment the date/time display */
  305.              /* and redisplay stars each 'hour' */
  306.          if(UpdateTime()) DisplayStars();
  307.              set_timer(&timermsg,1L,0L);
  308.              if (ErrorMessCount > 0) /* time the error message */
  309.                 { ErrorMessCount--;
  310.                   if (ErrorMessCount == 0)
  311.                      SetWindowTitles(w,WinTitle,(UBYTE *)-1);
  312.                 }
  313.             }
  314.  
  315.         if ((mes = GetMsg(w->UserPort)) == NULL) 
  316.           {
  317.             Wait((USERBIT) | (TIMERBIT));
  318.             if (GetMsg(timerport)) timeout = TRUE;
  319.             continue;
  320.           }
  321.  
  322.        if (mes == NULL) continue;
  323.         class = mes->Class;
  324.         code = mes->Code;
  325.         mx = mes->MouseX;
  326.         my = mes->MouseY;
  327.         Iadr = mes->IAddress;
  328.         ReplyMsg(mes);
  329.           
  330.   
  331.         switch (class) {
  332.  
  333.                case CLOSEWINDOW:   CloseThings();   exit(NULL);  break;
  334.  
  335.                case GADGETUP:   /* do nothing for now  - except note that*/
  336.                                 /* the gadget was selected.              */
  337.                                 SelectedGadget = (struct Gadget *)Iadr;
  338.                      break;
  339.  
  340.                case MENUPICK:  /* Do menu processing (in star2.c) */
  341.                            if (code != MENUNULL)
  342.                              Do_menu((LONG)MENUNUM(code), 
  343.                      (LONG)ITEMNUM(code),
  344.                      (LONG)SUBNUM(code));
  345.                            break;
  346.  
  347.                case GADGETDOWN: /* do nothing for now  - except note that*/
  348.                                 /* the gadget was selected.              */
  349.                                 SelectedGadget = (struct Gadget *)Iadr;
  350.                                break;
  351.  
  352.                case MOUSEBUTTONS: /* Handle mouse clicks, i.e. identify star or */
  353.                             /* constellations based on the position of the*/
  354.                          /* mouse pointer.    (In Star5.c)                */
  355.                          Do_buttons(code,mx,my); 
  356.                          break;
  357.  
  358.                case MOUSEMOVE:  /* Handle mouse moves to see if the pointer is */
  359.                         /* in the menu area - if so,  permit selection */
  360.                        if (my < 10) 
  361.                    { if (menus_on == FALSE)
  362.                     {menus_on = TRUE;
  363.                      Forbid();
  364.                      w->Flags &= ~RMBTRAP;
  365.                      Permit();
  366.                      ClearPointer(w);
  367.                     }
  368.                    }
  369.                  else 
  370.                    if (menus_on)
  371.                      { menus_on = FALSE;
  372.                        Forbid();
  373.                                    w->Flags |= RMBTRAP;
  374.                                    Permit();
  375.                    do_StarSprite(w);
  376.                                  }
  377.                         break;
  378.  
  379.  
  380.                } /* end of class switch */
  381.     } /* end of for(ever) loop */
  382. }/* end of main */
  383.  
  384. /*========================================================================
  385.   init_timer - opens up the timer device for use.
  386. ========================================================================*/
  387. init_timer()
  388. { return(OpenDevice(TIMERNAME,UNIT_VBLANK,&timermsg,0L));
  389. }
  390.  
  391. /*========================================================================
  392.   set_timer - Sets the timer device to respond in the given number of 
  393.   seconds and/or microseconds.
  394. ========================================================================*/
  395. set_timer(tm,sec,micro)
  396. struct timerequest *tm;
  397. ULONG sec,micro;
  398. {
  399.   tm->tr_node.io_Command = TR_ADDREQUEST;
  400.   tm->SECONDS = sec;
  401.   tm->MICROSECONDS = micro;
  402.   tm->tr_node.io_Flags = 0;
  403.   tm->tr_node.io_Error = 0;
  404.   tm->tr_node.io_Message.mn_ReplyPort = timerport;
  405.   SendIO(tm);
  406.   return (0);
  407. }
  408.  
  409. /*========================================================================
  410.   CloseThings - Function that closes everything properly if any failures
  411.    occur before main loop entry,
  412. ======================================================================*/
  413. CloseThings()
  414. {
  415.     /* change the color registers back to default colors */
  416.     if (mask & COLORMAP)  LoadRGB4(vport,&defaultCT,32L);
  417.     if (mask & MENU)      ClearMenuStrip(w);
  418.     if (mask & WINDOW)    CloseWindow(w);
  419.     if (mask & SCREEN)    CloseScreen(scr);
  420.     if (mask & ICONLIB)   CloseLibrary(IconBase);
  421.     if (mask & MATHLIB)   CloseLibrary(MathBase);
  422.     if (mask & MATHTRANS) CloseLibrary(MathTransBase);
  423.     if (mask & TIMEPORT)  DeletePort(timerport);
  424.     if (mask & INTUITION) CloseLibrary(IntuitionBase);
  425.     if (mask & GRAPHICS)  CloseLibrary(GfxBase);
  426.  
  427.     
  428. }
  429.