summaryrefslogtreecommitdiffstats
path: root/examples/graphicsview/portedasteroids
diff options
context:
space:
mode:
Diffstat (limited to 'examples/graphicsview/portedasteroids')
-rw-r--r--examples/graphicsview/portedasteroids/animateditem.cpp11
-rw-r--r--examples/graphicsview/portedasteroids/animateditem.h18
-rw-r--r--examples/graphicsview/portedasteroids/ledmeter.cpp40
-rw-r--r--examples/graphicsview/portedasteroids/ledmeter.h12
-rw-r--r--examples/graphicsview/portedasteroids/main.cpp4
-rw-r--r--examples/graphicsview/portedasteroids/portedasteroids.desktop11
-rw-r--r--examples/graphicsview/portedasteroids/portedasteroids.pro13
-rw-r--r--examples/graphicsview/portedasteroids/sprites.h2
-rw-r--r--examples/graphicsview/portedasteroids/toplevel.cpp85
-rw-r--r--examples/graphicsview/portedasteroids/toplevel.h15
-rw-r--r--examples/graphicsview/portedasteroids/view.cpp270
-rw-r--r--examples/graphicsview/portedasteroids/view.h21
12 files changed, 288 insertions, 214 deletions
diff --git a/examples/graphicsview/portedasteroids/animateditem.cpp b/examples/graphicsview/portedasteroids/animateditem.cpp
index 489ef34..c36c141 100644
--- a/examples/graphicsview/portedasteroids/animateditem.cpp
+++ b/examples/graphicsview/portedasteroids/animateditem.cpp
@@ -40,12 +40,13 @@
#include "animateditem.h"
-#include <QtGui/qbitmap.h>
-#include <QtGui/qpainter.h>
+#include <QtGui/QBitmap>
+#include <QtGui/QPainter>
+#include <QGraphicsScene>
AnimatedPixmapItem::AnimatedPixmapItem(const QList<QPixmap> &animation,
QGraphicsScene *scene)
- : QGraphicsItem(0, scene), currentFrame(0), vx(0), vy(0)
+ : QGraphicsItem(0), currentFrame(0), vx(0), vy(0)
{
for (int i = 0; i < animation.size(); ++i) {
QPixmap pixmap = animation.at(i);
@@ -55,6 +56,8 @@ AnimatedPixmapItem::AnimatedPixmapItem(const QList<QPixmap> &animation,
frame.boundingRect = pixmap.rect();
frames << frame;
}
+
+ scene->addItem(this);
}
void AnimatedPixmapItem::setFrame(int frame)
@@ -63,6 +66,8 @@ void AnimatedPixmapItem::setFrame(int frame)
prepareGeometryChange();
currentFrame = frame % frames.size();
}
+
+ //scene->addItem(this);
}
void AnimatedPixmapItem::advance(int phase)
diff --git a/examples/graphicsview/portedasteroids/animateditem.h b/examples/graphicsview/portedasteroids/animateditem.h
index 712d70d..23117b4 100644
--- a/examples/graphicsview/portedasteroids/animateditem.h
+++ b/examples/graphicsview/portedasteroids/animateditem.h
@@ -49,18 +49,12 @@ public:
AnimatedPixmapItem(const QList<QPixmap> &animation, QGraphicsScene *scene = 0);
void setFrame(int frame);
- inline int frame() const
- { return currentFrame; }
- inline int frameCount() const
- { return frames.size(); }
- inline QPixmap image(int frame) const
- { return frames.isEmpty() ? QPixmap() : frames.at(frame % frames.size()).pixmap; }
- inline void setVelocity(qreal xvel, qreal yvel)
- { vx = xvel; vy = yvel; }
- inline qreal xVelocity() const
- { return vx; }
- inline qreal yVelocity() const
- { return vy; }
+ inline int frame() const { return currentFrame; }
+ inline int frameCount() const { return frames.size(); }
+ inline QPixmap image(int frame) const { return frames.isEmpty() ? QPixmap() : frames.at(frame % frames.size()).pixmap; }
+ inline void setVelocity(qreal xvel, qreal yvel) { vx = xvel; vy = yvel; }
+ inline qreal xVelocity() const { return vx; }
+ inline qreal yVelocity() const { return vy; }
QRectF boundingRect() const;
QPainterPath shape() const;
diff --git a/examples/graphicsview/portedasteroids/ledmeter.cpp b/examples/graphicsview/portedasteroids/ledmeter.cpp
index 9653fc6..aefe200 100644
--- a/examples/graphicsview/portedasteroids/ledmeter.cpp
+++ b/examples/graphicsview/portedasteroids/ledmeter.cpp
@@ -44,15 +44,14 @@
* Part of the KDE project
*/
-#include <qpainter.h>
-//Added by qt3to4:
+#include <QPainter>
#include <QResizeEvent>
-#include <Q3Frame>
+#include <QFrame>
+#include <QColorGroup>
#include "ledmeter.h"
-KALedMeter::KALedMeter( QWidget *parent ) : Q3Frame( parent )
+KALedMeter::KALedMeter( QWidget *parent ) : QFrame( parent )
{
- mCRanges.setAutoDelete( TRUE );
mRange = 100;
mCount = 20;
mCurrentCount = 0;
@@ -60,6 +59,13 @@ KALedMeter::KALedMeter( QWidget *parent ) : Q3Frame( parent )
setMinimumWidth( mCount * 2 + frameWidth() );
}
+KALedMeter::~KALedMeter()
+{
+ qDeleteAll(mCRanges);
+ mCRanges.clear();
+}
+
+
void KALedMeter::setRange( int r )
{
mRange = r;
@@ -106,27 +112,30 @@ void KALedMeter::addColorRange( int pc, const QColor &c )
void KALedMeter::resizeEvent( QResizeEvent *e )
{
- Q3Frame::resizeEvent( e );
+ QFrame::resizeEvent( e );
int w = ( width() - frameWidth() - 2 ) / mCount * mCount;
w += frameWidth() + 2;
setFrameRect( QRect( 0, 0, w, height() ) );
}
-void KALedMeter::drawContents( QPainter *p )
+void KALedMeter::paintEvent(QPaintEvent *event)
{
+ QFrame::paintEvent(event);
+
QRect b = contentsRect();
+ QPainter p(this);
unsigned cidx = 0;
int ncol = mCount;
- QColor col = colorGroup().foreground();
+ QColor col = palette().foreground().color();
if ( !mCRanges.isEmpty() )
{
col = mCRanges.at( cidx )->mColor;
ncol = mCRanges.at( cidx )->mValue;
}
- p->setBrush( col );
- p->setPen( col );
+ p.setBrush( col );
+ p.setPen( col );
int lw = b.width() / mCount;
int lx = b.left() + 1;
@@ -138,21 +147,22 @@ void KALedMeter::drawContents( QPainter *p )
{
col = mCRanges.at( cidx )->mColor;
ncol = mCRanges.at( cidx )->mValue;
- p->setBrush( col );
- p->setPen( col );
+ p.setBrush( col );
+ p.setPen( col );
}
}
- p->drawRect( lx, b.top() + 1, lw - 1, b.height() - 2 );
+ p.drawRect( lx, b.top() + 1, lw - 1, b.height() - 2 );
}
}
void KALedMeter::calcColorRanges()
{
int prev = 0;
- ColorRange *cr;
- for ( cr = mCRanges.first(); cr; cr = mCRanges.next() )
+
+ for(QList<ColorRange*>::iterator it = mCRanges.begin(); it != mCRanges.end(); it++)
{
+ ColorRange *cr = *it;
cr->mValue = prev + cr->mPc * mCount / 100;
prev = cr->mValue;
}
diff --git a/examples/graphicsview/portedasteroids/ledmeter.h b/examples/graphicsview/portedasteroids/ledmeter.h
index 2d4ae23..0e3851f 100644
--- a/examples/graphicsview/portedasteroids/ledmeter.h
+++ b/examples/graphicsview/portedasteroids/ledmeter.h
@@ -47,17 +47,17 @@
#ifndef __LEDMETER_H__
#define __LEDMETER_H__
-#include <q3frame.h>
-#include <q3ptrlist.h>
-//Added by qt3to4:
+#include <QFrame>
+#include <QList>
#include <QResizeEvent>
-class KALedMeter : public Q3Frame
+class KALedMeter : public QFrame
{
Q_OBJECT
public:
KALedMeter( QWidget *parent );
+ ~KALedMeter();
int range() const { return mRange; }
void setRange( int r );
@@ -74,7 +74,7 @@ public slots:
protected:
virtual void resizeEvent( QResizeEvent * );
- virtual void drawContents( QPainter * );
+ virtual void paintEvent(QPaintEvent *event);
void calcColorRanges();
protected:
@@ -89,7 +89,7 @@ protected:
int mCount;
int mCurrentCount;
int mValue;
- Q3PtrList<ColorRange> mCRanges;
+ QList<ColorRange*> mCRanges;
};
#endif
diff --git a/examples/graphicsview/portedasteroids/main.cpp b/examples/graphicsview/portedasteroids/main.cpp
index 4ed4e9f..e6c7623 100644
--- a/examples/graphicsview/portedasteroids/main.cpp
+++ b/examples/graphicsview/portedasteroids/main.cpp
@@ -52,7 +52,11 @@ int main(int argc, char **argv)
KAstTopLevel topLevel;
topLevel.setWindowTitle("Ported Asteroids Game");
+#if defined(Q_OS_SYMBIAN)
+ topLevel.showFullScreen();
+#else
topLevel.show();
+#endif
app.setQuitOnLastWindowClosed(true);
return app.exec();
diff --git a/examples/graphicsview/portedasteroids/portedasteroids.desktop b/examples/graphicsview/portedasteroids/portedasteroids.desktop
new file mode 100644
index 0000000..abd0616
--- /dev/null
+++ b/examples/graphicsview/portedasteroids/portedasteroids.desktop
@@ -0,0 +1,11 @@
+[Desktop Entry]
+Encoding=UTF-8
+Version=1.0
+Type=Application
+Terminal=false
+Name=Ported Asteroids
+Exec=/opt/usr/bin/portedasteroids
+Icon=portedasteroids
+X-Window-Icon=
+X-HildonDesk-ShowInToolbar=true
+X-Osso-Type=application/x-executable
diff --git a/examples/graphicsview/portedasteroids/portedasteroids.pro b/examples/graphicsview/portedasteroids/portedasteroids.pro
index b28ab54..98ec4fe 100644
--- a/examples/graphicsview/portedasteroids/portedasteroids.pro
+++ b/examples/graphicsview/portedasteroids/portedasteroids.pro
@@ -2,13 +2,8 @@ TEMPLATE = app
INCLUDEPATH += .
# Input
-HEADERS += ledmeter.h sprites.h toplevel.h view.h
-SOURCES += ledmeter.cpp main.cpp toplevel.cpp view.cpp
-#The following line was inserted by qt3to4
-QT += qt3support
-
-HEADERS += animateditem.h
-SOURCES += animateditem.cpp
+HEADERS += ledmeter.h sprites.h toplevel.h view.h animateditem.h
+SOURCES += ledmeter.cpp main.cpp toplevel.cpp view.cpp animateditem.cpp
RESOURCES += portedasteroids.qrc
@@ -16,6 +11,10 @@ RESOURCES += portedasteroids.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/portedasteroids
sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS portedasteroids.pro bg.png sounds sprites
sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/portedasteroids
+
INSTALLS += target sources
symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri)
+maemo5: include($$QT_SOURCE_TREE/examples/maemo5pkgrules.pri)
+
+simulator: warning(This example might not fully work on Simulator platform)
diff --git a/examples/graphicsview/portedasteroids/sprites.h b/examples/graphicsview/portedasteroids/sprites.h
index e5f1dbb..7275cba 100644
--- a/examples/graphicsview/portedasteroids/sprites.h
+++ b/examples/graphicsview/portedasteroids/sprites.h
@@ -144,7 +144,7 @@ public:
{
if (cskip-- <= 0) {
setFrame( (frame()+step+frameCount())%frameCount() );
- cskip = QABS(skip);
+ cskip = qAbs(skip);
}
}
diff --git a/examples/graphicsview/portedasteroids/toplevel.cpp b/examples/graphicsview/portedasteroids/toplevel.cpp
index 367f8c6..6aa63c3 100644
--- a/examples/graphicsview/portedasteroids/toplevel.cpp
+++ b/examples/graphicsview/portedasteroids/toplevel.cpp
@@ -44,21 +44,20 @@
* Part of the KDE project
*/
// --- toplevel.cpp ---
-#include <q3accel.h>
-#include <qlabel.h>
-#include <qlayout.h>
-#include <qlcdnumber.h>
-#include <qpushbutton.h>
-
-#include <qapplication.h>
-//Added by qt3to4:
-#include <Q3HBoxLayout>
+#include <QAction>
+#include <QLabel>
+#include <QLayout>
+#include <QLCDNumber>
+#include <QPushButton>
+
+#include <QApplication>
+#include <QHBoxLayout>
#include <QShowEvent>
-#include <Q3Frame>
+#include <QFrame>
#include <QPixmap>
#include <QHideEvent>
#include <QKeyEvent>
-#include <Q3VBoxLayout>
+#include <QVBoxLayout>
#include "toplevel.h"
#include "ledmeter.h"
@@ -110,18 +109,28 @@ const char *soundDefaults[] =
};
-KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
- : Q3MainWindow( parent, name, 0 )
+KAstTopLevel::KAstTopLevel( QWidget *parent)
+ : QMainWindow(parent)
{
QWidget *border = new QWidget( this );
- border->setBackgroundColor( Qt::black );
+
+ QPalette palette;
+ palette.setColor(border->backgroundRole(), Qt::black);
+ border->setPalette(palette);
+
setCentralWidget( border );
- Q3VBoxLayout *borderLayout = new Q3VBoxLayout( border );
+ QVBoxLayout *borderLayout = new QVBoxLayout( border );
borderLayout->addStretch( 1 );
QWidget *mainWin = new QWidget( border );
+#if defined(Q_WS_MAEMO_5)
+ mainWin->setFixedSize(800, 430);
+#elif defined(Q_OS_SYMBIAN)
+ mainWin->setFixedSize(640, 340);
+#else
mainWin->setFixedSize(640, 480);
+#endif
borderLayout->addWidget( mainWin, 0, Qt::AlignHCenter );
borderLayout->addStretch( 1 );
@@ -133,15 +142,18 @@ KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
connect( view, SIGNAL(rocksRemoved()), SLOT(slotRocksRemoved()) );
connect( view, SIGNAL(updateVitals()), SLOT(slotUpdateVitals()) );
- Q3VBoxLayout *vb = new Q3VBoxLayout( mainWin );
- Q3HBoxLayout *hb = new Q3HBoxLayout;
- Q3HBoxLayout *hbd = new Q3HBoxLayout;
+ QVBoxLayout *vb = new QVBoxLayout( mainWin );
+ QHBoxLayout *hb = new QHBoxLayout;
+ QHBoxLayout *hbd = new QHBoxLayout;
vb->addLayout( hb );
+#if defined(Q_OS_SYMBIAN)
+ QFont labelFont( "helvetica", 8 );
+#else
QFont labelFont( "helvetica", 24 );
- QColorGroup grp( Qt::darkGreen, Qt::black, QColor( 128, 128, 128 ),
- QColor( 64, 64, 64 ), Qt::black, Qt::darkGreen, Qt::black );
- QPalette pal( grp, grp, grp );
+#endif
+
+ QPalette pal(Qt::darkGreen, Qt::black, QColor( 128, 128, 128 ), QColor( 64, 64, 64 ), Qt::black, Qt::darkGreen, Qt::black);
mainWin->setPalette( pal );
@@ -155,7 +167,7 @@ KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
hb->addWidget( label );
scoreLCD = new QLCDNumber( 6, mainWin );
- scoreLCD->setFrameStyle( Q3Frame::NoFrame );
+ scoreLCD->setFrameStyle( QFrame::NoFrame );
scoreLCD->setSegmentStyle( QLCDNumber::Flat );
scoreLCD->setFixedWidth( 150 );
scoreLCD->setPalette( pal );
@@ -169,7 +181,7 @@ KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
hb->addWidget( label );
levelLCD = new QLCDNumber( 2, mainWin );
- levelLCD->setFrameStyle( Q3Frame::NoFrame );
+ levelLCD->setFrameStyle( QFrame::NoFrame );
levelLCD->setSegmentStyle( QLCDNumber::Flat );
levelLCD->setFixedWidth( 70 );
levelLCD->setPalette( pal );
@@ -183,7 +195,7 @@ KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
hb->addWidget( label );
shipsLCD = new QLCDNumber( 1, mainWin );
- shipsLCD->setFrameStyle( Q3Frame::NoFrame );
+ shipsLCD->setFrameStyle( QFrame::NoFrame );
shipsLCD->setSegmentStyle( QLCDNumber::Flat );
shipsLCD->setFixedWidth( 40 );
shipsLCD->setPalette( pal );
@@ -196,7 +208,11 @@ KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
// -- bottom layout:
vb->addLayout( hbd );
+#if defined(Q_OS_SYMBIAN)
+ QFont smallFont( "helvetica", 6 );
+#else
QFont smallFont( "helvetica", 14 );
+#endif
hbd->addSpacing( 10 );
QString sprites_prefix = ":/trolltech/examples/graphicsview/portedasteroids/sprites/";
@@ -224,7 +240,7 @@ KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
hbd->addWidget( label );
brakesLCD = new QLCDNumber( 1, mainWin );
- brakesLCD->setFrameStyle( Q3Frame::NoFrame );
+ brakesLCD->setFrameStyle( QFrame::NoFrame );
brakesLCD->setSegmentStyle( QLCDNumber::Flat );
brakesLCD->setPalette( pal );
brakesLCD->setFixedHeight( 20 );
@@ -240,7 +256,7 @@ KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
hbd->addWidget( label );
shieldLCD = new QLCDNumber( 1, mainWin );
- shieldLCD->setFrameStyle( Q3Frame::NoFrame );
+ shieldLCD->setFrameStyle( QFrame::NoFrame );
shieldLCD->setSegmentStyle( QLCDNumber::Flat );
shieldLCD->setPalette( pal );
shieldLCD->setFixedHeight( 20 );
@@ -256,7 +272,7 @@ KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
hbd->addWidget( label );
shootLCD = new QLCDNumber( 1, mainWin );
- shootLCD->setFrameStyle( Q3Frame::NoFrame );
+ shootLCD->setFrameStyle( QFrame::NoFrame );
shootLCD->setSegmentStyle( QLCDNumber::Flat );
shootLCD->setPalette( pal );
shootLCD->setFixedHeight( 20 );
@@ -271,7 +287,7 @@ KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
hbd->addWidget( label );
powerMeter = new KALedMeter( mainWin );
- powerMeter->setFrameStyle( Q3Frame::Box | Q3Frame::Plain );
+ powerMeter->setFrameStyle( QFrame::Box | QFrame::Plain );
powerMeter->setRange( MAX_POWER_LEVEL );
powerMeter->addColorRange( 10, Qt::darkRed );
powerMeter->addColorRange( 20, QColor(160, 96, 0) );
@@ -295,6 +311,15 @@ KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
actions.insert( Qt::Key_L, Launch );
actions.insert( Qt::Key_N, NewGame );
+#if defined(Q_OS_SYMBIAN)
+ actions.insert( 122, Teleport );
+ actions.insert( 120, Brake );
+ actions.insert( 115, Shield );
+ actions.insert( 112, Pause );
+ actions.insert( 108, Launch );
+ actions.insert( 110, NewGame );
+#endif
+
view->showText( tr( "Press N to start playing" ), Qt::yellow );
}
@@ -431,14 +456,14 @@ void KAstTopLevel::keyReleaseEvent( QKeyEvent *event )
void KAstTopLevel::showEvent( QShowEvent *e )
{
- Q3MainWindow::showEvent( e );
+ QMainWindow::showEvent( e );
view->pause( FALSE );
view->setFocus();
}
void KAstTopLevel::hideEvent( QHideEvent *e )
{
- Q3MainWindow::hideEvent( e );
+ QMainWindow::hideEvent( e );
view->pause( TRUE );
}
diff --git a/examples/graphicsview/portedasteroids/toplevel.h b/examples/graphicsview/portedasteroids/toplevel.h
index 767580e..36b3afc 100644
--- a/examples/graphicsview/portedasteroids/toplevel.h
+++ b/examples/graphicsview/portedasteroids/toplevel.h
@@ -47,10 +47,9 @@
#ifndef __KAST_TOPLEVEL_H__
#define __KAST_TOPLEVEL_H__
-#include <q3mainwindow.h>
-#include <q3dict.h>
-#include <qmap.h>
-//Added by qt3to4:
+#include <QMainWindow>
+#include <QMultiHash>
+#include <QMap>
#include <QShowEvent>
#include <QHideEvent>
#include <QKeyEvent>
@@ -63,11 +62,11 @@ QT_BEGIN_NAMESPACE
class QLCDNumber;
QT_END_NAMESPACE
-class KAstTopLevel : public Q3MainWindow
+class KAstTopLevel : public QMainWindow
{
Q_OBJECT
public:
- KAstTopLevel( QWidget *parent=0, const char *name=0 );
+ KAstTopLevel( QWidget *parent = 0);
virtual ~KAstTopLevel();
private:
@@ -104,7 +103,7 @@ private:
KALedMeter *powerMeter;
bool sound;
- Q3Dict<QString> soundDict;
+ //Q3Dict<QString> soundDict;
// waiting for user to press Enter to launch a ship
bool waitShip;
@@ -118,7 +117,7 @@ private:
enum Action { Launch, Thrust, RotateLeft, RotateRight, Shoot, Teleport,
Brake, Shield, Pause, NewGame };
- QMap<int,Action> actions;
+ QMap<int, Action> actions;
};
#endif
diff --git a/examples/graphicsview/portedasteroids/view.cpp b/examples/graphicsview/portedasteroids/view.cpp
index 9429111..e4f46c8 100644
--- a/examples/graphicsview/portedasteroids/view.cpp
+++ b/examples/graphicsview/portedasteroids/view.cpp
@@ -48,16 +48,16 @@
#include <math.h>
#include <qapplication.h>
#include <qnamespace.h>
-#include <q3accel.h>
-#include <qmessagebox.h>
-#include <q3scrollview.h>
-#include <qdir.h>
+#include <QAction>
+#include <QMessageBox>
+#include <QScrollArea>
+#include <QDir>
#include <QGraphicsItem>
-//Added by qt3to4:
#include <QTimerEvent>
#include <QPixmap>
#include <QResizeEvent>
#include <QShowEvent>
+#include <QtDebug>
#include "view.h"
@@ -110,10 +110,10 @@ kas_animations [] =
{ 0, 0, 0 }
};
-KAsteroidsView::KAsteroidsView( QWidget *parent, const char *name )
- : QWidget( parent, name ),
+KAsteroidsView::KAsteroidsView( QWidget *parent)
+ : QWidget( parent),
field(0, 0, 640, 440),
- view(&field,this)
+ view(&field, this)
{
view.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
view.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
@@ -123,11 +123,6 @@ KAsteroidsView::KAsteroidsView( QWidget *parent, const char *name )
| QGraphicsView::DontSavePainterState
| QGraphicsView::DontAdjustForAntialiasing);
view.viewport()->setFocusProxy( this );
- rocks.setAutoDelete( TRUE );
- missiles.setAutoDelete( TRUE );
- bits.setAutoDelete( TRUE );
- powerups.setAutoDelete( TRUE );
- exhaust.setAutoDelete( TRUE );
QPixmap pm( IMG_BACKGROUND );
field.setBackgroundBrush( pm );
@@ -164,6 +159,11 @@ KAsteroidsView::KAsteroidsView( QWidget *parent, const char *name )
KAsteroidsView::~KAsteroidsView()
{
+ qDeleteAll(rocks); rocks.clear();
+ qDeleteAll(missiles); missiles.clear();
+ qDeleteAll(bits); bits.clear();
+ qDeleteAll(powerups); powerups.clear();
+ qDeleteAll(exhaust); exhaust.clear();
}
// - - -
@@ -172,11 +172,11 @@ void KAsteroidsView::reset()
{
if ( !initialized )
return;
- rocks.clear();
- missiles.clear();
- bits.clear();
- powerups.clear();
- exhaust.clear();
+ qDeleteAll(rocks); rocks.clear();
+ qDeleteAll(missiles); missiles.clear();
+ qDeleteAll(bits); bits.clear();
+ qDeleteAll(powerups); powerups.clear();
+ qDeleteAll(exhaust); exhaust.clear();
shotsFired = 0;
shotsHit = 0;
@@ -217,6 +217,11 @@ void KAsteroidsView::newGame()
void KAsteroidsView::endGame()
{
+ qDeleteAll(rocks); rocks.clear();
+ qDeleteAll(missiles); missiles.clear();
+ qDeleteAll(bits); bits.clear();
+ qDeleteAll(powerups); powerups.clear();
+ qDeleteAll(exhaust); exhaust.clear();
}
void KAsteroidsView::pause( bool p )
@@ -266,7 +271,7 @@ void KAsteroidsView::newShip()
ship->show();
shield->show();
mShieldCount = 1; // just in case the ship appears on a rock.
- shieldTimer->start( 1000, TRUE );
+ shieldTimer->start(1000);
}
void KAsteroidsView::setShield( bool s )
@@ -410,11 +415,9 @@ void KAsteroidsView::timerEvent( QTimerEvent * )
{
field.advance();
- AnimatedPixmapItem *rock;
-
// move rocks forward
- for ( rock = rocks.first(); rock; rock = rocks.next() ) {
- ((KRock *)rock)->nextFrame();
+ foreach(AnimatedPixmapItem *rock, rocks) {
+ ((KRock *)rock)->nextFrame();
wrapSprite( rock );
}
@@ -424,21 +427,24 @@ void KAsteroidsView::timerEvent( QTimerEvent * )
processMissiles();
// these are generated when a ship explodes
- for ( KBit *bit = bits.first(); bit; bit = bits.next() )
+ for(QList<KBit*>::iterator it = bits.begin(); it != bits.end(); it++)
{
- if ( bit->expired() )
+ KBit *bit = *it;
+ if( bit->expired() )
{
- bits.removeRef( bit );
+ delete bit;
+ it = bits.erase(it);
+ break;
}
else
{
- bit->growOlder();
- bit->setFrame( ( bit->frame()+1 ) % bit->frameCount() );
+ bit->growOlder();
+ bit->setFrame( ( bit->frame()+1 ) % bit->frameCount() );
}
}
- for ( KExhaust *e = exhaust.first(); e; e = exhaust.next() )
- exhaust.removeRef( e );
+ qDeleteAll(exhaust);
+ exhaust.clear();
// move / rotate ship.
// check for collision with a rock.
@@ -570,7 +576,16 @@ void KAsteroidsView::rockHit( AnimatedPixmapItem *hit )
}
else if ( hit->type() == ID_ROCK_SMALL )
emit rockHit( 2 );
- rocks.removeRef( hit );
+
+ for(QList<AnimatedPixmapItem*>::iterator it = rocks.begin(); it != rocks.end(); it++)
+ {
+ if((*it) == hit) {
+ delete *it;
+ it = rocks.erase(it);
+ break;
+ }
+ }
+
if ( rocks.count() == 0 )
emit rocksRemoved();
}
@@ -605,38 +620,43 @@ void KAsteroidsView::addExhaust( double x, double y, double dx,
void KAsteroidsView::processMissiles()
{
- KMissile *missile;
-
// if a missile has hit a rock, remove missile and break rock into smaller
// rocks or remove completely.
- Q3PtrListIterator<KMissile> it(missiles);
-
- for ( ; it.current(); ++it )
+ QList<KMissile*>::iterator itMissile = missiles.begin();
+ while(itMissile != missiles.end())
{
- missile = it.current();
- missile->growOlder();
+ (*itMissile)->growOlder();
- if ( missile->expired() )
+ if ( (*itMissile)->expired() )
{
- missiles.removeRef( missile );
- continue;
+ delete (*itMissile);
+ itMissile = missiles.erase(itMissile);
+ continue;
}
- wrapSprite( missile );
+ wrapSprite(*itMissile);
- QList<QGraphicsItem *> hits = missile->collidingItems(Qt::IntersectsItemBoundingRect);
- QList<QGraphicsItem *>::Iterator hit;
- for ( hit = hits.begin(); hit != hits.end(); ++hit )
+ bool missileErased = false;
+ QList<QGraphicsItem*> hits = (*itMissile)->collidingItems(Qt::IntersectsItemBoundingRect);
+ QList<QGraphicsItem*>::iterator itHit = hits.begin();
+
+ while (itHit != hits.end())
{
- if ( (*hit)->type() >= ID_ROCK_LARGE &&
- (*hit)->type() <= ID_ROCK_SMALL && (*hit)->collidesWithItem(missile) )
+ if ( (*itHit)->type() >= ID_ROCK_LARGE &&
+ (*itHit)->type() <= ID_ROCK_SMALL && (*itHit)->collidesWithItem(*itMissile) )
{
shotsHit++;
- rockHit( static_cast<AnimatedPixmapItem *>(*hit) );
- missiles.removeRef( missile );
+ rockHit( static_cast<AnimatedPixmapItem *>(*itHit) );
+ delete *itMissile;
+ itMissile = missiles.erase(itMissile);
+ missileErased = true;
break;
}
+ itHit++;
}
+
+ if(!missileErased)
+ itMissile++;
}
}
@@ -712,7 +732,7 @@ void KAsteroidsView::processShip()
bit->setVelocity( 1-randDouble()*2,
1-randDouble()*2 );
bit->setDeath( 60 + randInt(60) );
- bits.append( bit );
+ bits.push_back( bit );
}
ship->hide();
shield->hide();
@@ -820,15 +840,15 @@ void KAsteroidsView::processShip()
if ( shootShip )
{
- if ( !shootDelay && (int)missiles.count() < mShootCount + 2 )
+ if ( !shootDelay && (int)missiles.size() < mShootCount + 2 )
{
- KMissile *missile = new KMissile( animation[ID_MISSILE], &field );
+ KMissile *missile = new KMissile( animation[ID_MISSILE], &field );
missile->setPos( 21+ship->x()+cosangle*21,
21+ship->y()+sinangle*21 );
missile->setFrame( 0 );
missile->setVelocity( shipDx + cosangle*MISSILE_SPEED,
shipDy + sinangle*MISSILE_SPEED );
- missiles.append( missile );
+ missiles.push_back( missile );
shotsFired++;
reducePower( 1 );
@@ -857,75 +877,83 @@ void KAsteroidsView::processShip()
void KAsteroidsView::processPowerups()
{
- if ( !powerups.isEmpty() )
- {
- // if player gets the powerup remove it from the screen, if option
- // "Can destroy powerups" is enabled and a missile hits the powerup
- // destroy it
-
- KPowerup *pup;
- Q3PtrListIterator<KPowerup> it( powerups );
-
- for( ; it.current(); ++it )
- {
- pup = it.current();
- pup->growOlder();
-
- if( pup->expired() )
- {
- powerups.removeRef( pup );
- continue;
- }
-
- wrapSprite( pup );
-
- QList<QGraphicsItem *> hits = pup->collidingItems();
- QList<QGraphicsItem *>::Iterator it;
- for ( it = hits.begin(); it != hits.end(); ++it )
- {
- if ( (*it) == ship )
- {
- switch( pup->type() )
- {
- case ID_ENERGY_POWERUP:
- shipPower += 150;
- if ( shipPower > MAX_POWER_LEVEL )
- shipPower = MAX_POWER_LEVEL;
- break;
- case ID_TELEPORT_POWERUP:
- mTeleportCount++;
- break;
- case ID_BRAKE_POWERUP:
- if ( mBrakeCount < MAX_BRAKES )
- mBrakeCount++;
- break;
- case ID_SHIELD_POWERUP:
- if ( mShieldCount < MAX_SHIELDS )
- mShieldCount++;
- break;
- case ID_SHOOT_POWERUP:
- if ( mShootCount < MAX_FIREPOWER )
- mShootCount++;
- break;
- }
+ // if player gets the powerup remove it from the screen, if option
+ // "Can destroy powerups" is enabled and a missile hits the powerup
+ // destroy it
+ QList<KPowerup*>::iterator itPup = powerups.begin();
- powerups.removeRef( pup );
- vitalsChanged = TRUE;
- }
- else if ( (*it) == shield )
- {
- powerups.removeRef( pup );
- }
- else if ( (*it)->type() == ID_MISSILE )
- {
- if ( can_destroy_powerups )
- {
- powerups.removeRef( pup );
- }
- }
- }
- }
- } // -- if( powerups.isEmpty() )
+ while(itPup != powerups.end())
+ {
+ (*itPup)->growOlder();
+
+ if((*itPup)->expired())
+ {
+ delete *itPup;
+ itPup = powerups.erase(itPup);
+ continue;
+ }
+
+ wrapSprite(*itPup);
+
+ bool pupErased = false;
+
+ QList<QGraphicsItem *> hits = (*itPup)->collidingItems();
+ for(QList<QGraphicsItem *>::Iterator itHits = hits.begin(); itHits != hits.end(); itHits++)
+ {
+ if ( (*itHits) == ship )
+ {
+ switch( (*itPup)->type() )
+ {
+ case ID_ENERGY_POWERUP:
+ shipPower += 150;
+ if ( shipPower > MAX_POWER_LEVEL )
+ shipPower = MAX_POWER_LEVEL;
+ break;
+ case ID_TELEPORT_POWERUP:
+ mTeleportCount++;
+ break;
+ case ID_BRAKE_POWERUP:
+ if ( mBrakeCount < MAX_BRAKES )
+ mBrakeCount++;
+ break;
+ case ID_SHIELD_POWERUP:
+ if ( mShieldCount < MAX_SHIELDS )
+ mShieldCount++;
+ break;
+ case ID_SHOOT_POWERUP:
+ if ( mShootCount < MAX_FIREPOWER )
+ mShootCount++;
+ break;
+ }
+
+ delete *itPup;
+ itPup = powerups.erase(itPup);
+ pupErased = true;
+ vitalsChanged = TRUE;
+ break;
+ }
+ else if((*itHits) == shield )
+ {
+ delete *itPup;
+ itPup = powerups.erase(itPup);
+ pupErased = true;
+ break;
+ }
+ else if ( (*itHits)->type() == ID_MISSILE )
+ {
+ if ( can_destroy_powerups )
+ {
+ delete *itPup;
+ itPup = powerups.erase(itPup);
+ pupErased = true;
+ break;
+ }
+ }
+ }
+
+ if(!pupErased)
+ itPup++;
+ }
}
// - - -
diff --git a/examples/graphicsview/portedasteroids/view.h b/examples/graphicsview/portedasteroids/view.h
index eeb7e2b..31ae3a0 100644
--- a/examples/graphicsview/portedasteroids/view.h
+++ b/examples/graphicsview/portedasteroids/view.h
@@ -47,13 +47,12 @@
#ifndef __AST_VIEW_H__
#define __AST_VIEW_H__
-#include <qwidget.h>
-#include <q3ptrlist.h>
-#include <q3intdict.h>
-#include <qtimer.h>
+#include <QWidget>
+#include <QList>
+#include <QMultiHash>
+#include <QTimer>
#include <QGraphicsScene>
#include <QGraphicsView>
-//Added by qt3to4:
#include <QTimerEvent>
#include <QShowEvent>
#include <QResizeEvent>
@@ -65,7 +64,7 @@ class KAsteroidsView : public QWidget
{
Q_OBJECT
public:
- KAsteroidsView( QWidget *parent = 0, const char *name = 0 );
+ KAsteroidsView( QWidget *parent = 0);
virtual ~KAsteroidsView();
int refreshRate;
@@ -129,11 +128,11 @@ private:
QGraphicsScene field;
QGraphicsView view;
QMap<int, QList<QPixmap> > animation;
- Q3PtrList<AnimatedPixmapItem> rocks;
- Q3PtrList<KMissile> missiles;
- Q3PtrList<KBit> bits;
- Q3PtrList<KExhaust> exhaust;
- Q3PtrList<KPowerup> powerups;
+ QList<AnimatedPixmapItem*> rocks;
+ QList<KMissile*> missiles;
+ QList<KBit*> bits;
+ QList<KExhaust*> exhaust;
+ QList<KPowerup*> powerups;
KShield *shield;
AnimatedPixmapItem *ship;
QGraphicsTextItem *textSprite;