簡體   English   中英

GLUT定時器功能

[英]GLUT timer function

我試圖讓我在棋盤游戲上的代幣慢慢下降。 現在,他們跌倒了,但跌得如此之快。 如何在我的代碼中實現計時器功能? 現在我做了一個循環,更新 glTranslate 的 y 坐標。 但是還是太快了! 頂部 y 是我在屏幕上按下的 y 坐標,底部是令牌最低開放點的坐標。

col =0;

double bottomy = 0;
int row = 0;

circlex = (double)x / width ;
circley = (double)y / height ;

row = board.getRow(col) + 1;
bottomy = 500 - (25*row);

for( double topy = y ; topy <= bottomy; topy += 2 ){
    glTranslatef(circlex, circley, 0.0f);
    circley += .0000000000000000001;
    display();
}        

r = board.makeMove(col);

您可以使用glutTimerFunc定期執行函數。 這個有簽名

void glutTimerFunc(unsigned int msecs,
                   void (*func)(int value),
                   value);

例如,如果您的繪圖功能是

void UpdateTokens(int time);

然后您可以使用以下調用每 0.5 秒調用一次更新(其中current_time是當前模擬時間)

glutTimerFunc(500, UpdateTokens, current_time);

為了獲得更精確的計時,我建議使用<chrono>代替,並使用std::chrono::steady_clock std::chrono::durationstd::chrono::steady_clock類的東西來執行std::chrono::steady_clock

這里的實際問題是過剩是如何運作的。 基本上,用戶只能在主循環結束時獲得一個圖像。 只要您不從鼠標功能返回,屏幕上就不會顯示任何內容。 您可以通過將工作轉移到顯示功能並將翻譯分布在多個框架中來解決問題:

全局變量:

double circlex = 0, circley = 0, bottomy = 0;
bool isfalling = false;
int topy = 0;

鼠標功能:

if (isfalling == false) //Prevents the user from clicking during an animation
{
    circlex = (double)x / width ;
    circley = (double)y / height ;

    int row = board.getRow(col) + 1;
    bottomy = 500 - (25*row);
    topy = y;

    isfalling = true;
}

display_func:

if (isfalling)
{
    circley += .0000000000000000001;
    topy += 2;

    if (topy >= bottomy)
        isfalling = false;
}

glTranslatef(circlex, circley, 0.0f);
display();

暫無
暫無

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

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