void C4Application::Execute() { CStdApp::Execute(); // Recursive execution check static int32_t iRecursionCount = 0; ++iRecursionCount; // Exec depending on game state assert(AppState != C4AS_None); switch (AppState) { case C4AS_Quit: // Do nothing, HandleMessage will return HR_Failure soon break; case C4AS_PreInit: if (!PreInit()) Quit(); break; case C4AS_Startup: #ifdef USE_CONSOLE // Console engines just stay in this state until aborted or new commands arrive // on stdin #else AppState = C4AS_Game; // if no scenario or direct join has been specified, get game startup // parameters by startup dialog Game.ScenarioTitle.Copy(LoadResStr("IDS_PRC_INITIALIZE")); if (!C4Startup::Execute()) { Quit(); --iRecursionCount; return; } AppState = C4AS_StartGame; #endif break; case C4AS_StartGame: // immediate progress to next state; OpenGame will enter // HandleMessage-loops // in startup and lobby! AppState = C4AS_Game; // first-time game initialization if (!OpenGame()) { // set error flag (unless this was a lobby user abort) if (!C4GameLobby::UserAbort) Game.fQuitWithError = true; // no start: Regular QuitGame; this may reset the engine to startup mode // if desired QuitGame(); } break; case C4AS_Game: { uint32_t iThisGameTick = timeGetTime(); // Game (do additional timing check) if (Game.IsRunning && iRecursionCount <= 1) if (Game.GameGo || !iExtraGameTickDelay || (iThisGameTick > iLastGameTick + iExtraGameTickDelay)) { // Execute Game.Execute(); // Save back time iLastGameTick = iThisGameTick; } // Graphics if (!Game.DoSkipFrame) { uint32_t iPreGfxTime = timeGetTime(); // Fullscreen mode if (isFullScreen) FullScreen.Execute(iRecursionCount > 1); // Console mode else Console.Execute(); // Automatic frame skip if graphics are slowing down the game (skip max. // every 2nd frame) Game.DoSkipFrame = Game.Parameters.AutoFrameSkip && ((iPreGfxTime + iGameTickDelay) < timeGetTime()); } else Game.DoSkipFrame = false; // Sound SoundSystem.Execute(); // Gamepad if (pGamePadControl) pGamePadControl->Execute(); break; } } --iRecursionCount; }
void BeCheckersWindow::MessageReceived(BMessage *message) { switch(message->what) { case B_ABOUT_REQUESTED: AboutRequested(); break; case BECHECKERS_NEW: if(openedFile != NULL) openedFile = NULL; // I think there is a small mem leak here?? StartGame(); break; case BECHECKERS_OPEN: { // This is a cludge, because I was too lazy to create another class, but it works ;) // Create the open saved game window here BRect frame(Frame()); // The bounds are (0,0,350,125), I hate magic numbers too but oh well. BeCheckers is full of em! openWindow = new BWindow(BRect(frame.left+48, frame.top+40, frame.left+398, frame.top+165), "Saved Games", B_FLOATING_WINDOW_LOOK,B_MODAL_APP_WINDOW_FEEL, B_NOT_RESIZABLE | B_NOT_ZOOMABLE); BView *v = new BView(BRect(0, 0, 350, 125), "v", B_NOT_RESIZABLE, B_WILL_DRAW); v->SetViewColor(216, 216, 216); BButton *b = new BButton(BRect(10, 16, 70, 41), "Open", "Open", new BMessage(BECHECKERS_OPEN_OPEN)); b->MakeDefault(true); b->SetTarget(this); v->AddChild(b); BButton *b1 = new BButton(BRect(10, 51, 70, 76), "Delete", "Delete", new BMessage(BECHECKERS_OPEN_DELETE)); b1->SetTarget(this); v->AddChild(b1); BButton *b2 = new BButton(BRect(10, 86, 70, 111), "Close", "Close", new BMessage(BECHECKERS_OPEN_CLOSE)); b2->SetTarget(this); v->AddChild(b2); savedGameList = new BListView(BRect(95, 15, 320, 108), "City"); SavedGames(savedGameList); // Get the saved games from disk BScrollView *sv = new BScrollView("ScrollView", savedGameList,B_FOLLOW_LEFT|B_FOLLOW_TOP, 0, false, true, B_FANCY_BORDER); v->AddChild(sv); openWindow->AddChild(v); openWindow->Show(); } break; case BECHECKERS_OPEN_OPEN: { int index = savedGameList->CurrentSelection(); if(index >= 0) { // Set seleted file as openedFile and open it openedFile = File(((BStringItem *) savedGameList->ItemAt(index))->Text()); OpenGame(openedFile); openWindow->Lock(); openWindow->Quit(); } else (new BAlert("NoFile", "A saved game must be selected before opening.", "Okay"))->Go(); } break; case BECHECKERS_OPEN_CLOSE: openWindow->Lock(); openWindow->Quit(); break; case BECHECKERS_OPEN_DELETE: { int index = savedGameList->CurrentSelection(); if(index >= 0) { // If the file being deleted was the last file opened then set openedFile to (null) if(openedFile != NULL && strcmp(File(((BStringItem *) savedGameList->ItemAt(index))->Text()), openedFile) == 0) openedFile = NULL; // Delete saved game from harddisk BEntry entry(File(((BStringItem *) savedGameList->ItemAt(index))->Text())); entry.Remove(); // Delete the filename from the BListView openWindow->Lock(); savedGameList->RemoveItem(index); openWindow->Unlock(); } else (new BAlert("NoFile", "A saved game must be selected before deleting.", "Okay"))->Go(); } break; case BECHECKERS_SAVE: if(openedFile != NULL) SaveGame(openedFile); else this->PostMessage(BECHECKERS_SAVEAS); break; case BECHECKERS_SAVEAS: // Set the new file as the last openedFile and save to harddisk openedFile = File(CreateFileName()); SaveGame(openedFile); break; case CHECKER_WIN: bool color; message->FindBool("Color", &color); mvIndicationLabel->SetText(color == DARK ? "Dark won" : "Light won"); if((new BAlert("Winner", color == DARK ? "The dark colored checkers won the game!" : "The light colored checkers won the game!", "Quit", "Play again"))->Go() == 0) be_app->PostMessage(B_QUIT_REQUESTED); else this->PostMessage(BECHECKERS_NEW); break; case CHECKER_TURN: bool turn; message->FindBool("Turn", &turn); mvIndicationLabel->SetText(turn == DARK ? "Dark to move" : "Light to move"); break; default: BWindow::MessageReceived(message); break; } }