www.pearson-books.com An imprint of Object-oriented Object-oriented Game Development GAME GAME Development Julian Gold is a software engineer at Microsoft Research in Cambridge investigating advanced machine learning for videogames. As Senior Programmer for Sony Computer Entertainment Europe he developed the visually stunning Colony Wars: Red Sun (BAFTA Nominee). At Six By Nine he worked on the best-selling soccer management game LMA Manager 2002 and his experience also includes time with Sega and Argonaut. Perfect the art of game development – read this book today! No member of the game development team should work in isolation. Whether you’re a producer, designer, artist or programmer this book will help you develop today’s ever more complex entertainment software within the constraints of deadlines, budgets and changing technologies. If you’re a student taking a games degree or module, the balance of best practice meets real-world know-how will give you the understanding you need to begin your career with confidence. ‘I love this book’ Liam Hislop, Full Sail Real World Education, Florida, USA ‘Game developers can learn a lot from this book’ Eric Le, Ubisoft, Canada ‘I would wholeheartedly recommend it to my students’ Paul Parry, Sheffield Hallam University, UK • Practical OO design methodologies with examples from real commercial code. • Design patterns that work in practice. • Write reusable code that will be reused. • Write games using component technology. • Develop multi-platform games efficiently. • Use iterative techniques in program and schedule development. Julian Gold Gold "LMA Manager"™ 2002/2003/2004 is a trademark of Codemasters. © The Codemasters Software Company. All other copyrights or trademarks appearing in the game are the property of their respective owners. Colony Wars Red Sun is the copyright of Sony Computer Entertainment Europe Limited. Object-oriented Game Development 8985 OOGD_A01.QXD 2/12/03 12:57 pm Page i We work with leading authors to develop the strongest educational materials in computing, bringing cutting-edge thinking and best learning practice to a global market. Under a range of well-known imprints, including Addison Wesley, we craft high-quality print and electronic publications which help readers to understand and apply their content, whether studying or at work. To find out more about the complete range of our publishing, please visit us on the World Wide Web at: www.pearsoned.co.uk 8985 OOGD_A01.QXD 2/12/03 12:57 pm Page ii Julian Gold Object-oriented Game Development 8985 OOGD_A01.QXD 2/12/03 12:57 pm Page iii Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: www.pearsoned.co.uk First published 2004 © Pearson Education Limited 2004 The right of Julian Gold to be identified as author of this work has been asserted by him in accordance with the Copyright, Designs and Patents Act 1988. All rights reserved; no part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise without either the prior written permission of the publisher or a licence permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London W1T 4LP. The programs in this book have been included for their instructional value. They have been tested with care but are not guaranteed for any particular purpose. The publisher does not offer any warranties or representations nor does it accept any liabilities with respect to the programs. All trademarks used herein are the property of their respective owners. The use of any trademark in this text does not vest in the author or publisher any trademark ownership rights in such trademarks, nor does the use of such trademarks imply any affiliation with or endorsement of this book by such owners. ISBN 0 321 17660 X British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library. Library of Congress Cataloging-in-Publication Data Gold, Julian. Object-oriented game development / Julian Gold. p. cm. Includes bibliographical references and index. ISBN 0-321-17660-X (pbk.) 1. Computer games Programming. 2. Object-oriented programming (Computer science) I. Title. QA76.76.C672G65 2004 794.8’151 dc22 2003062857 10 9 8 7 6 5 4 3 2 1 09 08 07 06 05 04 Typeset in 9/12 pt Stone Serif by 30 Printed and bound in Great Britain by Biddles Ltd, Guildford and King’s Lynn. The publishers’ policy is to use paper manufactured from sustainable forests 8985 OOGD_A01.QXD 2/12/03 12:57 pm Page iv To Sienna 8985 OOGD_A01.QXD 2/12/03 12:57 pm Page v 8985 OOGD_A01.QXD 2/12/03 12:57 pm Page vi Contents Acknowledgements xiii 1 Introduction 1 1.1 What is this book? 1 1.2 But why? 2 1.3 Who am I? 2 1.4 Who are you? 3 1.5 So what will you read about? 3 1.6 A brief history of games 4 1.6.1 The time that land forgot 4 1.6.2 It’s all academic 6 1.6.3 My! Hasn’t he grown? 7 1.6.4 From bedroom to boardroom 8 1.6.5 Summary 8 2The game development process 9 2.1 Philosophy 9 2.1.1 Context 9 2.1.2 Iterate! 10 2.1.3 Not all statistics are damned lies 11 2.1.4 Don’t do it again 12 2.1.5 Do it again 13 2.1.6 Don’t do it again (again) 13 2.1.7 See it from all sides 13 2.2 Reality bites 14 2.2.1 Hard cash 14 2.2.2 The Hacker’s Charter 15 2.2.3 So why are games different? 18 2.2.4 Conclusion 20 2.3 Summary 21 8985 OOGD_A01.QXD 2/12/03 12:57 pm Page vii 3 Software engineering for games 23 3.1 The peasants are revolting 23 3.2 The lords are revolting 24 3.3 Stopping the rot 25 3.3.1 From bedroom to office 25 3.3.2 Working practices for programmers 26 3.3.3 Software standards 27 3.3.4 Good working practice 30 3.3.5 Good programming practice 30 3.3.6 Code reuse 33 3.3.7 Dependencies: the curse of Hades 38 3.3.8 Reuse granularity 45 3.3.9 When not to reuse 50 3.4 The choice of language 51 3.4.1 The four elements of object orientation 52 3.4.2 Problem areas 56 3.4.3 Standard Template Library 59 3.4.4 Templates 60 3.5 A C++ coding policy 63 3.5.1 General 64 3.5.2 Policy specifics 64 3.6 Summary 67 4 Object-oriented design for games 69 4.1 Notation 69 4.1.1 Classes 69 4.1.2 Relationships 70 4.2 The design process 71 4.2.1 Phase 1: brainstorming 71 4.2.2 Phase 2: prune the tree 72 4.2.3 Phase 3: draw the bubbles and lines 73 4.2.4 Phase 4: validate the design 75 4.3 Patterns 76 4.3.1 The interface 77 4.3.2 Singleton 81 4.3.3 Object factory 89 4.3.4 Manager 95 4.3.5 Visitor/iterator 96 4.3.6 Strawman 105 Object-oriented game developmentviii 8985 OOGD_A01.QXD 2/12/03 12:57 pm Page viii 4.3.7 Prototype 108 4.3.8 Russian doll 111 4.4 Summary 134 5 The component model for game development 135 5.1 Definition: game engine 135 5.2 Motivation 135 5.2.1 Your engine has stalled 135 5.2.2 The alternative 136 5.3 Some guiding principles 138 5.3.1 Keep things local 138 5.3.2 Keep data and their visual representations logically and physically apart 140 5.3.3 Keep static and dynamic data separate 143 5.3.4 Avoid illogical dependencies 148 5.3.5 Better dead than thread? 149 5.4 Meet the components 149 5.4.1 Naming conventions 150 5.4.2 The application 150 5.4.3 Container components 152 5.4.4 Maths component 158 5.4.5 Text and language processing 178 5.4.6 Graphics 181 5.4.7 PRIM 186 5.4.8 Collision detection 189 5.4.9 Resource management 192 5.4.10 Newtonian physics 205 5.4.11 Network gaming 220 5.4.12 Summary 226 5.5 Summary 227 6 Cross-platform development 229 6.1 Introduction 229 6.1.1 Analyse this 229 6.1.2 Welcome to Fantasy Land 230 6.1.3 Same capability, different methodology 235 6.1.4 Platforms of different capability 251 6.1.5 Cross-platform component architecture 254 6.2 Summary 265 Contents ix 8985 OOGD_A01.QXD 2/12/03 12:57 pm Page ix [...]...8985 OOGD_A01.QXD x 2/12/03 12:57 pm Page x Object- oriented game development 7 Game objects 267 7.1 267 267 269 270 274 281 282 296 311 326 7.2 7.3 8 Open your GOB 7.1.1 Collapsed hierarchy 7.1.2 Shallow hierarchy 7.1.3 Vertical hierarchy 7.1.4 Mix-in inheritance Game object management 7.2.1 Creation and destruction 7.2.2 Referencing 7.2.3 Persistent damage... better than not doing them 1 8985 OOGD_C01.QXD 2 2/12/03 12:58 pm Page 2 Object- oriented game development 1.2 But why? Currently, as we begin the twenty-first century, commercial game development is at a crossroads As I type this, three new game platforms are launching or queuing up for launch in Europe: Sony’s PlayStation™2, Nintendo’s GameCube™ and Microsoft’s X-Box™ These consoles represent another quantum... argued that what makes game development unique is the synergy of constraints, the par- 8985 OOGD_C02.QXD 1/12/03 2:26 pm Page 21 The game development process ticular combination of requirements for the development Whilst this might be true, any other combination of constraints is just as unique, so in essence games development is no more unique than any other strand of software development The essence... commercial version of Russell’s Spacewar, which he called Computer Space In 1971, Bushnell’s game became the world’s first coin-operated computer game Bushnell went on to found the Atari Corporation, which was to dominate video game technology for over a decade 8985 OOGD_C01.QXD 2/12/03 6 12:58 pm Page 6 Object- oriented game development 1.6.2 It’s all academic Meanwhile, in the late 1960s, what we know as the... existence, never mind video game development We become better workers – and easiergoing people – when we are able to weigh up the pros and cons of several options and decide which to take in a more objective fashion Weigh up the pros and cons for all the potential solutions to a problem Avoid being dogmatic 13 8985 OOGD_C02.QXD 14 1/12/03 2:26 pm Page 14 Object- oriented game development 2.2 Reality bites... 1/12/03 2:26 pm Page 16 Object- oriented game development ● ● Control: the object I’m controlling must respond (perceptibly) immediately to my changing the physical controller state – whatever that might be Robustness: the game should never crash, especially if that would result in the loss of a player’s efforts How appropriate, then, is the hacker’s claim that these represent unique development priorities... deliver late There is no magic wand to wave 17 8985 OOGD_C02.QXD 18 1/12/03 2:26 pm Page 18 Object- oriented game development Nevertheless, a team that has delivering on time and within budget as a primary development goal will generally cost less and take less time than a team that does not Historically, the game development process has had a producer or management acting as a control valve on the developers... Core Figure 2.1 Classification of game features 8985 OOGD_C02.QXD 20 1/12/03 2:26 pm Page 20 Object- oriented game development Artistic content Many other flavours of software contain artistically rich imagery, but games outstrip all other kinds by a huge margin The quantity of 2D – still images, full-motion video (FMV), texturing, special effects, etc – and 3D models sets games aside from any other kind... sections focusing on development more useful But feel free to pick and choose, as they can be read in isolation with a minimal amount of cross-referencing So without further ado, let’s start 3 8985 OOGD_C01.QXD 4 2/12/03 12:58 pm Page 4 Object- oriented game development 1.6 A brief history of games Though it is beyond the scope of this book to provide a detailed history of computer games, it is well worth... artists, the impact on games programming is significant: the mechanism by which a model or image moves from an art package to being an active game object is one of the most crucial pieces of the development jigsaw and will be looked at in detail in forthcoming chapters Control methodology Most games have a control system that is quite different from other software applications For console games – whose host . of Object- oriented Object- oriented Game Development GAME GAME Development Julian Gold is a software engineer at Microsoft Research in Cambridge investigating advanced machine learning for videogames Data Gold, Julian. Object- oriented game development / Julian Gold. p. cm. Includes bibliographical references and index. ISBN 0-321-17660-X (pbk.) 1. Computer games Programming. 2. Object- oriented programming. 12:58 pm Page 1 Object- oriented game development2 1.2 But why? Currently, as we begin the twenty-first century, commercial game development is at a crossroads. As I type this, three new game platforms