[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-games-devel
Subject:    [Kde-games-devel] kspaceduel: porting to Graphics View
From:       Dirk <dirkrathlev () gmx ! de>
Date:       2006-12-28 15:02:21
Message-ID: 4593DC7D.40808 () gmx ! de
[Download RAW message or body]

Hello,

in order to get rid of some Qt3 support dependencies, I followed this site
http://doc.trolltech.com/4.2/graphicsview-porting.html
and tried to port kspaceduel to Graphics View. I don't know whether this 
is the correct way to go, so I post the patch here before commiting 
anything.


As animation support is removed from QGraphicsPixmapItem which seems to 
be the replacement for QCanvasPixmap, I took some code from the "Ported 
Asteroids Example" 
http://doc.trolltech.com/4.2/graphicsview-portedasteroids.html
This code is GPL 2. kspaceduel is "GPL 2 or later". Is this a problem? 
And is there maybe alrady existing code which meets better kspaceduel's 
requirements?

In addition I encountered heavy performance problems when using a 
QPixmap as background (field.setBackgroundBrush(QBrush(backgr));), so I 
set the background to pure black. Maybe anyone has a solution for this?

Another problem I encountered and couldn't solve by now is that you can 
clicking on the view. If you do so, the arrow keys are "bound" to the 
view and you can't navigate your ship anymore.

I also removed the "rotation" animation of the spaceships and used 
QGraphicsItem::rotate instead.

Until now, *.ppm are used for the grapgics and *.pbm for transparency. I 
planned to convert graphics to *.png and use png's transparency instead.

I'm thinking about porting kspaceduel to SVG, as this seems the way to 
go for kdegames. This is meant as a first step, although I don't know 
whether it's a correct one.

There is still some porting to Qt4 left and after that, I will have to 
do a lot of clean ups and some refactoring. But wanted to post this first.

Please make objections and hints.

Thank you,
Dirk

["port2graphicsview.diff" (text/x-diff)]

Index: mainview.cpp
===================================================================
--- mainview.cpp	(Revision 616219)
+++ mainview.cpp	(Arbeitskopie)
@@ -39,25 +39,48 @@ This program is free software; you can r
 #include <kiconloader.h>
 #include <kglobal.h>
 
+#include <QBrush>
+#include <qfileinfo.h>
+#include <qdir.h>
+
 #include "ai.h"
 #include "options.h"
 
 KToggleAction *MyMainView::pauseAction = 0;
 
+static struct
+ {
+     int id;
+     const char *path;
+     int frames;
+ }
+ kspd_animations [] =
+ {
+     { ID_SHIP1, "ship1/ship0%1.png", 1 },
+     { ID_EXPLOSION, "explosion/explos%1.ppm", 31},
+     { ID_MINE1, "ship1/mine%1.ppm", 2},
+     { ID_MINE2, "ship2/mine%1.ppm", 2},
+     { ID_MINEEXPLO, "explosion/mineex%1.ppm", 18},
+     { 0, 0, 0 }
+ };
+
 MyMainView::MyMainView(QWidget *parent)
     :QWidget(parent),
-    field(DEF_WIDTH,DEF_HEIGHT),
+    field(this),//0,0,DEF_WIDTH,DEF_HEIGHT),
     view(&field,this)
 {
    int i,p;
    setMinimumSize(600,400);
    random.setSeed(0);
    QPixmap backgr(KStandardDirs::locate("appdata", MV_BACKGROUND));
-   field.setBackgroundPixmap(backgr);
+   
+   // FIXME: too slow with pixmap..
+   field.setBackgroundBrush(Qt::black);//QBrush(backgr));
 
-   view.setResizePolicy(Q3ScrollView::AutoOne);
-   view.setHScrollBarMode(Q3ScrollView::AlwaysOff);
-   view.setVScrollBarMode(Q3ScrollView::AlwaysOff);
+   // FIXME: is this needed anymore?
+   //view.setResizePolicy(Q3ScrollView::AutoOne);
+   view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+   view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
    for(p=0;p<2;p++)
    {
@@ -68,42 +91,27 @@ MyMainView::MyMainView(QWidget *parent)
    }
 
    QString tmp = KGlobal::dirs()->findResourceDir("appdata", \
                (QString)MV_BACKGROUND);
-
-   Q3CanvasPixmapArray *sunsequence
-      = loadOldPixmapSequence( tmp + MV_SUN_PPM, tmp + MV_SUN_PBM );
-   sun=new SunSprite(sunsequence, &field);
-   sun->move(width()/2-1,height()/2-1);
-   sun->show();
-
-   explosionsequence = loadOldPixmapSequence( tmp + MV_EXPLOSION_PPM,
-                                              tmp + MV_EXPLOSION_PBM, 31 );
-   mineexplosionsequence = loadOldPixmapSequence(tmp + MV_MINEEX_PPM,
-                                                 tmp + MV_MINEEX_PBM, 18 );
-
-   shipsequence[0] = loadOldPixmapSequence( tmp + MV_SHIP1_PPM,
-                                            tmp + MV_SHIP1_PBM, ROTNUM );
-   shipsequence[1] = loadOldPixmapSequence( tmp + MV_SHIP2_PPM,
-                                            tmp + MV_SHIP2_PBM, ROTNUM);
-   ship[0]=new ShipSprite(shipsequence[0],&field,0);
-   ship[1]=new ShipSprite(shipsequence[1],&field,1);
-
-   bulletsequence[0] = loadOldPixmapSequence( tmp + MV_BULLET1_PPM,
-                                              tmp + MV_BULLET1_PBM );
-   bulletsequence[1] = loadOldPixmapSequence( tmp + MV_BULLET2_PPM,
-                                              tmp + MV_BULLET2_PBM );
-   minesequence[0] = loadOldPixmapSequence( tmp + MV_MINE1_PPM,
-                                            tmp + MV_MINE1_PBM, 2);
-   minesequence[1] = loadOldPixmapSequence( tmp + MV_MINE2_PPM,
-                                            tmp + MV_MINE2_PBM, 2);
-   powerupsequence[PowerupSprite::PowerupMine]
-      = loadOldPixmapSequence( tmp + MV_POWERMINE_PPM, tmp + MV_POWERMINE_PBM );
-   powerupsequence[PowerupSprite::PowerupBullet]
-      = loadOldPixmapSequence( tmp + MV_POWERBULLET_PPM, tmp + MV_POWERBULLET_PBM );
-   powerupsequence[PowerupSprite::PowerupShield]
-      = loadOldPixmapSequence( tmp + MV_POWERSHIELD_PPM, tmp + MV_POWERSHIELD_PBM );
-   powerupsequence[PowerupSprite::PowerupEnergy]
-      = loadOldPixmapSequence( tmp + MV_POWERENERGY_PPM, tmp + MV_POWERENERGY_PBM );
-
+   
+   sunpixmap = new QPixmap(tmp + MV_SUN_PPM);
+   sun=new SunSprite(sunpixmap, &field);
+   // FIXME: don't use fixed sun size (75x75)
+   sun->setPos(QPointF(width()/2-1-37,height()/2-1-37));
+   
+   shippixmap[0] = new QPixmap(tmp + MV_SHIP1_PPM);
+   shippixmap[1] = new QPixmap(tmp + MV_SHIP2_PPM);
+   ship[0]=new ShipSprite(shippixmap[0],&field,0);
+   ship[1]=new ShipSprite(shippixmap[1],&field,1);
+   
+   bulletpixmap[0] = new QPixmap(tmp + MV_BULLET1_PPM);
+   bulletpixmap[1] = new QPixmap(tmp + MV_BULLET2_PPM);
+   
+   poweruppixmap[0] = new QPixmap( tmp + MV_POWERMINE_PPM);
+   poweruppixmap[1] = new QPixmap( tmp + MV_POWERBULLET_PPM);
+   poweruppixmap[2] = new QPixmap( tmp + MV_POWERSHIELD_PPM);
+   poweruppixmap[3] = new QPixmap( tmp + MV_POWERENERGY_PPM);
+      
+   readSprites();
+   
    for(i=0;i<2;i++)
    {
       // ship[i]->setBoundsAction(QwRealMobileSprite::Wrap);
@@ -114,6 +122,7 @@ MyMainView::MyMainView(QWidget *parent)
       mines[i]->setAutoDelete(true);
 
    }
+   
 
    explosions.setAutoDelete(true);
    powerups.setAutoDelete(true);
@@ -134,6 +143,30 @@ void MyMainView::setActionCollection(KAc
    actionCollection = a;
 }
 
+ bool MyMainView::readSprites()
+ {
+     QString sprites_prefix = KGlobal::dirs()->findResourceDir("appdata", \
(QString)MV_BACKGROUND) + "sprites/"; +
+     int i = 0;
+     while ( kspd_animations[i].id )
+     {
+         QList<QPixmap> anim;
+         QString wildcard = sprites_prefix + kspd_animations[i].path;
+         wildcard.replace("%1", "*");
+         QFileInfo fi(wildcard);
+         foreach (QString entry, QDir(fi.path(), fi.fileName()).entryList())
+             anim << QPixmap(fi.path() + "/" + entry);
+	 
+	 foreach (QString entry, QDir(fi.path(), fi.fileName()).entryList())
+             kDebug() << fi.path() << "/" << entry << endl;
+         animation.insert( kspd_animations[i].id, anim );
+         i++;
+     }
+
+     // FixMe: test!
+     return true;
+ }
+
 void MyMainView::readConfig()
 {
    KConfigGroup game(KGlobal::config(), "Game");
@@ -420,15 +453,17 @@ void MyMainView::resizeEvent(QResizeEven
    my=(event->size().height()-event->oldSize().height())/2.0;
    QWidget::resizeEvent(event);
    view.resize(width(),height());
-   field.resize(width(),height());
+   field.setSceneRect(0, 0, width(),height());
 
    // printf("%d %d\n",field.width(),field.height());
-   sun->move(width()/2-1,height()/2-1);
+
+   // FIXME don't use fixed sun size (75x75)
+   sun->setPos(QPointF(width()/2-1-37,height()/2-1-37));
 
    for(i=0;i<2;i++)
    {
       // ship[i]->adoptSpritefieldBounds();
-      ship[i]->moveBy(mx,my);
+      ship[i]->setPos(QPointF(mx,my));
       current=mines[i]->at();
       for(mine=mines[i]->first();mine;mine=mines[i]->next())
       {
@@ -450,6 +485,7 @@ void MyMainView::resizeEvent(QResizeEven
    }
    if(textSprite)
       textSprite->moveBy((int)mx,(int)my);
+   
    current=powerups.at();
    for(powerup=powerups.first();powerup;powerup=powerups.next())
       powerup->moveBy(mx,my);
@@ -468,13 +504,11 @@ void MyMainView::newRound()
    QAbstractEventDispatcher::instance()->unregisterTimers(this);
    mx=width()/2.0;
    my=height()/2.0;
-   ship[0]->move(mx+config.startPosX,my+config.startPosY);
+   ship[0]->setPos(QPointF(mx+config.startPosX,my+config.startPosY));
    ship[0]->setRotation(0.0);
-   ship[0]->setFrame(0);
 
-   ship[1]->move(mx-config.startPosX,my-config.startPosY);
+   ship[1]->setPos(QPointF(mx-config.startPosX,my-config.startPosY));
    ship[1]->setRotation(M_PI);
-   ship[1]->setFrame(ROTNUM/2);
 
    ship[0]->setVelocity(config.startVelX,config.startVelY);
    ship[1]->setVelocity(-config.startVelX,-config.startVelY);
@@ -484,7 +518,7 @@ void MyMainView::newRound()
       ship[i]->setEnergy(MAX_ENERGY);
       ship[i]->setHitPoints(Options::startHitPoints(i));
       ship[i]->stop(false);
-      ship[i]->setExplosion(-1);
+    //  ship[i]->setExplosion(-1);
       emit(energy(i,(int)ship[i]->getEnergy()));
       emit(hitPoints(i,ship[i]->getHitPoints()));
       bulletShot[i]=false;
@@ -536,6 +570,7 @@ void MyMainView::timerEvent(QTimerEvent 
    int i;
    bool stopped = false;
 
+   //kDebug() << ship[0]->getHitPoints() << "/" << ship[1]->getHitPoints() << endl;
    if(event->timerId()==timerID)
    {
       QAbstractEventDispatcher::instance()->unregisterTimers(this);
@@ -551,11 +586,11 @@ void MyMainView::timerEvent(QTimerEvent 
                delete textSprite;
                textSprite=0;
             }
-
-            textSprite=new Q3CanvasText(&field);
-            textSprite->move(width()/2,height()/2-90);
-            textSprite->setTextFlags(Qt::AlignCenter);
-            textSprite->setColor(qRgb(255,160,0));
+            textSprite=new QGraphicsSimpleTextItem(0,&field);
+	    // FIXME
+            textSprite->setPos(QPointF(width()/2,height()/2-90));
+           // textSprite->setTextFlags(Qt::AlignCenter);
+	    textSprite->setPen(QPen(QColor(255,160,0)));
             textSprite->setFont(QFont(KGlobalSettings::generalFont().family(),14));
             textSprite->show( );
             if(ship[0]->getHitPoints()==0)
@@ -639,8 +674,7 @@ void MyMainView::moveShips()
          en=ship[i]->getEnergy();
          nr=ship[i]->getRotation();
 
-
-         nf=ship[i]->frame();
+         nf = 0;
          nx=cos(nr);
          ny=sin(nr);
          if ( ((!playerIsAi && playerKeyPressed[i][PlayerKeyAcc])
@@ -666,6 +700,7 @@ void MyMainView::moveShips()
             en=MAX_ENERGY;
 
          ship[i]->forward(config.gamespeed);
+	 
              //Bullets and Mines
          if(!playerIsAi&&playerKeyPressed[i][PlayerKeyShot]
             ||playerIsAi&&ai[i]->shootBullet())
@@ -677,15 +712,15 @@ void MyMainView::moveShips()
                {
                   ship[i]->bullet(config.bulletReloadTime);
                   en-=config.shotEnergyNeed;
-                  bullet=new BulletSprite(bulletsequence[i],&field,i,
+                  bullet=new BulletSprite(bulletpixmap[i],&field,i,
                                           config.bulletLifeTime);
-                  bullet->move(ship[i]->x()+nx*SHOTDIST,
-                                 ship[i]->y()-ny*SHOTDIST);
+		  QPointF p;
+		  p = ship[i]->mapToScene(QPointF(11.5,17));
+		  bullet->setPos(QPointF(p.x()+nx*SHOTDIST,p.y()-ny*SHOTDIST));
                   bullet->setVelocity(ship[i]->xVelocity()+nx*config.shotSpeed,
                                       ship[i]->yVelocity()-ny*config.shotSpeed);
                   // bullet->setBoundsAction(QwRealMobileSprite::Wrap);
                   bullet->show();
-
                   bullets[i]->append(bullet);
                }
             }
@@ -700,9 +735,14 @@ void MyMainView::moveShips()
                {
                   ship[i]->mine(config.mineReloadTime);
                   en-=config.mineEnergyNeed;
-                  mine=new MineSprite(minesequence[i],&field,i,
+		  if (i==0)
+                     mine=new \
MineSprite(animation[ID_MINE1],animation[ID_MINEEXPLO],&field,i, +                    \
config.mineActivateTime,config.mineFuel); +		  else
+		     mine=new MineSprite(animation[ID_MINE2],animation[ID_MINEEXPLO],&field,i,
                                       config.mineActivateTime,config.mineFuel);
-                  mine->move(ship[i]->x(),ship[i]->y());
+		  QPointF p;
+		  mine->setPos(ship[i]->mapToScene(QPointF(4,10.5)));
                   mine->setVelocity(0,0);
                   //mine->setBoundsAction(QwRealMobileSprite::Wrap);
                   mine->show();
@@ -804,7 +844,7 @@ void MyMainView::calculatePowerups()
    {
       timeToNextPowerup= random.getDouble() * config.powerupRefreshTime;
       type= random.getLong(PowerupSprite::PowerupNum);
-      sp=new PowerupSprite(powerupsequence[type],&field,type,
+     sp=new PowerupSprite(poweruppixmap[type],&field,type,
                            config.powerupLifeTime);
       do
       {
@@ -812,7 +852,7 @@ void MyMainView::calculatePowerups()
          y = random.getLong(height()-40)+20;
       }
       while(((x-width()/2)*(x-width()/2)+(y-height()/2)*(y-height()/2))<(50*50));
-      sp->move(x,y);
+      sp->setPos(QPointF(x,y));
       powerups.append(sp);
       sp->show();
    }
@@ -821,32 +861,34 @@ void MyMainView::calculatePowerups()
 void MyMainView::collisions()
 {
    int pl,hp,op,oldhp[2],ohp;
-   Q3CanvasItemList unexact;
-   Q3CanvasItem *sprite;
+   QList<QGraphicsItem *> unexact;
+   QGraphicsItem *sprite;
    BulletSprite *bullet;
    MineSprite *mine;
    ExplosionSprite *expl;
    ShipSprite *s;
    PowerupSprite *power;
-   Q3CanvasItemList hitlist;
+   QList<QGraphicsItem *> hitlist;
    double ndx[2],ndy[2];
    double en;
-   Q3CanvasItemList::Iterator it;
+   QList<QGraphicsItem *>::Iterator it;
 
    for(pl=0;pl<2;pl++)
    {
       if(!ship[pl]->isStopped())
       {
          unexact.clear();
-         unexact=ship[pl]->collisions(false);
+	 // FIXME: is this ported correctly?
+	 unexact = ship[pl]->collidingItems(Qt::IntersectsItemBoundingRect);
+        // unexact=ship[pl]->collisions(false);
          oldhp[pl]=hp=ship[pl]->getHitPoints();
          hitlist.clear();
          for(it=unexact.begin(); it != unexact.end(); ++it)
          {
             sprite = (*it);
-            if((sprite->rtti()!=S_EXPLOSION)
-               && !((sprite->rtti()!=S_SUN)&&(ship[pl]->getHitPoints()==0)))
-               if(ship[pl]->collidesWith(sprite))
+            if((sprite->type()!=S_EXPLOSION)
+               && !((sprite->type()!=S_SUN)&&(ship[pl]->getHitPoints()==0)))
+               if(ship[pl]->collidesWithItem(sprite))
                   if(!hitlist.contains(sprite))
                      hitlist.append(sprite);
          }
@@ -854,7 +896,7 @@ void MyMainView::collisions()
          for(it=hitlist.begin(); it != hitlist.end(); ++it)
          {
             sprite = (*it);
-            switch(sprite->rtti())
+            switch(sprite->type())
             {
                case S_SUN:
                   hp=0;
@@ -886,7 +928,7 @@ void MyMainView::collisions()
                   mine=(MineSprite *)sprite;
                   if(mine->isActive()&& !mine->explodes())
                   {
-                     mine->explode(mineexplosionsequence);
+                     mine->explode();
                      ndx[0]=(ship[pl]->xVelocity()+0.3*mine->xVelocity())/1.3;
                      ndy[0]=(ship[pl]->yVelocity()+0.3*mine->yVelocity())/1.3;
                      ship[pl]->setVelocity(ndx[0],ndy[0]);
@@ -931,19 +973,19 @@ void MyMainView::collisions()
          if(!mine->explodes())
          {
             unexact.clear();
-            unexact=mine->collisions(false);
+            unexact=mine->collidingItems(Qt::IntersectsItemBoundingRect);
             hitlist.clear();
             for( it=unexact.begin(); it != unexact.end(); ++it )
             {
                sprite = (*it);
-               if(sprite->rtti()==S_BULLET)
-                  if(mine->collidesWith(sprite))
+               if(sprite->type()==S_BULLET)
+                  if(mine->collidesWithItem(sprite))
                      if(!hitlist.contains(sprite))
                         hitlist.append(sprite);
             }
             if(hitlist.count()>0)
             {
-               mine->explode(mineexplosionsequence);
+               mine->explode();
                for(it=hitlist.begin(); it != hitlist.end(); ++it)
                {
                   bullet=(BulletSprite*)(*it);
@@ -956,20 +998,20 @@ void MyMainView::collisions()
 
    hitlist.clear();
    unexact.clear();
-   unexact=sun->collisions(false);
+   unexact=sun->collidingItems(Qt::IntersectsItemBoundingRect);
    for( it = unexact.begin(); it != unexact.end(); ++it)
    {
       sprite=(*it);
-      switch(sprite->rtti())
+      switch(sprite->type())
       {
          case S_BULLET:
-            if(sun->collidesWith(sprite))
+            if(sun->collidesWithItem(sprite))
                if(!hitlist.contains(sprite))
                   hitlist.append(sprite);
             break;
          case S_MINE:
             if(!((MobileSprite*)sprite)->isStopped())
-               if(sun->collidesWith(sprite))
+               if(sun->collidesWithItem(sprite))
                   if(!hitlist.contains(sprite))
                      hitlist.append(sprite);
             break;
@@ -979,7 +1021,7 @@ void MyMainView::collisions()
    for(it=hitlist.begin(); it != hitlist.end(); ++it)
    {
       sprite=(*it);
-      switch(sprite->rtti())
+      switch(sprite->type())
       {
          case S_BULLET:
             bullet=(BulletSprite *)sprite;
@@ -990,7 +1032,7 @@ void MyMainView::collisions()
             mine=(MineSprite*)sprite;
             mine->stop();
             if(!mine->explodes())
-               mine->explode(mineexplosionsequence);
+               mine->explode();//mineexplosionsequence);
             break;
       }
    }
@@ -1005,7 +1047,7 @@ void MyMainView::collisions()
       {
          op=(pl+1)%2;
          ship[pl]->setExplosion((int)(EXPLOSION_TIME/config.gamespeed));
-         expl=new ExplosionSprite(explosionsequence,&field,ship[pl]);
+	 expl = new ExplosionSprite(animation[ID_EXPLOSION],&field,ship[pl]);
          expl->show();
          explosions.append(expl);
          gameEnd=Options::timeAfterKill()/config.gamespeed;
@@ -1033,52 +1075,4 @@ void MyMainView::closeSettings(){
     config=modifyConfig(customConfig);
 }
 
-Q3CanvasPixmapArray* MyMainView::loadOldPixmapSequence(const QString& datapattern,
-                            const QString& maskpattern, int framecount)
-{
-   int image;
-   Q3PtrList<QPixmap> pixmaplist;
-   Q3PtrList<QPoint> pointlist;
-   QString dataname, maskname;
-   QPixmap *pix;
-   QBitmap *bitmap;
-   int hotx=0, hoty=0;
-   QPoint *point;
-
-   for( image=0; image < framecount; image++ )
-   {
-      // #### Why is this a QString??
-      dataname.sprintf( datapattern.toLatin1().constData(), image );
-      maskname.sprintf( maskpattern.toLatin1().constData(), image );
-
-      QFile file(dataname);
-      if( file.open( QIODevice::ReadOnly ) )
-      {
-         char line[128];
-         file.readLine( line, 128 ); // Skip "P6"/"P3" line
-         file.readLine( line, 128 );
-
-         while ( line[0] == '#' )
-         {
-            // Comment line - see if it has additional parameters
-            if ( 0 == strncmp( line,"# HOTSPOT ", 10 ) )
-               sscanf( line+10, "%d %d", &hotx, &hoty);
-
-            file.readLine( line, 128 );
-         }
-         point = new QPoint( hotx, hoty );
-         pointlist.append( point );
-      }
-
-      pix = new QPixmap( dataname );
-      bitmap = new QBitmap( maskname );
-      pix->setMask( *bitmap );
-
-      pixmaplist.append( pix );
-   }
-
-   Q3CanvasPixmapArray* newarray = new Q3CanvasPixmapArray( pixmaplist, pointlist );
-   return newarray;
-}
-
 #include "mainview.moc"
Index: mainview.h
===================================================================
--- mainview.h	(Revision 616217)
+++ mainview.h	(Arbeitskopie)
@@ -19,12 +19,17 @@ This program is free software; you can r
 #define __MY_MAIN_VIEW_H
 
 #include <QWidget>
-#include <qimage.h>
 //Added by qt3to4:
 #include <QTimerEvent>
 #include <QResizeEvent>
 #include <QKeyEvent>
 #include <Q3PtrList>
+#include <QLinkedList>
+
+#include <QGraphicsScene>
+#include <QGraphicsView>
+
+#include <QGraphicsSimpleTextItem>
 
 class KToggleAction;
 class KActionCollection;
@@ -47,6 +52,7 @@ public:
 
    static KToggleAction *pauseAction;
    void setActionCollection(KActionCollection *a);
+   bool readSprites();
 
 public slots:
    void newRound();
@@ -72,8 +78,6 @@ protected:
    virtual void keyPressEvent(QKeyEvent *event);
    virtual void keyReleaseEvent(QKeyEvent *event);
    SConfig modifyConfig(SConfig conf);
-   Q3CanvasPixmapArray* loadOldPixmapSequence(const QString& datapattern,
-                           const QString& maskpattern, int framecount=1);
    void moveShips();
    void moveBullets();
    void moveMines();
@@ -83,8 +87,8 @@ protected:
 private:
    KActionCollection *actionCollection;
 
-   Q3Canvas field;
-   Q3CanvasView view;
+   QGraphicsScene field;
+   QGraphicsView view;
 
    SConfig customConfig,config;
 
@@ -99,18 +103,17 @@ private:
 // sprites
    Q3PtrList<QImage> shipImages;
    Q3PtrList<QPoint> points;
-   QImage bulletImage;
-   Q3CanvasPixmapArray *bulletsequence[2];
-   Q3CanvasPixmapArray *shipsequence[2];
-   Q3CanvasPixmapArray *explosionsequence;
-   Q3CanvasPixmapArray *minesequence[2];
-   Q3CanvasPixmapArray *mineexplosionsequence;
-   Q3CanvasPixmapArray *powerupsequence[PowerupSprite::PowerupNum];
-
+   
+   QPixmap *sunpixmap;
+   QPixmap *shippixmap[2];
+   QPixmap *bulletpixmap[2];
+   QPixmap *poweruppixmap[PowerupSprite::PowerupNum];
+   
+   QMap<int, QList<QPixmap> > animation;
 
    ShipSprite *ship[2];
    SunSprite *sun;
-   Q3CanvasText *textSprite;
+   QGraphicsSimpleTextItem *textSprite;
    Q3PtrList<BulletSprite> *bullets[2];
    Q3PtrList<MineSprite> *mines[2];
    Q3PtrList<ExplosionSprite> explosions;
Index: sprites.cpp
===================================================================
--- sprites.cpp	(Revision 616217)
+++ sprites.cpp	(Arbeitskopie)
@@ -18,27 +18,37 @@ This program is free software; you can r
 #include "sprites.h"
 #include "mathroutines.h"
 #include <math.h>
+#include <kdebug.h>
 
+#include "mainview.h"
 
-SunSprite::SunSprite(Q3CanvasPixmapArray *seq, Q3Canvas* canvas)
-      :Q3CanvasSprite(seq, canvas)
+
+SunSprite::SunSprite(QPixmap* pixmap, QGraphicsScene* scene)
+      :QGraphicsPixmapItem(*pixmap, 0, scene)
 {
     // doesn't work with Qt 2.2.2 anymore
     // setZ(0);
 }
 
 
-PowerupSprite::PowerupSprite(Q3CanvasPixmapArray* seq, Q3Canvas* canvas, int t,
+PowerupSprite::PowerupSprite(QPixmap* pixmap, QGraphicsScene* scene, int t,
                              double lifetime)
-      :Q3CanvasSprite(seq, canvas)
+      :QGraphicsPixmapItem(*pixmap, 0, scene)
 {
    time=lifetime;
-   type=t;
+   mtype=t;
 }
 
 
-MobileSprite::MobileSprite(Q3CanvasPixmapArray* seq, Q3Canvas* canvas, int pn)
-      :Q3CanvasSprite(seq, canvas)
+MobileSprite::MobileSprite(QPixmap* pixmap, QGraphicsScene* scene, int pn)
+      :QGraphicsPixmapItem(*pixmap, 0, scene)
+{
+   stopped=false;
+   playerNumber=pn;
+}
+
+MobileSprite::MobileSprite(QGraphicsItem * parent, QGraphicsScene * scene, int pn)
+      :QGraphicsPixmapItem(parent, scene)
 {
    stopped=false;
    playerNumber=pn;
@@ -48,19 +58,20 @@ void MobileSprite::forward(double mult, 
 {
    if(!stopped)
    {
-      Q3CanvasSprite::moveBy(xVelocity()*mult,yVelocity()*mult);
+      QGraphicsPixmapItem::moveBy(xVelocity()*mult,yVelocity()*mult);
       checkBounds();
-      setFrame(fr);
+      // FIXME
+      //setFrame(fr);
    }
-   else
-      setFrame(fr);
+  // else
+    //  setFrame(fr);
 }
 
 void MobileSprite::forward(double mult)
 {
    if(!stopped)
    {
-      Q3CanvasSprite::moveBy(xVelocity()*mult,yVelocity()*mult);
+      QGraphicsPixmapItem::moveBy(xVelocity()*mult,yVelocity()*mult);
       checkBounds();
    }
 }
@@ -72,8 +83,8 @@ void MobileSprite::checkBounds()
 
    cx = x();
    cy = y();
-   ch = canvas()->height();
-   cw = canvas()->width();
+   ch = (int)scene()->height();
+   cw = (int)scene()->width();
 
    if ( (int)(cx+0.5) < 0 )
       cx = cw - 1 - fmod( -cx, cw );
@@ -86,7 +97,7 @@ void MobileSprite::checkBounds()
    if ( (cx != x()) || (cy != y()) )
    {
       // printf("%5.2f %5.2f %5.2f %5.2f\n", x(), y(), cx, cy);
-      move( cx, cy );
+      setPos(QPointF(cx, cy));
    }
 }
 
@@ -96,8 +107,8 @@ void MobileSprite::calculateGravity(doub
 
    if(!stopped)
    {
-      ex=x()-canvas()->width()/2.0;
-      ey=y()-canvas()->height()/2.0;
+      ex=x()-scene()->width()/2.0;
+      ey=y()-scene()->height()/2.0;
 
       abs_2=ex*ex+ey*ey;
       sq=sqrt(abs_2);
@@ -110,13 +121,19 @@ void MobileSprite::calculateGravity(doub
    }
 }
 
+void MobileSprite::setVelocity(double vx, double vy)
+{
+	xvel = vx;
+	yvel = vy;
+}
+
 AiSprite MobileSprite::toAiSprite()
 {
        // y direction: screen:       bottom to top
        //              mathematical: top to bottom
    AiSprite as;
-   as.x=x()-canvas()->width()/2.0;
-   as.y=-(y()-canvas()->height()/2.0);
+   as.x=x()-scene()->width()/2.0;
+   as.y=-(y()-scene()->height()/2.0);
    as.dx=xVelocity();
    as.dy=-yVelocity();
    as.sun=false;
@@ -124,29 +141,75 @@ AiSprite MobileSprite::toAiSprite()
    return as;
 }
 
-ShipSprite::ShipSprite(Q3CanvasPixmapArray* seq, Q3Canvas* canvas, int pn)
-      :MobileSprite(seq,canvas,pn)
+AnimatedSprite::AnimatedSprite(const QList<QPixmap> &animation,
+                                        QGraphicsScene *scene, int pn)
+    : MobileSprite(0, scene)
 {
-   hitpoints=99;
-   energy=99.9;
+	frames = animation;
+	currentFrame = 0;
+	setFrame(0);
+}
+
+void AnimatedSprite::setFrame(int frame)
+{
+    if (!frames.isEmpty()) {
+        currentFrame = frame % frames.size();
+        setPixmap(frames.at(currentFrame));
+    }
+}
+
+void AnimatedSprite::advance(int phase)
+{
+    if (phase == 1 && !frames.isEmpty()) {
+        currentFrame = (currentFrame + 1) % frames.size();
+        setPixmap(frames.at(currentFrame));
+    }
+}
+
+QPainterPath AnimatedSprite::shape() const
+{
+    QPainterPath path;
+    path.addRect(0, 0,
+                 frames.at(currentFrame).width(),
+                 frames.at(currentFrame).height());
+    return path;
+}
+
+void AnimatedSprite::setAnimation(const QList<QPixmap> &animation)
+{
+	frames = animation;
+	setFrame(0);
+}
+
+ShipSprite::ShipSprite(QPixmap* pixmap, QGraphicsScene* scene, int pn)
+      :MobileSprite(pixmap,scene,pn)
+{
+   hitpoints=MAX_HP;
+   energy=MAX_ENERGY;
    explosion=-1;
-   setZ(-20);
+   setZValue(-20);
    rotation=0;
    bulletPowerups=0;
    minePowerups=0;
+   angle = 0;
 }
 
 void ShipSprite::setRotation(double r)
 {
-   int nf,of=frame();
+   //int nf,of=frame();
    rotation=r;
    if(rotation<0)
       rotation-=((int)(rotation/(2*M_PI))-1)*2*M_PI;
    if(rotation>=2*M_PI)
       rotation-=(int)(rotation/(2*M_PI))*2*M_PI;
-   nf=(int)(rotation/(2*M_PI)*ROTNUM)%ROTNUM;
-   if(nf!=of)
-      setFrame(nf);
+  // nf=(int)(rotation/(2*M_PI)*ROTNUM)%ROTNUM;
+  //  if(nf!=of)
+  //    setFrame(nf);*/
+   translate(10.5,17);
+   rotate(-(rotation-angle)*57.3);
+   //kDebug() << rotation-angle << endl;
+   translate(-10.5,-17);
+   angle = rotation;
 }
 
 void ShipSprite::forward(double mult)
@@ -187,8 +250,8 @@ void ShipSprite::calculateGravityAndEner
 
    if(!stopped)
    {
-      ex=x()-canvas()->width()/2.0;
-      ey=y()-canvas()->height()/2.0;
+      ex=x()-scene()->width()/2.0;
+      ey=y()-scene()->height()/2.0;
 
       abs_2=ex*ex+ey*ey;
       sq=sqrt(abs_2);
@@ -232,10 +295,10 @@ void ShipSprite::rotateLeft(double rotat
    }
 }
 
-BulletSprite::BulletSprite(Q3CanvasPixmapArray* seq,Q3Canvas* canvas, int pn,double \
                lifetime)
-      :MobileSprite(seq,canvas,pn)
+BulletSprite::BulletSprite(QPixmap* pixmap, QGraphicsScene* scene, int pn,double \
lifetime) +      :MobileSprite(pixmap,scene,pn)
 {
-   setZ(-10);
+   setZValue(-10);
    time=lifetime;
 }
 
@@ -251,11 +314,12 @@ void BulletSprite::forward(double mult,i
    time-=mult;
 }
 
-MineSprite::MineSprite(Q3CanvasPixmapArray* seq, Q3Canvas* canvas, int pn,double \
                atime,double f)
-      :MobileSprite(seq,canvas,pn)
+MineSprite::MineSprite(const QList<QPixmap> &animation, const QList<QPixmap> \
&exploanimation, QGraphicsScene* scene, int pn,double atime,double f) +      \
:AnimatedSprite(animation,scene,pn)  {
+   exploframes = exploanimation;
    activateTime=atime;
-   setZ(-25);
+   setZValue(-25);
    fuel=f;
    explosiontime=0;
    timeToGo=0.0;
@@ -263,14 +327,13 @@ MineSprite::MineSprite(Q3CanvasPixmapArr
    active=false;
 }
 
-void MineSprite::explode(Q3CanvasPixmapArray *seq)
+void MineSprite::explode()//Q3CanvasPixmapArray *seq)
 {
-   setSequence(seq);
-   timeToGo=seq->count();
+   setAnimation(exploframes);
+   timeToGo=frameCount();
    expl=true;
-   setFrame(0);
    explosiontime=0.0;
-   setZ(-8);
+   setZValue(-8);
    setFuel(0.0);
 }
 
@@ -305,8 +368,8 @@ void MineSprite::calculateGravity(double
 
    if(!stopped)
    {
-      ex=x()-canvas()->width()/2.0;
-      ey=y()-canvas()->height()/2.0;
+      ex=x()-scene()->width()/2.0;
+      ey=y()-scene()->height()/2.0;
 
       abs_2=ex*ex+ey*ey;
       sq=sqrt(abs_2);
@@ -321,22 +384,22 @@ void MineSprite::calculateGravity(double
    }
 }
 
-ExplosionSprite::ExplosionSprite(Q3CanvasPixmapArray* seq, Q3Canvas* canvas, \
                MobileSprite *sp)
-      :Q3CanvasSprite(seq, canvas)
+ExplosionSprite::ExplosionSprite(const QList<QPixmap> &animation, QGraphicsScene \
*scene, MobileSprite *sp) +      :AnimatedSprite(animation, scene)
 {
    over=false;
-   setZ(-5);
+   setZValue(-5);
    obj=sp;
-   timeToGo=seq->count();
+   timeToGo = frameCount();
    time=0;
 
-   move(sp->x(),sp->y());
+   setPos(QPointF(sp->x(),sp->y()));
 }
 
 void ExplosionSprite::forward(double mult)
 {
    int of=frame();
-   move(obj->x(),obj->y());
+   setPos(QPointF(obj->x(),obj->y()));
    time+=mult;
 
    if(time>=timeToGo)
@@ -348,10 +411,3 @@ void ExplosionSprite::forward(double mul
       if((int)time!=of)
          setFrame((int)time);
 }
-
-
-void ExplosionSprite::setSequence(Q3CanvasPixmapArray *seq)
-{
-   timeToGo=seq->count();
-   Q3CanvasSprite::setSequence(seq);
-}
Index: sprites.h
===================================================================
--- sprites.h	(Revision 616217)
+++ sprites.h	(Arbeitskopie)
@@ -18,7 +18,9 @@ This program is free software; you can r
 #ifndef __SPRITE_OBJECTS_H
 #define __SPRITE_OBJECTS_H
 
-#include <q3canvas.h>
+#include <QGraphicsScene>
+#include <QGraphicsPixmapItem>
+
 #include "defines.h"
 
 #ifdef sun
@@ -31,57 +33,84 @@ struct AiSprite
    bool sun, border;
 };
 
-class SunSprite:public Q3CanvasSprite
+class SunSprite:public QGraphicsPixmapItem
 {
 public:
-   SunSprite(Q3CanvasPixmapArray* seq, Q3Canvas* canvas);
-   virtual int rtti() const {return S_SUN;}
+   SunSprite(QPixmap* pixmap, QGraphicsScene* scene);
+   virtual int type() const {return S_SUN;}
 };
 
-
-class PowerupSprite:public Q3CanvasSprite
+class PowerupSprite:public QGraphicsPixmapItem
 {
 public:
    enum {PowerupMine=0, PowerupBullet, PowerupShield, PowerupEnergy,
          PowerupNum};
-   PowerupSprite(Q3CanvasPixmapArray* seq, Q3Canvas* canvas, int t, double \
                lifetime);
-   virtual int rtti() const {return S_POWERUP;}
+   PowerupSprite(QPixmap* pixmap, QGraphicsScene* scene, int t, double lifetime);
+   virtual int type() const {return S_POWERUP;}
 
    double getLifetime() {return time;}
    void setLifetime(double t) {time=t;}
-   int getType() {return type;}
+   int getType() {return mtype;}
 private:
    double time;
-   int type;
+   int mtype;
 };
 
-class MobileSprite:public Q3CanvasSprite
+class MobileSprite:public QGraphicsPixmapItem
 {
 public:
-   MobileSprite(Q3CanvasPixmapArray* array, Q3Canvas* canvas, int pn);
+   MobileSprite(QPixmap* pixmap, QGraphicsScene* scene, int pn);
 
    virtual void forward(double mult,int frame);
    virtual void forward(double mult);
    virtual void calculateGravity(double gravity,double mult);
-   int spriteFieldWidth(){return canvas()->width();}
-   int spriteFieldHeight(){return canvas()->height();}
+   int spriteFieldWidth(){return (int)scene()->width();}
+   int spriteFieldHeight(){return (int)scene()->height();}
+   double xVelocity(){return xvel;}
+   double yVelocity(){return yvel;}
+   void setVelocity(double vx, double vy);
    AiSprite toAiSprite();
 
    bool isStopped() {return stopped;}
    void stop(bool s=true) {stopped=s;}
    int getPlayerNumber() {return playerNumber;}
 protected:
+   MobileSprite(QGraphicsItem* parent = 0, QGraphicsScene * scene = 0, int pn = 0);
+	
    void checkBounds();
    bool stopped;
    int playerNumber;
+   double xvel;
+   double yvel;
 };
 
+class AnimatedSprite:public MobileSprite
+{
+public:
+   AnimatedSprite(const QList<QPixmap> &animation, QGraphicsScene *scene = 0, int \
pn=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()); }
+   QPainterPath shape() const;
+   void setAnimation(const QList<QPixmap> &animation);
+
+   void advance(int phase);
+
+private:
+   int currentFrame;
+   QList<QPixmap> frames;
+};
 
 class ShipSprite:public MobileSprite
 {
 public:
-   ShipSprite(Q3CanvasPixmapArray* seq, Q3Canvas* canvas, int pn);
-   virtual int rtti() const {return S_SHIP;}
+   ShipSprite(QPixmap* pixmap, QGraphicsScene* scene, int pn);
+   virtual int type() const {return S_SHIP;}
    int getHitPoints() {return hitpoints;}
    void setHitPoints(int hp) {hitpoints=(hp<0?0:hp);}
    double getEnergy() {return energy;}
@@ -111,13 +140,14 @@ private:
    int hitpoints, wins, explosion;
    double energy,rotation,reloadBulletTime,reloadMineTime;
    int bulletPowerups,minePowerups;
+   double angle; // current rotation angle
 };
 
 class BulletSprite:public MobileSprite
 {
 public:
-   BulletSprite(Q3CanvasPixmapArray* seq, Q3Canvas* canvas, int pn,double lifetime);
-   virtual int rtti() const {return S_BULLET;}
+   BulletSprite(QPixmap* pixmap, QGraphicsScene* scene, int pn,double lifetime);
+   virtual int type() const {return S_BULLET;}
    virtual void forward(double mult);
    virtual void forward(double mult,int fr);
    bool timeOut() {return time<=0;}
@@ -125,32 +155,33 @@ private:
    double time;
 };
 
-class MineSprite:public MobileSprite
+class MineSprite:public AnimatedSprite
 {
 public:
-   MineSprite(Q3CanvasPixmapArray* seq, Q3Canvas* canvas, int pn,double atime,double \
                f);
-   virtual int rtti() const {return S_MINE;}
+   MineSprite(const QList<QPixmap> &animation, const QList<QPixmap> &exploanimation, \
QGraphicsScene* scene, int pn,double atime,double f); +   virtual int type() const \
{return S_MINE;}  bool isActive() {return active;}
    double getFuel() {return fuel;}
    void setFuel(double f) {fuel=(f<0.0?0.0:f);}
    virtual void forward(double mult);
-   void explode(Q3CanvasPixmapArray* s);
+   void explode();
    bool explodes() {return expl;}
    bool over() {return (expl&&(explosiontime>(timeToGo-0.1)));}
    virtual void calculateGravity(double gravity,double mult);
 private:
    bool expl,active;
    double activateTime,fuel,timeToGo,explosiontime;
+   QList<QPixmap> exploframes; // when mine explodes, we set frames to exploframes \
(needed because both player's mines have +   				// the same explosion animation
 };
 
-class ExplosionSprite:public Q3CanvasSprite
+class ExplosionSprite:public AnimatedSprite
 {
 public:
-   ExplosionSprite(Q3CanvasPixmapArray *seq, Q3Canvas* field, MobileSprite *sp);
-   virtual int rtti() const {return S_EXPLOSION;}
+   ExplosionSprite(const QList<QPixmap> &animation, QGraphicsScene *scene = 0, \
MobileSprite *sp = 0); +   virtual int type() const {return S_EXPLOSION;}
    bool isOver() {return over;}
    virtual void forward(double mult);
-   void setSequence(Q3CanvasPixmapArray *seq);
 private:
    double timeToGo,time;
    bool over;
Index: defines.h
===================================================================
--- defines.h	(Revision 616217)
+++ defines.h	(Arbeitskopie)
@@ -32,9 +32,9 @@ This program is free software; you can r
 
 #define MV_BACKGROUND "sprites/backgr.png"
 
-#define MV_SHIP1_PPM "sprites/ship1/ship%02d.ppm"
+#define MV_SHIP1_PPM "sprites/ship1/ship00.ppm"//ship%02d.ppm"
 #define MV_SHIP1_PBM "sprites/ship1/ship%02d.pbm"
-#define MV_SHIP2_PPM "sprites/ship2/ship%02d.ppm"
+#define MV_SHIP2_PPM "sprites/ship2/ship00.ppm"//ship%02d.ppm"
 #define MV_SHIP2_PBM "sprites/ship2/ship%02d.pbm"
 
 #define MV_BULLET1_PPM "sprites/ship1/bullet.ppm"
@@ -42,12 +42,12 @@ This program is free software; you can r
 #define MV_BULLET2_PPM "sprites/ship2/bullet.ppm"
 #define MV_BULLET2_PBM "sprites/ship2/bullet.pbm"
 
-#define MV_MINE1_PPM "sprites/ship1/mine%d.ppm"
+#define MV_MINE1_PPM "sprites/ship1/mine1.ppm"//%d.ppm"
 #define MV_MINE1_PBM "sprites/ship1/mine%d.pbm"
-#define MV_MINE2_PPM "sprites/ship2/mine%d.ppm"
+#define MV_MINE2_PPM "sprites/ship2/mine1.ppm"//%d.ppm"
 #define MV_MINE2_PBM "sprites/ship2/mine%d.pbm"
 
-#define MV_SUN_PPM "sprites/sun/sun.ppm"
+#define MV_SUN_PPM "sprites/sun/sun.ppm" //ppm"
 #define MV_SUN_PBM "sprites/sun/sun.pbm"
 
 #define MV_EXPLOSION_PPM "sprites/explosion/explos%02d.ppm"
@@ -77,7 +77,7 @@ This program is free software; you can r
 #define S_EXPLOSION S_BASE+4
 #define S_POWERUP S_BASE+5
 
-#define SHOTDIST 14
+#define SHOTDIST 18//14
 #define EPSILON 0.1
 
 #define MAX_HP 99
@@ -85,3 +85,9 @@ This program is free software; you can r
 #define MAX_VELOCITY 20
 
 #define GAME_START_SHORTCUT Qt::Key_Space
+
+#define ID_SHIP1                 1350
+#define ID_EXPLOSION	1351
+#define ID_MINE1	1352
+#define ID_MINE2	1353
+#define ID_MINEEXPLO	1354



_______________________________________________
kde-games-devel mailing list
kde-games-devel@kde.org
https://mail.kde.org/mailman/listinfo/kde-games-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic