home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1995 July / IMM0795.ISO / share / os2 / pmfract / src / pmfrinit.c < prev    next >
C/C++ Source or Header  |  1994-01-24  |  12KB  |  335 lines

  1. /*--------------------------------------------------
  2.    PMFRINIT.C - FRACTINT for PM
  3.           Initialization Routines
  4.  
  5.    04/26/91     Code by Donald P. Egen (with help)
  6.  
  7.    These subroutines handle initialization for the
  8.    process as a whole, or the initialization of the
  9.    window proc.
  10.    Out of line so that they can be in a different
  11.    segment, and therefore discarded after start-up.
  12.  ---------------------------------------------------*/
  13.  
  14. #define INCL_WIN
  15. #define INCL_GPI
  16. #define INCL_DOS
  17. #include <os2.h>
  18. #include <process.h>
  19. #include <stdio.h>
  20. #include <smplhelp.h>
  21. #include <malloc.h>
  22. #include <string.h>
  23.  
  24. #include "pmfract.h"
  25. #include "fractint.h"
  26. #include "fractype.h"
  27.  
  28. int level = 0;
  29.  
  30.   /* Internal Quick-Sort Procedure.
  31.      No, I can't use the one in the library because I need
  32.      to sort a _FAR array in a medium model program.       */
  33.  
  34. int compare_fractalnames( int _far *element1, int _far *element2)
  35. {
  36. int i, j, k;
  37. for (i = 0; i < 100; i++) {
  38.     j = *element1;
  39.     k = *element2;
  40.     if (fractalspecific[j].name[i] < fractalspecific[k].name[i])
  41.         return(-1);
  42.     if (fractalspecific[j].name[i] > fractalspecific[k].name[i])
  43.         return(1);
  44.     if (fractalspecific[j].name[i] == 0)
  45.         return(0);
  46.     }
  47. return(0);
  48. }
  49.  
  50. void Swap(int _far *i, int _far *j )
  51.  
  52.   {
  53.       /* Swap the i-th and j-th elements */
  54.  
  55.    int x;
  56.  
  57.     /*    writeln('swapping ',i,' and ',j);  */
  58.        x = *i;
  59.        *i = *j;
  60.        *j = x;
  61.   }
  62.  
  63. int _far *Rearr(int _far *lb, int _far *ub)
  64.   {
  65.  
  66.     /* Portion of Quick Sort - Find the Pivot value */
  67.  
  68.      /* writeln('level=',level,' rearr ub=',ub,' lb=',lb); */
  69.        do {
  70.          while ((ub > lb) && (compare_fractalnames(ub, lb) >= 0))  ub = ub - 1;
  71.          if (ub != lb)
  72.             {
  73.              Swap(ub, lb);
  74.              while ((lb < ub) && (compare_fractalnames(lb, ub) <= 0))
  75.                 lb = lb + 1;
  76.              if (lb != ub)    Swap(lb, ub);
  77.             }
  78.        }
  79.        while (! (lb == ub));
  80.  
  81.        return(lb);
  82.   }
  83.  
  84. void fr_Qsort(int _far *lb, int _far *ub)
  85.   {
  86.       /* The actual Quick Sort procedure */
  87.  
  88.   int _far *j;
  89.  
  90.     /*    writeln('level=',level,' lb=',lb,' ub=',ub); */
  91.        level = level+1;
  92.        if (lb < ub)
  93.       {
  94.            j = Rearr(lb, ub);
  95.            if (j != &asFracTypeList[0])  fr_Qsort(lb, j-1);
  96.            fr_Qsort(j+1, ub);
  97.       }
  98.       level = level-1;
  99.   }
  100.  
  101. VOID    PMfrGlobalInit(VOID)
  102.    {
  103.      SHORT k;
  104.  
  105.      /* get some significant screen dimensions */
  106.      cxScreen = SYSVAL(SV_CXSCREEN);
  107.      cyScreen = SYSVAL(SV_CYSCREEN);
  108.      cyIcon = SYSVAL(SV_CYICON);
  109.      cxFullScreen = SYSVAL(SV_CXFULLSCREEN);
  110.      cyFullScreen = SYSVAL(SV_CYFULLSCREEN);
  111.      cyMenu = SYSVAL(SV_CYMENU);
  112.      cxMouseTol = SYSVAL(SV_CXDBLCLK);
  113.      cyMouseTol = SYSVAL(SV_CYDBLCLK);
  114.  
  115.      /* Load string values from resource file */
  116.      WinLoadString (hab, (HMODULE) 0, IDS_ZOOM_IN,  sizeof szZoomIn,   szZoomIn );
  117.      WinLoadString (hab, (HMODULE) 0, IDS_ZOOM_OUT, sizeof szZoomOut,  szZoomOut);
  118.      WinLoadString (hab, (HMODULE) 0, IDS_FREEZE,   sizeof szFreeze,   szFreeze );
  119.      WinLoadString (hab, (HMODULE) 0, IDS_HALT,     sizeof szHalt,     szHalt   );
  120.      WinLoadString (hab, (HMODULE) 0, IDS_TITLEBAR, sizeof szTitleBar, szTitleBar);
  121.      WinLoadString (hab, (HMODULE) 0, IDS_SWAP_MANDEL, sizeof szSwapMandel, szSwapMandel);
  122.      WinLoadString (hab, (HMODULE) 0, IDS_SWAP_JULIA,  sizeof szSwapJulia,  szSwapJulia );
  123.      WinLoadString (hab, (HMODULE) 0, IDS_MISSING_SUB, sizeof szMissingSub, szMissingSub);
  124.      WinLoadString (hab, (HMODULE) 0, IDS_FLOAT_FORMAT, sizeof szFloatFormat, szFloatFormat);
  125.      WinLoadString (hab, (HMODULE) 0, IDS_VAL_NOTAPP, sizeof szValueNotApplicable,
  126.                               szValueNotApplicable);
  127.      WinLoadString (hab, (HMODULE) 0, IDS_CLIENT_CLASS, sizeof szClientClass,
  128.                               szClientClass);
  129.      WinLoadString (hab, (HMODULE) 0, IDS_BAD_NUMERIC, sizeof szBadNumeric,
  130.                               szBadNumeric);
  131.      WinLoadString (hab, (HMODULE) 0, IDS_SUBTASK_FATAL, sizeof szSubtaskFatal,
  132.                               szSubtaskFatal);
  133.      WinLoadString (hab, (HMODULE) 0, IDS_INIT_FATAL, sizeof szInitFatal,
  134.                               szInitFatal);
  135.      WinLoadString (hab, (HMODULE) 0, IDS_WORKING, sizeof szWorking,
  136.                               szWorking);
  137.      WinLoadString (hab, (HMODULE) 0, IDS_PRINT_NOOPEN, sizeof szPrintNoOpen,
  138.                               szPrintNoOpen);
  139.      WinLoadString (hab, (HMODULE) 0, IDS_PRINT_ACTION, sizeof szPrintAction,
  140.                               szPrintAction);
  141.      WinLoadString (hab, (HMODULE) 0, IDS_PRINT_HELP, sizeof szPrintHelp,
  142.                               szPrintHelp);
  143.      WinLoadString (hab, (HMODULE) 0, IDS_OPEN_TITLE, sizeof szOpenTitle,
  144.                               szOpenTitle);
  145.      WinLoadString (hab, (HMODULE) 0, IDS_OPEN_HELP, sizeof szOpenHelp,
  146.                               szOpenHelp);
  147.      WinLoadString (hab, (HMODULE) 0, IDS_FORMULA_FILE, sizeof szFormTitle,
  148.                               szFormTitle);
  149.      WinLoadString (hab, (HMODULE) 0, IDS_FORMULA_FILE_HELP, sizeof szFormHelp,
  150.                               szFormHelp);
  151.      WinLoadString (hab, (HMODULE) 0, IDS_LSYSTEM_FILE, sizeof szLsysTitle,
  152.                               szLsysTitle);
  153.      WinLoadString (hab, (HMODULE) 0, IDS_LSYSTEM_FILE_HELP, sizeof szLsysHelp,
  154.                               szLsysHelp);
  155.      WinLoadString (hab, (HMODULE) 0, IDS_IFS_FILE, sizeof szIFSTitle,
  156.                               szIFSTitle);
  157.      WinLoadString (hab, (HMODULE) 0, IDS_IFS_FILE_HELP, sizeof szIFSHelp,
  158.                               szIFSHelp);
  159.      WinLoadString (hab, (HMODULE) 0, IDS_IFS3D_FILE, sizeof szIFS3DTitle,
  160.                               szIFS3DTitle);
  161.      WinLoadString (hab, (HMODULE) 0, IDS_IFS3D_FILE_HELP, sizeof szIFS3DHelp,
  162.                               szIFS3DHelp);
  163.      WinLoadString (hab, (HMODULE) 0, IDS_SEL_FORMULA, sizeof szSelFormula,
  164.                               szSelFormula);
  165.      WinLoadString (hab, (HMODULE) 0, IDS_SEL_LSYSTEM, sizeof szSelLsystem,
  166.                               szSelLsystem);
  167.      WinLoadString (hab, (HMODULE) 0, IDS_COLORMAP_TITLE, sizeof szColorMapTitle,
  168.                               szColorMapTitle);
  169.      WinLoadString (hab, (HMODULE) 0, IDS_COLORMAP_HELP, sizeof szColorMapHelp,
  170.                               szColorMapHelp);
  171.      WinLoadString (hab, (HMODULE) 0, IDS_LOAD_WHAT,     sizeof szLoadWhatFmt,
  172.                               szLoadWhatFmt);
  173.      WinLoadString (hab, (HMODULE) 0, IDS_SAVE_WHAT,     sizeof szSaveWhatFmt,
  174.                               szSaveWhatFmt);
  175.      WinLoadString (hab, (HMODULE) 0, IDS_EXT_GIF,     sizeof szExtGIF,
  176.                               szExtGIF);
  177.      WinLoadString (hab, (HMODULE) 0, IDS_EXT_BMP,     sizeof szExtBMP,
  178.                               szExtBMP);
  179.      WinLoadString (hab, (HMODULE) 0, IDS_EXT_MET,     sizeof szExtMET,
  180.                               szExtMET);
  181.      WinLoadString (hab, (HMODULE) 0, IDS_EXT_PCX,     sizeof szExtPCX,
  182.                               szExtPCX);
  183.  
  184.      /* interogate the fractal engine driver table to find the number
  185.     and names (in alphabetical order) of the fractals (today) */
  186.  
  187.      CountFractalList = 0;       /* get a count the first pass */
  188.      for (k = 0; fractalspecific[k].name != NULL; k++)
  189.     if (fractalspecific[k].name[0] != '*' &&
  190.         (fractalspecific[k].flags & WINFRAC) != 0 )
  191.          CountFractalList++;
  192.  
  193.      asFracTypeList = (SHORT _far *) _fmalloc(CountFractalList * sizeof(SHORT) );
  194.  
  195.      CountFractalList = 0;
  196.      for (k = 0; fractalspecific[k].name != NULL; k++)
  197.     if (fractalspecific[k].name[0] != '*' &&
  198.         (fractalspecific[k].flags & WINFRAC) != 0 )
  199.          asFracTypeList[CountFractalList++] = k;
  200.  
  201.      fr_Qsort(&asFracTypeList[0],
  202.           &asFracTypeList[CountFractalList-1]);
  203.  
  204.      /* Initialize key workarea variables. */
  205.  
  206.      cp.ulSemMemPS = 0L;
  207.      cp.ulSemTrigger = 0L;
  208.      cp.ulSemSubEnded = 0L;
  209.      DosSemClear(&cp.ulSemMemPS) ;     /* indicate unowned */
  210.      DosSemSet (&cp.ulSemTrigger) ;     /* must wait for trigger */
  211.      DosSemSet (&cp.ulSemSubEnded) ;     /* not started, so not ended */
  212.      cp.ulSemSyncMsg = 0L;
  213.      cp.ulSemSyncMsgDone = 0L;
  214.      cp.SyncMsgAnswer = 0;
  215.      DosSemClear(&cp.ulSemSyncMsg);     /* indicate unowned */
  216.      DosSemClear(&cp.ulSemSyncMsgDone);
  217.  
  218.      cp.sSubAction = 0;
  219.      cp.fNewParms = FALSE;
  220.      cp.fContinueCalc = TRUE;
  221.      cp.fNewBits = FALSE;
  222.      cp.fSuppressPaint = TRUE;
  223.  
  224.    }
  225.  
  226. BOOL    PMfrWindowInit(HWND hwnd, MPARAM mp2)
  227.    {
  228.    PCREATESTRUCT pcrst = (PCREATESTRUCT) PVOIDFROMMP(mp2);
  229.    SIZEL sizl;
  230.    char _far *fp1;
  231.    char _far *fp2;
  232.    USHORT i, sOffset, sReps;
  233.    USHORT cxMe, cyMe, xMe, yMe;
  234.    SHORT rc;
  235.    HWND hwndMyFrame;
  236.  
  237.    hwndMyFrame = WinQueryWindow(hwnd, QW_PARENT, FALSE);
  238.  
  239.    cp.hdcScreen = WinOpenWindowDC (hwnd);
  240.    /* get required device characteristics */
  241.    DevQueryCaps ( cp.hdcScreen , CAPS_VERTICAL_RESOLUTION , 1L , & cyScreenRes ) ;
  242.    DevQueryCaps ( cp.hdcScreen , CAPS_HORIZONTAL_RESOLUTION , 1L , & cxScreenRes ) ;
  243.    DevQueryCaps ( cp.hdcScreen , CAPS_COLORS, 1L, & lScreenColors ) ;
  244.    sizl.cx = 0; sizl.cy = 0;
  245.    cp.hpsScreen = GpiCreatePS (hab, cp.hdcScreen, &sizl,
  246.             PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC);
  247.  
  248.    /* read the physical palette.  set the screen PS log color table */
  249.    /* to that palette.    the subthread will set the log color table */
  250.    /* of the memory PS to the same. */
  251.    alPhysicalColors = (ULONG _far *) _fmalloc(lScreenColors * sizeof(ULONG) ) ;
  252.    GpiQueryRealColors(cp.hpsScreen, LCOLOPT_REALIZED,
  253.               0L,   lScreenColors, alPhysicalColors);
  254.    GpiCreateLogColorTable(cp.hpsScreen, LCOL_RESET | LCOL_REALIZABLE,
  255.           LCOLF_CONSECRGB, 0L, lScreenColors, alPhysicalColors);
  256.  
  257.    /* set the forground and background colors so that
  258.       displaying a 2-color image (1-bit image) gives us black and white */
  259.  
  260.    GpiSetColor(cp.hpsScreen,
  261.         GpiQueryColorIndex(cp.hpsScreen,    /* white */
  262.                  LCOLOPT_REALIZED, 0x00FFFFFF) );
  263.    GpiSetBackColor(cp.hpsScreen,
  264.         GpiQueryColorIndex(cp.hpsScreen,    /* black */
  265.                  LCOLOPT_REALIZED, 0x00000000) );
  266.  
  267.    /* propogate the actual physical palette into the palette arrays.
  268.       This will allow this palette to be selected later. */
  269.    /* beginning of dest array */
  270.    fp2 = (char _far *) &bmiColorTablePhys.argbColor[0];
  271.    /* beginning of source array */
  272.    fp1 = (char _far *) &alPhysicalColors[0];
  273.    for (i = 0; i < lScreenColors; i++)
  274.        {   /* copy base bytes for number of screen colors */
  275.        _fmemcpy(fp2, fp1, sizeof(RGB) );
  276.        fp1 += sizeof(ULONG);
  277.        fp2 += sizeof(RGB);
  278.        }
  279.    /* now replicate the squeezed color table to 256 entries, if necessary */
  280.    if (lScreenColors < 256L)
  281.       {
  282.       fp1 = (char _far *) &bmiColorTablePhys.argbColor[0];   /* source */
  283.       sOffset = fp2 - fp1;           /* distance we have come so far */
  284.       sReps = 256L / lScreenColors;    /* if it ain't even, forget excess! */
  285.       for (i = 1; i < sReps; i++)
  286.       {
  287.       _fmemcpy(fp2, fp1, sOffset); /* copy a thwak */
  288.       fp2 += sOffset;           /*  then bump dest */
  289.       }
  290.       }
  291.  
  292.    cp.hwnd = hwnd ;
  293.  
  294.    /* size us full width, at the top of the screen, with about
  295.     one line of Icons showing at the bottom. */
  296.  
  297.    cxClient = cxMe = (USHORT) cxScreen - 2;
  298.    cyClient = cyMe = (USHORT) cyScreen - 2 * ((USHORT) cyIcon);
  299.    xMe = 0;
  300.    yMe = 2 * ((USHORT) cyIcon);
  301.    WinSetWindowPos (hwndMyFrame,
  302.        HWND_TOP, xMe, yMe, cxMe, cyMe,
  303.        /* SWP_ACTIVATE | SWP_ZORDER | */ SWP_MOVE | SWP_SIZE | SWP_SHOW);
  304.  
  305.    /* We are Active if our frame is active; else we are not active */
  306.  
  307.    fActive = (hwndMyFrame == WinQueryActiveWindow(HWND_DESKTOP, FALSE) );
  308.  
  309.    /*
  310.       Get the hourglass and arrow pointers
  311.    */
  312.    hptrWait  = WinQuerySysPointer(HWND_DESKTOP, SPTR_WAIT,  FALSE);
  313.    hptrArrow = WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE);
  314.  
  315.    sStatus = STATUS_INIT;
  316.    fPan = FALSE; fZoomWin = FALSE;
  317.    fGoodPan = FALSE; fGoodZoom = FALSE;
  318.    cp.fHaveUserPalette = FALSE;
  319.    cp.sLastLoadSaveType = SUB_LOADSAVE_GIF;
  320.  
  321.    /* Now attach the subthread, and away we go */
  322.  
  323.    rc = DosCreateThread (PMfrThread, &tidCalc,
  324.             (PBYTE) &bThreadStack[STACKSIZE]);
  325.    /* if attach was successful, set priority to low (Idle) */
  326.    if (rc == 0)
  327.       {
  328.       rc = DosSetPrty(PRTYS_THREAD, PRTYC_IDLETIME,
  329.        0, tidCalc);
  330.       }
  331.  
  332.    return (BOOL) ( (rc==0) ? TRUE : FALSE);
  333.  
  334.    }
  335.