home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / RWALK.ZIP / RWALK.C next >
Text File  |  1991-06-13  |  7KB  |  214 lines

  1.  
  2. // this is RWALK.C, a simple-minded random walk program. The Esc key will clear
  3. // the screen and put the walker in the middle. Use F3 to end to program. 
  4.  
  5. #define INCL_DOSDATETIME
  6. #define INCL_GPIBITMAPS
  7. #define INCL_GPICONTROL
  8. #define INCL_GPIPRIMITIVES
  9. #define INCL_WINERRORS
  10. #define INCL_WININPUT
  11. #define INCL_WINSYS
  12. #define INCL_WINTIMER
  13. #define INCL_WINWINMANAGER
  14. #include <os2.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #define PMG_CASU       ( KC_CTRL | KC_ALT | KC_SHIFT | KC_KEYUP )
  18. #define PMG_SWP        ( SWP_SIZE | SWP_MOVE | SWP_MAXIMIZE )
  19. #define MY_TIMER       17
  20. #define NCOLOR         25
  21. #define NPOINTS        32000
  22. #define USERMSG1       (WM_USER+0)
  23.  
  24. // #define PMGDEBUG
  25. #ifdef  PMGDEBUG
  26. // #define PMGALLMSGS
  27. FILE *fp;
  28. #endif
  29.  
  30. int RandomMove(POINTL *ptlPP,int x,int y,int incr)
  31.    {
  32.    static int color=(short) CLR_BLUE,dir=8,colors[NCOLOR]={
  33.       (short) CLR_RED,(short) CLR_RED,(short) CLR_RED,(short) CLR_RED,
  34.       (short) CLR_GREEN,(short) CLR_GREEN,(short) CLR_GREEN,(short) CLR_GREEN,
  35.       (short) CLR_GREEN,
  36.       (short) CLR_CYAN,(short) CLR_CYAN,
  37.       (short) CLR_YELLOW, (short) CLR_YELLOW,(short) CLR_YELLOW,
  38.       (short) CLR_YELLOW,
  39.       (short) CLR_WHITE,(short) CLR_WHITE,
  40.       (short) CLR_PINK,(short) CLR_PINK,(short) CLR_PINK,(short) CLR_PINK,
  41.       (short) CLR_BLUE,(short) CLR_BLUE,(short) CLR_BLUE,(short) CLR_BLUE};
  42.    int nc=0,nx=(short) ptlPP->x,ny=(short) ptlPP->y;
  43.    int tmp=incr*((28&rand())+4),tdir=7&rand();
  44.    if ( 4==(dir^tdir) ) return (short) CLR_ERROR;
  45.    if ( 2>tdir || 7==tdir )
  46.       if ( (nx+tmp)<x ) nx+=tmp; else nc=1;
  47.    if ( 0<tdir && 4>tdir )
  48.       if ( (ny+tmp)<y ) ny+=tmp; else nc=1;
  49.    if ( 2<tdir && 6>tdir ) 
  50.       if ( nx>=tmp ) nx-=tmp; else nc=1;
  51.    if ( 4<tdir )
  52.       if ( ny>=tmp ) ny-=tmp; else nc=1;
  53.    if ( nc ) color=colors[rand()%NCOLOR];
  54.    else
  55.       {
  56.       ptlPP->x=nx;
  57.       ptlPP->y=ny;
  58.       dir=tdir;
  59.       }
  60.    return color;
  61.    }
  62. //1
  63. MRESULT EXPENTRY CWProc(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
  64.    {
  65.    static HAB myhab;
  66.    static POINTL ptlPointerPos,aptl[3];
  67.    static HPS hpsWindow;
  68.    static int color,cornerx,cornery,dx,dy,erase,ix,rate,size,
  69.               xynum,x[NPOINTS],y[NPOINTS],z[NPOINTS];
  70. #ifdef  PMGALLMSGS
  71. fprintf(fp,"msg=%4.4X, mp1.1=%4.4X, mp1.2=%4.4X mp2.1=%4.4X, mp2.2=%4.4X\n",
  72. msg,SHORT1FROMMP(mp1),SHORT2FROMMP(mp1),SHORT1FROMMP(mp2),SHORT2FROMMP(mp2) );
  73. #endif
  74.    switch (msg)
  75.       {
  76.    case WM_CREATE:
  77.       ix=-1;
  78.       xynum=1;
  79.       cornerx=32767;
  80.       WinStartTimer(myhab,hwnd,MY_TIMER,rate);
  81.       return (MRESULT) FALSE;
  82.    case WM_SIZE:
  83.       ix=0;
  84.       dx=SHORT1FROMMP(mp2)&(-32);
  85.       dy=SHORT2FROMMP(mp2)&(-32);
  86.       if ( dx>=cornerx && dy>=cornery )
  87.          {
  88.          cornerx=dx;
  89.          cornery=dy;
  90.          dx=(dx>>1)-x[0];
  91.          dy=(dy>>1)-y[0];
  92.          x[0]=dx;
  93.          y[0]=dy;
  94.          for ( ix=1 ; ix<xynum ; ix++ ) 
  95.             {
  96.             x[ix]+=dx;
  97.             y[ix]+=dy;
  98.             }
  99.          }
  100.       else 
  101.          {
  102.          cornerx=dx;
  103.          cornery=dy;
  104.          size=(cornerx<cornery?cornerx:cornery)/128;
  105.          if ( 0==size ) size=1;
  106.          WinPostMsg(hwnd,WM_CHAR,MPFROM2SHORT(6,257),MPFROM2SHORT(0X11B,15));
  107.          }
  108.       x[0]=cornerx>>1;
  109.       y[0]=cornery>>1;
  110.       return (MRESULT) FALSE;
  111.    case USERMSG1:
  112.       erase=SHORT1FROMMP(mp1);
  113.       if ( NPOINTS<(erase-1) ) erase=NPOINTS-1;
  114.       rate=SHORT2FROMMP(mp1);
  115.       myhab=PVOIDFROMMP(mp2);
  116.       return (MRESULT) FALSE;
  117. //2
  118.    case WM_CHAR:
  119.       if ( 0==(SHORT1FROMMP(mp1) & PMG_CASU)
  120.       && (SHORT1FROMMP(mp1) & KC_VIRTUALKEY) )
  121.          {
  122.          if ( '\x1b'==CHAR1FROMMP(mp2) )
  123.             { // Esc key
  124.             xynum=1;
  125.             ptlPointerPos.x=x[0];
  126.             ptlPointerPos.y=y[0];
  127.             WinInvalidateRect(hwnd,NULL,TRUE);
  128.             break;
  129.             }
  130.          if ( VK_F3==CHAR3FROMMP(mp2) ) 
  131.             { // f3 key
  132.             WinStopTimer(myhab,hwnd,MY_TIMER);
  133.             WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  134.             break;
  135.             }
  136.          }
  137.       break;
  138.    case WM_PAINT:    // clear screen, set pointer to center
  139.       hpsWindow=WinBeginPaint(hwnd,NULL,(PRECTL) aptl);
  140.       GpiBitBlt(hpsWindow,NULL,3L,aptl,ROP_ZERO,BBO_OR);  // BLACK
  141.       ptlPointerPos.x=x[0];
  142.       ptlPointerPos.y=y[0];
  143.       GpiMove(hpsWindow,&ptlPointerPos);
  144.       for ( ix=1 ; ix<xynum ; ix++ ) 
  145.          {
  146.          GpiSetColor(hpsWindow,(long) z[ix]);
  147.          ptlPointerPos.x=x[ix];
  148.          ptlPointerPos.y=y[ix];
  149.          GpiLine(hpsWindow,&ptlPointerPos);
  150.          }
  151.       WinEndPaint(hpsWindow);
  152.       ix=-1;
  153.       return (MRESULT) FALSE;
  154.    case WM_TIMER:
  155.       if ( MY_TIMER!=SHORT1FROMMP(mp1) || ix>=0 ) break;
  156.       hpsWindow=WinGetPS(hwnd);
  157.       GpiMove(hpsWindow,&ptlPointerPos);
  158.       color=RandomMove(&ptlPointerPos,cornerx,cornery,size);
  159.       if ( ((short) CLR_ERROR)!=color )
  160.          {
  161.          x[xynum]=(short) ptlPointerPos.x;
  162.          y[xynum]=(short) ptlPointerPos.y;
  163.          z[xynum++]=color;
  164.          GpiSetColor(hpsWindow,(long) color);
  165.          GpiLine(hpsWindow,&ptlPointerPos);
  166.          }
  167.       WinReleasePS(hpsWindow);
  168.       if ( xynum>=erase ) 
  169.          WinPostMsg(hwnd,WM_CHAR,MPFROM2SHORT(6,257),MPFROM2SHORT(0X11B,15));
  170.       break;
  171.       }
  172.    return WinDefWindowProc(hwnd,msg,mp1,mp2);
  173.    }
  174. //3
  175. int main(int argc, char *argv[])
  176.    {
  177.    static HAB hab;
  178.    static HMQ hmq;
  179.    static HWND hwndFrame,hwndClient;
  180.    static QMSG qmsg;
  181.    static ULONG flFrameFlags=
  182.           FCF_TITLEBAR+FCF_SYSMENU+FCF_MINMAX+FCF_SIZEBORDER+FCF_SHELLPOSITION;
  183.    static char szCC[]="Rwalk";
  184.    int erase=1800,rate=75;
  185.    DATETIME dt;
  186.    if ( 1<argc ) sscanf(argv[1],"%d",&rate);
  187.    if ( 2<argc ) sscanf(argv[2],"%d",&erase);
  188. #ifdef  PMGDEBUG
  189. if ( NULL==( fp=fopen("WINMSGS","w") ) ) return printf("open failed\n");
  190. #endif
  191.    DosGetDateTime(&dt);
  192.    srand(dt.hours+dt.minutes+dt.seconds+dt.hundredths+dt.day+dt.month+dt.year);
  193.    hab=WinInitialize(0);
  194.    CWProc(NULL,USERMSG1,MPFROM2SHORT(erase,rate),MPFROMP(hab));      // cheat!
  195.    hmq=WinCreateMsgQueue(hab,0); 
  196.    WinRegisterClass(hab,szCC,CWProc,CS_SIZEREDRAW,0);
  197.    hwndFrame=WinCreateStdWindow(HWND_DESKTOP,WS_VISIBLE,&flFrameFlags,
  198.                                 szCC,"Random Walker",0L,NULL,0,&hwndClient);
  199.    if ( NULL==hwndFrame ) WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,
  200.        "Unable to create window",szCC,0,MB_OK | MB_ICONEXCLAMATION );
  201.    else
  202.       {
  203.       while ( WinGetMsg(hab,&qmsg,NULL,0,0) ) WinDispatchMsg(hab,&qmsg);
  204.       WinDestroyWindow(hwndFrame);
  205.       }
  206.    WinDestroyMsgQueue(hmq);
  207.    WinTerminate(hab);
  208. #ifdef  PMGDEBUG
  209. fclose(fp);
  210. #endif
  211.    return 0;
  212.    }
  213. //4
  214.