summaryrefslogtreecommitdiff
path: root/noncore/games/minesweep/minesweep.cpp
Unidiff
Diffstat (limited to 'noncore/games/minesweep/minesweep.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/minesweep/minesweep.cpp5
1 files changed, 3 insertions, 2 deletions
diff --git a/noncore/games/minesweep/minesweep.cpp b/noncore/games/minesweep/minesweep.cpp
index c14609d..18449ff 100644
--- a/noncore/games/minesweep/minesweep.cpp
+++ b/noncore/games/minesweep/minesweep.cpp
@@ -1,408 +1,409 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of the Qtopia Environment. 4** This file is part of the Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20 20
21#include "minesweep.h" 21#include "minesweep.h"
22#include "minefield.h" 22#include "minefield.h"
23 23
24#include <opie2/oresource.h>
25
24#include <qtopia/qpeapplication.h> 26#include <qtopia/qpeapplication.h>
25#include <qtopia/resource.h>
26#include <qtopia/config.h> 27#include <qtopia/config.h>
27 28
28#include <qtoolbar.h> 29#include <qtoolbar.h>
29#include <qmenubar.h> 30#include <qmenubar.h>
30#include <qpushbutton.h> 31#include <qpushbutton.h>
31#include <qlcdnumber.h> 32#include <qlcdnumber.h>
32#include <qtimer.h> 33#include <qtimer.h>
33#include <qlabel.h> 34#include <qlabel.h>
34 35
35#include <stdlib.h> 36#include <stdlib.h>
36#include <time.h> 37#include <time.h>
37 38
38 39
39 40
40 41
41static const char *pix_new[]={ 42static const char *pix_new[]={
42"20 20 3 1", 43"20 20 3 1",
43" c None", 44" c None",
44"# c #00FF00", 45"# c #00FF00",
45". c #000000", 46". c #000000",
46" ", 47" ",
47" ...... ", 48" ...... ",
48" ..######.. ", 49" ..######.. ",
49" .##########. ", 50" .##########. ",
50" .############. ", 51" .############. ",
51" .##############. ", 52" .##############. ",
52" .##############. ", 53" .##############. ",
53" .################. ", 54" .################. ",
54" .################. ", 55" .################. ",
55" .################. ", 56" .################. ",
56" .################. ", 57" .################. ",
57" .################. ", 58" .################. ",
58" .################. ", 59" .################. ",
59" .##############. ", 60" .##############. ",
60" .##############. ", 61" .##############. ",
61" .############. ", 62" .############. ",
62" .##########. ", 63" .##########. ",
63" ..######.. ", 64" ..######.. ",
64" ...... ", 65" ...... ",
65" "}; 66" "};
66 67
67 68
68/* XPM */ 69/* XPM */
69static const char * happy_xpm[] = { 70static const char * happy_xpm[] = {
70"20 20 3 1", 71"20 20 3 1",
71 " c None", 72 " c None",
72 ".c #ffff3f ", 73 ".c #ffff3f ",
73 "#c #000000", 74 "#c #000000",
74" ", 75" ",
75" ###### ", 76" ###### ",
76" ##......## ", 77" ##......## ",
77" #..........# ", 78" #..........# ",
78" #............# ", 79" #............# ",
79" #..............# ", 80" #..............# ",
80" #..............# ", 81" #..............# ",
81" #....##....##....# ", 82" #....##....##....# ",
82" #....##....##....# ", 83" #....##....##....# ",
83" #................# ", 84" #................# ",
84" #................# ", 85" #................# ",
85" #................# ", 86" #................# ",
86" #...#........#...# ", 87" #...#........#...# ",
87" #.##........##.# ", 88" #.##........##.# ",
88" #...########...# ", 89" #...########...# ",
89" #...######...# ", 90" #...######...# ",
90" #..........# ", 91" #..........# ",
91" ##......## ", 92" ##......## ",
92" ###### ", 93" ###### ",
93" "}; 94" "};
94 95
95 96
96/* XPM */ 97/* XPM */
97static const char * worried_xpm[] = { 98static const char * worried_xpm[] = {
98"20 20 3 1", 99"20 20 3 1",
99 " c None", 100 " c None",
100 ".c #ffff3f", 101 ".c #ffff3f",
101 "#c #000000", 102 "#c #000000",
102" ", 103" ",
103" ###### ", 104" ###### ",
104" ##......## ", 105" ##......## ",
105" #..........# ", 106" #..........# ",
106" #............# ", 107" #............# ",
107" #..............# ", 108" #..............# ",
108" #..............# ", 109" #..............# ",
109" #....##....##....# ", 110" #....##....##....# ",
110" #....##....##....# ", 111" #....##....##....# ",
111" #................# ", 112" #................# ",
112" #................# ", 113" #................# ",
113" #................# ", 114" #................# ",
114" #................# ", 115" #................# ",
115" #....######....# ", 116" #....######....# ",
116" #..............# ", 117" #..............# ",
117" #............# ", 118" #............# ",
118" #..........# ", 119" #..........# ",
119" ##......## ", 120" ##......## ",
120" ###### ", 121" ###### ",
121" "}; 122" "};
122 123
123 124
124/* XPM */ 125/* XPM */
125static const char * dead_xpm[] = { 126static const char * dead_xpm[] = {
126"20 20 3 1", 127"20 20 3 1",
127 " c None", 128 " c None",
128 ".c #ffff3f", 129 ".c #ffff3f",
129 "#c #000000", 130 "#c #000000",
130" ", 131" ",
131" ###### ", 132" ###### ",
132" ##......## ", 133" ##......## ",
133" #..........# ", 134" #..........# ",
134" #............# ", 135" #............# ",
135" #..............# ", 136" #..............# ",
136" #..#.#...#.#...# ", 137" #..#.#...#.#...# ",
137" #....#.....#.....# ", 138" #....#.....#.....# ",
138" #...#.#...#.#....# ", 139" #...#.#...#.#....# ",
139" #................# ", 140" #................# ",
140" #................# ", 141" #................# ",
141" #................# ", 142" #................# ",
142" #......####......# ", 143" #......####......# ",
143" #....# #....# ", 144" #....# #....# ",
144" #...#......#...# ", 145" #...#......#...# ",
145" #............# ", 146" #............# ",
146" #..........# ", 147" #..........# ",
147" ##......## ", 148" ##......## ",
148" ###### ", 149" ###### ",
149" "}; 150" "};
150 151
151 152
152class ResultIndicator : private QLabel 153class ResultIndicator : private QLabel
153{ 154{
154public: 155public:
155 static void showResult( QWidget *ref, bool won ); 156 static void showResult( QWidget *ref, bool won );
156private: 157private:
157 ResultIndicator( QWidget *parent, const char *name, WFlags f) 158 ResultIndicator( QWidget *parent, const char *name, WFlags f)
158 :QLabel( parent, name, f ) {} 159 :QLabel( parent, name, f ) {}
159 160
160 void timerEvent( QTimerEvent *); 161 void timerEvent( QTimerEvent *);
161 void center(); 162 void center();
162 bool twoStage; 163 bool twoStage;
163 int timerId; 164 int timerId;
164}; 165};
165 166
166void ResultIndicator::showResult( QWidget *ref, bool won ) 167void ResultIndicator::showResult( QWidget *ref, bool won )
167{ 168{
168 ResultIndicator *r = new ResultIndicator( ref, 0, WStyle_Customize | WStyle_Tool | WType_TopLevel ); 169 ResultIndicator *r = new ResultIndicator( ref, 0, WStyle_Customize | WStyle_Tool | WType_TopLevel );
169 170
170 r->setAlignment( AlignCenter ); 171 r->setAlignment( AlignCenter );
171 r->setFrameStyle( Sunken|StyledPanel ); 172 r->setFrameStyle( Sunken|StyledPanel );
172 if ( won ) { 173 if ( won ) {
173 r->setText( MineSweep::tr("You won!") ); 174 r->setText( MineSweep::tr("You won!") );
174 r->center(); 175 r->center();
175 r->show(); 176 r->show();
176 r->twoStage = FALSE; 177 r->twoStage = FALSE;
177 r->timerId = r->startTimer(1500); 178 r->timerId = r->startTimer(1500);
178 } else { 179 } else {
179 QPalette p( red ); 180 QPalette p( red );
180 r->setPalette( p ); 181 r->setPalette( p );
181 r->setText( MineSweep::tr("You exploded!") ); 182 r->setText( MineSweep::tr("You exploded!") );
182 r->resize( ref->size() ); 183 r->resize( ref->size() );
183 r->move( ref->mapToGlobal(QPoint(0,0)) ); 184 r->move( ref->mapToGlobal(QPoint(0,0)) );
184 r->show(); 185 r->show();
185 r->twoStage = TRUE; 186 r->twoStage = TRUE;
186 r->timerId =r->startTimer(200); 187 r->timerId =r->startTimer(200);
187 } 188 }
188} 189}
189 190
190void ResultIndicator::center() 191void ResultIndicator::center()
191{ 192{
192 QWidget *w = parentWidget(); 193 QWidget *w = parentWidget();
193 194
194 QPoint pp = w->mapToGlobal( QPoint(0,0) ); 195 QPoint pp = w->mapToGlobal( QPoint(0,0) );
195 QSize s = sizeHint()*3; 196 QSize s = sizeHint()*3;
196 s.setWidth( QMIN(s.width(), w->width()) ); 197 s.setWidth( QMIN(s.width(), w->width()) );
197 pp = QPoint( pp.x() + w->width()/2 - s.width()/2, 198 pp = QPoint( pp.x() + w->width()/2 - s.width()/2,
198 pp.y() + w->height()/ 2 - s.height()/2 ); 199 pp.y() + w->height()/ 2 - s.height()/2 );
199 200
200 setGeometry( QRect(pp, s) ); 201 setGeometry( QRect(pp, s) );
201 202
202} 203}
203 204
204void ResultIndicator::timerEvent( QTimerEvent *te ) 205void ResultIndicator::timerEvent( QTimerEvent *te )
205{ 206{
206 if ( te->timerId() != timerId ) 207 if ( te->timerId() != timerId )
207 return; 208 return;
208 killTimer( timerId ); 209 killTimer( timerId );
209 if ( twoStage ) { 210 if ( twoStage ) {
210 center(); 211 center();
211 twoStage = FALSE; 212 twoStage = FALSE;
212 timerId = startTimer( 1000 ); 213 timerId = startTimer( 1000 );
213 } else { 214 } else {
214 delete this; 215 delete this;
215 } 216 }
216} 217}
217 218
218 219
219class MineFrame : public QFrame 220class MineFrame : public QFrame
220{ 221{
221public: 222public:
222 MineFrame( QWidget *parent, const char *name = 0 ) 223 MineFrame( QWidget *parent, const char *name = 0 )
223 :QFrame( parent, name ), field(0) {} 224 :QFrame( parent, name ), field(0) {}
224 void setField( MineField *f ) { 225 void setField( MineField *f ) {
225 field = f; 226 field = f;
226 setMinimumSize( field->sizeHint() ); 227 setMinimumSize( field->sizeHint() );
227 } 228 }
228protected: 229protected:
229 void resizeEvent( QResizeEvent *e ) { 230 void resizeEvent( QResizeEvent *e ) {
230 field->setAvailableRect( contentsRect()); 231 field->setAvailableRect( contentsRect());
231 QFrame::resizeEvent(e); 232 QFrame::resizeEvent(e);
232 } 233 }
233private: 234private:
234 MineField *field; 235 MineField *field;
235}; 236};
236 237
237 238
238 239
239MineSweep::MineSweep( QWidget* parent, const char* name, WFlags f ) 240MineSweep::MineSweep( QWidget* parent, const char* name, WFlags f )
240: QMainWindow( parent, name, f ) 241: QMainWindow( parent, name, f )
241{ 242{
242 srand(::time(0)); 243 srand(::time(0));
243 setCaption( tr("Mine Sweep") ); 244 setCaption( tr("Mine Sweep") );
244 QPEApplication::setInputMethodHint(this, QPEApplication::AlwaysOff ); 245 QPEApplication::setInputMethodHint(this, QPEApplication::AlwaysOff );
245 setIcon( Resource::loadPixmap( "minesweep/MineSweep" ) ); 246 setIcon( Opie::Core::OResource::loadPixmap( "minesweep/MineSweep" ) );
246 247
247 QToolBar *toolBar = new QToolBar( this ); 248 QToolBar *toolBar = new QToolBar( this );
248 toolBar->setHorizontalStretchable( TRUE ); 249 toolBar->setHorizontalStretchable( TRUE );
249 250
250 QMenuBar *menuBar = new QMenuBar( toolBar ); 251 QMenuBar *menuBar = new QMenuBar( toolBar );
251 252
252 QPopupMenu *gameMenu = new QPopupMenu( this ); 253 QPopupMenu *gameMenu = new QPopupMenu( this );
253 gameMenu->insertItem( tr("Beginner"), this, SLOT( beginner() ) ); 254 gameMenu->insertItem( tr("Beginner"), this, SLOT( beginner() ) );
254 gameMenu->insertItem( tr("Advanced"), this, SLOT( advanced() ) ); 255 gameMenu->insertItem( tr("Advanced"), this, SLOT( advanced() ) );
255 256
256 if (qApp->desktop()->width() >= 240) { 257 if (qApp->desktop()->width() >= 240) {
257 gameMenu->insertItem( tr("Expert"), this, SLOT( expert() ) ); 258 gameMenu->insertItem( tr("Expert"), this, SLOT( expert() ) );
258 } 259 }
259 260
260 menuBar->insertItem( tr("Game"), gameMenu ); 261 menuBar->insertItem( tr("Game"), gameMenu );
261 262
262 guessLCD = new QLCDNumber( toolBar ); 263 guessLCD = new QLCDNumber( toolBar );
263 toolBar->setStretchableWidget( guessLCD ); 264 toolBar->setStretchableWidget( guessLCD );
264 265
265 QPalette lcdPal( red ); 266 QPalette lcdPal( red );
266 lcdPal.setColor( QColorGroup::Background, QApplication::palette().active().background() ); 267 lcdPal.setColor( QColorGroup::Background, QApplication::palette().active().background() );
267 lcdPal.setColor( QColorGroup::Button, QApplication::palette().active().button() ); 268 lcdPal.setColor( QColorGroup::Button, QApplication::palette().active().button() );
268 269
269// guessLCD->setPalette( lcdPal ); 270// guessLCD->setPalette( lcdPal );
270 guessLCD->setSegmentStyle( QLCDNumber::Flat ); 271 guessLCD->setSegmentStyle( QLCDNumber::Flat );
271 guessLCD->setFrameStyle( QFrame::NoFrame ); 272 guessLCD->setFrameStyle( QFrame::NoFrame );
272 guessLCD->setNumDigits( 2 ); 273 guessLCD->setNumDigits( 2 );
273 guessLCD->setBackgroundMode( PaletteButton ); 274 guessLCD->setBackgroundMode( PaletteButton );
274 newGameButton = new QPushButton( toolBar ); 275 newGameButton = new QPushButton( toolBar );
275 newGameButton->setPixmap( QPixmap( pix_new ) ); 276 newGameButton->setPixmap( QPixmap( pix_new ) );
276 newGameButton->setFocusPolicy(QWidget::NoFocus); 277 newGameButton->setFocusPolicy(QWidget::NoFocus);
277 connect( newGameButton, SIGNAL(clicked()), this, SLOT(newGame()) ); 278 connect( newGameButton, SIGNAL(clicked()), this, SLOT(newGame()) );
278 279
279 timeLCD = new QLCDNumber( toolBar ); 280 timeLCD = new QLCDNumber( toolBar );
280// timeLCD->setPalette( lcdPal ); 281// timeLCD->setPalette( lcdPal );
281 timeLCD->setSegmentStyle( QLCDNumber::Flat ); 282 timeLCD->setSegmentStyle( QLCDNumber::Flat );
282 timeLCD->setFrameStyle( QFrame::NoFrame ); 283 timeLCD->setFrameStyle( QFrame::NoFrame );
283 timeLCD->setNumDigits( 5 ); // "mm:ss" 284 timeLCD->setNumDigits( 5 ); // "mm:ss"
284 timeLCD->setBackgroundMode( PaletteButton ); 285 timeLCD->setBackgroundMode( PaletteButton );
285 286
286 setToolBarsMovable ( FALSE ); 287 setToolBarsMovable ( FALSE );
287 288
288 addToolBar( toolBar ); 289 addToolBar( toolBar );
289 290
290 MineFrame *mainframe = new MineFrame( this ); 291 MineFrame *mainframe = new MineFrame( this );
291 mainframe->setFrameShape( QFrame::Box ); 292 mainframe->setFrameShape( QFrame::Box );
292 mainframe->setFrameShadow( QFrame::Raised ); 293 mainframe->setFrameShadow( QFrame::Raised );
293 294
294 mainframe->setLineWidth(2); 295 mainframe->setLineWidth(2);
295 296
296 field = new MineField( mainframe ); 297 field = new MineField( mainframe );
297 mainframe->setField( field ); 298 mainframe->setField( field );
298 QFont fnt = field->font(); 299 QFont fnt = field->font();
299 fnt.setBold( TRUE ); 300 fnt.setBold( TRUE );
300 field->setFont( QFont( fnt ) ); 301 field->setFont( QFont( fnt ) );
301 field->setFocus(); 302 field->setFocus();
302 setCentralWidget( mainframe ); 303 setCentralWidget( mainframe );
303 304
304 connect( field, SIGNAL( gameOver(bool) ), this, SLOT( gameOver(bool) ) ); 305 connect( field, SIGNAL( gameOver(bool) ), this, SLOT( gameOver(bool) ) );
305 connect( field, SIGNAL( mineCount(int) ), this, SLOT( setCounter(int) ) ); 306 connect( field, SIGNAL( mineCount(int) ), this, SLOT( setCounter(int) ) );
306 connect( field, SIGNAL( gameStarted()), this, SLOT( startPlaying() ) ); 307 connect( field, SIGNAL( gameStarted()), this, SLOT( startPlaying() ) );
307 308
308 timer = new QTimer( this ); 309 timer = new QTimer( this );
309 connect( timer, SIGNAL( timeout() ), this, SLOT( updateTime() ) ); 310 connect( timer, SIGNAL( timeout() ), this, SLOT( updateTime() ) );
310 311
311 readConfig(); 312 readConfig();
312} 313}
313 314
314MineSweep::~MineSweep() 315MineSweep::~MineSweep()
315{ 316{
316 writeConfig(); 317 writeConfig();
317} 318}
318 319
319void MineSweep::gameOver( bool won ) 320void MineSweep::gameOver( bool won )
320{ 321{
321 field->showMines(); 322 field->showMines();
322 if ( won ) { 323 if ( won ) {
323 newGameButton->setPixmap( QPixmap( happy_xpm ) ); 324 newGameButton->setPixmap( QPixmap( happy_xpm ) );
324 } else { 325 } else {
325 newGameButton->setPixmap( QPixmap( dead_xpm ) ); 326 newGameButton->setPixmap( QPixmap( dead_xpm ) );
326 } 327 }
327 ResultIndicator::showResult( this, won ); 328 ResultIndicator::showResult( this, won );
328 timer->stop(); 329 timer->stop();
329} 330}
330 331
331void MineSweep::newGame() 332void MineSweep::newGame()
332{ 333{
333 newGame(field->level()); 334 newGame(field->level());
334} 335}
335 336
336void MineSweep::newGame(int level) 337void MineSweep::newGame(int level)
337{ 338{
338 timeLCD->display( "0:00" ); 339 timeLCD->display( "0:00" );
339 field->setup( level ); 340 field->setup( level );
340 newGameButton->setPixmap( QPixmap( pix_new ) ); 341 newGameButton->setPixmap( QPixmap( pix_new ) );
341 timer->stop(); 342 timer->stop();
342} 343}
343 344
344void MineSweep::startPlaying() 345void MineSweep::startPlaying()
345{ 346{
346 newGameButton->setPixmap( QPixmap( worried_xpm ) ); 347 newGameButton->setPixmap( QPixmap( worried_xpm ) );
347 starttime = QDateTime::currentDateTime(); 348 starttime = QDateTime::currentDateTime();
348 timer->start( 1000 ); 349 timer->start( 1000 );
349} 350}
350 351
351void MineSweep::beginner() 352void MineSweep::beginner()
352{ 353{
353 newGame(1); 354 newGame(1);
354} 355}
355 356
356void MineSweep::advanced() 357void MineSweep::advanced()
357{ 358{
358 newGame(2); 359 newGame(2);
359} 360}
360 361
361void MineSweep::expert() 362void MineSweep::expert()
362{ 363{
363 newGame(3); 364 newGame(3);
364} 365}
365 366
366void MineSweep::setCounter( int c ) 367void MineSweep::setCounter( int c )
367{ 368{
368 if ( !guessLCD ) 369 if ( !guessLCD )
369 return; 370 return;
370 371
371 guessLCD->display( c ); 372 guessLCD->display( c );
372} 373}
373 374
374void MineSweep::updateTime() 375void MineSweep::updateTime()
375{ 376{
376 if ( !timeLCD ) 377 if ( !timeLCD )
377 return; 378 return;
378 379
379 int s = starttime.secsTo(QDateTime::currentDateTime()); 380 int s = starttime.secsTo(QDateTime::currentDateTime());
380 if ( s/60 > 99 ) 381 if ( s/60 > 99 )
381 timeLCD->display( "-----" ); 382 timeLCD->display( "-----" );
382 else 383 else
383 timeLCD->display( QString().sprintf("%2d:%02d",s/60,s%60) ); 384 timeLCD->display( QString().sprintf("%2d:%02d",s/60,s%60) );
384} 385}
385 386
386void MineSweep::writeConfig() const 387void MineSweep::writeConfig() const
387{ 388{
388 Config cfg("MineSweep"); 389 Config cfg("MineSweep");
389 cfg.setGroup("Panel"); 390 cfg.setGroup("Panel");
390 cfg.writeEntry("Time", 391 cfg.writeEntry("Time",
391 timer->isActive() ? starttime.secsTo(QDateTime::currentDateTime()) : -1); 392 timer->isActive() ? starttime.secsTo(QDateTime::currentDateTime()) : -1);
392 field->writeConfig(cfg); 393 field->writeConfig(cfg);
393} 394}
394 395
395void MineSweep::readConfig() 396void MineSweep::readConfig()
396{ 397{
397 Config cfg("MineSweep"); 398 Config cfg("MineSweep");
398 field->readConfig(cfg); 399 field->readConfig(cfg);
399 cfg.setGroup("Panel"); 400 cfg.setGroup("Panel");
400 int s = cfg.readNumEntry("Time",-1); 401 int s = cfg.readNumEntry("Time",-1);
401 if ( s<0 ) { 402 if ( s<0 ) {
402 newGame(); 403 newGame();
403 } else { 404 } else {
404 startPlaying(); 405 startPlaying();
405 starttime = QDateTime::currentDateTime().addSecs(-s); 406 starttime = QDateTime::currentDateTime().addSecs(-s);
406 updateTime(); 407 updateTime();
407 } 408 }
408} 409}