Tải bản đầy đủ (.pdf) (533 trang)

The fundamentals of c c++ game programming using target based development on SBCs

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (27.53 MB, 533 trang )


The Fundamentals of C/C++
Game Programming



The Fundamentals of C/C++
Game Programming
Using Target-Based Development on SBC’s

Brian Beuken


CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2018 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Printed on acid-free paper
International Standard Book Number-13: 978-1-4987-8874-8 (Paperback)
978-0-8153-5527-4 (Hardback)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to
publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or
the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced
in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright
material has not been acknowledged please write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any
form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and
recording, or in any information storage or retrieval system, without written permission from the publishers.


For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.
copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400.
CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been
granted a photocopy license by the CCC, a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification
and explanation without intent to infringe.
Library of Congress Cataloging‑in‑Publication Data
Names: Beuken, Brian, author.
Title: The Fundamentals of C/C++ Game Development : using Target-based
Development on SBC’s / Brian Beuken.
Description: First edition. | Boca Raton, FL : CRC Press, Taylor & Francis
Group, 2018. | “A CRC title, part of the Taylor & Francis imprint, a
member of the Taylor & Francis Group, the academic division of T&F Informa
plc.”
Identifiers: LCCN 2017048100 | ISBN 9781498788748 (pbk. : acid-free paper) |
ISBN 9780815355274 (hardback : acid-free paper)
Subjects: LCSH: Computer games--Programming. | C (Computer program language)
| C++ (Computer program language)
Classification: LCC QA76.76.C672 B49 2018 | DDC 005.13/3--dc23
LC record available at />Visit the Taylor & Francis Web site at

and the CRC Press Web site at





Contents

“From Hello World to Halo—It’s Just Code!”


xv

Thanks

xxvii

Brian Beuken: Who Is He?

xxix

1. Getting Started

1

Mine Looks Different? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
First Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Setting Things Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Introducing Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
Hello Place of My Choosing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11

2. Getting Our Target Ready

13

Setting Up the Target . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
Oh Wait…Did We Plug-In? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Starting Up VisualGDB for the First Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16


vii


Getting the Machines to Talk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Sending Our First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
Debugger Hangs Too Much? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28

3. Using the Target

31

Ready to Rock and Ermm Indeed Roll! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
Graphics Explained! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
So It Be OpenGL ES Ye Be Wanting Arrgghhh! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
Where Is OpenGLES2 .0 on My Target? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
A Nice New Project with Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
So Much Typing? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Our First Graphics Project! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
Are We There Yet? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
Houston We Have a Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
Behold the Triangle Code! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Why Are We Working in a Window? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51

4. Putting It All Together

53

Expanding Our First Graphics Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53
Loading Graphics or Other Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53

Adding Assets to the Build Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
Keeping Things Tidy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
Add Some Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Displaying More Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
But I Didn’t Do Anything Wrong? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
But I Fixed It? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Making a Dynamic Playfield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68
Old School Frame Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68
Setting Up the Frame Buffer and Switch System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74

5. Finally Our First Games

77

5 .1

Invaders from Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77
Using the OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78
Start as We Mean to Go on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85
We’re Here Now . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Every Story Needs a Villan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91
Arrays or Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93
Move Em Out! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96
Animation 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98
Hand Me a Bag of Bullets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100

viii

Contents



Did We Hit It? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101
Box Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102
Circle Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102
Give Me Shelter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104
So Which Is Better? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110
Final Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110
Simple Text Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111
A Simple Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113
How Did We Do? The Infamous Postmortem . . . . . . . . . . . . . . . . . . . . . . . . . .118
Fix Question 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119
A Pat on the Back . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Kamikazi Invaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123
The Ship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Da Baddies! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130
Now We’re Talking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132
Make Them Move . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135
Get Them Flying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136
A Nice Arc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Step by Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141
Dive Dive Dive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
Bombs Away . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144
Get Back to Where You Once Belonged . . . . . . . . . . . . . . . . . . . . . . .145
Home Again! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145
Vectors, Our Flexible Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146
Lets Get Lethal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151
Bombs Away for Real Now . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Danger UXB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161
Stepping Back, Deciding When to Go . . . . . . . . . . . . . . . . . . . . . . . .162

Breaker Breaker Rubber Duck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167
Fred Reacts! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169
Tidy Up the Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
Twiddles and Tweaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
Postmortem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
Jumping around a Bit Though? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174
Crawling Over, Time for Baby Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175
Object-Oriented Programming Is Not an Error . . . . . . . . . . . . . . . . . . . . . . . . .175
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176
Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176
Start the Music . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176
Welcome to OpenAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177
Installing OpenAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177
Getting OpenAL Working . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179

Contents

ix


Dealing with Sound as Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182
How Does OpenAL Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183
How Does Alut Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184
Horrible Earworms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Streaming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191
The War against Sloppy Code Storage! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192
Our Own Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .193
Using This New Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196

Lets Get a Bit More Compiler Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198
5 .2 Tiles and Backgrounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198
What Do We Mean by Tiles? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199
Working with Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199
What a Wonderful World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199
Homing in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Wrapping It Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211
Is This All We Need? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211
5 .3 Single-Screen Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214
A World with Gravity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214
Routine Bad Guys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221
Point-to-Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221
Patrolling Enemy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Homing Enemy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Ladders and Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Data, Our Flexible Friend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Loading Our Maps (and Other Resources) . . . . . . . . . . . . . . . . . . . .235
5 .4 Lets Scroll This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239
Simple Scrolling Shooter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Let Them Eat Lead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Bring on the Bad Guys! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Process Everything? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251
No More Mr Nice Guy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252
What Will Make It Better? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
There’s No Wrong Way…But There Are Always Better Ways . . . . . . . . . . . . . .255
For a FireWork, Life Is Short But Sweet! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255
A New Dawn for Particle Kind! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261
There’s Always a Price to Pay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .270
Handling Large Numbers of Objects . . . . . . . . . . . . . . . . . . . . . . . . .271
Locking the Frame Rate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271

Recapping the 2D Experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272

6. A New Third Dimension

275

A Short Explanation of 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .276
MATHS!! Don’t Be Scared (Much) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
How Does This Witchcraft Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .281
This Is All a Bit Much? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

x

Contents


Installing a Maths Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Danger, Will Robinson! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Normal Programming Resumes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Three Types of Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291
Model Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291
View Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291
Projection Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
The Relationship of These Three Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Other Matrix Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .295
Moving Around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Revisiting Hello Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Let’s Try a Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Mistakes Waiting to Happen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
A Quick Word about Using Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

HelloCubes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
I Thought We Could Move 100’s of Thousands of Them? . . . . . . . . . . . . . . . . . . 306
How the GPU Gets Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Buffers, Buffers Everywhere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Vertex Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Attribute Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311
Texture Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .312
Frame Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .312
Render Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .313
Buffers Are Not Free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .313
Let’s Get Back to It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314
Time to Texture Our Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315
The Fixed Pipeline Isn’t Quite Dead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318
Mapping a Texture to Our Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319
Choose the Size to Suit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Limited Numbers of Textures? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Everyone Loves a Triangle But! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .322
3D Lets Get into the Heart of Our GPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325
What Else You Got? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325
Loading Models (OBJ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .327
Locate and Install an OBJ Model Loader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .327
Installing and Using TinyObjLoader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329
Do We Care about the Data? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .330
Lights Camera Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .332
The Return of the Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335
Dot Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335
Another Fun Vector Fact-Cross Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .336
Who’s to Say Whats Normal? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .336
Types of Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .338
Light Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339

Shadows, a Place Where Danger Hides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339
Shaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
So What Is a Shader? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Contents

xi


Keeping Track of Them . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Introducing the Shader Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Let’s Light It Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
The Camera Never Lies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .350
But What Does It All Do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .351
In Space, No One Can Hear You Smashing Your Keyboard As You Scream
Why “Don’t You Work!!!” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353

7. Space the Final Frontier

355

Space, Why All the Asteroids? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Skyboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361
The Game’s Afoot Which Way to Turn? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
We’re Going to Need Some Game Mechanics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
HUD and Cockpits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Screen or Render . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
3Dfont . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Hit Em Where It Shows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
3D Collision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368

Primitive Collision Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Culling Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Grids, Quad Trees, and OctTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Possible Collision Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .372
Sphere-to-Sphere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373
3D Particles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .374
The Wrap Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .375

8. Recognizable Environments

377

Let’s Talk about Time! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .378
Animating Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .379
Limitations of OBJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
The MD(x) Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .381
Controlling the Animation of Our MD2 Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Explaining Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
The Ground, the Place Where You Will Fall to! . . . . . . . . . . . . . . . . . . . . . . . . . . 390
A Simple Ground Plane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Level of Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .391
Mipmapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .393
Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
We Don’t All Live in the Netherlands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .395
Using an OBJ File—The Simple Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
How Far above Sea Level Are We? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Interacting with Our World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Collision Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397

xii


Contents


Render Culling! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .398
Adding the Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Physics, More Scary Maths Stuff? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .401
Subtitle…More Long Winded Explanations! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .401
Introducing Bullet Physics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
How It Works, and Finally Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Let’s Get to It, at Last . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Setting Things Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Stepping Through . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Visualizing Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Force, Torque, and Impulse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .410
Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .412
The Downside . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .415
Basic Racing Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .416
Getting and Making the Car Controllable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417
I Like to Move It, Move It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .418
Staying on Track . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .421
Using Bullet to Collide with the Terrain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Can’t Find My Way Home? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Other Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Other Performance Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .431

9. Let’s Go Indoors

433


The Beginnings, Doom, and Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433
Occlusion, a Discussion to Have for a Problem to Solve Later . . . . . . . . . . . . . . . . . 434
Exploring the Maze . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Moving the Baddies Around, Things! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
What Do We Draw? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Whats the Point? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Ray Cast and Picking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Are We Going to Do Any AI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444

10. Graphics Need a Boost

447

That Cat’s Been in the Bag Long Enough . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Shadow Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Recap the Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .452

11. Populating the World

455

Keeping Track of the Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .455
Scene Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Wrangling All That Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Asset Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Fragmentation, a Problem That’s Hard to Solve . . . . . . . . . . . . . . . . . . . . . . . . . . 460

Contents

xiii



Expanding to an SDK? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .461
The Last Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Ready-Made or Roll Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Limitations of Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Cross-Platform Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

12. Some Advanced Stuff

467

Multicore Fun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
What Is Threading? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Threads and Pthreads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471
Job Managing, Also Kinda Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471
With Great Power Comes Great Heat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .473
The End Is Near! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .474

Appendix I

477

Appendix II

479

Appendix III

481


Appendix IV

483

Appendix V

485

Appendix VI

487

Appendix VII

489

Appendix VIII

491

Index

493

Where Files Live on Non-Raspberry Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Updating versus New SD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .479
A Word or Two about Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .481
Bits Bytes and Nibbles Make You Hungry! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
OpenGLES3 .0+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

The Libs We Used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
On the PC End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Writing My Own Games? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Visual Studio 2017 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .491

xiv

Contents


“From Hello World to
Halo—It’s Just Code!”

Who Is This Book for?
The Fundamentals of C/C++ Game Programming: Using Target-Based SBC’s, is quite a
mouthful, isn’t it, as a title, it’s also making a few promises that it probably can’t keep,
because there are so many definitions of what are the fundamental skills a game programmer needs to have . But it’s my view that there are a few things that can get people up and
running and develop their hunger for learning, and it’s those things I want to bring to you
here . This is for people who want to be game programmers, but probably don’t quite know
how to do it or feel a little daunted that their coding skills don’t really let them explore
their game-creation skills . Although this is very much a beginner’s book, it is not really
aimed at a total novice who has never programmed before; you should be able to understand at least the basic concepts and syntax of C/C++ programming .
There are some excellent beginner’s books that I recommend to all my students, such
as Michael Dawson’s excellent Beginning C++ Through Game Programming (2014; Cengage
Learning; Australia), which though it throws little light on actual graphic gaming, is a
superb foundation for C++ . I will give some very simple getting-started examples and
build on those, so even the most code wary the beginner, should pick things up as they go .
None of the code in this book is complex, indeed it can be comfortably argued that it’s
oversimplified, it’s designed as a jumping-off point for a novice to expand their knowledge
and most of the initial explanations are going to be understandable even for a total beginner . I’ll start slowly and explain much at first, but the pace will pick up as we get going and

I’ll let the online source code go into more detail as I explain the overall intent of what we

xv


are doing rather than the specific functions . If you still don’t understand the syntax of the
code, you should undertake a beginners’ coding course, there are several online .
In addition, despite the title, this isn’t a book solely about programming Single Board
Computers (SBCs) . The use of a cheap target system is a means to an end to encourage the
reader to limit expectations and work within tight constraints, which game programmers,
especially console programmers have to work with . I want primarily to focus on gameplay
concepts and game structures, which will let us get games up and running really quickly .
However, we do have to introduce some technical concepts later, when we’re a bit more
comfortable, because most of these technical concepts will have a direct impact on the
performance of your games . You will need to know just enough to avoid some pitfalls and
get things up and running correctly .
SBCs are usually quite simple systems, so building a working knowledge of the fairly
generic hardware to produce graphics, sound, and data storage is generally easier to learn
on them, than it would be on your up to the minute PC, which will shield you from errors
by virtue of massive processing performance and near unlimited memory .
Once understood, all of the concepts and projects in this book are easily transferrable
to any development target where the reader can stretch their growing skills on more powerful systems while still being mindful of the need to work within constraints of hardware,
which are hard to push, and personal limits, which should always be pushed .
But SBCs are really fun to work with, cheap to acquire, and present a real sense of
achievement when you make them to do more than just act as media servers or control units .
Most important, this is not a how to do x, with y kind of book . I want to take you
through a journey of discovery, mine as well as yours, and provide suggestions and working examples on how to do things that games need, and let you decide if the approach I’ve
taken is valid . I want to make you question things and hopefully come to different conclusions, using what I supply as a base for debate and expansion rather than a gospel to be
followed . When working with beginners, I don’t believe in imposing the right way, I prefer
to have faith in, “this works for me, can I make it better?” The right way, for you at least,

will come with practice and the joy of achievement .

What Are We Gonna Do?
For the last 9 years, I’ve been teaching beginner-level game programmers how to write
computer games . Not so much the actual languages used in programming, but the principles of actually creating games . Starting with simple space shooter games, scrolling platformers, maze games, character animation games, and so on!
Eventually, at some point in their development when they start to move beyond such
simple themes, and their confidence is high enough, I then encourage them to take their
first steps in writing more complex 3D immersive games on consoles, such as the Nintendo
Wii U, Microsoft Xbox One, and Sony PlayStation 4 .
It sounds like a massive leap, to teach beginners how to make a simple space shooter
game in 2D to then write games on the most powerful consoles on the market, but in fact
the progression is really quite simple . Coding is coding…once you have the basics in your
head, the rest of it is down to understanding how to get your code to work on different
machines and with increasingly larger projects, only the levels of complexity change .
Internally, you could not find more different machines than a PC, a Nintendo Wii U,
and a PlayStation 4 . However, the basic ideas of getting something to appear on screen,
xvi

“From Hello World to Halo—It’s Just Code!”


move around under some kind of control, and process some form of logic are what the games
programmers are really trying to do . That is independent of any machine architecture!
Through various concepts such as APIs, Libraries, and Engines, a lot of the complexity of how the screen objects appear is hidden from the programmer and is accessed via
simpler means, though there is always a chance to unhide it, usually causing untold damage as a result . We generally leave the technical side of the machines to tools and graphics
coders who make those APIs, Libraries, and Engines available to us, so we can focus on
the simpler concepts of getting our chomping pizza to move around the maze collecting
his trademarked pills .
When you move away from the worry of the technical issues of how to draw things
and start to abstract your thinking toward making, a thing, do a thing you want that thing

to do, you find yourself to be able to explain and understand your game concept far easier .
What you understand, you can code!
For some time, I wanted to write a tutorial that could take some of my methods of
learning as you go/do, taking beginners from their first simple projects to producing
fully working games on consoles . But there are some roadblocks I have to overcome .
Not least of which is while PC coding is easy enough to enter into, and I would have no
problem converting my first-year courses into a simple tutorial . Console coding, which
I also teach, is essentially a closed shop, open only to industry and a few elite educational
institutions .
So I thought it might be an interesting journey to take a machine I’d never worked
on before and document my way through it, simulating the out of my depth feel many
new programmers have when confronted by a blank screen, while at the same time giving the reader a chance to understand the basic concepts of game programming I’ve been
involved with for more than 30 years .
First things first, I need a target machine… .In my normal game dev career; I’ve
worked mostly with consoles from Nintendo and more recently Sony . So my basic techniques of coding are firmly entrenched in the idea of writing games on an editing system
that you don’t actually run the code on .
For the vast majority of my career, I’ve used what we call a development machine,
more often than not a PC, to write the code, which in turn is transmitted via some kind of
network link to the actual target machine the games will be played on, usually some form of
specialist development kit that allows communications and debugging to take place . All the
main console systems use Dev Kits, and I’m incredibly lucky to have access to all of them .
That creates something of a problem . The general public are not actually allowed to
purchase these dev kits, also I’m not allowed to talk about them, having Non-Disclosure
Agreements (NDAs) all properly signed up, so even if I wanted to, I can’t tell you how to
write games on a PS4 or Nintendo Wii, but I do very much want to give you a flavor of it .
Looking around at the kind of equipment most beginner programmers have, there
are however a few choices . The most obvious are Apple devices and Android phones .
These days almost everyone has one of these, to take selfies or spend a few minutes playing
games, so it seems like an ideal choice .
But there are issues with them . Apple devices really work best when using Mac’s and

their own idiosyncratic (but by no means bad) Objective C-based development tools .
These are great tools, but I find them a little too specific for my liking, and since one of the
objectives in writing this is to get you, the reader, to be able to work on standard types of
machines, I feel I should rule them out, perhaps that’s another book .
“From Hello World to Halo—It’s Just Code!”

xvii


Android uses Java……I suppose now it is a good time to admit I don’t like Java…but
the reasons for that will take too long to go into, but I’m certain that in at least one chapter
of this book or forum post, I’ll explain and you may feel I’m overreacting…I am of course,
but allow me a little bias . I believe Java is a fine language for some things, just not console
games . Again that may be another book .
That said, it’s quite possible to code at a low level on an Android system using the
Native Development Kit (NDK) essentially working on the hardware level; however, that
then raises another issue with Android machines, at the hardware level there is a massive
amount of difference from one maker to another, resulting in what are called abstraction
layers between the hardware and Android to maintain compatibility for the Android OS,
but less consistency in access methods, if you want to deal with hardware direct . Since I
want you the reader to have as few issues with the hardware as possible, and not be wrestling with it, it means we cannot be 100% certain that the code is going to work on every
Android system . That, more than the Java, rules out Android for us .
So common devices exist, there are ways to hook them up to the PC but do they provide a good experience that simulates development for consoles?
I don’t think so, Apple comes close and indeed for all basic definitions most Apple
devices are small consoles but the Objective C issue and confusing tools make it a bit too
specialized, If you want to do apps for Apple devices, I think there are more than enough
excellent tutorials out there .
Most industry programming is done in C/C++ so that’s what I want to work with .
Yes…before someone pipes up, you CAN do C/C++ on Apple and Android devices… .it’s
just not as easy as I want it to be, and while I don’t anticipate this journey to be without

incident, I prefer to keep to paths I know I can travel .
This book is intended to be a tutorial…So I wanted to find a simple and easy system
to work with .
I didn’t find one!
And there’s a good reason for that, pretty much any computer system has its own
drawbacks when it comes to getting it set up, often this is software based and that in turn
led me to another decision I needed to make before starting . What kind of standard development Integrated Development Environment (IDE) was I going to use…I wanted to use
Visual Studio .
Why? Well because I like it! I’m used to it, almost every other game developer not
doing Apple or Android product uses it, and moreover…it’s now free! Free is good .
Especially since I am going to encourage you to spend a little bit of money to fund your
journey into the joys of game programming .
It’s not everyone’s favorite IDE; in fact, I know Pros who will spend hours explaining
to you just how bad it is, but truthfully if you’re a beginner and all you want is a simple
system to write code, press a deploy button and watch the fireworks…It does just that . If
you’re a Pro, you want to totally investigate the code, be able to search for sections that
contain information, and so on…Visual Studio does that too, it won’t stop me cursing it
from time to time, but I never met a programmer who was 100% happy with his tools .
As with most things in life, once you get used to something, you don’t like to change .
Mac users love X-code (mostly) and PC users love Visual Studio (mostly) . We’ll just use
what works .
So, our development system is chosen, we are going to use a PC or a Mac, running Windows and Visual Studio . But we don’t really need to worry about what kind of
xviii

“From Hello World to Halo—It’s Just Code!”


Windows; I’m still using 8 .1 on my main system, as I write this and occasionally on my
laptop running Windows 10 . At the time of writing, I’ve just upgraded to Visual Studio
2015 and I’m sure it’s going to annoy the hell out of me for all the things it does different

from Visual Studio 2013 . That anger will pass but for this book, I mostly used my trusty
Visual Studio 2013 . I’m not planning to do anything Visual Studio specific, so all should
be good whichever version you are using .
Now for the tricky bit, what’s the target system going to be…As I said before I wanted
to use a machine I’d never used before so that I could also experience some of the things a
new coder will come across . It also needed to be able to hook up to the PC somehow and it
needed to be available to the general public .

2π | !2π
Ok, let’s stop beating about the bush, after all the section heading gives it away . I chose
the Raspberry Pi, because it’s cheap, it’s freely available all over the world, it connects to
a PC via network cables or wirelessly, it has consistent hardware, so what works on one is
sure to work on another even if a few differences in speed happen, and in my opinion, it’s
a machine that has largely been ignored by the games development community . So you’re
going to be treading in largely virgin sand, that’s quite exciting .
I need a consistent, fun bit of hardware, with sufficient power, reasonable graphic
abilities, and onboard memory to create a range of decent little games to learn with . The
Raspberry Pi gives me all that, and most of its clones are close enough to also give us some
insight into the fragmentation issues in a small enough scale to cope!
Now it also has to be said, once I settled on the Raspberry machine I had my eyes
opened up to the fact that there are several Raspberry Pi-type machines out there; in fact,
there is a thriving community of similar small board System on Chip (SoC) machine’s
with their own communities . So I expanded my remit a little to include as many of the
main ones as I could find with a simple limit of cost . I only looked at units I could buy for
under U .S .$100 .
So if you have one of these other systems then it’s only fair that I make sure what we
do here are going to work on them too, so long as they run some form of Linux and have
OpenGLES2 .0 for their Graphics systems . We should be able to get our games to work on
them too . I’ll try to give a summary of machines I’ve tried, and maintain an update on the
support site .

Of course, technology never stands still, and as I was a quarter of the way in writing
this book, the Raspberry Pi foundation announced a new model, the Raspberry Pi 3, and
as usual it sold out within hours of its announcement . Not quite as big a leap in performance as the 2 was over the 1, but still another boost in performance for the same price is
much appreciated .
So I guess most of you will now be on model 4 by the time this comes out . But the nice
thing about the Raspberry range is that aside from memory and speed, they all are based
on the same hardware principles and they have maintained the mantra of compatibility .
So even though I’m going to continue with my Raspberry Pi Model 2B for now, swapping
over to the Model 3B quite soon I am sure, everything in this book will be checked on the
latest models before it goes to the printers .
I should say to owners of earlier Raspberry Pi models, all the things in this book will
work for you, but the later explanations on multicore processing will be useless as earlier
“From Hello World to Halo—It’s Just Code!”

xix


models had single core processors . You may find speed is an issue on the later 3D projects,
but given the cost of a Raspberry Pi 2(3/4/5), why not upgrade? The extra power is well
worth it . I do have a small collection of the earlier machines and will be trying the code
out on them as we go to give you notice of any issue I find .
Also, since part of the thinking behind writing this book is to introduce some of
the concepts and limitations of working on consoles, I have set myself some limitations .
As far as possible, I plan not to use any external third-party libraries, though consoles
do sometimes use external libs (audio for example, commonly uses third-party libs even
though SDKs usually provide some support) . Wherever possible, the only libraries I will
try to add will be on the Raspberry Pi already . This will create some difficulties for us and
require a bit of imagination to overcome them, but will hopefully create a more rewarding
experience and producing more compact code on machines with limited memory . I will,
of course, break this rule, it’s impossible to keep really, but I will not do so lightly, if we do

have to use an external lib I will ensure it is free and easily available . You will also have to
take care if you distribute your projects, that the end user also has access to those libraries,
either supplied by you, or with a helpful test and request to install if missing .

You Call This Code?
A word about my coding style in this book… .Aside from some quite deliberately poor
design choices, I fully intend to make to show you how to improve, the style is, ermm, my
own!
I tend to write in a mixture of C and C++, partly because many consoles and embedded toolchains I use still use C, and partly because it’s a style I have found my students pick
up quite well before fully adopting C++ . I find full Object-Oriented Programming (OOP)
C++ to be a little unreadable, and confusing, especially for beginners . I also don’t want
to have to spend two or three pages to explain why C++ wants you to do things a certain
way, before we can move forward . So I’ll continue with my C with classes approach until
we find that it does not fit our needs . But I will be introducing more structured C++ as
this book goes on .
The thing about a computer language is this: it is there to make your life easier . If you
find it easier to write in mostly C . . .do so! Never let anyone stop you unless you are in a
team doing OOP . But on your own at this stage, I encourage you to simply write code that
works, something that makes sense to you, at least until you are 100% sure you know what
you are doing, then you can explore the majesty, or lunacy, of coding standards . With
practice you will naturally start to see patterns in your code that will lead to improvement
and understanding of the more advanced quirks of any language, once you start to see
faults in your own code and that becomes the second nature to you, you’re a coder .
But my C with classes style, is functional, my aim here is to make things clear, to give
you room to expand and improve on the basic systems . I’m also not a hardware expert,
so much of the information I am going to pass on is taken from a basic users’ view point,
based on what I have found the machine can do within the project I am producing . I am
quite sure a technical graphic coder could get and give a lot more info on the tech side of
things but that’s not really what I want to focus on here .
That’s not to say we should not learn new things, I always aim to improve, but I’m usually just too busy making games! It’s supposed to be fun! This means a lot of the early samples in this book will be mostly C based to tie in with the traditional starter projects found

xx

“From Hello World to Halo—It’s Just Code!”


in most textbooks . I’ll introduce some classes and then hopefully some more recognizable
standard C++ as we progress . I don’t use a lot of C++11 usually, but I will introduce
some more useful and up-to-date C++11 concepts when we need them, as we go through
the projects . I hear that there is now a C++17… .meh, mañana!
If you want to write tidy sharable code for the team, I suggest a Computer Science (CS)
course somewhere . If you just want to gain confidence in coding, make something work
and improve your understanding of how to make games . Read on, and feel free to rework
my code when you are confident enough to do it . Also take note, most of the technical stuff
we have to deal with, in the 3D sections in particular, can ALL be done better! I’m giving
you functional, but deliberately not optimal systems, it leaves you open to play with and
improve things, and I will suggest some ways for you to explore and research, so you get a
bigger confidence boost from doing it yourself .

Uggh It’s All So Old
One other thing you need to also consider is that many of the libraries, file, and data
formats used in this book are older concepts that don’t have too much traction any more
in modern game dev . So why present them here? Simple, we are using target machines
that do not have the horsepower of a modern computer system; in fact, they are about
10–15 years behind the current power level of even a modest PC . A great many cuttingedge concepts need a lot of horsepower or large data stores to function; we just don’t have
those resources available to us .
So it’s quite appropriate that we are going to use some techniques from the good
old days, which are still perfectly functional and once grasped will make updating these
methods a journey you will want to take with foundation knowledge to help you .
Coding and using, things that work, will give you a sense of achievement and quick
visible results, rather than a sense of frustration that tends to cause new coders to give up .

We will enhance some of the older ideas with our more modern systems and methods
where viable, so you are always free and encouraged to try to use more modern concepts as
soon as you feel you are capable of writing them . If your target can handle them! But I am
deliberately presenting relatively simple methods, which are compact, fast, and effective
even on the lowliest of target system .
Finally, though most of the current range of SBCs are multicore and get a significant
boost when using parallel processing, I’m not going to explore that in the projects presented here, though I will explain the concepts and some of the projects should be suitable
for parallel processing if the reader feels they want to tinker . I do this because parallel
coding works best if the projects are built with this in mind, but as this book is targeting
beginners I want to avoid potentially confusing technical concepts until such time as the
beginner starts to understand what they are doing . There’s no rush, when you get it, you’re
free to do anything you want to the code and enjoy the achievement of improvement for
yourself .

What Do We Need to Get Started?
As with every new technical challenge the first thing you need is some cash, you’re going
to have to spend a bit of money . Not much, trust me, as a Scot, with Dutch ancestry, spending money is as painful to me as it is to you, so we’ll limit it to what we need .
“From Hello World to Halo—It’s Just Code!”

xxi


Throughout this book I am going to refer to our target machine as the Target or
Raspberry Pi, which due purely to its massive market lead, I am going to assume you
will be using . But with very few exceptions any SBC with onboard/integrated Graphics
Processing Unit (GPU), keyboard and mouse connections, and a display output can be
used . So long as it’s running some form of Linux, which we only need for file and I/O
handling, and most important is using and has drivers for OpenGLES 2 .0 or higher as
a graphic Application Programming Interface (API) . This will all be explained in more
detail later .

I don’t have the foggiest idea how Linux itself works, so I’m only going to use what I
need to use to make the code work . I tend to use Rasbian or Debian, because that’s what
the Raspberry Pi uses, but a few machines prefer Ubuntu . As I say, once we are running
our code we don’t care about the OS . I’ll limit my usage to getting the IP address, installing
some libraries, and making sure it has a compiler and required drivers on it, some things
may need to be downloaded, but are all available for free!
Those of you with Android installed on your (non-Raspberry) machines will need
to change, Android is something else entirely though and not supported in this book but
it is supported by some of the tools we use . It’s not impossible to convert the projects to
work with an Android-based machine; however, you do risk incompatibility because of
significant differences in machines, which again is a chief reason this is not a book about
Android programming . That may come later . It might be a fun project for you when you’re
done with this book to try using any new skills you develop to get the projects running on
Android, it can be done, I’ve got them all running on a couple of brand name tablets with
little real effort but can’t get anything to work on another leading brand phone .
There are some issues with the different SBCs and Linux methods of setting up rendering windows, but for the most part all these machines provide drivers that make that possible, usually in some sample OpenGLES2 .0 code you can find in the makers downloads .
Some of the most popular SBCs are detailed on the support site and I will have personally tried all the samples on them so can give good feedback on any set up and execution issues you might come across . I’m picking up as many as I can over the next months
to make sure I can give you a chance to use your SBC of choice .
Once individual drivers are set up and installed, after any specific initializing systems are called the code samples in here should work on any valid target with few issues .
However, computers are interesting things and even two machines of the same model
from the same maker but with different production runs, may display differences, or have
different user set ups, so it would be naive of me to suggest everything is going to work
on every machine on a first attempt, but as far as possible, it should . Any significant differences in machine setup or installation will be covered as much as I am able to on the
support site .

The Target
We need an SBC of course; for the most part I will assume Raspberry Pi, 12 million+ users
would indicate that most of you are using that . So that’s your first purchase, if you haven’t
done so already, you need to do this now . At the time of writing, I’m using the current
model, a Raspberry Pi 2 Model B, but am soon going to plug-in my new Raspberry Pi 3

Model B . I will do the odd sanity check with older Raspberry Pi Model A+/B+/Zeroes
I  have to hand . I also have picked up quite a few of the so-called Raspberry Pi beaters
xxii

“From Hello World to Halo—It’s Just Code!”


that are on the market, such as, Nano Pi, Banana Pi, Orange Pi, Pine A64, and so on,
which also use a Linux OS . One awesome thing about these little SBCs is most are pocket
money cheap, so adding to my collection is proving to be a fun hobby . I’ll document any
issues I find in the support site . I do know that some of the others are faster or have more
impressive graphic systems and a few have SATA and USB3 to make disk access a factor,
but I strongly suggest we all stay with the Raspberry Pi’s for learning and then transition
to others if that’s what you want to use .
I hope the later versions of the Raspberry Pi that are sure to come, will be as compatible . I’m sure they’ll be even more powerful allowing you to fully explore that power with
this book .
Buying an SBC on its own is a bit useless though, it usually needs an SD card, to act as
its boot drive and storage medium, ideally preformatted with some form of Linux, which
on the Raspberry Pi is called Rasbian, (there are other options but Rasbian is easy to use) .
It also needs some means to power it, ideally a 5 V, 2–3 A, wall wart .
Internet access for your target is needed to install libraries and updates . A wifi dongle
is a wise extra purchase if your target does not have it onboard, and is especially useful
on boards with no wired network connection, though aftermarket USB-based network
systems are available . Wired network connection to your PC is preferred for faster communication between the target and dev system .
I’d recommend a case to keep the Raspberry Pi safe and tidy . These are minor extra costs
and usually most sellers will offer you a bundle . I got my new Model 3 for under €60euros .
Remember that the Raspberry Pi is a computer…therefore, it will also need a display,
a keyboard and a mouse to get the most out of it, I tend to have a few broken and bashed
keyboards lying around, which met their fate during some horror bug hunting session, but
any simple cheap keyboard and mouse will work . We will be doing our coding on our own

PCs, so the Raspberry Pi’s keyboard and mouse only needs to be functional and you can
pick up really cheap combination keyboard/mousepads .
As noted, your target also needs to have a display, it may be you have a monitor on
your PC that has dual HDMI inputs or you want to use a KVM system, this will work, but
will not be effective when you want to debug on one screen and see the project running
on another . Display switching between the two images will not be a satisfactory experience . A small monitor with your Raspberry Pi hooked up, beside your main monitor
is ideal . There are also some very serviceable low-cost LCD panels you can hook up to
the boards directly but don’t get less than 7ʺ, our early games are mostly going to run in
1024 × 640 pixel resolution, which we’ll scale to the screen’s resolution most of the time
and the small sub 7ʺ screens can’t really handle even that low res making scaling look odd .
If you are using a really low-power machine like a CHIP or Pi Model A, you can always
drop the resolution down to 640 × 480, or even worst case 320 × 200 . It will provide a
speed increase, especially on the 2D projects .
If you plan to use a target system you already had for a while, and it currently is set up
with lots of apps, which are set to run in the background, it would be wise to create another
project-based SD to boot clean, so that we make sure our target machine is not using resources
on other applications when we are developing . Throughout this book, I am assuming a total
clean fresh install of Rasbian or your usual flavor of Linux, with all default settings .
People have many reasons for buying SBCs and game programming is rarely one of
them, so there are many keen users out there who have expectations that focus on their
particular needs . When researching a board to buy, you may see a lot of comments on
“From Hello World to Halo—It’s Just Code!”

xxiii


what the best board and passionate explanations of why a particular board is bad, very
bad, a total con, and so on!
It’s quite true that some of the boards out there are, shall we say, less than optimal
for use as so-called maker boards . They may have badly implemented certain important

features, have a chronic lack of support, or any number of an absolute plethora of quite
genuine issues that will get people raging on their keyboards .
But very few of these genuine issues have a direct impact on us using our boards
as programming tools for the game development . We don’t care about the OS; we don’t
care about the I/O Pins or the hardware’s layout . So long as it runs some form of Linux,
has OpenGLES2 .0 with drivers, and can display an image . We should not have too many
problems…famous last words there!

The Development Machine
Any desktop or laptop style PC can be used, we are really only interested in its performance as an editor, so a decent keyboard, mouse, and display are all you really need . The
only hardware consideration is that it needs to be able to network in some way, via standard cables or wirelessly .
Next, you’ll need a copy of Visual Studio . This presupposes you are running on a
computer with a Windows OS . Later, models of Mac’s can run Windows, so pop-off to
Microsoft’s site and see if you can download a version, it can sometimes be obtained for
free or on discount for students . Visual Studio itself is free in its rich featured Community
version from https://www .v isualstudio .com .
I must take a moment to thank Microsoft for this; it really is a great gesture to make
such professional tools free to everyone .
Next you need some software to allow you an easy connection from your PC to the
Raspberry Pi . Whether you do this via a network cable direct to your PC, or via a router,
or even wirelessly . A network connection needs to be made . Wired connections are much
faster, but wireless can be tidier if you don’t mind the speed lag .
If you are of a technical mind you can probably work out how to get two machines
with different operating systems to work together, maybe even get them to communicate
via Visual Studio . But that takes time and effort, and if you’re reading this you’re probably
a beginner, so why go to so much effort? I am of a technical mind, but I’m also quite lazy
when it comes to doing things when I know there are easier solutions, so I decided to have
a serious hunt around for a solution to this, and I found it .
There is a wonderful piece of software called VisualGDB, which allows you to get your
version of Visual Studio to talk to a whole range of different machines, send them code,

run it, and debug it on your development machine .
I therefore give you a choice:
1. Write your own interface between Visual Studio and a Linux-based Raspberry Pi
2. Download and install the 30-day free trial of VisualGDB
I’m pretty sure you will opt for option 2 . If you went for option 1 . Close the book now, go
write your interface, and be sure to write to me in a few months’ time when you’re ready
to start again .
xxiv

“From Hello World to Halo—It’s Just Code!”


×