SFML community forums
Help => General => Topic started by: Kazius on February 20, 2010, 06:36:33 pm
-
Hi guys. I am new to sfml, so i am still testing it.
First here is the beginning of my Game class:
class c_Game
{
private:
sf::RenderWindow* screen;
sf::Event* events;
c_Object* player;
c_Object* object;
float eTime;
const sf::Input input;
public:
c_Game()
{
screen = new sf::RenderWindow(VideoMode(800,600,32), "hi");
screen->UseVerticalSync(true);
player=new c_Object(0,0,100,100,sf::Color(0,255,0,100));
object=new c_Object(300,300,100,100,sf::Color(100,100,0,100));
events=new sf::Event();
input=screen->GetInput();
}
.
.
.
So, you can see it contains my window, events, my timer...and my sf::Input. And there is my problem: The function screen->(GetInput) returns a constant sf::Input. But i can't declare a const before, because i don't have my window yet.
Is there a way to get sf::Input without const?
Normally i wanted to handle my input like this:
if (input.IsKeyDown(Key::D))
{
player->Move(500*eTime,0);
}
If i leave out the variable input and use screen->GetInput() it works, but i don't think it's very efficient.
if (screen->GetInput().IsKeyDown(Key::D))
{
player->Move(500*eTime,0);
}
-
GetInput() returns a constant reference.
Try:
const sf::Input& input;
-
Hi. Yes that was a mistake in my code, but i wasn't my problem. I have to declare a const variable before, but there i have no window and so i can't initialize it.
-
This is the type of issue a constructor is designed for. Ideally you use an initializer list.
class c_Game
{
private:
sf::RenderWindow screen;
sf::Event events;
c_Object player;
c_Object object;
float eTime;
const sf::Input& input;
public:
c_Game() :
screen(VideoMode(800,600,32), "Hi"),
player(0,0,100,100,sf::Color(0,255,0,100)),
object(300,300,100,100,sf::Color(100,100,0,100)),
input(screen.GetInput())
{
screen->UseVerticalSync(true);
}
.
.
.
That should work.
-
aye, thanks. It is working now fine. I forgot that there is something like the initializer list :lol: .
greetz