SFML community forums
Help => General => Topic started by: Makuto on August 22, 2011, 07:22:12 pm
-
Hi, I'm having trouble programming my own input class. Here is the basic code I'm having problems with:
class input
{
sf::RenderWindow *iApp;
sf::Input Input;
input(sf::RenderWindow *window)
{
iApp=window;
}
//////THIS FUNCTION MUST BE CALLED EVERY FRAME TO GET NEW //INPUTS
void refreshInput()
{
Input=iApp->GetInput();
}
////////////////////////////////////////////////////////////////
bool checkKey(sf::Key::Code code)
{
return Input.IsKeyDown(code);
}
void checkMouse(unsigned int *x, unsigned int *y)
{
*x=Input.GetMouseX();
*y=Input.GetMouseY();
}
bool checkMouseKey(sf::Mouse::Button button)
{
return Input.IsMouseButtonDown(button);
}
};
My compiler (code::blocks gcc) outputs this error:
..\..\..\..\..\..\..\..\SFML-1.6\include\SFML\System\NonCopyable.hpp||In member function `sf::Input& sf::Input::operator=(const sf::Input&)':|
..\..\..\..\..\..\..\..\SFML-1.6\include\SFML\System\NonCopyable.hpp|64|error: `sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCopyable&)' is private|
C:\Documents and Settings\My Documents\Development\Programming\C++\My Projects\Game Lib Testing\main.cpp|18|error: within this context|
||=== Build finished: 2 errors, 0 warnings ===|
Thanks for the help...
-
Use a reference there:
sf::Input& Input;
-
There is still an error:
C:\Documents and Settings\Macoy\My Documents\Development\Programming\C++\My Projects\Game Lib Testing\main.cpp||In constructor `input::input(sf::RenderWindow*)':|
C:\Documents and Settings\Macoy\My Documents\Development\Programming\C++\My Projects\Game Lib Testing\main.cpp|12|error: uninitialized reference member `input::Input'|
..\..\..\..\..\..\..\..\SFML-1.6\include\SFML\System\NonCopyable.hpp||In member function `sf::Input& sf::Input::operator=(const sf::Input&)':|
..\..\..\..\..\..\..\..\SFML-1.6\include\SFML\System\NonCopyable.hpp|64|error: `sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCopyable&)' is private|
C:\Documents and Settings\Macoy\My Documents\Development\Programming\C++\My Projects\Game Lib Testing\main.cpp|18|error: within this context|
||=== Build finished: 3 errors, 0 warnings ===|
-
References are constant. Use the constructor initializer list to initialize them.
But why do you even need a separate sf::Input member? As you already store sf::RenderWindow, you can directly call GetInput().
-
I don't understand exactly what you're saying... will you just edit my original code from the first code to show me what you mean? Sorry for the dumbness...
-
By accessing it directly he means you can just do this:
renderWindow.GetInput( ).IsKeyDown( code )
instead of having to store your own external reference to the input object.
-
Sorry for the dumbness...
You're not dumb, you should just take the time to read a C++ book ;)
You probably don't understand my statement because you lack some knowledge about constructor initializer lists or references. They would be explained in a good book (like Accelerated C++, C++ Primer or Thinking in C++), the invested time really pays off!
-
Oh thanks CodeCriminal and Nexus. I'm working on Dietal's C++ How to Program (which is awesome), so I know the basics, but I'm still working on classes, pointers, etc. I just made my first linked list yesterday :(
The code works great. Sorry for the waste of time :)