SFML Game Development By Example Create and develop exciting games from start to finish using SFML Raimondas Pupius BIRMINGHAM - MUMBAI SFML Game Development By Example Copyright © 2015 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 author, 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: December 2015 Production reference: 1181215 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78528-734-3 www.packtpub.com Credits Author Project Coordinator Raimondas Pupius Sanjeet Rao Reviewers Proofreader Tom Ivanyo Safis Editing Vittorio Romeo Richa Sachdeva Indexer Priya Sane Commissioning Editor Dipika Gaonkar Graphics Kirk D'Penha Acquisition Editors Prachi Bisht Production Coordinator Aparna Bhagat Usha Iyer Cover Work Content Development Editor Aparna Bhagat Mamata Walker Technical Editor Pramod Kumavat Copy Editors Ting Baker Kevin McGowan About the Author Raimondas Pupius is a game development enthusiast from Lithuania. He is currently working towards getting a degree in software engineering as well as working on a few projects of his own. Starting his unofficial education in this field at the age of 9, and having been introduced to video games even prior to that, helped narrow down his decision regarding a career choice. His ultimate dream is, of course, starting his own company and creating professional games for a living. "It beats my previous choice of being a plumber when I was four years old!" he says jokingly. His other interests include web development, which was his primary interest before game development, music, and linguistics. First, I would like to express my deepest thanks to Usha Iyer for offering me the opportunity to write this book and bringing me on board. In addition to that, I would like to thank Prachi Bisht, Mamata Walkar, and Pramod Kumavat for being great to work with and handling the production duties. Lastly, I would like to thank my mom, grandmother, my beautiful wife, as well as her entire family for showing me endless love and support throughout this entire ordeal. I wouldn't be where I am today without your kindness, understanding, and patience during my late-night binge writing. This book is dedicated to you! About the Reviewers Tom Ivanyo is an aspiring game and software developer. After learning Visual Basic, he picked up several other languages, from Assembly to C#. Currently, he is working as a software developer and spending his free time creating games along with his friend, Doug Madden. Together, they started their small gaming studio named S2D Games (http://s2dgames.com). Vittorio Romeo is a computer science student at the University of Messina and a C++ enthusiast. Since childhood, he has been interested in computers, gaming, and programming. He learned to develop games and applications as an autodidact at a very young age. He started with the VB/C# and the .NET environment, and moved on to C++ and native cross-platform programming. He works on his open source general-purpose C++14 libraries in his spare time and develops open source free games using SFML2. The evolution of C++ is something that greatly interests him. He has also spoken about game development with the latest standard features at CppCon 2014. Richa Sachdeva is an avid programmer. She believes in designing games that are high on educational content as well as entertainment and is giving her two cents towards creating and exploring different dimensions in the field of game programming. She is a physics graduate who somewhere along the line found her true calling in computers and has ever since been amazed by this strange pixilated world. When not thinking about games or deciding on which movie to watch, she finds solace in writing. www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface ix Chapter 1: It's Alive! It's Alive! – Setup and First Program 1 What is SFML? 2 SFML licensing 4 Resources and installation 4 Setting up a Microsoft Visual Studio project 5 Opening a window 5 Basics of SFML drawing 9 Drawing images in SFML 11 What is a sprite? 12 Common mistakes 15 Summary 17 Chapter 2: Give It Some Structure – Building the Game Framework 19 Graduating to ravioli 20 Implementing the window class 21 Building the game class 24 Putting our code to work 25 Hardware and execution time 27 Controlling the frame-rate 29 Using the SFML clock 29 Fixed time-step 32 Common mistakes 33 Summary 35 [ i ] Table of Contents Chapter 3: Get Your Hands Dirty – What You Need to Know 37 Introducing snake 38 Game design decisions 39 Implementing the snake structure 40 The snake class 41 The World class 49 Time to integrate 53 Hunting bugs 56 Fixing bugs 57 Going the extra mile 59 Common mistakes 63 Summary 65 Chapter 4: Grab That Joystick – Input and Event Management 67 Retrieving peripheral input 68 Checking for mouse input 68 Plug in your controller 69 Understanding the sf::Event 71 Introducing the event manager 73 Standard function wrapper 78 Building the event manager 78 Implementing the event manager 80 Integrating the EventManager class 86 Moving a sprite revisited 88 Principles of use 90 Common mistakes 90 Summary 91 Chapter 5: Can I Pause This? – Application States 93 What is a state? 94 The most simplistic approach 94 Introducing the state pattern 96 Defining common types 97 The state manager class 98 Defining the state manager class 99 Implementing the state manager 100 Improving the Event Manager class 106 Incorporating the state manager 109 Creating the intro state 110 Implementing the intro state 111 The main menu state 115 A sample game state 119 [ ii ] Table of Contents The means to pause 121 Common mistakes 124 Summary 124 Chapter 6: Set It in Motion! – Animating and Moving around Your World 125 Use of copyrighted resources 126 Finding and using the current directory 126 Using the SFML views 128 Preparing application states for views 130 Automated resource management 133 Designing a resource manager 133 Implementing the texture manager 138 Introducing sprite sheets 139 Implementing a sprite sheet class 140 The base animation class 144 Implementing the base animation class 145 Directional animation 148 Loading the sprite sheet files 150 Summary 152 Chapter 7: Rediscovering Fire – Common Game Design Elements 153 The game map 154 What is a tile? 155 Building the game world 156 The flyweight pattern 156 Designing the map class 158 The parent of all world objects 166 Creating the base entity class 166 Collisions and bounding boxes 168 Implementing the base entity class 170 Entity-on-tile collisions 174 Entity storage and management 180 Implementing the entity manager 181 Using entities to build characters 188 Implementing the character class 189 Creating the player 195 Adding enemies 197 Loading entities from the map file 200 [ iii ]