summaryrefslogtreecommitdiffstats
path: root/kue/main.cpp
blob: f6779afee1f14348e924c632643e0f27e4f4cc97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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"