├── .gitignore ├── README.md ├── build.sh ├── chapter_01 ├── 1. Opening the window │ ├── Main.cpp │ └── build.sh ├── 2. Rendering the rectangle │ ├── Main.cpp │ └── build.sh └── 3. Drawing a sprite │ ├── Main.cpp │ ├── Mushroom.png │ └── build.sh ├── chapter_02 └── 1. Core game structure │ ├── Game.cpp │ ├── Game.h │ ├── Main.cpp │ ├── Mushroom.png │ ├── Window.cpp │ ├── Window.h │ └── build.sh ├── chapter_03 ├── Game.cpp ├── Game.h ├── Main.cpp ├── Snake.cpp ├── Snake.h ├── Textbox.cpp ├── Textbox.h ├── Window.cpp ├── Window.h ├── World.cpp ├── World.h ├── arial.ttf └── build.sh ├── chapter_04 ├── EventManager.cpp ├── EventManager.h ├── Game.cpp ├── Game.h ├── Main.cpp ├── Mushroom.png ├── Window.cpp ├── Window.h ├── build.sh ├── keys.cfg └── main ├── chapter_05 ├── BaseState.h ├── EventManager.cpp ├── EventManager.h ├── Game.cpp ├── Game.h ├── Main.cpp ├── Mushroom.png ├── SharedContext.h ├── StateManager.cpp ├── StateManager.h ├── State_Game.cpp ├── State_Game.h ├── State_Intro.cpp ├── State_Intro.h ├── State_MainMenu.cpp ├── State_MainMenu.h ├── State_Paused.cpp ├── State_Paused.h ├── Window.cpp ├── Window.h ├── arial.ttf ├── build.sh ├── intro.png └── keys.cfg ├── chapter_06 ├── Anim_Base.cpp ├── Anim_Base.h ├── Anim_Directional.cpp ├── Anim_Directional.h ├── BaseState.h ├── Directions.h ├── EventManager.cpp ├── EventManager.h ├── Game.cpp ├── Game.h ├── Main.cpp ├── Mushroom.png ├── ResourceManager.h ├── SharedContext.h ├── SpriteSheet.cpp ├── SpriteSheet.h ├── StateManager.cpp ├── StateManager.h ├── State_Game.cpp ├── State_Game.h ├── State_GameOver.cpp ├── State_GameOver.h ├── State_Intro.cpp ├── State_Intro.h ├── State_MainMenu.cpp ├── State_MainMenu.h ├── State_Paused.cpp ├── State_Paused.h ├── TextureManager.h ├── Utilities.h ├── Window.cpp ├── Window.h ├── arial.ttf ├── build.sh ├── intro.png └── keys.cfg ├── chapter_07 ├── Anim_Base.cpp ├── Anim_Base.h ├── Anim_Directional.cpp ├── Anim_Directional.h ├── BaseState.h ├── Character.cpp ├── Character.h ├── DebugOverlay.h ├── Directions.h ├── Enemy.cpp ├── Enemy.h ├── EntityBase.cpp ├── EntityBase.h ├── EntityManager.cpp ├── EntityManager.h ├── EventManager.cpp ├── EventManager.h ├── Game.cpp ├── Game.h ├── Main.cpp ├── Map.cpp ├── Map.h ├── Player.cpp ├── Player.h ├── ResourceManager.h ├── Resources │ ├── keys.cfg │ ├── media │ │ ├── Characters │ │ │ ├── EnemyList.list │ │ │ ├── Player.char │ │ │ └── Rat.char │ │ ├── Fonts │ │ │ └── arial.ttf │ │ ├── Maps │ │ │ ├── map1.map │ │ │ └── map2.map │ │ ├── Spritesheets │ │ │ ├── Player.sheet │ │ │ └── Rat.sheet │ │ └── Textures │ │ │ ├── PlayerSheet.png │ │ │ ├── RatSheet.png │ │ │ ├── bg1.png │ │ │ ├── bg2.png │ │ │ ├── bg3.png │ │ │ ├── intro.png │ │ │ └── tilesheet.png │ ├── textures.cfg │ └── tiles.cfg ├── SharedContext.h ├── SpriteSheet.cpp ├── SpriteSheet.h ├── StateManager.cpp ├── StateManager.h ├── State_Game.cpp ├── State_Game.h ├── State_GameOver.cpp ├── State_GameOver.h ├── State_Intro.cpp ├── State_Intro.h ├── State_MainMenu.cpp ├── State_MainMenu.h ├── State_Paused.cpp ├── State_Paused.h ├── TextureManager.h ├── Utilities.h ├── Window.cpp ├── Window.h └── build.sh ├── chapter_08 ├── Anim_Base.cpp ├── Anim_Base.h ├── Anim_Directional.cpp ├── Anim_Directional.h ├── Assets │ ├── keys.cfg │ ├── media │ │ ├── Entities │ │ │ ├── Player.entity │ │ │ └── Skeleton.entity │ │ ├── Fonts │ │ │ └── arial.ttf │ │ ├── Maps │ │ │ ├── map1 - Copy.map │ │ │ └── map1.map │ │ ├── Spritesheets │ │ │ ├── Player.sheet │ │ │ └── Skeleton.sheet │ │ └── Textures │ │ │ ├── PlayerSheet.png │ │ │ ├── SkeletonSheet.png │ │ │ ├── intro.png │ │ │ └── tilesheet.png │ ├── textures.cfg │ └── tiles.cfg ├── BaseState.h ├── Bitmask.h ├── C_Base.h ├── C_Collidable.h ├── C_Drawable.h ├── C_Movable.h ├── C_Position.h ├── C_SpriteSheet.h ├── C_State.h ├── Communicator.h ├── DebugOverlay.h ├── Directions.h ├── ECS_Types.h ├── EntityEvents.h ├── EntityMessages.h ├── Entity_Manager.cpp ├── Entity_Manager.h ├── EventManager.cpp ├── EventManager.h ├── Event_Queue.h ├── Game.cpp ├── Game.h ├── Main.cpp ├── Map.cpp ├── Map.h ├── Message.h ├── MessageHandler.h ├── Observer.h ├── ResourceManager.h ├── S_Base.cpp ├── S_Base.h ├── S_Collision.cpp ├── S_Collision.h ├── S_Control.cpp ├── S_Control.h ├── S_Movement.cpp ├── S_Movement.h ├── S_Renderer.cpp ├── S_Renderer.h ├── S_SheetAnimation.cpp ├── S_SheetAnimation.h ├── S_State.cpp ├── S_State.h ├── SharedContext.h ├── SpriteSheet.cpp ├── SpriteSheet.h ├── StateManager.cpp ├── StateManager.h ├── State_Game.cpp ├── State_Game.h ├── State_GameOver.cpp ├── State_GameOver.h ├── State_Intro.cpp ├── State_Intro.h ├── State_MainMenu.cpp ├── State_MainMenu.h ├── State_Paused.cpp ├── State_Paused.h ├── System_Manager.cpp ├── System_Manager.h ├── TextureManager.h ├── Utilities.h ├── Window.cpp ├── Window.h └── build.sh ├── chapter_09 ├── Anim_Base.cpp ├── Anim_Base.h ├── Anim_Directional.cpp ├── Anim_Directional.h ├── Assets │ ├── keys.cfg │ ├── media │ │ ├── Entities │ │ │ ├── Player.entity │ │ │ └── Skeleton.entity │ │ ├── Fonts │ │ │ └── arial.ttf │ │ ├── Maps │ │ │ ├── map1 - Copy.map │ │ │ └── map1.map │ │ ├── Spritesheets │ │ │ ├── Player.sheet │ │ │ └── Skeleton.sheet │ │ └── Textures │ │ │ ├── PlayerSheet.png │ │ │ ├── SkeletonSheet.png │ │ │ ├── intro.png │ │ │ └── tilesheet.png │ ├── textures.cfg │ └── tiles.cfg ├── BaseState.h ├── Bitmask.h ├── C_Base.h ├── C_Collidable.h ├── C_Controller.h ├── C_Drawable.h ├── C_Movable.h ├── C_Position.h ├── C_SpriteSheet.h ├── C_State.h ├── Communicator.h ├── DebugOverlay.h ├── Directions.h ├── ECS_Types.h ├── EntityEvents.h ├── EntityMessages.h ├── Entity_Manager.cpp ├── Entity_Manager.h ├── EventManager.cpp ├── EventManager.h ├── Event_Queue.h ├── Game.cpp ├── Game.h ├── Main.cpp ├── Map.cpp ├── Map.h ├── Message.h ├── MessageHandler.h ├── Observer.h ├── ResourceManager.h ├── S_Base.cpp ├── S_Base.h ├── S_Collision.cpp ├── S_Collision.h ├── S_Control.cpp ├── S_Control.h ├── S_Movement.cpp ├── S_Movement.h ├── S_Renderer.cpp ├── S_Renderer.h ├── S_SheetAnimation.cpp ├── S_SheetAnimation.h ├── S_State.cpp ├── S_State.h ├── SharedContext.h ├── SpriteSheet.cpp ├── SpriteSheet.h ├── StateManager.cpp ├── StateManager.h ├── State_Game.cpp ├── State_Game.h ├── State_GameOver.cpp ├── State_GameOver.h ├── State_Intro.cpp ├── State_Intro.h ├── State_MainMenu.cpp ├── State_MainMenu.h ├── State_Paused.cpp ├── State_Paused.h ├── System_Manager.cpp ├── System_Manager.h ├── TextureManager.h ├── Utilities.h ├── Window.cpp ├── Window.h └── build.sh ├── chapter_10 ├── Anim_Base.cpp ├── Anim_Base.h ├── Anim_Directional.cpp ├── Anim_Directional.h ├── Assets │ ├── keys.cfg │ ├── media │ │ ├── Entities │ │ │ ├── Player.entity │ │ │ └── Skeleton.entity │ │ ├── Fonts │ │ │ └── arial.ttf │ │ ├── Maps │ │ │ ├── map1 - Copy.map │ │ │ └── map1.map │ │ ├── Spritesheets │ │ │ ├── Player.sheet │ │ │ └── Skeleton.sheet │ │ └── Textures │ │ │ ├── PlayerSheet.png │ │ │ ├── SkeletonSheet.png │ │ │ ├── intro.png │ │ │ └── tilesheet.png │ ├── textures.cfg │ └── tiles.cfg ├── BaseState.h ├── Bitmask.h ├── C_Base.h ├── C_Collidable.h ├── C_Controller.h ├── C_Drawable.h ├── C_Movable.h ├── C_Position.h ├── C_SpriteSheet.h ├── C_State.h ├── Communicator.h ├── DebugOverlay.h ├── Directions.h ├── ECS_Types.h ├── EntityEvents.h ├── EntityMessages.h ├── Entity_Manager.cpp ├── Entity_Manager.h ├── EventManager.cpp ├── EventManager.h ├── Event_Queue.h ├── FontManager.h ├── GUI_Element.cpp ├── GUI_Element.h ├── GUI_Event.h ├── GUI_Interface.cpp ├── GUI_Interface.h ├── GUI_Label.cpp ├── GUI_Label.h ├── GUI_Manager.cpp ├── GUI_Manager.h ├── GUI_Scrollbar.cpp ├── GUI_Scrollbar.h ├── GUI_Style.h ├── GUI_Textfield.cpp ├── GUI_Textfield.h ├── Game.cpp ├── Game.h ├── Main.cpp ├── Map.cpp ├── Map.h ├── Message.h ├── MessageHandler.h ├── Observer.h ├── ResourceManager.h ├── S_Base.cpp ├── S_Base.h ├── S_Collision.cpp ├── S_Collision.h ├── S_Control.cpp ├── S_Control.h ├── S_Movement.cpp ├── S_Movement.h ├── S_Renderer.cpp ├── S_Renderer.h ├── S_SheetAnimation.cpp ├── S_SheetAnimation.h ├── S_State.cpp ├── S_State.h ├── SharedContext.h ├── SpriteSheet.cpp ├── SpriteSheet.h ├── StateManager.cpp ├── StateManager.h ├── State_Game.cpp ├── State_Game.h ├── State_GameOver.cpp ├── State_GameOver.h ├── State_Intro.cpp ├── State_Intro.h ├── State_MainMenu.cpp ├── State_MainMenu.h ├── State_Paused.cpp ├── State_Paused.h ├── System_Manager.cpp ├── System_Manager.h ├── TextureManager.h ├── Utilities.h ├── Window.cpp ├── Window.h └── build.sh ├── chapter_11 ├── Anim_Base.cpp ├── Anim_Base.h ├── Anim_Directional.cpp ├── Anim_Directional.h ├── Assets │ ├── fonts.cfg │ ├── keys.cfg │ ├── media │ │ ├── Entities │ │ │ ├── Player.entity │ │ │ └── Skeleton.entity │ │ ├── Fonts │ │ │ └── Vegur-Regular.otf │ │ ├── GUI_Interfaces │ │ │ ├── MainMenu.interface │ │ │ └── Test.interface │ │ ├── GUI_Styles │ │ │ ├── MainMenu.style │ │ │ ├── MainMenuLabel.style │ │ │ ├── MainMenuTitle.style │ │ │ ├── Test.style │ │ │ ├── Test2.style │ │ │ ├── TestInterface.style │ │ │ └── TestScrollbar.style │ │ ├── Maps │ │ │ └── map1.map │ │ ├── Spritesheets │ │ │ ├── Player.sheet │ │ │ └── Skeleton.sheet │ │ └── Textures │ │ │ ├── ADOM-OGA-UI.png │ │ │ ├── PlayerSheet.png │ │ │ ├── SkeletonSheet.png │ │ │ ├── intro.png │ │ │ └── tilesheet.png │ ├── textures.cfg │ └── tiles.cfg ├── BaseState.h ├── Bitmask.h ├── C_Base.h ├── C_Collidable.h ├── C_Controller.h ├── C_Drawable.h ├── C_Movable.h ├── C_Position.h ├── C_SpriteSheet.h ├── C_State.h ├── Communicator.h ├── DebugOverlay.h ├── Directions.h ├── ECS_Types.h ├── EntityEvents.h ├── EntityMessages.h ├── Entity_Manager.cpp ├── Entity_Manager.h ├── EventManager.cpp ├── EventManager.h ├── Event_Queue.h ├── FontManager.h ├── GUI_Element.cpp ├── GUI_Element.h ├── GUI_Event.h ├── GUI_Interface.cpp ├── GUI_Interface.h ├── GUI_Label.cpp ├── GUI_Label.h ├── GUI_Manager.cpp ├── GUI_Manager.h ├── GUI_Scrollbar.cpp ├── GUI_Scrollbar.h ├── GUI_Style.h ├── GUI_Textfield.cpp ├── GUI_Textfield.h ├── Game.cpp ├── Game.h ├── Main.cpp ├── Map.cpp ├── Map.h ├── Message.h ├── MessageHandler.h ├── Observer.h ├── ResourceManager.h ├── S_Base.cpp ├── S_Base.h ├── S_Collision.cpp ├── S_Collision.h ├── S_Control.cpp ├── S_Control.h ├── S_Movement.cpp ├── S_Movement.h ├── S_Renderer.cpp ├── S_Renderer.h ├── S_SheetAnimation.cpp ├── S_SheetAnimation.h ├── S_State.cpp ├── S_State.h ├── SharedContext.h ├── SpriteSheet.cpp ├── SpriteSheet.h ├── StateManager.cpp ├── StateManager.h ├── State_Game.cpp ├── State_Game.h ├── State_GameOver.cpp ├── State_GameOver.h ├── State_Intro.cpp ├── State_Intro.h ├── State_MainMenu.cpp ├── State_MainMenu.h ├── State_Paused.cpp ├── State_Paused.h ├── System_Manager.cpp ├── System_Manager.h ├── TextureManager.h ├── Utilities.h ├── Window.cpp ├── Window.h └── build.sh ├── chapter_12 ├── Anim_Base.cpp ├── Anim_Base.h ├── Anim_Directional.cpp ├── Anim_Directional.h ├── Assets │ ├── audio.cfg.txt │ ├── fonts.cfg.txt │ ├── keys.cfg.txt │ ├── media │ │ ├── Audio │ │ │ ├── 1_Mono.ogg │ │ │ ├── 1_Stereo.ogg │ │ │ ├── Electrix_NES.ogg │ │ │ ├── TownTheme.ogg │ │ │ ├── footstep.ogg │ │ │ └── glass.ogg │ │ ├── Entities │ │ │ ├── Player.entity │ │ │ └── Skeleton.entity │ │ ├── Fonts │ │ │ └── Vegur-Regular.otf │ │ ├── GUI_Interfaces │ │ │ ├── MainMenu.interface │ │ │ └── Test.interface │ │ ├── GUI_Styles │ │ │ ├── MainMenu.style │ │ │ ├── MainMenuLabel.style │ │ │ ├── MainMenuTitle.style │ │ │ ├── Test.style │ │ │ ├── Test2.style │ │ │ ├── TestInterface.style │ │ │ └── TestScrollbar.style │ │ ├── Maps │ │ │ └── map1.map │ │ ├── Sounds │ │ │ ├── footstep.sound │ │ │ ├── test.sound │ │ │ └── test2.sound │ │ ├── Spritesheets │ │ │ ├── Player.sheet │ │ │ └── Skeleton.sheet │ │ └── Textures │ │ │ ├── ADOM-OGA-UI.png │ │ │ ├── PlayerSheet.png │ │ │ ├── SkeletonSheet.png │ │ │ ├── intro.png │ │ │ └── tilesheet.png │ ├── openal32.dll │ ├── textures.cfg.txt │ └── tiles.cfg.txt ├── AudioManager.h ├── BaseState.h ├── Bitmask.h ├── C_Base.h ├── C_Collidable.h ├── C_Controller.h ├── C_Drawable.h ├── C_Movable.h ├── C_Position.h ├── C_SoundEmitter.h ├── C_SoundListener.h ├── C_SpriteSheet.h ├── C_State.h ├── Communicator.h ├── DebugOverlay.h ├── Directions.h ├── ECS_Types.h ├── EntityEvents.h ├── EntityMessages.h ├── Entity_Manager.cpp ├── Entity_Manager.h ├── EventManager.cpp ├── EventManager.h ├── Event_Queue.h ├── FontManager.h ├── GUI_Element.cpp ├── GUI_Element.h ├── GUI_Event.h ├── GUI_Interface.cpp ├── GUI_Interface.h ├── GUI_Label.cpp ├── GUI_Label.h ├── GUI_Manager.cpp ├── GUI_Manager.h ├── GUI_Scrollbar.cpp ├── GUI_Scrollbar.h ├── GUI_Style.h ├── GUI_TextField.cpp ├── GUI_TextField.h ├── Game.cpp ├── Game.h ├── Main.cpp ├── Map.cpp ├── Map.h ├── Message.h ├── MessageHandler.h ├── Observer.h ├── ResourceManager.h ├── S_Base.cpp ├── S_Base.h ├── S_Collision.cpp ├── S_Collision.h ├── S_Control.cpp ├── S_Control.h ├── S_Movement.cpp ├── S_Movement.h ├── S_Renderer.cpp ├── S_Renderer.h ├── S_SheetAnimation.cpp ├── S_SheetAnimation.h ├── S_Sound.cpp ├── S_Sound.h ├── S_State.cpp ├── S_State.h ├── SharedContext.h ├── SoundManager.cpp ├── SoundManager.h ├── SoundProps.h ├── SpriteSheet.cpp ├── SpriteSheet.h ├── StateManager.cpp ├── StateManager.h ├── State_Game.cpp ├── State_Game.h ├── State_GameOver.cpp ├── State_GameOver.h ├── State_Intro.cpp ├── State_Intro.h ├── State_MainMenu.cpp ├── State_MainMenu.h ├── State_Paused.cpp ├── State_Paused.h ├── System_Manager.cpp ├── System_Manager.h ├── TextureManager.h ├── Utilities.h ├── Window.cpp └── Window.h ├── chapter_13 ├── Client │ ├── Client.cpp │ ├── Client.h │ └── Client_Main.cpp ├── Server │ ├── Server.cpp │ ├── Server.h │ └── Server_Main.cpp └── Shared │ ├── Main.cpp │ ├── NetworkDefinitions.h │ ├── PacketTypes.cpp │ └── PacketTypes.h └── chapter_14 ├── Client ├── Anim_Base.cpp ├── Anim_Base.h ├── Anim_Directional.cpp ├── Anim_Directional.h ├── AudioManager.h ├── BaseState.h ├── C_Drawable.h ├── C_SoundEmitter.h ├── C_SoundListener.h ├── C_SpriteSheet.h ├── C_UI_Element.h ├── Client.cpp ├── Client.h ├── Client_Entity_Manager.cpp ├── Client_Entity_Manager.h ├── Client_System_Manager.cpp ├── Client_System_Manager.h ├── DebugOverlay.h ├── EventManager.cpp ├── EventManager.h ├── FontManager.h ├── GUI_Element.cpp ├── GUI_Element.h ├── GUI_Event.h ├── GUI_Interface.cpp ├── GUI_Interface.h ├── GUI_Label.cpp ├── GUI_Label.h ├── GUI_Manager.cpp ├── GUI_Manager.h ├── GUI_Scrollbar.cpp ├── GUI_Scrollbar.h ├── GUI_Style.h ├── GUI_Textfield.cpp ├── GUI_Textfield.h ├── Game.cpp ├── Game.h ├── Main.cpp ├── Map.cpp ├── Map.h ├── NetSettings.h ├── Resources │ ├── Client.exe[change] │ ├── audio.cfg.txt │ ├── fonts.cfg.txt │ ├── keys.cfg.txt │ ├── media │ │ ├── Audio │ │ │ ├── Electrix_NES.ogg │ │ │ ├── TownTheme.ogg │ │ │ ├── death.ogg │ │ │ ├── footstep.ogg │ │ │ ├── glass.ogg │ │ │ ├── gruntsound.ogg │ │ │ ├── impact.ogg │ │ │ └── swish.ogg │ │ ├── Entities │ │ │ └── Player.entity │ │ ├── Fonts │ │ │ └── Vegur-Regular.otf │ │ ├── GUI_Interfaces │ │ │ ├── MainMenu.interface │ │ │ └── Test.interface │ │ ├── GUI_Styles │ │ │ ├── DefaultLabel.style │ │ │ ├── MainMenu.style │ │ │ ├── MainMenuLabel.style │ │ │ ├── MainMenuTextfield.style │ │ │ ├── MainMenuTitle.style │ │ │ ├── Test.style │ │ │ ├── Test2.style │ │ │ ├── TestInterface.style │ │ │ └── TestScrollbar.style │ │ ├── Maps │ │ │ └── map1.map │ │ ├── Sounds │ │ │ ├── footstep.sound │ │ │ ├── grunt.sound │ │ │ ├── impact.sound │ │ │ ├── playerdeath.sound │ │ │ ├── swish.sound │ │ │ ├── test.sound │ │ │ └── test2.sound │ │ ├── Spritesheets │ │ │ ├── Player.sheet │ │ │ └── Skeleton.sheet │ │ └── Textures │ │ │ ├── ADOM-OGA-UI.png │ │ │ ├── HeartBar.png │ │ │ ├── PlayerSheet.png │ │ │ ├── SkeletonSheet.png │ │ │ ├── intro.png │ │ │ └── tilesheet.png │ ├── openal32.dll[change] │ ├── textures.cfg.txt │ └── tiles.cfg.txt ├── S_CharacterUI.cpp ├── S_CharacterUI.h ├── S_Collision.cpp ├── S_Collision.h ├── S_Movement.cpp ├── S_Movement.h ├── S_Network.cpp ├── S_Network.h ├── S_Renderer.cpp ├── S_Renderer.h ├── S_SheetAnimation.cpp ├── S_SheetAnimation.h ├── S_Sound.cpp ├── S_Sound.h ├── SharedContext.h ├── SoundManager.cpp ├── SoundManager.h ├── SoundProps.h ├── SpriteSheet.cpp ├── SpriteSheet.h ├── StateManager.cpp ├── StateManager.h ├── State_Game.cpp ├── State_Game.h ├── State_GameOver.cpp ├── State_GameOver.h ├── State_Intro.cpp ├── State_Intro.h ├── State_MainMenu.cpp ├── State_MainMenu.h ├── State_Paused.cpp ├── State_Paused.h ├── TextureManager.h ├── Window.cpp └── Window.h ├── Server ├── C_Attacker.h ├── Map.cpp ├── Map.h ├── NetSettings.h ├── Resources │ ├── Server.exe[change] │ ├── media │ │ ├── Entities │ │ │ └── Player.entity │ │ └── Maps │ │ │ └── map1.map │ └── tiles.cfg.txt ├── S_Collision.cpp ├── S_Collision.h ├── S_Combat.cpp ├── S_Combat.h ├── S_Movement.cpp ├── S_Movement.h ├── S_Network.cpp ├── S_Network.h ├── S_Timers.cpp ├── S_Timers.h ├── Server.cpp ├── Server.h ├── Server_Entity_Manager.cpp ├── Server_Entity_Manager.h ├── Server_Main.cpp ├── Server_System_Manager.cpp ├── Server_System_Manager.h ├── World.cpp └── World.h └── Shared ├── Bitmask.h ├── C_Base.h ├── C_Client.h ├── C_Collidable.h ├── C_Controller.h ├── C_Health.h ├── C_Movable.h ├── C_Name.h ├── C_Position.h ├── C_State.h ├── C_TimedComponentBase.h ├── Communicator.h ├── Directions.h ├── ECS_Types.h ├── EntityEvents.h ├── EntityMessages.h ├── EntitySnapshot.cpp ├── EntitySnapshot.h ├── Entity_Manager.cpp ├── Entity_Manager.h ├── Event_Queue.h ├── Main.cpp ├── Message.h ├── MessageHandler.h ├── NetworkDefinitions.h ├── Observer.h ├── PacketTypes.cpp ├── PacketTypes.h ├── ResourceManager.h ├── S_Base.cpp ├── S_Base.h ├── S_Control.cpp ├── S_Control.h ├── S_State.cpp ├── S_State.h ├── System_Manager.cpp ├── System_Manager.h └── Utilities.h /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/.gitignore -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SFML Game Development By Example 2 | This is the books code, fixedand made runnable for linux 3 | 4 | ## Build (Linux) 5 | To build, go to the chapter and run ```./build.sh```. I will write some CMakeLists later. 6 | 7 | After that, you get the executable named *main* 8 | 9 | ## Windows users 10 | To build on windows, you need to fix the respective *Utilities.h* files, first introduced in the code of *chapter_06*. There is currently no build-script for windows. 11 | 12 | ## Current state 13 | chapter_01 - chapter_11 are done. It is much work because the original code is full of errors and some logical problems. 14 | 15 | chapter_12 - chapter_14 coming -------------------------------------------------------------------------------- /chapter_01/1. Opening the window/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char* argv[]){ 4 | sf::RenderWindow window(sf::VideoMode(640,480), "First window!"); 5 | 6 | while(window.isOpen()){ 7 | sf::Event event; 8 | while(window.pollEvent(event)){ 9 | if(event.type == sf::Event::Closed){ 10 | // Close window button clicked. 11 | window.close(); 12 | } 13 | } 14 | window.clear(sf::Color::Black); 15 | // Draw here. 16 | window.display(); 17 | } 18 | } -------------------------------------------------------------------------------- /chapter_01/1. Opening the window/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Main.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_01/2. Rendering the rectangle/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char* argv[]){ 4 | sf::RenderWindow window(sf::VideoMode(640,480), "Rendering the rectangle."); 5 | 6 | // Creating our shape. 7 | sf::RectangleShape rectangle(sf::Vector2f(128.0f,128.0f)); 8 | rectangle.setFillColor(sf::Color::Red); 9 | rectangle.setPosition(320,240); 10 | rectangle.setOrigin(rectangle.getSize().x / 2, rectangle.getSize().y / 2); 11 | 12 | while(window.isOpen()){ 13 | sf::Event event; 14 | while(window.pollEvent(event)){ 15 | if(event.type == sf::Event::Closed){ 16 | // Close window button clicked. 17 | window.close(); 18 | } 19 | } 20 | window.clear(sf::Color::Black); 21 | window.draw(rectangle); // Drawing our shape. 22 | window.display(); 23 | } 24 | } -------------------------------------------------------------------------------- /chapter_01/2. Rendering the rectangle/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Main.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_01/3. Drawing a sprite/Mushroom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_01/3. Drawing a sprite/Mushroom.png -------------------------------------------------------------------------------- /chapter_01/3. Drawing a sprite/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Main.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_02/1. Core game structure/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | 4 | class Game{ 5 | public: 6 | Game(); 7 | ~Game(); 8 | 9 | void HandleInput(); 10 | void Update(); 11 | void Render(); 12 | 13 | Window* GetWindow(); 14 | 15 | sf::Time GetElapsed(); 16 | void RestartClock(); 17 | private: 18 | void MoveMushroom(); 19 | 20 | Window m_window; 21 | sf::Clock m_clock; 22 | sf::Time m_elapsed; 23 | 24 | sf::Texture m_mushroomTexture; 25 | sf::Sprite m_mushroom; 26 | sf::Vector2i m_increment; 27 | }; -------------------------------------------------------------------------------- /chapter_02/1. Core game structure/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | int main(int argc, char* argv[]){ 4 | // Program entry point. 5 | Game game; // Creating our game object. 6 | while(!game.GetWindow()->IsDone()){ 7 | // Game loop. 8 | game.HandleInput(); 9 | game.Update(); 10 | game.Render(); 11 | game.RestartClock(); 12 | } 13 | } -------------------------------------------------------------------------------- /chapter_02/1. Core game structure/Mushroom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_02/1. Core game structure/Mushroom.png -------------------------------------------------------------------------------- /chapter_02/1. Core game structure/Window.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | class Window{ 7 | public: 8 | Window(); 9 | Window(const std::string& title, const sf::Vector2u& size); 10 | ~Window(); 11 | 12 | void BeginDraw(); 13 | void EndDraw(); 14 | 15 | void Update(); 16 | 17 | bool IsDone(); 18 | bool IsFullscreen(); 19 | sf::Vector2u GetWindowSize(); 20 | 21 | void ToggleFullscreen(); 22 | 23 | void Draw(sf::Drawable& l_drawable); 24 | private: 25 | void Setup(const std::string title, const sf::Vector2u& size); 26 | void Create(); 27 | void Destroy(); 28 | 29 | sf::RenderWindow m_window; 30 | sf::Vector2u m_windowSize; 31 | std::string m_windowTitle; 32 | bool m_isDone; 33 | bool m_isFullscreen; 34 | }; -------------------------------------------------------------------------------- /chapter_02/1. Core game structure/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Main.cpp Game.cpp Window.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_03/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "World.h" 4 | #include "Snake.h" 5 | #include "Textbox.h" 6 | 7 | class Game{ 8 | public: 9 | Game(); 10 | ~Game(); 11 | 12 | void HandleInput(); 13 | void Update(); 14 | void Render(); 15 | 16 | sf::Time GetElapsed(); 17 | void RestartClock(); 18 | 19 | Window* GetWindow(); 20 | private: 21 | Window m_window; 22 | sf::Clock m_clock; 23 | float m_elapsed; 24 | 25 | World m_world; 26 | Snake m_snake; 27 | Textbox m_textbox; 28 | }; -------------------------------------------------------------------------------- /chapter_03/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | int main(int argc, char* argv[]){ 4 | // Program entry point. 5 | Game game; 6 | while(!game.GetWindow()->IsDone()){ 7 | game.HandleInput(); 8 | game.Update(); 9 | game.Render(); 10 | game.RestartClock(); 11 | } 12 | } -------------------------------------------------------------------------------- /chapter_03/Textbox.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | using MessageContainer = std::vector; 7 | 8 | class Textbox{ 9 | public: 10 | Textbox(); 11 | Textbox(int l_visible, int l_charSize, 12 | int l_width, sf::Vector2f l_screenPos); 13 | ~Textbox(); 14 | 15 | void Setup(int l_visible, int l_charSize, 16 | int l_width, sf::Vector2f l_screenPos); 17 | void Add(std::string l_message); 18 | void Clear(); 19 | 20 | void Render(sf::RenderWindow& l_wind); 21 | private: 22 | MessageContainer m_messages; 23 | int m_numVisible; 24 | 25 | sf::RectangleShape m_backdrop; 26 | sf::Font m_font; 27 | sf::Text m_content; 28 | }; -------------------------------------------------------------------------------- /chapter_03/Window.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | class Window{ 7 | public: 8 | Window(); 9 | Window(const std::string& title, const sf::Vector2u& size); 10 | ~Window(); 11 | 12 | void BeginDraw(); 13 | void EndDraw(); 14 | 15 | void Update(); 16 | 17 | bool IsDone(); 18 | bool IsFullscreen(); 19 | sf::RenderWindow* GetRenderWindow(); 20 | sf::Vector2u GetWindowSize(); 21 | 22 | void ToggleFullscreen(); 23 | 24 | void Draw(sf::Drawable& l_drawable); 25 | private: 26 | void Setup(const std::string title, const sf::Vector2u& size); 27 | void Create(); 28 | void Destroy(); 29 | 30 | sf::RenderWindow m_window; 31 | sf::Vector2u m_windowSize; 32 | std::string m_windowTitle; 33 | bool m_isDone; 34 | bool m_isFullscreen; 35 | }; -------------------------------------------------------------------------------- /chapter_03/World.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "Snake.h" 4 | 5 | class World{ 6 | public: 7 | World(sf::Vector2u l_windSize); 8 | ~World(); 9 | 10 | int GetBlockSize(); 11 | 12 | void RespawnApple(); 13 | 14 | void Update(Snake& l_player); 15 | void Render(sf::RenderWindow& l_window); 16 | private: 17 | sf::Vector2u m_windowSize; 18 | sf::Vector2i m_item; 19 | int m_blockSize; 20 | 21 | sf::CircleShape m_appleShape; 22 | sf::RectangleShape m_bounds[4]; 23 | }; -------------------------------------------------------------------------------- /chapter_03/arial.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_03/arial.ttf -------------------------------------------------------------------------------- /chapter_03/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Main.cpp Game.cpp Window.cpp Snake.cpp Textbox.cpp World.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_04/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include 5 | 6 | class Game{ 7 | public: 8 | Game(); 9 | ~Game(); 10 | 11 | void Update(); 12 | void Render(); 13 | 14 | sf::Time GetElapsed(); 15 | void RestartClock(); 16 | 17 | void MoveSprite(EventDetails* l_details); 18 | 19 | Window* GetWindow(); 20 | private: 21 | Window m_window; 22 | sf::Clock Clock; 23 | 24 | sf::Texture m_texture; 25 | sf::Sprite m_sprite; 26 | }; -------------------------------------------------------------------------------- /chapter_04/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | int main(int argc, char* argv[]){ 4 | // Program entry point. 5 | Game game; 6 | while(!game.GetWindow()->IsDone()){ 7 | game.Update(); 8 | game.Render(); 9 | game.RestartClock(); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter_04/Mushroom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_04/Mushroom.png -------------------------------------------------------------------------------- /chapter_04/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Main.cpp Game.cpp Window.cpp EventManager.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_04/keys.cfg: -------------------------------------------------------------------------------- 1 | Window_close 0:0 2 | Fullscreen_toggle 5:89 3 | Move 9:0 -------------------------------------------------------------------------------- /chapter_04/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_04/main -------------------------------------------------------------------------------- /chapter_05/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "StateManager.h" 5 | #include "SharedContext.h" 6 | #include 7 | 8 | class Game{ 9 | public: 10 | Game(); 11 | ~Game(); 12 | 13 | void Update(); 14 | void Render(); 15 | void LateUpdate(); 16 | 17 | sf::Time GetElapsed(); 18 | 19 | Window* GetWindow(); 20 | private: 21 | SharedContext m_context; 22 | Window m_window; 23 | StateManager m_stateManager; 24 | sf::Clock m_clock; 25 | sf::Time m_elapsed; 26 | void RestartClock(); 27 | }; -------------------------------------------------------------------------------- /chapter_05/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | int main(int argc, char* argv[]){ 4 | // Program entry point. 5 | Game game; 6 | while(!game.GetWindow()->IsDone()){ 7 | game.Update(); 8 | game.Render(); 9 | game.LateUpdate(); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter_05/Mushroom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_05/Mushroom.png -------------------------------------------------------------------------------- /chapter_05/SharedContext.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | struct SharedContext{ 5 | SharedContext() 6 | :m_wind(nullptr), 7 | m_eventManager(nullptr){} 8 | Window* m_wind; 9 | EventManager* m_eventManager; 10 | }; -------------------------------------------------------------------------------- /chapter_05/State_Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Game : public BaseState{ 6 | public: 7 | State_Game(StateManager* l_stateManager); 8 | ~State_Game(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void MainMenu(EventDetails* l_details); 20 | void Pause(EventDetails* l_details); 21 | private: 22 | sf::Texture m_texture; 23 | sf::Sprite m_sprite; 24 | sf::Vector2f m_increment; 25 | }; -------------------------------------------------------------------------------- /chapter_05/State_Intro.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Intro : public BaseState{ 6 | public: 7 | State_Intro(StateManager* l_stateManager); 8 | ~State_Intro(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Continue(EventDetails* l_details); 20 | private: 21 | sf::Texture m_introTexture; 22 | sf::Sprite m_introSprite; 23 | sf::Font m_font; 24 | sf::Text m_text; 25 | 26 | float m_timePassed; 27 | }; -------------------------------------------------------------------------------- /chapter_05/State_MainMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_MainMenu : public BaseState{ 6 | public: 7 | State_MainMenu(StateManager* l_stateManager); 8 | ~State_MainMenu(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void MouseClick(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | 24 | sf::Vector2f m_buttonSize; 25 | sf::Vector2f m_buttonPos; 26 | unsigned int m_buttonPadding; 27 | 28 | sf::RectangleShape m_rects[3]; 29 | sf::Text m_labels[3]; 30 | }; -------------------------------------------------------------------------------- /chapter_05/State_Paused.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Paused : public BaseState{ 6 | public: 7 | State_Paused(StateManager* l_stateManager); 8 | ~State_Paused(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Unpause(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | sf::RectangleShape m_rect; 24 | }; -------------------------------------------------------------------------------- /chapter_05/arial.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_05/arial.ttf -------------------------------------------------------------------------------- /chapter_05/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Main.cpp Game.cpp Window.cpp EventManager.cpp State_Game.cpp State_Intro.cpp State_MainMenu.cpp State_Paused.cpp StateManager.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_05/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_05/intro.png -------------------------------------------------------------------------------- /chapter_05/keys.cfg: -------------------------------------------------------------------------------- 1 | Window_close 0:0 2 | Fullscreen_toggle 5:89 3 | Intro_Continue 5:57 4 | Mouse_Left 9:0 5 | Key_Escape 5:36 6 | Key_P 5:15 -------------------------------------------------------------------------------- /chapter_06/Anim_Directional.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Anim_Base.h" 3 | #include "Directions.h" 4 | 5 | class Anim_Directional : public Anim_Base{ 6 | protected: 7 | void FrameStep(); 8 | void CropSprite(); 9 | void ReadIn(std::stringstream& l_stream); 10 | }; -------------------------------------------------------------------------------- /chapter_06/Directions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | enum class Direction{ Right = 0, Left }; -------------------------------------------------------------------------------- /chapter_06/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "StateManager.h" 5 | #include "SharedContext.h" 6 | #include 7 | 8 | class Game{ 9 | public: 10 | Game(); 11 | ~Game(); 12 | 13 | void Update(); 14 | void Render(); 15 | void LateUpdate(); 16 | 17 | sf::Time GetElapsed(); 18 | 19 | Window* GetWindow(); 20 | private: 21 | SharedContext m_context; 22 | Window m_window; 23 | StateManager m_stateManager; 24 | sf::Clock m_clock; 25 | sf::Time m_elapsed; 26 | void RestartClock(); 27 | }; -------------------------------------------------------------------------------- /chapter_06/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | int main(int argc, char* argv[]){ 4 | // Program entry point. 5 | Game game; 6 | while(!game.GetWindow()->IsDone()){ 7 | game.Update(); 8 | game.Render(); 9 | game.LateUpdate(); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter_06/Mushroom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_06/Mushroom.png -------------------------------------------------------------------------------- /chapter_06/SharedContext.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | struct SharedContext{ 5 | SharedContext() 6 | :m_wind(nullptr), 7 | m_eventManager(nullptr){} 8 | Window* m_wind; 9 | EventManager* m_eventManager; 10 | }; -------------------------------------------------------------------------------- /chapter_06/State_Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Game : public BaseState{ 6 | public: 7 | State_Game(StateManager* l_stateManager); 8 | ~State_Game(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void MainMenu(EventDetails* l_details); 20 | void Pause(EventDetails* l_details); 21 | private: 22 | sf::Texture m_texture; 23 | sf::Sprite m_sprite; 24 | sf::Vector2f m_increment; 25 | }; -------------------------------------------------------------------------------- /chapter_06/State_GameOver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include 4 | 5 | class State_GameOver : public BaseState{ 6 | public: 7 | State_GameOver(StateManager* l_stateManager); 8 | ~State_GameOver(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | private: 19 | sf::Font m_font; 20 | sf::Text m_text; 21 | float m_elapsed; 22 | }; -------------------------------------------------------------------------------- /chapter_06/State_Intro.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Intro : public BaseState{ 6 | public: 7 | State_Intro(StateManager* l_stateManager); 8 | ~State_Intro(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Continue(EventDetails* l_details); 20 | private: 21 | sf::Texture m_introTexture; 22 | sf::Sprite m_introSprite; 23 | sf::Font m_font; 24 | sf::Text m_text; 25 | 26 | float m_timePassed; 27 | }; -------------------------------------------------------------------------------- /chapter_06/State_MainMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_MainMenu : public BaseState{ 6 | public: 7 | State_MainMenu(StateManager* l_stateManager); 8 | ~State_MainMenu(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void MouseClick(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | 24 | sf::Vector2f m_buttonSize; 25 | sf::Vector2f m_buttonPos; 26 | unsigned int m_buttonPadding; 27 | 28 | sf::RectangleShape m_rects[3]; 29 | sf::Text m_labels[3]; 30 | }; -------------------------------------------------------------------------------- /chapter_06/State_Paused.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Paused : public BaseState{ 6 | public: 7 | State_Paused(StateManager* l_stateManager); 8 | ~State_Paused(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Unpause(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | sf::RectangleShape m_rect; 24 | }; -------------------------------------------------------------------------------- /chapter_06/TextureManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class TextureManager: public ResourceManager{ 6 | public: 7 | TextureManager(): ResourceManager("textures.cfg"){} 8 | 9 | sf::Texture* Load(const std::string& l_path){ 10 | sf::Texture* texture = new sf::Texture(); 11 | if(!texture->loadFromFile( 12 | Utils::GetWorkingDirectory() + l_path)) 13 | { 14 | delete texture; 15 | texture = nullptr; 16 | std::cerr << "! Failed to load texture: " << l_path << std::endl; 17 | } 18 | return texture; 19 | } 20 | }; -------------------------------------------------------------------------------- /chapter_06/arial.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_06/arial.ttf -------------------------------------------------------------------------------- /chapter_06/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Main.cpp Game.cpp Anim_Base.cpp Anim_Directional.cpp EventManager.cpp SpriteSheet.cpp State_Game.cpp State_GameOver.cpp State_Intro.cpp State_MainMenu.cpp State_Paused.cpp StateManager.cpp Window.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_06/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_06/intro.png -------------------------------------------------------------------------------- /chapter_06/keys.cfg: -------------------------------------------------------------------------------- 1 | Window_close 0:0 2 | Fullscreen_toggle 5:89 3 | Intro_Continue 5:57 4 | Mouse_Left 9:0 5 | Key_Escape 5:36 6 | Key_P 5:15 -------------------------------------------------------------------------------- /chapter_07/Anim_Directional.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Anim_Base.h" 3 | #include "Directions.h" 4 | 5 | class Anim_Directional : public Anim_Base{ 6 | protected: 7 | void FrameStep(); 8 | void CropSprite(); 9 | void ReadIn(std::stringstream& l_stream); 10 | }; -------------------------------------------------------------------------------- /chapter_07/Character.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "EntityBase.h" 3 | #include "SpriteSheet.h" 4 | 5 | class Character : public EntityBase{ 6 | friend class EntityManager; 7 | public: 8 | Character(EntityManager* l_entityMgr); 9 | virtual ~Character(); 10 | 11 | void Move(const Direction& l_dir); 12 | 13 | void Jump(); 14 | void Attack(); 15 | void GetHurt(const int& l_damage); 16 | 17 | void Load(const std::string& l_path); 18 | 19 | virtual void OnEntityCollision( 20 | EntityBase* l_collider, bool l_attack) = 0; 21 | 22 | virtual void Update(float l_dT); 23 | void Draw(sf::RenderWindow* l_wind); 24 | protected: 25 | void UpdateAttackAABB(); 26 | void Animate(); 27 | SpriteSheet m_spriteSheet; 28 | float m_jumpVelocity; 29 | 30 | int m_hitpoints; 31 | 32 | sf::FloatRect m_attackAABB; 33 | sf::Vector2f m_attackAABBoffset; 34 | }; -------------------------------------------------------------------------------- /chapter_07/DebugOverlay.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | class DebugOverlay{ 6 | public: 7 | DebugOverlay(){ 8 | m_debug = false; 9 | } 10 | 11 | void Add(sf::Drawable* l_drawable){ 12 | m_drawables.push_back(l_drawable); 13 | } 14 | 15 | void Draw(sf::RenderWindow* l_wind){ 16 | while(m_drawables.begin() != m_drawables.end()){ 17 | l_wind->draw(*m_drawables.back()); 18 | delete m_drawables.back(); 19 | m_drawables.pop_back(); 20 | } 21 | } 22 | 23 | bool Debug(){ return m_debug; } 24 | void SetDebug(const bool& l_val){ m_debug = l_val; } 25 | private: 26 | std::vector m_drawables; 27 | bool m_debug; 28 | }; -------------------------------------------------------------------------------- /chapter_07/Directions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | enum class Direction{ Right = 0, Left }; -------------------------------------------------------------------------------- /chapter_07/Enemy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Character.h" 3 | 4 | class Enemy : public Character{ 5 | public: 6 | Enemy(EntityManager* l_entityMgr); 7 | ~Enemy(); 8 | 9 | void OnEntityCollision(EntityBase* l_collider, bool l_attack); 10 | void Update(float l_dT); 11 | private: 12 | sf::Vector2f m_destination; 13 | bool m_hasDestination; 14 | }; -------------------------------------------------------------------------------- /chapter_07/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "StateManager.h" 5 | #include "TextureManager.h" 6 | #include "EntityManager.h" 7 | #include 8 | 9 | class Game{ 10 | public: 11 | Game(); 12 | ~Game(); 13 | 14 | void Update(); 15 | void Render(); 16 | void LateUpdate(); 17 | 18 | sf::Time GetElapsed(); 19 | 20 | Window* GetWindow(); 21 | private: 22 | void RestartClock(); 23 | 24 | sf::Clock m_clock; 25 | sf::Time m_elapsed; 26 | SharedContext m_context; 27 | Window m_window; 28 | EntityManager m_entityManager; 29 | TextureManager m_textureManager; 30 | StateManager m_stateManager; 31 | }; -------------------------------------------------------------------------------- /chapter_07/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | int main(int argc, char* argv[]){ 4 | // Program entry point. 5 | { 6 | Game game; 7 | while(!game.GetWindow()->IsDone()){ 8 | game.Update(); 9 | game.Render(); 10 | game.LateUpdate(); 11 | } 12 | } 13 | system("PAUSE"); 14 | } -------------------------------------------------------------------------------- /chapter_07/Player.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Character.h" 3 | #include "EventManager.h" 4 | 5 | class Player : public Character{ 6 | public: 7 | Player(EntityManager* l_entityMgr); 8 | ~Player(); 9 | 10 | void OnEntityCollision(EntityBase* l_collider, bool l_attack); 11 | void React(EventDetails* l_details); 12 | }; -------------------------------------------------------------------------------- /chapter_07/Resources/keys.cfg: -------------------------------------------------------------------------------- 1 | Window_close 0:0 2 | Fullscreen_toggle 5:89 3 | Intro_Continue 5:57 4 | Mouse_Left 9:0 5 | Key_Escape 5:36 6 | Key_P 5:15 7 | Key_T 5:19 8 | Key_K 5:10 9 | Key_O 5:14 10 | Key_X 5:23 11 | Player_MoveLeft 24:0 12 | Player_MoveRight 24:3 13 | Player_Jump 5:57 14 | Player_Attack 24:58 -------------------------------------------------------------------------------- /chapter_07/Resources/media/Characters/EnemyList.list: -------------------------------------------------------------------------------- 1 | |Name|CharFile| 2 | Rat Rat.char -------------------------------------------------------------------------------- /chapter_07/Resources/media/Characters/Player.char: -------------------------------------------------------------------------------- 1 | Name Player 2 | Spritesheet Player.sheet 3 | Hitpoints 5 4 | BoundingBox 20 26 5 | DamageBox -5 0 26 26 6 | Speed 1024 128 7 | JumpVelocity 250 8 | MaxVelocity 200 1024 -------------------------------------------------------------------------------- /chapter_07/Resources/media/Characters/Rat.char: -------------------------------------------------------------------------------- 1 | Name Rat 2 | Spritesheet Rat.sheet 3 | Hitpoints 2 4 | BoundingBox 14 14 5 | DamageBox 0 0 14 14 6 | Speed 512 64 7 | JumpVelocity 128 8 | MaxVelocity 128 1024 -------------------------------------------------------------------------------- /chapter_07/Resources/media/Fonts/arial.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_07/Resources/media/Fonts/arial.ttf -------------------------------------------------------------------------------- /chapter_07/Resources/media/Spritesheets/Player.sheet: -------------------------------------------------------------------------------- 1 | Texture PlayerSprite 2 | Size 32 32 3 | Scale 1.0 1.0 4 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 5 | AnimationType Directional 6 | Animation Idle 0 7 0 0.2 -1 -1 7 | Animation Walk 0 5 2 0.1 -1 -1 8 | Animation Jump 0 3 4 0.2 -1 -1 9 | Animation Attack 0 4 6 0.08 2 3 10 | Animation Hurt 0 2 8 0.2 -1 -1 11 | Animation Death 0 8 10 0.15 -1 -1 -------------------------------------------------------------------------------- /chapter_07/Resources/media/Spritesheets/Rat.sheet: -------------------------------------------------------------------------------- 1 | Texture RatSprite 2 | Size 32 32 3 | Scale 0.5 0.5 4 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 5 | AnimationType Directional 6 | Animation Idle 0 8 0 0.2 -1 -1 7 | Animation Walk 0 4 2 0.1 -1 -1 8 | Animation Jump 0 4 4 0.2 -1 -1 9 | Animation Attack 0 5 6 0.1 2 3 10 | Animation Hurt 0 2 8 0.2 -1 -1 11 | Animation Death 0 8 10 0.2 -1 -1 -------------------------------------------------------------------------------- /chapter_07/Resources/media/Textures/PlayerSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_07/Resources/media/Textures/PlayerSheet.png -------------------------------------------------------------------------------- /chapter_07/Resources/media/Textures/RatSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_07/Resources/media/Textures/RatSheet.png -------------------------------------------------------------------------------- /chapter_07/Resources/media/Textures/bg1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_07/Resources/media/Textures/bg1.png -------------------------------------------------------------------------------- /chapter_07/Resources/media/Textures/bg2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_07/Resources/media/Textures/bg2.png -------------------------------------------------------------------------------- /chapter_07/Resources/media/Textures/bg3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_07/Resources/media/Textures/bg3.png -------------------------------------------------------------------------------- /chapter_07/Resources/media/Textures/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_07/Resources/media/Textures/intro.png -------------------------------------------------------------------------------- /chapter_07/Resources/media/Textures/tilesheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_07/Resources/media/Textures/tilesheet.png -------------------------------------------------------------------------------- /chapter_07/Resources/textures.cfg: -------------------------------------------------------------------------------- 1 | Intro media/Textures/intro.png 2 | PlayerSprite media/Textures/PlayerSheet.png 3 | RatSprite media/Textures/RatSheet.png 4 | TileSheet media/Textures/tilesheet.png 5 | Bg1 media/Textures/bg1.png 6 | Bg2 media/Textures/bg2.png 7 | Bg3 media/Textures/bg3.png -------------------------------------------------------------------------------- /chapter_07/Resources/tiles.cfg: -------------------------------------------------------------------------------- 1 | |id|name|friction x|friction y|deadly 2 | 0 Grass 0.8 0 0 3 | 1 Dirt 0.8 0 0 4 | 2 Stone 0.8 0 0 5 | 3 Brick 0.8 0 0 6 | 4 Brick_Red 0.8 0 0 7 | 5 Rock 0.8 0 0 8 | 6 Icy_Rock 0.6 0 0 9 | 7 Spikes 1.0 0 1 10 | 8 Ice 0.25 0 0 -------------------------------------------------------------------------------- /chapter_07/SharedContext.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "TextureManager.h" 5 | #include "EntityManager.h" 6 | #include "DebugOverlay.h" 7 | 8 | class Map; 9 | struct SharedContext{ 10 | SharedContext(): 11 | m_wind(nullptr), 12 | m_eventManager(nullptr), 13 | m_textureManager(nullptr), 14 | m_entityManager(nullptr), 15 | m_gameMap(nullptr){} 16 | 17 | Window* m_wind; 18 | EventManager* m_eventManager; 19 | TextureManager* m_textureManager; 20 | EntityManager* m_entityManager; 21 | Map* m_gameMap; 22 | DebugOverlay m_debugOverlay; 23 | }; -------------------------------------------------------------------------------- /chapter_07/State_Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "Map.h" 4 | #include "EventManager.h" 5 | 6 | class State_Game : public BaseState{ 7 | public: 8 | State_Game(StateManager* l_stateManager); 9 | ~State_Game(); 10 | 11 | void OnCreate(); 12 | void OnDestroy(); 13 | 14 | void Activate(); 15 | void Deactivate(); 16 | 17 | void Update(const sf::Time& l_time); 18 | void Draw(); 19 | 20 | void MainMenu(EventDetails* l_details); 21 | void Pause(EventDetails* l_details); 22 | void ToggleOverlay(EventDetails* l_details); 23 | private: 24 | Map* m_gameMap; 25 | }; -------------------------------------------------------------------------------- /chapter_07/State_GameOver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include 4 | 5 | class State_GameOver : public BaseState{ 6 | public: 7 | State_GameOver(StateManager* l_stateManager); 8 | ~State_GameOver(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | private: 19 | sf::Font m_font; 20 | sf::Text m_text; 21 | float m_elapsed; 22 | }; -------------------------------------------------------------------------------- /chapter_07/State_Intro.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Intro : public BaseState{ 6 | public: 7 | State_Intro(StateManager* l_stateManager); 8 | ~State_Intro(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Continue(EventDetails* l_details); 20 | private: 21 | sf::Sprite m_introSprite; 22 | sf::Font m_font; 23 | sf::Text m_text; 24 | }; -------------------------------------------------------------------------------- /chapter_07/State_MainMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_MainMenu : public BaseState{ 6 | public: 7 | State_MainMenu(StateManager* l_stateManager); 8 | ~State_MainMenu(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void MouseClick(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | 24 | sf::Vector2f m_buttonSize; 25 | sf::Vector2f m_buttonPos; 26 | unsigned int m_buttonPadding; 27 | 28 | sf::RectangleShape m_rects[3]; 29 | sf::Text m_labels[3]; 30 | }; -------------------------------------------------------------------------------- /chapter_07/State_Paused.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Paused : public BaseState{ 6 | public: 7 | State_Paused(StateManager* l_stateManager); 8 | ~State_Paused(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Unpause(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | sf::RectangleShape m_rect; 24 | }; -------------------------------------------------------------------------------- /chapter_07/TextureManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class TextureManager: public ResourceManager{ 6 | public: 7 | TextureManager(): ResourceManager("textures.cfg"){} 8 | 9 | sf::Texture* Load(const std::string& l_path){ 10 | sf::Texture* texture = new sf::Texture(); 11 | if(!texture->loadFromFile( 12 | Utils::GetResourceDirectory() + l_path)) 13 | { 14 | delete texture; 15 | texture = nullptr; 16 | std::cerr << "! Failed to load texture: " << l_path << std::endl; 17 | } 18 | return texture; 19 | } 20 | }; -------------------------------------------------------------------------------- /chapter_07/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Anim_Base.cpp Enemy.cpp EventManager.cpp Map.cpp State_Game.cpp State_MainMenu.cpp Window.cpp Anim_Directional.cpp EntityBase.cpp Game.cpp Player.cpp State_GameOver.cpp StateManager.cpp Character.cpp EntityManager.cpp Main.cpp SpriteSheet.cpp State_Intro.cpp State_Paused.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_08/Anim_Directional.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Anim_Base.h" 3 | #include "Directions.h" 4 | 5 | class Anim_Directional : public Anim_Base{ 6 | protected: 7 | void FrameStep(); 8 | void CropSprite(); 9 | void ReadIn(std::stringstream& l_stream); 10 | }; -------------------------------------------------------------------------------- /chapter_08/Assets/keys.cfg: -------------------------------------------------------------------------------- 1 | Window_close 0:0 2 | Fullscreen_toggle 5:89 3 | Intro_Continue 5:57 4 | Mouse_Left 9:0 5 | Key_Escape 5:36 6 | Key_P 5:15 7 | Key_T 5:19 8 | Key_K 5:10 9 | Key_O 5:14 10 | Key_X 5:23 11 | Player_MoveLeft 24:0 12 | Player_MoveRight 24:3 13 | Player_MoveUp 24:22 14 | Player_MoveDown 24:18 15 | Player_Attack 24:58 -------------------------------------------------------------------------------- /chapter_08/Assets/media/Entities/Player.entity: -------------------------------------------------------------------------------- 1 | Name Player 2 | Attributes 63 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 1 5 | Component 1 Player 6 | Component 2 0 7 | Component 3 128.0 1024.0 1024.0 1 8 | Component 4 9 | Component 5 20.0 20.0 0.0 0.0 2 -------------------------------------------------------------------------------- /chapter_08/Assets/media/Entities/Skeleton.entity: -------------------------------------------------------------------------------- 1 | Name Skeleton 2 | Attributes 63 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 0 5 | Component 1 Skeleton 6 | Component 2 0 7 | Component 3 128.0 1024.0 1024.0 1 8 | Component 4 9 | Component 5 20.0 20.0 0.0 0.0 2 -------------------------------------------------------------------------------- /chapter_08/Assets/media/Fonts/arial.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_08/Assets/media/Fonts/arial.ttf -------------------------------------------------------------------------------- /chapter_08/Assets/media/Spritesheets/Player.sheet: -------------------------------------------------------------------------------- 1 | Texture PlayerSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 0 0 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_08/Assets/media/Spritesheets/Skeleton.sheet: -------------------------------------------------------------------------------- 1 | Texture SkeletonSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 8 8 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_08/Assets/media/Textures/PlayerSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_08/Assets/media/Textures/PlayerSheet.png -------------------------------------------------------------------------------- /chapter_08/Assets/media/Textures/SkeletonSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_08/Assets/media/Textures/SkeletonSheet.png -------------------------------------------------------------------------------- /chapter_08/Assets/media/Textures/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_08/Assets/media/Textures/intro.png -------------------------------------------------------------------------------- /chapter_08/Assets/media/Textures/tilesheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_08/Assets/media/Textures/tilesheet.png -------------------------------------------------------------------------------- /chapter_08/Assets/textures.cfg: -------------------------------------------------------------------------------- 1 | Intro media/Textures/intro.png 2 | PlayerSprite media/Textures/PlayerSheet.png 3 | SkeletonSprite media/Textures/SkeletonSheet.png 4 | TileSheet media/Textures/tilesheet.png -------------------------------------------------------------------------------- /chapter_08/C_Base.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "ECS_Types.h" 5 | 6 | class C_Base{ 7 | public: 8 | C_Base(const Component& l_type): m_type(l_type){} 9 | virtual ~C_Base(){} 10 | 11 | Component GetType(){ return m_type; } 12 | 13 | friend std::stringstream& operator >>( 14 | std::stringstream& l_stream, C_Base& b) 15 | { 16 | b.ReadIn(l_stream); 17 | return l_stream; 18 | } 19 | 20 | virtual void ReadIn(std::stringstream& l_stream) = 0; 21 | protected: 22 | Component m_type; 23 | }; -------------------------------------------------------------------------------- /chapter_08/C_Drawable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | #include 4 | 5 | class C_Drawable : public C_Base{ 6 | public: 7 | C_Drawable(const Component& l_type) : C_Base(l_type){} 8 | virtual ~C_Drawable(){} 9 | 10 | virtual void UpdatePosition(const sf::Vector2f& l_vec) = 0; 11 | virtual const sf::Vector2u& GetSize() = 0; 12 | virtual void Draw(sf::RenderWindow* l_wind) = 0; 13 | private: 14 | 15 | }; -------------------------------------------------------------------------------- /chapter_08/C_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | enum class EntityState{ Idle, Walking, Attacking, Hurt, Dying }; 5 | 6 | class C_State : public C_Base{ 7 | public: 8 | C_State(): C_Base(Component::State){} 9 | void ReadIn(std::stringstream& l_stream){ 10 | unsigned int state = 0; 11 | l_stream >> state; 12 | m_state = (EntityState)state; 13 | } 14 | 15 | EntityState GetState(){ return m_state; } 16 | void SetState(const EntityState& l_state){ 17 | m_state = l_state; 18 | } 19 | private: 20 | EntityState m_state; 21 | }; -------------------------------------------------------------------------------- /chapter_08/DebugOverlay.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | class DebugOverlay{ 6 | public: 7 | DebugOverlay(){ 8 | m_debug = false; 9 | } 10 | 11 | ~DebugOverlay(){ 12 | while(m_drawables.begin() != m_drawables.end()){ 13 | delete m_drawables.back(); 14 | m_drawables.pop_back(); 15 | } 16 | } 17 | 18 | void Add(sf::Drawable* l_drawable){ 19 | m_drawables.push_back(l_drawable); 20 | } 21 | 22 | void Draw(sf::RenderWindow* l_wind){ 23 | while(m_drawables.begin() != m_drawables.end()){ 24 | l_wind->draw(*m_drawables.back()); 25 | delete m_drawables.back(); 26 | m_drawables.pop_back(); 27 | } 28 | } 29 | 30 | bool Debug(){ return m_debug; } 31 | void SetDebug(const bool& l_val){ m_debug = l_val; } 32 | private: 33 | std::vector m_drawables; 34 | bool m_debug; 35 | }; -------------------------------------------------------------------------------- /chapter_08/Directions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | enum class Direction{ Up = 0, Left, Down, Right }; -------------------------------------------------------------------------------- /chapter_08/ECS_Types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using ComponentType = unsigned int; 3 | #define N_COMPONENT_TYPES 32 4 | 5 | enum class Component{ 6 | Position = 0, SpriteSheet, State, Movable, Controller, Collidable 7 | }; 8 | 9 | enum class System{ 10 | Renderer = 0, Movement, Collision, Control, State, SheetAnimation 11 | }; -------------------------------------------------------------------------------- /chapter_08/EntityEvents.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityEvent{ 4 | Spawned, Despawned, Colliding_X, Colliding_Y, 5 | Moving_Left, Moving_Right, Moving_Up, Moving_Down, 6 | Elevation_Change, Became_Idle, Began_Moving 7 | }; -------------------------------------------------------------------------------- /chapter_08/EntityMessages.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityMessage{ 4 | Move, Is_Moving, State_Changed, Direction_Changed, 5 | Switch_State, Attack_Action, Dead 6 | }; -------------------------------------------------------------------------------- /chapter_08/Event_Queue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | using EventID = unsigned int; 5 | 6 | class EventQueue{ 7 | public: 8 | void AddEvent(const EventID& l_event){m_queue.push(l_event);} 9 | 10 | bool ProcessEvents(EventID& l_id){ 11 | if (m_queue.empty()){ return false; } 12 | l_id = m_queue.front(); 13 | m_queue.pop(); 14 | return true; 15 | } 16 | 17 | void Clear(){ while(!m_queue.empty()){ m_queue.pop(); }} 18 | private: 19 | std::queue m_queue; 20 | }; -------------------------------------------------------------------------------- /chapter_08/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "StateManager.h" 5 | #include "TextureManager.h" 6 | #include 7 | 8 | class Game{ 9 | public: 10 | Game(); 11 | ~Game(); 12 | 13 | void Update(); 14 | void Render(); 15 | void LateUpdate(); 16 | 17 | sf::Time GetElapsed(); 18 | 19 | Window* GetWindow(); 20 | private: 21 | void RestartClock(); 22 | 23 | sf::Clock m_clock; 24 | sf::Time m_elapsed; 25 | SharedContext m_context; 26 | Window m_window; 27 | TextureManager m_textureManager; 28 | SystemManager m_systemManager; 29 | EntityManager m_entityManager; 30 | StateManager m_stateManager; 31 | }; -------------------------------------------------------------------------------- /chapter_08/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | int main(int argc, char* argv[]){ 4 | // Program entry point. 5 | { 6 | Game game; 7 | while(!game.GetWindow()->IsDone()){ 8 | game.Update(); 9 | game.Render(); 10 | game.LateUpdate(); 11 | } 12 | } 13 | system("PAUSE"); 14 | } -------------------------------------------------------------------------------- /chapter_08/Message.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using MessageType = unsigned int; 3 | struct TwoFloats{ float m_x; float m_y; }; 4 | 5 | struct Message{ 6 | Message(const MessageType& l_type) : m_type(l_type){} 7 | 8 | MessageType m_type; 9 | int m_sender; 10 | int m_receiver; 11 | 12 | union{ 13 | TwoFloats m_2f; 14 | bool m_bool; 15 | int m_int; 16 | }; 17 | }; -------------------------------------------------------------------------------- /chapter_08/MessageHandler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Communicator.h" 3 | #include "EntityMessages.h" 4 | #include 5 | 6 | using Subscribtions = std::unordered_map; 7 | 8 | class MessageHandler{ 9 | public: 10 | bool Subscribe(const EntityMessage& l_type, Observer* l_observer) 11 | { 12 | return m_communicators[l_type].AddObserver(l_observer); 13 | } 14 | 15 | bool Unsubscribe(const EntityMessage& l_type, Observer* l_observer) 16 | { 17 | return m_communicators[l_type].RemoveObserver(l_observer); 18 | } 19 | 20 | void Dispatch(const Message& l_msg){ 21 | auto itr = m_communicators.find((EntityMessage)l_msg.m_type); 22 | if (itr == m_communicators.end()){ return; } 23 | itr->second.Broadcast(l_msg); 24 | } 25 | private: 26 | Subscribtions m_communicators; 27 | }; -------------------------------------------------------------------------------- /chapter_08/Observer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Message.h" 3 | 4 | class Observer{ 5 | public: 6 | virtual ~Observer(){} 7 | virtual void Notify(const Message& l_message) = 0; 8 | }; -------------------------------------------------------------------------------- /chapter_08/S_Control.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | class S_Control : public S_Base{ 5 | public: 6 | S_Control(SystemManager* l_systemMgr); 7 | ~S_Control(); 8 | 9 | void Update(float l_dT); 10 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 11 | void Notify(const Message& l_message); 12 | private: 13 | void MoveEntity(const EntityId& l_entity, const Direction& l_dir); 14 | }; -------------------------------------------------------------------------------- /chapter_08/S_Movement.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | enum class Axis{ x, y }; 5 | 6 | class Map; 7 | class C_Movable; 8 | 9 | class S_Movement : public S_Base{ 10 | public: 11 | S_Movement(SystemManager* l_systemMgr); 12 | ~S_Movement(); 13 | 14 | void Update(float l_dT); 15 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 16 | void Notify(const Message& l_message); 17 | 18 | void SetMap(Map* l_gameMap); 19 | private: 20 | void StopEntity(const EntityId& l_entity, const Axis& l_axis); 21 | void SetDirection(const EntityId& l_entity, const Direction& l_dir); 22 | const sf::Vector2f& GetTileFriction(unsigned int l_elevation, 23 | unsigned int l_x, unsigned int l_y); 24 | void MovementStep(float l_dT, C_Movable* l_movable, C_Position* l_position); 25 | 26 | Map* m_gameMap; 27 | }; -------------------------------------------------------------------------------- /chapter_08/S_Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include 4 | #include 5 | #include "Window.h" 6 | 7 | class S_Renderer : public S_Base{ 8 | public: 9 | S_Renderer(SystemManager* l_systemMgr); 10 | ~S_Renderer(); 11 | 12 | void Update(float l_dT); 13 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 14 | void Notify(const Message& l_message); 15 | void Render(Window* l_wind, unsigned int l_layer); 16 | private: 17 | void SetSheetDirection(const EntityId& l_entity, const Direction& l_dir); 18 | void SortDrawables(); 19 | }; -------------------------------------------------------------------------------- /chapter_08/S_SheetAnimation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | class S_SheetAnimation : public S_Base{ 5 | public: 6 | S_SheetAnimation(SystemManager* l_systemMgr); 7 | ~S_SheetAnimation(); 8 | 9 | void Update(float l_dT); 10 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 11 | void Notify(const Message& l_message); 12 | private: 13 | void ChangeAnimation(const EntityId& l_entity, 14 | const std::string& l_anim, bool l_play, bool l_loop); 15 | }; -------------------------------------------------------------------------------- /chapter_08/S_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "C_State.h" 4 | 5 | class S_State : public S_Base{ 6 | public: 7 | S_State(SystemManager* l_systemMgr); 8 | ~S_State(); 9 | 10 | void Update(float l_dT); 11 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 12 | void Notify(const Message& l_message); 13 | private: 14 | void ChangeState(const EntityId& l_entity, const EntityState& l_state, 15 | const bool& l_force); 16 | }; -------------------------------------------------------------------------------- /chapter_08/SharedContext.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "TextureManager.h" 5 | #include "System_Manager.h" 6 | #include "Entity_Manager.h" 7 | #include "DebugOverlay.h" 8 | 9 | class Map; 10 | 11 | struct SharedContext{ 12 | SharedContext(): 13 | m_wind(nullptr), 14 | m_eventManager(nullptr), 15 | m_textureManager(nullptr), 16 | m_systemManager(nullptr), 17 | m_entityManager(nullptr), 18 | m_gameMap(nullptr){} 19 | 20 | Window* m_wind; 21 | EventManager* m_eventManager; 22 | TextureManager* m_textureManager; 23 | SystemManager* m_systemManager; 24 | EntityManager* m_entityManager; 25 | Map* m_gameMap; 26 | DebugOverlay m_debugOverlay; 27 | }; -------------------------------------------------------------------------------- /chapter_08/State_Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "Map.h" 4 | #include "EventManager.h" 5 | 6 | class State_Game : public BaseState{ 7 | public: 8 | State_Game(StateManager* l_stateManager); 9 | ~State_Game(); 10 | 11 | void OnCreate(); 12 | void OnDestroy(); 13 | 14 | void Activate(); 15 | void Deactivate(); 16 | 17 | void Update(const sf::Time& l_time); 18 | void Draw(); 19 | 20 | void MainMenu(EventDetails* l_details); 21 | void Pause(EventDetails* l_details); 22 | void PlayerMove(EventDetails* l_details); 23 | 24 | // Debug: 25 | void ToggleOverlay(EventDetails* l_details); 26 | private: 27 | void UpdateCamera(); 28 | 29 | Map* m_gameMap; 30 | int m_player; 31 | }; -------------------------------------------------------------------------------- /chapter_08/State_GameOver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_GameOver : public BaseState{ 6 | public: 7 | State_GameOver(StateManager* l_stateManager); 8 | ~State_GameOver(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | private: 19 | sf::Text m_text; 20 | float m_elapsed; 21 | }; -------------------------------------------------------------------------------- /chapter_08/State_Intro.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Intro : public BaseState{ 6 | public: 7 | State_Intro(StateManager* l_stateManager); 8 | ~State_Intro(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Continue(EventDetails* l_details); 20 | private: 21 | sf::Sprite m_introSprite; 22 | sf::Text m_text; 23 | }; -------------------------------------------------------------------------------- /chapter_08/State_MainMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_MainMenu : public BaseState{ 6 | public: 7 | State_MainMenu(StateManager* l_stateManager); 8 | ~State_MainMenu(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void MouseClick(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | 24 | sf::Vector2f m_buttonSize; 25 | sf::Vector2f m_buttonPos; 26 | unsigned int m_buttonPadding; 27 | 28 | sf::RectangleShape m_rects[3]; 29 | sf::Text m_labels[3]; 30 | }; -------------------------------------------------------------------------------- /chapter_08/State_Paused.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Paused : public BaseState{ 6 | public: 7 | State_Paused(StateManager* l_stateManager); 8 | ~State_Paused(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Unpause(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | sf::RectangleShape m_rect; 24 | }; -------------------------------------------------------------------------------- /chapter_08/TextureManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class TextureManager: public ResourceManager{ 6 | public: 7 | TextureManager(): ResourceManager("textures.cfg"){} 8 | 9 | sf::Texture* Load(const std::string& l_path){ 10 | sf::Texture* texture = new sf::Texture(); 11 | if(!texture->loadFromFile( 12 | Utils::GetResourceDirectory() + l_path)) 13 | { 14 | delete texture; 15 | texture = nullptr; 16 | std::cerr << "! Failed to load texture: " << l_path << std::endl; 17 | } 18 | return texture; 19 | } 20 | }; -------------------------------------------------------------------------------- /chapter_08/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Anim_Base.cpp EventManager.cpp Map.cpp State_Intro.cpp State_Paused.cpp Anim_Directional.cpp Game.cpp S_Base.cpp S_Collision.cpp S_Control.cpp S_Movement.cpp S_Renderer.cpp S_SheetAnimation.cpp S_State.cpp State_Game.cpp State_MainMenu.cpp System_Manager.cpp Entity_Manager.cpp Main.cpp SpriteSheet.cpp State_GameOver.cpp StateManager.cpp Window.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_09/Anim_Directional.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Anim_Base.h" 3 | #include "Directions.h" 4 | 5 | class Anim_Directional : public Anim_Base{ 6 | protected: 7 | void FrameStep(); 8 | void CropSprite(); 9 | void ReadIn(std::stringstream& l_stream); 10 | }; -------------------------------------------------------------------------------- /chapter_09/Assets/keys.cfg: -------------------------------------------------------------------------------- 1 | Window_close 0:0 2 | Fullscreen_toggle 5:89 3 | Intro_Continue 5:57 4 | Mouse_Left 9:0 5 | Key_Escape 5:36 6 | Key_P 5:15 7 | Key_T 5:19 8 | Key_K 5:10 9 | Key_O 5:14 10 | Key_X 5:23 11 | Player_MoveLeft 24:0 12 | Player_MoveRight 24:3 13 | Player_MoveUp 24:22 14 | Player_MoveDown 24:18 15 | Player_Attack 24:58 -------------------------------------------------------------------------------- /chapter_09/Assets/media/Entities/Player.entity: -------------------------------------------------------------------------------- 1 | Name Player 2 | Attributes 63 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 1 5 | Component 1 Player 6 | Component 2 0 7 | Component 3 128.0 1024.0 1024.0 1 8 | Component 4 9 | Component 5 20.0 20.0 0.0 0.0 2 -------------------------------------------------------------------------------- /chapter_09/Assets/media/Entities/Skeleton.entity: -------------------------------------------------------------------------------- 1 | Name Skeleton 2 | Attributes 63 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 0 5 | Component 1 Skeleton 6 | Component 2 0 7 | Component 3 128.0 1024.0 1024.0 1 8 | Component 4 9 | Component 5 20.0 20.0 0.0 0.0 2 -------------------------------------------------------------------------------- /chapter_09/Assets/media/Fonts/arial.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_09/Assets/media/Fonts/arial.ttf -------------------------------------------------------------------------------- /chapter_09/Assets/media/Spritesheets/Player.sheet: -------------------------------------------------------------------------------- 1 | Texture PlayerSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 0 0 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_09/Assets/media/Spritesheets/Skeleton.sheet: -------------------------------------------------------------------------------- 1 | Texture SkeletonSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 8 8 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_09/Assets/media/Textures/PlayerSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_09/Assets/media/Textures/PlayerSheet.png -------------------------------------------------------------------------------- /chapter_09/Assets/media/Textures/SkeletonSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_09/Assets/media/Textures/SkeletonSheet.png -------------------------------------------------------------------------------- /chapter_09/Assets/media/Textures/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_09/Assets/media/Textures/intro.png -------------------------------------------------------------------------------- /chapter_09/Assets/media/Textures/tilesheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_09/Assets/media/Textures/tilesheet.png -------------------------------------------------------------------------------- /chapter_09/Assets/textures.cfg: -------------------------------------------------------------------------------- 1 | Intro media/Textures/intro.png 2 | PlayerSprite media/Textures/PlayerSheet.png 3 | SkeletonSprite media/Textures/SkeletonSheet.png 4 | TileSheet media/Textures/tilesheet.png -------------------------------------------------------------------------------- /chapter_09/C_Base.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "ECS_Types.h" 5 | 6 | class C_Base{ 7 | public: 8 | C_Base(const Component& l_type): m_type(l_type){} 9 | virtual ~C_Base(){} 10 | 11 | Component GetType(){ return m_type; } 12 | 13 | friend std::stringstream& operator >>( 14 | std::stringstream& l_stream, C_Base& b) 15 | { 16 | b.ReadIn(l_stream); 17 | return l_stream; 18 | } 19 | 20 | virtual void ReadIn(std::stringstream& l_stream) = 0; 21 | protected: 22 | Component m_type; 23 | }; -------------------------------------------------------------------------------- /chapter_09/C_Controller.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | class C_Controller : public C_Base{ 5 | public: 6 | C_Controller() : C_Base(Component::Controller){} 7 | void ReadIn(std::stringstream& l_stream){} 8 | private: 9 | 10 | }; -------------------------------------------------------------------------------- /chapter_09/C_Drawable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | #include 4 | 5 | class C_Drawable : public C_Base{ 6 | public: 7 | C_Drawable(const Component& l_type) : C_Base(l_type){} 8 | virtual ~C_Drawable(){} 9 | 10 | virtual void UpdatePosition(const sf::Vector2f& l_vec) = 0; 11 | virtual const sf::Vector2u& GetSize() = 0; 12 | virtual void Draw(sf::RenderWindow* l_wind) = 0; 13 | private: 14 | 15 | }; -------------------------------------------------------------------------------- /chapter_09/C_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | enum class EntityState{ Idle, Walking, Attacking, Hurt, Dying }; 5 | 6 | class C_State : public C_Base{ 7 | public: 8 | C_State(): C_Base(Component::State){} 9 | void ReadIn(std::stringstream& l_stream){ 10 | unsigned int state = 0; 11 | l_stream >> state; 12 | m_state = (EntityState)state; 13 | } 14 | 15 | EntityState GetState(){ return m_state; } 16 | void SetState(const EntityState& l_state){ 17 | m_state = l_state; 18 | } 19 | private: 20 | EntityState m_state; 21 | }; -------------------------------------------------------------------------------- /chapter_09/DebugOverlay.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | class DebugOverlay{ 6 | public: 7 | DebugOverlay(){ 8 | m_debug = false; 9 | } 10 | 11 | ~DebugOverlay(){ 12 | while(m_drawables.begin() != m_drawables.end()){ 13 | delete m_drawables.back(); 14 | m_drawables.pop_back(); 15 | } 16 | } 17 | 18 | void Add(sf::Drawable* l_drawable){ 19 | m_drawables.push_back(l_drawable); 20 | } 21 | 22 | void Draw(sf::RenderWindow* l_wind){ 23 | while(m_drawables.begin() != m_drawables.end()){ 24 | l_wind->draw(*m_drawables.back()); 25 | delete m_drawables.back(); 26 | m_drawables.pop_back(); 27 | } 28 | } 29 | 30 | bool Debug(){ return m_debug; } 31 | void SetDebug(const bool& l_val){ m_debug = l_val; } 32 | private: 33 | std::vector m_drawables; 34 | bool m_debug; 35 | }; -------------------------------------------------------------------------------- /chapter_09/Directions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | enum class Direction{ Up = 0, Left, Down, Right }; -------------------------------------------------------------------------------- /chapter_09/ECS_Types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using ComponentType = unsigned int; 3 | #define N_COMPONENT_TYPES 32 4 | 5 | enum class Component{ 6 | Position = 0, SpriteSheet, State, Movable, Controller, Collidable 7 | }; 8 | 9 | enum class System{ 10 | Renderer = 0, Movement, Collision, Control, State, SheetAnimation 11 | }; -------------------------------------------------------------------------------- /chapter_09/EntityEvents.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityEvent{ 4 | Spawned, Despawned, Colliding_X, Colliding_Y, 5 | Moving_Left, Moving_Right, Moving_Up, Moving_Down, 6 | Elevation_Change, Became_Idle, Began_Moving 7 | }; -------------------------------------------------------------------------------- /chapter_09/EntityMessages.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityMessage{ 4 | Move, Is_Moving, State_Changed, Direction_Changed, 5 | Switch_State, Attack_Action, Dead 6 | }; -------------------------------------------------------------------------------- /chapter_09/Event_Queue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | using EventID = unsigned int; 5 | 6 | class EventQueue{ 7 | public: 8 | void AddEvent(const EventID& l_event){m_queue.push(l_event);} 9 | 10 | bool ProcessEvents(EventID& l_id){ 11 | if (m_queue.empty()){ return false; } 12 | l_id = m_queue.front(); 13 | m_queue.pop(); 14 | return true; 15 | } 16 | 17 | void Clear(){ while(!m_queue.empty()){ m_queue.pop(); }} 18 | private: 19 | std::queue m_queue; 20 | }; -------------------------------------------------------------------------------- /chapter_09/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "StateManager.h" 5 | #include "TextureManager.h" 6 | #include 7 | 8 | class Game{ 9 | public: 10 | Game(); 11 | ~Game(); 12 | 13 | void Update(); 14 | void Render(); 15 | void LateUpdate(); 16 | 17 | sf::Time GetElapsed(); 18 | 19 | Window* GetWindow(); 20 | private: 21 | void RestartClock(); 22 | 23 | sf::Clock m_clock; 24 | sf::Time m_elapsed; 25 | SharedContext m_context; 26 | Window m_window; 27 | TextureManager m_textureManager; 28 | SystemManager m_systemManager; 29 | EntityManager m_entityManager; 30 | StateManager m_stateManager; 31 | }; -------------------------------------------------------------------------------- /chapter_09/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | int main(int argc, char* argv[]){ 4 | // Program entry point. 5 | { 6 | Game game; 7 | while(!game.GetWindow()->IsDone()){ 8 | game.Update(); 9 | game.Render(); 10 | game.LateUpdate(); 11 | } 12 | } 13 | system("PAUSE"); 14 | } -------------------------------------------------------------------------------- /chapter_09/Message.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using MessageType = unsigned int; 3 | struct TwoFloats{ float m_x; float m_y; }; 4 | 5 | struct Message{ 6 | Message(const MessageType& l_type) : m_type(l_type){} 7 | 8 | MessageType m_type; 9 | int m_sender; 10 | int m_receiver; 11 | 12 | union{ 13 | TwoFloats m_2f; 14 | bool m_bool; 15 | int m_int; 16 | }; 17 | }; -------------------------------------------------------------------------------- /chapter_09/MessageHandler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Communicator.h" 3 | #include "EntityMessages.h" 4 | #include 5 | 6 | using Subscribtions = std::unordered_map; 7 | 8 | class MessageHandler{ 9 | public: 10 | bool Subscribe(const EntityMessage& l_type, Observer* l_observer) 11 | { 12 | return m_communicators[l_type].AddObserver(l_observer); 13 | } 14 | 15 | bool Unsubscribe(const EntityMessage& l_type, Observer* l_observer) 16 | { 17 | return m_communicators[l_type].RemoveObserver(l_observer); 18 | } 19 | 20 | void Dispatch(const Message& l_msg){ 21 | auto itr = m_communicators.find((EntityMessage)l_msg.m_type); 22 | if (itr == m_communicators.end()){ return; } 23 | itr->second.Broadcast(l_msg); 24 | } 25 | private: 26 | Subscribtions m_communicators; 27 | }; -------------------------------------------------------------------------------- /chapter_09/Observer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Message.h" 3 | 4 | class Observer{ 5 | public: 6 | virtual ~Observer(){} 7 | virtual void Notify(const Message& l_message) = 0; 8 | }; -------------------------------------------------------------------------------- /chapter_09/S_Control.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | class S_Control : public S_Base{ 5 | public: 6 | S_Control(SystemManager* l_systemMgr); 7 | ~S_Control(); 8 | 9 | void Update(float l_dT); 10 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 11 | void Notify(const Message& l_message); 12 | private: 13 | void MoveEntity(const EntityId& l_entity, const Direction& l_dir); 14 | }; -------------------------------------------------------------------------------- /chapter_09/S_Movement.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | enum class Axis{ x, y }; 5 | 6 | class Map; 7 | 8 | class S_Movement : public S_Base{ 9 | public: 10 | S_Movement(SystemManager* l_systemMgr); 11 | ~S_Movement(); 12 | 13 | void Update(float l_dT); 14 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 15 | void Notify(const Message& l_message); 16 | 17 | void SetMap(Map* l_gameMap); 18 | private: 19 | void StopEntity(const EntityId& l_entity, const Axis& l_axis); 20 | void SetDirection(const EntityId& l_entity, const Direction& l_dir); 21 | const sf::Vector2f& GetTileFriction(unsigned int l_elevation, 22 | unsigned int l_x, unsigned int l_y); 23 | void MovementStep(float l_dT, C_Movable* l_movable, C_Position* l_position); 24 | 25 | Map* m_gameMap; 26 | }; -------------------------------------------------------------------------------- /chapter_09/S_Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include 4 | #include 5 | #include "Window.h" 6 | 7 | class S_Renderer : public S_Base{ 8 | public: 9 | S_Renderer(SystemManager* l_systemMgr); 10 | ~S_Renderer(); 11 | 12 | void Update(float l_dT); 13 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 14 | void Notify(const Message& l_message); 15 | void Render(Window* l_wind, unsigned int l_layer); 16 | private: 17 | void SetSheetDirection(const EntityId& l_entity, const Direction& l_dir); 18 | void SortDrawables(); 19 | }; -------------------------------------------------------------------------------- /chapter_09/S_SheetAnimation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | class S_SheetAnimation : public S_Base{ 5 | public: 6 | S_SheetAnimation(SystemManager* l_systemMgr); 7 | ~S_SheetAnimation(); 8 | 9 | void Update(float l_dT); 10 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 11 | void Notify(const Message& l_message); 12 | private: 13 | void ChangeAnimation(const EntityId& l_entity, 14 | const std::string& l_anim, bool l_play, bool l_loop); 15 | }; -------------------------------------------------------------------------------- /chapter_09/S_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "C_State.h" 4 | 5 | class S_State : public S_Base{ 6 | public: 7 | S_State(SystemManager* l_systemMgr); 8 | ~S_State(); 9 | 10 | void Update(float l_dT); 11 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 12 | void Notify(const Message& l_message); 13 | private: 14 | void ChangeState(const EntityId& l_entity, const EntityState& l_state, 15 | const bool& l_force); 16 | }; -------------------------------------------------------------------------------- /chapter_09/SharedContext.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "TextureManager.h" 5 | #include "System_Manager.h" 6 | #include "Entity_Manager.h" 7 | #include "DebugOverlay.h" 8 | 9 | class Map; 10 | 11 | struct SharedContext{ 12 | SharedContext(): 13 | m_wind(nullptr), 14 | m_eventManager(nullptr), 15 | m_textureManager(nullptr), 16 | m_systemManager(nullptr), 17 | m_entityManager(nullptr), 18 | m_gameMap(nullptr){} 19 | 20 | Window* m_wind; 21 | EventManager* m_eventManager; 22 | TextureManager* m_textureManager; 23 | SystemManager* m_systemManager; 24 | EntityManager* m_entityManager; 25 | Map* m_gameMap; 26 | DebugOverlay m_debugOverlay; 27 | }; -------------------------------------------------------------------------------- /chapter_09/State_Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "Map.h" 4 | #include "EventManager.h" 5 | 6 | class State_Game : public BaseState{ 7 | public: 8 | State_Game(StateManager* l_stateManager); 9 | ~State_Game(); 10 | 11 | void OnCreate(); 12 | void OnDestroy(); 13 | 14 | void Activate(); 15 | void Deactivate(); 16 | 17 | void Update(const sf::Time& l_time); 18 | void Draw(); 19 | 20 | void MainMenu(EventDetails* l_details); 21 | void Pause(EventDetails* l_details); 22 | void PlayerMove(EventDetails* l_details); 23 | 24 | // Debug: 25 | void ToggleOverlay(EventDetails* l_details); 26 | private: 27 | void UpdateCamera(); 28 | 29 | Map* m_gameMap; 30 | int m_player; 31 | }; -------------------------------------------------------------------------------- /chapter_09/State_GameOver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_GameOver : public BaseState{ 6 | public: 7 | State_GameOver(StateManager* l_stateManager); 8 | ~State_GameOver(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | private: 19 | sf::Text m_text; 20 | float m_elapsed; 21 | }; -------------------------------------------------------------------------------- /chapter_09/State_Intro.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Intro : public BaseState{ 6 | public: 7 | State_Intro(StateManager* l_stateManager); 8 | ~State_Intro(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Continue(EventDetails* l_details); 20 | private: 21 | sf::Sprite m_introSprite; 22 | sf::Text m_text; 23 | }; -------------------------------------------------------------------------------- /chapter_09/State_MainMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_MainMenu : public BaseState{ 6 | public: 7 | State_MainMenu(StateManager* l_stateManager); 8 | ~State_MainMenu(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void MouseClick(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | 24 | sf::Vector2f m_buttonSize; 25 | sf::Vector2f m_buttonPos; 26 | unsigned int m_buttonPadding; 27 | 28 | sf::RectangleShape m_rects[3]; 29 | sf::Text m_labels[3]; 30 | }; -------------------------------------------------------------------------------- /chapter_09/State_Paused.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Paused : public BaseState{ 6 | public: 7 | State_Paused(StateManager* l_stateManager); 8 | ~State_Paused(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Unpause(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | sf::RectangleShape m_rect; 24 | }; -------------------------------------------------------------------------------- /chapter_09/TextureManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class TextureManager: public ResourceManager{ 6 | public: 7 | TextureManager(): ResourceManager("textures.cfg"){} 8 | 9 | sf::Texture* Load(const std::string& l_path){ 10 | sf::Texture* texture = new sf::Texture(); 11 | if(!texture->loadFromFile( 12 | Utils::GetResourceDirectory() + l_path)) 13 | { 14 | delete texture; 15 | texture = nullptr; 16 | std::cerr << "! Failed to load texture: " << l_path << std::endl; 17 | } 18 | return texture; 19 | } 20 | }; -------------------------------------------------------------------------------- /chapter_09/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Anim_Base.cpp EventManager.cpp Map.cpp S_Control.cpp S_Renderer.cpp State_Game.cpp State_Intro.cpp State_Paused.cpp State_MainMenu.cpp System_Manager.cpp Anim_Directional.cpp Game.cpp S_Base.cpp S_Movement.cpp S_SheetAnimation.cpp State_GameOver.cpp StateManager.cpp Window.cpp Entity_Manager.cpp Main.cpp S_Collision.cpp SpriteSheet.cpp S_State.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_10/Anim_Directional.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Anim_Base.h" 3 | #include "Directions.h" 4 | 5 | class Anim_Directional : public Anim_Base{ 6 | protected: 7 | void FrameStep(); 8 | void CropSprite(); 9 | void ReadIn(std::stringstream& l_stream); 10 | }; -------------------------------------------------------------------------------- /chapter_10/Assets/keys.cfg: -------------------------------------------------------------------------------- 1 | Window_close 0:0 2 | Fullscreen_toggle 5:89 3 | Intro_Continue 5:57 4 | Mouse_Left 9:0 5 | Key_Escape 5:36 6 | Key_P 5:15 7 | Key_T 5:19 8 | Key_K 5:10 9 | Key_O 5:14 10 | Key_X 5:23 11 | Player_MoveLeft 24:0 12 | Player_MoveRight 24:3 13 | Player_MoveUp 24:22 14 | Player_MoveDown 24:18 15 | Player_Attack 24:58 -------------------------------------------------------------------------------- /chapter_10/Assets/media/Entities/Player.entity: -------------------------------------------------------------------------------- 1 | Name Player 2 | Attributes 63 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 1 5 | Component 1 Player 6 | Component 2 0 7 | Component 3 128.0 1024.0 1024.0 1 8 | Component 4 9 | Component 5 20.0 20.0 0.0 0.0 2 -------------------------------------------------------------------------------- /chapter_10/Assets/media/Entities/Skeleton.entity: -------------------------------------------------------------------------------- 1 | Name Skeleton 2 | Attributes 63 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 0 5 | Component 1 Skeleton 6 | Component 2 0 7 | Component 3 128.0 1024.0 1024.0 1 8 | Component 4 9 | Component 5 20.0 20.0 0.0 0.0 2 -------------------------------------------------------------------------------- /chapter_10/Assets/media/Fonts/arial.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_10/Assets/media/Fonts/arial.ttf -------------------------------------------------------------------------------- /chapter_10/Assets/media/Spritesheets/Player.sheet: -------------------------------------------------------------------------------- 1 | Texture PlayerSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 0 0 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_10/Assets/media/Spritesheets/Skeleton.sheet: -------------------------------------------------------------------------------- 1 | Texture SkeletonSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 8 8 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_10/Assets/media/Textures/PlayerSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_10/Assets/media/Textures/PlayerSheet.png -------------------------------------------------------------------------------- /chapter_10/Assets/media/Textures/SkeletonSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_10/Assets/media/Textures/SkeletonSheet.png -------------------------------------------------------------------------------- /chapter_10/Assets/media/Textures/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_10/Assets/media/Textures/intro.png -------------------------------------------------------------------------------- /chapter_10/Assets/media/Textures/tilesheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_10/Assets/media/Textures/tilesheet.png -------------------------------------------------------------------------------- /chapter_10/Assets/textures.cfg: -------------------------------------------------------------------------------- 1 | Intro media/Textures/intro.png 2 | PlayerSprite media/Textures/PlayerSheet.png 3 | SkeletonSprite media/Textures/SkeletonSheet.png 4 | TileSheet media/Textures/tilesheet.png -------------------------------------------------------------------------------- /chapter_10/C_Base.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "ECS_Types.h" 5 | 6 | class C_Base{ 7 | public: 8 | C_Base(const Component& l_type): m_type(l_type){} 9 | virtual ~C_Base(){} 10 | 11 | Component GetType(){ return m_type; } 12 | 13 | friend std::stringstream& operator >>( 14 | std::stringstream& l_stream, C_Base& b) 15 | { 16 | b.ReadIn(l_stream); 17 | return l_stream; 18 | } 19 | 20 | virtual void ReadIn(std::stringstream& l_stream) = 0; 21 | protected: 22 | Component m_type; 23 | }; -------------------------------------------------------------------------------- /chapter_10/C_Controller.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | class C_Controller : public C_Base{ 5 | public: 6 | C_Controller() : C_Base(Component::Controller){} 7 | void ReadIn(std::stringstream& l_stream){} 8 | private: 9 | 10 | }; -------------------------------------------------------------------------------- /chapter_10/C_Drawable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | #include 4 | 5 | class C_Drawable : public C_Base{ 6 | public: 7 | C_Drawable(const Component& l_type) : C_Base(l_type){} 8 | virtual ~C_Drawable(){} 9 | 10 | virtual void UpdatePosition(const sf::Vector2f& l_vec) = 0; 11 | virtual const sf::Vector2u& GetSize() = 0; 12 | virtual void Draw(sf::RenderWindow* l_wind) = 0; 13 | private: 14 | 15 | }; -------------------------------------------------------------------------------- /chapter_10/C_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | enum class EntityState{ Idle, Walking, Attacking, Hurt, Dying }; 5 | 6 | class C_State : public C_Base{ 7 | public: 8 | C_State(): C_Base(Component::State){} 9 | void ReadIn(std::stringstream& l_stream){ 10 | unsigned int state = 0; 11 | l_stream >> state; 12 | m_state = (EntityState)state; 13 | } 14 | 15 | EntityState GetState(){ return m_state; } 16 | void SetState(const EntityState& l_state){ 17 | m_state = l_state; 18 | } 19 | private: 20 | EntityState m_state; 21 | }; -------------------------------------------------------------------------------- /chapter_10/DebugOverlay.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | class DebugOverlay{ 6 | public: 7 | DebugOverlay(){ 8 | m_debug = false; 9 | } 10 | 11 | ~DebugOverlay(){ 12 | while(m_drawables.begin() != m_drawables.end()){ 13 | delete m_drawables.back(); 14 | m_drawables.pop_back(); 15 | } 16 | } 17 | 18 | void Add(sf::Drawable* l_drawable){ 19 | m_drawables.push_back(l_drawable); 20 | } 21 | 22 | void Draw(sf::RenderWindow* l_wind){ 23 | while(m_drawables.begin() != m_drawables.end()){ 24 | l_wind->draw(*m_drawables.back()); 25 | delete m_drawables.back(); 26 | m_drawables.pop_back(); 27 | } 28 | } 29 | 30 | bool Debug(){ return m_debug; } 31 | void SetDebug(const bool& l_val){ m_debug = l_val; } 32 | private: 33 | std::vector m_drawables; 34 | bool m_debug; 35 | }; -------------------------------------------------------------------------------- /chapter_10/Directions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | enum class Direction{ Up = 0, Left, Down, Right }; -------------------------------------------------------------------------------- /chapter_10/ECS_Types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using ComponentType = unsigned int; 3 | #define N_COMPONENT_TYPES 32 4 | 5 | enum class Component{ 6 | Position = 0, SpriteSheet, State, Movable, Controller, Collidable 7 | }; 8 | 9 | enum class System{ 10 | Renderer = 0, Movement, Collision, Control, State, SheetAnimation 11 | }; -------------------------------------------------------------------------------- /chapter_10/EntityEvents.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityEvent{ 4 | Spawned, Despawned, Colliding_X, Colliding_Y, 5 | Moving_Left, Moving_Right, Moving_Up, Moving_Down, 6 | Elevation_Change, Became_Idle, Began_Moving 7 | }; -------------------------------------------------------------------------------- /chapter_10/EntityMessages.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityMessage{ 4 | Move, Is_Moving, State_Changed, Direction_Changed, 5 | Switch_State, Attack_Action, Dead 6 | }; -------------------------------------------------------------------------------- /chapter_10/Event_Queue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | using EventID = unsigned int; 5 | 6 | class EventQueue{ 7 | public: 8 | void AddEvent(const EventID& l_event){m_queue.push(l_event);} 9 | 10 | bool ProcessEvents(EventID& l_id){ 11 | if (m_queue.empty()){ return false; } 12 | l_id = m_queue.front(); 13 | m_queue.pop(); 14 | return true; 15 | } 16 | 17 | void Clear(){ while(!m_queue.empty()){ m_queue.pop(); }} 18 | private: 19 | std::queue m_queue; 20 | }; -------------------------------------------------------------------------------- /chapter_10/FontManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class FontManager : public ResourceManager{ 6 | public: 7 | FontManager() : ResourceManager("fonts.cfg"){} 8 | 9 | sf::Font* Load(const std::string& l_path){ 10 | sf::Font* font = new sf::Font(); 11 | if (!font->loadFromFile(Utils::GetResourceDirectory() + l_path)) 12 | { 13 | delete font; 14 | font = nullptr; 15 | std::cerr << "! Failed to load font: " << l_path << std::endl; 16 | } 17 | return font; 18 | } 19 | }; -------------------------------------------------------------------------------- /chapter_10/GUI_Event.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | enum class GUI_EventType{ None, Click, Release, Hover, Leave }; 5 | 6 | struct ClickCoordinates{ 7 | float x, y; 8 | }; 9 | 10 | struct GUI_Event{ 11 | GUI_EventType m_type; 12 | const char* m_element; 13 | const char* m_interface; 14 | union{ 15 | ClickCoordinates m_clickCoords; 16 | }; 17 | }; -------------------------------------------------------------------------------- /chapter_10/GUI_Label.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "GUI_Element.h" 3 | 4 | class GUI_Label : public GUI_Element{ 5 | public: 6 | GUI_Label(const std::string& l_name, GUI_Interface* l_owner); 7 | ~GUI_Label(); 8 | 9 | void ReadIn(std::stringstream& l_stream); 10 | void OnClick(const sf::Vector2f& l_mousePos); 11 | void OnRelease(); 12 | void OnHover(const sf::Vector2f& l_mousePos); 13 | void OnLeave(); 14 | void Update(float l_dT); 15 | void Draw(sf::RenderTarget* l_target); 16 | }; -------------------------------------------------------------------------------- /chapter_10/GUI_Scrollbar.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "GUI_Element.h" 3 | 4 | enum class SliderType{ Horizontal, Vertical }; 5 | 6 | class GUI_Scrollbar : public GUI_Element{ 7 | public: 8 | GUI_Scrollbar(const std::string& l_name, GUI_Interface* l_owner); 9 | ~GUI_Scrollbar(); 10 | 11 | void SetPosition(const sf::Vector2f& l_pos); 12 | 13 | void ReadIn(std::stringstream& l_stream); 14 | void OnClick(const sf::Vector2f& l_mousePos); 15 | void OnRelease(); 16 | void OnHover(const sf::Vector2f& l_mousePos); 17 | void OnLeave(); 18 | 19 | void ApplyStyle(); 20 | void UpdateStyle(const GUI_ElementState& l_state, const GUI_Style& l_style); 21 | 22 | void Update(float l_dT); 23 | void Draw(sf::RenderTarget* l_target); 24 | private: 25 | SliderType m_sliderType; 26 | sf::RectangleShape m_slider; 27 | sf::Vector2f m_moveMouseLast; 28 | int m_percentage; 29 | }; -------------------------------------------------------------------------------- /chapter_10/GUI_Textfield.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "GUI_Element.h" 3 | 4 | class GUI_Textfield : public GUI_Element{ 5 | public: 6 | GUI_Textfield(const std::string& l_name, GUI_Interface* l_owner); 7 | ~GUI_Textfield(); 8 | 9 | void ReadIn(std::stringstream& l_stream); 10 | void OnClick(const sf::Vector2f& l_mousePos); 11 | void OnRelease(); 12 | void OnHover(const sf::Vector2f& l_mousePos); 13 | void OnLeave(); 14 | void Update(float l_dT); 15 | void Draw(sf::RenderTarget* l_target); 16 | }; -------------------------------------------------------------------------------- /chapter_10/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "StateManager.h" 5 | #include "TextureManager.h" 6 | #include 7 | 8 | class Game{ 9 | public: 10 | Game(); 11 | ~Game(); 12 | 13 | void Update(); 14 | void Render(); 15 | void LateUpdate(); 16 | 17 | sf::Time GetElapsed(); 18 | 19 | Window* GetWindow(); 20 | private: 21 | void RestartClock(); 22 | 23 | sf::Clock m_clock; 24 | sf::Time m_elapsed; 25 | SharedContext m_context; 26 | Window m_window; 27 | TextureManager m_textureManager; 28 | SystemManager m_systemManager; 29 | EntityManager m_entityManager; 30 | StateManager m_stateManager; 31 | }; -------------------------------------------------------------------------------- /chapter_10/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | int main(int argc, char* argv[]){ 4 | // Program entry point. 5 | { 6 | Game game; 7 | while(!game.GetWindow()->IsDone()){ 8 | game.Update(); 9 | game.Render(); 10 | game.LateUpdate(); 11 | } 12 | } 13 | system("PAUSE"); 14 | } -------------------------------------------------------------------------------- /chapter_10/Message.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using MessageType = unsigned int; 3 | struct TwoFloats{ float m_x; float m_y; }; 4 | 5 | struct Message{ 6 | Message(const MessageType& l_type) : m_type(l_type){} 7 | 8 | MessageType m_type; 9 | int m_sender; 10 | int m_receiver; 11 | 12 | union{ 13 | TwoFloats m_2f; 14 | bool m_bool; 15 | int m_int; 16 | }; 17 | }; -------------------------------------------------------------------------------- /chapter_10/MessageHandler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Communicator.h" 3 | #include "EntityMessages.h" 4 | #include 5 | 6 | using Subscribtions = std::unordered_map; 7 | 8 | class MessageHandler{ 9 | public: 10 | bool Subscribe(const EntityMessage& l_type, Observer* l_observer) 11 | { 12 | return m_communicators[l_type].AddObserver(l_observer); 13 | } 14 | 15 | bool Unsubscribe(const EntityMessage& l_type, Observer* l_observer) 16 | { 17 | return m_communicators[l_type].RemoveObserver(l_observer); 18 | } 19 | 20 | void Dispatch(const Message& l_msg){ 21 | auto itr = m_communicators.find((EntityMessage)l_msg.m_type); 22 | if (itr == m_communicators.end()){ return; } 23 | itr->second.Broadcast(l_msg); 24 | } 25 | private: 26 | Subscribtions m_communicators; 27 | }; -------------------------------------------------------------------------------- /chapter_10/Observer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Message.h" 3 | 4 | class Observer{ 5 | public: 6 | virtual ~Observer(){} 7 | virtual void Notify(const Message& l_message) = 0; 8 | }; -------------------------------------------------------------------------------- /chapter_10/S_Control.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | class S_Control : public S_Base{ 5 | public: 6 | S_Control(SystemManager* l_systemMgr); 7 | ~S_Control(); 8 | 9 | void Update(float l_dT); 10 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 11 | void Notify(const Message& l_message); 12 | private: 13 | void MoveEntity(const EntityId& l_entity, const Direction& l_dir); 14 | }; -------------------------------------------------------------------------------- /chapter_10/S_Movement.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | enum class Axis{ x, y }; 5 | 6 | class Map; 7 | 8 | class S_Movement : public S_Base{ 9 | public: 10 | S_Movement(SystemManager* l_systemMgr); 11 | ~S_Movement(); 12 | 13 | void Update(float l_dT); 14 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 15 | void Notify(const Message& l_message); 16 | 17 | void SetMap(Map* l_gameMap); 18 | private: 19 | void StopEntity(const EntityId& l_entity, const Axis& l_axis); 20 | void SetDirection(const EntityId& l_entity, const Direction& l_dir); 21 | const sf::Vector2f& GetTileFriction(unsigned int l_elevation, 22 | unsigned int l_x, unsigned int l_y); 23 | void MovementStep(float l_dT, C_Movable* l_movable, C_Position* l_position); 24 | 25 | Map* m_gameMap; 26 | }; -------------------------------------------------------------------------------- /chapter_10/S_Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include 4 | #include 5 | #include "Window.h" 6 | 7 | class S_Renderer : public S_Base{ 8 | public: 9 | S_Renderer(SystemManager* l_systemMgr); 10 | ~S_Renderer(); 11 | 12 | void Update(float l_dT); 13 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 14 | void Notify(const Message& l_message); 15 | void Render(Window* l_wind, unsigned int l_layer); 16 | private: 17 | void SetSheetDirection(const EntityId& l_entity, const Direction& l_dir); 18 | void SortDrawables(); 19 | }; -------------------------------------------------------------------------------- /chapter_10/S_SheetAnimation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | class S_SheetAnimation : public S_Base{ 5 | public: 6 | S_SheetAnimation(SystemManager* l_systemMgr); 7 | ~S_SheetAnimation(); 8 | 9 | void Update(float l_dT); 10 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 11 | void Notify(const Message& l_message); 12 | private: 13 | void ChangeAnimation(const EntityId& l_entity, 14 | const std::string& l_anim, bool l_play, bool l_loop); 15 | }; -------------------------------------------------------------------------------- /chapter_10/S_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "C_State.h" 4 | 5 | class S_State : public S_Base{ 6 | public: 7 | S_State(SystemManager* l_systemMgr); 8 | ~S_State(); 9 | 10 | void Update(float l_dT); 11 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 12 | void Notify(const Message& l_message); 13 | private: 14 | void ChangeState(const EntityId& l_entity, const EntityState& l_state, 15 | const bool& l_force); 16 | }; -------------------------------------------------------------------------------- /chapter_10/SharedContext.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "TextureManager.h" 5 | #include "FontManager.h" 6 | #include "System_Manager.h" 7 | #include "Entity_Manager.h" 8 | #include "DebugOverlay.h" 9 | 10 | class Map; 11 | 12 | struct SharedContext{ 13 | SharedContext(): 14 | m_wind(nullptr), 15 | m_eventManager(nullptr), 16 | m_textureManager(nullptr), 17 | m_fontManager(nullptr), 18 | m_systemManager(nullptr), 19 | m_entityManager(nullptr), 20 | m_gameMap(nullptr){} 21 | 22 | Window* m_wind; 23 | EventManager* m_eventManager; 24 | TextureManager* m_textureManager; 25 | FontManager* m_fontManager; 26 | SystemManager* m_systemManager; 27 | EntityManager* m_entityManager; 28 | Map* m_gameMap; 29 | DebugOverlay m_debugOverlay; 30 | }; -------------------------------------------------------------------------------- /chapter_10/State_Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "Map.h" 4 | #include "EventManager.h" 5 | 6 | class State_Game : public BaseState{ 7 | public: 8 | State_Game(StateManager* l_stateManager); 9 | ~State_Game(); 10 | 11 | void OnCreate(); 12 | void OnDestroy(); 13 | 14 | void Activate(); 15 | void Deactivate(); 16 | 17 | void Update(const sf::Time& l_time); 18 | void Draw(); 19 | 20 | void MainMenu(EventDetails* l_details); 21 | void Pause(EventDetails* l_details); 22 | void PlayerMove(EventDetails* l_details); 23 | 24 | // Debug: 25 | void ToggleOverlay(EventDetails* l_details); 26 | private: 27 | void UpdateCamera(); 28 | 29 | Map* m_gameMap; 30 | int m_player; 31 | }; -------------------------------------------------------------------------------- /chapter_10/State_GameOver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_GameOver : public BaseState{ 6 | public: 7 | State_GameOver(StateManager* l_stateManager); 8 | ~State_GameOver(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | private: 19 | sf::Text m_text; 20 | float m_elapsed; 21 | }; -------------------------------------------------------------------------------- /chapter_10/State_Intro.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Intro : public BaseState{ 6 | public: 7 | State_Intro(StateManager* l_stateManager); 8 | ~State_Intro(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Continue(EventDetails* l_details); 20 | private: 21 | sf::Sprite m_introSprite; 22 | sf::Text m_text; 23 | }; -------------------------------------------------------------------------------- /chapter_10/State_MainMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_MainMenu : public BaseState{ 6 | public: 7 | State_MainMenu(StateManager* l_stateManager); 8 | ~State_MainMenu(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void MouseClick(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | 24 | sf::Vector2f m_buttonSize; 25 | sf::Vector2f m_buttonPos; 26 | unsigned int m_buttonPadding; 27 | 28 | sf::RectangleShape m_rects[3]; 29 | sf::Text m_labels[3]; 30 | }; -------------------------------------------------------------------------------- /chapter_10/State_Paused.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Paused : public BaseState{ 6 | public: 7 | State_Paused(StateManager* l_stateManager); 8 | ~State_Paused(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Unpause(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | sf::RectangleShape m_rect; 24 | }; -------------------------------------------------------------------------------- /chapter_10/TextureManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class TextureManager: public ResourceManager{ 6 | public: 7 | TextureManager(): ResourceManager("textures.cfg"){} 8 | 9 | sf::Texture* Load(const std::string& l_path){ 10 | sf::Texture* texture = new sf::Texture(); 11 | if(!texture->loadFromFile( 12 | Utils::GetResourceDirectory() + l_path)) 13 | { 14 | delete texture; 15 | texture = nullptr; 16 | std::cerr << "! Failed to load texture: " << l_path << std::endl; 17 | } 18 | return texture; 19 | } 20 | }; -------------------------------------------------------------------------------- /chapter_10/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Anim_Base.cpp Game.cpp Map.cpp S_Movement.cpp S_State.cpp State_MainMenu.cpp Window.cpp Anim_Directional.cpp GUI_Element.cpp S_Base.cpp SpriteSheet.cpp State_Game.cpp StateManager.cpp Entity_Manager.cpp GUI_Interface.cpp S_Collision.cpp S_Renderer.cpp State_GameOver.cpp State_Paused.cpp EventManager.cpp Main.cpp S_Control.cpp S_SheetAnimation.cpp State_Intro.cpp GUI_Manager.cpp GUI_Label.cpp GUI_Scrollbar.cpp GUI_Textfield.cpp System_Manager.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_11/Anim_Directional.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Anim_Base.h" 3 | #include "Directions.h" 4 | 5 | class Anim_Directional : public Anim_Base{ 6 | protected: 7 | void FrameStep(); 8 | void CropSprite(); 9 | void ReadIn(std::stringstream& l_stream); 10 | }; -------------------------------------------------------------------------------- /chapter_11/Assets/fonts.cfg: -------------------------------------------------------------------------------- 1 | Main media/Fonts/Vegur-Regular.otf -------------------------------------------------------------------------------- /chapter_11/Assets/keys.cfg: -------------------------------------------------------------------------------- 1 | Window_close 0:0 2 | Text_Entered 4:0 3 | Fullscreen_toggle 5:89 4 | Intro_Continue 5:57 5 | Mouse_Left 9:0 6 | Mouse_Left_Release 10:0 7 | Key_Escape 5:36 8 | Key_P 5:15 9 | Key_T 5:19 10 | Key_K 5:10 11 | Key_O 5:14 12 | Key_X 5:23 13 | Player_MoveLeft 24:0 14 | Player_MoveRight 24:3 15 | Player_MoveUp 24:22 16 | Player_MoveDown 24:18 17 | Player_Attack 24:58 18 | MainMenu_Play 27:MainMenu:Play 19 | MainMenu_Quit 27:MainMenu:Quit -------------------------------------------------------------------------------- /chapter_11/Assets/media/Entities/Player.entity: -------------------------------------------------------------------------------- 1 | Name Player 2 | Attributes 63 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 1 5 | Component 1 Player 6 | Component 2 0 7 | Component 3 128.0 1024.0 1024.0 1 8 | Component 4 9 | Component 5 20.0 20.0 0.0 0.0 2 -------------------------------------------------------------------------------- /chapter_11/Assets/media/Entities/Skeleton.entity: -------------------------------------------------------------------------------- 1 | Name Skeleton 2 | Attributes 63 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 0 5 | Component 1 Skeleton 6 | Component 2 0 7 | Component 3 128.0 1024.0 1024.0 1 8 | Component 4 9 | Component 5 20.0 20.0 0.0 0.0 2 -------------------------------------------------------------------------------- /chapter_11/Assets/media/Fonts/Vegur-Regular.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_11/Assets/media/Fonts/Vegur-Regular.otf -------------------------------------------------------------------------------- /chapter_11/Assets/media/GUI_Interfaces/MainMenu.interface: -------------------------------------------------------------------------------- 1 | Interface MainMenu MainMenu.style 0 0 Immovable NoTitle "Main menu" 2 | Element Label Title 100 0 MainMenuTitle.style "Main menu:" 3 | Element Label Play 0 32 MainMenuLabel.style "PLAY" 4 | Element Label Credits 0 68 MainMenuLabel.style "CREDITS" 5 | Element Label Quit 0 104 MainMenuLabel.style "EXIT" -------------------------------------------------------------------------------- /chapter_11/Assets/media/GUI_Interfaces/Test.interface: -------------------------------------------------------------------------------- 1 | Interface SomeWindow TestInterface.style 50 50 Movable Title "Test Interface" 2 | Element Label TestLabel 0 0 Test.style "This is a label." 3 | Element Label OtherLabel 0 320 Test2.style "This is a different label" 4 | Element Scrollbar Slider1 0 0 TestScrollbar.style Vertical -------------------------------------------------------------------------------- /chapter_11/Assets/media/GUI_Styles/MainMenu.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 300 150 3 | TextSize 12 4 | Font Main 5 | /State -------------------------------------------------------------------------------- /chapter_11/Assets/media/GUI_Styles/MainMenuLabel.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 300 32 3 | BgColor 255 0 0 255 4 | TextColor 255 255 255 255 5 | TextSize 14 6 | Font Main 7 | TextPadding 150 16 8 | TextOriginCenter 9 | /State 10 | 11 | State Hover 12 | BgColor 255 100 0 255 13 | /State 14 | 15 | State Clicked 16 | BgColor 255 150 0 255 17 | /State -------------------------------------------------------------------------------- /chapter_11/Assets/media/GUI_Styles/MainMenuTitle.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 118 32 3 | TextColor 255 255 255 255 4 | TextSize 24 5 | Font Main 6 | /State -------------------------------------------------------------------------------- /chapter_11/Assets/media/GUI_Styles/Test.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 64 16 3 | TextColor 0 0 0 255 4 | TextSize 12 5 | Font Main 6 | TextPadding 0 0 7 | /State -------------------------------------------------------------------------------- /chapter_11/Assets/media/GUI_Styles/Test2.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 64 32 3 | TextColor 0 0 0 255 4 | TextSize 12 5 | Font Main 6 | TextPadding 0 0 7 | /State 8 | 9 | State Hover 10 | TextColor 255 255 255 255 11 | /State 12 | 13 | State Clicked 14 | TextColor 255 0 0 255 15 | /State -------------------------------------------------------------------------------- /chapter_11/Assets/media/GUI_Styles/TestInterface.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 450 277 3 | BgImage TestGUIBg 4 | BgImageColor 255 255 255 150 5 | TextColor 255 255 255 255 6 | TextSize 12 7 | Font Main 8 | TextPadding 0 0 9 | ElementColor 128 128 128 255 10 | /State 11 | 12 | State Hover 13 | BgImageColor 255 255 255 200 14 | /State 15 | 16 | State Clicked 17 | BgImageColor 255 255 255 200 18 | /State -------------------------------------------------------------------------------- /chapter_11/Assets/media/GUI_Styles/TestScrollbar.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 12 12 3 | BgColor 80 80 80 200 4 | |BgImage TestImage 5 | ElementColor 50 50 50 255 6 | |Glyph TestImage2 7 | GlyphPadding 0 0 8 | /State 9 | 10 | State Hover 11 | BgColor 155 155 155 200 12 | /State 13 | 14 | State Clicked 15 | BgColor 200 200 200 200 16 | /State -------------------------------------------------------------------------------- /chapter_11/Assets/media/Spritesheets/Player.sheet: -------------------------------------------------------------------------------- 1 | Texture PlayerSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 0 0 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_11/Assets/media/Spritesheets/Skeleton.sheet: -------------------------------------------------------------------------------- 1 | Texture SkeletonSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 8 8 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_11/Assets/media/Textures/ADOM-OGA-UI.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_11/Assets/media/Textures/ADOM-OGA-UI.png -------------------------------------------------------------------------------- /chapter_11/Assets/media/Textures/PlayerSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_11/Assets/media/Textures/PlayerSheet.png -------------------------------------------------------------------------------- /chapter_11/Assets/media/Textures/SkeletonSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_11/Assets/media/Textures/SkeletonSheet.png -------------------------------------------------------------------------------- /chapter_11/Assets/media/Textures/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_11/Assets/media/Textures/intro.png -------------------------------------------------------------------------------- /chapter_11/Assets/media/Textures/tilesheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_11/Assets/media/Textures/tilesheet.png -------------------------------------------------------------------------------- /chapter_11/Assets/textures.cfg: -------------------------------------------------------------------------------- 1 | Intro media/Textures/intro.png 2 | PlayerSprite media/Textures/PlayerSheet.png 3 | SkeletonSprite media/Textures/SkeletonSheet.png 4 | TileSheet media/Textures/tilesheet.png 5 | TestGUIBg media/Textures/ADOM-OGA-UI.png -------------------------------------------------------------------------------- /chapter_11/C_Base.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "ECS_Types.h" 5 | 6 | class C_Base{ 7 | public: 8 | C_Base(const Component& l_type): m_type(l_type){} 9 | virtual ~C_Base(){} 10 | 11 | Component GetType(){ return m_type; } 12 | 13 | friend std::stringstream& operator >>( 14 | std::stringstream& l_stream, C_Base& b) 15 | { 16 | b.ReadIn(l_stream); 17 | return l_stream; 18 | } 19 | 20 | virtual void ReadIn(std::stringstream& l_stream) = 0; 21 | protected: 22 | Component m_type; 23 | }; -------------------------------------------------------------------------------- /chapter_11/C_Controller.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | class C_Controller : public C_Base{ 5 | public: 6 | C_Controller() : C_Base(Component::Controller){} 7 | void ReadIn(std::stringstream& l_stream){} 8 | private: 9 | 10 | }; -------------------------------------------------------------------------------- /chapter_11/C_Drawable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | #include 4 | 5 | class C_Drawable : public C_Base{ 6 | public: 7 | C_Drawable(const Component& l_type) : C_Base(l_type){} 8 | virtual ~C_Drawable(){} 9 | 10 | virtual void UpdatePosition(const sf::Vector2f& l_vec) = 0; 11 | virtual const sf::Vector2u& GetSize() = 0; 12 | virtual void Draw(sf::RenderWindow* l_wind) = 0; 13 | private: 14 | 15 | }; -------------------------------------------------------------------------------- /chapter_11/C_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | enum class EntityState{ Idle, Walking, Attacking, Hurt, Dying }; 5 | 6 | class C_State : public C_Base{ 7 | public: 8 | C_State(): C_Base(Component::State){} 9 | void ReadIn(std::stringstream& l_stream){ 10 | unsigned int state = 0; 11 | l_stream >> state; 12 | m_state = (EntityState)state; 13 | } 14 | 15 | EntityState GetState(){ return m_state; } 16 | void SetState(const EntityState& l_state){ 17 | m_state = l_state; 18 | } 19 | private: 20 | EntityState m_state; 21 | }; -------------------------------------------------------------------------------- /chapter_11/DebugOverlay.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | class DebugOverlay{ 6 | public: 7 | DebugOverlay(){ 8 | m_debug = false; 9 | } 10 | 11 | ~DebugOverlay(){ 12 | while(m_drawables.begin() != m_drawables.end()){ 13 | delete m_drawables.back(); 14 | m_drawables.pop_back(); 15 | } 16 | } 17 | 18 | void Add(sf::Drawable* l_drawable){ 19 | m_drawables.push_back(l_drawable); 20 | } 21 | 22 | void Draw(sf::RenderWindow* l_wind){ 23 | while(m_drawables.begin() != m_drawables.end()){ 24 | l_wind->draw(*m_drawables.back()); 25 | delete m_drawables.back(); 26 | m_drawables.pop_back(); 27 | } 28 | } 29 | 30 | bool Debug(){ return m_debug; } 31 | void SetDebug(const bool& l_val){ m_debug = l_val; } 32 | private: 33 | std::vector m_drawables; 34 | bool m_debug; 35 | }; -------------------------------------------------------------------------------- /chapter_11/Directions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | enum class Direction{ Up = 0, Left, Down, Right }; -------------------------------------------------------------------------------- /chapter_11/ECS_Types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using ComponentType = unsigned int; 3 | #define N_COMPONENT_TYPES 32 4 | 5 | enum class Component{ 6 | Position = 0, SpriteSheet, State, Movable, Controller, Collidable 7 | }; 8 | 9 | enum class System{ 10 | Renderer = 0, Movement, Collision, Control, State, SheetAnimation 11 | }; -------------------------------------------------------------------------------- /chapter_11/EntityEvents.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityEvent{ 4 | Spawned, Despawned, Colliding_X, Colliding_Y, 5 | Moving_Left, Moving_Right, Moving_Up, Moving_Down, 6 | Elevation_Change, Became_Idle, Began_Moving 7 | }; -------------------------------------------------------------------------------- /chapter_11/EntityMessages.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityMessage{ 4 | Move, Is_Moving, State_Changed, Direction_Changed, 5 | Switch_State, Attack_Action, Dead 6 | }; -------------------------------------------------------------------------------- /chapter_11/Event_Queue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | using EventID = unsigned int; 5 | 6 | class EventQueue{ 7 | public: 8 | void AddEvent(const EventID& l_event){m_queue.push(l_event);} 9 | 10 | bool ProcessEvents(EventID& l_id){ 11 | if (m_queue.empty()){ return false; } 12 | l_id = m_queue.front(); 13 | m_queue.pop(); 14 | return true; 15 | } 16 | 17 | void Clear(){ while(!m_queue.empty()){ m_queue.pop(); }} 18 | private: 19 | std::queue m_queue; 20 | }; -------------------------------------------------------------------------------- /chapter_11/FontManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class FontManager : public ResourceManager{ 6 | public: 7 | FontManager() : ResourceManager("fonts.cfg"){} 8 | 9 | sf::Font* Load(const std::string& l_path){ 10 | sf::Font* font = new sf::Font(); 11 | if (!font->loadFromFile(Utils::GetResourceDirectory() + l_path)) 12 | { 13 | delete font; 14 | font = nullptr; 15 | std::cerr << "! Failed to load font: " << l_path << std::endl; 16 | } 17 | return font; 18 | } 19 | }; -------------------------------------------------------------------------------- /chapter_11/GUI_Event.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | enum class GUI_EventType{ None, Click, Release, Hover, Leave }; 5 | 6 | struct ClickCoordinates{ 7 | float x, y; 8 | }; 9 | 10 | struct GUI_Event{ 11 | GUI_EventType m_type; 12 | const char* m_element; 13 | const char* m_interface; 14 | union{ 15 | ClickCoordinates m_clickCoords; 16 | }; 17 | }; -------------------------------------------------------------------------------- /chapter_11/GUI_Label.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "GUI_Element.h" 3 | 4 | class GUI_Label : public GUI_Element{ 5 | public: 6 | GUI_Label(const std::string& l_name, GUI_Interface* l_owner); 7 | ~GUI_Label(); 8 | 9 | void ReadIn(std::stringstream& l_stream); 10 | void OnClick(const sf::Vector2f& l_mousePos); 11 | void OnRelease(); 12 | void OnHover(const sf::Vector2f& l_mousePos); 13 | void OnLeave(); 14 | void Update(float l_dT); 15 | void Draw(sf::RenderTarget* l_target); 16 | }; -------------------------------------------------------------------------------- /chapter_11/GUI_Textfield.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "GUI_Element.h" 3 | 4 | class GUI_Textfield : public GUI_Element{ 5 | public: 6 | GUI_Textfield(const std::string& l_name, GUI_Interface* l_owner); 7 | ~GUI_Textfield(); 8 | 9 | void ReadIn(std::stringstream& l_stream); 10 | void OnClick(const sf::Vector2f& l_mousePos); 11 | void OnRelease(); 12 | void OnHover(const sf::Vector2f& l_mousePos); 13 | void OnLeave(); 14 | void Update(float l_dT); 15 | void Draw(sf::RenderTarget* l_target); 16 | }; -------------------------------------------------------------------------------- /chapter_11/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "StateManager.h" 5 | #include "TextureManager.h" 6 | #include 7 | 8 | class Game{ 9 | public: 10 | Game(); 11 | ~Game(); 12 | 13 | void Update(); 14 | void Render(); 15 | void LateUpdate(); 16 | 17 | sf::Time GetElapsed(); 18 | 19 | Window* GetWindow(); 20 | private: 21 | void RestartClock(); 22 | 23 | sf::Clock m_clock; 24 | sf::Time m_elapsed; 25 | SharedContext m_context; 26 | Window m_window; 27 | TextureManager m_textureManager; 28 | FontManager m_fontManager; 29 | SystemManager m_systemManager; 30 | EntityManager m_entityManager; 31 | GUI_Manager m_guiManager; 32 | StateManager m_stateManager; 33 | }; 34 | -------------------------------------------------------------------------------- /chapter_11/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | int main(int argc, char* argv[]){ 4 | // Program entry point. 5 | { 6 | Game game; 7 | while(!game.GetWindow()->IsDone()){ 8 | game.Update(); 9 | game.Render(); 10 | game.LateUpdate(); 11 | } 12 | } 13 | system("PAUSE"); 14 | } -------------------------------------------------------------------------------- /chapter_11/Message.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using MessageType = unsigned int; 3 | struct TwoFloats{ float m_x; float m_y; }; 4 | 5 | struct Message{ 6 | Message(const MessageType& l_type) : m_type(l_type){} 7 | 8 | MessageType m_type; 9 | int m_sender; 10 | int m_receiver; 11 | 12 | union{ 13 | TwoFloats m_2f; 14 | bool m_bool; 15 | int m_int; 16 | }; 17 | }; -------------------------------------------------------------------------------- /chapter_11/MessageHandler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Communicator.h" 3 | #include "EntityMessages.h" 4 | #include 5 | 6 | using Subscribtions = std::unordered_map; 7 | 8 | class MessageHandler{ 9 | public: 10 | bool Subscribe(const EntityMessage& l_type, Observer* l_observer) 11 | { 12 | return m_communicators[l_type].AddObserver(l_observer); 13 | } 14 | 15 | bool Unsubscribe(const EntityMessage& l_type, Observer* l_observer) 16 | { 17 | return m_communicators[l_type].RemoveObserver(l_observer); 18 | } 19 | 20 | void Dispatch(const Message& l_msg){ 21 | auto itr = m_communicators.find((EntityMessage)l_msg.m_type); 22 | if (itr == m_communicators.end()){ return; } 23 | itr->second.Broadcast(l_msg); 24 | } 25 | private: 26 | Subscribtions m_communicators; 27 | }; -------------------------------------------------------------------------------- /chapter_11/Observer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Message.h" 3 | 4 | class Observer{ 5 | public: 6 | virtual ~Observer(){} 7 | virtual void Notify(const Message& l_message) = 0; 8 | }; -------------------------------------------------------------------------------- /chapter_11/S_Control.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | class S_Control : public S_Base{ 5 | public: 6 | S_Control(SystemManager* l_systemMgr); 7 | ~S_Control(); 8 | 9 | void Update(float l_dT); 10 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 11 | void Notify(const Message& l_message); 12 | private: 13 | void MoveEntity(const EntityId& l_entity, const Direction& l_dir); 14 | }; -------------------------------------------------------------------------------- /chapter_11/S_Movement.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | enum class Axis{ x, y }; 5 | 6 | class Map; 7 | 8 | class S_Movement : public S_Base{ 9 | public: 10 | S_Movement(SystemManager* l_systemMgr); 11 | ~S_Movement(); 12 | 13 | void Update(float l_dT); 14 | void HandleEvent(const EntityId& l_entity, const EntityEvent& l_event); 15 | void Notify(const Message& l_message); 16 | 17 | void SetMap(Map* l_gameMap); 18 | private: 19 | void StopEntity(const EntityId& l_entity, const Axis& l_axis); 20 | void SetDirection(const EntityId& l_entity, const Direction& l_dir); 21 | const sf::Vector2f& GetTileFriction(unsigned int l_elevation, 22 | unsigned int l_x, unsigned int l_y); 23 | void MovementStep(float l_dT, C_Movable* l_movable, C_Position* l_position); 24 | 25 | Map* m_gameMap; 26 | }; -------------------------------------------------------------------------------- /chapter_11/S_Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include 4 | #include 5 | #include "Window.h" 6 | 7 | class S_Renderer : public S_Base{ 8 | public: 9 | S_Renderer(SystemManager* l_systemMgr); 10 | ~S_Renderer(); 11 | 12 | void Update(float l_dT); 13 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 14 | void Notify(const Message& l_message); 15 | void Render(Window* l_wind, unsigned int l_layer); 16 | private: 17 | void SetSheetDirection(const EntityId& l_entity, const Direction& l_dir); 18 | void SortDrawables(); 19 | }; -------------------------------------------------------------------------------- /chapter_11/S_SheetAnimation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | class S_SheetAnimation : public S_Base{ 5 | public: 6 | S_SheetAnimation(SystemManager* l_systemMgr); 7 | ~S_SheetAnimation(); 8 | 9 | void Update(float l_dT); 10 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 11 | void Notify(const Message& l_message); 12 | private: 13 | void ChangeAnimation(const EntityId& l_entity, 14 | const std::string& l_anim, bool l_play, bool l_loop); 15 | }; -------------------------------------------------------------------------------- /chapter_11/S_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "C_State.h" 4 | 5 | class S_State : public S_Base{ 6 | public: 7 | S_State(SystemManager* l_systemMgr); 8 | ~S_State(); 9 | 10 | void Update(float l_dT); 11 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 12 | void Notify(const Message& l_message); 13 | private: 14 | void ChangeState(const EntityId& l_entity, const EntityState& l_state, 15 | const bool& l_force); 16 | }; -------------------------------------------------------------------------------- /chapter_11/State_Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "Map.h" 4 | #include 5 | 6 | class State_Game : public BaseState{ 7 | public: 8 | State_Game(StateManager* l_stateManager); 9 | ~State_Game(); 10 | 11 | void OnCreate(); 12 | void OnDestroy(); 13 | 14 | void Activate(); 15 | void Deactivate(); 16 | 17 | void Update(const sf::Time& l_time); 18 | void Draw(); 19 | 20 | void MainMenu(EventDetails* l_details); 21 | void Pause(EventDetails* l_details); 22 | void PlayerMove(EventDetails* l_details); 23 | 24 | // Debug: 25 | void ToggleOverlay(EventDetails* l_details); 26 | private: 27 | void UpdateCamera(); 28 | Map* m_gameMap; 29 | int m_player; 30 | }; -------------------------------------------------------------------------------- /chapter_11/State_GameOver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_GameOver : public BaseState{ 6 | public: 7 | State_GameOver(StateManager* l_stateManager); 8 | ~State_GameOver(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | private: 19 | sf::Text m_text; 20 | float m_elapsed; 21 | }; -------------------------------------------------------------------------------- /chapter_11/State_Intro.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Intro : public BaseState{ 6 | public: 7 | State_Intro(StateManager* l_stateManager); 8 | ~State_Intro(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Continue(EventDetails* l_details); 20 | private: 21 | sf::Sprite m_introSprite; 22 | sf::Text m_text; 23 | }; -------------------------------------------------------------------------------- /chapter_11/State_MainMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include 4 | #include "EventManager.h" 5 | 6 | class State_MainMenu : public BaseState{ 7 | public: 8 | State_MainMenu(StateManager* l_stateManager); 9 | ~State_MainMenu(); 10 | 11 | void OnCreate(); 12 | void OnDestroy(); 13 | 14 | void Activate(); 15 | void Deactivate(); 16 | 17 | void Update(const sf::Time& l_time); 18 | void Draw(); 19 | 20 | void Play(EventDetails* l_details); 21 | void Quit(EventDetails* l_details); 22 | private: 23 | 24 | }; -------------------------------------------------------------------------------- /chapter_11/State_Paused.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Paused : public BaseState{ 6 | public: 7 | State_Paused(StateManager* l_stateManager); 8 | ~State_Paused(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Unpause(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | sf::RectangleShape m_rect; 24 | }; -------------------------------------------------------------------------------- /chapter_11/TextureManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class TextureManager: public ResourceManager{ 6 | public: 7 | TextureManager(): ResourceManager("textures.cfg"){} 8 | 9 | sf::Texture* Load(const std::string& l_path){ 10 | sf::Texture* texture = new sf::Texture(); 11 | if(!texture->loadFromFile( 12 | Utils::GetResourceDirectory() + l_path)) 13 | { 14 | delete texture; 15 | texture = nullptr; 16 | std::cerr << "! Failed to load texture: " << l_path << std::endl; 17 | } 18 | return texture; 19 | } 20 | }; -------------------------------------------------------------------------------- /chapter_11/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | g++ Anim_Base.cpp GUI_Element.cpp GUI_Textfield.cpp S_Collision.cpp S_SheetAnimation.cpp State_MainMenu.cpp Anim_Directional.cpp GUI_Interface.cpp S_Control.cpp S_State.cpp StateManager.cpp Entity_Manager.cpp GUI_Label.cpp Main.cpp S_Movement.cpp State_Game.cpp State_Paused.cpp EventManager.cpp GUI_Manager.cpp Map.cpp SpriteSheet.cpp State_GameOver.cpp System_Manager.cpp Game.cpp GUI_Scrollbar.cpp S_Base.cpp S_Renderer.cpp State_Intro.cpp Window.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main -------------------------------------------------------------------------------- /chapter_12/Anim_Directional.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Anim_Base.h" 3 | #include "Directions.h" 4 | 5 | class Anim_Directional : public Anim_Base{ 6 | protected: 7 | void FrameStep(); 8 | void CropSprite(); 9 | void ReadIn(std::stringstream& l_stream); 10 | }; -------------------------------------------------------------------------------- /chapter_12/Assets/audio.cfg.txt: -------------------------------------------------------------------------------- 1 | TestSound media/Audio/1_Mono.ogg 2 | Glass media/Audio/glass.ogg 3 | Footstep media/Audio/footstep.ogg 4 | Electrix media/Audio/Electrix_NES.ogg 5 | TownTheme media/Audio/TownTheme.ogg -------------------------------------------------------------------------------- /chapter_12/Assets/fonts.cfg.txt: -------------------------------------------------------------------------------- 1 | Main media/Fonts/Vegur-Regular.otf -------------------------------------------------------------------------------- /chapter_12/Assets/keys.cfg.txt: -------------------------------------------------------------------------------- 1 | Window_close 0:0 2 | Fullscreen_toggle 5:89 3 | Intro_Continue 5:57 4 | Mouse_Left 9:0 5 | Mouse_Left_Release 10:0 6 | Key_Escape 5:36 7 | Key_P 5:15 8 | Key_T 5:19 9 | Key_K 5:10 10 | Key_O 5:14 11 | Key_X 5:23 12 | Player_MoveLeft 24:0 13 | Player_MoveRight 24:3 14 | Player_MoveUp 24:22 15 | Player_MoveDown 24:18 16 | Player_Attack 24:58 17 | MainMenu_Play 27:MainMenu:Play 18 | MainMenu_Quit 27:MainMenu:Quit -------------------------------------------------------------------------------- /chapter_12/Assets/media/Audio/1_Mono.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Audio/1_Mono.ogg -------------------------------------------------------------------------------- /chapter_12/Assets/media/Audio/1_Stereo.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Audio/1_Stereo.ogg -------------------------------------------------------------------------------- /chapter_12/Assets/media/Audio/Electrix_NES.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Audio/Electrix_NES.ogg -------------------------------------------------------------------------------- /chapter_12/Assets/media/Audio/TownTheme.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Audio/TownTheme.ogg -------------------------------------------------------------------------------- /chapter_12/Assets/media/Audio/footstep.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Audio/footstep.ogg -------------------------------------------------------------------------------- /chapter_12/Assets/media/Audio/glass.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Audio/glass.ogg -------------------------------------------------------------------------------- /chapter_12/Assets/media/Entities/Player.entity: -------------------------------------------------------------------------------- 1 | Name Player 2 | Attributes 255 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 1 5 | Component 1 Player 6 | Component 2 0 7 | Component 3 128.0 1024.0 1024.0 1 8 | Component 4 9 | Component 5 20.0 20.0 0.0 0.0 2 10 | Component 6 footstep:1,4 11 | Component 7 -------------------------------------------------------------------------------- /chapter_12/Assets/media/Entities/Skeleton.entity: -------------------------------------------------------------------------------- 1 | Name Skeleton 2 | Attributes 63 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 0 5 | Component 1 Skeleton 6 | Component 2 0 7 | Component 3 128.0 1024.0 1024.0 1 8 | Component 4 9 | Component 5 20.0 20.0 0.0 0.0 2 -------------------------------------------------------------------------------- /chapter_12/Assets/media/Fonts/Vegur-Regular.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Fonts/Vegur-Regular.otf -------------------------------------------------------------------------------- /chapter_12/Assets/media/GUI_Interfaces/MainMenu.interface: -------------------------------------------------------------------------------- 1 | Interface MainMenu MainMenu.style 0 0 Immovable NoTitle "Main menu" 2 | Element Label Title 100 0 MainMenuTitle.style "Main menu:" 3 | Element Label Play 0 32 MainMenuLabel.style "PLAY" 4 | Element Label Credits 0 68 MainMenuLabel.style "CREDITS" 5 | Element Label Quit 0 104 MainMenuLabel.style "EXIT" -------------------------------------------------------------------------------- /chapter_12/Assets/media/GUI_Interfaces/Test.interface: -------------------------------------------------------------------------------- 1 | Interface SomeWindow TestInterface.style 50 50 Movable Title "Test Interface" 2 | Element Label TestLabel 0 0 Test.style "This is a label." 3 | Element Label OtherLabel 0 320 Test2.style "This is a different label" 4 | Element Scrollbar Slider1 0 0 TestScrollbar.style Vertical -------------------------------------------------------------------------------- /chapter_12/Assets/media/GUI_Styles/MainMenu.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 300 150 3 | TextSize 12 4 | Font Main 5 | /State -------------------------------------------------------------------------------- /chapter_12/Assets/media/GUI_Styles/MainMenuLabel.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 300 32 3 | BgColor 255 0 0 255 4 | TextColor 255 255 255 255 5 | TextSize 14 6 | Font Main 7 | TextPadding 150 16 8 | TextOriginCenter 9 | /State 10 | 11 | State Hover 12 | BgColor 255 100 0 255 13 | /State 14 | 15 | State Clicked 16 | BgColor 255 150 0 255 17 | /State -------------------------------------------------------------------------------- /chapter_12/Assets/media/GUI_Styles/MainMenuTitle.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 118 32 3 | TextColor 255 255 255 255 4 | TextSize 24 5 | Font Main 6 | /State -------------------------------------------------------------------------------- /chapter_12/Assets/media/GUI_Styles/Test.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 64 16 3 | TextColor 0 0 0 255 4 | TextSize 12 5 | Font Main 6 | TextPadding 0 0 7 | /State -------------------------------------------------------------------------------- /chapter_12/Assets/media/GUI_Styles/Test2.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 64 32 3 | TextColor 0 0 0 255 4 | TextSize 12 5 | Font Main 6 | TextPadding 0 0 7 | /State 8 | 9 | State Hover 10 | TextColor 255 255 255 255 11 | /State 12 | 13 | State Clicked 14 | TextColor 255 0 0 255 15 | /State -------------------------------------------------------------------------------- /chapter_12/Assets/media/GUI_Styles/TestInterface.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 450 277 3 | BgImage TestGUIBg 4 | BgImageColor 255 255 255 150 5 | TextColor 255 255 255 255 6 | TextSize 12 7 | Font Main 8 | TextPadding 0 0 9 | ElementColor 128 128 128 255 10 | /State 11 | 12 | State Hover 13 | BgImageColor 255 255 255 200 14 | /State 15 | 16 | State Clicked 17 | BgImageColor 255 255 255 200 18 | /State -------------------------------------------------------------------------------- /chapter_12/Assets/media/GUI_Styles/TestScrollbar.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 12 12 3 | BgColor 80 80 80 200 4 | |BgImage TestImage 5 | ElementColor 50 50 50 255 6 | |Glyph TestImage2 7 | GlyphPadding 0 0 8 | /State 9 | 10 | State Hover 11 | BgColor 155 155 155 200 12 | /State 13 | 14 | State Clicked 15 | BgColor 200 200 200 200 16 | /State -------------------------------------------------------------------------------- /chapter_12/Assets/media/Sounds/footstep.sound: -------------------------------------------------------------------------------- 1 | Audio Footstep 2 | Volume 25 3 | Pitch 1.0 4 | Distance 150 5 | Attenuation 2 -------------------------------------------------------------------------------- /chapter_12/Assets/media/Sounds/test.sound: -------------------------------------------------------------------------------- 1 | Audio TestSound 2 | Volume 100 3 | Pitch 1.0 4 | Distance 150 5 | Attenuation 1 -------------------------------------------------------------------------------- /chapter_12/Assets/media/Sounds/test2.sound: -------------------------------------------------------------------------------- 1 | Audio Glass 2 | Volume 100 3 | Pitch 1.0 4 | Distance 30 5 | Attenuation 5 -------------------------------------------------------------------------------- /chapter_12/Assets/media/Spritesheets/Player.sheet: -------------------------------------------------------------------------------- 1 | Texture PlayerSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 0 0 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_12/Assets/media/Spritesheets/Skeleton.sheet: -------------------------------------------------------------------------------- 1 | Texture SkeletonSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 8 8 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_12/Assets/media/Textures/ADOM-OGA-UI.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Textures/ADOM-OGA-UI.png -------------------------------------------------------------------------------- /chapter_12/Assets/media/Textures/PlayerSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Textures/PlayerSheet.png -------------------------------------------------------------------------------- /chapter_12/Assets/media/Textures/SkeletonSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Textures/SkeletonSheet.png -------------------------------------------------------------------------------- /chapter_12/Assets/media/Textures/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Textures/intro.png -------------------------------------------------------------------------------- /chapter_12/Assets/media/Textures/tilesheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/media/Textures/tilesheet.png -------------------------------------------------------------------------------- /chapter_12/Assets/openal32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_12/Assets/openal32.dll -------------------------------------------------------------------------------- /chapter_12/Assets/textures.cfg.txt: -------------------------------------------------------------------------------- 1 | Intro media/Textures/intro.png 2 | PlayerSprite media/Textures/PlayerSheet.png 3 | SkeletonSprite media/Textures/SkeletonSheet.png 4 | TileSheet media/Textures/tilesheet.png 5 | TestGUIBg media/Textures/ADOM-OGA-UI.png -------------------------------------------------------------------------------- /chapter_12/AudioManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class AudioManager : public ResourceManager{ 6 | public: 7 | AudioManager() : ResourceManager("audio.cfg"){} 8 | 9 | sf::SoundBuffer* Load(const std::string& l_path){ 10 | sf::SoundBuffer* sound = new sf::SoundBuffer(); 11 | if (!sound->loadFromFile( 12 | Utils::GetWorkingDirectory() + l_path)) 13 | { 14 | delete sound; 15 | sound = nullptr; 16 | std::cerr << "! Failed to load sound: " << l_path << std::endl; 17 | } 18 | return sound; 19 | } 20 | }; -------------------------------------------------------------------------------- /chapter_12/C_Base.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "ECS_Types.h" 5 | 6 | class C_Base{ 7 | public: 8 | C_Base(const Component& l_type): m_type(l_type){} 9 | virtual ~C_Base(){} 10 | 11 | Component GetType(){ return m_type; } 12 | 13 | friend std::stringstream& operator >>( 14 | std::stringstream& l_stream, C_Base& b) 15 | { 16 | b.ReadIn(l_stream); 17 | return l_stream; 18 | } 19 | 20 | virtual void ReadIn(std::stringstream& l_stream) = 0; 21 | protected: 22 | Component m_type; 23 | }; -------------------------------------------------------------------------------- /chapter_12/C_Controller.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | class C_Controller : public C_Base{ 5 | public: 6 | C_Controller() : C_Base(Component::Controller){} 7 | void ReadIn(std::stringstream& l_stream){} 8 | private: 9 | 10 | }; -------------------------------------------------------------------------------- /chapter_12/C_Drawable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | #include 4 | 5 | class C_Drawable : public C_Base{ 6 | public: 7 | C_Drawable(const Component& l_type) : C_Base(l_type){} 8 | virtual ~C_Drawable(){} 9 | 10 | virtual void UpdatePosition(const sf::Vector2f& l_vec) = 0; 11 | virtual const sf::Vector2u& GetSize() = 0; 12 | virtual void Draw(sf::RenderWindow* l_wind) = 0; 13 | private: 14 | 15 | }; -------------------------------------------------------------------------------- /chapter_12/C_SoundListener.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | class C_SoundListener : public C_Base{ 5 | public: 6 | C_SoundListener() : C_Base(Component::SoundListener){} 7 | void ReadIn(std::stringstream& l_stream){} 8 | private: 9 | 10 | }; -------------------------------------------------------------------------------- /chapter_12/C_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | enum class EntityState{ Idle, Walking, Attacking, Hurt, Dying }; 5 | 6 | class C_State : public C_Base{ 7 | public: 8 | C_State(): C_Base(Component::State){} 9 | void ReadIn(std::stringstream& l_stream){ 10 | unsigned int state = 0; 11 | l_stream >> state; 12 | m_state = (EntityState)state; 13 | } 14 | 15 | EntityState GetState(){ return m_state; } 16 | void SetState(const EntityState& l_state){ 17 | m_state = l_state; 18 | } 19 | private: 20 | EntityState m_state; 21 | }; -------------------------------------------------------------------------------- /chapter_12/DebugOverlay.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | class DebugOverlay{ 6 | public: 7 | DebugOverlay(){ 8 | m_debug = false; 9 | } 10 | 11 | ~DebugOverlay(){ 12 | while(m_drawables.begin() != m_drawables.end()){ 13 | delete m_drawables.back(); 14 | m_drawables.pop_back(); 15 | } 16 | } 17 | 18 | void Add(sf::Drawable* l_drawable){ 19 | m_drawables.push_back(l_drawable); 20 | } 21 | 22 | void Draw(sf::RenderWindow* l_wind){ 23 | while(m_drawables.begin() != m_drawables.end()){ 24 | l_wind->draw(*m_drawables.back()); 25 | delete m_drawables.back(); 26 | m_drawables.pop_back(); 27 | } 28 | } 29 | 30 | bool Debug(){ return m_debug; } 31 | void SetDebug(const bool& l_val){ m_debug = l_val; } 32 | private: 33 | std::vector m_drawables; 34 | bool m_debug; 35 | }; -------------------------------------------------------------------------------- /chapter_12/Directions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | enum class Direction{ Up = 0, Left, Down, Right }; -------------------------------------------------------------------------------- /chapter_12/ECS_Types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using ComponentType = unsigned int; 3 | #define N_COMPONENT_TYPES 32 4 | 5 | enum class Component{ 6 | Position = 0, SpriteSheet, State, Movable, Controller, Collidable, SoundEmitter, SoundListener 7 | }; 8 | 9 | enum class System{ 10 | Renderer = 0, Movement, Collision, Control, State, SheetAnimation, Sound 11 | }; -------------------------------------------------------------------------------- /chapter_12/EntityEvents.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityEvent{ 4 | Spawned, Despawned, Colliding_X, Colliding_Y, 5 | Moving_Left, Moving_Right, Moving_Up, Moving_Down, 6 | Elevation_Change, Became_Idle, Began_Moving 7 | }; -------------------------------------------------------------------------------- /chapter_12/EntityMessages.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityMessage{ 4 | Move, Is_Moving, Frame_Change, State_Changed, 5 | Direction_Changed, Switch_State, 6 | Attack_Action, Dead 7 | }; -------------------------------------------------------------------------------- /chapter_12/Event_Queue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | using EventID = unsigned int; 5 | 6 | class EventQueue{ 7 | public: 8 | void AddEvent(const EventID& l_event){m_queue.push(l_event);} 9 | 10 | bool ProcessEvents(EventID& l_id){ 11 | if (m_queue.empty()){ return false; } 12 | l_id = m_queue.front(); 13 | m_queue.pop(); 14 | return true; 15 | } 16 | 17 | void Clear(){ while(!m_queue.empty()){ m_queue.pop(); }} 18 | private: 19 | std::queue m_queue; 20 | }; -------------------------------------------------------------------------------- /chapter_12/FontManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class FontManager : public ResourceManager{ 6 | public: 7 | FontManager() : ResourceManager("fonts.cfg"){} 8 | 9 | sf::Font* Load(const std::string& l_path){ 10 | sf::Font* font = new sf::Font(); 11 | if (!font->loadFromFile(Utils::GetWorkingDirectory() + l_path)) 12 | { 13 | delete font; 14 | font = nullptr; 15 | std::cerr << "! Failed to load font: " << l_path << std::endl; 16 | } 17 | return font; 18 | } 19 | }; -------------------------------------------------------------------------------- /chapter_12/GUI_Event.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | enum class GUI_EventType{ None, Click, Release, Hover, Leave }; 5 | 6 | struct ClickCoordinates{ 7 | float x, y; 8 | }; 9 | 10 | struct GUI_Event{ 11 | GUI_EventType m_type; 12 | const char* m_element; 13 | const char* m_interface; 14 | union{ 15 | ClickCoordinates m_clickCoords; 16 | }; 17 | }; -------------------------------------------------------------------------------- /chapter_12/GUI_Label.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "GUI_Element.h" 3 | 4 | class GUI_Label : public GUI_Element{ 5 | public: 6 | GUI_Label(const std::string& l_name, GUI_Interface* l_owner); 7 | ~GUI_Label(); 8 | 9 | void ReadIn(std::stringstream& l_stream); 10 | void OnClick(const sf::Vector2f& l_mousePos); 11 | void OnRelease(); 12 | void OnHover(const sf::Vector2f& l_mousePos); 13 | void OnLeave(); 14 | void Update(float l_dT); 15 | void Draw(sf::RenderTarget* l_target); 16 | }; -------------------------------------------------------------------------------- /chapter_12/GUI_TextField.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "GUI_Element.h" 3 | 4 | class GUI_Textfield : public GUI_Element{ 5 | public: 6 | GUI_Textfield(const std::string& l_name, GUI_Interface* l_owner); 7 | ~GUI_Textfield(); 8 | 9 | void ReadIn(std::stringstream& l_stream); 10 | void OnClick(const sf::Vector2f& l_mousePos); 11 | void OnRelease(); 12 | void OnHover(const sf::Vector2f& l_mousePos); 13 | void OnLeave(); 14 | void Update(float l_dT); 15 | void Draw(sf::RenderTarget* l_target); 16 | }; -------------------------------------------------------------------------------- /chapter_12/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Window.h" 3 | #include "EventManager.h" 4 | #include "StateManager.h" 5 | #include "TextureManager.h" 6 | #include 7 | 8 | class Game{ 9 | public: 10 | Game(); 11 | ~Game(); 12 | 13 | void Update(); 14 | void Render(); 15 | void LateUpdate(); 16 | 17 | sf::Time GetElapsed(); 18 | 19 | Window* GetWindow(); 20 | private: 21 | void RestartClock(); 22 | sf::Clock m_clock; 23 | sf::Time m_elapsed; 24 | SharedContext m_context; 25 | Window m_window; 26 | TextureManager m_textureManager; 27 | FontManager m_fontManager; 28 | AudioManager m_audioManager; 29 | SoundManager m_soundManager; 30 | SystemManager m_systemManager; 31 | EntityManager m_entityManager; 32 | GUI_Manager m_guiManager; 33 | StateManager m_stateManager; 34 | }; -------------------------------------------------------------------------------- /chapter_12/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | void main(int argc, void** argv[]){ 4 | // Program entry point. 5 | { 6 | Game game; 7 | while(!game.GetWindow()->IsDone()){ 8 | game.Update(); 9 | game.Render(); 10 | game.LateUpdate(); 11 | } 12 | } 13 | system("PAUSE"); 14 | } -------------------------------------------------------------------------------- /chapter_12/Message.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using MessageType = unsigned int; 3 | struct TwoFloats{ float m_x; float m_y; }; 4 | 5 | struct Message{ 6 | Message(const MessageType& l_type) : m_type(l_type){} 7 | 8 | MessageType m_type; 9 | int m_sender; 10 | int m_receiver; 11 | 12 | union{ 13 | TwoFloats m_2f; 14 | bool m_bool; 15 | int m_int; 16 | }; 17 | }; -------------------------------------------------------------------------------- /chapter_12/MessageHandler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Communicator.h" 3 | #include "EntityMessages.h" 4 | #include 5 | 6 | using Subscribtions = std::unordered_map; 7 | 8 | class MessageHandler{ 9 | public: 10 | bool Subscribe(const EntityMessage& l_type, Observer* l_observer) 11 | { 12 | return m_communicators[l_type].AddObserver(l_observer); 13 | } 14 | 15 | bool Unsubscribe(const EntityMessage& l_type, Observer* l_observer) 16 | { 17 | return m_communicators[l_type].RemoveObserver(l_observer); 18 | } 19 | 20 | void Dispatch(const Message& l_msg){ 21 | auto itr = m_communicators.find((EntityMessage)l_msg.m_type); 22 | if (itr == m_communicators.end()){ return; } 23 | itr->second.Broadcast(l_msg); 24 | } 25 | private: 26 | Subscribtions m_communicators; 27 | }; -------------------------------------------------------------------------------- /chapter_12/Observer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Message.h" 3 | 4 | class Observer{ 5 | public: 6 | virtual ~Observer(){} 7 | virtual void Notify(const Message& l_message) = 0; 8 | }; -------------------------------------------------------------------------------- /chapter_12/S_Control.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | class S_Control : public S_Base{ 5 | public: 6 | S_Control(SystemManager* l_systemMgr); 7 | ~S_Control(); 8 | 9 | void Update(float l_dT); 10 | void HandleEvent(const EntityId& l_entity, const EntityEvent& l_event); 11 | void Notify(const Message& l_message); 12 | private: 13 | void MoveEntity(const EntityId& l_entity, const Direction& l_dir); 14 | }; -------------------------------------------------------------------------------- /chapter_12/S_Movement.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | enum class Axis{ x, y }; 5 | 6 | class Map; 7 | 8 | class S_Movement : public S_Base{ 9 | public: 10 | S_Movement(SystemManager* l_systemMgr); 11 | ~S_Movement(); 12 | 13 | void Update(float l_dT); 14 | void HandleEvent(const EntityId& l_entity, const EntityEvent& l_event); 15 | void Notify(const Message& l_message); 16 | 17 | void SetMap(Map* l_gameMap); 18 | private: 19 | void StopEntity(const EntityId& l_entity, const Axis& l_axis); 20 | void SetDirection(const EntityId& l_entity, const Direction& l_dir); 21 | const sf::Vector2f& GetTileFriction(unsigned int l_elevation, 22 | unsigned int l_x, unsigned int l_y); 23 | void MovementStep(float l_dT, C_Movable* l_movable, C_Position* l_position); 24 | 25 | Map* m_gameMap; 26 | }; -------------------------------------------------------------------------------- /chapter_12/S_Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include 4 | #include 5 | #include "Window.h" 6 | 7 | class S_Renderer : public S_Base{ 8 | public: 9 | S_Renderer(SystemManager* l_systemMgr); 10 | ~S_Renderer(); 11 | 12 | void Update(float l_dT); 13 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 14 | void Notify(const Message& l_message); 15 | void Render(Window* l_wind, unsigned int l_layer); 16 | private: 17 | void SetSheetDirection(const EntityId& l_entity, const Direction& l_dir); 18 | void SortDrawables(); 19 | }; -------------------------------------------------------------------------------- /chapter_12/S_SheetAnimation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | 4 | class S_SheetAnimation : public S_Base{ 5 | public: 6 | S_SheetAnimation(SystemManager* l_systemMgr); 7 | ~S_SheetAnimation(); 8 | 9 | void Update(float l_dT); 10 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 11 | void Notify(const Message& l_message); 12 | private: 13 | void ChangeAnimation(const EntityId& l_entity, 14 | const std::string& l_anim, bool l_play, bool l_loop); 15 | }; -------------------------------------------------------------------------------- /chapter_12/S_Sound.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "C_SoundEmitter.h" 4 | #include "C_SoundListener.h" 5 | 6 | class S_Sound : public S_Base{ 7 | public: 8 | S_Sound(SystemManager* l_systemMgr); 9 | ~S_Sound(); 10 | 11 | void Update(float l_dT); 12 | void HandleEvent(const EntityId& l_entity, const EntityEvent& l_event); 13 | void Notify(const Message& l_message); 14 | 15 | void SetUp(AudioManager* l_audioManager, SoundManager* l_soundManager); 16 | private: 17 | sf::Vector3f MakeSoundPosition(const sf::Vector2f& l_entityPos, unsigned int l_elevation); 18 | void EmitSound(const EntityId& l_entity, const EntitySound& l_sound, 19 | bool l_useId, bool l_relative, int l_checkFrame = -1); 20 | AudioManager* m_audioManager; 21 | SoundManager* m_soundManager; 22 | }; -------------------------------------------------------------------------------- /chapter_12/S_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "C_State.h" 4 | 5 | class S_State : public S_Base{ 6 | public: 7 | S_State(SystemManager* l_systemMgr); 8 | ~S_State(); 9 | 10 | void Update(float l_dT); 11 | void HandleEvent(const EntityId& l_entity, const EntityEvent& l_event); 12 | void Notify(const Message& l_message); 13 | private: 14 | void ChangeState(const EntityId& l_entity, const EntityState& l_state, bool l_force); 15 | }; -------------------------------------------------------------------------------- /chapter_12/SoundProps.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | struct SoundProps{ 3 | SoundProps(const std::string& l_name): m_audioName(l_name),m_volume(100), m_pitch(1.f), 4 | m_minDistance(10.f), m_attenuation(10.f){} 5 | std::string m_audioName; 6 | float m_volume; 7 | float m_pitch; 8 | float m_minDistance; 9 | float m_attenuation; 10 | }; -------------------------------------------------------------------------------- /chapter_12/State_Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "Map.h" 4 | #include 5 | 6 | class State_Game : public BaseState{ 7 | public: 8 | State_Game(StateManager* l_stateManager); 9 | ~State_Game(); 10 | 11 | void OnCreate(); 12 | void OnDestroy(); 13 | 14 | void Activate(); 15 | void Deactivate(); 16 | 17 | void Update(const sf::Time& l_time); 18 | void Draw(); 19 | 20 | void MainMenu(EventDetails* l_details); 21 | void Pause(EventDetails* l_details); 22 | void PlayerMove(EventDetails* l_details); 23 | 24 | // Debug: 25 | void ToggleOverlay(EventDetails* l_details); 26 | private: 27 | void UpdateCamera(); 28 | Map* m_gameMap; 29 | int m_player; 30 | }; -------------------------------------------------------------------------------- /chapter_12/State_GameOver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_GameOver : public BaseState{ 6 | public: 7 | State_GameOver(StateManager* l_stateManager); 8 | ~State_GameOver(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | private: 19 | sf::Text m_text; 20 | float m_elapsed; 21 | }; -------------------------------------------------------------------------------- /chapter_12/State_Intro.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include 4 | #include 5 | #include "EventManager.h" 6 | 7 | class State_Intro : public BaseState{ 8 | public: 9 | State_Intro(StateManager* l_stateManager); 10 | ~State_Intro(); 11 | 12 | void OnCreate(); 13 | void OnDestroy(); 14 | 15 | void Activate(); 16 | void Deactivate(); 17 | 18 | void Update(const sf::Time& l_time); 19 | void Draw(); 20 | 21 | void Continue(EventDetails* l_details); 22 | private: 23 | sf::Sprite m_introSprite; 24 | sf::Text m_text; 25 | }; -------------------------------------------------------------------------------- /chapter_12/State_MainMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include 4 | #include "EventManager.h" 5 | 6 | class State_MainMenu : public BaseState{ 7 | public: 8 | State_MainMenu(StateManager* l_stateManager); 9 | ~State_MainMenu(); 10 | 11 | void OnCreate(); 12 | void OnDestroy(); 13 | 14 | void Activate(); 15 | void Deactivate(); 16 | 17 | void Update(const sf::Time& l_time); 18 | void Draw(); 19 | 20 | void Play(EventDetails* l_details); 21 | void Quit(EventDetails* l_details); 22 | private: 23 | 24 | }; -------------------------------------------------------------------------------- /chapter_12/State_Paused.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Paused : public BaseState{ 6 | public: 7 | State_Paused(StateManager* l_stateManager); 8 | ~State_Paused(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Unpause(EventDetails* l_details); 20 | private: 21 | sf::Font m_font; 22 | sf::Text m_text; 23 | sf::RectangleShape m_rect; 24 | }; -------------------------------------------------------------------------------- /chapter_12/TextureManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class TextureManager: public ResourceManager{ 6 | public: 7 | TextureManager(): ResourceManager("textures.cfg"){} 8 | 9 | sf::Texture* Load(const std::string& l_path){ 10 | sf::Texture* texture = new sf::Texture(); 11 | if(!texture->loadFromFile( 12 | Utils::GetWorkingDirectory() + l_path)) 13 | { 14 | delete texture; 15 | texture = nullptr; 16 | std::cerr << "! Failed to load texture: " << l_path << std::endl; 17 | } 18 | return texture; 19 | } 20 | }; -------------------------------------------------------------------------------- /chapter_13/Shared/Main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_13/Shared/Main.cpp -------------------------------------------------------------------------------- /chapter_13/Shared/NetworkDefinitions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | enum class Network{ 3 | HighestTimestamp = 2147483647, ClientTimeout = 10000, ServerPort = 5600, 4 | NullID = -1 5 | }; 6 | using ClientID = int; 7 | using PortNumber = unsigned short; -------------------------------------------------------------------------------- /chapter_13/Shared/PacketTypes.cpp: -------------------------------------------------------------------------------- 1 | #include "PacketTypes.h" 2 | void StampPacket(const PacketType& l_type, sf::Packet& l_packet){ 3 | l_packet << PacketID(l_type); 4 | } -------------------------------------------------------------------------------- /chapter_13/Shared/PacketTypes.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | using PacketID = sf::Int8; 4 | enum class PacketType{ 5 | Disconnect = -1, Connect, Heartbeat, Snapshot, 6 | Player_Update, Message, Hurt, OutOfBounds 7 | }; 8 | 9 | void StampPacket(const PacketType& l_type, sf::Packet& l_packet); -------------------------------------------------------------------------------- /chapter_14/Client/Anim_Directional.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Anim_Base.h" 3 | #include "Directions.h" 4 | 5 | class Anim_Directional : public Anim_Base{ 6 | protected: 7 | void FrameStep(); 8 | void CropSprite(); 9 | void ReadIn(std::stringstream& l_stream); 10 | }; -------------------------------------------------------------------------------- /chapter_14/Client/AudioManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class AudioManager : public ResourceManager{ 6 | public: 7 | AudioManager() : ResourceManager("audio.cfg"){} 8 | 9 | sf::SoundBuffer* Load(const std::string& l_path){ 10 | sf::SoundBuffer* sound = new sf::SoundBuffer(); 11 | if (!sound->loadFromFile( 12 | Utils::GetWorkingDirectory() + l_path)) 13 | { 14 | delete sound; 15 | sound = nullptr; 16 | std::cerr << "! Failed to load sound: " << l_path << std::endl; 17 | } 18 | return sound; 19 | } 20 | }; -------------------------------------------------------------------------------- /chapter_14/Client/C_Drawable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | #include 4 | 5 | class C_Drawable : public C_Base{ 6 | public: 7 | C_Drawable(const Component& l_type) : C_Base(l_type){} 8 | virtual ~C_Drawable(){} 9 | 10 | virtual void UpdatePosition(const sf::Vector2f& l_vec) = 0; 11 | virtual const sf::Vector2u& GetSize() = 0; 12 | virtual void Draw(sf::RenderWindow* l_wind) = 0; 13 | private: 14 | 15 | }; -------------------------------------------------------------------------------- /chapter_14/Client/C_SoundListener.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | class C_SoundListener : public C_Base{ 5 | public: 6 | C_SoundListener() : C_Base(Component::SoundListener){} 7 | void ReadIn(std::stringstream& l_stream){} 8 | private: 9 | 10 | }; -------------------------------------------------------------------------------- /chapter_14/Client/C_UI_Element.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | #include 4 | 5 | class C_UI_Element : public C_Base{ 6 | public: 7 | C_UI_Element() : C_Base(Component::UI_Element), m_showHealth(false), m_showName(false){} 8 | void ReadIn(std::stringstream& l_stream){ 9 | l_stream >> m_offset.x >> m_offset.y; 10 | } 11 | 12 | const sf::Vector2f& GetOffset(){ return m_offset; } 13 | void SetOffset(const sf::Vector2f& l_offset){ m_offset = l_offset; } 14 | 15 | void SetShowHealth(bool l_show){ m_showHealth = l_show; } 16 | void SetShowName(bool l_show){ m_showName = l_show; } 17 | bool ShowHealth(){ return m_showHealth; } 18 | bool ShowName(){ return m_showName; } 19 | private: 20 | sf::Vector2f m_offset; 21 | bool m_showHealth; 22 | bool m_showName; 23 | }; -------------------------------------------------------------------------------- /chapter_14/Client/Client_Entity_Manager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Entity_Manager.h" 3 | #include "C_Collidable.h" 4 | #include "C_Controller.h" 5 | #include "C_Movable.h" 6 | #include "C_State.h" 7 | #include "Entity_Manager.h" 8 | #include "C_Position.h" 9 | #include "C_SpriteSheet.h" 10 | #include "C_SoundEmitter.h" 11 | #include "C_SoundListener.h" 12 | #include "C_Client.h" 13 | #include "C_Health.h" 14 | #include "C_Name.h" 15 | #include "C_UI_Element.h" 16 | #include "TextureManager.h" 17 | #include 18 | 19 | class SystemManager; 20 | 21 | class ClientEntityManager : public EntityManager{ 22 | public: 23 | ClientEntityManager(SystemManager* l_sysMgr, TextureManager* l_textureMgr); 24 | ~ClientEntityManager(); 25 | 26 | int AddEntity(const std::string& l_entityFile, int l_id = -1); 27 | protected: 28 | TextureManager* m_textureManager; 29 | }; -------------------------------------------------------------------------------- /chapter_14/Client/FontManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class FontManager : public ResourceManager{ 6 | public: 7 | FontManager() : ResourceManager("fonts.cfg"){} 8 | 9 | sf::Font* Load(const std::string& l_path){ 10 | sf::Font* font = new sf::Font(); 11 | if (!font->loadFromFile(Utils::GetWorkingDirectory() + l_path)) 12 | { 13 | delete font; 14 | font = nullptr; 15 | std::cerr << "! Failed to load font: " << l_path << std::endl; 16 | } 17 | return font; 18 | } 19 | }; -------------------------------------------------------------------------------- /chapter_14/Client/GUI_Event.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | enum class GUI_EventType{ None, Click, Release, Hover, Leave }; 5 | 6 | struct ClickCoordinates{ 7 | float x, y; 8 | }; 9 | 10 | struct GUI_Event{ 11 | GUI_EventType m_type; 12 | const char* m_element; 13 | const char* m_interface; 14 | union{ 15 | ClickCoordinates m_clickCoords; 16 | }; 17 | }; -------------------------------------------------------------------------------- /chapter_14/Client/GUI_Label.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "GUI_Element.h" 3 | 4 | class GUI_Label : public GUI_Element{ 5 | public: 6 | GUI_Label(const std::string& l_name, GUI_Interface* l_owner); 7 | ~GUI_Label(); 8 | 9 | void ReadIn(std::stringstream& l_stream); 10 | void OnClick(const sf::Vector2f& l_mousePos); 11 | void OnRelease(); 12 | void OnHover(const sf::Vector2f& l_mousePos); 13 | void OnLeave(); 14 | void Update(float l_dT); 15 | void Draw(sf::RenderTarget* l_target); 16 | }; -------------------------------------------------------------------------------- /chapter_14/Client/GUI_Textfield.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "GUI_Element.h" 3 | 4 | class GUI_Textfield : public GUI_Element{ 5 | public: 6 | GUI_Textfield(const std::string& l_name, GUI_Interface* l_owner); 7 | ~GUI_Textfield(); 8 | 9 | void ReadIn(std::stringstream& l_stream); 10 | void OnClick(const sf::Vector2f& l_mousePos); 11 | void OnRelease(); 12 | void OnHover(const sf::Vector2f& l_mousePos); 13 | void OnLeave(); 14 | void Update(float l_dT); 15 | void Draw(sf::RenderTarget* l_target); 16 | }; -------------------------------------------------------------------------------- /chapter_14/Client/Main.cpp: -------------------------------------------------------------------------------- 1 | //#include 2 | #include "Game.h" 3 | 4 | void main(int argc, void** argv[]){ 5 | // Program entry point. 6 | { 7 | Game game; 8 | while(!game.GetWindow()->IsDone()){ 9 | game.Update(); 10 | game.Render(); 11 | game.LateUpdate(); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /chapter_14/Client/NetSettings.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // CLIENT 3 | #define NET_RENDER_DELAY 100 // ms. 4 | #define PLAYER_UPDATE_INTERVAL 50 // ms -------------------------------------------------------------------------------- /chapter_14/Client/Resources/Client.exe[change]: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/Client.exe[change] -------------------------------------------------------------------------------- /chapter_14/Client/Resources/audio.cfg.txt: -------------------------------------------------------------------------------- 1 | TestSound media/Audio/1_Mono.ogg 2 | Glass media/Audio/glass.ogg 3 | Footstep media/Audio/footstep.ogg 4 | Grunt media/Audio/gruntsound.ogg 5 | Swish media/Audio/swish.ogg 6 | Impact media/Audio/impact.ogg 7 | PlayerDeath media/Audio/death.ogg 8 | Electrix media/Audio/Electrix_NES.ogg 9 | TownTheme media/Audio/TownTheme.ogg -------------------------------------------------------------------------------- /chapter_14/Client/Resources/fonts.cfg.txt: -------------------------------------------------------------------------------- 1 | Main media/Fonts/Vegur-Regular.otf -------------------------------------------------------------------------------- /chapter_14/Client/Resources/keys.cfg.txt: -------------------------------------------------------------------------------- 1 | Window_close 0:0 2 | Text_Entered 4:0 3 | Fullscreen_toggle 5:89 4 | Intro_Continue 5:57 5 | Mouse_Left 9:0 6 | Mouse_Left_Release 10:0 7 | Key_Escape 5:36 8 | Key_P 5:15 9 | Key_T 5:19 10 | Key_K 5:10 11 | Key_O 5:14 12 | Key_X 5:23 13 | Player_MoveLeft 24:0 14 | Player_MoveRight 24:3 15 | Player_MoveUp 24:22 16 | Player_MoveDown 24:18 17 | Player_Attack 24:58 18 | MainMenu_Play 27:MainMenu:Play 19 | MainMenu_Disconnect 27:MainMenu:Disconnect 20 | MainMenu_Quit 27:MainMenu:Quit -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Audio/Electrix_NES.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Audio/Electrix_NES.ogg -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Audio/TownTheme.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Audio/TownTheme.ogg -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Audio/death.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Audio/death.ogg -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Audio/footstep.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Audio/footstep.ogg -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Audio/glass.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Audio/glass.ogg -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Audio/gruntsound.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Audio/gruntsound.ogg -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Audio/impact.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Audio/impact.ogg -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Audio/swish.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Audio/swish.ogg -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Entities/Player.entity: -------------------------------------------------------------------------------- 1 | Name Player 2 | Attributes 8063 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 1 5 | Component 1 Player 6 | Component 2 0 7 | Component 3 128.0 1024.0 1024.0 1 8 | Component 4 9 | Component 5 20.0 20.0 0.0 0.0 2 10 | Component 6 footstep:1,4 swish:1 impact:1 grunt:3 11 | |Component 7 12 | Component 8 13 | Component 9 5 333 2000 14 | Component 10 Player 15 | Component 11 0 -32 16 | Component 12 0 0 16 16 500 333 -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Fonts/Vegur-Regular.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Fonts/Vegur-Regular.otf -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/GUI_Interfaces/MainMenu.interface: -------------------------------------------------------------------------------- 1 | Interface MainMenu MainMenu.style 0 0 Immovable NoTitle "Main menu" 2 | Element Label Title 100 0 MainMenuTitle.style "Main menu:" 3 | Element Label IpLabel 0 32 DefaultLabel.style "IP:" 4 | Element TextField IP 18 32 MainMenuTextfield.style "127.0.0.1" 5 | Element Label PortLabel 150 32 DefaultLabel.style "Port:" 6 | Element TextField PORT 175 32 MainMenuTextfield.style "5600" 7 | Element Label NameLabel 50 56 DefaultLabel.style "Nickname:" 8 | Element TextField Nickname 105 56 MainMenuTextfield.style "Player" 9 | Element Label Play 0 80 MainMenuLabel.style "CONNECT" 10 | Element Label Disconnect 0 116 MainMenuLabel.style "DISCONNECT" 11 | Element Label Credits 0 152 MainMenuLabel.style "CREDITS" 12 | Element Label Quit 0 188 MainMenuLabel.style "EXIT" -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/GUI_Interfaces/Test.interface: -------------------------------------------------------------------------------- 1 | Interface SomeWindow TestInterface.style 50 50 Movable Title "Test Interface" 2 | Element Label TestLabel 0 0 Test.style "This is a label." 3 | Element TextField TestField 192 0 MainMenuTextfield.style "Testing lol" 4 | Element Label OtherLabel 0 320 Test2.style "This is a different label" 5 | Element Scrollbar Slider1 0 0 TestScrollbar.style Vertical -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/GUI_Styles/DefaultLabel.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 16 16 3 | BgColor 0 0 0 0 4 | TextColor 255 255 255 255 5 | TextSize 12 6 | Font Main 7 | /State -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/GUI_Styles/MainMenu.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 300 226 3 | BgColor 0 0 0 100 4 | TextSize 12 5 | Font Main 6 | /State -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/GUI_Styles/MainMenuLabel.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 300 32 3 | BgColor 255 0 0 255 4 | TextColor 255 255 255 255 5 | TextSize 14 6 | Font Main 7 | TextPadding 150 16 8 | TextOriginCenter 9 | /State 10 | 11 | State Hover 12 | BgColor 255 100 0 255 13 | /State 14 | 15 | State Clicked 16 | BgColor 255 150 0 255 17 | /State -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/GUI_Styles/MainMenuTextfield.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 128 16 3 | BgColor 50 50 50 255 4 | TextColor 255 255 255 255 5 | TextSize 14 6 | Font Main 7 | TextPadding 0 0 8 | |TextOriginCenter 9 | /State 10 | 11 | State Hover 12 | BgColor 100 100 100 255 13 | /State 14 | 15 | State Clicked 16 | BgColor 150 150 150 255 17 | /State -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/GUI_Styles/MainMenuTitle.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 118 32 3 | TextColor 255 255 255 255 4 | TextSize 24 5 | Font Main 6 | /State -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/GUI_Styles/Test.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 64 16 3 | TextColor 0 0 0 255 4 | TextSize 12 5 | Font Main 6 | TextPadding 0 0 7 | /State -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/GUI_Styles/Test2.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 64 32 3 | TextColor 0 0 0 255 4 | TextSize 12 5 | Font Main 6 | TextPadding 0 0 7 | /State 8 | 9 | State Hover 10 | TextColor 255 255 255 255 11 | /State 12 | 13 | State Clicked 14 | TextColor 255 0 0 255 15 | /State -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/GUI_Styles/TestInterface.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 450 277 3 | BgImage TestGUIBg 4 | BgImageColor 255 255 255 150 5 | TextColor 255 255 255 255 6 | TextSize 12 7 | Font Main 8 | TextPadding 0 0 9 | ElementColor 128 128 128 255 10 | /State 11 | 12 | State Hover 13 | BgImageColor 255 255 255 200 14 | /State 15 | 16 | State Clicked 17 | BgImageColor 255 255 255 200 18 | /State -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/GUI_Styles/TestScrollbar.style: -------------------------------------------------------------------------------- 1 | State Neutral 2 | Size 12 12 3 | BgColor 80 80 80 200 4 | |BgImage TestImage 5 | ElementColor 50 50 50 255 6 | |Glyph TestImage2 7 | GlyphPadding 0 0 8 | /State 9 | 10 | State Hover 11 | BgColor 155 155 155 200 12 | /State 13 | 14 | State Clicked 15 | BgColor 200 200 200 200 16 | /State -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Sounds/footstep.sound: -------------------------------------------------------------------------------- 1 | Audio Footstep 2 | Volume 25 3 | Pitch 1.0 4 | Distance 150 5 | Attenuation 2 -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Sounds/grunt.sound: -------------------------------------------------------------------------------- 1 | Audio Grunt 2 | Volume 30 3 | Pitch 1.0 4 | Distance 155 5 | Attenuation 2 -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Sounds/impact.sound: -------------------------------------------------------------------------------- 1 | Audio Impact 2 | Volume 35 3 | Pitch 1.0 4 | Distance 165 5 | Attenuation 1 -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Sounds/playerdeath.sound: -------------------------------------------------------------------------------- 1 | Audio PlayerDeath 2 | Volume 30 3 | Pitch 1.0 4 | Distance 155 5 | Attenuation 2 -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Sounds/swish.sound: -------------------------------------------------------------------------------- 1 | Audio Swish 2 | Volume 20 3 | Pitch 1.0 4 | Distance 120 5 | Attenuation 2 -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Sounds/test.sound: -------------------------------------------------------------------------------- 1 | Audio TestSound 2 | Volume 100 3 | Pitch 1.0 4 | Distance 150 5 | Attenuation 1 -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Sounds/test2.sound: -------------------------------------------------------------------------------- 1 | Audio Glass 2 | Volume 100 3 | Pitch 1.0 4 | Distance 30 5 | Attenuation 5 -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Spritesheets/Player.sheet: -------------------------------------------------------------------------------- 1 | Texture PlayerSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 0 0 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Spritesheets/Skeleton.sheet: -------------------------------------------------------------------------------- 1 | Texture SkeletonSprite 2 | Size 50 49 3 | Scale 0.75 0.75 4 | Padding 7.0 13.0 5 | Spacing 14.0 16.0 6 | |Type|Name|StartFrame|EndFrame|Row|FrameTime|FrameActionStart|FrameActionEnd| 7 | AnimationType Directional 8 | Animation Idle 8 8 0 1.0 -1 -1 9 | Animation Walk 1 7 0 0.1 -1 -1 10 | Animation Attack 0 5 4 0.06 -1 -1 11 | Animation SpellCast 0 6 8 0.1 2 3 12 | Animation Death 0 5 12 0.1 -1 -1 -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Textures/ADOM-OGA-UI.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Textures/ADOM-OGA-UI.png -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Textures/HeartBar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Textures/HeartBar.png -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Textures/PlayerSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Textures/PlayerSheet.png -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Textures/SkeletonSheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Textures/SkeletonSheet.png -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Textures/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Textures/intro.png -------------------------------------------------------------------------------- /chapter_14/Client/Resources/media/Textures/tilesheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/media/Textures/tilesheet.png -------------------------------------------------------------------------------- /chapter_14/Client/Resources/openal32.dll[change]: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Client/Resources/openal32.dll[change] -------------------------------------------------------------------------------- /chapter_14/Client/Resources/textures.cfg.txt: -------------------------------------------------------------------------------- 1 | Intro media/Textures/intro.png 2 | PlayerSprite media/Textures/PlayerSheet.png 3 | SkeletonSprite media/Textures/SkeletonSheet.png 4 | TileSheet media/Textures/tilesheet.png 5 | TestGUIBg media/Textures/ADOM-OGA-UI.png 6 | HeartBar media/Textures/HeartBar.png -------------------------------------------------------------------------------- /chapter_14/Client/S_CharacterUI.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "Window.h" 4 | #include "C_UI_Element.h" 5 | #include "C_Health.h" 6 | #include "C_Name.h" 7 | #include "Client_System_Manager.h" 8 | 9 | class S_CharacterUI : public S_Base{ 10 | public: 11 | S_CharacterUI(SystemManager* l_systemMgr); 12 | ~S_CharacterUI(); 13 | 14 | void Update(float l_dT); 15 | void HandleEvent(const EntityId& l_entity, const EntityEvent& l_event); 16 | void Notify(const Message& l_message); 17 | 18 | void Render(Window* l_wind); 19 | private: 20 | sf::Sprite m_heartBar; 21 | sf::Text m_nickname; 22 | sf::RectangleShape m_nickbg; 23 | sf::Vector2u m_heartBarSize; 24 | }; -------------------------------------------------------------------------------- /chapter_14/Client/S_Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "Window.h" 5 | #include "Directions.h" 6 | #include "S_Base.h" 7 | #include "C_Position.h" 8 | #include "C_Drawable.h" 9 | 10 | class S_Renderer : public S_Base{ 11 | public: 12 | S_Renderer(SystemManager* l_systemMgr); 13 | ~S_Renderer(); 14 | 15 | void Update(float l_dT); 16 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 17 | void Notify(const Message& l_message); 18 | void Render(Window* l_wind, unsigned int l_layer); 19 | 20 | void SortDrawables(); 21 | private: 22 | void SetSheetDirection(const EntityId& l_entity, const Direction& l_dir); 23 | }; -------------------------------------------------------------------------------- /chapter_14/Client/S_SheetAnimation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "C_State.h" 4 | 5 | class S_SheetAnimation : public S_Base{ 6 | public: 7 | S_SheetAnimation(SystemManager* l_systemMgr); 8 | ~S_SheetAnimation(); 9 | 10 | void Update(float l_dT); 11 | void HandleEvent(const EntityId& l_entity,const EntityEvent& l_event); 12 | void Notify(const Message& l_message); 13 | private: 14 | void ChangeAnimation(const EntityId& l_entity, 15 | const std::string& l_anim, bool l_play, bool l_loop); 16 | }; -------------------------------------------------------------------------------- /chapter_14/Client/S_Sound.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "C_SoundEmitter.h" 4 | #include "C_SoundListener.h" 5 | #include "C_State.h" 6 | 7 | class S_Sound : public S_Base{ 8 | public: 9 | S_Sound(SystemManager* l_systemMgr); 10 | ~S_Sound(); 11 | 12 | void Update(float l_dT); 13 | void HandleEvent(const EntityId& l_entity, const EntityEvent& l_event); 14 | void Notify(const Message& l_message); 15 | 16 | void SetUp(AudioManager* l_audioManager, SoundManager* l_soundManager); 17 | private: 18 | sf::Vector3f MakeSoundPosition(const sf::Vector2f& l_entityPos, unsigned int l_elevation); 19 | void EmitSound(const EntityId& l_entity, const EntitySound& l_sound, 20 | bool l_useId, bool l_relative, int l_checkFrame = -1); 21 | AudioManager* m_audioManager; 22 | SoundManager* m_soundManager; 23 | }; -------------------------------------------------------------------------------- /chapter_14/Client/SoundProps.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | struct SoundProps{ 3 | SoundProps(const std::string& l_name): m_audioName(l_name),m_volume(100), m_pitch(1.f), 4 | m_minDistance(10.f), m_attenuation(10.f){} 5 | std::string m_audioName; 6 | float m_volume; 7 | float m_pitch; 8 | float m_minDistance; 9 | float m_attenuation; 10 | }; -------------------------------------------------------------------------------- /chapter_14/Client/State_GameOver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | 4 | class State_GameOver : public BaseState{ 5 | public: 6 | State_GameOver(StateManager* l_stateManager); 7 | ~State_GameOver(); 8 | 9 | void OnCreate(); 10 | void OnDestroy(); 11 | 12 | void Activate(); 13 | void Deactivate(); 14 | 15 | void Update(const sf::Time& l_time); 16 | void Draw(); 17 | private: 18 | sf::Text m_text; 19 | float m_elapsed; 20 | }; -------------------------------------------------------------------------------- /chapter_14/Client/State_Intro.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Intro : public BaseState{ 6 | public: 7 | State_Intro(StateManager* l_stateManager); 8 | ~State_Intro(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Continue(EventDetails* l_details); 20 | private: 21 | sf::Sprite m_introSprite; 22 | sf::Text m_text; 23 | }; -------------------------------------------------------------------------------- /chapter_14/Client/State_MainMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_MainMenu : public BaseState{ 6 | public: 7 | State_MainMenu(StateManager* l_stateManager); 8 | ~State_MainMenu(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Play(EventDetails* l_details); 20 | void Disconnect(EventDetails* l_details); 21 | void Quit(EventDetails* l_details); 22 | private: 23 | 24 | }; -------------------------------------------------------------------------------- /chapter_14/Client/State_Paused.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "BaseState.h" 3 | #include "EventManager.h" 4 | 5 | class State_Paused : public BaseState{ 6 | public: 7 | State_Paused(StateManager* l_stateManager); 8 | ~State_Paused(); 9 | 10 | void OnCreate(); 11 | void OnDestroy(); 12 | 13 | void Activate(); 14 | void Deactivate(); 15 | 16 | void Update(const sf::Time& l_time); 17 | void Draw(); 18 | 19 | void Unpause(EventDetails* l_details); 20 | private: 21 | sf::Text m_text; 22 | sf::RectangleShape m_rect; 23 | }; -------------------------------------------------------------------------------- /chapter_14/Client/TextureManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ResourceManager.h" 3 | #include 4 | 5 | class TextureManager : public ResourceManager{ 6 | public: 7 | TextureManager() : ResourceManager("textures.cfg"){} 8 | 9 | sf::Texture* Load(const std::string& l_path){ 10 | sf::Texture* texture = new sf::Texture(); 11 | if(!texture->loadFromFile( 12 | Utils::GetWorkingDirectory() + l_path)) 13 | { 14 | delete texture; 15 | texture = nullptr; 16 | std::cerr << "! Failed to load texture: " << l_path << std::endl; 17 | } 18 | return texture; 19 | } 20 | }; -------------------------------------------------------------------------------- /chapter_14/Server/NetSettings.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // SERVER 3 | #define SNAPSHOT_INTERVAL 100 -------------------------------------------------------------------------------- /chapter_14/Server/Resources/Server.exe[change]: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Server/Resources/Server.exe[change] -------------------------------------------------------------------------------- /chapter_14/Server/Resources/media/Entities/Player.entity: -------------------------------------------------------------------------------- 1 | Name Player 2 | Attributes 5949 3 | |Component|ID|Individual attributes| 4 | Component 0 0 0 1 5 | Component 2 0 6 | Component 3 128.0 1024.0 1024.0 1 7 | Component 4 8 | Component 5 20.0 20.0 0.0 0.0 2 9 | Component 8 10 | Component 9 5 333 2000 11 | Component 10 Player 12 | Component 12 0 0 16 16 500 333 -------------------------------------------------------------------------------- /chapter_14/Server/S_Combat.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "C_Position.h" 4 | #include "C_Attacker.h" 5 | #include "C_Health.h" 6 | #include "S_State.h" 7 | #include "C_Movable.h" 8 | 9 | class S_Combat : public S_Base{ 10 | public: 11 | S_Combat(SystemManager* l_systemMgr); 12 | ~S_Combat(); 13 | 14 | void Update(float l_dT); 15 | void HandleEvent(const EntityId& l_entity, const EntityEvent& l_event); 16 | void Notify(const Message& l_message); 17 | private: 18 | 19 | }; -------------------------------------------------------------------------------- /chapter_14/Server/S_Timers.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "C_Attacker.h" 4 | #include "C_Health.h" 5 | #include "S_State.h" 6 | 7 | class S_Timers : public S_Base{ 8 | public: 9 | S_Timers(SystemManager* l_systemMgr); 10 | ~S_Timers(); 11 | 12 | void Update(float l_dT); 13 | void HandleEvent(const EntityId& l_entity, const EntityEvent& l_event); 14 | void Notify(const Message& l_message); 15 | private: 16 | }; -------------------------------------------------------------------------------- /chapter_14/Server/Server_Entity_Manager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Entity_Manager.h" 3 | #include "C_Position.h" 4 | #include "C_State.h" 5 | #include "C_Movable.h" 6 | #include "C_Collidable.h" 7 | #include "C_Controller.h" 8 | #include "C_Client.h" 9 | #include "C_Health.h" 10 | #include "C_Name.h" 11 | #include "C_Attacker.h" 12 | #include "EntitySnapshot.h" 13 | 14 | class SystemManager; 15 | 16 | class ServerEntityManager : public EntityManager{ 17 | public: 18 | ServerEntityManager(SystemManager* l_sysMgr); 19 | ~ServerEntityManager(); 20 | 21 | void DumpEntityInfo(sf::Packet& l_packet); 22 | private: 23 | }; -------------------------------------------------------------------------------- /chapter_14/Server/Server_Main.cpp: -------------------------------------------------------------------------------- 1 | //#include 2 | #include 3 | #include 4 | #include "World.h" 5 | 6 | int main(){ 7 | World world; 8 | sf::Clock clock; 9 | clock.restart(); 10 | 11 | while (world.IsRunning()){ 12 | world.Update(clock.restart()); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /chapter_14/Server/Server_System_Manager.cpp: -------------------------------------------------------------------------------- 1 | #include "Server_System_Manager.h" 2 | 3 | ServerSystemManager::ServerSystemManager(){ 4 | AddSystem(System::Network); 5 | AddSystem(System::State); 6 | AddSystem(System::Control); 7 | AddSystem(System::Movement); 8 | AddSystem(System::Timers); 9 | AddSystem(System::Collision); 10 | AddSystem(System::Combat); 11 | } 12 | ServerSystemManager::~ServerSystemManager(){} -------------------------------------------------------------------------------- /chapter_14/Server/Server_System_Manager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "System_Manager.h" 3 | #include "S_Movement.h" 4 | #include "S_Collision.h" 5 | #include "S_State.h" 6 | #include "S_Control.h" 7 | #include "S_Network.h" 8 | #include "S_Combat.h" 9 | #include "S_Timers.h" 10 | 11 | class ServerSystemManager : public SystemManager{ 12 | public: 13 | ServerSystemManager(); 14 | ~ServerSystemManager(); 15 | private: 16 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/C_Base.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "ECS_Types.h" 5 | 6 | class C_Base{ 7 | public: 8 | C_Base(const Component& l_type) : m_type(l_type){} 9 | virtual ~C_Base(){} 10 | 11 | Component GetType(){ return m_type; } 12 | 13 | friend std::stringstream& operator >>( 14 | std::stringstream& l_stream, C_Base& b) 15 | { 16 | b.ReadIn(l_stream); 17 | return l_stream; 18 | } 19 | 20 | virtual void ReadIn(std::stringstream& l_stream) = 0; 21 | protected: 22 | Component m_type; 23 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/C_Client.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | #include "NetworkDefinitions.h" 4 | 5 | class C_Client : public C_Base{ 6 | public: 7 | C_Client(): C_Base(Component::Client), m_clientID((ClientID)Network::NullID){} 8 | void ReadIn(std::stringstream& l_stream){} 9 | 10 | ClientID GetClientID()const{ return m_clientID; } 11 | void SetClientID(const ClientID& l_id){ m_clientID = l_id; } 12 | private: 13 | ClientID m_clientID; 14 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/C_Controller.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | class C_Controller : public C_Base{ 5 | public: 6 | C_Controller() : C_Base(Component::Controller){} 7 | void ReadIn(std::stringstream& l_stream){} 8 | private: 9 | 10 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/C_Name.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | #include 4 | 5 | class C_Name : public C_Base{ 6 | public: 7 | C_Name() : C_Base(Component::Name){} 8 | void ReadIn(std::stringstream& l_stream){ l_stream >> m_name; } 9 | const std::string& GetName()const{ return m_name; } 10 | void SetName(const std::string& l_name){ m_name = l_name; } 11 | private: 12 | std::string m_name; 13 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/C_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | 4 | enum class EntityState{ Idle, Walking, Attacking, Hurt, Dying }; 5 | 6 | class C_State : public C_Base{ 7 | public: 8 | C_State() : C_Base(Component::State){} 9 | void ReadIn(std::stringstream& l_stream){ 10 | unsigned int state = 0; 11 | l_stream >> state; 12 | m_state = (EntityState)state; 13 | } 14 | 15 | EntityState GetState(){ return m_state; } 16 | void SetState(const EntityState& l_state){ 17 | m_state = l_state; 18 | } 19 | private: 20 | EntityState m_state; 21 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/C_TimedComponentBase.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "C_Base.h" 3 | #include 4 | 5 | class C_TimedComponentBase : public C_Base{ 6 | public: 7 | C_TimedComponentBase(const Component& l_type) 8 | : C_Base(l_type), m_duration(sf::milliseconds(0)){} 9 | virtual ~C_TimedComponentBase(){} 10 | 11 | const sf::Time& GetTimer()const{ return m_duration; } 12 | void SetTimer(const sf::Time& l_time){ m_duration = l_time; } 13 | void AddToTimer(const sf::Time& l_time){ m_duration += l_time; } 14 | 15 | void Reset(){ m_duration = sf::milliseconds(0); } 16 | protected: 17 | sf::Time m_duration; 18 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/Directions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | enum class Direction{ Up = 0, Left, Down, Right }; -------------------------------------------------------------------------------- /chapter_14/Shared/ECS_Types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using ComponentType = unsigned int; 3 | #define N_COMPONENT_TYPES 32 4 | 5 | enum class Component{ 6 | Position = 0, SpriteSheet, State, Movable, Controller, Collidable, SoundEmitter, SoundListener, 7 | Client, Health, Name, UI_Element, Attacker 8 | }; 9 | 10 | enum class System{ 11 | Renderer = 0, Movement, Collision, Control, State, SheetAnimation, Sound, Network, Combat, 12 | Character_UI, Timers 13 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/EntityEvents.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityEvent{ 4 | Spawned, Despawned, Colliding_X, Colliding_Y, 5 | Moving_Left, Moving_Right, Moving_Up, Moving_Down, 6 | Elevation_Change, Became_Idle, Began_Moving, Began_Attacking 7 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/EntityMessages.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class EntityMessage{ 4 | Move, Is_Moving, Frame_Change, State_Changed, 5 | Direction_Changed, Switch_State, Attack, 6 | Being_Attacked, Hurt, Die, Respawn, Removed_Entity 7 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/EntitySnapshot.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | struct EntitySnapshot{ 6 | std::string m_type; 7 | sf::Vector2f m_position; 8 | sf::Int32 m_elevation; 9 | sf::Vector2f m_velocity; 10 | sf::Vector2f m_acceleration; 11 | sf::Uint8 m_direction; 12 | sf::Uint8 m_state; 13 | sf::Uint8 m_health; 14 | std::string m_name; 15 | }; 16 | 17 | sf::Packet& operator <<(sf::Packet& l_packet, const EntitySnapshot& l_snapshot); 18 | sf::Packet& operator >>(sf::Packet& l_packet, EntitySnapshot& l_snapshot); -------------------------------------------------------------------------------- /chapter_14/Shared/Event_Queue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | using EventID = unsigned int; 5 | 6 | class EventQueue{ 7 | public: 8 | void AddEvent(const EventID& l_event){ m_queue.push(l_event); } 9 | 10 | bool ProcessEvents(EventID& l_id){ 11 | if (m_queue.empty()){ return false; } 12 | l_id = m_queue.front(); 13 | m_queue.pop(); 14 | return true; 15 | } 16 | 17 | void Clear(){ while (!m_queue.empty()){ m_queue.pop(); } } 18 | private: 19 | std::queue m_queue; 20 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/Main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sschellhoff/SFMLGameDevelopmentByExample/5dbe6f231ce255df811db2a320d98558df3e93a0/chapter_14/Shared/Main.cpp -------------------------------------------------------------------------------- /chapter_14/Shared/Message.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | using MessageType = unsigned int; 3 | struct TwoFloats{ float m_x; float m_y; }; 4 | 5 | struct Message{ 6 | Message(const MessageType& l_type) : m_type(l_type){} 7 | 8 | MessageType m_type; 9 | int m_sender; 10 | int m_receiver; 11 | 12 | union{ 13 | TwoFloats m_2f; 14 | bool m_bool; 15 | int m_int; 16 | }; 17 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/MessageHandler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Communicator.h" 3 | #include "EntityMessages.h" 4 | #include 5 | 6 | using Subscribtions = std::unordered_map; 7 | 8 | class MessageHandler{ 9 | public: 10 | bool Subscribe(const EntityMessage& l_type, Observer* l_observer) 11 | { 12 | return m_communicators[l_type].AddObserver(l_observer); 13 | } 14 | 15 | bool Unsubscribe(const EntityMessage& l_type, Observer* l_observer) 16 | { 17 | return m_communicators[l_type].RemoveObserver(l_observer); 18 | } 19 | 20 | void Dispatch(const Message& l_msg){ 21 | auto itr = m_communicators.find((EntityMessage)l_msg.m_type); 22 | if (itr == m_communicators.end()){ return; } 23 | itr->second.Broadcast(l_msg); 24 | } 25 | private: 26 | Subscribtions m_communicators; 27 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/NetworkDefinitions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | enum class Network{ 3 | HighestTimestamp = 2147483647, ClientTimeout = 10000, ServerPort = 5600, 4 | NullID = -1, PlayerUpdateDelim = -1 5 | }; 6 | 7 | using ClientID = int; 8 | using PortNumber = unsigned short; -------------------------------------------------------------------------------- /chapter_14/Shared/Observer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Message.h" 3 | 4 | class Observer{ 5 | public: 6 | virtual ~Observer(){} 7 | virtual void Notify(const Message& l_message) = 0; 8 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/PacketTypes.cpp: -------------------------------------------------------------------------------- 1 | #include "PacketTypes.h" 2 | void StampPacket(const PacketType& l_type, sf::Packet& l_packet){ 3 | l_packet << PacketID(l_type); 4 | } -------------------------------------------------------------------------------- /chapter_14/Shared/PacketTypes.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | using PacketID = sf::Int8; 4 | enum class PacketType{ 5 | Disconnect = -1, Connect, Heartbeat, Snapshot, PlayerUpdate, Message, Hurt, OutOfBounds 6 | }; 7 | 8 | void StampPacket(const PacketType& l_type, sf::Packet& l_packet); -------------------------------------------------------------------------------- /chapter_14/Shared/S_Control.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "Directions.h" 4 | #include "C_Movable.h" 5 | 6 | class S_Control : public S_Base{ 7 | public: 8 | S_Control(SystemManager* l_systemMgr); 9 | ~S_Control(); 10 | 11 | void Update(float l_dT); 12 | void HandleEvent(const EntityId& l_entity, const EntityEvent& l_event); 13 | void Notify(const Message& l_message); 14 | void MoveEntity(const EntityId& l_entity, const Direction& l_dir); 15 | private: 16 | }; -------------------------------------------------------------------------------- /chapter_14/Shared/S_State.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "S_Base.h" 3 | #include "C_State.h" 4 | #include "Directions.h" 5 | 6 | class S_State : public S_Base{ 7 | public: 8 | S_State(SystemManager* l_systemMgr); 9 | ~S_State(); 10 | 11 | void Update(float l_dT); 12 | void HandleEvent(const EntityId& l_entity, const EntityEvent& l_event); 13 | void Notify(const Message& l_message); 14 | 15 | void ChangeState(const EntityId& l_entity, const EntityState& l_state, bool l_force); 16 | EntityState GetState(const EntityId& l_entity); 17 | private: 18 | }; --------------------------------------------------------------------------------