SFML-Drawing objects into from other classes

I need help drawing objects from different classes onto the window. I thought I did what I was suppose to but it just isn't working.



main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "Screen.h"
#include <SFML/Graphics.hpp>





int main() {


sf::RenderWindow Gamewindow(sf::VideoMode(800 , 600), "Game");


while (Gamewindow.isOpen()){


    sf::Event event;
    while(Gamewindow.pollEvent(event)) {

        if(event.type == sf::Event::Closed){
           Gamewindow.close();
        }

    }


Gamewindow.clear(sf::Color::Blue);
Gamewindow.display();
}

return 0;

}




Screen.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef SCREEN_H
#define SCREEN_H
#include <SFML/Graphics.hpp>


class Screen
{
    public:
        Screen(sf::RenderWindow &Gamewindow);
    


};

#endif // SCREEN_H





Screen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <SFML/Graphics.hpp>
#include "Screen.h"

Screen::Screen(sf::RenderWindow &Gamewindow)
{

sf::RectangleShape rect;
rect.setFillColor(sf::Color::Red);




*Gamewindow.draw(rect);
*Gamewindow.display();
}


you dont have an instance of screen anywhere in main.

that would be your main problem

hehe get it?

EDIT: i would also like to add, your taking the window as a reference, you dont need the * in front if the two method calls
Last edited on
Ok I made an object for the class Screen so it gets called but the rectangle still wont get drawn even without the pointers, and when I do try the pointers I get an error. Any solution to that?
Your rectangle is not given a position.
Your rectangle is not given a size.
Your rectangle only exists for the duration of the Screen constructor.

I would tell you to visit the tutorials on the SFML site, but those aren't intended to teach you C++ and your grasp of C++ is the issue here. When you also don't grasp how to use the library, you've got a tough road ahead of you. I would recommend concentrating primarily on C++ at the moment and not so much third party libraries.

However, if you aren't dissuaded, the following is an example of how the thing you're trying to do may be done:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <random>
#include <deque>
#include <SFML/Graphics.hpp>


sf::Color lighten( sf::Color color )
{ 
    return sf::Color(color.r+15, color.g+15, color.b+15) ; 
}

class CascadingRectangle : public sf::Drawable
{
public:
    CascadingRectangle() : _rect(sf::Vector2f(10,10))
    {
        _rect.setPosition(sf::Vector2f(0,0)) ;
        _rect.setFillColor(sf::Color::Black) ;
    }

    CascadingRectangle( sf::Vector2f position, sf::Vector2f size, sf::Color color ) : _rect(size)
    {
        _rect.setPosition(position) ;
        _rect.setFillColor(color) ;
    }

    void draw( sf::RenderTarget& tgt, sf::RenderStates states) const
    {
        sf::RectangleShape cascade[2] = 
        {
            _rect,
            _rect
        };

        sf::Vector2f offset(5.0, 5.0) ;
        
        cascade[0].move(offset) ;
        cascade[1].move(offset*2.0f) ;

        cascade[0].setFillColor(lighten(_rect.getFillColor())) ;
        cascade[1].setFillColor(lighten(cascade[0].getFillColor())) ;

        tgt.draw(_rect, states) ;
        tgt.draw(cascade[0], states) ;
        tgt.draw(cascade[1], states) ;
    }

private:
    sf::RectangleShape _rect ;
};


int main()
{
    std::default_random_engine rng((std::random_device()())) ;
    sf::RenderWindow window(sf::VideoMode(800, 600), "EVENT Handling") ;
    std::deque<CascadingRectangle> boxes ;

    while ( window.isOpen() )
    {
        sf::Event event ;

        while ( window.pollEvent(event) )
        {
            if ( event.type == sf::Event::Closed )
                window.close() ;
            else if ( event.type == sf::Event::MouseButtonReleased )
            {
                sf::Vector2f pos(event.mouseButton.x, event.mouseButton.y) ;
                sf::Vector2f size( rng()%80 + 30, rng()%80 + 30) ;
                sf::Color color(rng()%128 + 64, rng()%128 + 64, rng()%128+64) ;

                boxes.push_back(CascadingRectangle(pos, size, color)) ;

                if ( boxes.size() > 5 )
                    boxes.pop_front() ;
            }
        }

        window.clear(sf::Color::White) ;

        for ( auto & box : boxes )
            window.draw(box) ;

        window.display() ;
    }
}


If it isn't clear from the code, press and release a mouse button (any button) to place a CascadingRectangle.
Topic archived. No new replies allowed.