00 FO SDL Frontmatter 10/21/02 11:52 AM Page i Focus On SDL Ernest Pazera Team LRN 00 FO SDL Frontmatter 10/21/02 11:52 AM Page ii © 2003 by Premier Press, a division of Course Technology All rights reserved No part of this book 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 written permission from Premier Press, except for the inclusion of brief quotations in a review The Premier Press logo and related trade dress are trademarks of Premier Press, Inc and may not be used without written permission All other trademarks are the property of their respective owners Publisher: Stacy L Hiquet Marketing Manager: Heather Buzzingham Managing Editor: Heather Talbot Acquisitions Editor: Emi Smith Project Editor/Copy Editor: Cathleen Snyder Technical Reviewer: André LaMothe Interior Layout: Shawn Morningstar Cover Design: Mike Tanamachi Indexer: Sharon Shock Proofreader: Jenny Davidson Microsoft, Windows, DirectDraw, DirectMusic, DirectPlay, DirectSound, DirectX, Visual C++, Xbox, and/or other Microsoft products referenced herein are either registered trademarks or trademarks of Microsoft Corporation in the U.S and/or other countries All other trademarks are the property of their respective owners Important: Premier Press cannot provide software support Please contact the appropriate software manufacturer’s technical support line or Web site for assistance Premier Press and the author have attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer Information contained in this book has been obtained by Premier Press from sources believed to be reliable However, because of the possibility of human or mechanical error by our sources, Premier Press, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information Readers should be particularly aware of the fact that the Internet is an ever-changing entity Some facts may have changed since this book went to press ISBN: 1-59200-030-4 Library of Congress Catalog Card Number: 2002111223 Printed in the United States of America 03 04 05 06 07 BH 10 Premier Press, a division of Course Technology 2645 Erie Avenue, Suite 41 Cincinnati, Ohio 45208 Team LRN 00 FO SDL Frontmatter 10/21/02 11:52 AM Page iii For Teri, Mark, Laura B., Laura W., Chris B., Nick, Sara A., Jeff B., Jaco, Jason H., Jason P., Tony, Reggie, Will, Martin, Joey, Amy, Kenton, Denise, Ruthie, Rain, Peggy, Sarah (with an H), Mike G., Mike C., Little Miss Dani, Dani R., Sharon, Rex, Jill, Rick, Paul T., Paul E., Bobbi, Tim, Jessica, Ramon, Terri, Ann, Nathan, Jeremy, Nate (“Ref”), the folks at Frank’s Diner not already mentioned, the folks at Common Grounds not already mentioned, and the folks at Paddy O’s not already mentioned Yes, even Rent-A-Bob Team LRN 00 FO SDL Frontmatter 10/21/02 11:52 AM Page iv Acknowledgements I would like to thank André LaMothe, all of the folks at Premier Press, all the folks at GameDev.net, and all of the people who have worked on developing SDL and related libraries, as well as all of the people who developed the games on the CD-ROM Team LRN 00 FO SDL Frontmatter 10/21/02 11:52 AM Page v About the Author Ernest Pazera was a programmer living his life in the Midwest when suddenly people started asking him to write books Once he was allowed to so, he kind of got addicted to it, so this is his fourth book We are trying to get Mr Pazera some professional help for his illness We apologize for yet another book by Mr Pazera showing up on the bookshelf, but please buy it anyway We will continue to monitor his medication, and hopefully, in time, he will make a full recovery Team LRN 00 FO SDL Frontmatter 10/21/02 11:52 AM Page vi Contents Letter from the Series Editor viii Introduction ix Part One The Core of SDL CHAPTER Setting Up Your System for SDL CHAPTER SDL: The Big Picture 19 CHAPTER SDL Video 31 CHAPTER SDL Event Handling and the Window Manager 87 CHAPTER SDL Audio and CD-ROM 119 CHAPTER SDL Joysticks 137 CHAPTER SDL Threads and Timers 149 Part Two Add-On Libraries 165 CHAPTER SDL_image 167 CHAPTER SDL_ttf 171 CHAPTER 10 SDL_net 181 CHAPTER 11 SDL_mixer 193 Team LRN 00 FO SDL Frontmatter 10/21/02 11:52 AM Page vii Contents vii Part Three SDL Game Application Framework in C++ 217 CHAPTER 12 Framework Overview 219 CHAPTER 13 Core Components 225 CHAPTER 14 Video Components 243 CHAPTER 15 Audio Components 267 CHAPTER 16 Networking Components 279 CHAPTER 17 User Interface Components 291 CHAPTER 18 The Road Ahead 309 Index 313 Team LRN 00 FO SDL Frontmatter 10/21/02 11:52 AM Page viii Letter from the Series Editor If you’re going to write games on the PC, there’s only one choice for high-performance graphics—DirectX, right? Wrong! In fact, amazingly enough, there is another API not written by Microsoft that is simpler than DirectX and is supported on a number of platforms, including Windows and Linux The name of the API/SDK is the Simple DirectMedia Layer, or SDL for short The cool thing about SDL is that if you use standard ANSI C/C++ along with it, you can port your games and applications to other platforms in a matter of hours or a day at most This is the real power of SDL—portability Focus On SDL will get you up and running with the SDL system almost immediately This book will bring you up to speed with this elegant and clear API in no time Ernest Pazera takes you through each important SDL core module, from graphics and sounds to networking Once you have the basic SDL system under your belt, he proceeds to create a high-level wrapper class around the system to give you more flexibility Of course, it’s up to you whether you want to use it or just stick to the basics In conclusion, I highly recommend this book if you are a graphics or media programmer on any platform and you want an API that allows you to port quickly from platform to platform In fact, I think this may be the only book on the subject that focuses 100% on SDL, rather than on SDL as just another API in a larger context Sincerely, André LaMothe Series Editor for the Premier Game Development Series Team LRN 00 FO SDL Frontmatter 10/21/02 11:52 AM Page ix Introduction I am a programmer who, after programming for about 13 years, suddenly had opportunity to write books The one you are holding in your hands is the fourth book I have written Moreover, I tend to be the author who picks the “odd little topics.” My other books include Isometric Game Programming with DirectX 7.0 (Premier Press, Inc 0-7615-3089-4, 2001), the Game Developer’s Guide to the Cybiko (Wordware Publishing, 2001), and Focus On 2D in Direct3D (Premier Press, Inc., 2002) After writing four books, I think I’ve learned my “style” of writing and how I approach various topics I don’t make lots of very game-like demos when I’m talking about whatever API I happen to be writing about that month Instead, I write very simple code that demonstrates a particular aspect of the API, so that the code for the new topic is easily isolated and understood by the readers I am also a very object-oriented programmer When I write real code in C++, I use the three pillars of encapsulation, inheritance, and polymorphism Much of the code in this book is, as I call it, “book code.” Book code is a bit watered down and simplified to make it more understandable to readers with varying levels of experience If I were to write code for a book the way I really write code, even I would have a hard time following it Who You Are You are the primary reason I write books You are the reader You and you alone determine whether or not I have done my job sufficiently You looked at this book on a bookshelf or at some online bookstore and said, “SDL? What is that?” Or maybe you already knew about SDL and you wanted something to help you on your path besides the SDL documentation, which is quality documentation but still rather dry when you really think about it Maybe you are tired of DirectX I hear you Perhaps you want to use OpenGL, but you want to use a nice cross-platform API for “the other Team LRN ...00 FO SDL Frontmatter 10/21/02 11:52 AM Page i Focus On SDL Ernest Pazera Team LRN 00 FO SDL Frontmatter 10/21/02 11:52 AM Page ii © 2003 by Premier Press, a division of Course Technology... to platform In fact, I think this may be the only book on the subject that focuses 100% on SDL, rather than on SDL as just another API in a larger context Sincerely, André LaMothe Series Editor... Setting Up Your System for SDL SDL: The Big Picture SDL Video SDL Event Handling and the Window Manager SDL Audio and CD-ROM SDL Joysticks SDL Threads and Timers Team LRN 01 FO SDL chapter 01 10/21/02