
- •Credits
- •Foreword
- •About the Authors
- •About the Reviewers
- •www.PacktPub.com
- •Table of Contents
- •Preface
- •Introducing SFML
- •Downloading and installation
- •A minimal example
- •A few notes on C++
- •Developing the first game
- •The Game class
- •Game loops and frames
- •Input over several frames
- •Vector algebra
- •Frame-independent movement
- •Fixed time steps
- •Other techniques related to frame rates
- •Displaying sprites on the screen
- •File paths and working directories
- •Real-time rendering
- •Adapting the code
- •Summary
- •Defining resources
- •Resources in SFML
- •Textures
- •Images
- •Fonts
- •Shaders
- •Sound buffers
- •Music
- •A typical use case
- •Graphics
- •Audio
- •Acquiring, releasing, and accessing resources
- •An automated approach
- •Finding an appropriate container
- •Loading from files
- •Accessing the textures
- •Error handling
- •Boolean return values
- •Throwing exceptions
- •Assertions
- •Generalizing the approach
- •Compatibility with sf::Music
- •A special case – sf::Shader
- •Summary
- •Entities
- •Aircraft
- •Alternative entity designs
- •Rendering the scene
- •Relative coordinates
- •SFML and transforms
- •Scene graphs
- •Scene nodes
- •Node insertion and removal
- •Making scene nodes drawable
- •Drawing entities
- •Connecting entities with resources
- •Aligning the origin
- •Scene layers
- •Updating the scene
- •One step back – absolute transforms
- •The view
- •Viewport
- •View optimizations
- •Resolution and aspect ratio
- •View scrolling
- •Zoom and rotation
- •Landscape rendering
- •SpriteNode
- •Landscape texture
- •Texture repeating
- •Composing our world
- •World initialization
- •Loading the textures
- •Building the scene
- •Update and draw
- •Integrating the Game class
- •Summary
- •Polling events
- •Window events
- •Joystick events
- •Keyboard events
- •Mouse events
- •Getting the input state in real time
- •Events and real-time input – when to use which
- •Delta movement from the mouse
- •Playing nice with your application neighborhood
- •A command-based communication system
- •Introducing commands
- •Receiver categories
- •Command execution
- •Command queues
- •Handling player input
- •Commands in a nutshell
- •Implementing the game logic
- •A general-purpose communication mechanism
- •Customizing key bindings
- •Why a player is not an entity
- •Summary
- •Defining a state
- •The state stack
- •Adding states to StateStack
- •Handling updates, input, and drawing
- •Input
- •Update
- •Draw
- •Delayed pop/push operations
- •The state context
- •Integrating the stack in the Application class
- •Navigating between states
- •Creating the game state
- •The title screen
- •Main menu
- •Pausing the game
- •The loading screen – sample
- •Progress bar
- •ParallelTask
- •Thread
- •Concurrency
- •Task implementation
- •Summary
- •The GUI hierarchy, the Java way
- •Updating the menu
- •The promised key bindings
- •Summary
- •Equipping the entities
- •Introducing hitpoints
- •Storing entity attributes in data tables
- •Displaying text
- •Creating enemies
- •Movement patterns
- •Spawning enemies
- •Adding projectiles
- •Firing bullets and missiles
- •Homing missiles
- •Picking up some goodies
- •Collision detection and response
- •Finding the collision pairs
- •Reacting to collisions
- •An outlook on optimizations
- •An interacting world
- •Cleaning everything up
- •Out of view, out of the world
- •The final update
- •Victory and defeat
- •Summary
- •Defining texture atlases
- •Adapting the game code
- •Low-level rendering
- •OpenGL and graphics cards
- •Understanding render targets
- •Texture mapping
- •Vertex arrays
- •Particle systems
- •Particles and particle types
- •Particle nodes
- •Emitter nodes
- •Affectors
- •Embedding particles in the world
- •Animated sprites
- •The Eagle has rolled!
- •Post effects and shaders
- •Fullscreen post effects
- •Shaders
- •The bloom effect
- •Summary
- •Music themes
- •Loading and playing
- •Use case – In-game themes
- •Sound effects
- •Loading, inserting, and playing
- •Removing sounds
- •Use case – GUI sounds
- •Sounds in 3D space
- •The listener
- •Attenuation factor and minimum distance
- •Positioning the listener
- •Playing spatial sounds
- •Use case – In-game sound effects
- •Summary
- •Playing multiplayer games
- •Interacting with sockets
- •Socket selectors
- •Custom protocols
- •Data transport
- •Network architectures
- •Peer-to-peer
- •Client-server architecture
- •Authoritative servers
- •Creating the structure for multiplayer
- •Working with the Server
- •Server thread
- •Server loop
- •Peers and aircraft
- •Hot Seat
- •Accepting new clients
- •Handling disconnections
- •Incoming packets
- •Studying our protocol
- •Understanding the ticks and updates
- •Synchronization issues
- •Taking a peek in the other end – the client
- •Client packets
- •Transmitting game actions via network nodes
- •The new pause state
- •Settings
- •The new Player class
- •Latency
- •Latency versus bandwidth
- •View scrolling compensation
- •Aircraft interpolation
- •Cheating prevention
- •Summary
- •Index

Chapter 3
The update function controls world scrolling and entity movement. First, the view is scrolled according to the passed time. Next, we check if the player's aircraft reaches a certain distance (150) from the world's borders, and flip its x velocity in this case.
This results in the plane moving back, until it reaches the other border, and the procedure repeats. Eventually, we forward the update to the scene graph, which actually applies the velocities.
void World::update(sf::Time dt)
{
mWorldView.move(0.f, mScrollSpeed * dt.asSeconds());
sf::Vector2f position = mPlayerAircraft->getPosition(); sf::Vector2f velocity = mPlayerAircraft->getVelocity();
if (position.x <= mWorldBounds.left + 150
|| position.x >= mWorldBounds.left + mWorldBounds.width - 150)
{
velocity.x = -velocity.x; mPlayerAircraft->setVelocity(velocity);
}
mSceneGraph.update(dt);
}
Integrating the Game class
By now we already know how the Game class works, what a game loop is for, and how to take advantage of it. For this chapter, we take the previously used Game class, and plug into it our newcomer World class.
Because we obeyed a few principles, this integration is very easy and it's just a matter of having a World object inside the Game class, and then letting it update and draw itself in the appropriate times.
The run() method
Our application's main() function has a simple job. It allocates a Game object, and lets it run itself through the run() method. When the run() method exits, the program releases its resources and closes.
Therefore, it is within the run() method that the magic happens! It is responsible for managing the famous game loop, fetching input from the window system, updating the world, and ordering the rendering of the game.
[ 81 ]
www.it-ebooks.info

Forge of the Gods – Shaping Our World
In the next chapter, events and input will be covered in depth. For now, it is
only important to understand how the drawing sequence happens inside the run() method:
mWindow.clear();
mWorld.draw();
mWindow.setView(mWindow.getDefaultView());
mWindow.draw(mStatisticsText);
mWindow.display();
This code closely resembles the high-level drawing of a frame for any game. The function call mWindow.clear() ensures that our frame buffer, the canvas where the frame will be drawn, is cleaned up before we start drawing. Then, we let the world draw itself, where it defines its own view and orders the scene graph to render itself. Finally, because we want to display some text in the corner of the screen for statistical purposes, we activate the default view, which is always the same so the text always appears fixed on the screen.
On the mWindow.display() call, we tell SFML we are done drawing the frame and it proceeds to upload it to our screen right away.
From this moment, you should be able to better dissect a game's mechanics and visualize its structure in your mind, mapping it directly to a data structure in C++. We can use the mental toolbox learned in this chapter to help us lay out our thoughts into actual computational data. This hypothetical toolbox contains many tools: managing entities and their transformations, scene graphs, views as cameras, graphical tricks, and others. But more importantly, we have started to understand how to properly glue all these elements together in a harmonious simulation. Furthermore, you have also seen many smaller techniques, which you may or may
not have known, for example, practical use cases of C++11 features. So far, our result looks as shown in the following screenshot:
[ 82 ]
www.it-ebooks.info