Jump to content
  • Advertisement
Sign in to follow this  
Ark86

Array problems

This topic is 4943 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[j] = new Sprite(_pBrickBitmap);
            _pBrickSprites[j]->SetPosition(_pGame->GetWidth()*j/(_iNumBrickCols+1) - _pBrickBitmap->GetWidth()/2,
                _iVerticalBrickSpacing*i + _pBrickBitmap->GetHeight()*(i-1));
            _pGame->AddSprite(_pBrickSprites[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[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[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[j] = new Sprite(_pBrickBitmap);
_pBrickSprites[j]->SetPosition(_pGame->GetWidth()*(j+1)/(_iNumBrickCols+1) - _pBrickBitmap->GetWidth()/2,
_iVerticalBrickSpacing*(i+1) + _pBrickBitmap->GetHeight()*i);
_pGame->AddSprite(_pBrickSprites[j]);
}
}

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!