Jump to content

  • Log In with Google      Sign In   
  • Create Account


Best way to get width of object before creation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 sednihp   Members   -  Reputation: 241

Like
0Likes
Like

Posted 04 January 2013 - 08:08 AM

I'm currently working on a Fruit Ninja clone using SDL and I want to restrict my fruit to being created at a random x co-ordinate, but so that they fully appear within the bounds of the window (800x600). My current code (which is a work in progress) looks something like this:

 

 

 

fruit.push_back(std::unique_ptr<Fruit>(new Fruit(rand() % (mediaCache.scrWidth() - 50), mediaCache.scrHeight(), mediaCache.getImage("fruit.png"))));

 

 

Which creates a new Fruit at a random x position at the bottom of the window (so it can bounce up into it) and gives it an image.

 

50 is the width of the Fruit, so to ensure the whole Fruit starts inside the window I take it away from the screen width. I can't access the width through a get() method before the Fruit itself is actually created, so is the best way to do this to make the width a public const static member inside Fruit and use Fruit::width instead of 50 or is there a better way?

 

Thanks



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9574

Like
4Likes
Like

Posted 04 January 2013 - 08:28 AM

Since the fruit object will know it's own size, why not have the fruit constructor call rand()?

Also, since it looks like you're using a C++11 compiler, you can simplify that expression by using std::vector::emplace_back() rather than push_back(). So the call would look something like this instead:
fruit.emplace_back(new Fruit(mediaCache.scrWidth(), mediaCache.scrHeight(), mediaCache.getImage("fruit.png")));


#3 BeerNutts   Crossbones+   -  Reputation: 2860

Like
1Likes
Like

Posted 04 January 2013 - 10:32 AM

Or, since you may not want all Fruit to be randomly placed, you could create a second Constructor that only takes the Image, and then assign the Location via a SetLocation() method.

Fruit *fruit = new Fruit(mediaCache.getImage("fruit.png"));
fruit.SetLocation(rand() % (mediaCache.scrWidth() - fruit.GetWidth()), mediaCache.scrHeight());
// I'd suggest renaming the vector fruits (with an s) to denote it's not a single fruit
fruits.push_back(fruit);


My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#4 sednihp   Members   -  Reputation: 241

Like
0Likes
Like

Posted 04 January 2013 - 11:43 AM

Cheers guys, seems so obvious to do it that way SiCrane, thanks for that. I'm using vs2012 so I'll have to see if emplace_back is one of the c++11 features that made it in.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS