- •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
www.it-ebooks.info
SFML Game Development
Learn how to use SFML 2.0 to develop your own feature-packed game
Jan Haller
Henrik Vogelius Hansson
Artur Moreira
BIRMINGHAM - MUMBAI
www.it-ebooks.info
SFML Game Development
Copyright © 2013 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: June 2013
Production Reference: 1170613
Published by Packt Publishing Ltd. Livery Place
35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-84969-684-5
www.packtpub.com
Cover Image by Vivek Sinha (vivek.ratan.sinha@gmail.com)
www.it-ebooks.info
Credits
Authors
Artur Moreira
Henrik Vogelius Hansson
Jan Haller
Reviewers
Brandon DeRosier
Karol Gasiński
Eyal Kalderon
Acquisition Editor
Edward Gordon
Lead Technical Editor
Mayur Hule
Technical Editors
Sanhita Sawant
Priya Singh
Nitee Shetty
Dennis John
Project Coordinator
Amey Sawant
Proofreaders
Dan McMahon
Stephen Swaney
Indexer
Monica Ajmera Mehta
Graphics
Ronak Dhruv
Abhinash Sahu
Production Coordinator
Pooja Chiplunkar
Cover Work
Pooja Chiplunkar
www.it-ebooks.info
Foreword
I'm really honored to write the first words of this book, the very first one about SFML.
When I started to write this library, in 2006, I couldn't imagine that it would become so much popular. Around 100,000 visitors per month, 100 new forum posts everyday...
this is huge! And this is just the beginning; with the release of SFML 2.0, the library makes an important step forward. While SFML 1 is a constantly evolving (understand "unstable") API, SFML 2.0 is meant to be a robust, stable, and mature foundation for the future. And hopefully a solid base for more and more great games.
Increasing popularity also means more effort from the authors to provide complete and quality documentation. Sadly, this is often overlooked by developers.
Although I do my best to provide detailed tutorials for SFML, they are nothing more than an improved API documentation. Users often ask me where they can find a more complete guide to start programming games with SFML. And until today,
I had nowhere to direct them to. But Artur, Henrik, and Jan did a great job to fill this hole. They have gathered their wisdom and experience into a unique book that covers the basics of game programming, as well as everything that SFML has to offer—from audio to network, through advanced graphics. What I really appreciate about this book is that it is written with the same philosophy that is behind SFML: good design, simplicity, and modern code.
I hope you will enjoy reading this book as much as I did and find what you're looking for; whether you're a beginner who wants to learn game programming with SFML, or a more experienced programmer who wants to improve his design and technical skills. And don't hesitate to come to the SFML forum if you have questions or comments. I, the authors of this book, and all the community will be glad to answer you.
Laurent Gomila
Author of SFML
Software Engineer, Tegesoft, France
www.it-ebooks.info
About the Authors
Artur Moreira is a game development enthusiast who ultimately aims to start a new game development company in his home country. He has been programming games and game-related software for over 4 years. Most of the effort in that time was put in creating an open source game-making library with lots of flexibility and portability. The library is called Nephilim and is known for supporting all major desktop and mobile operating systems, making game development fully crossplatform and fast. Alongside this big project, he keeps making his own prototypes and games for educational and commercial purposes.
Aside from the programming, he also puts some focus in creative areas such as 3D modeling, digital painting, and music composing.
I would like to thank, first of all, my girlfriend for her patience and unconditional support every single time the writing was taking all of my time and energy. Also, I can't be thankful enough for
the support of my parents and sisters, along with all the closest relatives, whose support is ever-present and always helpful. On the technical side, I would like to directly thank Jan Haller and Henrik Vogelius Hansson, the co-authors of the book, for their remarkable collaboration, skill, and teamwork. It's been always a pleasure to work with them.
www.it-ebooks.info
Henrik Vogelius Hansson has always been in love with both games and programming. He started his education fairly early and continued on into the indie scene with Defrost Games and their game Project Temporality. The next company that hired him was Paradox Development Studio where he got to work on titles such as
Crusader Kings 2.
Beside the game companies, Henrik has also been very active in the SFML community and has even provided a binding for Ruby called rbSFML.
I would like to thank my co-authors, Jan and Artur, for the amazing cooperation and great times. I am also very happy and would like to thank my family that encouraged me to pursue this line of work. Also special thanks to my grandmother.
Jan Haller is a Master's degree student of Electrical Engineering and Information Technology. In his free time, he occasionally develops games in C++. He is also interested in the creation of graphics and 3D models.
In 2008, Jan stumbled upon SFML. Since then, he has used the library for many of his works. One of his bigger projects was a 2D Jump'n'Run game, where the character has to find its way through different levels, defeating enemies and collecting various items. During the years, Jan has actively participated in the SFML community, getting a lot of insights into the development of SFML 2. He has also written a C++ library called Thor, which extends SFML by ready-to-use features such as particle systems or animations.
I would like to thank Laurent Gomila for the passion and huge efforts he invested into the Simple and Fast Multimedia Library. It has always been interesting to discuss with him about the library and its development process. I would also like to thank my coauthors, Artur Moreira and Henrik Vogelius Hansson, who have been a very nice team to work with.
www.it-ebooks.info