• Advertisement
Sign in to follow this  

Array problems

This topic is 4763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using an array of pointers to sprites like this: Sprite* _pBrickSprites[_iNumBrickRows][_iNumBrickCols]; And then I go through and initialize each like this:
    for(int i=1; i<=_iNumBrickRows; i++)
    {
        for(int j=1; j<=_iNumBrickCols; j++)
        {
            _pBrickSprites[i][j] = new Sprite(_pBrickBitmap);
            _pBrickSprites[i][j]->SetPosition(_pGame->GetWidth()*j/(_iNumBrickCols+1) - _pBrickBitmap->GetWidth()/2,
                _iVerticalBrickSpacing*i + _pBrickBitmap->GetHeight()*(i-1));
            _pGame->AddSprite(_pBrickSprites[i][j]);
        }
    }


In detecting collisions I use this code:
    // Loop through and check if the ball hit any of the bricks
    RECT brickRect;
    for(int i=0; i<_iNumBrickRows; i++)
    {
        for(int j=0; j<_iNumBrickCols; j++)
        {
            brickRect = _pBrickSprites[i][j]->GetPosition();

        }
    }


Everything compiles fine, but the line where I assign brickRect the position causes the game to crash. I have no clue why. This is the declaration of GetPosition() and the rect in the sprite class: RECT m_rcPosition; RECT& GetPosition() { return m_rcPosition; }; I have a feeling it has something to do with the reference being returned, but I can't figure it out. Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
This isn't a problem with your array, it's a problem with RECTs. IIRC, Windows RECTs don't like being copied/assigned. It would work if you did this instead:


RECT& brickRect = _pBrickSprites[i][j]->GetPosition();



Because then you're not copying the RECT itself, just the reference.

But, aside from being bad OOP practice, I'd just avoid using RECT entirely and use your own structure. I try to avoid Windows structures in general, though...

Share this post


Link to post
Share on other sites
Quote:
Original post by Morbo
This isn't a problem with your array, it's a problem with RECTs. IIRC, Windows RECTs don't like being copied/assigned. It would work if you did this instead:

*** Source Snippet Removed ***

Because then you're not copying the RECT itself, just the reference.

But, aside from being bad OOP practice, I'd just avoid using RECT entirely and use your own structure. I try to avoid Windows structures in general, though...


I've never heard of this, RECT is just a struct with 4 longs, the default shallow assingment operator should be able to handle this. I think its more likely what I mentioned earlier that the 0 row and column aren't being initialized.

Cheers
Chris

Share this post


Link to post
Share on other sites
Quote:
Original post by chollida1
Quote:
Original post by Morbo
This isn't a problem with your array, it's a problem with RECTs. IIRC, Windows RECTs don't like being copied/assigned. It would work if you did this instead:

*** Source Snippet Removed ***

Because then you're not copying the RECT itself, just the reference.

But, aside from being bad OOP practice, I'd just avoid using RECT entirely and use your own structure. I try to avoid Windows structures in general, though...


I've never heard of this, RECT is just a struct with 4 longs, the default shallow assingment operator should be able to handle this. I think its more likely what I mentioned earlier that the 0 row and column aren't being initialized.

Cheers
Chris
Exactly! chollida1 is correct.

Sorry Morbo, but what you said is merely superstition/heresay or something. Copying RECTs works absolutely fine. His code wouldn't crash like that anyway.

The problem is that he initialises his array using for loops like this:
    for(int i=1; i<=_iNumBrickRows; i++)
but accesses them with for loops like this:
    for(int i=0; i<_iNumBrickRows; i++)
(The second one is correct) It goes from 0 to _iNumBrickRows-1. The first loop has to be the same.
The first bit of code will need to change slightly to accomodate the shift in starting values...
    for(int i=0; i<_iNumBrickRows; i++)
{
for(int j=0; j<_iNumBrickCols; j++)
{
_pBrickSprites[i][j] = new Sprite(_pBrickBitmap);
_pBrickSprites[i][j]->SetPosition(_pGame->GetWidth()*(j+1)/(_iNumBrickCols+1) - _pBrickBitmap->GetWidth()/2,
_iVerticalBrickSpacing*(i+1) + _pBrickBitmap->GetHeight()*i);
_pGame->AddSprite(_pBrickSprites[i][j]);
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement