Sep 26, 2014 at 6:43pm UTC
And what should that line do?
Sep 26, 2014 at 6:46pm UTC
Last edited on Sep 26, 2014 at 6:47pm UTC
Sep 26, 2014 at 8:57pm UTC
Alrigth sweet I got it working, but now I have another problem, the character is supposed to turn in the direction i specify (W A S D) but She isnt so I think the variables are being destroyed when it leaves block scope, but it shouldnt do that because the functions are a part of the class right? Also I have a GUI class as you can see, and I need to use my variables from it in the Program() function but it's already a member function of Game so how do i use the variable from GUI class in it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/*
//
*/
#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 = Down;
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;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
source.y = Left;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
source.y = Down;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
source.y = Right;
}
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);
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;
}
Last edited on Sep 26, 2014 at 9:00pm UTC
Sep 26, 2014 at 9:02pm UTC
You do not change your sprite after key is pressed. You just changing some values in vector, that all.
Sep 26, 2014 at 9:10pm UTC
well in the CropSprite() function if i change: source.y = Down; to Up, it will change the direction the player is facing so doing what i have should work.
Sep 26, 2014 at 9:15pm UTC
No. CropSprite is called only from CreateCharacters which is only called from LoadCharacters which is callde only once before your game loop. After that you do not touch your sprite, so it never changes.
Sep 26, 2014 at 10:01pm UTC
So I need to call cropped sprite again? The image only needs cropping once so will it mess anything up by putting it in a loop that will continuously execute it?
Sep 27, 2014 at 7:47am UTC
That depends on how do you want to process your sprite, You can recreate it, replace it with another, etc.
Sep 27, 2014 at 7:29pm UTC
Ok I got it working, I added CropSprite in the beginning of UserInput()