File:  [OS/2 SDKs] / pmsdk / samples / chaos / chaos.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:28:13 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: pmsdk-1988, HEAD
Microsoft OS/2 SDK PM 08-08-1988

/*--------------------- file identification ------------------------*/

/*
        chaos.c
        Opens a window.

        Supports the system menu and default window behavior

        Also supports window menus and an about dialog

        Created by Microsoft Corp., 1988
*/


/*---------------------- include files -----------------------------*/
#define INCL_PM
#define INCL_DOS
#include <os2.h>
#include "chaos.h"

/*-------------------------- global variables ----------------------*/
/* message stuff */
HMQ     hmqTheMessageQueue;
HAB     habTheAnchorBlock;

/* window stuff */
char    szClassName[] = "chaos";      /* our window class name */
HWND    hwndClient;                 /* handle to the client */
HWND    hwndFrame;                  /* handle to the frame window */
HPS     hps;


/* Thread stuff */
#define STACK_SIZE 32000

BOOL    bContinue = FALSE;
LONG    lSemTrigger;
TID     idThread;
UCHAR   rbThreadStack[STACK_SIZE];


/*------------------------------ main ------------------------------*/

BOOL    main() 

        {
/*
 * local constants
 */
        #define USE_DEF_QUEUE_SIZE 0

/*
 * local variables
 */
        QMSG qmsg;

/*
 * initialize this application thread's use of the PM
 */
        habTheAnchorBlock = WinInitialize (NULL) ;
       
/*
 * create a message queue for this application thread
 */
        hmqTheMessageQueue = WinCreateMsgQueue
                                (habTheAnchorBlock, USE_DEF_QUEUE_SIZE) ;
 /*
  * initialize the application
  */
        if (!GenericInit())
                return(FALSE);          /* failed loading */



 /*
  * run the main event loop
  */
        while ( WinGetMsg ( (HAB)NULL, &qmsg, (HWND)NULL, 0, 0 ) )
                WinDispatchMsg( (HAB)NULL, &qmsg );



        /* suspend the other thread so it doesn't mess around while we quit */
        DosSuspendThread(idThread);

  /*
   * kill the window
   */
        WinDestroyWindow( hwndFrame );

  /*
   * PM cleanup
   */
        WinDestroyMsgQueue( hmqTheMessageQueue );
        WinTerminate( habTheAnchorBlock );
        }


/*--------------------- GenericInit -------------------------*/
BOOL GenericInit() {
    ULONG   lControlStyle = FCF_STANDARD;


    if (!WinRegisterClass( habTheAnchorBlock,
            szClassName,
            (PFNWP)GenericWndProc,
            CS_SIZEREDRAW,
            0))
        return( FALSE );

    hwndFrame = WinCreateStdWindow( HWND_DESKTOP,
        FS_ACCELTABLE | FS_BORDER,
        &lControlStyle,
        szClassName,
        "chaos",
        0L,
        NULL,
        ID_RESOURCE,                  /* resource ID from .rc file */
        &hwndClient);


    WinSetWindowPos(hwndFrame,          /* window */
                    HWND_TOP,           /* window behind */
                    50,                 /* x pos */
                    50,                 /* y pos */
                    200,                /* x size */
                    100,                /* y size */
                    SWP_ACTIVATE|SWP_SIZE|SWP_MOVE|SWP_SHOW);   /* flags */

    if (!hwndFrame)
        return(FALSE);
   
    return(TRUE);
}

/*---------------- the main window procedure --------------------------*/
MRESULT FAR PASCAL GenericWndProc( hwnd, usMessage, mp1, mp2 )
HWND   hwnd;
USHORT usMessage;
MPARAM  mp1;
MPARAM  mp2;
{
    HPS     hpsLocal;
    RECTL    rect;
    POINTL  point;
    USHORT  usBarWidth;
    LINEBUNDLE       stTheLineBundle ;

    switch (usMessage) {


        case WM_CREATE:
            hps = WinGetPS(hwnd);
            DosSemSet(&lSemTrigger);

            DosCreateThread((PFNTHREAD)ThreadProc,
                            &idThread,
                            rbThreadStack+STACK_SIZE-2);

            return 0L;

        case WM_PAINT:
            hpsLocal = WinBeginPaint(hwnd,NULL,&rect);
            WinFillRect(hpsLocal,&rect,CLR_WHITE);
            WinEndPaint(hpsLocal);
            return 0L;

        case WM_COMMAND:
            DoMenuCommand(hwnd,LOUSHORT (mp1));
            return 0L;

        case WM_RESETPLOT:
            /*hps = WinGetPS(hwndClient);*/
            WinQueryWindowRect(hwnd,&rect);
            point.x = 0;point.y = rect.yTop / 2;
            GpiMove(hps,&point);
            WinFillRect(hps,&rect,CLR_WHITE);
            return 0L;

        case WM_PLOTPOINT:
            WinQueryWindowRect(hwnd,&rect);
            usBarWidth = (rect.xRight /MAX_ITERATIONS) ;
            point.x = ((USHORT)mp1)* usBarWidth ;
            point.y = ((((USHORT)mp2) * rect.yTop) / 100);

            stTheLineBundle.lColor= CLR_RED;
            GpiSetAttrs(hps,
                    PRIM_LINE,
                    (ULONG) LBB_COLOR,
                    (ULONG) 0,
                     &stTheLineBundle);


            GpiLine(hps,&point);
            return 0L;

        case WM_CLOSE:
            WinReleasePS(hps);
            break;  /* go to default winproc */

    }
    return( WinDefWindowProc( hwnd, usMessage, mp1, mp2 ) );

}

/*--------------------------- AboutProc -----------------------------*/
MRESULT FAR PASCAL  AboutProc (hwnd, usMessage,
                                mp1, mp2)
        
    HWND    hwnd ;
    USHORT  usMessage ;
    MPARAM   mp1 ;
    MPARAM   mp2 ;
        
    {
    /* case out on the message */
    switch (usMessage)
        {
        case WM_COMMAND:
               switch (LOUSHORT (mp1))
                    {
                     case DID_OK:
                        WinDismissDlg(hwnd,0);
                        return 0L;
                    }
            break;
        }
    return ( WinDefDlgProc (hwnd, usMessage,
                            mp1, mp2) ) ;

    }

/*---------------------------- DoMenuCommand -----------------------*/
ULONG DoMenuCommand(hwnd, usMenuID)

    HWND    hwnd;
    USHORT  usMenuID;
    {
           switch (usMenuID)
                {
                case IDM_FI_GO:
                    bContinue = TRUE;
                    DosSemClear(&lSemTrigger);

                    break;
                case IDM_FI_STOP:
                    bContinue = FALSE;

                    break;
                case IDM_FI_QUIT:
                    /* Don't use WinSENDMsg here */
                    WinPostMsg(hwnd,WM_CLOSE,0L,0L);
                    break;
                case IDM_FI_ABOUT:
                    /* this is a modal dialog */
                    WinDlgBox(HWND_DESKTOP ,
                              hwndFrame,
                              (PFNWP)AboutProc,
                              NULL,
                              IDD_ABOUT,
                              NULL);
                    break;

                default:
                    break;

                };
    }

/*----------------------------- ThreadProc --------------------------*/

VOID FAR ThreadProc()
    {

    double  dPopulation = .5;
    double  dCurrentPop;
    double  dGrowthRate = INITIAL_GROWTH_RATE;
    double  dInc = .01;

    short   i;
    HPS     hps;
    RECTL   rect;
    POINTL  point;
    LINEBUNDLE       stTheLineBundle ;

   DosSetPrty(PRTYS_THREAD,
              PRTYC_IDLETIME,
              0,
              0);

    while (1)
        {
        DosSemWait(&lSemTrigger,-1L);

        WinPostMsg(hwndClient,WM_RESETPLOT,
                    0L,0L);

        dCurrentPop = dPopulation;

        for (i = 0; i <= MAX_ITERATIONS; i++)
            {

            if (!bContinue)
                break;

            dCurrentPop = dCurrentPop * dGrowthRate * (1 - dCurrentPop);

            WinPostMsg(hwndClient,WM_PLOTPOINT,
                       MPFROMSHORT(i),
                       MPFROMSHORT((USHORT)(dCurrentPop*100)));
            }


        dGrowthRate = dGrowthRate + dInc;
        if (dGrowthRate >= MAX_GROWTH_RATE)
            dGrowthRate = INITIAL_GROWTH_RATE;

        if (!bContinue)
            DosSemSet(&lSemTrigger);

        DosSleep(500L);

        }
    }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.