Virtual

Ok so i've been practising on polymorhism and inheritance and I think I pretty much understand it all, but about virtual I used it like this, is this still correct? The tutorial I watched he did it a bit differently but I think i'm still doing it the right way. He had multiple classes that were inheriting from a base though. Heres the tutorial just in case you want to see it:

https://www.youtube.com/watch?v=DudHooleNVg&list=PLAE85DE8440AA6B83&index=56

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <SFML/Graphics.hpp>

// Always include the necessary header files.
// Including SFGUI/SFGUI.hpp includes everything
// you can possibly need automatically.
#include <SFGUI/SFGUI.hpp>
#include <string>
#include <vector>

using namespace std;

 sfg::Desktop D;

class Window
{
    public:
        virtual void CreateWindow(string &title, int &width, int &height);
        virtual void Button(string &name, float &posX, float &posY);

    private:
        sfg::Window::Ptr win;
        sfg::Button::Ptr button;
};

void Window::CreateWindow(string &title, int &width, int &height)
{
    win = sfg::Window::Create();
    win->SetTitle(title);
    win->SetRequisition(sf::Vector2f(width ,height));
    win->SetPosition(sf::Vector2f(200, 200));

    //auto box_HOR = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL);

    D.Add(win);
}

void Window::Button(string &name, float &posX, float &posY)
{
    button = sfg::Button::Create(name);

    auto box_HOR = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL);
    box_HOR->Pack(button, false, true);

    auto box_VER = sfg::Box::Create(sfg::Box::Orientation::VERTICAL);
    box_VER->Pack(box_HOR, false, true);

    win->Add(box_VER);

    D.Add(win);
}

int main()
{
    Window w;
    sf::Mouse mouse;

    int winWidth = 250;
    int winHeight = 150;

    int winWidth2 = 450;
    int winHeight2 = 250;

    string buttonName = "Button1";
    float pos_X = mouse.getPosition().x;
    float pos_Y = mouse.getPosition().y;

    vector<string> winName;

	sf::RenderWindow app_window( sf::VideoMode( 800, 600 ), "SFGUI Window Example");

	app_window.resetGLStates();

	sfg::SFGUI sfgui;

	winName.push_back("Window One");
	winName.push_back("Window Two");
	winName.push_back("Window Three");


    w.CreateWindow(winName[0], winWidth, winHeight);
    w.CreateWindow(winName[1], winWidth2, winHeight2);
    w.CreateWindow(winName[2], winWidth2, winHeight2);

    w.Button(buttonName, pos_X, pos_Y);


	while ( app_window.isOpen() )
    {

		sf::Event event;

		while ( app_window.pollEvent( event ) )
        {
			// Every frame we have to send SFML events to the window
			// to enable user interactivity. Without doing this your
			// GUI is nothing but a big colorful picture ;)
            D.HandleEvent(event);

			if ( event.type == sf::Event::Closed )
            {
				app_window.close();
			}
		}
		D.Update(0.f);
		app_window.clear();

		// After drawing the rest of your game, you have to let the GUI
		// render itself. If you don't do this you will never be able
		// to see it ;)
		sfgui.Display( app_window );

		// NOTICE
		// Because the window doesn't have any children it will shrink to
		// it's minimum size of (0,0) resulting in you not seeing anything
		// except the title bar text ;) Don't worry, in the Label example
		// you'll get to see more.


		app_window.display();
	}

	return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.