Array problems

Started by
4 comments, last by iMalc 19 years, 3 months ago
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.
Advertisement
you never assign a sprite to the 0 row or column entries.


Chees
Chris
CheersChris
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...
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
CheersChris
I agree, there is nothing wrong with RECT.
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]);        }    }
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement