diff options
Diffstat (limited to 'kue/main.cpp')
-rw-r--r-- | kue/main.cpp | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/kue/main.cpp b/kue/main.cpp new file mode 100644 index 00000000..f6779afe --- /dev/null +++ b/kue/main.cpp @@ -0,0 +1,218 @@ +#include <stdio.h> +#include <time.h> +#include <stdlib.h> +#include <tdeapplication.h> +#include <tqtimer.h> +#include <tdelocale.h> +#include <tdecmdlineargs.h> +#include <tdeaboutdata.h> +#include <tdeconfig.h> +#include <tdeglobal.h> +#include <tdemenubar.h> +#include <tdemessagebox.h> +#include <tdepopupmenu.h> +#include <kstdaction.h> +#include <tdeaction.h> +#include <kstatusbar.h> +#include <kstdgameaction.h> +#include <klibloader.h> +#include <kdebug.h> + +#include "newgame.h" +#include "main.h" +#include "graphics.h" +#include "physics.h" +#include "rules.h" +#include "interface.h" +#include "localplayer.h" +#include "global.h" +#include "config.h" + +MainWindow::MainWindow() +{ + // We have a statusbar, show it now + statusBar(); + + // Make a game menu + TDEPopupMenu *game_menu = new TDEPopupMenu(this); + _new_action = KStdGameAction::gameNew(this, TQ_SLOT(newGame()), actionCollection()); + _new_action->plug(game_menu); + _end_action = KStdGameAction::end(this, TQ_SLOT(endGame()), actionCollection()); + _end_action->plug(game_menu); + TDEAction *quit_action = KStdGameAction::quit(this, TQ_SLOT(close()), actionCollection()); + quit_action->plug(game_menu); + menuBar()->insertItem(i18n("&Game"), game_menu); + + // Make a settings menu + TDEPopupMenu *settings_menu = new TDEPopupMenu(this); + TDEAction *menubar_action = KStdAction::showMenubar(this, TQ_SLOT(toggleMenubar()), actionCollection()); + menubar_action->plug(settings_menu); + TDEAction *statusbar_action = KStdAction::showStatusbar(this, TQ_SLOT(toggleStatusbar()), actionCollection()); + statusbar_action->plug(settings_menu); + menuBar()->insertItem(i18n("&Settings"), settings_menu); + + // Make a help menu + TDEPopupMenu *help_menu = helpMenu(); + menuBar()->insertItem(i18n("&Help"), help_menu); + + // Restore our window size + TDEGlobal::config()->setGroup("Window Settings"); + restoreWindowSize(TDEGlobal::config()); + + _in_game = false; + _end_action->setEnabled(false); +} + +MainWindow::~MainWindow() +{ + TDEGlobal::config()->setGroup("Window Settings"); + saveWindowSize(TDEGlobal::config()); +} + +void MainWindow::toggleMenubar() +{ + if (!menuBar()->isHidden()) + menuBar()->hide(); + else + menuBar()->show(); +} + +void MainWindow::toggleStatusbar() +{ + if (!statusBar()->isHidden()) + statusBar()->hide(); + else + statusBar()->show(); +} + +void MainWindow::newGame() +{ + // Show the "New Game" dialog + newGameDialog *new_dialog = new newGameDialog(this, "newgame"); + if (new_dialog->exec() == TQDialog::Accepted) { + // Reset our state + if (_in_game) + endGame(); + + // Recreate our basic objects + KueGlobal::_physics = new KuePhysics; + KueGlobal::_glWidget = new GLUserInterface(this); + setCentralWidget(KueGlobal::_glWidget); + + // Set up the teams + TQValueList<KueTeam*> selectedTeams = new_dialog->selectedTeams(); + + KueGlobal::teams()->resize(selectedTeams.count()); + for (unsigned int i = 0;i < KueGlobal::teams()->size(); i++) + { + KueGlobal::teams()->insert(i, selectedTeams.first()); + selectedTeams.pop_front(); + } + + // Load the plugin the user requested + KLibFactory *factory = KLibLoader::self()->factory(new_dialog->selectedPlugin().filename.local8Bit()); + + // Do we even have an object factory? + if (!factory) { + kdWarning() << "Unable to retrieve KLibFactory for " << new_dialog->selectedPlugin().filename << endl; + delete new_dialog; + return; + } + + // Actually request an object of type "KueKueGlobal::rules()" + TQObject *rules_object = factory->create(this, "KueGlobal::rules()", "KueRulesEngine"); + + // Did they return something at all? + if (!rules_object) { + kdWarning() << "Plugin unable to provide a KueRulesEngine" << endl; + delete new_dialog; + return; + } + + // Is the object -actually- a KueRulesEngine? + // Some broken plugins may not check their object type parameter, so this is a sanity check + if (!rules_object->inherits("KueRulesEngine")) { + kdWarning() << "Plugin returned an object of an unexpected type" << endl; + + delete rules_object; + delete new_dialog; + + return; + } + + // It checked out, set our KueGlobal::rules() to this object + KueGlobal::_rules = (KueRulesEngine*)rules_object; + + connect(KueGlobal::physics(), TQ_SIGNAL(billiardHit(unsigned int, unsigned int)), KueGlobal::rules(), TQ_SLOT(billiardHit(unsigned int, unsigned int))); + connect(KueGlobal::physics(), TQ_SIGNAL(billiardSunk(unsigned int, unsigned int)), KueGlobal::rules(), TQ_SLOT(billiardSunk(unsigned int, unsigned int))); + connect(KueGlobal::physics(), TQ_SIGNAL(motionStopped()), KueGlobal::rules(), TQ_SLOT(motionStopped())); + + connect(KueGlobal::rules(), TQ_SIGNAL(showMessage(const TQString &)), KueGlobal::mainWindow()->statusBar(), TQ_SLOT(message(const TQString &))); + connect(KueGlobal::rules(), TQ_SIGNAL(gameOver(const TQString &)), this, TQ_SLOT(endGame(const TQString &))); + + _in_game = true; + _end_action->setEnabled(true); + + KueGlobal::rules()->start(); + KueGlobal::glWidget()->show(); + } + + delete new_dialog; +} + +void MainWindow::endGame() +{ + delete KueGlobal::_rules; + delete KueGlobal::_physics; + delete KueGlobal::_glWidget; + + KueGlobal::_rules = nullptr; + KueGlobal::_physics = nullptr; + KueGlobal::_glWidget = nullptr; + + statusBar()->message(TQString::null); + _in_game = false; + _end_action->setEnabled(false); +} + +void MainWindow::endGame(const TQString &reason) +{ + // Stop the physics engine + KueGlobal::physics()->stop(); + + // Notify the user + KMessageBox::information(this, reason, i18n("Game Over")); + + // We do this delayed, because most modules don't (and can't) call this + // at a place where they are ready to have the entire game state + // destroyed + TQTimer::singleShot(0, this, TQ_SLOT(endGame())); +} + +MainWindow* MainWindow::the() +{ + if (!KueGlobal::_mainWindow) + KueGlobal::_mainWindow = new MainWindow; + return KueGlobal::_mainWindow; +} + +// Program starts here +int main(int argc, char *argv[]) +{ + TDEAboutData aboutData("kue", I18N_NOOP("Kue"), VERSION, + I18N_NOOP("A simple billiards game"), + TDEAboutData::License_GPL, + I18N_NOOP("(c) 2002 Ryan Cumming")); + + aboutData.addAuthor("Ryan Cumming", 0, "<bodnar42@phalynx.dhs.org>"); + TDECmdLineArgs::init(argc, argv, &aboutData); + + TDEApplication a; + TDEGlobal::locale()->insertCatalogue("libtdegames"); + TDEGlobal::locale()->insertCatalogue("libkdehighscores"); + + MainWindow::the()->show(); + return a.exec(); +} + +#include "main.moc" |