My character won't face a different direction when i hit one of the WASD keys, it will face another direction when i change the direction in Crop player though, but it wont do anything else. the code compiles and I'm able to move the character around the screen but it just wont change the direction its facing.
/*
//
*/
#include <SFML/Graphics.hpp>
#include <SFGUI/SFGUI.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Game
{
public:
void Program();
void CreateCharacters();
void LoadCharacters();
void CropSprite();
void UserInput();
void Collision();
private:
vector<sf::Vector2f> player_pos;
vector<sf::Sprite> load_sprites;
vector<sf::Texture> load_textures;
sf::Sprite sprite;
sf::Texture texture;
sf::Vector2i source;
enum Direction{Down, Left, Right, Up};
};
class GUI : public Game
{
public:
void CreateMenu();
void CloseMenu();
void MenuButtons();
private:
sfg::SFGUI sfgui;
sfg::Desktop dtop;
sfg::Window::Ptr menu_window;
sfg::Button::Ptr menu_exit;
sfg::Box::Ptr box_HOR;
sfg::Box::Ptr box_VER;
};
void GUI::CreateMenu()
{
box_HOR = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL);
box_HOR->Pack(menu_exit, false, true);
}
void GUI::MenuButtons()
{
menu_exit = sfg::Button::Create("Exit");
}
//Set characters to a sprite.
void Game::CreateCharacters()
{
sprite.setTexture(texture);
CropSprite();
}
//This will be a vector later and it will load all
//characters individually.
void Game::LoadCharacters()
{
if(!texture.loadFromFile("Resources/Characters/Player.png"))
{
cout << "Error loading texture" << endl;
}
CreateCharacters();
}
//We dont want our character to be the entire sprite sheet
//so we crop out the character and its position that we want.
void Game::CropSprite()
{
source.x = 1;
source.y = Left;
sprite.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
}
void Game::UserInput()
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
source.y = Up;
sprite.move(0, -1.3);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
source.y = Left;
sprite.move(-1.3, 0);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
source.y = Down;
sprite.move(0, 1.3);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
source.y = Right;
sprite.move(1.3, 0);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
//menu_exit
}
}
void Game::Collision()
{
}
void Game::Program()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "App");
window.setFramerateLimit(60);
window.resetGLStates();
LoadCharacters();
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
window.clear(sf::Color::White);
UserInput();
window.draw(sprite);
window.display();
}
}
int main()
{
Game game;
game.Program();
return 0;
}