Programming Linux Games phần 1 docx

41 278 0
Programming Linux Games phần 1 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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; info@nostarch.com; 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 the Sound Card with ESD . . . . . . . . . . . . . . . . . . . . . . . . . 195 Building Multi-Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Environmental Audio with OpenAL . . . . . . . . . . . . . . . . . . . . . . . . 206 OpenAL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Adding Environmental Audio to Penguin Warrior . . . . . . . . . . . . . . 213 Implementing Game Music with Ogg Vorbis . . . . . . . . . . . . . . . . . . . . 222 6 CONTENTS Working with Vorbis Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Adding Music to Penguin Warrior . . . . . . . . . . . . . . . . . . . . . . 227 6 Game Scripting Under Linux 237 A Crash Course in Tcl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Built-in Tcl Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Interfacing Tcl with C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Linking Against Tcl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Executing Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Understanding Commands and Objects . . . . . . . . . . . . . . . . . . . 250 A Simple Scripting Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Designing a Game Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Applying Scripting to the Real World . . . . . . . . . . . . . . . . . . . . . . . 265 Single Versus Multiple Contexts . . . . . . . . . . . . . . . . . . . . . . . . 266 Can We Trust the Script? . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Script Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Who’s Writing the Script? . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 7 Networked Gaming with Linux 271 ’Tis a Big Net, Quoth the Raven . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Internet Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Addresses and Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Name Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Socket Programming 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Connecting TCP Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Receiving TCP Connections . . . . . . . . . . . . . . . . . . . . . . . . . . 285 [...]... 3 01 Penguin Warrior’s Networking System 302 Network Game Performance 311 Security Issues 312 8 Gaming with the Linux Console 315 Pros and Cons of the Linux Framebuffer 316 Setting Up a Framebuffer Device 318 A First Foray into Framebuffer Programming 318 ... commercial games, produced mainly by Loki Software, Inc iv PREFACE Game programming has been one of my hobbies ever since my first Commodore 64 computer, and I wasn’t about to leave it behind when I left the Windows world for Linux The SVGALib library held me over for a while, but SDL quickly took over as my favorite way to write Linux games After meeting the Loki crew at a Linux trade show, I decided that Linux. .. programmers to write 2D and 3D games for Linux We will learn how to draw animated graphics on the screen, how to play high-quality digital sound through several different software libraries, and how to set up OpenGL to create fluid 3D graphics By the end of this book, you will know what makes Linux games tick, and how to create your own games for this platform 2 CHAPTER 1 This book is not about game... basic Linux programming tools from the perspective of a game developer If you are already experienced with Linux or UNIX programming, some parts of this chapter will be of less interest to you We will cover specific details of these tools later as we encounter them, so you will not be at a loss for skipping over sections of this chapter Programming Editors Before we can dive into the subject of Linux. .. recently, Linux has been lousy for gaming This isn’t due to any technical shortcoming; Linux has plenty of performance and stability to support high-performance multimedia applications It did, however, lack support from game developers Thanks to portable game programming toolkits like SDL and OpenAL, this is beginning to change Linux still hasn’t seen very much original game development, but Linux users... one such example, a role-playing game based on the Unreal engine Deus Ex has been ported to Linux, and I strongly recommend giving it a try Real-time Strategy Games The genre of games known as Real-Time Strategy (RTS ) games includes such popular titles as StarCraft, Command and Conquer, and Total Annihilation games that allow the player to command individual parts of an army from an overhead view, with... considered strategy games RTS games often have a high replay value; they’re fun to play again and again RTS games are comparatively easy to program, because, with some exceptions, they do not involve 3D graphics or complicated mathematics; however, good RTS games are hard to produce, and they tend to be few and far between They often involve a certain amount of artificial intelligence (AI) programming for... what Blizzard comes up with in the future Turn-Based Strategy Games Turn-Based Strategy (TBS ) games are like real-time strategy games, but the gameplay is divided into turns, usually with no time limit, thus giving the player time to think and relax, and lending the game an entirely different feel from the faster-paced strategy games TBS games are not decided by reflexes, but rather by careful planning,... tools we’ll use for Linux game programming, and then we’ll start to work with the libraries and interfaces that make it all possible If you’re already familiar with development tools like gcc and gdb, you may wish to skim over Chapter 2 and move on to Chapter 3 or 4 Chapter 2 Linux Development Tools As an operating system created by computer geeks, Linux provides a particularly nice programming environment... 375 8 CONTENTS 10 To Every Man a Linux Distribution 379 Source or Binary? 380 Local Configuration 3 81 Linux Isn’t Alone: Supporting FreeBSD 383 Packaging Systems 384 Graphical Installation Goodness: Loki’s Setup Program 387 Understanding the Linux Filesystem Standard . Data Programming linux games / Loki Software, Inc. p. cm. Includes index. ISBN 1- 886 411 -48-4 (pbk.) 1. Computer games programming. 2. Linux. I. Loki Software, Inc. QA76.76.C672 .L56 20 01 00-052689 794.8 15 268. 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  20 01 by Loki. trade in the United States by Publishers Group West, 17 00 Fourth Street, Berkeley, California 94 710 , phone: 800–788– 312 3 or 510 –528 14 44, fax: 510 –528–3444 Distributed to the book trade in Canada

Ngày đăng: 06/08/2014, 09:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan