1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | /*
|
---|
42 | * KAsteroids - Copyright (c) Martin R. Jones 1997
|
---|
43 | *
|
---|
44 | * Part of the KDE project
|
---|
45 | */
|
---|
46 | // --- toplevel.cpp ---
|
---|
47 | #include <q3accel.h>
|
---|
48 | #include <qlabel.h>
|
---|
49 | #include <qlayout.h>
|
---|
50 | #include <qlcdnumber.h>
|
---|
51 | #include <qpushbutton.h>
|
---|
52 |
|
---|
53 | #include <qapplication.h>
|
---|
54 | //Added by qt3to4:
|
---|
55 | #include <Q3HBoxLayout>
|
---|
56 | #include <QShowEvent>
|
---|
57 | #include <Q3Frame>
|
---|
58 | #include <QPixmap>
|
---|
59 | #include <QHideEvent>
|
---|
60 | #include <QKeyEvent>
|
---|
61 | #include <Q3VBoxLayout>
|
---|
62 |
|
---|
63 | #include "toplevel.h"
|
---|
64 | #include "ledmeter.h"
|
---|
65 |
|
---|
66 |
|
---|
67 | #define SB_SCORE 1
|
---|
68 | #define SB_LEVEL 2
|
---|
69 | #define SB_SHIPS 3
|
---|
70 |
|
---|
71 | struct SLevel
|
---|
72 | {
|
---|
73 | int nrocks;
|
---|
74 | double rockSpeed;
|
---|
75 | };
|
---|
76 |
|
---|
77 | #define MAX_LEVELS 16
|
---|
78 |
|
---|
79 | SLevel levels[MAX_LEVELS] =
|
---|
80 | {
|
---|
81 | { 1, 0.4 },
|
---|
82 | { 1, 0.6 },
|
---|
83 | { 2, 0.5 },
|
---|
84 | { 2, 0.7 },
|
---|
85 | { 2, 0.8 },
|
---|
86 | { 3, 0.6 },
|
---|
87 | { 3, 0.7 },
|
---|
88 | { 3, 0.8 },
|
---|
89 | { 4, 0.6 },
|
---|
90 | { 4, 0.7 },
|
---|
91 | { 4, 0.8 },
|
---|
92 | { 5, 0.7 },
|
---|
93 | { 5, 0.8 },
|
---|
94 | { 5, 0.9 },
|
---|
95 | { 5, 1.0 }
|
---|
96 | };
|
---|
97 |
|
---|
98 | const char *soundEvents[] =
|
---|
99 | {
|
---|
100 | "ShipDestroyed",
|
---|
101 | "RockDestroyed",
|
---|
102 | 0
|
---|
103 | };
|
---|
104 |
|
---|
105 | const char *soundDefaults[] =
|
---|
106 | {
|
---|
107 | "Explosion.wav",
|
---|
108 | "ploop.wav",
|
---|
109 | 0
|
---|
110 | };
|
---|
111 |
|
---|
112 |
|
---|
113 | KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
|
---|
114 | : Q3MainWindow( parent, name, 0 )
|
---|
115 | {
|
---|
116 | QWidget *border = new QWidget( this );
|
---|
117 | border->setBackgroundColor( Qt::black );
|
---|
118 | setCentralWidget( border );
|
---|
119 |
|
---|
120 | Q3VBoxLayout *borderLayout = new Q3VBoxLayout( border );
|
---|
121 | borderLayout->addStretch( 1 );
|
---|
122 |
|
---|
123 | QWidget *mainWin = new QWidget( border );
|
---|
124 | mainWin->setFixedSize(640, 480);
|
---|
125 | borderLayout->addWidget( mainWin, 0, Qt::AlignHCenter );
|
---|
126 |
|
---|
127 | borderLayout->addStretch( 1 );
|
---|
128 |
|
---|
129 | view = new KAsteroidsView( mainWin );
|
---|
130 | view->setFocusPolicy( Qt::StrongFocus );
|
---|
131 | connect( view, SIGNAL(shipKilled()), SLOT(slotShipKilled()) );
|
---|
132 | connect( view, SIGNAL(rockHit(int)), SLOT(slotRockHit(int)) );
|
---|
133 | connect( view, SIGNAL(rocksRemoved()), SLOT(slotRocksRemoved()) );
|
---|
134 | connect( view, SIGNAL(updateVitals()), SLOT(slotUpdateVitals()) );
|
---|
135 |
|
---|
136 | Q3VBoxLayout *vb = new Q3VBoxLayout( mainWin );
|
---|
137 | Q3HBoxLayout *hb = new Q3HBoxLayout;
|
---|
138 | Q3HBoxLayout *hbd = new Q3HBoxLayout;
|
---|
139 | vb->addLayout( hb );
|
---|
140 |
|
---|
141 | QFont labelFont( "helvetica", 24 );
|
---|
142 | QColorGroup grp( Qt::darkGreen, Qt::black, QColor( 128, 128, 128 ),
|
---|
143 | QColor( 64, 64, 64 ), Qt::black, Qt::darkGreen, Qt::black );
|
---|
144 | QPalette pal( grp, grp, grp );
|
---|
145 |
|
---|
146 | mainWin->setPalette( pal );
|
---|
147 |
|
---|
148 | hb->addSpacing( 10 );
|
---|
149 |
|
---|
150 | QLabel *label;
|
---|
151 | label = new QLabel( tr("Score"), mainWin );
|
---|
152 | label->setFont( labelFont );
|
---|
153 | label->setPalette( pal );
|
---|
154 | label->setFixedWidth( label->sizeHint().width() );
|
---|
155 | hb->addWidget( label );
|
---|
156 |
|
---|
157 | scoreLCD = new QLCDNumber( 6, mainWin );
|
---|
158 | scoreLCD->setFrameStyle( Q3Frame::NoFrame );
|
---|
159 | scoreLCD->setSegmentStyle( QLCDNumber::Flat );
|
---|
160 | scoreLCD->setFixedWidth( 150 );
|
---|
161 | scoreLCD->setPalette( pal );
|
---|
162 | hb->addWidget( scoreLCD );
|
---|
163 | hb->addStretch( 10 );
|
---|
164 |
|
---|
165 | label = new QLabel( tr("Level"), mainWin );
|
---|
166 | label->setFont( labelFont );
|
---|
167 | label->setPalette( pal );
|
---|
168 | label->setFixedWidth( label->sizeHint().width() );
|
---|
169 | hb->addWidget( label );
|
---|
170 |
|
---|
171 | levelLCD = new QLCDNumber( 2, mainWin );
|
---|
172 | levelLCD->setFrameStyle( Q3Frame::NoFrame );
|
---|
173 | levelLCD->setSegmentStyle( QLCDNumber::Flat );
|
---|
174 | levelLCD->setFixedWidth( 70 );
|
---|
175 | levelLCD->setPalette( pal );
|
---|
176 | hb->addWidget( levelLCD );
|
---|
177 | hb->addStretch( 10 );
|
---|
178 |
|
---|
179 | label = new QLabel( tr("Ships"), mainWin );
|
---|
180 | label->setFont( labelFont );
|
---|
181 | label->setFixedWidth( label->sizeHint().width() );
|
---|
182 | label->setPalette( pal );
|
---|
183 | hb->addWidget( label );
|
---|
184 |
|
---|
185 | shipsLCD = new QLCDNumber( 1, mainWin );
|
---|
186 | shipsLCD->setFrameStyle( Q3Frame::NoFrame );
|
---|
187 | shipsLCD->setSegmentStyle( QLCDNumber::Flat );
|
---|
188 | shipsLCD->setFixedWidth( 40 );
|
---|
189 | shipsLCD->setPalette( pal );
|
---|
190 | hb->addWidget( shipsLCD );
|
---|
191 |
|
---|
192 | hb->addStrut( 30 );
|
---|
193 |
|
---|
194 | vb->addWidget( view, 10 );
|
---|
195 |
|
---|
196 | // -- bottom layout:
|
---|
197 | vb->addLayout( hbd );
|
---|
198 |
|
---|
199 | QFont smallFont( "helvetica", 14 );
|
---|
200 | hbd->addSpacing( 10 );
|
---|
201 |
|
---|
202 | QString sprites_prefix = ":/trolltech/examples/graphicsview/portedasteroids/sprites/";
|
---|
203 | /*
|
---|
204 | label = new QLabel( tr( "T" ), mainWin );
|
---|
205 | label->setFont( smallFont );
|
---|
206 | label->setFixedWidth( label->sizeHint().width() );
|
---|
207 | label->setPalette( pal );
|
---|
208 | hbd->addWidget( label );
|
---|
209 |
|
---|
210 | teleportsLCD = new QLCDNumber( 1, mainWin );
|
---|
211 | teleportsLCD->setFrameStyle( QFrame::NoFrame );
|
---|
212 | teleportsLCD->setSegmentStyle( QLCDNumber::Flat );
|
---|
213 | teleportsLCD->setPalette( pal );
|
---|
214 | teleportsLCD->setFixedHeight( 20 );
|
---|
215 | hbd->addWidget( teleportsLCD );
|
---|
216 |
|
---|
217 | hbd->addSpacing( 10 );
|
---|
218 | */
|
---|
219 | QPixmap pm( sprites_prefix + "powerups/brake.png" );
|
---|
220 | label = new QLabel( mainWin );
|
---|
221 | label->setPixmap( pm );
|
---|
222 | label->setFixedWidth( label->sizeHint().width() );
|
---|
223 | label->setPalette( pal );
|
---|
224 | hbd->addWidget( label );
|
---|
225 |
|
---|
226 | brakesLCD = new QLCDNumber( 1, mainWin );
|
---|
227 | brakesLCD->setFrameStyle( Q3Frame::NoFrame );
|
---|
228 | brakesLCD->setSegmentStyle( QLCDNumber::Flat );
|
---|
229 | brakesLCD->setPalette( pal );
|
---|
230 | brakesLCD->setFixedHeight( 20 );
|
---|
231 | hbd->addWidget( brakesLCD );
|
---|
232 |
|
---|
233 | hbd->addSpacing( 10 );
|
---|
234 |
|
---|
235 | pm.load( sprites_prefix + "powerups/shield.png" );
|
---|
236 | label = new QLabel( mainWin );
|
---|
237 | label->setPixmap( pm );
|
---|
238 | label->setFixedWidth( label->sizeHint().width() );
|
---|
239 | label->setPalette( pal );
|
---|
240 | hbd->addWidget( label );
|
---|
241 |
|
---|
242 | shieldLCD = new QLCDNumber( 1, mainWin );
|
---|
243 | shieldLCD->setFrameStyle( Q3Frame::NoFrame );
|
---|
244 | shieldLCD->setSegmentStyle( QLCDNumber::Flat );
|
---|
245 | shieldLCD->setPalette( pal );
|
---|
246 | shieldLCD->setFixedHeight( 20 );
|
---|
247 | hbd->addWidget( shieldLCD );
|
---|
248 |
|
---|
249 | hbd->addSpacing( 10 );
|
---|
250 |
|
---|
251 | pm.load( sprites_prefix + "powerups/shoot.png" );
|
---|
252 | label = new QLabel( mainWin );
|
---|
253 | label->setPixmap( pm );
|
---|
254 | label->setFixedWidth( label->sizeHint().width() );
|
---|
255 | label->setPalette( pal );
|
---|
256 | hbd->addWidget( label );
|
---|
257 |
|
---|
258 | shootLCD = new QLCDNumber( 1, mainWin );
|
---|
259 | shootLCD->setFrameStyle( Q3Frame::NoFrame );
|
---|
260 | shootLCD->setSegmentStyle( QLCDNumber::Flat );
|
---|
261 | shootLCD->setPalette( pal );
|
---|
262 | shootLCD->setFixedHeight( 20 );
|
---|
263 | hbd->addWidget( shootLCD );
|
---|
264 |
|
---|
265 | hbd->addStretch( 1 );
|
---|
266 |
|
---|
267 | label = new QLabel( tr( "Fuel" ), mainWin );
|
---|
268 | label->setFont( smallFont );
|
---|
269 | label->setFixedWidth( label->sizeHint().width() + 10 );
|
---|
270 | label->setPalette( pal );
|
---|
271 | hbd->addWidget( label );
|
---|
272 |
|
---|
273 | powerMeter = new KALedMeter( mainWin );
|
---|
274 | powerMeter->setFrameStyle( Q3Frame::Box | Q3Frame::Plain );
|
---|
275 | powerMeter->setRange( MAX_POWER_LEVEL );
|
---|
276 | powerMeter->addColorRange( 10, Qt::darkRed );
|
---|
277 | powerMeter->addColorRange( 20, QColor(160, 96, 0) );
|
---|
278 | powerMeter->addColorRange( 70, Qt::darkGreen );
|
---|
279 | powerMeter->setCount( 40 );
|
---|
280 | powerMeter->setPalette( pal );
|
---|
281 | powerMeter->setFixedSize( 200, 12 );
|
---|
282 | hbd->addWidget( powerMeter );
|
---|
283 |
|
---|
284 | shipsRemain = 3;
|
---|
285 | showHiscores = FALSE;
|
---|
286 |
|
---|
287 | actions.insert( Qt::Key_Up, Thrust );
|
---|
288 | actions.insert( Qt::Key_Left, RotateLeft );
|
---|
289 | actions.insert( Qt::Key_Right, RotateRight );
|
---|
290 | actions.insert( Qt::Key_Space, Shoot );
|
---|
291 | actions.insert( Qt::Key_Z, Teleport );
|
---|
292 | actions.insert( Qt::Key_X, Brake );
|
---|
293 | actions.insert( Qt::Key_S, Shield );
|
---|
294 | actions.insert( Qt::Key_P, Pause );
|
---|
295 | actions.insert( Qt::Key_L, Launch );
|
---|
296 | actions.insert( Qt::Key_N, NewGame );
|
---|
297 |
|
---|
298 | view->showText( tr( "Press N to start playing" ), Qt::yellow );
|
---|
299 | }
|
---|
300 |
|
---|
301 | KAstTopLevel::~KAstTopLevel()
|
---|
302 | {
|
---|
303 | }
|
---|
304 |
|
---|
305 | void KAstTopLevel::playSound( const char * )
|
---|
306 | {
|
---|
307 | }
|
---|
308 |
|
---|
309 | void KAstTopLevel::keyPressEvent( QKeyEvent *event )
|
---|
310 | {
|
---|
311 | if ( event->isAutoRepeat() || !actions.contains( event->key() ) )
|
---|
312 | {
|
---|
313 | event->ignore();
|
---|
314 | return;
|
---|
315 | }
|
---|
316 |
|
---|
317 | Action a = actions[ event->key() ];
|
---|
318 |
|
---|
319 | switch ( a )
|
---|
320 | {
|
---|
|
---|