
- •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 8
float ratio = particle.lifetime.asSeconds()
/ Table[mType].lifetime.asSeconds();
c.a = static_cast<sf::Uint8>(255 * std::max(ratio, 0.f));
Now the interesting part: we add four vertices for each particle, one in every corner of our rectangle. The first two arguments denote the target coordinates; the next two denote the texture coordinates. The fifth argument is the vertex color. Since we need no gradient inside a particle, the color is uniform for all four vertices.
addVertex(pos.x - half.x, pos.y - half.y, 0.f, |
0.f, |
c); |
||
addVertex(pos.x + half.x, pos.y - half.y, size.x, |
0.f, |
c); |
||
addVertex(pos.x + |
half.x, pos.y + |
half.y, size.x, |
size.y, c); |
|
addVertex(pos.x - |
half.x, pos.y + |
half.y, 0.f, |
size.y, c); |
}
The function to add vertices itself is not very interesting—it builds sf::Vertex and adds it to sf::VertexArray:
void ParticleNode::addVertex(float worldX, float worldY, float texCoordX, float texCoordY, const sf::Color& color) const
{
sf::Vertex vertex;
vertex.position = sf::Vector2f(worldX, worldY); vertex.texCoords = sf::Vector2f(texCoordX, texCoordY); vertex.color = color;
mVertexArray.append(vertex);
}
Emitter nodes
Because particles should be emitted in the places where the missiles are located, it stands to reason that emitters should be attached to missiles. Once more, our scene graph comes in very handy: we can create a new scene node EmitterNode for emitters and attach it to the Projectile node of the missile.
EmitterNode is rather simple, its class definition is shown in the following code snippet:
class EmitterNode : public SceneNode
{
public: |
|
|
|
explicit |
EmitterNode(Particle::Type type); |
||
... |
|
|
|
|
|
|
|
|
|
[ 197 ] |
|
|
|
|
www.it-ebooks.info

Every Pixel Counts – Adding Visual Effects
private:
sf::Time mAccumulatedTime; Particle::Type mType; ParticleNode* mParticleSystem;
};
The pointer mParticleSystem points to the ParticleNode into which the EmitterNode emits particles. Initially, it is nullptr. In the update function, we emit particles if the particle system has already been initialized. Otherwise, we need to find the system corresponding to the emitter. "Corresponding" means both use the same particle type, for example, Particle::Smoke. We send a command through the scene graph to find the right particle system. It sets the member variable
mParticleSystem to the found ParticleNode:
void EmitterNode::updateCurrent(sf::Time dt, CommandQueue& commands)
{
if (mParticleSystem)
{
emitParticles(dt);
}
else
{
auto finder = [this] (ParticleNode& container, sf::Time)
{
if (container.getParticleType() == mType) mParticleSystem = &container;
};
Command command;
command.category = Category::ParticleSystem; command.action = derivedAction<ParticleNode>(finder);
commands.push(command);
}
}
After the emitter has been linked to a particle system, the method to emit particles becomes interesting. We set an emission rate and try to achieve it as closely as possible. Since this is not usually equal to our logic frame rate, the amount of emitted particles per frame differs. To cope with that problem, we again use accumulators, as we did for the logic game loop in Chapter 1, Making a Game Tick. We emit particles as long as the emission interval still fits into the current frame. The remaining time is stored in mAccumulatedTime and is carried over to the next frame.
[ 198 ]
www.it-ebooks.info