cardcol.cpp
来自「一个扑克牌游戏集合的源码,包含了很多基本c-c++语言应用」· C++ 代码 · 共 1,435 行 · 第 1/4 页
CPP
1,435 行
YGP::INIFile::writeSectionHeader (inifile, "SgtMayor"); inifile << "Tricks=" << SgtMayor::ENDTRICKS << '\n';#endif inifile << '\n'; } else { Glib::ustring msg (_("Couldn't save options (to file %1)!\n\nReason: %2.")); msg.replace (msg.find ("%1"), 2, options.pNameINIFile); msg.replace (msg.find ("%2"), 2, strerror (errno)); Gtk::MessageDialog (msg, false, Gtk::MESSAGE_ERROR).run (); }}//-----------------------------------------------------------------------------/// Saves the settings//-----------------------------------------------------------------------------void CardgameCollection::exit () { if (game) { if (game->isRunning ()) { Gtk::MessageDialog dlg (_("A game is running. Do you really want to quit?"), false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO); dlg.set_title (PACKAGE); if (dlg.run () == Gtk::RESPONSE_YES) { if (game->canBeStopped ()) Glib::signal_idle ().connect (mem_fun (*this, &CardgameCollection::terminateGameAndExit)); else { Glib::signal_idle ().connect (mem_fun (*this, &CardgameCollection::wait4EndGameAndExit)); return; } } else return; } } hide ();}//-----------------------------------------------------------------------------/// Stops the game and quits application/// \returns bool: Always false//-----------------------------------------------------------------------------bool CardgameCollection::terminateGameAndExit () { Check2 (game); game->stop (); return false;}//-----------------------------------------------------------------------------/// Waits til the game can be ended; stops it and quits application/// \returns bool: Always false//-----------------------------------------------------------------------------bool CardgameCollection::wait4EndGameAndExit () { Check2 (game); restart = -1U; game->end (false); return false;}#if TRACELEVEL >= 0//-----------------------------------------------------------------------------/// Toggles showing/hiding the cards//-----------------------------------------------------------------------------void CardgameCollection::toggleDebug () { static bool open = false; open = !open; if (game) game->playOpen (open);}#endif//-----------------------------------------------------------------------------/// Returns the name of the file to display in the help/// \returns \c Name of file to display//-----------------------------------------------------------------------------const char* CardgameCollection::getHelpfile () { std::string file (options.helpPath); if (file[file.size () - 1] != YGP::File::DIRSEPARATOR) file += YGP::File::DIRSEPARATOR; if (game) { std::string name (game->name ()); unsigned int pos; while ((pos = name.find (" ")) != std::string::npos) name.replace (pos, 1, 0, '\0'); file += name; file += ".html"; } else file += "CardCol.html"; return file.c_str ();}//-----------------------------------------------------------------------------/// Shows the about box for the program//-----------------------------------------------------------------------------void CardgameCollection::showAboutbox () { std::string ver (_("Copyright (C) 2002 - 2006 Markus Schwab" "\ne-mail: g17m0@lycos.com\n\nCompiled on %1 at %2")); ver.replace (ver.find ("%1"), 2, __DATE__); ver.replace (ver.find ("%2"), 2, __TIME__); XGP::XAbout* about (XGP::XAbout::create (ver, PACKAGE " V" VERSION)); about->setIconProgram (xpmGame); about->setIconAuthor (xpmAuthor); about->get_window ()->set_transient_for (get_window ());}//-----------------------------------------------------------------------------/// Callback to change the names of the playing people//-----------------------------------------------------------------------------void CardgameCollection::changePlayernames () { TRACE2 ("CardgameCollection::changePlayernames");#ifdef HAVE_LIBPTHREAD if (cmgr.getMode () != YGP::ConnectionMgr::NONE) broadcastNames ();#endif if (game) game->changeNames (aPlayer);}//-----------------------------------------------------------------------------/// Callback to change the carddecks/// \param deck: Deck to set/// \param back: Back of cards to set//-----------------------------------------------------------------------------void CardgameCollection::changeDecks (const std::string& deck, const std::string& back) { TRACE2 ("CardgameCollection::changeDecks (2x const std::string&)"); unsigned int option (0); if ((deck.size () && (deck != options.decks)) || !cardFaces.size ()) { option = 1; options.decks = deck; } if ((back.size () && (back != options.back)) || !cardFaces.hasBack ()) { option |= 2; options.back = back; } changeCards ((void*)option);}//-----------------------------------------------------------------------------/// Loads the cards (from xpm-files)/// \param opt: Actually a bit field! Option indicationg what to load/// \returns \c void*: Status; Not NULL when loading was OK, NULL otherwise//-----------------------------------------------------------------------------void* CardgameCollection::changeCards (void* opt) { TRACE2 ("CardgameCollection::changeCards (void*) - Option: " << opt); if (!opt) return this; // Cards need an realized (!) parent, so ensure that the window is already // shown Check3 (this->is_realized ()); TRACE3 ("CardgameCollection::changeCards (void*) - Use " << options.decks << " and " << options.back); void* rc (NULL); bool enable (false); try { if ((unsigned int)opt & 1) { cardFaces.loadDecks (options.decks); cards.getCards ().size () ? cards.update () : cards.addPacket (cardFaces); } if ((unsigned int)opt & 2) cardFaces.loadBack (options.back); enable = true; rc = this; } catch (Glib::ustring& e) { Glib::ustring msg ("Couldn't load the card images!\n\n" "Reason: %1"); msg.replace (msg.find ("%1"), 2, e); showMessage (msg); } Check3 (apMenus[NEW]); apMenus[NEW]->set_sensitive (enable);#ifdef HAVE_LIBPTHREAD Check3 (apMenus[CONNECT]); apMenus[CONNECT]->set_sensitive (enable);#endif return rc;}//----------------------------------------------------------------------------/// Shows an error from the communication thread/// \param msg: Received message to handle/// \returns bool: False/// \remarks msg wil be deleted at the end//----------------------------------------------------------------------------bool CardgameCollection::showMessage (const std::string msg) { Gtk::MessageDialog* dlg (new Gtk::MessageDialog (msg, false, Gtk::MESSAGE_ERROR)); dlg->set_title (PACKAGE); dlg->signal_response ().connect (bind (ptr_fun (&CardgameCollection::closeDialog), dlg)); dlg->show (); return false;}//-----------------------------------------------------------------------------/// Frees the passed dialog/// \param int: Response of dialog (ignored)/// \param dlg: Dialog to close additionally//-----------------------------------------------------------------------------void CardgameCollection::closeDialog (int, const Gtk::Dialog* dlg) { Check1 (dlg); delete dlg;}//-----------------------------------------------------------------------------/// Checks the user-input after asking if he wants to end the game; depending/// on the answer either stops or continues/// \returns bool: Flag, if the game has already been started//-----------------------------------------------------------------------------bool CardgameCollection::restartGame () { TRACE8 ("CardgameCollection::restartGame () - Restart: " << restart); Check1 (game); if (game->isRunning ()) { status.pop (); status.push (_("User canceled")); if (game->canBeStopped ()) { TRACE9 ("CardgameCollection::restartGame () - Game can be stopped"); game->stop (); if (restart) startGame (); } else { TRACE9 ("CardgameCollection::restartGame () - Delaying stop of game"); game->end ((actGame == oldGame) ? restart : false); return false; } } else { restart = false; startGame (); } return true;}//-----------------------------------------------------------------------------/// Loads the cards (from png-files)/// \remarks Cards need an realized (!) parent, so make somehow sure, that the/// window already exists//-----------------------------------------------------------------------------void CardgameCollection::loadCards () { Check3 (is_realized ()); status.push (_("Loading cardimages ...")); // This code needs the game-IDs in a sequence starting with 0! if (options.type >= GameTypes::LAST) options.type = 0; Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic (apMenus[STARTGAMES_MENU + options.type + 1])->set_active (); void* rc (changeCards ((void*)-1)); if (rc) { Check3 (apMenus[NEW]); apMenus[NEW]->set_sensitive (true);#ifdef HAVE_LIBPTHREAD Check3 (apMenus[CONNECT]); apMenus[CONNECT]->set_sensitive (true);#endif status.pop (); if (cmgr.getMode () != YGP::ConnectionMgr::CLIENT) { status.push (_("Start a new game with Ctrl+N (or Game -> New)")); } } Check3 (cardFaces.size ());}//-----------------------------------------------------------------------------/// Handling of game-events/// \param status: New status of game//-----------------------------------------------------------------------------void CardgameCollection::gameEvents (unsigned int status) { TRACE8 ("CardgameCollection::gameEvents (unsigned int) const - New status: " << status << "; Restart: " << restart); switch (status) { case Game::PLAYING: Check3 (apMenus[END]); apMenus[END]->set_sensitive (true);#ifdef HAVE_LIBPTHREAD Check3 (apMenus[CONNECT]); apMenus[CONNECT]->set_sensitive (false);#endif break; case Game::STOPPED: Check3 (apMenus[END]); apMenus[END]->set_sensitive (false);#ifdef HAVE_LIBPTHREAD Check3 (apMenus[CONNECT]); apMenus[CONNECT]->set_sensitive (true);#endif if (restart == 1) { // (Re)start the (new) game, when the event queue is empty (and // therefore the old game has ended). Glib::signal_idle ().connect (bind_return (mem_fun (*this, &CardgameCollection::doStartGame), false)); } else if (restart == -1U) Glib::signal_idle ().connect (bind_return (mem_fun (*this, &CardgameCollection::hide), false)); restart = false; break; }}//----------------------------------------------------------------------------/// Starts the game and unlocks a (locked) msg-handling mutex//----------------------------------------------------------------------------void CardgameCollection::doStartGame () { startGame ();#ifdef HAVE_LIBPTHREAD mxThreadCmd.unlock ();#endif}#ifdef WITH_ROVHULT//-----------------------------------------------------------------------------/// Checks if R鴙hult's special cards are valid; reset them if not//-----------------------------------------------------------------------------void CardgameCollection::checkRovhultSpecialCards () { CardWidget::NUMBERS* cards[] = { &Rovhult::cardNuke, &Rovhult::cardReverse, &Rovhult::cardSkip }; for (unsigned int i (0); i < (sizeof (cards) / sizeof (*cards) - 1); ++i) for (unsigned int j (i + 1); j < (sizeof (cards) / sizeof (*cards)); ++j) if (*cards[i] == *cards[j]) { Rovhult::cardNuke = CardWidget::TEN; Rovhult::cardReverse = CardWidget::SEVEN; Rovhult::cardSkip = CardWidget::EIGHT; Gtk::MessageDialog* dlg (new Gtk::MessageDialog (_("Invalid values for Rovhult's special cards!\n" "Resetting them to default values."), false, Gtk::MESSAGE_ERROR)); dlg->set_title (PACKAGE); dlg->signal_response ().connect (bind (ptr_fun (&CardgameCollection::closeDialog), dlg)); dlg->show (); return; }}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?