SFML Draw Issue

Hey guys,

I seem to be having an issue when trying to draw Buttons on to the screen. I have the button class stored as a vector pointer. below is the code for my draw function and the entire cpp file. Not sure if i'm doing something wrong that i shouldn't do, some advice would be much appreciated.

draw function
1
2
3
4
5
6
7
8
9
10
void Menu::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
	target.draw(m_BackgroundImage);

	std::vector<Button*>::const_iterator button;
	for (button = m_pButtons.cbegin(); button != m_pButtons.cend(); ++button)
	{
		target.draw(button);
	}
}


Menu.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <Game\Game_Main\menu.h>

Menu::Menu()
{
	//Button Objects
	m_pButtons.resize(3);
	m_pButtons.at(0) = new Button(100, 18.5, math::Vector2D(250, 180));
	m_pButtons.at(1) = new Button(100, 18.5, math::Vector2D(250, 210));
	m_pButtons.at(2) = new Button(100, 18.5, math::Vector2D(250, 250));

	//////////////////////////////////////////////////////////////////////////////////////
	//Textures
	m_pTextureManager = TextureManager::getInstance();
	this->loadTexture();

	//Main Menu Background Texture
	this->m_BackgroundImage.setTexture(this->m_pTextureManager->getReference("Main Menu"));
	/////////////////////////////////////////////////////////////////////////////////////

	//Delete All Objects
	this->deleteObjects();
}

Menu::~Menu()
{

}

void Menu::loadTexture()
{
	m_pTextureManager->loadTexture("Main Menu", "assests\\main_background_art\\menuscreen.png");
	m_pTextureManager->loadTexture("Button", "assests\\main_background_art\\Button.png");
}

void Menu::deleteObjects()
{
	std::vector<Button*>::iterator button;
	for (button = m_pButtons.begin(); button != m_pButtons.end(); ++button)
	{
		delete (*button);
	}
	m_pButtons.clear();
}

void Menu::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
	target.draw(m_BackgroundImage);

	std::vector<Button*>::const_iterator button;
	for (button = m_pButtons.cbegin(); button != m_pButtons.cend(); ++button)
	{
		target.draw(button);
	}
}


Menu header file

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
#pragma once

#ifndef _MENU_H
#define _MENU_H

#include <SFML\Graphics.hpp>
#include <Graphics\Texture\texturemanager.h>
#include <Game\Game_Main\background.h>
#include <GUI\button.h>

#include <vector>

class Menu : public sf::Drawable
{
	public:
		Menu();
		~Menu();

		void draw(sf::RenderTarget& target, sf::RenderStates states) const;
		void loadTexture();
		void deleteObjects();


	private:
		TextureManager* m_pTextureManager;
		sf::Sprite m_BackgroundImage;
		std::vector<Button*> m_pButtons;


};
#endif // !_MENU_H 


Thanks in advance.
Last edited on
I seem to be having an issue when trying to draw Buttons on to the screen.

Perhaps you could be a little more specific about the issue you're having.
Sorry my bad, The error I'm getting is no instance of overloaded function "sf::RenderTarget::draw" matches the argument list
argument types are: (std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<Button *>>>)
object type is: sf::RenderTarget
1
2
3
4
5
6
7
8
9
10
void Menu::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
	target.draw(m_BackgroundImage);

	std::vector<Button*>::const_iterator button;
	for (button = m_pButtons.cbegin(); button != m_pButtons.cend(); ++button)
	{
		target.draw(**button); // dereference the iterator and the pointer.
	}
}


Already gave that a shot the error message I get is " conversion to inaccessible to base class "sf::Drawable" is not allowed"
Already gave that a shot the error message I get is " conversion to inaccessible to base class "sf::Drawable" is not allowed"

Then you need to change your design so that it is allowed, or take a different approach.
Ah ok thanks
Found my problem. It was an idiotic mistake in the Button header file.

Had this
1
2
3
4
5
6
7
8
9
10
11
12
class Button : public Rectangle, sf::Drawable
{
	public:
		Button();
		Button(int, int, math::Vector2D);
		~Button();

		void draw(sf::RenderTarget &target, sf::RenderStates states) const;

	protected:
		sf::RectangleShape m_ButtonShape;
};


When I should have had this
1
2
3
4
5
6
7
8
9
10
11
12
class Button : public Rectangle, public sf::Drawable
{
	public:
		Button();
		Button(int, int, math::Vector2D);
		~Button();

		void draw(sf::RenderTarget &target, sf::RenderStates states) const;

	protected:
		sf::RectangleShape m_ButtonShape;
};
Topic archived. No new replies allowed.