Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with my Game class and const sf::Input  (Read 2612 times)

0 Members and 1 Guest are viewing this topic.

Kazius

  • Newbie
  • *
  • Posts: 3
    • View Profile
Problem with my Game class and const sf::Input
« 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:
Code: [Select]

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:
Code: [Select]

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.
Code: [Select]

if (screen->GetInput().IsKeyDown(Key::D))
{
player->Move(500*eTime,0);
}

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Re: Problem with my Game class and const sf::Input
« Reply #1 on: February 20, 2010, 06:54:45 pm »
GetInput() returns a constant reference.
Try:
Code: [Select]
const sf::Input& input;
Pluma - Plug-in Management Framework

Kazius

  • Newbie
  • *
  • Posts: 3
    • View Profile
Problem with my Game class and const sf::Input
« Reply #2 on: February 20, 2010, 08:52:43 pm »
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.

t0rento

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Problem with my Game class and const sf::Input
« Reply #3 on: February 22, 2010, 03:19:58 pm »
This is the type of issue a constructor is designed for. Ideally you use an initializer list.

Code: [Select]

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.

Kazius

  • Newbie
  • *
  • Posts: 3
    • View Profile
Problem with my Game class and const sf::Input
« Reply #4 on: February 22, 2010, 08:20:08 pm »
aye, thanks. It is working now fine. I forgot that there is something like the initializer list  :lol: .
greetz