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:
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:
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.
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
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.
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
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.
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.
3.147.74.211