0

I am using SetTimer to put a timer in my game, the timer will be triggered every 1000 ms, after some time, I want to change the time interval of the timer, so I call SetTimer again with the same timerId(according to MSDN, this will replace the old timer with the new time-out), but it seems not work, the timer was not fired.

here is my code, this is a typical game loop.

INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR szCmdLine, int iCmdShow)
{
    WNDCLASSEX winClass ;

    winClass.lpszClassName = L"RotationBySetTimer";
    winClass.cbSize        = sizeof(WNDCLASSEX);
    winClass.style         = CS_HREDRAW | CS_VREDRAW;
    winClass.lpfnWndProc   = MsgProc;
    winClass.hInstance     = hInstance;
    winClass.hIcon         = NULL ;
    winClass.hIconSm       = NULL ;
    winClass.hCursor       = LoadCursor(NULL, IDC_ARROW) ;
    winClass.hbrBackground = NULL ;
    winClass.lpszMenuName  = NULL ;
    winClass.cbClsExtra    = 0;
    winClass.cbWndExtra    = 0;

    RegisterClassEx (&winClass) ;  

    HWND hWnd = CreateWindowEx(NULL,  
        winClass.lpszClassName,     // window class name
        L"RotationBySetTimer",                  // window caption
        WS_OVERLAPPEDWINDOW,        // window style
        32,                         // initial x position
        32,                         // initial y position
        600,                        // initial window width
        600,                        // initial window height
        NULL,                       // parent window handle
        NULL,                       // window menu handle
        hInstance,                  // program instance handle
        NULL) ;                     // creation parameters

    **SetTimer(hWnd, 1, 1000, NULL);**

    // Initialize Direct3D
    if( SUCCEEDED(InitD3D(hWnd)))
    { 
        // Show the window
        ShowWindow( hWnd, SW_SHOWDEFAULT );
        UpdateWindow( hWnd );

        MSG msg ; 
        ZeroMemory( &msg, sizeof(msg) );
        PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );

        // Get last time
        static DWORD lastTime = timeGetTime();

        while (msg.message != WM_QUIT)  
        {
            if(PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) != 0)
            {
                TranslateMessage (&msg) ;
                DispatchMessage (&msg) ;
            }
            else // Render the game if there is no message to process
            {
                // Get current time
                DWORD currTime  = timeGetTime();

                // Calculate time elapsed
                float timeDelta = (currTime - lastTime) * 0.001f;

                // Render
                Render(hWnd, timeDelta) ;

                // Update last time to current
                lastTime = currTime;
            }
        }
    }

    UnregisterClass(winClass.lpszClassName, hInstance) ;
    return 0;
}

and I call SetTimer again in Render function to change the time interval.

void Render(HWND hWnd, float timeDelta)
{
    **SetTimer(hWnd, 1, 2000, NULL);**

    if (!g_bActive)
    {
        Sleep(50) ;
    }

    SetupMatrix() ;

    // Clear the back-buffer to a RED color
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        // Draw teapot 
        g_pTeapotMesh->DrawSubset(0) ;

        // End the scene
        g_pd3dDevice->EndScene();
    }

    // Present the back-buffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

The code works well before I call SetTimer again in Render function, but stop working after adding this line, why?

4

1 回答 1

3

按照您的编码方式,您一遍又一遍地以高速率调用 SetTimer()。不断重置计时器,因此永远不会有足够的时间让 WM_TIMER 消息发布。

显然,您需要找到更好的触发器来更新计时器。也许仅在间隔值实际需要更改时才调用 SetTimer 。从发布的代码中如何最好地做到这一点尚不清楚,因为它没有明显的触发条件。

于 2013-05-11T09:38:52.187 回答