Can you change the name space to sf(with sfml) so that you won't need to write sf:: each time? |
Yup its just like with std namespace. You can do
using namespace sf;
using sf::event;
, or just write sf::Event (Which is probably the best way. Also by this I mean put sf:: in front of everything in the sf namespace not just type sf::Event.). Though I wouldn't recommend including the whole sf namespace, but it is up to you.
And... another thing. It may be very soon for this... but how can you handle key presses and move the thing. |
Well in SFML you can handle event's with the window.pollEvent loop. Which is this in the code above.
1 2 3 4 5 6
|
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
|
That is called your event loop which will handle all the events (Like when someone presses a key on the keyboard).
We only have one event in there so far which is sf::Event::Closed which will close the window whenever a close event is generated (Like pressing the X button, or pressing exit hotkeys like alt-f4).
If we wanted to handle when a player presses the W key we could do something like this.
1 2 3 4 5 6
|
if (event.type == sf::Event::KeyPress) // Checks if a key was pressed
{
// If so we figure out which key was pressed
if (event.key.code == sf::Keyboard::W)
// Do whatever we want for when W was pressed.
}
|
We would then define whatever keys we want to watch input for inside of the first if statement and define whatever actions for the individual keys.
More info on it can be found here http://www.sfml-dev.org/tutorials/2.1/window-events.php .
Though one you get the basics down you will generally make a class that deals with handling input.