Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help *lol*  (Read 9068 times)

0 Members and 2 Guests are viewing this topic.

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Help *lol*
« 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?

r1nux

  • Newbie
  • *
  • Posts: 9
    • View Profile
Help *lol*
« Reply #1 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.

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Help *lol*
« Reply #2 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Help *lol*
« Reply #3 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 ;)
Laurent Gomila - SFML developer

r1nux

  • Newbie
  • *
  • Posts: 9
    • View Profile
Help *lol*
« Reply #4 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.

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Help *lol*
« Reply #5 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?

r1nux

  • Newbie
  • *
  • Posts: 9
    • View Profile
Help *lol*
« Reply #6 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.

zarka

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Help *lol*
« Reply #7 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
//Zzzarka

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Help *lol*
« Reply #8 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  ?

zarka

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Help *lol*
« Reply #9 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 :)
//Zzzarka

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Help *lol*
« Reply #10 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

r1nux

  • Newbie
  • *
  • Posts: 9
    • View Profile
Help *lol*
« Reply #11 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

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Help *lol*
« Reply #12 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?

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Help *lol*
« Reply #13 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."

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Help *lol*
« Reply #14 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);  ?