I have created a card class that implements Drawable class, and I have also override the function draw, but after I have created the card object and tried to draw it using window.draw, it gave me an error saying that the card object I created is not a type of drawable. When I tried to cast card object to a drawable using (sf::Drawable)card, it says the draw function is not implemented. I am very confused.
here is my code
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
class Card: public sf::Drawable, sf::Transformable
{
public:
Card();
Card(int v, char c);
void setPos(sf::Vector2f v);
sf::Vector2f getPos();
private:
virtual void draw(sf::RenderTarget &window, sf::RenderStates state) const;
void creatCard();
sf::RectangleShape cardBackground;
sf::Text faceValue;
sf::Sprite suit;
int _value;
char _suit;
sf::Vector2f pos;
};
Card::Card()
{
_value = 1;
_suit = 'H';
creatCard();
}
void Card::draw(sf::RenderTarget &window, sf::RenderStates state) const
{
window.draw(cardBackground);
window.draw(faceValue);
}
void Card::creatCard()
{
sf::Image img;
sf::Texture texture;
switch (toupper(_suit)) {
case 'S':
if(!img.loadFromFile("card_suits.png"))
cout << "Fail to load card_suits.png" << endl;
break;
case 'H':
if(!img.loadFromFile("card_suits2.png"))
cout << "Fail to load card_suits.png" << endl;
break;
case 'D':
if(!img.loadFromFile("card_suits3.png"))
cout << "Fail to load card_suits.png" << endl;
break;
case 'C':
if(!img.loadFromFile("card_suits4.png"))
cout << "Fail to load card_suits.png" << endl;
}
texture.loadFromImage(img);
suit.setTexture(texture);
cardBackground.setSize(sf::Vector2f(300, 600));
cardBackground.setFillColor(sf::Color::White);
}
int main() {
sf::RenderWindow window(sf::VideoMode(500, 500, 32), "Test");
Card card();
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event)){
if (event.type == sf::Event::Closed){
window.close();
}
}
window.clear(sf::Color::Black);
window.draw(card);
window.display();
}
return 0;
}