I would like to just clarify that you are not actually using KeyPressed events (http://www.sfml-dev.org/tutorials/2.4/window-events.php#the-keypressed-and-keyreleased-events), which are sent to the window when they happen; you check events using the pollEvent (usually) method.
You are using the real-time states of the keys (http://www.sfml-dev.org/tutorials/2.4/window-inputs.php#keyboard). That is, you check to see - in real-time - the current state (pressed or not pressed) of a specific key at the time of checking.
Most common way to separate the two (for key presses) is to use events for single presses (i.e. do something when the key is pressed and not again while it is held - unless key repeat is still active, e.g. jump/shoot) and to use real-time states for held presses (e.g. move/fire continuously...)
I should totally have known what the difference is since I have the below header file for checking if the close event occurs. Sorry this is all new to me!
Slowly learning...I went from one massive int main (over 500 lines of code) to now using header files with functions and classes so that int main is 85 lines of code roughly. Now I struggle with when to use multiple .cpp files...(i get they are intended to define what is in the shared header file), but ah that goes out of the scope of this thread.
I appreciate your help Hapax!
I found the code drop down button as well for the tags lol.
#pragma once
#ifndef POLL_EVENT_H
#define POLL_EVENT_H
void close_event_checker(sf::RenderWindow &thatwindow)
{
sf::Event close_event;
thatwindow.pollEvent(close_event);
if (close_event.type == sf::Event::Closed)
{
thatwindow.close();
}
}
#endif
Again, you're welcome :)
Note that events may not be in any particular order (there may be multiple events waiting) and can only be "polled" once each. After that, they're gone.
With all that in mind, your "close checker" is problematic:
It only polls once; it should (preferably) be polling continuously until there are no more events to poll
e.g.
while (window.pollEvent(event))
{
// check event
}
This checks all of the events waiting - one at a time - and deals with them there and then.
Any other event would be ignored and discarded.
If you receive an event saying, for example, that the window has resized or a mouse button has been pressed, your close checker function would discard that event.
Again, the code above that polls all of the events is the main solution to this.
The "check event" part in the code above could pass the event to functions that deal with different types.
e.g.
while (window.pollEvent(event))
{
check_close_event(window, event);
check_mouse_events(event);
check_keyboard_events(event);
}
Then the checker would be a little smaller and simpler:
void check_close_event(sf::RenderWindow &thatwindow, sf::Event event)
{
if (event.type == sf::Event::Closed)
{
thatwindow.close();
}
}
As for headers and source files...
Generally, headers would contain only declarations and source files would contain definitions (the actual code).
Source (e.g. .cpp) files are compiled separately and are self-contained.
Header (e.g. .hpp or just .h) files are generally "included" - technically just copy and pasted where they are included.
This may not be the best description/explanation. Google knows more than I do ;)