CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Win32 SDK

  1. #1
    Join Date
    Mar 1999
    Posts
    1

    Win32 SDK



    Win 32 SDK

    I am trying to write a small Bricks Program.

    Here the bouncing ball is in a seperate thread while the primary

    thread handles the paddle movements.

    The basic BallProc is somewhat like this

    DWORD WINAPI BallProc(LPVOID lpThreadParam)

    {

    HWND hwnd = (HWND)lpThreadParam;

    RECT ClientRect;

    POINT PrevBallPos;

    HDC hdc;

    hdc = GetDC(hwnd);

    GetClientRect(hwnd,&ClientRect);

    BallPos.x = ClientRect.right / 2;

    BallPos.y = ClientRect.bottom / 2;

    BallDir.x = 1;

    BallDir.y = 1;

    while(!ExitBallThread)

    {

    BitBlt(hdc,BallPos.x,BallPos.y,BallSize,BallSize,MemBallDC,0,0,SRCCOPY);

    memcpy(&PrevBallPos,&BallPos,sizeof(POINT));

    BallPos.x += BallDir.x;

    BallPos.y += BallDir.y;

    if(BallPos.y >= ClientRect.bottom - BallSize|| BallPos.y <= ClientRect.top)

    BallDir.y = -BallDir.y;

    if(BallPos.x >= ClientRect.right - BallSize|| BallPos.x <= ClientRect.left)

    BallDir.x = -BallDir.x;

    /* Problem here */

    // Sleep(1);

    PatBlt(hdc,PrevBallPos.x,PrevBallPos.y,BallSize,BallSize,WHITENESS);

    }

    ReleaseDC(hwnd,hdc);

    ExitThread(0);

    }

    In the above code,if i do not give the Sleep,the ball movement is extremely

    fast.However if i give Sleep(1) the ball slows down a lot it is very easy to

    paddle it.I want to control the movement of the ball(ie i want the ball to move much faster than what i get with

    Sleep(1) but not as fast as the one without Sleep(1)).


    If anyone has got any ideas pl help me.


    SriramMR




  2. #2
    Join Date
    Mar 1999
    Posts
    3

    Re: Win32 SDK



    One simple solution would be to determine haw many pixel per second you want to move the ball.

    The values in nPixelsPerSecond and nFramesPerSecond should not

    be hardcoded since they should be changed depending on which speed the user wants and the performance of the PC it´s

    running on.

    int nPixelsPerSecond = 800; // High value = fast ball

    int nFramesPerSecond = 100; // High value = smother graphichs

    int nPixelsPerFrame = nPixelsPerSecond / nFramesPerSecond;

    int nMilliSecondsBetweenFrames = 1000/*one second in ms*/ / nFramesPerSecond;

    int nMilliSecondsElapsed;

    GetClientRect(hwnd,&ClientRect);

    BallPos.x = ClientRect.right / 2;

    BallPos.y = ClientRect.bottom / 2;

    BallDir.x = 2;

    BallDir.y = 2;

    while(::IsWindow(hwnd))

    {

    nMilliSecondsElapsed = (int)GetTickCount();

    PatBlt(hdc,PrevBallPos.x,PrevBallPos.y,BallSize,BallSize,WHITENESS);

    BitBlt(hdc,BallPos.x,BallPos.y,BallSize,BallSize,MemBallDC,0,0,SRCCOPY);

    memcpy(&PrevBallPos,&BallPos,sizeof(POINT));

    for ( int nIndex = 0; nIndex < nPixelsPerFrame; nIndex++ )

    {

    BallPos.x += BallDir.x;

    BallPos.y += BallDir.y;

    if(BallPos.y >= ClientRect.bottom - BallSize|| BallPos.y <= ClientRect.top)

    BallDir.y = -BallDir.y;

    if(BallPos.x >= ClientRect.right - BallSize|| BallPos.x <= ClientRect.left)

    BallDir.x = -BallDir.x;

    }

    nMilliSecondsElapsed = (int)GetTickCount() - nMilliSecondsElapsed;

    /* Problem here */

    if ( nMilliSecondsElapsed < nMilliSecondsBetweenFrames )

    {

    Sleep(nMilliSecondsBetweenFrames - nMilliSecondsElapsed);

    }



    }

    ReleaseDC(hwnd,hdc);

    ExitThread(0);


    Hope this is of some help to you

    Mats Bejedahl

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured