Annotation of pmsdk/samples/chaos/chaos.c, revision 1.1.1.1

1.1       root        1: /*--------------------- file identification ------------------------*/
                      2: 
                      3: /*
                      4:         chaos.c
                      5:         Opens a window.
                      6: 
                      7:         Supports the system menu and default window behavior
                      8: 
                      9:         Also supports window menus and an about dialog
                     10: 
                     11:         Created by Microsoft Corp., 1988
                     12: */
                     13: 
                     14: 
                     15: /*---------------------- include files -----------------------------*/
                     16: #define INCL_PM
                     17: #define INCL_DOS
                     18: #include <os2.h>
                     19: #include "chaos.h"
                     20: 
                     21: /*-------------------------- global variables ----------------------*/
                     22: /* message stuff */
                     23: HMQ     hmqTheMessageQueue;
                     24: HAB     habTheAnchorBlock;
                     25: 
                     26: /* window stuff */
                     27: char    szClassName[] = "chaos";      /* our window class name */
                     28: HWND    hwndClient;                 /* handle to the client */
                     29: HWND    hwndFrame;                  /* handle to the frame window */
                     30: HPS     hps;
                     31: 
                     32: 
                     33: /* Thread stuff */
                     34: #define STACK_SIZE 32000
                     35: 
                     36: BOOL    bContinue = FALSE;
                     37: LONG    lSemTrigger;
                     38: TID     idThread;
                     39: UCHAR   rbThreadStack[STACK_SIZE];
                     40: 
                     41: 
                     42: /*------------------------------ main ------------------------------*/
                     43: 
                     44: BOOL    main() 
                     45: 
                     46:         {
                     47: /*
                     48:  * local constants
                     49:  */
                     50:         #define USE_DEF_QUEUE_SIZE 0
                     51: 
                     52: /*
                     53:  * local variables
                     54:  */
                     55:         QMSG qmsg;
                     56: 
                     57: /*
                     58:  * initialize this application thread's use of the PM
                     59:  */
                     60:         habTheAnchorBlock = WinInitialize (NULL) ;
                     61:        
                     62: /*
                     63:  * create a message queue for this application thread
                     64:  */
                     65:         hmqTheMessageQueue = WinCreateMsgQueue
                     66:                                 (habTheAnchorBlock, USE_DEF_QUEUE_SIZE) ;
                     67:  /*
                     68:   * initialize the application
                     69:   */
                     70:         if (!GenericInit())
                     71:                 return(FALSE);          /* failed loading */
                     72: 
                     73: 
                     74: 
                     75:  /*
                     76:   * run the main event loop
                     77:   */
                     78:         while ( WinGetMsg ( (HAB)NULL, &qmsg, (HWND)NULL, 0, 0 ) )
                     79:                 WinDispatchMsg( (HAB)NULL, &qmsg );
                     80: 
                     81: 
                     82: 
                     83:         /* suspend the other thread so it doesn't mess around while we quit */
                     84:         DosSuspendThread(idThread);
                     85: 
                     86:   /*
                     87:    * kill the window
                     88:    */
                     89:         WinDestroyWindow( hwndFrame );
                     90: 
                     91:   /*
                     92:    * PM cleanup
                     93:    */
                     94:         WinDestroyMsgQueue( hmqTheMessageQueue );
                     95:         WinTerminate( habTheAnchorBlock );
                     96:         }
                     97: 
                     98: 
                     99: /*--------------------- GenericInit -------------------------*/
                    100: BOOL GenericInit() {
                    101:     ULONG   lControlStyle = FCF_STANDARD;
                    102: 
                    103: 
                    104:     if (!WinRegisterClass( habTheAnchorBlock,
                    105:             szClassName,
                    106:             (PFNWP)GenericWndProc,
                    107:             CS_SIZEREDRAW,
                    108:             0))
                    109:         return( FALSE );
                    110: 
                    111:     hwndFrame = WinCreateStdWindow( HWND_DESKTOP,
                    112:         FS_ACCELTABLE | FS_BORDER,
                    113:         &lControlStyle,
                    114:         szClassName,
                    115:         "chaos",
                    116:         0L,
                    117:         NULL,
                    118:         ID_RESOURCE,                  /* resource ID from .rc file */
                    119:         &hwndClient);
                    120: 
                    121: 
                    122:     WinSetWindowPos(hwndFrame,          /* window */
                    123:                     HWND_TOP,           /* window behind */
                    124:                     50,                 /* x pos */
                    125:                     50,                 /* y pos */
                    126:                     200,                /* x size */
                    127:                     100,                /* y size */
                    128:                     SWP_ACTIVATE|SWP_SIZE|SWP_MOVE|SWP_SHOW);   /* flags */
                    129: 
                    130:     if (!hwndFrame)
                    131:         return(FALSE);
                    132:    
                    133:     return(TRUE);
                    134: }
                    135: 
                    136: /*---------------- the main window procedure --------------------------*/
                    137: MRESULT FAR PASCAL GenericWndProc( hwnd, usMessage, mp1, mp2 )
                    138: HWND   hwnd;
                    139: USHORT usMessage;
                    140: MPARAM  mp1;
                    141: MPARAM  mp2;
                    142: {
                    143:     HPS     hpsLocal;
                    144:     RECTL    rect;
                    145:     POINTL  point;
                    146:     USHORT  usBarWidth;
                    147:     LINEBUNDLE       stTheLineBundle ;
                    148: 
                    149:     switch (usMessage) {
                    150: 
                    151: 
                    152:         case WM_CREATE:
                    153:             hps = WinGetPS(hwnd);
                    154:             DosSemSet(&lSemTrigger);
                    155: 
                    156:             DosCreateThread((PFNTHREAD)ThreadProc,
                    157:                             &idThread,
                    158:                             rbThreadStack+STACK_SIZE-2);
                    159: 
                    160:             return 0L;
                    161: 
                    162:         case WM_PAINT:
                    163:             hpsLocal = WinBeginPaint(hwnd,NULL,&rect);
                    164:             WinFillRect(hpsLocal,&rect,CLR_WHITE);
                    165:             WinEndPaint(hpsLocal);
                    166:             return 0L;
                    167: 
                    168:         case WM_COMMAND:
                    169:             DoMenuCommand(hwnd,LOUSHORT (mp1));
                    170:             return 0L;
                    171: 
                    172:         case WM_RESETPLOT:
                    173:             /*hps = WinGetPS(hwndClient);*/
                    174:             WinQueryWindowRect(hwnd,&rect);
                    175:             point.x = 0;point.y = rect.yTop / 2;
                    176:             GpiMove(hps,&point);
                    177:             WinFillRect(hps,&rect,CLR_WHITE);
                    178:             return 0L;
                    179: 
                    180:         case WM_PLOTPOINT:
                    181:             WinQueryWindowRect(hwnd,&rect);
                    182:             usBarWidth = (rect.xRight /MAX_ITERATIONS) ;
                    183:             point.x = ((USHORT)mp1)* usBarWidth ;
                    184:             point.y = ((((USHORT)mp2) * rect.yTop) / 100);
                    185: 
                    186:             stTheLineBundle.lColor= CLR_RED;
                    187:             GpiSetAttrs(hps,
                    188:                     PRIM_LINE,
                    189:                     (ULONG) LBB_COLOR,
                    190:                     (ULONG) 0,
                    191:                      &stTheLineBundle);
                    192: 
                    193: 
                    194:             GpiLine(hps,&point);
                    195:             return 0L;
                    196: 
                    197:         case WM_CLOSE:
                    198:             WinReleasePS(hps);
                    199:             break;  /* go to default winproc */
                    200: 
                    201:     }
                    202:     return( WinDefWindowProc( hwnd, usMessage, mp1, mp2 ) );
                    203: 
                    204: }
                    205: 
                    206: /*--------------------------- AboutProc -----------------------------*/
                    207: MRESULT FAR PASCAL  AboutProc (hwnd, usMessage,
                    208:                                 mp1, mp2)
                    209:         
                    210:     HWND    hwnd ;
                    211:     USHORT  usMessage ;
                    212:     MPARAM   mp1 ;
                    213:     MPARAM   mp2 ;
                    214:         
                    215:     {
                    216:     /* case out on the message */
                    217:     switch (usMessage)
                    218:         {
                    219:         case WM_COMMAND:
                    220:                switch (LOUSHORT (mp1))
                    221:                     {
                    222:                      case DID_OK:
                    223:                         WinDismissDlg(hwnd,0);
                    224:                         return 0L;
                    225:                     }
                    226:             break;
                    227:         }
                    228:     return ( WinDefDlgProc (hwnd, usMessage,
                    229:                             mp1, mp2) ) ;
                    230: 
                    231:     }
                    232: 
                    233: /*---------------------------- DoMenuCommand -----------------------*/
                    234: ULONG DoMenuCommand(hwnd, usMenuID)
                    235: 
                    236:     HWND    hwnd;
                    237:     USHORT  usMenuID;
                    238:     {
                    239:            switch (usMenuID)
                    240:                 {
                    241:                 case IDM_FI_GO:
                    242:                     bContinue = TRUE;
                    243:                     DosSemClear(&lSemTrigger);
                    244: 
                    245:                     break;
                    246:                 case IDM_FI_STOP:
                    247:                     bContinue = FALSE;
                    248: 
                    249:                     break;
                    250:                 case IDM_FI_QUIT:
                    251:                     /* Don't use WinSENDMsg here */
                    252:                     WinPostMsg(hwnd,WM_CLOSE,0L,0L);
                    253:                     break;
                    254:                 case IDM_FI_ABOUT:
                    255:                     /* this is a modal dialog */
                    256:                     WinDlgBox(HWND_DESKTOP ,
                    257:                               hwndFrame,
                    258:                               (PFNWP)AboutProc,
                    259:                               NULL,
                    260:                               IDD_ABOUT,
                    261:                               NULL);
                    262:                     break;
                    263: 
                    264:                 default:
                    265:                     break;
                    266: 
                    267:                 };
                    268:     }
                    269: 
                    270: /*----------------------------- ThreadProc --------------------------*/
                    271: 
                    272: VOID FAR ThreadProc()
                    273:     {
                    274: 
                    275:     double  dPopulation = .5;
                    276:     double  dCurrentPop;
                    277:     double  dGrowthRate = INITIAL_GROWTH_RATE;
                    278:     double  dInc = .01;
                    279: 
                    280:     short   i;
                    281:     HPS     hps;
                    282:     RECTL   rect;
                    283:     POINTL  point;
                    284:     LINEBUNDLE       stTheLineBundle ;
                    285: 
                    286:    DosSetPrty(PRTYS_THREAD,
                    287:               PRTYC_IDLETIME,
                    288:               0,
                    289:               0);
                    290: 
                    291:     while (1)
                    292:         {
                    293:         DosSemWait(&lSemTrigger,-1L);
                    294: 
                    295:         WinPostMsg(hwndClient,WM_RESETPLOT,
                    296:                     0L,0L);
                    297: 
                    298:         dCurrentPop = dPopulation;
                    299: 
                    300:         for (i = 0; i <= MAX_ITERATIONS; i++)
                    301:             {
                    302: 
                    303:             if (!bContinue)
                    304:                 break;
                    305: 
                    306:             dCurrentPop = dCurrentPop * dGrowthRate * (1 - dCurrentPop);
                    307: 
                    308:             WinPostMsg(hwndClient,WM_PLOTPOINT,
                    309:                        MPFROMSHORT(i),
                    310:                        MPFROMSHORT((USHORT)(dCurrentPop*100)));
                    311:             }
                    312: 
                    313: 
                    314:         dGrowthRate = dGrowthRate + dInc;
                    315:         if (dGrowthRate >= MAX_GROWTH_RATE)
                    316:             dGrowthRate = INITIAL_GROWTH_RATE;
                    317: 
                    318:         if (!bContinue)
                    319:             DosSemSet(&lSemTrigger);
                    320: 
                    321:         DosSleep(500L);
                    322: 
                    323:         }
                    324:     }

unix.superglobalmegacorp.com

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