Chapter 12. Conquer the Universe

Congratulations! You have come this far. If you are reading this chapter, then you have already created two games—a 2D game and a 3D game. Sure, they aren't going to sell and make you a million dollars, but you already completed more games than 90 percent of all people who try.

There is a lot more to learn, and there is no way that we can cover everything in a single book. This chapter will briefly cover a few more topics and hopefully give you at least enough information to experiment further after you are done with the book. In fact, we are going to set up a framework that will allow you to play, so we will call it the playground.

The topics that we will cover include the following:

  • The playground: We will begin by setting up a template that you can use over and over again as you experiment with different features. This template will also be a good starting ground for any future games that you may want to create.
  • Texture mapping: So far, we worked with color, not textures. It would be pretty difficult to make realistic games with only color. It turns out that we can put textures onto our models to make them more realistic. We will learn the basics of texture mapping on a simple 3D shape.
  • Lighting: So far, we used the default lighting that was provided by OpenGL. Most of the time, we want more control over the lighting. We will discuss the various types of lighting and how they are used.
  • Skyboxes: The game universe can't go on forever. We often use a device known as a skybox to surround our game world and make it look like it is larger than it really is. We will learn how to add a skybox to our space game.
  • Physics: In the real world, objects bounce, fall, and do other things based on the laws of physics. We will discuss how objects interact with each other and the rest of the universe.
  • AI: Many games have enemies or weapons seeking to destroy the player. These enemies are usually controlled by some form of Artificial Intelligence (AI). We will discuss some simple forms of AI and learn how the game can control objects in the game.
  • Where to go from here: Finally, I'll give you a few tips on how you can continue to improve your skills once you have completed this book. We'll talk about game engines and topics for additional study.

A fun framework

Now, it's time to create our playground. Before we start coding, let's decide on the basic features that we want to set up:

  • Visual Studio project
  • Windows environment
  • OpenGL environment
  • Game loop

That's all we are going to do for now. The idea is to set up a basic template that you can use to start any game or experimental project. We don't want to include too much in this basic framework, so we will leave out sound, input, sprite, and model loading for now. These can be added in as they are needed.

Setting up the Visual Studio project

Start a new blank project and name it FunWith3D. Make sure to add the correct libraries as we have done before in the project Properties, Configuration Properties, Linker, Input, Additional Dependencies property:

glu32.lib;opengl32.lib;SOIL.lib;

We are going to include the SOIL library because it is so useful to load images. You will want to copy the following files over from our SpaceRacer3D.cpp project folder:

  • glut.h
  • glut32.lib
  • glut32.dll
  • SOIL.h
  • SOIL.lib

Add the following libraries to Properties, Configuration Properties, Input, and Additional Dependencies:

  • glut32.lib
  • SOIL.lib

Setting up the Windows environment

Create a new C++ file and name it FunWith3D.cpp. Then add the following code:

#include <windows.h>
#include <stdio.h>
#include "glut.h"
#include "SOIL.h"

const int screenWidth = 1024;
const int screenHeight = 768;

// Global Variables:
HINSTANCE hInstance = NULL;
HDC hDC = NULL;
HGLRC hRC = NULL;
HWND hWnd = NULL;

bool fullscreen = false;


// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

Now, open SpaceRacer3D.cpp from the previous project and copy the following functions:

  • WinMain
  • WndProc

These are the header files and two functions that are required for Windows to do its stuff. All of this code has been explained in previous chapters, so I'm not going to re-explain it here. In fact, you could save yourself some typing and copy this code directly from our previous project.

Setting up the OpenGL environment

Now, it is time to set up OpenGL. Copy the following function from SpaceRacer3D and add them after the WndProc declaration:

  • ReSizeGLScene
  • InitGL
  • KillGLWindow
  • CreateGLWindow

Setting up the game loop

Now, we add the function that defines our game loop. Add these functions after the OpenGL code that you just added:

void StartGame()
{
}
void Update(const float p_deltaTime)
{
}

void Enable2D()
{
  glColor3f(1.0f, 1.0f, 1.0f);
  glEnable(GL_TEXTURE_2D);
  
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  glOrtho(0, screenWidth, screenHeight, 0, 0, 1);
  
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();
  
  glPushAttrib(GL_DEPTH_BUFFER_BIT);
  glDisable(GL_DEPTH_TEST);
}

void Disable2D()
{
  glPopAttrib();
  
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();
  
  glDisable(GL_TEXTURE_2D);
}

void Render2D()
{
  Enable2D();
  //Add your 2D rendering here
  Disable2D();
}

void Render3D()
{
  //Add your 3D rendering here
}

void Render()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  Render3D();
  Render2D();
  SwapBuffers(hDC);
}
void EndGame()
{
}

void GameLoop(const float p_deltatTime)
{
  Update(p_deltatTime);
  Render();
}

In order to be consistent with some other code that we have written, you need to add the following precompile directives in the project Properties, Configuration Properties, C/C++, Preprocessor, and Preprocessor Definitions property:

  • _USE_MATH_DEFINES
  • _CRT_SECURE_NO_WARNINGS

Congratulations! You now have a framework that you can use for any future projects and experiments. You have also just successfully reviewed the OpenGL and game code that we have been working with throughout the entire book.

You will notice that I also left the code in so that you will be able render in either 3D or 2D! All together, you now have a small yet effective start for your own game engine. I suggest that you save a copy of the folder containing this solution and project. Then, when you are ready to start a new project, you can simply copy the solution folder, give it another name, and you are ready to go. We are going to use this as the basis for any code that we write in this chapter.

Tip

To save space and keep our little playground simple, I did not include some key features, such as input, sprites, models, and sound. If you feel that any of these are essential to your playground, then this will be your first exercise. In general, you will have to simply copy the relevant files and/or code into your project folder from the last version of SpaceRacer3D.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.58.122