SFML community forums

Help => General => Topic started by: ravenheart on September 25, 2008, 03:45:42 pm

Title: Help *lol*
Post by: ravenheart on September 25, 2008, 03:45:42 pm
o my god,

well i wrote a class called bat

Code: [Select]
class
{
Shape x;

...
}


and now i want my bat in main to be drawn, how can i tell the programm how to do it?

Code: [Select]

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?
Title: Help *lol*
Post by: r1nux on September 25, 2008, 04:24:08 pm
Have a "get-function" in the Bat-class?

Ex:

Code: [Select]
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.
Title: Help *lol*
Post by: ravenheart on September 25, 2008, 04:30:56 pm
well to have a little solution i added a function

Code: [Select]

Shape get()
{
return Shape;
}

App.Draw(bat.get());



it works but i doubt its best solution
Title: Help *lol*
Post by: Laurent on September 25, 2008, 04:45:03 pm
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 ;)
Title: Help *lol*
Post by: r1nux on September 25, 2008, 04:45:29 pm
You would probably want to send a reference instead of a copy of the shape
for performance.
Title: Help *lol*
Post by: ravenheart on September 25, 2008, 04:58:49 pm
i still wonder wheter i should use the heritage or like i did it now
add new functions

Code: [Select]


bat.move(f x, f y)
{
polygon.Move(x,y);
}



what do u think?
Title: Help *lol*
Post by: r1nux on September 25, 2008, 05:05:08 pm
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.
Title: Help *lol*
Post by: zarka on September 25, 2008, 05:10:27 pm
Quote from: "ravenheart"
well to have a little solution i added a function

Code: [Select]

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

Code: [Select]

class Bat
{
public:
    void Draw(const sf::RenderWindow &rw) const
    {
        rw.Draw(shape);
    }
private:
    sf::Shape shape;
};


or have Bat inherit from Shape

Code: [Select]

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
Title: Help *lol*
Post by: ravenheart on September 25, 2008, 05:13:30 pm
my problem is that i do programming only

Code: [Select]
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


Code: [Select]
class bat: public sf::Shape

could i have any problems with that coz laurent said to inherit sf::Drawable  ?
Title: Help *lol*
Post by: zarka on September 25, 2008, 05:31:03 pm
Quote from: "ravenheart"


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 :)
Title: Help *lol*
Post by: ravenheart on September 25, 2008, 05:57:52 pm
:cry:
i never understood virtual destructors, could u tell me how one could work with sf::shape?

edit: i do not understand ANY destructor
Title: Help *lol*
Post by: r1nux on September 25, 2008, 08:11:19 pm
Quote from: "ravenheart"
: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:
Code: [Select]
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)
Title: Help *lol*
Post by: ravenheart on September 25, 2008, 08:40:17 pm
so far so good but what do i have to do??


Code: [Select]

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?
Title: Help *lol*
Post by: quasius on September 25, 2008, 09:03:07 pm
Quote from: "ravenheart"
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."
Title: Help *lol*
Post by: ravenheart on September 25, 2008, 09:33:19 pm
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);  ?
Title: Help *lol*
Post by: quasius on September 25, 2008, 10:38:23 pm
Quote from: "ravenheart"
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.
Title: Help *lol*
Post by: MrQuizzles on September 26, 2008, 06:40:41 am
Quote from: "ravenheart"
bat *x = new bat;
do i have do add something like

~bat(){ x = NULL);  ?


It'd simply be

Code: [Select]
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.
Title: Help *lol*
Post by: Laurent on September 26, 2008, 07:45:52 am
Don't inherit from sf::Shape. A bat is not a shape, it's implemented using a shape, which means composition or private inheritance.