Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9326


Ignore:
Timestamp:
Jul 22, 2012, 5:06:56 PM (12 years ago)
Author:
landauf
Message:

small refactoring of tetris gametype, fixed memory leak

Location:
code/branches/presentation2012merge/src/modules/tetris
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2012merge/src/modules/tetris/Tetris.cc

    r9323 r9326  
    9595    void Tetris::cleanup()
    9696    {
    97         /*for(int i = 0;i < this->stones_.size(); i++) //TODO: Why isn't there any code like this
    98         {                                              // compensating the 'new' statement?
    99             delete this->stones_[i];
    100         }//*/
    101 
     97        if (this->isInitialized())
     98        {
     99            if (this->activeBrick_)
     100            {
     101                this->activeBrick_->destroy();
     102                this->activeBrick_ = NULL;
     103            }
     104            if (this->futureBrick_)
     105            {
     106                this->futureBrick_->destroy();
     107                this->futureBrick_ = NULL;
     108            }
     109
     110            for (std::list<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
     111                (*it)->destroy();
     112            this->stones_.clear();
     113        }
    102114    }
    103115
     
    111123            if(!this->isValidBrickPosition(this->activeBrick_, this->activeBrick_->getPosition()))
    112124            {
     125                for (unsigned int i = 0; i < this->activeBrick_->getNumberOfStones(); i++)
     126                    this->stones_.push_back(this->activeBrick_->getStone(i));
    113127                this->activeBrick_->setVelocity(Vector3::ZERO);
    114128                this->activeBrick_->releaseStones(this->center_);
    115                 //delete this->activeBrick_; //releasing the memory
    116129                this->findFullRows();
    117130                if(this->endGameCriteria_ < 0.1f) //end game if two bricks are created within a 0.1s interval.
     131                {
    118132                    this->end();
    119                 this->createBrick();
     133                    return;
     134                }
    120135                this->startBrick();
    121136                this->endGameCriteria_ = 0.0f;
     
    133148            return false;
    134149
    135         for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
    136         {
    137             if(stone == *it)
    138                 continue;
    139 
     150        for(std::list<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
     151        {
    140152            const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
    141153
     
    189201
    190202        // we use a reverse iterator because we have to check for collisions with the topmost stones first
    191         for(std::vector<TetrisStone*>::const_reverse_iterator it = this->stones_.rbegin(); it != this->stones_.rend(); ++it)
    192         {
    193             if(this->activeBrick_->contains(*it))//skip the moving brick' stones
    194                 continue;
     203        for(std::list<TetrisStone*>::const_reverse_iterator it = this->stones_.rbegin(); it != this->stones_.rend(); ++it)
     204        {
    195205            //Vector3 currentStonePosition = rotateVector((*it)->getPosition(), this->activeBrick_->getRotationCount());
    196206            const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
     
    264274        if (this->center_ != NULL) // There needs to be a TetrisCenterpoint, i.e. the area the game takes place.
    265275        {
    266             // Create the futureBrick_
    267             this->createBrick();
    268276            // Create the first brick.
    269277            this->createBrick();
     
    350358            cameraIndex = this->activeBrick_->getCurrentCameraIndex();
    351359            this->player_->stopControl();
     360            // destroy old active brick
     361            this->activeBrick_->destroy();
    352362        }
    353363
    354364        // Make the last brick to be created the active brick.
    355         this->activeBrick_ = this->bricks_.back();
    356 
     365        this->activeBrick_ = this->futureBrick_;
     366        this->futureBrick_ = NULL;
     367
     368        // set its position
    357369        this->player_->startControl(this->activeBrick_);
     370        float xPos = (this->center_->getWidth()/2 + ((this->center_->getWidth() % 2)*2-1)/2.0f)*this->center_->getStoneSize();
     371        float yPos = (this->center_->getHeight()-0.5f)*this->center_->getStoneSize();
     372        this->activeBrick_->setPosition(xPos, yPos, 0.0f);
    358373        this->activeBrick_->setVelocity(0.0f, -this->center_->getStoneSpeed(), 0.0f);
    359374        this->activeBrick_->setCameraPosition(cameraIndex);
     375
     376        // create a new future brick
     377        this->createBrick();
    360378    }
    361379
    362380    void Tetris::createBrick(void)             //TODO: random rotation offset between 0 and 3 (times 90°)
    363381    {
    364         // Use the futureBrick_ as new currentBrick by setting its position and storing it in this->bricks
    365         if(this->futureBrick_ != NULL)
    366         {
    367             for (unsigned int i = 0; i < this->futureBrick_->getNumberOfStones(); i++)
    368             {
    369                 this->stones_.push_back(this->futureBrick_->getStone(i));
    370             }
    371             float xPos = (this->center_->getWidth()/2 + ((this->center_->getWidth() % 2)*2-1)/2.0f)*this->center_->getStoneSize();
    372             float yPos = (this->center_->getHeight()-0.5f)*this->center_->getStoneSize();
    373             this->futureBrick_->setPosition(xPos, yPos, 0.0f);
    374             this->bricks_.push_back(this->futureBrick_);
    375         }
    376382        // create new futureBrick_
    377383        this->futureBrick_ = new TetrisBrick(this->center_);
     
    426432        {
    427433            stonesPerRow = 0;
    428             for(std::vector<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
    429             {
    430                 correctPosition = static_cast<unsigned int>(((*it)->getPosition().y - 5)/this->center_->getStoneSize());
     434            for(std::list<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); )
     435            {
     436                std::list<TetrisStone*>::iterator it_temp = it++;
     437                correctPosition = static_cast<unsigned int>(((*it_temp)->getPosition().y - 5)/this->center_->getStoneSize());
    431438                if(correctPosition == row)
    432439                {
     
    447454    void Tetris::clearRow(unsigned int row)
    448455    {// clear the full row
    449         for(std::vector<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
     456        for(std::list<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); )
    450457        {
    451458            if(static_cast<unsigned int>(((*it)->getPosition().y - 5)/this->center_->getStoneSize()) == row)
    452                 (*it)->setPosition(Vector3(-50,-50,100));
    453                 //{(*it)->destroy(); this->stones_.erase(it); orxout()<< "destroy row "<<endl;}//experimental
     459            {
     460                (*it)->destroy();
     461                this->stones_.erase(it++);
     462            }
     463            else
     464                ++it;
    454465        }
    455466      // adjust height of stones above the deleted row //TODO: check if this could be a source of a bug.
    456         for(std::vector<TetrisStone*>::iterator it2 = this->stones_.begin(); it2 != this->stones_.end(); ++it2)
    457         {
    458             if(static_cast<unsigned int>(((*it2)->getPosition().y - 5)/this->center_->getStoneSize()) > row)
    459                 (*it2)->setPosition((*it2)->getPosition()-Vector3(0,10,0));
     467        for(std::list<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
     468        {
     469            if(static_cast<unsigned int>(((*it)->getPosition().y - 5)/this->center_->getStoneSize()) > row)
     470                (*it)->setPosition((*it)->getPosition()-Vector3(0,10,0));
    460471        }
    461472
  • code/branches/presentation2012merge/src/modules/tetris/Tetris.h

    r9286 r9326  
    5656        public:
    5757            Tetris(BaseObject* creator); //!< Constructor. Registers and initializes the object.
    58             virtual ~Tetris(); //!< Destructor. Cleans up, if initialized.           
     58            virtual ~Tetris(); //!< Destructor. Cleans up, if initialized.
    5959
    6060            virtual void start(void); //!< Starts the Tetris minigame.
     
    8888            void clearRow(unsigned int row);
    8989
    90            
     90
    9191            PlayerInfo* player_;
    9292
    9393            WeakPtr<TetrisCenterpoint> center_; //!< The playing field.
    94             std::vector<TetrisBrick*> bricks_; //!< A list of all bricks in play.
    95             std::vector<TetrisStone*> stones_; //!< A list of all stones in play.
    96             std::vector< std::vector<bool> > grid_;
     94            std::list<TetrisStone*> stones_; //!< A list of all stones in play.
    9795            TetrisBrick* activeBrick_;
    9896            TetrisBrick* futureBrick_;
    99            
     97
    10098            Timer starttimer_; //!< A timer to delay the start of the game.
    10199            float endGameCriteria_; //<! Works as a timer which is resetted, whenever a brick is created.
  • code/branches/presentation2012merge/src/modules/tetris/TetrisBrick.cc

    r9286 r9326  
    175175        return NULL;
    176176    }
    177 //TODO: refactor this function; is not needed if brickstones are added to Tetris::stones_ after collision.
    178     bool TetrisBrick::contains(TetrisStone* stone)
    179     {
    180         for(unsigned int i = 0; i < brickStones_.size(); i++)
    181         {
    182             if(stone == brickStones_[i])
    183                 return true;
    184         }
    185         return false;
    186     }
    187177
    188178    /**
  • code/branches/presentation2012merge/src/modules/tetris/TetrisBrick.h

    r9286 r9326  
    6565                { return this->brickStones_.size(); }
    6666            TetrisStone* getStone(unsigned int i);
    67             bool contains(TetrisStone* stone);
    6867
    6968            void setGame(Tetris* tetris)
Note: See TracChangeset for help on using the changeset viewer.