#include <SFML/Graphics.hpp>
#include <iostream>
#include "Player.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(512, 512), "SFML testing", sf::Style::Close | sf::Style::Resize);
Player player = Player();
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
switch (evnt.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::TextEntered:
if (evnt.text.unicode < 128)
{
printf("%c", evnt.text.unicode);
}
}
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
sf::Vector2i mousepos = sf::Mouse::getPosition(window);
player.setPosition(sf::Vector2f(static_cast<float>(mousepos.x), static_cast<float>(mousepos.y)));
}
window.clear();
player.render(window);
window.display();
}
return 0;
}
#pragma once
#include <SFML/Graphics.hpp>
class Player
{
public:
Player();
~Player();
void setPosition(sf::Vector2f position);
sf::RectangleShape sprite_;
void render(sf::RenderWindow &window);
};
#include "Player.h"
Player::Player()
{
sf::RectangleShape sprite_(sf::Vector2f(100.f, 100.f));
sprite_.setFillColor(sf::Color::Cyan);
sprite_.setOrigin(sf::Vector2f(50.f, 50.f));
}
Player::~Player()
{
}
void Player::setPosition(sf::Vector2f position)
{
sprite_.setPosition(position);
}
void Player::render(sf::RenderWindow &window)
{
window.draw(sprite_);
}
Basically i'm trying to create a Player object with a cyan rectangle as a sprite that moves to my mousepos when i press the left mouse button. To show the rectangle in the window, i'm trying to pass my window to the render function in the player object. Unfortunately i just get a black window and nothing happens when i left click it. There are no error messages or anything, it runs fine but doesn't do anything. I'm guessing there is probably something wrong with the constructor and the sprite_ is not initialized correctly.
Now obviously i'm pretty inexperienced with C++ and need to pick up a book to learn the language properly, but i'd really like to solve this now, i'm sure it's a simple fix and it would help me immensely if someone could point me in the right direction.