コード例 #1
0
ファイル: events.cpp プロジェクト: boyjimeking/paintown
static void doStandardLoop(Logic & logic, Draw & draw){
    if (Graphics::screenParameter.current() == NULL){
        throw Exception::Base(__FILE__, __LINE__);
    }
    const Graphics::Bitmap & screen = *Graphics::screenParameter.current();
    screen.clear();
    draw.drawFirst(screen.aspectRatio(640, 480));
    screen.BlitToScreen();
    Global::speed_counter4 = 0;
    double runCounter = 0;
    try{
        const int maxCount = 20;
        int frameCount = 0;
        uint64_t frameTime = 0;
        int logicCount = 0;
        uint64_t logicTime = 0;
        while (!logic.done()){
            if (Global::speed_counter4 > 0){
                // Global::debug(0) << "Speed counter " << Global::speed_counter4 << std::endl;
                runCounter += logic.ticks(Global::speed_counter4);
                Global::speed_counter4 = 0;
                bool need_draw = false;
                while (runCounter >= 1.0){
                    need_draw = true;
                    InputManager::poll();
                    checkFullscreen();
                    runCounter -= 1;
                    logicCount += 1;
                    uint64_t now = System::currentMilliseconds();
                    logic.run();
                    uint64_t later = System::currentMilliseconds();
                    logicTime += (later - now);

                    if (logicCount >= maxCount){
                        // Global::debug(0) << "Logic average " << (logicTime / logicCount) << "ms" << std::endl;
                        logicCount = 0;
                        logicTime = 0;
                    }

                    if (shutdown()){
                        throw ShutdownException();
                    }

                    if (logic.done()){
                        /* quit the loop immediately */
                        throw LoopDone();
                    }
                }

                if (need_draw){
                    frameCount += 1;
                    draw.updateFrames();
                    uint64_t now = System::currentMilliseconds();
                    screen.clear();
                    draw.draw(screen.aspectRatio(640, 480));
                    screen.BlitToScreen();
                    uint64_t later = System::currentMilliseconds();
                    frameTime += (later - now);

                    if (frameCount >= maxCount){
                        // Global::debug(0) << "Draw average " << (frameTime / frameCount) << "ms" << std::endl;
                        frameCount = 0;
                        frameTime = 0;
                    }
                }
            }

            while (Global::speed_counter4 == 0){
                /* if the fps is limited then don't keep redrawing */
                if (Global::rateLimit){
                    rest(1);
                } else {
                    draw.updateFrames();
                    screen.clear();
                    draw.draw(screen.aspectRatio(640, 480));
                    screen.BlitToScreen();
                }
            }
        }
    } catch (const LoopDone & done){
    }
}