summaryrefslogtreecommitdiff
path: root/noncore/games/bounce/game.cpp
Unidiff
Diffstat (limited to 'noncore/games/bounce/game.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/bounce/game.cpp17
1 files changed, 9 insertions, 8 deletions
diff --git a/noncore/games/bounce/game.cpp b/noncore/games/bounce/game.cpp
index bbd3d0b..5ef9f15 100644
--- a/noncore/games/bounce/game.cpp
+++ b/noncore/games/bounce/game.cpp
@@ -9,144 +9,144 @@
9 * This program is distributed in the hope that it will be useful, 9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details. 12 * Library General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU Library General Public 14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the Free 15 * License along with this program; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */ 17 */
18 18
19 19
20#include "game.h" 20#include "game.h"
21 21
22/* OPIE */ 22/* OPIE */
23#include <opie2/odebug.h> 23#include <opie2/odebug.h>
24#include <qpe/qpeapplication.h> 24#include <qpe/qpeapplication.h>
25using namespace Opie::Core; 25using namespace Opie::Core;
26 26
27/* QT */ 27/* QT */
28#include <qtimer.h> 28#include <qtimer.h>
29 29
30/* STD */ 30/* STD */
31#include <stdlib.h> 31#include <stdlib.h>
32 32
33#define TILE_SIZE 9 33#define TILE_SIZE 9
34 34
35#define TILE_FIRST ((FIELD_WIDTH-2)*(FIELD_HEIGHT-2)) 35#define TILE_FIRST ((FIELD_WIDTH-2)*(FIELD_HEIGHT-2))
36#define TILE_FREE (TILE_FIRST + 0) 36#define TILE_FREE (TILE_FIRST + 0)
37#define TILE_BORDER (TILE_FIRST + 1) 37#define TILE_BORDER (TILE_FIRST + 1)
38#define TILE_WALLEND (TILE_FIRST + 2) 38#define TILE_WALLEND (TILE_FIRST + 2)
39#define TILE_WALLUP (TILE_FIRST + 3) 39#define TILE_WALLUP (TILE_FIRST + 3)
40#define TILE_WALLDOWN (TILE_FIRST + 4) 40#define TILE_WALLDOWN (TILE_FIRST + 4)
41#define TILE_WALLLEFT (TILE_FIRST + 5) 41#define TILE_WALLLEFT (TILE_FIRST + 5)
42#define TILE_WALLRIGHT (TILE_FIRST + 6) 42#define TILE_WALLRIGHT (TILE_FIRST + 6)
43 43
44#define GAME_DELAY 15 44#define GAME_DELAY 15
45#define BALL_ANIM_DELAY 60 45#define BALL_ANIM_DELAY 60
46#define WALL_DELAY 100 46#define WALL_DELAY 100
47 47
48#define MS2TICKS( ms ) ((ms)/GAME_DELAY) 48#define MS2TICKS( ms ) ((ms)/GAME_DELAY)
49 49
50Arrow::Arrow(QCanvasPixmapArray* array, QCanvas* canvas) 50Arrow::Arrow(QCanvasPixmapArray* array, QCanvas* canvas)
51 : QCanvasSprite( array, canvas ) 51 : QCanvasSprite( array, canvas )
52{ 52{
53 m_vertical = true; 53 m_vertical = true;
54 move(3,3); 54 move(3,3);
55} 55}
56 56
57void Arrow::update() 57void Arrow::update()
58{ 58{
59 if ( m_vertical ) 59 if ( m_vertical )
60 setFrame( 0 ); 60 setFrame( 0 );
61 else 61 else
62 setFrame( 1 ); 62 setFrame( 1 );
63} 63}
64 64
65void Arrow::changeDirection() 65void Arrow::changeDirection()
66{ 66{
67 m_vertical = ! m_vertical; 67 m_vertical = ! m_vertical;
68 update(); 68 update();
69} 69}
70 70
71 71
72Ball::Ball(QCanvasPixmapArray* array, QCanvas* canvas) 72Ball::Ball(QCanvasPixmapArray* array, QCanvas* canvas)
73 : QCanvasSprite( array, canvas ), m_animDelay( 0 ), m_soundDelay( MS2TICKS(BALL_ANIM_DELAY)/2 ) 73 : QCanvasSprite( array, canvas ), m_animDelay( 0 ), m_soundDelay( MS2TICKS(BALL_ANIM_DELAY)/2 )
74{ 74{
75} 75}
76 76
77void Ball::update() 77void Ball::update()
78{ 78{
79 m_animDelay--; 79 m_animDelay--;
80 if ( m_animDelay<=0 ) 80 if ( m_animDelay<=0 )
81 { 81 {
82 m_animDelay = MS2TICKS(BALL_ANIM_DELAY); 82 m_animDelay = MS2TICKS(BALL_ANIM_DELAY);
83 int frameNum = frame(); 83 int frameNum = frame();
84 frameNum++; 84 frameNum++;
85 if ( frameNum>=frameCount() ) 85 if ( frameNum>=frameCount() )
86 frameNum = 0; 86 frameNum = 0;
87 setFrame( frameNum ); 87 setFrame( frameNum );
88 } 88 }
89} 89}
90 90
91void Ball::advance(int stage) 91void Ball::advance(int stage)
92{ 92{
93 bool reflectX = false; 93 bool reflectX = false;
94 bool reflectY = false; 94 bool reflectY = false;
95 95
96 // check for collisions 96 // check for collisions
97 if ( collide(xVelocity(), 0) ) reflectX = true; 97 if ( collide(xVelocity(), 0) ) reflectX = true;
98 if ( collide(0, yVelocity()) ) reflectY = true; 98 if ( collide(0, yVelocity()) ) reflectY = true;
99 if ( !reflectX && !reflectY && collide(xVelocity(), yVelocity()) ) reflectX = reflectY = true; 99 if ( !reflectX && !reflectY && collide(xVelocity(), yVelocity()) ) reflectX = reflectY = true;
100 100
101 // emit collision 101 // emit collision
102 QRect r = boundingRect(); 102 QRect r = boundingRect();
103 r.moveBy( xVelocity(), yVelocity() ); 103 r.moveBy( static_cast<int>(xVelocity()), static_cast<int>( yVelocity() ) );
104 JezzField* field = (JezzField *)canvas(); 104 JezzField* field = (JezzField *)canvas();
105 105
106 int ul = field->tile( r.left() / TILE_SIZE, r.top() / TILE_SIZE ); 106 int ul = field->tile( r.left() / TILE_SIZE, r.top() / TILE_SIZE );
107 int ur = field->tile( r.right() / TILE_SIZE, r.top() / TILE_SIZE ); 107 int ur = field->tile( r.right() / TILE_SIZE, r.top() / TILE_SIZE );
108 int bl = field->tile( r.left() / TILE_SIZE, r.bottom() / TILE_SIZE ); 108 int bl = field->tile( r.left() / TILE_SIZE, r.bottom() / TILE_SIZE );
109 int br = field->tile( r.right() / TILE_SIZE, r.bottom() / TILE_SIZE ); 109 int br = field->tile( r.right() / TILE_SIZE, r.bottom() / TILE_SIZE );
110 110
111 if ( ul!=TILE_FREE ) field->emitBallCollisiton( this, r.left() / TILE_SIZE, r.top() / TILE_SIZE, ul ); else 111 if ( ul!=TILE_FREE ) field->emitBallCollisiton( this, r.left() / TILE_SIZE, r.top() / TILE_SIZE, ul ); else
112 if ( ur!=TILE_FREE ) field->emitBallCollisiton( this, r.right() / TILE_SIZE, r.top() / TILE_SIZE, ur ); else 112 if ( ur!=TILE_FREE ) field->emitBallCollisiton( this, r.right() / TILE_SIZE, r.top() / TILE_SIZE, ur ); else
113 if ( bl!=TILE_FREE ) field->emitBallCollisiton( this, r.left() / TILE_SIZE, r.bottom() / TILE_SIZE, bl ); else 113 if ( bl!=TILE_FREE ) field->emitBallCollisiton( this, r.left() / TILE_SIZE, r.bottom() / TILE_SIZE, bl ); else
114 if ( br!=TILE_FREE ) field->emitBallCollisiton( this, r.right() / TILE_SIZE, r.bottom() / TILE_SIZE, br ); 114 if ( br!=TILE_FREE ) field->emitBallCollisiton( this, r.right() / TILE_SIZE, r.bottom() / TILE_SIZE, br );
115 115
116 // apply reflection 116 // apply reflection
117 if ( reflectX ) setXVelocity( -xVelocity() ); 117 if ( reflectX ) setXVelocity( -xVelocity() );
118 if ( reflectY ) setYVelocity( -yVelocity() ); 118 if ( reflectY ) setYVelocity( -yVelocity() );
119 119
120 // update field 120 // update field
121 update(); 121 update();
122 QCanvasSprite::advance( stage ); 122 QCanvasSprite::advance( stage );
123} 123}
124 124
125bool Ball::collide( double dx, double dy ) 125bool Ball::collide( double dx, double dy )
126{ 126{
127 QRect r = boundingRect(); 127 QRect r = boundingRect();
128 r.moveBy( dx, dy ); 128 r.moveBy( static_cast<int>( dx ), static_cast<int>( dy ) );
129 JezzField* field = (JezzField *)canvas(); 129 JezzField* field = (JezzField *)canvas();
130 130
131 int ul = field->tile( r.left() / TILE_SIZE, r.top() / TILE_SIZE ); 131 int ul = field->tile( r.left() / TILE_SIZE, r.top() / TILE_SIZE );
132 int ur = field->tile( r.right() / TILE_SIZE, r.top() / TILE_SIZE ); 132 int ur = field->tile( r.right() / TILE_SIZE, r.top() / TILE_SIZE );
133 int bl = field->tile( r.left() / TILE_SIZE, r.bottom() / TILE_SIZE ); 133 int bl = field->tile( r.left() / TILE_SIZE, r.bottom() / TILE_SIZE );
134 int br = field->tile( r.right() / TILE_SIZE, r.bottom() / TILE_SIZE ); 134 int br = field->tile( r.right() / TILE_SIZE, r.bottom() / TILE_SIZE );
135 135
136 return ( ul!=TILE_FREE || ur!=TILE_FREE || bl!=TILE_FREE || br!=TILE_FREE ); 136 return ( ul!=TILE_FREE || ur!=TILE_FREE || bl!=TILE_FREE || br!=TILE_FREE );
137} 137}
138 138
139/*************************************************************************/ 139/*************************************************************************/
140 140
141Wall::Wall( JezzField *field, int x, int y, Direction dir, int tile, QObject *parent, const char *name ) 141Wall::Wall( JezzField *field, int x, int y, Direction dir, int tile, QObject *parent, const char *name )
142 : QObject( parent, name ), m_dir( dir ), m_field( field ), m_startX( x ), m_startY( y ), 142 : QObject( parent, name ), m_dir( dir ), m_field( field ), m_startX( x ), m_startY( y ),
143 m_tile( tile ), m_delay( MS2TICKS(WALL_DELAY)/2 ), m_active( true ) 143 m_tile( tile ), m_delay( MS2TICKS(WALL_DELAY)/2 ), m_active( true )
144{ 144{
145 // setup position and direction 145 // setup position and direction
146 m_dx = 0; 146 m_dx = 0;
147 m_dy = 0; 147 m_dy = 0;
148 switch ( m_dir ) 148 switch ( m_dir )
149 { 149 {
150 case Up: m_dy = -1; break; 150 case Up: m_dy = -1; break;
151 case Down: m_dy = 1; break; 151 case Down: m_dy = 1; break;
152 case Left: m_dx = -1; break; 152 case Left: m_dx = -1; break;
@@ -278,163 +278,164 @@ void JezzView::viewportMouseReleaseEvent( QMouseEvent *ev )
278 { 278 {
279 emit buildWall( ev->x()/TILE_SIZE, ev->y()/TILE_SIZE, m_vertical ); 279 emit buildWall( ev->x()/TILE_SIZE, ev->y()/TILE_SIZE, m_vertical );
280 } 280 }
281} 281}
282 282
283void JezzView::changeCursor() 283void JezzView::changeCursor()
284{ 284{
285 m_vertical = !m_vertical; 285 m_vertical = !m_vertical;
286 if ( m_vertical ) 286 if ( m_vertical )
287 { 287 {
288 setCursor( sizeVerCursor ); 288 setCursor( sizeVerCursor );
289 } 289 }
290 else 290 else
291 { 291 {
292 setCursor( sizeHorCursor ); 292 setCursor( sizeHorCursor );
293 } 293 }
294} 294}
295 295
296/*************************************************************************/ 296/*************************************************************************/
297 297
298JezzGame::JezzGame( int ballNum, QWidget *parent, const char *name ) 298JezzGame::JezzGame( int ballNum, QWidget *parent, const char *name )
299 : QWidget( parent, name ), m_wall1( 0 ), m_wall2( 0 ), 299 : QWidget( parent, name ), m_wall1( 0 ), m_wall2( 0 ),
300 m_text( 0 ), m_running( false ), m_percent( 0 ), m_pictured( false ) 300 m_text( 0 ), m_running( false ), m_percent( 0 ), m_pictured( false )
301{ 301{
302 QString path = QPEApplication::qpeDir()+"pics/bounce/"; 302 QString path = QPEApplication::qpeDir()+"pics/bounce/";
303 303
304 // load gfx 304 // load gfx
305 m_ballPixmaps = new QCanvasPixmapArray( path + "ball%1.png", 25 ); 305 m_ballPixmaps = new QCanvasPixmapArray( path + "ball%1.png", 25 );
306 for ( unsigned n=0; n < m_ballPixmaps->count(); n++ ) 306 for ( unsigned n=0; n < m_ballPixmaps->count(); n++ )
307 m_ballPixmaps->image(n)->setOffset( 0, 0 ); 307 m_ballPixmaps->image(n)->setOffset( 0, 0 );
308 308
309 m_arrowPixmaps = new QCanvasPixmapArray( path + "arrow%1.png", 2 ); 309 m_arrowPixmaps = new QCanvasPixmapArray( path + "arrow%1.png", 2 );
310 for ( unsigned n=0; n < m_arrowPixmaps->count(); n++ ) 310 for ( unsigned n=0; n < m_arrowPixmaps->count(); n++ )
311 m_arrowPixmaps->image(n)->setOffset( 0, 0 ); 311 m_arrowPixmaps->image(n)->setOffset( 0, 0 );
312 312
313 QPixmap tiles( path + "tiles.png" ); 313 QPixmap tiles( path + "tiles.png" );
314 314
315 // create field 315 // create field
316 m_field = new JezzField( tiles, this, "m_field" ); 316 m_field = new JezzField( tiles, this, "m_field" );
317 m_field->resize( TILE_SIZE*FIELD_WIDTH, TILE_SIZE*FIELD_HEIGHT ); 317 m_field->resize( TILE_SIZE*FIELD_WIDTH, TILE_SIZE*FIELD_HEIGHT );
318 318
319 for ( int x=0; x<FIELD_WIDTH; x++ ) 319 for ( int x=0; x<FIELD_WIDTH; x++ )
320 m_field->setTile( x, 0, TILE_BORDER ); 320 m_field->setTile( x, 0, TILE_BORDER );
321 for ( int y=1; y<FIELD_HEIGHT-1; y++ ) 321 for ( int y=1; y<FIELD_HEIGHT-1; y++ )
322 { 322 {
323 m_field->setTile( 0, y, TILE_BORDER ); 323 m_field->setTile( 0, y, TILE_BORDER );
324 for ( int x=1; x<FIELD_WIDTH-1; x++ ) 324 for ( int x=1; x<FIELD_WIDTH-1; x++ )
325 m_field->setTile( x, y, TILE_FREE ); 325 m_field->setTile( x, y, TILE_FREE );
326 m_field->setTile( FIELD_WIDTH-1, y, TILE_BORDER ); 326 m_field->setTile( FIELD_WIDTH-1, y, TILE_BORDER );
327 } 327 }
328 for ( int x=0; x<FIELD_WIDTH; x++ ) 328 for ( int x=0; x<FIELD_WIDTH; x++ )
329 m_field->setTile( x, FIELD_HEIGHT-1, TILE_BORDER ); 329 m_field->setTile( x, FIELD_HEIGHT-1, TILE_BORDER );
330 330
331 connect( m_field, SIGNAL(ballCollision(Ball*,int,int,int)), this, SLOT(ballCollision(Ball*,int,int,int)) ); 331 connect( m_field, SIGNAL(ballCollision(Ball*,int,int,int)), this, SLOT(ballCollision(Ball*,int,int,int)) );
332 332
333 // create view 333 // create view
334 m_view = new JezzView( m_field, this, "m_view" ); 334 m_view = new JezzView( m_field, this, "m_view" );
335 m_view->move( 0, 0 ); 335 m_view->move( 0, 0 );
336 m_view->adjustSize(); 336 m_view->adjustSize();
337 connect( m_view, SIGNAL(buildWall(int,int,bool)), this, SLOT(buildWall(int,int,bool)) ); 337 connect( m_view, SIGNAL(buildWall(int,int,bool)), this, SLOT(buildWall(int,int,bool)) );
338 338
339 // create balls 339 // create balls
340 for ( int n=0; n<ballNum; n++ ) 340 for ( int n=0; n<ballNum; n++ )
341 { 341 {
342 Ball *ball = new Ball( m_ballPixmaps, m_field ); 342 Ball *ball = new Ball( m_ballPixmaps, m_field );
343 m_balls.append( ball ); 343 m_balls.append( ball );
344 ball->setVelocity( ((rand() & 1)*2-1)*2, ((rand() & 1)*2-1)*2 ); 344 ball->setVelocity( ((rand() & 1)*2-1)*2, ((rand() & 1)*2-1)*2 );
345 ball->setFrame( rand() % 25 ); 345 ball->setFrame( rand() % 25 );
346 ball->move( 4*TILE_SIZE + ( rand() - 50 ) % ( (FIELD_WIDTH-8)*TILE_SIZE ), 346 ball->move( 4*TILE_SIZE + ( rand() - 50 ) % ( (FIELD_WIDTH-8)*TILE_SIZE ),
347 4*TILE_SIZE + rand() % ( (FIELD_HEIGHT-8)*TILE_SIZE ) ); 347 4*TILE_SIZE + rand() % ( (FIELD_HEIGHT-8)*TILE_SIZE ) );
348 ball->show(); 348 ball->show();
349 } 349 }
350 350
351 // create arrow 351 // create arrow
352 arrow = new Arrow( m_arrowPixmaps, m_field ); 352 arrow = new Arrow( m_arrowPixmaps, m_field );
353 arrow->show(); 353 arrow->show();
354 354
355 // create text label 355 // create text label
356 m_text = new QCanvasText( m_field ); 356 m_text = new QCanvasText( m_field );
357 357
358 // create game clock 358 // create game clock
359 m_clock = new QTimer( this ); 359 m_clock = new QTimer( this );
360 connect( m_clock, SIGNAL(timeout()), this, SLOT(tick()) ); 360 connect( m_clock, SIGNAL(timeout()), this, SLOT(tick()) );
361 m_clock->start( GAME_DELAY ); 361 m_clock->start( GAME_DELAY );
362 362
363 // setup geometry 363 // setup geometry
364 setFixedSize( m_view->size() ); 364 setFixedSize( m_view->size() );
365} 365}
366 366
367JezzGame::~JezzGame() 367JezzGame::~JezzGame()
368{ 368{
369 m_balls.clear(); 369 m_balls.clear();
370 delete m_view; 370 delete m_view;
371 delete m_field; 371 delete m_field;
372 delete m_ballPixmaps; 372 delete m_ballPixmaps;
373} 373}
374 374
375void JezzGame::display( QString text, int size ) 375void JezzGame::display( QString text, int size )
376{ 376{
377 odebug << "This function \"display\" shouldn't be called!!!" << oendl; 377 odebug << "This function \"display\" shouldn't be called!!!" << oendl;
378 if ( !text.isEmpty() ) 378 if ( !text.isEmpty() )
379 { 379 {
380 QFont font( "Helvetica", size, QFont::Bold ); 380 QFont font( "Helvetica", size, QFont::Bold );
381 font.setStyleHint( QFont::Helvetica ); 381 font.setStyleHint( QFont::Helvetica );
382 m_text->setFont( font ); 382 m_text->setFont( font );
383 m_text->setText( text ); 383 m_text->setText( text );
384 384
385 QRect size = m_text->boundingRect(); 385 QRect size = m_text->boundingRect();
386 m_text->move( ( FIELD_WIDTH*TILE_SIZE - size.width() ) / 2, 386 m_text->move( ( FIELD_WIDTH*TILE_SIZE - size.width() ) / 2,
387 ( FIELD_HEIGHT*TILE_SIZE - size.height() ) / 2 ); 387 ( FIELD_HEIGHT*TILE_SIZE - size.height() ) / 2 );
388 388
389 m_text->show(); 389 m_text->show();
390 } else 390 } else
391 { 391 {
392 m_text->hide(); 392 m_text->hide();
393 } 393 }
394} 394}
395 395
396void JezzGame::start() 396void JezzGame::start()
397{ 397{
398 m_running = true; 398 m_running = true;
399} 399}
400 400
401void JezzGame::stop() 401void JezzGame::stop()
402{ 402{
403 m_running = false; 403 m_running = false;
404} 404}
405 405
406 406
407void JezzGame::makeBlack() 407void JezzGame::makeBlack()
408{ 408{
409 // copy current field into buffer 409 // copy current field into buffer
410 for ( int y=0; y<FIELD_HEIGHT; y++ ) 410 for ( int y=0; y<FIELD_HEIGHT; y++ )
411 for ( int x=0; x<FIELD_WIDTH; x++ ) 411 for ( int x=0; x<FIELD_WIDTH; x++ )
412 m_buf[x][y] = m_field->tile( x, y ); 412 m_buf[x][y] = m_field->tile( x, y );
413 413
414 // fill areas that contains a ball 414 // fill areas that contains a ball
415 for ( Ball *ball=m_balls.first(); ball!=0; ball=m_balls.next() ) 415 for ( Ball *ball=m_balls.first(); ball!=0; ball=m_balls.next() )
416 fill( ball->x()/TILE_SIZE, ball->y()/TILE_SIZE ); 416 fill( static_cast<int>( ball->x()/TILE_SIZE ),
417 static_cast<int>( ball->y()/TILE_SIZE ) );
417 418
418 // areas still free can be blacked now 419 // areas still free can be blacked now
419 for ( int y=0; y<FIELD_HEIGHT; y++ ) 420 for ( int y=0; y<FIELD_HEIGHT; y++ )
420 for ( int x=0; x<FIELD_WIDTH; x++ ) 421 for ( int x=0; x<FIELD_WIDTH; x++ )
421 { 422 {
422 if ( m_buf[x][y]==TILE_FREE ) 423 if ( m_buf[x][y]==TILE_FREE )
423 m_field->setGameTile( x, y, true ); 424 m_field->setGameTile( x, y, true );
424 } 425 }
425 426
426 m_field->update(); 427 m_field->update();
427 m_view->repaint(); 428 m_view->repaint();
428 429
429 // count percent value of occupied area 430 // count percent value of occupied area
430 int p = percent(); 431 int p = percent();
431 if ( p!=m_percent ) 432 if ( p!=m_percent )
432 { 433 {
433 m_percent = p; 434 m_percent = p;
434 emit newPercent( m_percent ); 435 emit newPercent( m_percent );
435 } 436 }
436} 437}
437 438
438int JezzGame::percent() 439int JezzGame::percent()
439{ 440{
440 int notFree = 0; 441 int notFree = 0;