Beispiel #1
0
// メインループ
void Game::MainLoop(void) {
    // 音楽再生
    Mix_PlayMusic(music_main,-1);
    // イベント
    SDL_Event event;
    double next_frame = SDL_GetTicks();
    double wait = 1000.0 / 60;
    // TOP画面
    Top top;
    // PLAY画面
    Play play;
    // ゲーム設定
    Config config;
    // パズルの区画やブロックを並べる
    PuzzleManager puzzle_manager;
    // ブロックの集合
    Block blocks[BLOCK_MAX] = {};
    // 区画の集合
    Section sections[SECTION_MAX] = {};
    // ストップウォッチ
    boost::timer t;
    
    for (;;) {
        // すべてのイベントを処理する
        while (SDL_PollEvent(&event)) {
            // 終了 (QUIT イベントが発生するか、ESC キーが押されたら)
            if ((event.type == SDL_QUIT) ||
                (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE))
                return;

            // TOP画面
            if (view_type == VIEW_TOP){
                // イベント処理
                int result = top.Event(&event, &config, &puzzle_manager, sections, blocks);
                if (result == ON){
                    view_type = VIEW_PLAY;
                }
            }
            
            // PLAY画面
            if (view_type == VIEW_PLAY){
                // イベント処理
                play.Event(&event, &config, &puzzle_manager, sections, blocks, &t);
            }
  
            // 全画面共通
            // キーボード操作
            if (event.type == SDL_KEYDOWN){
                // 右Shift
                if (event.key.keysym.sym == SDLK_RSHIFT){
                    // 設定初期化
                    config.Reset();
                    // ブロック未操作
                    play.SetFlagOperated(OFF);
                    // 得点リセット
                    puzzle_manager.ResetScore();
                    // 操作回数リセット
                    play.SetNumberOfOperations(OFF);
                    // クリア状況リセット
                    play.SetPlayResult(OFF);
                    // プレイ状況リセット
                    play.Reset();
                    // TOP画面へ遷移
                    view_type = VIEW_TOP;
                }
            }
        }
        
        // 1秒間に60回Updateされるようにする
        if (SDL_GetTicks() >= next_frame) {
            Update(&config, &puzzle_manager, sections, &top, &play, blocks);
            // 描画
            Draw(&config, sections, &top, &play, &puzzle_manager, blocks, &t);
            next_frame += wait;
            // CPU休ませる。どれくらいの数値がベスト…?
            SDL_Delay(3);
        }
    }
}