Monday, April 11, 2005 Torque Game Engine Startup

After following the Introduction tutorials, you should be ready to actually start creating our game. We're going to walk through it step by step, one piece at a time, so a fuller understanding of the pieces is achieved.

Once you run most games, the first thing you see is a splash screen, introducing the game, and occassionally allowing other items to load in the background. Following the splash screen is a menu screen, allowing the user to set preferences, start, or exit the game. This is where we will start also. You can use any jpg, gif, or png image file you'd like for your background images. I've provided a simple pair here if you'd like to use them. Save them to your game directory, we'll get into game folder organization later. splash.jpg | menu.jpg.

To display a splash screen, we need to create a canvas on which to draw graphical objects. The canvas requires a few global settings to tell the TGE which engine to draw with first. Then we use the createCanvas function to create it.

main.cs
// main.cs 11 APR 05
$pref::video::allowOpenGL = true;
$pref::video::displayDevice = "OpenGL";

function onExit() {
}

function resetCanvas() {
   if (isObject(canvas)) canvas.repaint();
}

enableWinConsole(true);
setLogMode(2);
trace(true);

if (!createCanvas("Tank Battle v0.1 pre-alpha")) quit();

quit();

When you run this game, it quickly displays the canvas then quits. If you remove the quit function at the end of our script, then it won't close, but then you have to use CTRL + C to stop the game.

The first two lines set global preferences, identifying to the TGE that we are allowing the use of the OpenGL Application Protocal Interface, or API. API's are methods of telling the operating system or other libraries, such as the OpenGL graphics library, what to do and how to do it. OpenGL has the benefit that it is available across the Windows, Mac and Linux platforms, so your TGE games can run on all three operating systems. The displayDevice can also be set to D3D for Direct3D, a Microsoft only API.

The resetCanvas function is use to redraw the canvas area when it is reset. If the canvas object exists then the method, or function, repaint, is performed on that object. And the last item that is new is our call to the createCanvas function. When we call createCanvas, the only argument is the text to display in the tilebar of the window. If the command succeeds in creating the canvas object, it returns a true result. We can check for this true result, and quit our game if it fails in creating the canvas. The exclamation point before the function call is the "NOT" operator, and reverses the results. So instead of writing: if (createCanvas("Tank...") == false) quit(); we can check for true, then reverse the results with the not operator.

When the window opens, the default window size is 800 x 600, but we can change that if we want. We'll cover that later.

Next