SFML community forums
Help => General => Topic started by: ravenheart on September 25, 2008, 03:45:42 pm
-
o my god,
well i wrote a class called bat
class
{
Shape x;
...
}
and now i want my bat in main to be drawn, how can i tell the programm how to do it?
RenderWindow App;
App.Create(sf::VideoMode(800, 600, 32), "SFML Window", Style::Fullscreen);
bat left;
App.Draw(left);
surely will not work, but how can i do it?
-
Have a "get-function" in the Bat-class?
Ex:
class Bat
{
public:
...
sf::Shape getShape() { return m_shapeX; }
private:
sf::Shape m_shapeX;
}
sf::RenderWindow App;
...
Bat left;
App.Draw(left.getShape());
PS.
Havn't tested to compile this code.
But it's should give some kind of idea anyways.
-
well to have a little solution i added a function
Shape get()
{
return Shape;
}
App.Draw(bat.get());
it works but i doubt its best solution
-
You can make bat inherit from sf::Drawable and override the Render virtual function to draw your shape, but then it will have its own position / rotation / scale / center, applied to everything it draws.
Sometimes it's useful, sometimes not ;)
-
You would probably want to send a reference instead of a copy of the shape
for performance.
-
i still wonder wheter i should use the heritage or like i did it now
add new functions
bat.move(f x, f y)
{
polygon.Move(x,y);
}
what do u think?
-
The overhead for calling one extra function is so small for simple games that
you wont notice it anyways.
But my 2 cents.
Try the both techniques to see what suits you the most.
-
well to have a little solution i added a function
Shape get()
{
return Shape;
}
App.Draw(bat.get());
it works but i doubt its best solution
you could also make a Draw function in the bat class
class Bat
{
public:
void Draw(const sf::RenderWindow &rw) const
{
rw.Draw(shape);
}
private:
sf::Shape shape;
};
or have Bat inherit from Shape
class Bat : public sf::Shape
{
public:
void MySpecialFunc();
};
in the later example you can pass your Bat object to the Draw function of RenderWindow.
so what you need to ask your self is this. does bat have a shape, or is bat a shape. if it is a shape then inheritance is a good solution .. if it has a shape then composition would be the way to go. the rule is to generally prefer composition because inheritance will give you a overhead. usually not noticeable in small projects but still a good thing to keep in mind / know about.
EDIT: wow .. i was realy slow .. 3 posts between my click to ansawer and post :D
-
my problem is that i do programming only
if( me == tired && girlfriend == not_here)
so i dont even have a plan what i want to do, at moment i only do some experiments and i dont know what i will need, to be on the safe side i decided to inherit
but i did
class bat: public sf::Shape
could i have any problems with that coz laurent said to inherit sf::Drawable ?
-
could i have any problems with that coz laurent said to inherit sf::Drawable ?
depends on how Laurent have implemented sf::Shape if it has a virtual destructor it should be safe to inherit from.
if you inherit from sf::Drawable you will have to implement all the drawing yourself (using openGL). if you inherit from sf::Shape you also inherit Shapes render code meaning you do not need to implement it yourself :)
sdo i would say you definatly whant to inherit from sf::Shape.. and if that is unsafe you should go with composition :)
-
:cry:
i never understood virtual destructors, could u tell me how one could work with sf::shape?
edit: i do not understand ANY destructor
-
:cry:
i never understood virtual destructors, could u tell me how one could work with sf::shape?
edit: i do not understand ANY destructor
By making the Base class Destructor virtual, both the destructors will be called in order.
One thing im confused about is that the shape-class dosnt declare
a default constructor.
Dosnt that mean that the shape-class gets a "regular" non-virtual destructor?
Edit:
Think of the destructor as the function that gets called when the object gets
destroyed or gets out of scope.
Example:
class AClass
{
public:
AClass(); // Constructor
~AClass(); // Destructor
};
int main(int argc, char** argv)
{
AClass* testClass = new AClass; // Constructor gets called
delete testClass; // Destructor gets called
return 0;
}
or
int main(int argc, char** argv)
{
AClass testClass; // Constructor gets called
return 0; // Destructor gets called.
}
Class Reference (http://www.cplusplus.com/doc/tutorial/classes.html)
-
so far so good but what do i have to do??
class bat: public Shape
{
bat();
...
~bat();
}
what should the funtion ~bat() look like ? somehow i should set the memory free how do i do that?
-
what should the funtion ~bat() look like ? somehow i should set the memory free how do i do that?
You need to delete any objects you new'd, or you get a memory leak. If that doesn't make sense, google "c++ new delete."
-
as far as i know
new and delete are for dynamic memory;
im sorry bur i dont have any clue how i should use it in my class
when i create a bat
bat x;;
do i need to add some code concerning destruction?
and when i creat it by pointer?
bat *x = new bat;
do i have do add something like
~bat(){ x = NULL); ?
-
as far as i know
new and delete are for dynamic memory;
im sorry bur i dont have any clue how i should use it in my class
when i create a bat
bat x;;
do i need to add some code concerning destruction?
and when i creat it by pointer?
bat *x = new bat;
do i have do add something like
~bat(){ x = NULL); ?
Whenever you use new, you have to use delete at some point to free that memory. And yes, that's for dynamic memory allocated at run time (what you're calling "create by pointer.") If you just declare variables without new, you don't need to call delete.
-
bat *x = new bat;
do i have do add something like
~bat(){ x = NULL); ?
It'd simply be
delete x;
and yes, any anonymous variables you have floating around need to be specifically deleted in the destructor. If you don't, then they'll be doomed to sit in memory until the application ends since you'll no longer have any way to reference them.
Also be very sure that you don't try to use that pointer after it's been deleted. This can happen when you pass that location around a bit. You'll get a fun, fun null pointer exception and wonder where and why it is happening.
-
Don't inherit from sf::Shape. A bat is not a shape, it's implemented using a shape, which means composition or private inheritance.