I've been trying to get into object oriented C++ for the last couple of weeks, so today I tried to start a project (that being a simple calculator
, everybody's gotta start somewhere). So, I made a simple Button class.
Basically, what I have in Button.h is:
class Button : public sf::Drawable
{
private:
sf::RectangleShape rect;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
.....
}
While in Button.cpp I have
void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(rect, states);
}
The errors that pop up are:
D:\SFML projects\Calculator\Button.cpp|74|error: non-member function 'void draw(sf::RenderTarget&, sf::RenderStates)' cannot have cv-qualifier|
D:\SFML projects\Calculator\Button.cpp||In function 'void draw(sf::RenderTarget&, sf::RenderStates)':|
D:\SFML projects\Calculator\Button.cpp|76|error: 'rect' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|
Built with Code::Blocks(MinGW) and SFML 2.1 (32-bit SJLJ version).
I'm fairly certain I'm doing something wrong, so I'd like some help in understanding how sf::Drawable should be integrated into my class.
All help would be really appreciated.
Full code(didn't find a spoiler tag, so I guess I'll have to leave it like this):
Button.h
#ifndef ADD_H
#define ADD_H
#include "SFML/Graphics.hpp"
class Button : public sf::Drawable
{
bool markedflag;
sf::RectangleShape rect;
sf::Color unmarked, marked, clicked;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
public:
Button();
Button(int x, int y, int a, int b);
Button(sf::Vector2f a, sf::Vector2f b);
Button(sf::IntRect a);
void checkMouse(sf::Vector2f mous);
void setRect ();
void setRect (int x, int y, int a, int b);
void setRect (sf::Vector2f a, sf::Vector2f b);
void setRect (sf::IntRect a);
void setColors (sf::Color col1, sf::Color col2, sf::Color col3);
void updateColor ();
};
#endif
Button.cpp
#include "Button.h"
Button::Button()
{
rect.setPosition(0, 0);
rect.setSize(sf::Vector2f(0, 0));
}
Button::Button(int x, int y, int a, int b)
{
rect.setPosition(x, y);
rect.setSize(sf::Vector2f(a, b));
}
Button::Button(sf::Vector2f a, sf::Vector2f b)
{
rect.setPosition(a);
rect.setSize(b);
}
Button::Button(sf::IntRect a)
{
rect.setPosition(a.left, a.top);
rect.setSize(sf::Vector2f(a.width, a.height));
}
void Button::setRect()
{
rect.setPosition(0, 0);
rect.setSize(sf::Vector2f(0, 0));
}
void Button::setRect(int x, int y, int a, int b)
{
rect.setPosition(x, y);
rect.setSize(sf::Vector2f(a, b));
}
void Button::setRect(sf::Vector2f a, sf::Vector2f b)
{
rect.setPosition(a);
rect.setSize(b);
}
void Button::setRect(sf::IntRect a)
{
rect.setPosition(a.left, a.top);
rect.setSize(sf::Vector2f(a.width, a.height));
}
void Button::setColors(sf::Color col1, sf::Color col2, sf::Color col3)
{
unmarked=col1;
marked=col2;
clicked=col3;
}
void Button::updateColor()
{
if (markedflag==0) rect.setFillColor(unmarked);
else if (markedflag==1) rect.setFillColor(marked);
else rect.setFillColor(clicked);
}
void Button::checkMouse(sf::Vector2f mous)
{
sf::Vector2f pos=rect.getPosition(), siz=rect.getSize();
if (pos.x<=mous.x&&pos.y<=mous.y&&siz.x>=mous.x&&siz.y>=mous.y) {if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) markedflag=2;
else markedflag=1;}
else markedflag=0;
}
void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(rect, states);
}