You are previewing OpenGL Game Development By Example.
O'Reilly logo
OpenGL Game Development By Example

Book Description

Design and code your own 2D and 3D games efficiently using OpenGL and C++

About This Book

  • Create 2D and 3D games completely, through a series of end-to-end game projects

  • Learn to render high performance 2D and 3D graphics using OpenGL

  • Implement a rudimentary game engine using step-by-step code

  • Who This Book Is For

    If you are a prospective game developer with some experience using C++, then this book is for you. Both prospective and experienced game programmers will find nuggets of wisdom and practical advice as they learn to code two full games using OpenGL, C++, and a host of related tools.

    What You Will Learn

  • Set up your development environment in Visual Studio using OpenGL

  • Use 2D and 3D coordinate systems

  • Implement an input system to handle the mouse and the keyboard

  • Create a state machine to handle complex changes in the game

  • Load, display, and manipulate both 2D and 3D graphics

  • Implement collision detection and basic physics

  • Discover the key components needed to complete a polished game

  • Handle audio files and implement sound effects and music

  • In Detail

    OpenGL is one of the most popular rendering SDKs used to develop games. OpenGL has been used to create everything from 3D masterpieces running on desktop computers to 2D puzzles running on mobile devices. You will learn to apply both 2D and 3D technologies to bring your game idea to life.

    There is a lot more to making a game than just drawing pictures and that is where this book is unique! It provides a complete tutorial on designing and coding games from the setup of the development environment to final credits screen, through the creation of a 2D and 3D game.

    The book starts off by showing you how to set up a development environment using Visual Studio, and create a code framework for your game. It then walks you through creation of two games–a 2D platform game called Roboracer 2D and a 3D first-person space shooter game–using OpenGL to render both 2D and 3D graphics using a 2D coordinate system. You'll create sprite classes, render sprites and animation, and navigate and control the characters. You will also learn how to implement input, use audio, and code basic collision and physics systems. From setting up the development environment to creating the final credits screen, the book will take you through the complete journey of creating a game engine that you can extend to create your own games.

    Style and approach

    An easy-to-follow guide full of code examples to illustrate every concept and help you build a 2D and 3D game from scratch, while learning the key tools that surround a typical OpenGL project.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. OpenGL Game Development By Example
      1. Table of Contents
      2. OpenGL Game Development By Example
      3. Credits
      4. About the Authors
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Building the Foundation
        1. Introducing the development environment
          1. A quick look at Visual Studio
            1. Start screen
            2. The Solution Explorer panel
            3. The Standard Toolbar panel
            4. The code window
            5. The output window
          2. Starting your project
        2. The game loop
          1. The game structure
            1. Initialization
            2. The game loop
            3. Shutdown
          2. Creating the game structure
            1. Port of access
            2. The Windows message loop
        3. Introducing OpenGL
          1. What is OpenGL?
          2. The other GL
          3. Downloading OpenGL
          4. Adding OpenGL to the project
            1. Linking to the OpenGL library
        4. Summary
      9. 2. Your Point of View
        1. Plotting your revenge
          1. The OpenGL coordinate system
            1. Making your point
            2. Understanding the code
              1. Header files
              2. Initializing OpenGL
              3. The main entry point
              4. The update function
              5. Drawing the points
            3. Running the program
            4. Stretching your point
          2. Getting primitive
            1. A triangle by any other name
            2. A primitive example
            3. From triangles to models
        2. Introducing textures
          1. Using textures to fill the triangles
          2. A matter of reference
          3. Hanging out in the quad
            1. Coding the quad
          4. Rendering a texture
            1. Loading the texture
            2. Texture wrapping
            3. Creating a textured quad
        3. Putting the pieces together
        4. Summary
      10. 3. A Matter of Character
        1. Spritely speaking
          1. Sprites versus non-sprites
          2. Flipbook animation
          3. Framed animation
        2. Creating sprites
          1. Working with PNGs
            1. Linking to the SOIL library
            2. Including the SOIL header file
            3. Opening an image file
          2. Coding a sprite class
          3. Creating sprite frames
          4. Saving each frame
          5. Loading a sprite from individual textures
          6. Creating a sprite sheet
          7. Loading a sprite sheet
          8. Loading our sprites
        3. Rendering
          1. Adding a render to the game loop
          2. Implementing the main Render function
          3. Implementing Render in the Sprite class
          4. UV mapping
          5. One more detail
        4. A moving example
          1. Adding update to the game loop
          2. Implementing the main Update call
          3. Implementing Update in the Sprite class
          4. Character movement
          5. Using delta time
          6. Calculating delta time
          7. Flipping
          8. Scrolling the background
        5. Using an atlas
        6. Summary
      11. 4. Control Freak
        1. A penny for your input
          1. The keyboard input
          2. Using the mouse
          3. Touch
          4. Other inputs
        2. Someone is listening
          1. The WndProc event listener
          2. Handling the message queue
          3. Handling mouse and keyboard inputs
          4. Creating the Input class
          5. Virtual key codes
          6. Querying for input
          7. Implementing the Input class
          8. Adding input to the game loop
          9. Processing our input
          10. Changes to the Sprite class
        3. Graphical User Interface
          1. Creating a button
          2. Enhancing the Input class
          3. Adding UI elements to the list
          4. Checking each UI element
          5. Pushing your buttons
          6. Adding our pauseButton
        4. State management
          1. Creating a state manager
          2. Pausing the game
        5. Summary
      12. 5. Hit and Run
        1. Out of bounds!
          1. Getting anchored
          2. Collision rectangles
          3. Embedding
          4. Fixing the background
        2. Collideables
          1. Ready to score
          2. A friend indeed
          3. Time to spawn
        3. Circular collision detection
          1. The Pythagorean Theorem
          2. Adding the circular collision code
          3. Why use circular collision detection?
          4. Wiring in the collision detection
        4. Rectangular collision detection
          1. The enemy within
          2. Spawning the enemy
          3. Adding the rectangular collision code
          4. Wiring continued
        5. Summary
      13. 6. Polishing the Silver
        1. The state of the game
          1. State machines
          2. Why do we need a state machine?
          3. Planning for state
          4. Defining the new state
          5. Implementing the state machine
        2. Making a splash
          1. Creating the splash screen
          2. Defining the splash screen
          3. Loading our resources
        3. What's on the menu?
          1. Creating the menu
          2. Defining the menu buttons
        4. Getting some credit
          1. Creating the credits screen
          2. Getting back to the main menu
        5. Working with fonts
          1. Creating the font
          2. Drawing text
          3. Wiring in the font support
        6. Level up!
          1. Displaying the score
          2. Game progression
          3. Defining game levels
          4. Game stats
          5. The next level screen
          6. Continuing the game
        7. Game over
          1. The game over screen
          2. Replaying the game
        8. Summary
      14. 7. Audio Adrenaline
        1. Bits and bytes
          1. A sound by any other name
          2. Making noise
        2. Revving up your engine
          1. Accessing the FMOD .dll file
          2. Linking to the library
          3. Point to the include files
        3. Initializing FMOD
          1. Virtual channels
          2. Channel priority
        4. Bleeps and bloops
          1. Sound effects
            1. Setting up the sounds
            2. Playing sounds
          2. UI feedback
        5. The sound of music
        6. Cleaning up the house
          1. Release sprites
          2. Release input
          3. Releasing fonts
          4. Releasing audio
        7. Summary
      15. 8. Expanding Your Horizons
        1. Into the third dimension!
          1. Simulating 3D
          2. Real 3D
          3. 3D Coordinate Systems
        2. The camera
          1. Remember those home movies?
          2. Steady as she goes!
          3. The viewport
        3. Entering the matrix
          1. Vectors
          2. Combining vectors
          3. Identity matrix
        4. Coding in 3D
          1. Creating the project
          2. Retrieving OpenGL files
          3. Linking projects to OpenGL libraries
        5. Setting up the OpenGL window
          1. Including header files
          2. Defining global variables
          3. Creating a function to create the OpenGL window
          4. Sizing the OpenGL window
          5. Initializing the OpenGL window
          6. Creating a function to remove the OpenGL window
          7. Creating the OpenGL window
          8. Creating the Windows event handler
          9. The Game loop
          10. The finale
        6. Summary
      16. 9. Super Models
        1. New Space
        2. A computer in a computer
        3. Drawing your weapons
          1. Getting primitive
          2. Drawing primitives
          3. Making your point
            1. Gl_Points
          4. Getting in line
            1. Gl_Lines
            2. Gl_Line_Strip
            3. Gl_Line_Loop
          5. Triangulation
            1. Gl_Triangles
            2. Gl_Triangle_Strip
            3. Gl_Triangle_Fan
          6. Being square
            1. Gl_Quads
            2. Gl_Quad_Strip
          7. Saving face
          8. Back to Egypt
        4. A modeling career
          1. Blending in
          2. Blender overview
          3. Building your spaceship
          4. Exporting the object
          5. Getting loaded
        5. Summary
      17. 10. Expanding Space
        1. Creation 101
          1. Preparing the project
          2. Loading game objects
            1. The Model class header
            2. Implementing the Model class
            3. Modifying the game code
        2. Taking control
          1. Implementing input
        3. Asteroid slalom
          1. Setting up collision detection
          2. Turning on collision
        4. Summary
      18. 11. Heads Up
        1. Mixing things up
          1. The saving state
          2. Push and pop
          3. Two state rendering
        2. A matter of state
          1. Adding the state machine
          2. Getting ready for a splash
        3. Creating the user interface
          1. Defining the text system
          2. Defining textures
          3. Wiring in render, update, and the game loop
        4. Summary
      19. 12. Conquer the Universe
        1. A fun framework
          1. Setting up the Visual Studio project
          2. Setting up the Windows environment
          3. Setting up the OpenGL environment
          4. Setting up the game loop
        2. Texture mapping
          1. Loading the texture
          2. Rendering the cube
          3. Mapping operations
        3. Let there be light!
          1. Defining a light source
        4. The skybox
        5. Advanced topics
          1. Game physics
          2. AI
        6. The future
        7. Summary
      20. Index