I thought about using the ctor to change the size like you did, but I had my reason not to in this case.
Wanted to point out something to you:
Code: Select all
MemeField::MemeField( const Vei2& center,int nMemes, int width_in, int height_in)
:
topLeft( center - Vei2( width * SpriteCodex::tileSize,height * SpriteCodex::tileSize ) / 2 ),
height(height_in),
width(width_in)
{
//...
}
Do you know why this works? The reason I ask is because you use members width and height to initialize topleft, seemingly before initializing them. I know why this works, just wanted to get your take on it.
Yes, you should be deleting the Tile *field each time you delete MemeField *Game::field. The easiest way to do this is by adding a destructor to MemeField ( check my repo for example ) and delete[] the field pointer in there. This way when you delete Game::field, the ~MemeField destructor is called and then Tile *field will be deleted also, with no extra function calls on your part.
I didn't think about making Game::field a pointer, honestly, I almost refuse nowadays to make single instance pointers with the exception of using polymorphism. There is a bug in my solution now that I'm reading this and have looked over yours. For some reason, the game wouldn't go back to the menu when game was over, so I didn't think about it. Anyway, if that were the case, since I don't use the constructor, I would be leaking memory since I don't free the MemeField::field pointer between games. Same goes for your current solution. If you aren't freeing the dynamically allocated Tile array between games, you too are leaking memory.
When chili goes over std::unique_ptr/vector or RAII, you'll learn destructors will be rare and handling resources will be somewhat simplified.
Good solution, just need to fix the mem leak and you've got it. The biggest reason I didn't use the constructor is because I read you were going to go that route, and I didn't want to do the same thing.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com