├── .gitignore ├── 01_Intro ├── Include │ └── Book │ │ ├── Game.hpp │ │ ├── StringHelpers.hpp │ │ └── StringHelpers.inl ├── Media │ ├── Sansation.ttf │ └── Textures │ │ └── Eagle.png └── Source │ ├── CMakeLists.txt │ ├── Game.cpp │ └── Main.cpp ├── 02_Resources ├── Include │ └── Book │ │ ├── ResourceHolder.hpp │ │ └── ResourceHolder.inl ├── Media │ └── Textures │ │ ├── Desert.png │ │ └── Eagle.png └── Source │ ├── CMakeLists.txt │ └── Main.cpp ├── 03_World ├── Include │ └── Book │ │ ├── Aircraft.hpp │ │ ├── Entity.hpp │ │ ├── Foreach.hpp │ │ ├── Game.hpp │ │ ├── ResourceHolder.hpp │ │ ├── ResourceHolder.inl │ │ ├── ResourceIdentifiers.hpp │ │ ├── SceneNode.hpp │ │ ├── SpriteNode.hpp │ │ ├── StringHelpers.hpp │ │ ├── StringHelpers.inl │ │ └── World.hpp ├── Media │ ├── Sansation.ttf │ └── Textures │ │ ├── Desert.png │ │ ├── Eagle.png │ │ └── Raptor.png └── Source │ ├── Aircraft.cpp │ ├── CMakeLists.txt │ ├── Entity.cpp │ ├── Game.cpp │ ├── Main.cpp │ ├── SceneNode.cpp │ ├── SpriteNode.cpp │ └── World.cpp ├── 04_Input ├── Include │ └── Book │ │ ├── Aircraft.hpp │ │ ├── Category.hpp │ │ ├── Command.hpp │ │ ├── CommandQueue.hpp │ │ ├── Entity.hpp │ │ ├── Foreach.hpp │ │ ├── Game.hpp │ │ ├── Player.hpp │ │ ├── ResourceHolder.hpp │ │ ├── ResourceHolder.inl │ │ ├── ResourceIdentifiers.hpp │ │ ├── SceneNode.hpp │ │ ├── SpriteNode.hpp │ │ ├── StringHelpers.hpp │ │ ├── StringHelpers.inl │ │ └── World.hpp ├── Media │ ├── Sansation.ttf │ └── Textures │ │ ├── Desert.png │ │ ├── Eagle.png │ │ └── Raptor.png └── Source │ ├── Aircraft.cpp │ ├── CMakeLists.txt │ ├── Command.cpp │ ├── CommandQueue.cpp │ ├── Entity.cpp │ ├── Game.cpp │ ├── Main.cpp │ ├── Player.cpp │ ├── SceneNode.cpp │ ├── SpriteNode.cpp │ └── World.cpp ├── 05_States ├── Include │ └── Book │ │ ├── Aircraft.hpp │ │ ├── Application.hpp │ │ ├── Category.hpp │ │ ├── Command.hpp │ │ ├── CommandQueue.hpp │ │ ├── Entity.hpp │ │ ├── Foreach.hpp │ │ ├── GameState.hpp │ │ ├── LoadingState.hpp │ │ ├── MenuState.hpp │ │ ├── ParallelTask.hpp │ │ ├── PauseState.hpp │ │ ├── Player.hpp │ │ ├── ResourceHolder.hpp │ │ ├── ResourceHolder.inl │ │ ├── ResourceIdentifiers.hpp │ │ ├── SceneNode.hpp │ │ ├── SpriteNode.hpp │ │ ├── State.hpp │ │ ├── StateIdentifiers.hpp │ │ ├── StateStack.hpp │ │ ├── TitleState.hpp │ │ ├── Utility.hpp │ │ ├── Utility.inl │ │ └── World.hpp ├── Media │ ├── Sansation.ttf │ └── Textures │ │ ├── Desert.png │ │ ├── Eagle.png │ │ ├── Raptor.png │ │ └── TitleScreen.png └── Source │ ├── Aircraft.cpp │ ├── Application.cpp │ ├── CMakeLists.txt │ ├── Command.cpp │ ├── CommandQueue.cpp │ ├── Entity.cpp │ ├── GameState.cpp │ ├── LoadingState.cpp │ ├── Main.cpp │ ├── MenuState.cpp │ ├── ParallelTask.cpp │ ├── PauseState.cpp │ ├── Player.cpp │ ├── SceneNode.cpp │ ├── SpriteNode.cpp │ ├── State.cpp │ ├── StateStack.cpp │ ├── TitleState.cpp │ ├── Utility.cpp │ └── World.cpp ├── 06_Menus ├── Include │ └── Book │ │ ├── Aircraft.hpp │ │ ├── Application.hpp │ │ ├── Button.hpp │ │ ├── Category.hpp │ │ ├── Command.hpp │ │ ├── CommandQueue.hpp │ │ ├── Component.hpp │ │ ├── Container.hpp │ │ ├── Entity.hpp │ │ ├── Foreach.hpp │ │ ├── GameState.hpp │ │ ├── Label.hpp │ │ ├── MenuState.hpp │ │ ├── PauseState.hpp │ │ ├── Player.hpp │ │ ├── ResourceHolder.hpp │ │ ├── ResourceHolder.inl │ │ ├── ResourceIdentifiers.hpp │ │ ├── SceneNode.hpp │ │ ├── SettingsState.hpp │ │ ├── SpriteNode.hpp │ │ ├── State.hpp │ │ ├── StateIdentifiers.hpp │ │ ├── StateStack.hpp │ │ ├── TitleState.hpp │ │ ├── Utility.hpp │ │ ├── Utility.inl │ │ └── World.hpp ├── Media │ ├── Sansation.ttf │ └── Textures │ │ ├── ButtonNormal.png │ │ ├── ButtonPressed.png │ │ ├── ButtonSelected.png │ │ ├── Desert.png │ │ ├── Eagle.png │ │ ├── Raptor.png │ │ └── TitleScreen.png └── Source │ ├── Aircraft.cpp │ ├── Application.cpp │ ├── Button.cpp │ ├── CMakeLists.txt │ ├── Command.cpp │ ├── CommandQueue.cpp │ ├── Component.cpp │ ├── Container.cpp │ ├── Entity.cpp │ ├── GameState.cpp │ ├── Label.cpp │ ├── Main.cpp │ ├── MenuState.cpp │ ├── PauseState.cpp │ ├── Player.cpp │ ├── SceneNode.cpp │ ├── SettingsState.cpp │ ├── SpriteNode.cpp │ ├── State.cpp │ ├── StateStack.cpp │ ├── TitleState.cpp │ ├── Utility.cpp │ └── World.cpp ├── 07_Gameplay ├── Include │ └── Book │ │ ├── Aircraft.hpp │ │ ├── Application.hpp │ │ ├── Button.hpp │ │ ├── Category.hpp │ │ ├── Command.hpp │ │ ├── CommandQueue.hpp │ │ ├── Component.hpp │ │ ├── Container.hpp │ │ ├── DataTables.hpp │ │ ├── Entity.hpp │ │ ├── Foreach.hpp │ │ ├── GameOverState.hpp │ │ ├── GameState.hpp │ │ ├── Label.hpp │ │ ├── MenuState.hpp │ │ ├── PauseState.hpp │ │ ├── Pickup.hpp │ │ ├── Player.hpp │ │ ├── Projectile.hpp │ │ ├── ResourceHolder.hpp │ │ ├── ResourceHolder.inl │ │ ├── ResourceIdentifiers.hpp │ │ ├── SceneNode.hpp │ │ ├── SettingsState.hpp │ │ ├── SpriteNode.hpp │ │ ├── State.hpp │ │ ├── StateIdentifiers.hpp │ │ ├── StateStack.hpp │ │ ├── TextNode.hpp │ │ ├── TitleState.hpp │ │ ├── Utility.hpp │ │ ├── Utility.inl │ │ └── World.hpp ├── Media │ ├── Sansation.ttf │ └── Textures │ │ ├── Avenger.png │ │ ├── Bullet.png │ │ ├── ButtonNormal.png │ │ ├── ButtonPressed.png │ │ ├── ButtonSelected.png │ │ ├── Desert.png │ │ ├── Eagle.png │ │ ├── FireRate.png │ │ ├── FireSpread.png │ │ ├── HealthRefill.png │ │ ├── Missile.png │ │ ├── MissileRefill.png │ │ ├── Raptor.png │ │ └── TitleScreen.png └── Source │ ├── Aircraft.cpp │ ├── Application.cpp │ ├── Button.cpp │ ├── CMakeLists.txt │ ├── Command.cpp │ ├── CommandQueue.cpp │ ├── Component.cpp │ ├── Container.cpp │ ├── DataTables.cpp │ ├── Entity.cpp │ ├── GameOverState.cpp │ ├── GameState.cpp │ ├── Label.cpp │ ├── Main.cpp │ ├── MenuState.cpp │ ├── PauseState.cpp │ ├── Pickup.cpp │ ├── Player.cpp │ ├── Projectile.cpp │ ├── SceneNode.cpp │ ├── SettingsState.cpp │ ├── SpriteNode.cpp │ ├── State.cpp │ ├── StateStack.cpp │ ├── TextNode.cpp │ ├── TitleState.cpp │ ├── Utility.cpp │ └── World.cpp ├── 08_Graphics ├── Include │ └── Book │ │ ├── Aircraft.hpp │ │ ├── Animation.hpp │ │ ├── Application.hpp │ │ ├── BloomEffect.hpp │ │ ├── Button.hpp │ │ ├── Category.hpp │ │ ├── Command.hpp │ │ ├── CommandQueue.hpp │ │ ├── Component.hpp │ │ ├── Container.hpp │ │ ├── DataTables.hpp │ │ ├── EmitterNode.hpp │ │ ├── Entity.hpp │ │ ├── Foreach.hpp │ │ ├── GameOverState.hpp │ │ ├── GameState.hpp │ │ ├── Label.hpp │ │ ├── MenuState.hpp │ │ ├── Particle.hpp │ │ ├── ParticleNode.hpp │ │ ├── PauseState.hpp │ │ ├── Pickup.hpp │ │ ├── Player.hpp │ │ ├── PostEffect.hpp │ │ ├── Projectile.hpp │ │ ├── ResourceHolder.hpp │ │ ├── ResourceHolder.inl │ │ ├── ResourceIdentifiers.hpp │ │ ├── SceneNode.hpp │ │ ├── SettingsState.hpp │ │ ├── SpriteNode.hpp │ │ ├── State.hpp │ │ ├── StateIdentifiers.hpp │ │ ├── StateStack.hpp │ │ ├── TextNode.hpp │ │ ├── TitleState.hpp │ │ ├── Utility.hpp │ │ ├── Utility.inl │ │ └── World.hpp ├── Media │ ├── Sansation.ttf │ ├── Shaders │ │ ├── Add.frag │ │ ├── Brightness.frag │ │ ├── DownSample.frag │ │ ├── Fullpass.vert │ │ └── GuassianBlur.frag │ └── Textures │ │ ├── Buttons.png │ │ ├── Entities.png │ │ ├── Explosion.png │ │ ├── FinishLine.png │ │ ├── Jungle.png │ │ ├── Particle.png │ │ └── TitleScreen.png └── Source │ ├── Aircraft.cpp │ ├── Animation.cpp │ ├── Application.cpp │ ├── BloomEffect.cpp │ ├── Button.cpp │ ├── CMakeLists.txt │ ├── Command.cpp │ ├── CommandQueue.cpp │ ├── Component.cpp │ ├── Container.cpp │ ├── DataTables.cpp │ ├── EmitterNode.cpp │ ├── Entity.cpp │ ├── GameOverState.cpp │ ├── GameState.cpp │ ├── Label.cpp │ ├── Main.cpp │ ├── MenuState.cpp │ ├── ParticleNode.cpp │ ├── PauseState.cpp │ ├── Pickup.cpp │ ├── Player.cpp │ ├── PostEffect.cpp │ ├── Projectile.cpp │ ├── SceneNode.cpp │ ├── SettingsState.cpp │ ├── SpriteNode.cpp │ ├── State.cpp │ ├── StateStack.cpp │ ├── TextNode.cpp │ ├── TitleState.cpp │ ├── Utility.cpp │ └── World.cpp ├── 09_Audio ├── Include │ └── Book │ │ ├── Aircraft.hpp │ │ ├── Animation.hpp │ │ ├── Application.hpp │ │ ├── BloomEffect.hpp │ │ ├── Button.hpp │ │ ├── Category.hpp │ │ ├── Command.hpp │ │ ├── CommandQueue.hpp │ │ ├── Component.hpp │ │ ├── Container.hpp │ │ ├── DataTables.hpp │ │ ├── EmitterNode.hpp │ │ ├── Entity.hpp │ │ ├── Foreach.hpp │ │ ├── GameOverState.hpp │ │ ├── GameState.hpp │ │ ├── Label.hpp │ │ ├── MenuState.hpp │ │ ├── MusicPlayer.hpp │ │ ├── Particle.hpp │ │ ├── ParticleNode.hpp │ │ ├── PauseState.hpp │ │ ├── Pickup.hpp │ │ ├── Player.hpp │ │ ├── PostEffect.hpp │ │ ├── Projectile.hpp │ │ ├── ResourceHolder.hpp │ │ ├── ResourceHolder.inl │ │ ├── ResourceIdentifiers.hpp │ │ ├── SceneNode.hpp │ │ ├── SettingsState.hpp │ │ ├── SoundNode.hpp │ │ ├── SoundPlayer.hpp │ │ ├── SpriteNode.hpp │ │ ├── State.hpp │ │ ├── StateIdentifiers.hpp │ │ ├── StateStack.hpp │ │ ├── TextNode.hpp │ │ ├── TitleState.hpp │ │ ├── Utility.hpp │ │ ├── Utility.inl │ │ └── World.hpp ├── Media │ ├── Music │ │ ├── MenuTheme.ogg │ │ └── MissionTheme.ogg │ ├── Sansation.ttf │ ├── Shaders │ │ ├── Add.frag │ │ ├── Brightness.frag │ │ ├── DownSample.frag │ │ ├── Fullpass.vert │ │ └── GuassianBlur.frag │ ├── Sound │ │ ├── AlliedGunfire.wav │ │ ├── Button.wav │ │ ├── CollectPickup.wav │ │ ├── EnemyGunfire.wav │ │ ├── Explosion1.wav │ │ ├── Explosion2.wav │ │ └── LaunchMissile.wav │ └── Textures │ │ ├── Buttons.png │ │ ├── Entities.png │ │ ├── Explosion.png │ │ ├── FinishLine.png │ │ ├── Jungle.png │ │ ├── Particle.png │ │ └── TitleScreen.png └── Source │ ├── Aircraft.cpp │ ├── Animation.cpp │ ├── Application.cpp │ ├── BloomEffect.cpp │ ├── Button.cpp │ ├── CMakeLists.txt │ ├── Command.cpp │ ├── CommandQueue.cpp │ ├── Component.cpp │ ├── Container.cpp │ ├── DataTables.cpp │ ├── EmitterNode.cpp │ ├── Entity.cpp │ ├── GameOverState.cpp │ ├── GameState.cpp │ ├── Label.cpp │ ├── Main.cpp │ ├── MenuState.cpp │ ├── MusicPlayer.cpp │ ├── ParticleNode.cpp │ ├── PauseState.cpp │ ├── Pickup.cpp │ ├── Player.cpp │ ├── PostEffect.cpp │ ├── Projectile.cpp │ ├── SceneNode.cpp │ ├── SettingsState.cpp │ ├── SoundNode.cpp │ ├── SoundPlayer.cpp │ ├── SpriteNode.cpp │ ├── State.cpp │ ├── StateStack.cpp │ ├── TextNode.cpp │ ├── TitleState.cpp │ ├── Utility.cpp │ └── World.cpp ├── 10_Network ├── Include │ └── Book │ │ ├── Aircraft.hpp │ │ ├── Animation.hpp │ │ ├── Application.hpp │ │ ├── BloomEffect.hpp │ │ ├── Button.hpp │ │ ├── Category.hpp │ │ ├── Command.hpp │ │ ├── CommandQueue.hpp │ │ ├── Component.hpp │ │ ├── Container.hpp │ │ ├── DataTables.hpp │ │ ├── EmitterNode.hpp │ │ ├── Entity.hpp │ │ ├── Foreach.hpp │ │ ├── GameOverState.hpp │ │ ├── GameServer.hpp │ │ ├── GameState.hpp │ │ ├── KeyBinding.hpp │ │ ├── Label.hpp │ │ ├── MenuState.hpp │ │ ├── MultiplayerGameState.hpp │ │ ├── MusicPlayer.hpp │ │ ├── NetworkNode.hpp │ │ ├── NetworkProtocol.hpp │ │ ├── Particle.hpp │ │ ├── ParticleNode.hpp │ │ ├── PauseState.hpp │ │ ├── Pickup.hpp │ │ ├── Player.hpp │ │ ├── PostEffect.hpp │ │ ├── Projectile.hpp │ │ ├── ResourceHolder.hpp │ │ ├── ResourceHolder.inl │ │ ├── ResourceIdentifiers.hpp │ │ ├── SceneNode.hpp │ │ ├── SettingsState.hpp │ │ ├── SoundNode.hpp │ │ ├── SoundPlayer.hpp │ │ ├── SpriteNode.hpp │ │ ├── State.hpp │ │ ├── StateIdentifiers.hpp │ │ ├── StateStack.hpp │ │ ├── TextNode.hpp │ │ ├── TitleState.hpp │ │ ├── Utility.hpp │ │ ├── Utility.inl │ │ └── World.hpp ├── Media │ ├── Music │ │ ├── MenuTheme.ogg │ │ └── MissionTheme.ogg │ ├── Sansation.ttf │ ├── Shaders │ │ ├── Add.frag │ │ ├── Brightness.frag │ │ ├── DownSample.frag │ │ ├── Fullpass.vert │ │ └── GuassianBlur.frag │ ├── Sound │ │ ├── AlliedGunfire.wav │ │ ├── Button.wav │ │ ├── CollectPickup.wav │ │ ├── EnemyGunfire.wav │ │ ├── Explosion1.wav │ │ ├── Explosion2.wav │ │ └── LaunchMissile.wav │ └── Textures │ │ ├── Buttons.png │ │ ├── Entities.png │ │ ├── Explosion.png │ │ ├── FinishLine.png │ │ ├── Jungle.png │ │ ├── Particle.png │ │ └── TitleScreen.png └── Source │ ├── Aircraft.cpp │ ├── Animation.cpp │ ├── Application.cpp │ ├── BloomEffect.cpp │ ├── Button.cpp │ ├── CMakeLists.txt │ ├── Command.cpp │ ├── CommandQueue.cpp │ ├── Component.cpp │ ├── Container.cpp │ ├── DataTables.cpp │ ├── EmitterNode.cpp │ ├── Entity.cpp │ ├── GameOverState.cpp │ ├── GameServer.cpp │ ├── GameState.cpp │ ├── KeyBinding.cpp │ ├── Label.cpp │ ├── Main.cpp │ ├── MenuState.cpp │ ├── MultiplayerGameState.cpp │ ├── MusicPlayer.cpp │ ├── NetworkNode.cpp │ ├── ParticleNode.cpp │ ├── PauseState.cpp │ ├── Pickup.cpp │ ├── Player.cpp │ ├── PostEffect.cpp │ ├── Projectile.cpp │ ├── SceneNode.cpp │ ├── SettingsState.cpp │ ├── SoundNode.cpp │ ├── SoundPlayer.cpp │ ├── SpriteNode.cpp │ ├── State.cpp │ ├── StateStack.cpp │ ├── TextNode.cpp │ ├── TitleState.cpp │ ├── Utility.cpp │ └── World.cpp ├── CMake └── FindSFML.cmake ├── CMakeLists.txt ├── License.txt └── ReadMe.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | 9 | # Compiled Static libraries 10 | *.lai 11 | *.la 12 | *.a 13 | 14 | # Project files 15 | *.sublime-project 16 | *.sublime-workspace 17 | 18 | # Generated folders 19 | CMake/Build/* 20 | CMake/Install/* 21 | abandoned 22 | -------------------------------------------------------------------------------- /01_Intro/Include/Book/Game.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_GAME_HPP 2 | #define BOOK_GAME_HPP 3 | 4 | #include 5 | 6 | 7 | class Game : private sf::NonCopyable 8 | { 9 | public: 10 | Game(); 11 | void run(); 12 | 13 | 14 | private: 15 | void processEvents(); 16 | void update(sf::Time elapsedTime); 17 | void render(); 18 | 19 | void updateStatistics(sf::Time elapsedTime); 20 | void handlePlayerInput(sf::Keyboard::Key key, bool isPressed); 21 | 22 | 23 | private: 24 | static const float PlayerSpeed; 25 | static const sf::Time TimePerFrame; 26 | 27 | sf::RenderWindow mWindow; 28 | sf::Texture mTexture; 29 | sf::Sprite mPlayer; 30 | sf::Font mFont; 31 | sf::Text mStatisticsText; 32 | sf::Time mStatisticsUpdateTime; 33 | 34 | std::size_t mStatisticsNumFrames; 35 | bool mIsMovingUp; 36 | bool mIsMovingDown; 37 | bool mIsMovingRight; 38 | bool mIsMovingLeft; 39 | }; 40 | 41 | #endif // BOOK_GAME_HPP 42 | -------------------------------------------------------------------------------- /01_Intro/Include/Book/StringHelpers.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_STRINGHELPERS_HPP 2 | #define BOOK_STRINGHELPERS_HPP 3 | 4 | #include 5 | 6 | // Since std::to_string doesn't work on MinGW we have to implement 7 | // our own to support all platforms. 8 | template 9 | std::string toString(const T& value); 10 | 11 | #include 12 | #endif // BOOK_STRINGHELPERS_HPP 13 | -------------------------------------------------------------------------------- /01_Intro/Include/Book/StringHelpers.inl: -------------------------------------------------------------------------------- 1 | 2 | template 3 | std::string toString(const T& value) 4 | { 5 | std::stringstream stream; 6 | stream << value; 7 | return stream.str(); 8 | } 9 | -------------------------------------------------------------------------------- /01_Intro/Media/Sansation.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/01_Intro/Media/Sansation.ttf -------------------------------------------------------------------------------- /01_Intro/Media/Textures/Eagle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/01_Intro/Media/Textures/Eagle.png -------------------------------------------------------------------------------- /01_Intro/Source/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | set (SRC 3 | Game.cpp 4 | ) 5 | 6 | build_chapter(01_Intro SOURCES ${SRC}) -------------------------------------------------------------------------------- /01_Intro/Source/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | Game game; 6 | game.run(); 7 | } 8 | -------------------------------------------------------------------------------- /02_Resources/Include/Book/ResourceHolder.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_RESOURCEHOLDER_HPP 2 | #define BOOK_RESOURCEHOLDER_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | template 12 | class ResourceHolder 13 | { 14 | public: 15 | void load(Identifier id, const std::string& filename); 16 | 17 | template 18 | void load(Identifier id, const std::string& filename, const Parameter& secondParam); 19 | 20 | Resource& get(Identifier id); 21 | const Resource& get(Identifier id) const; 22 | 23 | 24 | private: 25 | void insertResource(Identifier id, std::unique_ptr resource); 26 | 27 | 28 | private: 29 | std::map> mResourceMap; 30 | }; 31 | 32 | #include "ResourceHolder.inl" 33 | #endif // BOOK_RESOURCEHOLDER_HPP 34 | -------------------------------------------------------------------------------- /02_Resources/Media/Textures/Desert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/02_Resources/Media/Textures/Desert.png -------------------------------------------------------------------------------- /02_Resources/Media/Textures/Eagle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/02_Resources/Media/Textures/Eagle.png -------------------------------------------------------------------------------- /02_Resources/Source/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | set (SRC "") 3 | 4 | build_chapter(02_Resources SOURCES ${SRC}) -------------------------------------------------------------------------------- /03_World/Include/Book/Aircraft.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_AIRCRAFT_HPP 2 | #define BOOK_AIRCRAFT_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | 10 | class Aircraft : public Entity 11 | { 12 | public: 13 | enum Type 14 | { 15 | Eagle, 16 | Raptor, 17 | }; 18 | 19 | 20 | public: 21 | Aircraft(Type type, const TextureHolder& textures); 22 | 23 | 24 | private: 25 | virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; 26 | 27 | 28 | private: 29 | Type mType; 30 | sf::Sprite mSprite; 31 | }; 32 | 33 | #endif // BOOK_AIRCRAFT_HPP 34 | -------------------------------------------------------------------------------- /03_World/Include/Book/Entity.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_ENTITY_HPP 2 | #define BOOK_ENTITY_HPP 3 | 4 | #include 5 | 6 | 7 | class Entity : public SceneNode 8 | { 9 | public: 10 | void setVelocity(sf::Vector2f velocity); 11 | void setVelocity(float vx, float vy); 12 | sf::Vector2f getVelocity() const; 13 | 14 | 15 | private: 16 | virtual void updateCurrent(sf::Time dt); 17 | 18 | 19 | private: 20 | sf::Vector2f mVelocity; 21 | }; 22 | 23 | #endif // BOOK_ENTITY_HPP 24 | -------------------------------------------------------------------------------- /03_World/Include/Book/Game.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_GAME_HPP 2 | #define BOOK_GAME_HPP 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | class Game : private sf::NonCopyable 14 | { 15 | public: 16 | Game(); 17 | void run(); 18 | 19 | 20 | private: 21 | void processEvents(); 22 | void update(sf::Time elapsedTime); 23 | void render(); 24 | 25 | void updateStatistics(sf::Time elapsedTime); 26 | void handlePlayerInput(sf::Keyboard::Key key, bool isPressed); 27 | 28 | 29 | private: 30 | static const sf::Time TimePerFrame; 31 | 32 | sf::RenderWindow mWindow; 33 | World mWorld; 34 | 35 | sf::Font mFont; 36 | sf::Text mStatisticsText; 37 | sf::Time mStatisticsUpdateTime; 38 | std::size_t mStatisticsNumFrames; 39 | }; 40 | 41 | #endif // BOOK_GAME_HPP 42 | -------------------------------------------------------------------------------- /03_World/Include/Book/ResourceHolder.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_RESOURCEHOLDER_HPP 2 | #define BOOK_RESOURCEHOLDER_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | template 12 | class ResourceHolder 13 | { 14 | public: 15 | void load(Identifier id, const std::string& filename); 16 | 17 | template 18 | void load(Identifier id, const std::string& filename, const Parameter& secondParam); 19 | 20 | Resource& get(Identifier id); 21 | const Resource& get(Identifier id) const; 22 | 23 | 24 | private: 25 | void insertResource(Identifier id, std::unique_ptr resource); 26 | 27 | 28 | private: 29 | std::map> mResourceMap; 30 | }; 31 | 32 | #include "ResourceHolder.inl" 33 | #endif // BOOK_RESOURCEHOLDER_HPP 34 | -------------------------------------------------------------------------------- /03_World/Include/Book/ResourceIdentifiers.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_RESOURCEIDENTIFIERS_HPP 2 | #define BOOK_RESOURCEIDENTIFIERS_HPP 3 | 4 | 5 | // Forward declaration of SFML classes 6 | namespace sf 7 | { 8 | class Texture; 9 | } 10 | 11 | namespace Textures 12 | { 13 | enum ID 14 | { 15 | Eagle, 16 | Raptor, 17 | Desert, 18 | }; 19 | } 20 | 21 | // Forward declaration and a few type definitions 22 | template 23 | class ResourceHolder; 24 | 25 | typedef ResourceHolder TextureHolder; 26 | 27 | #endif // BOOK_RESOURCEIDENTIFIERS_HPP 28 | -------------------------------------------------------------------------------- /03_World/Include/Book/SpriteNode.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_SPRITENODE_HPP 2 | #define BOOK_SPRITENODE_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | 9 | class SpriteNode : public SceneNode 10 | { 11 | public: 12 | explicit SpriteNode(const sf::Texture& texture); 13 | SpriteNode(const sf::Texture& texture, const sf::IntRect& textureRect); 14 | 15 | 16 | private: 17 | virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; 18 | 19 | 20 | private: 21 | sf::Sprite mSprite; 22 | }; 23 | 24 | #endif // BOOK_SPRITENODE_HPP 25 | -------------------------------------------------------------------------------- /03_World/Include/Book/StringHelpers.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_STRINGHELPERS_HPP 2 | #define BOOK_STRINGHELPERS_HPP 3 | 4 | #include 5 | 6 | // Since std::to_string doesn't work on MinGW we have to implement 7 | // our own to support all platforms. 8 | template 9 | std::string toString(const T& value); 10 | 11 | #include 12 | #endif // BOOK_STRINGHELPERS_HPP 13 | -------------------------------------------------------------------------------- /03_World/Include/Book/StringHelpers.inl: -------------------------------------------------------------------------------- 1 | 2 | template 3 | std::string toString(const T& value) 4 | { 5 | std::stringstream stream; 6 | stream << value; 7 | return stream.str(); 8 | } 9 | -------------------------------------------------------------------------------- /03_World/Media/Sansation.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/03_World/Media/Sansation.ttf -------------------------------------------------------------------------------- /03_World/Media/Textures/Desert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/03_World/Media/Textures/Desert.png -------------------------------------------------------------------------------- /03_World/Media/Textures/Eagle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/03_World/Media/Textures/Eagle.png -------------------------------------------------------------------------------- /03_World/Media/Textures/Raptor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/03_World/Media/Textures/Raptor.png -------------------------------------------------------------------------------- /03_World/Source/Aircraft.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | 8 | Textures::ID toTextureID(Aircraft::Type type) 9 | { 10 | switch (type) 11 | { 12 | case Aircraft::Eagle: 13 | return Textures::Eagle; 14 | 15 | case Aircraft::Raptor: 16 | return Textures::Raptor; 17 | } 18 | return Textures::Eagle; 19 | } 20 | 21 | Aircraft::Aircraft(Type type, const TextureHolder& textures) 22 | : mType(type) 23 | , mSprite(textures.get(toTextureID(type))) 24 | { 25 | sf::FloatRect bounds = mSprite.getLocalBounds(); 26 | mSprite.setOrigin(bounds.width / 2.f, bounds.height / 2.f); 27 | } 28 | 29 | void Aircraft::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const 30 | { 31 | target.draw(mSprite, states); 32 | } 33 | -------------------------------------------------------------------------------- /03_World/Source/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | set (SRC 3 | Aircraft.cpp 4 | Entity.cpp 5 | Game.cpp 6 | SceneNode.cpp 7 | SpriteNode.cpp 8 | World.cpp) 9 | 10 | build_chapter(03_World SOURCES ${SRC}) -------------------------------------------------------------------------------- /03_World/Source/Entity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | void Entity::setVelocity(sf::Vector2f velocity) 5 | { 6 | mVelocity = velocity; 7 | } 8 | 9 | void Entity::setVelocity(float vx, float vy) 10 | { 11 | mVelocity.x = vx; 12 | mVelocity.y = vy; 13 | } 14 | 15 | sf::Vector2f Entity::getVelocity() const 16 | { 17 | return mVelocity; 18 | } 19 | 20 | void Entity::updateCurrent(sf::Time dt) 21 | { 22 | move(mVelocity * dt.asSeconds()); 23 | } -------------------------------------------------------------------------------- /03_World/Source/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | 7 | int main() 8 | { 9 | try 10 | { 11 | Game game; 12 | game.run(); 13 | } 14 | catch (std::exception& e) 15 | { 16 | std::cout << "\nEXCEPTION: " << e.what() << std::endl; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /03_World/Source/SpriteNode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | 6 | SpriteNode::SpriteNode(const sf::Texture& texture) 7 | : mSprite(texture) 8 | { 9 | } 10 | 11 | SpriteNode::SpriteNode(const sf::Texture& texture, const sf::IntRect& textureRect) 12 | : mSprite(texture, textureRect) 13 | { 14 | } 15 | 16 | void SpriteNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const 17 | { 18 | target.draw(mSprite, states); 19 | } -------------------------------------------------------------------------------- /04_Input/Include/Book/Aircraft.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_AIRCRAFT_HPP 2 | #define BOOK_AIRCRAFT_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | 10 | class Aircraft : public Entity 11 | { 12 | public: 13 | enum Type 14 | { 15 | Eagle, 16 | Raptor, 17 | }; 18 | 19 | 20 | public: 21 | Aircraft(Type type, const TextureHolder& textures); 22 | virtual unsigned int getCategory() const; 23 | 24 | 25 | private: 26 | virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; 27 | 28 | 29 | private: 30 | Type mType; 31 | sf::Sprite mSprite; 32 | }; 33 | 34 | #endif // BOOK_AIRCRAFT_HPP 35 | -------------------------------------------------------------------------------- /04_Input/Include/Book/Category.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_CATEGORY_HPP 2 | #define BOOK_CATEGORY_HPP 3 | 4 | 5 | // Entity/scene node category, used to dispatch commands 6 | namespace Category 7 | { 8 | enum Type 9 | { 10 | None = 0, 11 | Scene = 1 << 0, 12 | PlayerAircraft = 1 << 1, 13 | AlliedAircraft = 1 << 2, 14 | EnemyAircraft = 1 << 3, 15 | }; 16 | } 17 | 18 | #endif // BOOK_CATEGORY_HPP 19 | -------------------------------------------------------------------------------- /04_Input/Include/Book/Command.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_COMMAND_HPP 2 | #define BOOK_COMMAND_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | 12 | class SceneNode; 13 | 14 | struct Command 15 | { 16 | Command(); 17 | 18 | std::function action; 19 | unsigned int category; 20 | }; 21 | 22 | template 23 | std::function derivedAction(Function fn) 24 | { 25 | return [=] (SceneNode& node, sf::Time dt) 26 | { 27 | // Check if cast is safe 28 | assert(dynamic_cast(&node) != nullptr); 29 | 30 | // Downcast node and invoke function on it 31 | fn(static_cast(node), dt); 32 | }; 33 | } 34 | 35 | #endif // BOOK_COMMAND_HPP 36 | -------------------------------------------------------------------------------- /04_Input/Include/Book/CommandQueue.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_COMMANDQUEUE_HPP 2 | #define BOOK_COMMANDQUEUE_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | 9 | class CommandQueue 10 | { 11 | public: 12 | void push(const Command& command); 13 | Command pop(); 14 | bool isEmpty() const; 15 | 16 | 17 | private: 18 | std::queue mQueue; 19 | }; 20 | 21 | #endif // BOOK_COMMANDQUEUE_HPP 22 | -------------------------------------------------------------------------------- /04_Input/Include/Book/Entity.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_ENTITY_HPP 2 | #define BOOK_ENTITY_HPP 3 | 4 | #include 5 | 6 | 7 | class Entity : public SceneNode 8 | { 9 | public: 10 | void setVelocity(sf::Vector2f velocity); 11 | void setVelocity(float vx, float vy); 12 | void accelerate(sf::Vector2f velocity); 13 | void accelerate(float vx, float vy); 14 | sf::Vector2f getVelocity() const; 15 | 16 | 17 | private: 18 | virtual void updateCurrent(sf::Time dt); 19 | 20 | 21 | private: 22 | sf::Vector2f mVelocity; 23 | }; 24 | 25 | #endif // BOOK_ENTITY_HPP 26 | -------------------------------------------------------------------------------- /04_Input/Include/Book/Game.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_GAME_HPP 2 | #define BOOK_GAME_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | class Game : private sf::NonCopyable 14 | { 15 | public: 16 | Game(); 17 | void run(); 18 | 19 | 20 | private: 21 | void processInput(); 22 | void update(sf::Time elapsedTime); 23 | void render(); 24 | void updateStatistics(sf::Time elapsedTime); 25 | 26 | 27 | private: 28 | static const sf::Time TimePerFrame; 29 | 30 | sf::RenderWindow mWindow; 31 | World mWorld; 32 | Player mPlayer; 33 | 34 | sf::Font mFont; 35 | sf::Text mStatisticsText; 36 | sf::Time mStatisticsUpdateTime; 37 | std::size_t mStatisticsNumFrames; 38 | }; 39 | 40 | #endif // BOOK_GAME_HPP 41 | -------------------------------------------------------------------------------- /04_Input/Include/Book/Player.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_PLAYER_HPP 2 | #define BOOK_PLAYER_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | 10 | 11 | class CommandQueue; 12 | 13 | class Player 14 | { 15 | public: 16 | enum Action 17 | { 18 | MoveLeft, 19 | MoveRight, 20 | MoveUp, 21 | MoveDown, 22 | ActionCount 23 | }; 24 | 25 | 26 | public: 27 | Player(); 28 | 29 | void handleEvent(const sf::Event& event, CommandQueue& commands); 30 | void handleRealtimeInput(CommandQueue& commands); 31 | 32 | void assignKey(Action action, sf::Keyboard::Key key); 33 | sf::Keyboard::Key getAssignedKey(Action action) const; 34 | 35 | 36 | private: 37 | void initializeActions(); 38 | static bool isRealtimeAction(Action action); 39 | 40 | 41 | private: 42 | std::map mKeyBinding; 43 | std::map mActionBinding; 44 | }; 45 | 46 | #endif // BOOK_PLAYER_HPP 47 | -------------------------------------------------------------------------------- /04_Input/Include/Book/ResourceHolder.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_RESOURCEHOLDER_HPP 2 | #define BOOK_RESOURCEHOLDER_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | template 12 | class ResourceHolder 13 | { 14 | public: 15 | void load(Identifier id, const std::string& filename); 16 | 17 | template 18 | void load(Identifier id, const std::string& filename, const Parameter& secondParam); 19 | 20 | Resource& get(Identifier id); 21 | const Resource& get(Identifier id) const; 22 | 23 | 24 | private: 25 | void insertResource(Identifier id, std::unique_ptr resource); 26 | 27 | 28 | private: 29 | std::map> mResourceMap; 30 | }; 31 | 32 | #include "ResourceHolder.inl" 33 | #endif // BOOK_RESOURCEHOLDER_HPP 34 | -------------------------------------------------------------------------------- /04_Input/Include/Book/ResourceIdentifiers.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_RESOURCEIDENTIFIERS_HPP 2 | #define BOOK_RESOURCEIDENTIFIERS_HPP 3 | 4 | 5 | // Forward declaration of SFML classes 6 | namespace sf 7 | { 8 | class Texture; 9 | } 10 | 11 | namespace Textures 12 | { 13 | enum ID 14 | { 15 | Eagle, 16 | Raptor, 17 | Desert, 18 | }; 19 | } 20 | 21 | // Forward declaration and a few type definitions 22 | template 23 | class ResourceHolder; 24 | 25 | typedef ResourceHolder TextureHolder; 26 | 27 | #endif // BOOK_RESOURCEIDENTIFIERS_HPP 28 | -------------------------------------------------------------------------------- /04_Input/Include/Book/SpriteNode.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_SPRITENODE_HPP 2 | #define BOOK_SPRITENODE_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | 9 | class SpriteNode : public SceneNode 10 | { 11 | public: 12 | explicit SpriteNode(const sf::Texture& texture); 13 | SpriteNode(const sf::Texture& texture, const sf::IntRect& textureRect); 14 | 15 | 16 | private: 17 | virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; 18 | 19 | 20 | private: 21 | sf::Sprite mSprite; 22 | }; 23 | 24 | #endif // BOOK_SPRITENODE_HPP 25 | -------------------------------------------------------------------------------- /04_Input/Include/Book/StringHelpers.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_STRINGHELPERS_HPP 2 | #define BOOK_STRINGHELPERS_HPP 3 | 4 | #include 5 | 6 | // Since std::to_string doesn't work on MinGW we have to implement 7 | // our own to support all platforms. 8 | template 9 | std::string toString(const T& value); 10 | 11 | #include 12 | #endif // BOOK_STRINGHELPERS_HPP 13 | -------------------------------------------------------------------------------- /04_Input/Include/Book/StringHelpers.inl: -------------------------------------------------------------------------------- 1 | 2 | template 3 | std::string toString(const T& value) 4 | { 5 | std::stringstream stream; 6 | stream << value; 7 | return stream.str(); 8 | } 9 | -------------------------------------------------------------------------------- /04_Input/Media/Sansation.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/04_Input/Media/Sansation.ttf -------------------------------------------------------------------------------- /04_Input/Media/Textures/Desert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/04_Input/Media/Textures/Desert.png -------------------------------------------------------------------------------- /04_Input/Media/Textures/Eagle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/04_Input/Media/Textures/Eagle.png -------------------------------------------------------------------------------- /04_Input/Media/Textures/Raptor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/04_Input/Media/Textures/Raptor.png -------------------------------------------------------------------------------- /04_Input/Source/Aircraft.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | 8 | Textures::ID toTextureID(Aircraft::Type type) 9 | { 10 | switch (type) 11 | { 12 | case Aircraft::Eagle: 13 | return Textures::Eagle; 14 | 15 | case Aircraft::Raptor: 16 | return Textures::Raptor; 17 | } 18 | return Textures::Eagle; 19 | } 20 | 21 | Aircraft::Aircraft(Type type, const TextureHolder& textures) 22 | : mType(type) 23 | , mSprite(textures.get(toTextureID(type))) 24 | { 25 | sf::FloatRect bounds = mSprite.getLocalBounds(); 26 | mSprite.setOrigin(bounds.width / 2.f, bounds.height / 2.f); 27 | } 28 | 29 | void Aircraft::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const 30 | { 31 | target.draw(mSprite, states); 32 | } 33 | 34 | unsigned int Aircraft::getCategory() const 35 | { 36 | switch (mType) 37 | { 38 | case Eagle: 39 | return Category::PlayerAircraft; 40 | 41 | default: 42 | return Category::EnemyAircraft; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /04_Input/Source/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | set (SRC 3 | Aircraft.cpp 4 | Command.cpp 5 | CommandQueue.cpp 6 | Entity.cpp 7 | Game.cpp 8 | Player.cpp 9 | SceneNode.cpp 10 | SpriteNode.cpp 11 | World.cpp) 12 | 13 | build_chapter(04_Input SOURCES ${SRC}) -------------------------------------------------------------------------------- /04_Input/Source/Command.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | Command::Command() 5 | : action() 6 | , category(Category::None) 7 | { 8 | } 9 | -------------------------------------------------------------------------------- /04_Input/Source/CommandQueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void CommandQueue::push(const Command& command) 6 | { 7 | mQueue.push(command); 8 | } 9 | 10 | Command CommandQueue::pop() 11 | { 12 | Command command = mQueue.front(); 13 | mQueue.pop(); 14 | return command; 15 | } 16 | 17 | bool CommandQueue::isEmpty() const 18 | { 19 | return mQueue.empty(); 20 | } 21 | -------------------------------------------------------------------------------- /04_Input/Source/Entity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | void Entity::setVelocity(sf::Vector2f velocity) 5 | { 6 | mVelocity = velocity; 7 | } 8 | 9 | void Entity::setVelocity(float vx, float vy) 10 | { 11 | mVelocity.x = vx; 12 | mVelocity.y = vy; 13 | } 14 | 15 | sf::Vector2f Entity::getVelocity() const 16 | { 17 | return mVelocity; 18 | } 19 | 20 | void Entity::accelerate(sf::Vector2f velocity) 21 | { 22 | mVelocity += velocity; 23 | } 24 | 25 | void Entity::accelerate(float vx, float vy) 26 | { 27 | mVelocity.x += vx; 28 | mVelocity.y += vy; 29 | } 30 | 31 | void Entity::updateCurrent(sf::Time dt) 32 | { 33 | move(mVelocity * dt.asSeconds()); 34 | } -------------------------------------------------------------------------------- /04_Input/Source/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | 7 | int main() 8 | { 9 | try 10 | { 11 | Game game; 12 | game.run(); 13 | } 14 | catch (std::exception& e) 15 | { 16 | std::cout << "\nEXCEPTION: " << e.what() << std::endl; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /04_Input/Source/SpriteNode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | 6 | SpriteNode::SpriteNode(const sf::Texture& texture) 7 | : mSprite(texture) 8 | { 9 | } 10 | 11 | SpriteNode::SpriteNode(const sf::Texture& texture, const sf::IntRect& textureRect) 12 | : mSprite(texture, textureRect) 13 | { 14 | } 15 | 16 | void SpriteNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const 17 | { 18 | target.draw(mSprite, states); 19 | } -------------------------------------------------------------------------------- /05_States/Include/Book/Aircraft.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_AIRCRAFT_HPP 2 | #define BOOK_AIRCRAFT_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | 10 | class Aircraft : public Entity 11 | { 12 | public: 13 | enum Type 14 | { 15 | Eagle, 16 | Raptor, 17 | }; 18 | 19 | 20 | public: 21 | Aircraft(Type type, const TextureHolder& textures); 22 | virtual unsigned int getCategory() const; 23 | 24 | 25 | private: 26 | virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; 27 | 28 | 29 | private: 30 | Type mType; 31 | sf::Sprite mSprite; 32 | }; 33 | 34 | #endif // BOOK_AIRCRAFT_HPP 35 | -------------------------------------------------------------------------------- /05_States/Include/Book/Application.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_APPLICATION_HPP 2 | #define BOOK_APPLICATION_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | class Application 15 | { 16 | public: 17 | Application(); 18 | void run(); 19 | 20 | 21 | private: 22 | void processInput(); 23 | void update(sf::Time dt); 24 | void render(); 25 | 26 | void updateStatistics(sf::Time dt); 27 | void registerStates(); 28 | 29 | 30 | private: 31 | static const sf::Time TimePerFrame; 32 | 33 | sf::RenderWindow mWindow; 34 | TextureHolder mTextures; 35 | FontHolder mFonts; 36 | Player mPlayer; 37 | 38 | StateStack mStateStack; 39 | 40 | sf::Text mStatisticsText; 41 | sf::Time mStatisticsUpdateTime; 42 | std::size_t mStatisticsNumFrames; 43 | }; 44 | 45 | #endif // BOOK_APPLICATION_HPP 46 | -------------------------------------------------------------------------------- /05_States/Include/Book/Category.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_CATEGORY_HPP 2 | #define BOOK_CATEGORY_HPP 3 | 4 | 5 | // Entity/scene node category, used to dispatch commands 6 | namespace Category 7 | { 8 | enum Type 9 | { 10 | None = 0, 11 | Scene = 1 << 0, 12 | PlayerAircraft = 1 << 1, 13 | AlliedAircraft = 1 << 2, 14 | EnemyAircraft = 1 << 3, 15 | }; 16 | } 17 | 18 | #endif // BOOK_CATEGORY_HPP 19 | -------------------------------------------------------------------------------- /05_States/Include/Book/Command.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_COMMAND_HPP 2 | #define BOOK_COMMAND_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | 12 | class SceneNode; 13 | 14 | struct Command 15 | { 16 | Command(); 17 | 18 | std::function action; 19 | unsigned int category; 20 | }; 21 | 22 | template 23 | std::function derivedAction(Function fn) 24 | { 25 | return [=] (SceneNode& node, sf::Time dt) 26 | { 27 | // Check if cast is safe 28 | assert(dynamic_cast(&node) != nullptr); 29 | 30 | // Downcast node and invoke function on it 31 | fn(static_cast(node), dt); 32 | }; 33 | } 34 | 35 | #endif // BOOK_COMMAND_HPP 36 | -------------------------------------------------------------------------------- /05_States/Include/Book/CommandQueue.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_COMMANDQUEUE_HPP 2 | #define BOOK_COMMANDQUEUE_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | 9 | class CommandQueue 10 | { 11 | public: 12 | void push(const Command& command); 13 | Command pop(); 14 | bool isEmpty() const; 15 | 16 | 17 | private: 18 | std::queue mQueue; 19 | }; 20 | 21 | #endif // BOOK_COMMANDQUEUE_HPP 22 | -------------------------------------------------------------------------------- /05_States/Include/Book/Entity.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_ENTITY_HPP 2 | #define BOOK_ENTITY_HPP 3 | 4 | #include 5 | 6 | 7 | class Entity : public SceneNode 8 | { 9 | public: 10 | void setVelocity(sf::Vector2f velocity); 11 | void setVelocity(float vx, float vy); 12 | void accelerate(sf::Vector2f velocity); 13 | void accelerate(float vx, float vy); 14 | sf::Vector2f getVelocity() const; 15 | 16 | 17 | private: 18 | virtual void updateCurrent(sf::Time dt); 19 | 20 | 21 | private: 22 | sf::Vector2f mVelocity; 23 | }; 24 | 25 | #endif // BOOK_ENTITY_HPP 26 | -------------------------------------------------------------------------------- /05_States/Include/Book/GameState.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_GAMESTATE_HPP 2 | #define BOOK_GAMESTATE_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | 12 | class GameState : public State 13 | { 14 | public: 15 | GameState(StateStack& stack, Context context); 16 | 17 | virtual void draw(); 18 | virtual bool update(sf::Time dt); 19 | virtual bool handleEvent(const sf::Event& event); 20 | 21 | 22 | private: 23 | World mWorld; 24 | Player& mPlayer; 25 | }; 26 | 27 | #endif // BOOK_GAMESTATE_HPP -------------------------------------------------------------------------------- /05_States/Include/Book/LoadingState.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_LOADINGSTATE_HPP 2 | #define BOOK_LOADINGSTATE_HPP 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | 12 | class LoadingState : public State 13 | { 14 | public: 15 | LoadingState(StateStack& stack, Context context); 16 | 17 | virtual void draw(); 18 | virtual bool update(sf::Time dt); 19 | virtual bool handleEvent(const sf::Event& event); 20 | 21 | void setCompletion(float percent); 22 | 23 | private: 24 | sf::Text mLoadingText; 25 | sf::RectangleShape mProgressBarBackground; 26 | sf::RectangleShape mProgressBar; 27 | 28 | ParallelTask mLoadingTask; 29 | }; 30 | 31 | #endif // BOOK_LOADINGSTATE_HPP 32 | -------------------------------------------------------------------------------- /05_States/Include/Book/MenuState.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_MENUSTATE_HPP 2 | #define BOOK_MENUSTATE_HPP 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | 10 | class MenuState : public State 11 | { 12 | public: 13 | MenuState(StateStack& stack, Context context); 14 | 15 | virtual void draw(); 16 | virtual bool update(sf::Time dt); 17 | virtual bool handleEvent(const sf::Event& event); 18 | 19 | void updateOptionText(); 20 | 21 | 22 | private: 23 | enum OptionNames 24 | { 25 | Play, 26 | Exit, 27 | }; 28 | 29 | 30 | private: 31 | sf::Sprite mBackgroundSprite; 32 | 33 | std::vector mOptions; 34 | std::size_t mOptionIndex; 35 | }; 36 | 37 | #endif // BOOK_MENUSTATE_HPP 38 | -------------------------------------------------------------------------------- /05_States/Include/Book/ParallelTask.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_PARALLELTASK_HPP 2 | #define BOOK_PARALLELTASK_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | class ParallelTask 11 | { 12 | public: 13 | ParallelTask(); 14 | void execute(); 15 | bool isFinished(); 16 | float getCompletion(); 17 | 18 | private: 19 | void runTask(); 20 | 21 | private: 22 | sf::Thread mThread; 23 | bool mFinished; 24 | sf::Clock mElapsedTime; 25 | sf::Mutex mMutex; 26 | }; 27 | 28 | #endif // BOOK_PARALLELTASK_HPP 29 | -------------------------------------------------------------------------------- /05_States/Include/Book/PauseState.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_PAUSESTATE_HPP 2 | #define BOOK_PAUSESTATE_HPP 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | 10 | class PauseState : public State 11 | { 12 | public: 13 | PauseState(StateStack& stack, Context context); 14 | 15 | virtual void draw(); 16 | virtual bool update(sf::Time dt); 17 | virtual bool handleEvent(const sf::Event& event); 18 | 19 | 20 | private: 21 | sf::Sprite mBackgroundSprite; 22 | sf::Text mPausedText; 23 | sf::Text mInstructionText; 24 | }; 25 | 26 | #endif // BOOK_PAUSESTATE_HPP -------------------------------------------------------------------------------- /05_States/Include/Book/Player.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_PLAYER_HPP 2 | #define BOOK_PLAYER_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | 10 | 11 | class CommandQueue; 12 | 13 | class Player 14 | { 15 | public: 16 | enum Action 17 | { 18 | MoveLeft, 19 | MoveRight, 20 | MoveUp, 21 | MoveDown, 22 | ActionCount 23 | }; 24 | 25 | 26 | public: 27 | Player(); 28 | 29 | void handleEvent(const sf::Event& event, CommandQueue& commands); 30 | void handleRealtimeInput(CommandQueue& commands); 31 | 32 | void assignKey(Action action, sf::Keyboard::Key key); 33 | sf::Keyboard::Key getAssignedKey(Action action) const; 34 | 35 | 36 | private: 37 | void initializeActions(); 38 | static bool isRealtimeAction(Action action); 39 | 40 | 41 | private: 42 | std::map mKeyBinding; 43 | std::map mActionBinding; 44 | }; 45 | 46 | #endif // BOOK_PLAYER_HPP 47 | -------------------------------------------------------------------------------- /05_States/Include/Book/ResourceHolder.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_RESOURCEHOLDER_HPP 2 | #define BOOK_RESOURCEHOLDER_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | template 12 | class ResourceHolder 13 | { 14 | public: 15 | void load(Identifier id, const std::string& filename); 16 | 17 | template 18 | void load(Identifier id, const std::string& filename, const Parameter& secondParam); 19 | 20 | Resource& get(Identifier id); 21 | const Resource& get(Identifier id) const; 22 | 23 | 24 | private: 25 | void insertResource(Identifier id, std::unique_ptr resource); 26 | 27 | 28 | private: 29 | std::map> mResourceMap; 30 | }; 31 | 32 | #include "ResourceHolder.inl" 33 | #endif // BOOK_RESOURCEHOLDER_HPP 34 | -------------------------------------------------------------------------------- /05_States/Include/Book/ResourceIdentifiers.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_RESOURCEIDENTIFIERS_HPP 2 | #define BOOK_RESOURCEIDENTIFIERS_HPP 3 | 4 | 5 | // Forward declaration of SFML classes 6 | namespace sf 7 | { 8 | class Texture; 9 | class Font; 10 | } 11 | 12 | namespace Textures 13 | { 14 | enum ID 15 | { 16 | Eagle, 17 | Raptor, 18 | Desert, 19 | TitleScreen, 20 | }; 21 | } 22 | 23 | namespace Fonts 24 | { 25 | enum ID 26 | { 27 | Main, 28 | }; 29 | } 30 | 31 | // Forward declaration and a few type definitions 32 | template 33 | class ResourceHolder; 34 | 35 | typedef ResourceHolder TextureHolder; 36 | typedef ResourceHolder FontHolder; 37 | 38 | #endif // BOOK_RESOURCEIDENTIFIERS_HPP 39 | -------------------------------------------------------------------------------- /05_States/Include/Book/SpriteNode.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_SPRITENODE_HPP 2 | #define BOOK_SPRITENODE_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | 9 | class SpriteNode : public SceneNode 10 | { 11 | public: 12 | explicit SpriteNode(const sf::Texture& texture); 13 | SpriteNode(const sf::Texture& texture, const sf::IntRect& textureRect); 14 | 15 | 16 | private: 17 | virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; 18 | 19 | 20 | private: 21 | sf::Sprite mSprite; 22 | }; 23 | 24 | #endif // BOOK_SPRITENODE_HPP 25 | -------------------------------------------------------------------------------- /05_States/Include/Book/StateIdentifiers.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_STATEIDENTIFIERS_HPP 2 | #define BOOK_STATEIDENTIFIERS_HPP 3 | 4 | 5 | namespace States 6 | { 7 | enum ID 8 | { 9 | None, 10 | Title, 11 | Menu, 12 | Game, 13 | Loading, 14 | Pause 15 | }; 16 | } 17 | 18 | #endif // BOOK_STATEIDENTIFIERS_HPP 19 | -------------------------------------------------------------------------------- /05_States/Include/Book/TitleState.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_TITLESTATE_HPP 2 | #define BOOK_TITLESTATE_HPP 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | 10 | class TitleState : public State 11 | { 12 | public: 13 | TitleState(StateStack& stack, Context context); 14 | 15 | virtual void draw(); 16 | virtual bool update(sf::Time dt); 17 | virtual bool handleEvent(const sf::Event& event); 18 | 19 | 20 | private: 21 | sf::Sprite mBackgroundSprite; 22 | sf::Text mText; 23 | 24 | bool mShowText; 25 | sf::Time mTextEffectTime; 26 | }; 27 | 28 | #endif // BOOK_TITLESTATE_HPP 29 | -------------------------------------------------------------------------------- /05_States/Include/Book/Utility.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_UTILITY_HPP 2 | #define BOOK_UTILITY_HPP 3 | 4 | #include 5 | 6 | 7 | namespace sf 8 | { 9 | class Sprite; 10 | class Text; 11 | } 12 | 13 | // Since std::to_string doesn't work on MinGW we have to implement 14 | // our own to support all platforms. 15 | template 16 | std::string toString(const T& value); 17 | 18 | void centerOrigin(sf::Sprite& sprite); 19 | void centerOrigin(sf::Text& text); 20 | 21 | #include 22 | #endif // BOOK_UTILITY_HPP 23 | -------------------------------------------------------------------------------- /05_States/Include/Book/Utility.inl: -------------------------------------------------------------------------------- 1 | 2 | template 3 | std::string toString(const T& value) 4 | { 5 | std::stringstream stream; 6 | stream << value; 7 | return stream.str(); 8 | } 9 | -------------------------------------------------------------------------------- /05_States/Media/Sansation.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/05_States/Media/Sansation.ttf -------------------------------------------------------------------------------- /05_States/Media/Textures/Desert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/05_States/Media/Textures/Desert.png -------------------------------------------------------------------------------- /05_States/Media/Textures/Eagle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/05_States/Media/Textures/Eagle.png -------------------------------------------------------------------------------- /05_States/Media/Textures/Raptor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/05_States/Media/Textures/Raptor.png -------------------------------------------------------------------------------- /05_States/Media/Textures/TitleScreen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SFML/SFML-Game-Development-Book/0eee1af4885cdcff87b15c534f3f118ae355b689/05_States/Media/Textures/TitleScreen.png -------------------------------------------------------------------------------- /05_States/Source/Aircraft.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | 8 | 9 | Textures::ID toTextureID(Aircraft::Type type) 10 | { 11 | switch (type) 12 | { 13 | case Aircraft::Eagle: 14 | return Textures::Eagle; 15 | 16 | case Aircraft::Raptor: 17 | return Textures::Raptor; 18 | } 19 | } 20 | 21 | Aircraft::Aircraft(Type type, const TextureHolder& textures) 22 | : mType(type) 23 | , mSprite(textures.get(toTextureID(type))) 24 | { 25 | centerOrigin(mSprite); 26 | } 27 | 28 | void Aircraft::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const 29 | { 30 | target.draw(mSprite, states); 31 | } 32 | 33 | unsigned int Aircraft::getCategory() const 34 | { 35 | switch (mType) 36 | { 37 | case Eagle: 38 | return Category::PlayerAircraft; 39 | 40 | default: 41 | return Category::EnemyAircraft; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /05_States/Source/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | set (SRC 3 | Aircraft.cpp 4 | Application.cpp 5 | Command.cpp 6 | CommandQueue.cpp 7 | Entity.cpp 8 | GameState.cpp 9 | MenuState.cpp 10 | PauseState.cpp 11 | Player.cpp 12 | SceneNode.cpp 13 | SpriteNode.cpp 14 | State.cpp 15 | StateStack.cpp 16 | TitleState.cpp 17 | Utility.cpp 18 | World.cpp) 19 | 20 | build_chapter(05_States SOURCES ${SRC}) -------------------------------------------------------------------------------- /05_States/Source/Command.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | Command::Command() 5 | : action() 6 | , category(Category::None) 7 | { 8 | } 9 | -------------------------------------------------------------------------------- /05_States/Source/CommandQueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void CommandQueue::push(const Command& command) 6 | { 7 | mQueue.push(command); 8 | } 9 | 10 | Command CommandQueue::pop() 11 | { 12 | Command command = mQueue.front(); 13 | mQueue.pop(); 14 | return command; 15 | } 16 | 17 | bool CommandQueue::isEmpty() const 18 | { 19 | return mQueue.empty(); 20 | } 21 | -------------------------------------------------------------------------------- /05_States/Source/Entity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | void Entity::setVelocity(sf::Vector2f velocity) 5 | { 6 | mVelocity = velocity; 7 | } 8 | 9 | void Entity::setVelocity(float vx, float vy) 10 | { 11 | mVelocity.x = vx; 12 | mVelocity.y = vy; 13 | } 14 | 15 | sf::Vector2f Entity::getVelocity() const 16 | { 17 | return mVelocity; 18 | } 19 | 20 | void Entity::accelerate(sf::Vector2f velocity) 21 | { 22 | mVelocity += velocity; 23 | } 24 | 25 | void Entity::accelerate(float vx, float vy) 26 | { 27 | mVelocity.x += vx; 28 | mVelocity.y += vy; 29 | } 30 | 31 | void Entity::updateCurrent(sf::Time dt) 32 | { 33 | move(mVelocity * dt.asSeconds()); 34 | } -------------------------------------------------------------------------------- /05_States/Source/GameState.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | GameState::GameState(StateStack& stack, Context context) 5 | : State(stack, context) 6 | , mWorld(*context.window) 7 | , mPlayer(*context.player) 8 | { 9 | } 10 | 11 | void GameState::draw() 12 | { 13 | mWorld.draw(); 14 | } 15 | 16 | bool GameState::update(sf::Time dt) 17 | { 18 | mWorld.update(dt); 19 | 20 | CommandQueue& commands = mWorld.getCommandQueue(); 21 | mPlayer.handleRealtimeInput(commands); 22 | 23 | return true; 24 | } 25 | 26 | bool GameState::handleEvent(const sf::Event& event) 27 | { 28 | // Game input handling 29 | CommandQueue& commands = mWorld.getCommandQueue(); 30 | mPlayer.handleEvent(event, commands); 31 | 32 | // Escape pressed, trigger the pause screen 33 | if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) 34 | requestStackPush(States::Pause); 35 | 36 | return true; 37 | } -------------------------------------------------------------------------------- /05_States/Source/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | 7 | int main() 8 | { 9 | try 10 | { 11 | Application app; 12 | app.run(); 13 | } 14 | catch (std::exception& e) 15 | { 16 | std::cout << "\nEXCEPTION: " << e.what() << std::endl; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /05_States/Source/ParallelTask.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | ParallelTask::ParallelTask() 5 | : mThread(&ParallelTask::runTask, this) 6 | , mFinished(false) 7 | { 8 | } 9 | 10 | void ParallelTask::execute() 11 | { 12 | mFinished = false; 13 | mElapsedTime.restart(); 14 | mThread.launch(); 15 | } 16 | 17 | bool ParallelTask::isFinished() 18 | { 19 | sf::Lock lock(mMutex); 20 | return mFinished; 21 | } 22 | 23 | float ParallelTask::getCompletion() 24 | { 25 | sf::Lock lock(mMutex); 26 | 27 | // 100% at 10 seconds of elapsed time 28 | return mElapsedTime.getElapsedTime().asSeconds() / 10.f; 29 | } 30 | 31 | void ParallelTask::runTask() 32 | { 33 | // Dummy task - stall 10 seconds 34 | bool ended = false; 35 | while (!ended) 36 | { 37 | sf::Lock lock(mMutex); // Protect the clock 38 | if (mElapsedTime.getElapsedTime().asSeconds() >= 10.f) 39 | ended = true; 40 | } 41 | 42 | { // mFinished may be accessed from multiple threads, protect it 43 | sf::Lock lock(mMutex); 44 | mFinished = true; 45 | } 46 | } -------------------------------------------------------------------------------- /05_States/Source/SpriteNode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | 6 | SpriteNode::SpriteNode(const sf::Texture& texture) 7 | : mSprite(texture) 8 | { 9 | } 10 | 11 | SpriteNode::SpriteNode(const sf::Texture& texture, const sf::IntRect& textureRect) 12 | : mSprite(texture, textureRect) 13 | { 14 | } 15 | 16 | void SpriteNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const 17 | { 18 | target.draw(mSprite, states); 19 | } -------------------------------------------------------------------------------- /05_States/Source/State.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | State::Context::Context(sf::RenderWindow& window, TextureHolder& textures, FontHolder& fonts, Player& player) 6 | : window(&window) 7 | , textures(&textures) 8 | , fonts(&fonts) 9 | , player(&player) 10 | { 11 | } 12 | 13 | State::State(StateStack& stack, Context context) 14 | : mStack(&stack) 15 | , mContext(context) 16 | { 17 | } 18 | 19 | State::~State() 20 | { 21 | } 22 | 23 | void State::requestStackPush(States::ID stateID) 24 | { 25 | mStack->pushState(stateID); 26 | } 27 | 28 | void State::requestStackPop() 29 | { 30 | mStack->popState(); 31 | } 32 | 33 | void State::requestStateClear() 34 | { 35 | mStack->clearStates(); 36 | } 37 | 38 | State::Context State::getContext() const 39 | { 40 | return mContext; 41 | } 42 | -------------------------------------------------------------------------------- /05_States/Source/Utility.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | 9 | void centerOrigin(sf::Sprite& sprite) 10 | { 11 | sf::FloatRect bounds = sprite.getLocalBounds(); 12 | sprite.setOrigin(std::floor(bounds.left + bounds.width / 2.f), std::floor(bounds.top + bounds.height / 2.f)); 13 | } 14 | 15 | void centerOrigin(sf::Text& text) 16 | { 17 | sf::FloatRect bounds = text.getLocalBounds(); 18 | text.setOrigin(std::floor(bounds.left + bounds.width / 2.f), std::floor(bounds.top + bounds.height / 2.f)); 19 | } 20 | -------------------------------------------------------------------------------- /06_Menus/Include/Book/Aircraft.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_AIRCRAFT_HPP 2 | #define BOOK_AIRCRAFT_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | 10 | class Aircraft : public Entity 11 | { 12 | public: 13 | enum Type 14 | { 15 | Eagle, 16 | Raptor, 17 | }; 18 | 19 | 20 | public: 21 | Aircraft(Type type, const TextureHolder& textures); 22 | virtual unsigned int getCategory() const; 23 | 24 | 25 | private: 26 | virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; 27 | 28 | private: 29 | Type mType; 30 | sf::Sprite mSprite; 31 | }; 32 | 33 | #endif // BOOK_AIRCRAFT_HPP 34 | -------------------------------------------------------------------------------- /06_Menus/Include/Book/Application.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_APPLICATION_HPP 2 | #define BOOK_APPLICATION_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | class Application 15 | { 16 | public: 17 | Application(); 18 | void run(); 19 | 20 | 21 | private: 22 | void processInput(); 23 | void update(sf::Time dt); 24 | void render(); 25 | 26 | void updateStatistics(sf::Time dt); 27 | void registerStates(); 28 | 29 | 30 | private: 31 | static const sf::Time TimePerFrame; 32 | 33 | sf::RenderWindow mWindow; 34 | TextureHolder mTextures; 35 | FontHolder mFonts; 36 | Player mPlayer; 37 | 38 | StateStack mStateStack; 39 | 40 | sf::Text mStatisticsText; 41 | sf::Time mStatisticsUpdateTime; 42 | std::size_t mStatisticsNumFrames; 43 | }; 44 | 45 | #endif // BOOK_APPLICATION_HPP 46 | -------------------------------------------------------------------------------- /06_Menus/Include/Book/Category.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_CATEGORY_HPP 2 | #define BOOK_CATEGORY_HPP 3 | 4 | 5 | // Entity/scene node category, used to dispatch commands 6 | namespace Category 7 | { 8 | enum Type 9 | { 10 | None = 0, 11 | Scene = 1 << 0, 12 | PlayerAircraft = 1 << 1, 13 | AlliedAircraft = 1 << 2, 14 | EnemyAircraft = 1 << 3, 15 | }; 16 | } 17 | 18 | #endif // BOOK_CATEGORY_HPP 19 | -------------------------------------------------------------------------------- /06_Menus/Include/Book/Command.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_COMMAND_HPP 2 | #define BOOK_COMMAND_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | 12 | class SceneNode; 13 | 14 | struct Command 15 | { 16 | Command(); 17 | 18 | std::function action; 19 | unsigned int category; 20 | }; 21 | 22 | template 23 | std::function derivedAction(Function fn) 24 | { 25 | return [=] (SceneNode& node, sf::Time dt) 26 | { 27 | // Check if cast is safe 28 | assert(dynamic_cast(&node) != nullptr); 29 | 30 | // Downcast node and invoke function on it 31 | fn(static_cast(node), dt); 32 | }; 33 | } 34 | 35 | #endif // BOOK_COMMAND_HPP 36 | -------------------------------------------------------------------------------- /06_Menus/Include/Book/CommandQueue.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_COMMANDQUEUE_HPP 2 | #define BOOK_COMMANDQUEUE_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | 9 | class CommandQueue 10 | { 11 | public: 12 | void push(const Command& command); 13 | Command pop(); 14 | bool isEmpty() const; 15 | 16 | 17 | private: 18 | std::queue mQueue; 19 | }; 20 | 21 | #endif // BOOK_COMMANDQUEUE_HPP 22 | -------------------------------------------------------------------------------- /06_Menus/Include/Book/Container.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_CONTAINER_HPP 2 | #define BOOK_CONTAINER_HPP 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | 10 | namespace GUI 11 | { 12 | 13 | class Container : public Component 14 | { 15 | public: 16 | typedef std::shared_ptr Ptr; 17 | 18 | 19 | public: 20 | Container(); 21 | 22 | void pack(Component::Ptr component); 23 | 24 | virtual bool isSelectable() const; 25 | virtual void handleEvent(const sf::Event& event); 26 | 27 | 28 | private: 29 | virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; 30 | 31 | bool hasSelection() const; 32 | void select(std::size_t index); 33 | void selectNext(); 34 | void selectPrevious(); 35 | 36 | 37 | private: 38 | std::vector mChildren; 39 | int mSelectedChild; 40 | }; 41 | 42 | } 43 | 44 | #endif // BOOK_CONTAINER_HPP 45 | -------------------------------------------------------------------------------- /06_Menus/Include/Book/Entity.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_ENTITY_HPP 2 | #define BOOK_ENTITY_HPP 3 | 4 | #include 5 | 6 | 7 | class Entity : public SceneNode 8 | { 9 | public: 10 | void setVelocity(sf::Vector2f velocity); 11 | void setVelocity(float vx, float vy); 12 | void accelerate(sf::Vector2f velocity); 13 | void accelerate(float vx, float vy); 14 | sf::Vector2f getVelocity() const; 15 | 16 | 17 | private: 18 | virtual void updateCurrent(sf::Time dt); 19 | 20 | 21 | private: 22 | sf::Vector2f mVelocity; 23 | }; 24 | 25 | #endif // BOOK_ENTITY_HPP 26 | -------------------------------------------------------------------------------- /06_Menus/Include/Book/GameState.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_GAMESTATE_HPP 2 | #define BOOK_GAMESTATE_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | 12 | class GameState : public State 13 | { 14 | public: 15 | GameState(StateStack& stack, Context context); 16 | 17 | virtual void draw(); 18 | virtual bool update(sf::Time dt); 19 | virtual bool handleEvent(const sf::Event& event); 20 | 21 | 22 | private: 23 | World mWorld; 24 | Player& mPlayer; 25 | }; 26 | 27 | #endif // BOOK_GAMESTATE_HPP -------------------------------------------------------------------------------- /06_Menus/Include/Book/Label.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOK_LABEL_HPP 2 | #define BOOK_LABEL_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | 11 | namespace GUI 12 | { 13 | 14 | class Label : public Component 15 | { 16 | public: 17 | typedef std::shared_ptr