SFML community forums

Help => General => Topic started by: ravenheart on October 03, 2008, 06:43:05 pm

Title: drawable class
Post by: ravenheart on October 03, 2008, 06:43:05 pm
hi i wanted to create a class derived from sf::Drawable that can be drawed by sf::RenderWindow
it should be a mix of the MyPicture class and something

so i want my calss to have an image and a sprite and some more things;

hoe could something like thast look?
Title: Re: drawable class
Post by: ravenheart on October 03, 2008, 08:03:33 pm
Quote from: "ravenheart"
hi i wanted to create a class derived from sf::Drawable that can be drawed by sf::RenderWindow
it should be a mix of the MyPicture class and something

so i want my calss to have an image and a sprite and some more things;

hoe could something like thast look?


hmm i have a problem i want to create a file object.h

in this:
Code: [Select]
class object: public Drawable {...}
but it doesnt know sf::Drawable there what  am i doing wrong?
Title: drawable class
Post by: Kreeg on October 03, 2008, 08:44:31 pm
It's not a problem with SFML, It's a C++ problem ! Anyway I'm going to help. So what do you want to do exactly with a sf::Drawable ? A derived class might not be the key to resolving your problem.

I see some reasons why this code won't compile. Is there any 'using namespace sf;' in your code ? If it's the case I can't tell more knowing what you gave us, except that there should be no 'using namespace' in a public interface. Otherwise, try replacing Drawable by sf::Drawable and tell us if it compiled without problems.
Title: drawable class
Post by: ravenheart on October 04, 2008, 09:11:03 am
well :

object.h:
Code: [Select]
class object;

object.cpp


Code: [Select]
#include "object.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <iomanip>
#include <sstream>

using namespace sf;
using namespace std;


class object
{
    public:

    Image Bild;
    Sprite Abbild;

    float x_pos;
    float y_pos;
    float height;
    float width;

    bool collides(object &partner);
    void LoadFromFile(const std::string& Filename);
};

void object::LoadFromFile(const std::string& Filename)
{
    Bild.LoadFromFile(Filename);
}


main.cpp

Code: [Select]
#include <iostream>
#include "object.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
using namespace sf;


/*class object
{
    public:

    Image Bild;
    Sprite Abbild;

    float x_pos;
    float y_pos;
    float height;
    float width;

    bool collides(object &partner);
    void LoadFromFile(const std::string& Filename);
};

void object::LoadFromFile(const std::string& Filename)
{
    Bild.LoadFromFile(Filename);
}
*/

int main()
{
    object x;
    RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
    x.LoadFromFile("h.jpg");

    return 0;
}


returns :
Code: [Select]
C:\Programme\CodeBlocks\MEINS\Object\main.cpp|35|error: aggregate `object x' has incomplete type and cannot be defined|

when i dont split the data and write it all in main.cpp it worksd fine, wqhy?
Title: drawable class
Post by: Kreeg on October 04, 2008, 09:16:56 am
You forgot to add a constructor to you object :D
I repeat : you should NOT use 'using namespace' in a public interface. Take them away from "object.h" !
Title: drawable class
Post by: ravenheart on October 04, 2008, 09:46:41 am
is the default constructor not enough?

or do i need to add something cause of the Image and Sprite?
Title: drawable class
Post by: Laurent on October 04, 2008, 10:26:37 am
If object.h only contains the forward declaration of the object class, you won't be able to do anything with it. You must put the class definition in the header, not in the cpp file.

You should definitely practice more C++ before trying to build SFML applications, I don't think you have all the necessary skills to do it.

We'll still be glad to help you, but we won't teach you C++ on this forum you know ;)
Title: drawable class
Post by: ravenheart on October 04, 2008, 07:30:10 pm
when i put it in the .h file it doesnt know sf::Image

and i surely may not write any includes in the header?

( it´s something hard to do first things that are not in any book)

object.h

Code: [Select]
class object
{
    public:

    Image Bild;
    Sprite Abbild;

    float x_pos;
    float y_pos;
    float height;
    float width;

    object();
    bool collides(object &partner);
    void LoadFromFile(const std::string& Filename);
};



object.cpp

Code: [Select]
#include "object.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <iomanip>
#include <sstream>

using namespace sf;
using namespace std;


void object::LoadFromFile(const std::string& Filename)
{
    Bild.LoadFromFile(Filename);
}


errors:

Title: drawable class
Post by: Laurent on October 04, 2008, 07:54:35 pm
Of course you need to include <SFML/Graphics.hpp> in your header so that your class has the definition of the SFML classes you use.
Title: drawable class
Post by: MrQuizzles on October 05, 2008, 09:42:35 am
Also, try looking up the term "encapsulation". It's a very important principle of object-oriented design.

Basically:

Make it so that your objects can only be used in exactly the way you want them to be used.
Make the implementation of the object irrelevant to its usage.



This is mainly in response to the fact that you made everything public. It's actually very bad form to do so and can cause difficulties working with the class.
Title: drawable class
Post by: Kreeg on October 06, 2008, 01:50:28 pm
Quote from: "ravenheart"
is the default constructor not enough?


I did not see any object(); in your class.