Game Programming Primer.Docx
Total Page:16
File Type:pdf, Size:1020Kb
Game Programming Primer Introduction Goal of this Article The Hobbyist Professional Programming Memory intensive Computation intensive Prerequisites Variables, Keywords and Identifiers Basic formatted input/output Control structures Arrays Functions Pointers Structures Classes Quick Refresher Stack and Heap Bits and Bytes Arrays Second class type Sizeof Pointers Address of Indirection Pass by value References Pointers and Arrays Pointer arithmetic Pointer to pointer Preprocessor #define #include #ifdef, #endif, #undef Compiling and Linking Code Organization for Large Projects Coupling Source files Header files Macros Prototypes Multiple Include protection Circular inclusion Forward Declarations Extern Data structures and program design Linked lists Ordered list Array std::vector Hash table Messaging Game Engine Architecture Overview Systems Main Loop Display Input Memory Management Object Management Game logic Physics AI Component Based Design Component Based Game Objects Factory Data driven composition Additional isolated topics Finite State Machines ActionList Improved Messaging Generic programming Bitmasks User Interface by Sean Reilly Game User Interface From Scratch Preexisting Library Editor User Interface In-Game External External with Game Embedded Serialization Dump the Bytes Streams What to Serialize Serializing Pointers Optimization for C++ Games Delayed Construction Pre and Post Increment Initializer List Preallocate Objects Reflection: Type MetaData Example Engine Introduction Hello! My name is Randy Gaul, and I am a computer science student. I study at DigiPen Institute of Technology, and am majoring in Real-Time Interactive Simulation (a fancy way of saying game programming). I’d like to share my know-how in the ways of game programming as a profession for anyone interested in learning. I encourage anyone interested in programming or creating games, no matter how little knowledge you have in either topic, to check out this article. Goal of this Article Before my studies at university I had spent a good deal of time trying to learn what it is like to create a piece of useful software from scratch. I spent many hours studying the basics of programming such as control structures, program design and even abstract data types. I however was hardpressed to find any high-level information on how to piece together the smaller tools of programming in order to make a project of my own. This article aims at piecing together a lot various topics and information by providing high-level descriptions and explanations of how everything works together to form a game, from a more broad perspective. There will be very little actual code examples throughout the article, and this is intentional; by providing a higher level summary I can keep the article concise (I can’t write an entire novel inside of a single article!). In doing so I can summarize and present many explanations and concepts that are essential to understand in order to progress as a programmer in a very clear and unified manner. I’d like for even non-programmers to be able to skim through the article and find interest in the ideas and concepts presented. In a way you can expect this entire article to be a firehose of information. If a reader hasn’t be introduced to many of the concepts presented here, he will surely return again and again as much information will not be fully gleaned in a single read-through. The Hobbyist There’s a type of game programming that can be done purely as a hobby. This usually involves a single person or a very small group of people developing a video game in their own spare time. Middleware is a term used to describe a tool that is used to create or connect a software project together. Middleware such as FMOD for a sound system or Box2D for physics can be used to allow a developer to cut drastic chunks of work needed to get a project up and running. Hobbyists in the sense of what I’m trying to refer to don’t usually have professional level skills in game development, and simply create games for fun. Tools for creating games in a manner like this are quite plentiful; here’s a few: ● Pygame ● Flash ● Game Maker ● XNA ● Other pre-created engines or libraries ○ Unity ○ Box2D ○ SDL ○ SFML Tools like these can be wonderful for creating games, but this article isn’t dedicated to this area of game programming. I intend to write about how to start programming with the intentions of programming as a profession. Professional Programming As a professional programmer one should strive to understand as much as possible as to how a computer works, and how they can leverage a programming language to solve problems efficiently. As such, writing a game as a professional programmer revolves around the use of C/C++. This is because C++ lets you get as close to the hardware as realistically possible, thus granting lots of control over how the program runs; many things in C and C++ are hidden away from the developer and in turn make them slower. Having slow code isn’t acceptable for high performance real-time applications such as games. C++ is integral for anyone that wishes to have a career in the video games industry as a professional developer, especially if you want to have the best chances at success when you first start attempting to get a job as a game programmer. Real-time applications, like video games, need to run in a highly efficient manner, and as such require skills of the programmer that may be important in other areas of Computer Science. As I myself am studying to become a professional software engineer, this Know-How is geared towards an audience interested in learning about game programming as a professional, not only as a hobbyist. For the rest of the article I will be referring to development of video games as a profession. Memory intensive Video games are very memory intensive. They can easily require a lot of memory, and as such will usually benefit from efficient use of memory. Efficient memory usage can let a game have more content and assets on the screen at a given time, or allow for more advanced features than the hardware would otherwise allow. For example the SNES had a very hard time scaling and rotating images in real-time with some of its hardware, and as such most snes games don’t do these two things. However with the addition of a specialized GPU chip in the SNES, some games were able to make interesting use of such a feature (Yoshi’s Island comes to mind). In order to get the most out of the limited powers of the SNES very tight memory management had to be employed. So what is memory management? Memory management is the handling computer memory. This includes allocation and deallocation. Many situations involving memory management become very non-trivial, and as such it’s an area of study that is crucial for anyone interested in professional software engineering, especially in game development. Computation intensive Besides being memory intensive, real-time simulations have the requirement of having very efficient algorithms and computations. In mathematics there’s no sense whatsoever of efficiency in solving problems, however each operation done on a computer has a cost. It’s up to the programmer to find the most efficient ways to solve problems. This efficiency requirement is due to video games needing to be run at a speed that is not only playable but fun. Prerequisites There’s some prerequisite knowledge required in order to apply the concepts presented within this article. However I’ve attempted to structure the article such that a lack of basic programming knowledge shouldn’t hinder the reader too much in understanding the overall ideas. I’ll quickly list off the various prerequisites required to to start actually applying the concepts yourself, and in the next section will provide a quick reference to understanding some of the “problem areas” a lot of programmers come across. Though I do cover some of the more advanced areas of basic programming, I will not be writing a comprehensive guide to learning C and C++. Comprehensive books on both C and C++ are required for anyone interested in becoming a professional game programmer. Luckily I have two wonderful books that are pretty much my bibles when it comes to both C and C++. The first book is on C programming by K.N. King: C Programming: A Modern Approach 2nd Ed. Here’s a brief description of the book as seen on Amazon: Professor King's spiral approach made it accessible to a broad range of readers, from beginners to more advanced students. With adoptions at over 225 colleges, the first edition was one of the leading C textbooks of the last ten years. The second edition maintains all the book's popular features and brings it up to date with coverage of the C99 standard. The new edition also adds a significant number of exercises and longer programming projects, and includes extensive revisions and updates. - Amazon.com C Programming: A Modern Approach is definitely the best C book for learning all of the preliminaries shown in this entire article. This book also covers a lot of the topics in great detail from my A Quick Refresher section. Though I do link to many free alternatives, be warned that when I first started learning programming nothing could ever compare to this book. The second book I have to recommend is C++ Primer, the latest edition. Here’s an excerpt from Amazon.com about the book: If you are new to C++ programming, C++ Primer Plus, Fifth Edition is a friendly and easy-to-use self-study guide.