SDL_GetTicks() accuracy below the millisecond level

后端 未结 2 1565
小蘑菇
小蘑菇 2021-01-18 16:13

I program currently something with SDL2. All works fine, but I have a problem with the SDL_GetTicks() method. Normally it should return the total application ti

2条回答
  •  日久生厌
    2021-01-18 16:28

    Well, of course, you need to actually wait until >=1ms has passed before updating your last tick count

    void Application::Update()
    {
        Uint32  current_time = SDL_GetTicks();
        Uint32  delta_time = current_time - last_update_time;
    
        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                {
                    should_close = true;
                }
                break;
    
                default:
                    break;
            }
        }
    
        if (delta_time >= 1)
        {
            // Update game objects with delta_time
    
            last_update_time = current_time;
        }   
    }
    

提交回复
热议问题