home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / misc / aspringies-1.0.lha / ASpringies / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-24  |  9.2 KB  |  297 lines

  1. /* main.c -- Amiga specific setup and cleanup functions etc.
  2.  * Copyright (C) 1994  Torsten Klein
  3.  *
  4.  * This file is part of ASpringies, a mass and spring simulation system for the Amiga
  5.  *
  6.  * ASpringies is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 1, or (at your option)
  9.  * any later version.
  10.  *
  11.  * ASpringies is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with ASpringies; see the file COPYING.  If not, write to
  18.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * $Id: main.c,v 2.9 1994/08/19 11:49:05 Torsten_Klein Exp $  
  21.  */
  22.  
  23. #include <exec/memory.h>
  24.  
  25. #include "main.h"
  26. #include "defs.h"
  27. #include "obj.h"
  28.  
  29. struct Library                  *TimerBase, *MUIMasterBase;
  30. struct Window            *anim_wnd;
  31. struct Screen                   *anim_scr;
  32. struct RastPort                 *anim_rport[ANIM_BUFS];
  33.  
  34. LONG __oslibversion = 37, __stack = 8192;
  35.  
  36. static int    display_open;
  37. static struct RasInfo             *anim_rinfo;
  38. static struct BitMap              *anim_bmap[ANIM_BUFS];
  39. static struct Layer_Info          *anim_linfo[ANIM_BUFS];
  40. static struct Layer               *anim_layer[ANIM_BUFS];
  41. static struct IORequest            iORequest;
  42. static struct FileRequester       *AslFileReq;
  43. static struct ScreenModeRequester *AslScrReq;
  44.  
  45. /* MUI stuff
  46.  */
  47. APTR 
  48.   AP_ASpringies,
  49.   WI_ASpringies,
  50.  
  51.   RA_Mode,
  52.  
  53.   CY_Force,
  54.   PG_Force,
  55.  
  56.   CK_DrawSpr,    CK_Action,       CK_FixedMass,    CK_AdaptTStep,
  57.   CK_GravActive, CK_CenterActive, CK_PtAttrActive, CK_WallFActive,
  58.   CK_WallTop,    CK_WallLeft,     CK_WallRight,    CK_WallBottom,
  59.  
  60.   BT_SelAll,     BT_SetRLen,      BT_SetCenter,
  61.  
  62.   SL_Grid,
  63.  
  64.   ST_Mass,       ST_Elas,         ST_Kspr,         ST_Kdmp,
  65.   ST_Visc,       ST_Stic,         ST_Step,         ST_Prec, 
  66.   ST_GravGrav,   ST_GravDir,      ST_CenterMagn,   ST_CenterDamp, 
  67.   ST_PtAttrMagn, ST_PtAttrExpo,   ST_WallFMagn,    ST_WallFExpo,
  68.  
  69.   TX_Fps;
  70.      
  71. /* ******************************************************************** */
  72.      
  73. int  CXBRK(void) { return(0); } 
  74.  
  75. void chkabort(void)  {}
  76.  
  77. main(int argc, char **argv)
  78. {
  79.   FAILOPEN(MUIMASTER_NAME,
  80.        MUIMasterBase = OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN));
  81.   SetUpMui();
  82.  
  83.   FAILOPEN("timer.device (UNIT_MICROHZ)",
  84.        !OpenDevice("timer.device", UNIT_MICROHZ, &iORequest, 0));
  85.   TimerBase = (struct Library *)iORequest.io_Device;
  86.  
  87.   FAILOPEN("Asl file requester", AslFileReq = MUI_AllocAslRequestTags
  88.        (ASL_FileRequest,
  89.         ASLFR_InitialDrawer,             "PROGDIR:lib/",
  90.         ASLFR_InitialPattern,            "#?.xsp",
  91.         ASLFR_DoPatterns,                TRUE,
  92.         TAG_DONE));
  93.  
  94.   if (!(AslScrReq = MUI_AllocAslRequestTags
  95.        (
  96.         ASL_ScreenModeRequest,
  97.         ASLSM_InitialDisplayID,     DEFAULT_MONITOR_ID | HIRESLACE_KEY,
  98.         ASLSM_InitialOverscanType,  OSCAN_STANDARD,
  99.         ASLSM_InitialDisplayWidth,  640,
  100.         ASLSM_InitialDisplayHeight, 548,
  101.         ASLSM_TitleText,            "Animation screen mode",
  102.         ASLSM_DoWidth,              TRUE,
  103.         ASLSM_DoHeight,             TRUE,
  104.         ASLSM_DoOverscanType,       TRUE,
  105.         ASLSM_DoAutoScroll,         TRUE,
  106.         TAG_DONE
  107.         )))
  108.     DoMethod(AP_ASpringies, MUIM_Application_SetMenuState, ID_ME_ScreenMode, FALSE);
  109.  
  110.   OpenAnimDisplay(FALSE);
  111.   DoMethod(WI_ASpringies, MUIM_Window_ScreenToFront);
  112.   DoMethod(WI_ASpringies, MUIM_Window_ToFront); 
  113.   set(WI_ASpringies, MUIA_Window_Activate, TRUE);
  114.  
  115.   new_main();
  116.  
  117.   main_fail(NULL, NULL, 0);
  118. }
  119.  
  120.   /* ******************************************************************** */
  121.  
  122. void OpenAnimDisplay(boolean request)
  123. {
  124.   int i;
  125.  
  126.   if (request)
  127.     MUI_AslRequestTags(AslScrReq,TAG_DONE);
  128.  
  129.   FAILOPEN("animation screen", anim_scr = OpenScreenTags
  130.        (NULL,
  131.         SA_DisplayID,     AslScrReq->sm_DisplayID,
  132.         SA_Width,         AslScrReq->sm_DisplayWidth,
  133.         SA_Height,        AslScrReq->sm_DisplayHeight,
  134.         SA_Overscan,      AslScrReq->sm_OverscanType,
  135.         SA_AutoScroll,    AslScrReq->sm_AutoScroll,
  136.         SA_Depth,         1,
  137.         SA_Type,          CUSTOMSCREEN | SCREENQUIET | SCREENBEHIND,
  138.         SA_Title,         "ASpringies animation screen",
  139.         TAG_DONE
  140.         ));
  141.   
  142.   FAILOPEN("animation window", anim_wnd = OpenWindowTags
  143.        (NULL,
  144.         WA_Width,         anim_scr->Width,
  145.         WA_Height,        anim_scr->Height,
  146.         WA_IDCMP,         IDCMP_MOUSEBUTTONS | IDCMP_VANILLAKEY
  147.                           | IDCMP_MOUSEMOVE  | IDCMP_RAWKEY,
  148.         WA_Flags,         WFLG_BORDERLESS    | WFLG_RMBTRAP | WFLG_NOCAREREFRESH,
  149.         WA_CustomScreen,  anim_scr,
  150.         WA_AutoAdjust,    TRUE,
  151.         WA_MouseQueue,    1,
  152.         TAG_DONE
  153.         ));
  154.  
  155.   anim_rport[0] = anim_wnd->RPort;
  156.   anim_bmap[0]  = anim_rport[0]->BitMap;
  157.   anim_rinfo    = anim_scr->ViewPort.RasInfo;
  158.  
  159.   for (i=1; i<ANIM_BUFS; i++) {
  160.     FAILOPEN("BitMap structure for backbuffer", 
  161.          anim_bmap[i] = AllocMem(sizeof(struct BitMap), MEMF_CLEAR));
  162.  
  163.     InitBitMap(anim_bmap[i],1,(LONG)anim_scr->Width, (LONG)anim_scr->Height);
  164.  
  165.     FAILOPEN("raster for backbuffer BitMap", 
  166.          anim_bmap[i]->Planes[0] = AllocRaster((ULONG)anim_scr->Width,
  167.                            (ULONG)anim_scr->Height));
  168.  
  169.     FAILOPEN("LayerInfo for backbuffer", anim_linfo[i] = NewLayerInfo());
  170.  
  171.     FAILOPEN("BehindLayer for backbuffer",
  172.          anim_layer[i] = CreateBehindLayer(anim_linfo[i], anim_bmap[i], 0, 0,
  173.                            anim_scr->Width-1, anim_scr->Height-1,
  174.                            LAYERSIMPLE | LAYERBACKDROP, NULL));
  175.     anim_rport[i] = anim_layer[i]->rp;
  176.   }
  177.   display_open=TRUE;
  178. }
  179.  
  180. /* ******************************************************************** */
  181.  
  182. void CloseAnimDisplay(void)
  183. {
  184.   int i;
  185.   
  186.   if (display_open) ShowView(0);
  187.   for (i=1; i<ANIM_BUFS; i++) {
  188.     if (anim_layer[i])           DeleteLayer(0, anim_layer[i]);
  189.     WaitBlit();
  190.     if (anim_linfo[i])           DisposeLayerInfo(anim_linfo[i]);
  191.     if (anim_bmap[i] && anim_bmap[i]->Planes[0]) {
  192.       FreeRaster(anim_bmap[i]->Planes[0], (ULONG)anim_scr->Width, 
  193.          (ULONG)anim_scr->Height);
  194.       FreeMem(anim_bmap[i], sizeof(struct BitMap));
  195.     }
  196.   }
  197.   if (anim_wnd)    CloseWindow(anim_wnd);
  198.   if (anim_scr)    CloseScreen(anim_scr);
  199. }
  200.  
  201.  
  202. /* ******************************************************************** */
  203.  
  204. void ShowView(int n)
  205. {
  206.   anim_scr->RastPort.BitMap = anim_rinfo->BitMap = anim_bmap[n];
  207.   MakeScreen(anim_scr);
  208.   RethinkDisplay();
  209. }      
  210.  
  211. /* ******************************************************************** */
  212.  
  213. void main_fail(char *what, char *where, int whatline)
  214. {
  215.   CloseAnimDisplay();
  216.   if (AslScrReq)     MUI_FreeAslRequest(AslScrReq);
  217.   if (AslFileReq)    MUI_FreeAslRequest(AslFileReq);
  218.   CloseDevice(&iORequest);
  219.   if (AP_ASpringies) MUI_DisposeObject(AP_ASpringies);  
  220.   CloseLibrary(MUIMasterBase);
  221.  
  222.   if (what) {
  223.     fprintf(stderr, "*** ASPRINGIES FAILED to allocate %s in file %s, line %d\n",
  224.         what, where, whatline);
  225.     exit(20);
  226.   }
  227.   exit(0);
  228. }
  229.  
  230. /* ******************************************************************** */
  231.  
  232. void DoInfo(void)
  233. {
  234.   static char
  235.     *InfoText = "\
  236. \033u\033c\033bASpringies\033n\033u - an interactive mass and spring simulator\
  237. \n\
  238. \nCopyright (c) 1994 by Torsten Klein (torstenk@techfak.uni-bielefeld.de)\
  239. \n\033n\033l\
  240. \nbased upon 'xspringies' by Douglas DeCarlo (dd2i+@andrew.cmu.edu) et al.\
  241. \n\
  242. \n\033bASpringies comes with ABSOLUTELY NO WARRANTY; for details see\
  243. \nnode 'Disclaimer' in the online help system.  This is free software, and you\
  244. \nare welcome to redistribute it under certain conditions; please read the\
  245. \nfile 'COPYING' for details.\033n\
  246. \n\
  247. \nASpringies features a context sensitive hypertext help system.\
  248. \nPlease press the <Help> key now to see its table of contents.\
  249. \n\
  250. \n\033iPressing the <Help> key while the pointer is above a group of gadgets or a\
  251. \nmenu will pop up the corresponding page of the manual.";
  252.  
  253.  MUI_Request(AP_ASpringies, WI_ASpringies, 0, "Program information", "*Ok", InfoText);
  254. }
  255.  
  256. /* ******************************************************************** */
  257.  
  258. void FileIO(int command)
  259. {
  260.   static char filename[MAXPATHLEN];
  261.  
  262.   struct Window        *window;
  263.   char                 title[30]="Select a file to ";
  264.  
  265.   switch(command)
  266.     {
  267.     case F_LOAD   : strcat(title, "load...");   break;
  268.     case F_INSERT : strcat(title, "insert..."); break;
  269.     case F_SAVEAS : 
  270.     case F_SAVE   : strcat(title, "save...");    break;
  271.     }
  272.  
  273.   get(WI_ASpringies, MUIA_Window_Window, &window);
  274.   
  275.   if ((command != F_SAVE) || !strlen(filename)) {
  276.     if (MUI_AslRequestTags
  277.     (
  278.      AslFileReq, 
  279.      ASLFR_Window,          window,
  280.      ASLFR_TitleText,       title,
  281.      ASLFR_DoSaveMode,      (command==F_SAVE),
  282.      TAG_DONE)) {
  283.       strncpy(filename, AslFileReq->fr_Drawer,  sizeof(filename)-1);
  284.       AddPart(filename, AslFileReq->fr_File,    sizeof(filename)-1);
  285.       if (strlen(filename) && !strstr(filename, ".xsp")) strcat(filename, ".xsp");
  286.     } else return;
  287.   }
  288.   if (!file_command(filename, command)) {
  289.     MUI_Request(AP_ASpringies, WI_ASpringies, 0,
  290.         "ASpringies - Error", "*Ok", "Unable to perform file operation!");
  291.     *filename = NULL;
  292.   }
  293. }
  294.  
  295. /* ******************************************************************** */
  296.  
  297.