I am making this small project/game and I ran into a problem. I need my sprite(player) to rotate with mouse cursor. So where cursor is in window there my sprite rotates, and I got it to work. But now I need a View to follow my sprite, and when I set a View it seems that my Origin resets to 0,0.
Main:
#include <iostream>
#include "SFML/Graphics.hpp"
#include "Player.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(1024, 768), "Game");
sf::Event sfEvent;
Player* player = new Player("soldier.png");
sf::View camera;
camera.setSize(sf::Vector2f(1024, 768));
while (window.isOpen())
{
while (window.pollEvent(sfEvent))
{
if (sfEvent.type == sf::Event::EventType::Closed)
{
window.close();
}
}
player->movement();
player->facingDirection(window);
player->setOrigin();
camera.setCenter(player->getOrigin());
window.clear();
window.setView(camera);
player->draw(window);
window.display();
}
}
Player.cpp:
#include "Player.h"
Player::Player(std::string textureName)
{
_texture = new sf::Texture;
_texture->loadFromFile(P_TEX_PATH + textureName);
_sprite = new sf::Sprite(*_texture);
}
Player::~Player()
{
delete _texture;
delete _sprite;
}
void Player::movement()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
{
_sprite->move(-_speed, 0.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
{
_sprite->move(_speed, 0.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
{
_sprite->move(0.0f, -_speed);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
{
_sprite->move(0.0f, _speed);
}
}
void Player::facingDirection( sf::RenderWindow &window)
{
mousePosition = sf::Mouse::getPosition(window);
_spritePosition = _sprite->getPosition();
float dx = mousePosition.x - _spritePosition.x;
float dy = mousePosition.y - _spritePosition.y;
_rotation = (atan2(dy, dx)) * 180 / 3.14159265 ;
_sprite->setRotation(_rotation);
}
sf::Vector2f Player::getLocation()
{
return _sprite->getPosition();
}
sf::Vector2f Player::getOrigin()
{
return _sprite->getOrigin();
}
float Player::getSpriteOrientation()
{
return _sprite->getRotation();
}
void Player::setOrigin()
{
_scaleW = _sprite->getLocalBounds().width;
_scaleH = _sprite->getLocalBounds().height;
_scaleW = _scaleW / 2;
_scaleH = _scaleH / 2;
_sprite->setOrigin(_scaleH,_scaleW - 4.f);
}
void Player::draw(sf::RenderTarget& window)
{
window.draw(*_sprite);
}
Player.h
#pragma once
#include <iostream>
#include <string>
#include "SFML/Graphics.hpp"
#define P_TEX_PATH "textures\\player\\"
class Player
{
public:
Player(std::string textureName);
~Player();
void draw(sf::RenderTarget& window);
void movement();
void facingDirection(sf::RenderWindow &window);
sf::Vector2f getLocation();
sf::Vector2f getOrigin();
float getSpriteOrientation();
void setOrigin();
sf::Vector2i mousePosition;
private:
sf::Sprite* _sprite;
sf::Texture* _texture;
float _size = 20.f;
float _speed = 0.020f;
sf::Vector2f _spritePosition;
float _rotation = 0;
float _scaleW = 0;
float _scaleH = 0;
float _locationX = 0;
float _locationY = 0;
};
Any feedback about the rest of the code would be helpfull, ty