Programming Linux Games
Total Page:16
File Type:pdf, Size:1020Kb
Programming Linux Games Programming Linux Games Loki Software, Inc. with John R. Hall An imprint of No Starch Press, Inc. San Francisco Programming Linux Games. Copyright c 2001 by Loki Software, Inc. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed in the United States of America 1 2 3 4 5 6 7 8 9 10|04 03 02 01 Trademarked names are used throughout this book. Rather than including a funny little trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Co-publishers: William Pollock and Phil Hughes Project Editor: Karol Jurado Assistant Editor: Nick Hoff Cover and Interior Design: Octopod Studios Copyeditor: Rebecca Pepper Proofreader: Ken DellaPenta Distributed to the book trade in the United States by Publishers Group West, 1700 Fourth Street, Berkeley, California 94710, phone: 800{788{3123 or 510{528{1444, fax: 510{528{3444 Distributed to the book trade in Canada by Jacqueline Gross & Associates, Inc., One Atlantic Avenue, Suite 105, Toronto, Ontario M6K E7 Canada, phone: 416{531-06737, fax: 416{531{4259 For information on translations or book distributors outside the United States, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 555 De Haro Street, Suite 250 San Francisco, CA 94107 phone: 415{863{9900; fax: 415{863{9950; [email protected]; http://www.nostarch.com The information in this book is distributed on an \As Is" basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. Library of Congress Cataloging-in-Publication Data Programming linux games / Loki Software, Inc. p. cm. Includes index. ISBN 1-886411-48-4 (pbk.) 1. Computer games--programming. 2. Linux. I. Loki Software, Inc. QA76.76.C672 .L56 2001 00-052689 794.8'15268--dc21 Contents Foreword i Preface iii Who This Book Is For . iv Online Resources . iv Acknowledgements . v 1 The Anatomy of a Game 1 A Quick Survey of Game Genres . 2 Simulation Games . 2 First-Person Shooters . 4 Real-time Strategy Games . 6 Turn-Based Strategy Games . 7 Role-Playing Games . 7 Puzzle Games . 9 Multiuser Dungeons . 10 A Quick Look Under the Hood . 11 The Input Subsystem . 12 The Display Subsystem . 12 2 CONTENTS The Audio Subsystem . 13 The Network Subsystem . 14 The Update Subsystem . 14 The Game Loop . 15 2 Linux Development Tools 17 Programming Editors . 17 vi......................................... 18 Emacs . 19 NEdit . 20 Compiling Programs Under Linux . 20 Using the Make Utility . 24 Creating Makefiles . 24 Error Handling . 28 Working with Libraries . 29 Static Libraries . 29 Shared Libraries . 29 Linux Linker Quirks . 32 Debugging Linux Applications . 33 Compiling for Debugging . 33 gdb........................................ 34 ddd ....................................... 41 Bug Tracking . 42 Project Management with CVS . 42 A Brief Tutorial on CVS . 43 Other Useful Tools . 49 Rapid Text Searching with grep . 49 CONTENTS 3 Updating Source with diff and patch . 50 3 Linux Gaming APIs 53 Graphics APIs . 55 SVGALib . 55 GGI ....................................... 56 SDL ....................................... 56 ClanLib . 57 OpenGL . 57 Plib ....................................... 57 Glide . 58 Xlib ....................................... 58 Graphical User Interface Toolkits . 59 GTK+...................................... 59 Tk........................................ 59 Fltk ....................................... 60 Qt ........................................ 60 SDL GUI Support . 60 Audio APIs . 61 OSS ....................................... 61 ALSA ...................................... 62 ESD....................................... 62 OpenAL . 63 Scripting Libraries . 63 Tcl........................................ 63 Guile and MzScheme . 63 Python and Perl . 64 4 CONTENTS Networking APIs . 64 BSD Sockets . 65 OpenPlay . 65 IPX and SPX . 65 File Handling . 66 libpng and libjpeg . 66 libaudiofile and libsndfile . 67 Ogg Vorbis . 67 The SDL MPEG Library, SMPEG . 68 zlib........................................ 68 4 Mastering SDL 69 Computer Graphics Hardware . 70 The Framebuffer . 71 The SDL Video API . 72 Setting Up the Display . 74 Direct Surface Drawing . 77 Drawing with Blits . 83 Colorkeys and Transparency . 87 Loading Other Image Formats . 92 Alpha Blending . 92 Achieving Smooth Animation with SDL . 97 Input and Event Processing . 107 Processing Mouse Events . 108 Processing Keyboard Events . 112 Processing Joystick Events . 116 Multithreading with SDL . 120 CONTENTS 5 SDL Audio Programming . 125 Representing Sound with PCM . 125 Feeding a Sound Card . 128 An Example of SDL Audio Playback . 129 Integrating OpenGL with SDL . 140 Penguin Warrior . 144 Creating Graphics . 146 Implementing a Parallaxing Scroller in SDL . 147 A Simple Particle System . 153 Game Timing . 158 5 Linux Audio Programming 161 Competing APIs . 162 Introducing Multi-Play . 163 Loading Sound Files . 164 Using libsndfile . 164 Other Options . 170 Using OSS . 170 Reality Check . 175 Achieving Higher Performance with Direct DMA Buffer Access . 178 Playing Sound with ALSA . 187 Sharing.