簡體   English   中英

中斷定時器win32 c++

[英]Break timer win32 c++

我正在嘗試通過使用計時器創建一個 animation 循環。 我想在大約 5-10 秒后調用不同的SetRenderParams(int) 我不知道計時器是如何工作的,而且我可以在網上找到的文檔非常少。

我嘗試了ifwhile語句來打破計時器,但我開始明白它不能作為循環工作,因此它無法檢查計數。 我永遠無法移動到IDT_TIMER2 我想我可能不需要多個計時器,因為我不介意保持相同的 animation 速度(這是計時器本質上的幫凶),但是一種在不中斷計時器的情況下交替SetRenderParams(int)的方法,也許(?)。

我也檢查了timerqueues ,但是當我檢查它們時,我越來越困惑......我遵循的鏈接如下,但是這個沒有顯示按順序輸入多個動畫的方法,只有一個. 需要明確的是,我不想同時有多個動畫。 我想要 animation 之后的 animation,彼此不同。 http://www.winprog.org/tutorial/animation.html

我發布的代碼從我嘗試過的所有實驗中都很清楚。

LRESULT Framework::MsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // Set two timers. 
    const int IDT_TIMER1 = 1;
    const int IDT_TIMER2 = 2;
    SetTimer(hWnd,             // handle to main window 
        IDT_TIMER1,            // timer identifier 
        50,       // 10-second interval 
        NULL);     // no timer callback 

    SetTimer(hWnd,             // handle to main window 
        IDT_TIMER2,            // timer identifier 
        20000,                // five-minute interval 
        NULL);     // no timer callback 

    switch (message)
    {
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);

        //=== Add code for Rendering Models);
        RenderScene(hdc);

        EndPaint(hWnd, &ps);
    }
    break;

    case WM_TIMER:
    {
        int count = 0;
        RECT rcClient;
        GetClientRect(hWnd, &rcClient);
        switch (wParam)
        {

        case    IDT_TIMER1:
            SetRenderParams(13);
            InvalidateRect(hWnd, NULL, TRUE);
            ReleaseDC(hWnd, hdc);
            break;
        case  IDT_TIMER2:
            SetRenderParams(14);
            InvalidateRect(hWnd, NULL, TRUE);
            ReleaseDC(hWnd, hdc);
            break;
            }
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

編輯:忘記添加初始化變量。

編輯:提供.sln文件的鏈接https://www.mediafire.com/file/x42hy751n89z5bf/Rasteriser.zip/file

我想出了一個“解決方案”,但它確實是糟糕的代碼。 我創建了一個包含時鍾的Update方法,然后在WM_TIMER內部調用。 我必須手動輸入所有clock命令,因為我有大約 20 個SetRenderParams ,所以我們談論的是非常未優化的程序。 這是代碼:

void Framework::UpdateCube(HWND hWnd)
{
    clock_t  t;
    t = clock();
    if (t < 10000.0f)
    {
        SetRenderParams(13);
        InvalidateRect(hWnd, NULL, TRUE);
        t = clock();
    }
    if (t >= 10000.0f && t < 10100.0f)
    {
        SetRenderParams(10);
        InvalidateRect(hWnd, NULL, TRUE);
    }
    if (t >= 10100.0f && t < 20100.0f)
    {
        SetRenderParams(15);
        InvalidateRect(hWnd, NULL, TRUE);
    }
    if (t >= 20100.0f && t < 20200.0f)
    {
        SetRenderParams(10);
        InvalidateRect(hWnd, NULL, TRUE);
    }
}

. . .

case WM_TIMER:
{

    RECT rcClient;
    GetClientRect(hWnd, &rcClient);
    UpdateCube(hWnd);
}
break;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM