Game Development Community

dev|Pro Game Development Curriculum

Creating a canvas

by Johnathon · 11/08/2007 (1:48 pm) · 3 comments

Wow, I've done quiet a few things since my post on creating a Basic Console , so this post will be a little lengthy. I'm going to try and keep it small, as very large post tend to be difficult to read and stay focused on (of course this could just be my problem :)

I downloaded and used Torsion for my primary script editor, I wasn't sure how I was going to like it so I just downloaded the demo to give it a shot prior to purchasing it. I must say that I really like the editor and with it only costing $40, it seemed like a really good deal. It has a few things that are a little quirky, but for the most part it works really good. I really like how it handles debugging. I still have 17 days available on my demo license, but i ran across a post in the forums where someone had posted a link to the newest netMercs CodeWeaver. I decided to give it a shot and found that I liked it better than Torsion , and it's free! I haven't managed to figure out how to get debugging to work with it quiet yet. If I can't get that feature figured out I might go ahead and purchase Torsion , as debugging is an important aspect of developing a quality product.

So after messing around with CodeWeaver I imported my Basic Console and continued working on my little project. I wanted to create a blank canvas, but I was having difficulties trying to get it to run. I used the following code to try and create the canvas but TGE would always crash & burn.

package Base
{
	function OnStart()
	{
		Parent::OnStart();
		echo("------ Initializing Base ------");
		echo("Display Devices: " @ getDisplayDeviceList());
		echo("Video Driver Information: " @ getVideoDriverInfo());
		setDisplayDevice(getDisplayDeviceList(), 640, 480, 16, false);
		createCanvas($gameName);
	}
	
	function OnExit()
	{
		Parent::OnExit();
	}
};
activatePackage(Base);

I couldn't figure out why the engine was crashing, and so I dug around a little bit in my 'Game Programming All-In-One' book and found that they used 'InitCanvas'. I couldn't find that method anywhere and assumed that it had been replaced with createCanvas(), but I also noticed that they where setting the videodriver prior to creating the canvas. So I attempted the same thing.

[root]/Base/Init.cs
package Base
{
	function OnStart()
	{
		Parent::OnStart();
		echo("------ Initializing Base ------");
		
		echo("Assigning defualt video settings.");
		//Assign default video settings
		$pref::Video::allowOpenGL = true;
		$pref::Video::displayDevice = "OpenGL";
		
		//location where mission files are stored.
		$server::MissionFiles = "*/missions/*.mis";
		
		echo("Creating viewport canvas.");
		//create the viewport and assign a resolution
		createCanvas($gameName);
		setScreenMode(800,600,16,false);
		
		//Get this game rolling
		exec("./base.cs");
		
		InitializeClient();
	}
	
	function OnExit()
	{
		Parent::OnExit();
	}
	
	function resetCanvas()
	{
	}
};
activatePackage(Base);

The engine still crashed! Well, then I started looking in the torque examples included with the SDK and doh! I noticed the OpenGL libraries in the root directory of the examples. Duh! I needed those .dll's! I copied them into my projects root directory and hallelujah it worked! I added the SetScreenMethod after the canvas was created and had my canvas up and running.

Finally I created a player control and keymap and assigned some basic key bindings so that once I get an avatar into a 3D environment I will be able to move around a little bit.

Since I posted my main.cs code in my Basic Console post, and the above code is the updated Init.cs file that I had originally posted in Basic Console post as well. The remaining code is what I wrote last night.

[root]/Base/Base.cs
function InitializeServer()
{
	echo ("------ Initializing " @ $gameName @ " server ------");
	$server::MissionFiles = "*/missions/*.mis";
	
	exec("./server/server.cs");	
}

function InitializeClient()
{
	echo ("------ Initializing " @ $gameName @ " client ------");
	exec("./client/client.cs");
	exec("./Client/movement.cs");
	exec("./gui/mainMenu.gui");
	
	%conn = new GameConnection(ServerConnection);
	%conn.ConnectLocal();
}

[root]/Base/Client.cs
if (isObject( playerKeymap ))
	playerKeymap.delete();
	
new ActionMap(playerKeymap);

$movementSpeed = 1; //m/s

//The player sees the game via this control
new GameTSCtrl(PlayerInterface)
	{
	profile = "GuiContentProfile";
	noCursor = "1";
};

function PlayerInterface::onWake(%this)
{
	//enableDirectInput = 1;
	//activateDirectInput();
	
	//restore keymapping
	playerKeymap.push();
}

//This gets called directly within the engine during server intialization
function GameConnection::InitialControlSet(%this)
{
	echo("Setting initial control object");
	//first  control object has been set by the server so we are ready to go.
	canvas.SetContent(PlayerInterface);
}

[root]/Base/Movement.cs
//Strafing
function GoLeft(%val)
{
	$mvLeftAction = %val;
}

function GoRight(%val)
{
	$mvRightAction = %val;
}

//forwards
function GoAhead(%val)
{
	$mvForwardAction = %val;
}

//backwards
function Backup(%val)
{
	$mvBackwardAction = %val;
}

//Looking, spinning or aiming horizontally via mouse
function DoYaw(%val)
{
	$mvYaw += %val * (%cameraFov / 90) * 0.02;
}

//Looking vertically via mouse
function DoPitch(%val)
{
	$mvpitch += %val * (%cameraFov / 90) * 0.02;
}

//Jumping
function DoJump(%val)
{
	$mvTriggerCount2++;
}

//Keyboard control mapping
playerKeymap.bind(keyboard, up, GoAhead);
playerKeymap.bind(keyboard, down, Backup);
PlayerKeymap.bind(keyboard, left, GoLeft);
playerKeymap.bind(keyboard, right, GoRight);
playerKeymap.bind(keyboard, space, DoJump);
playerKeymap.bind(mouse, yaxis, DoPitch);
playerKeymap.bind(mouse, xaxis, DoYaw);
GlobalActionMap.BindCmd(keyboard, escape, "", "quit();");
GlobalActionMap.BindCmd(keyboard, tilde, "", ToggleConsole);

The code for player.cs and mainMenu.gui have yet to be wrote. That's next on the list!

The only downfall of creating a project without the use of the Torque Common scripts, is that I loose the World Editor and GUI Editor. This will make it a little more difficult working on my environments. I am going to dig around and see if I can't get the code needed for the editors added into my project. I'm not sure really where to look and what all I'm going to need. If I can't get something figured out I will just create my worlds and gui's using the starter.fps example, and then just copy my saved files and materials used into my project.

I also worked on how I want my game's directory structure to be setup. I wanted something other than the standard Torque
[root]
+Starter.fps
+common
+creator

I got tired of searching through 3 different main.cs, server.cs and client.cs files for what I was looking for. I created the following structure.
[root]
+base
+--client
+--gui
+--missions
+--objects
+--server
+--textures

So my game might not have a gui or world editor, but that's alright as I have already decided to not support 3rd party additions with my first game. I want to make things simple for myself and creating a game that will not be changed will help reduce my headaches. While I would love to have 3rd party support, and I know that the TorqueScript makes this super easy, I don't want to bother with it at this point. Maybe a little later down the road. That is why I have created the structure that I have, as there will be no need for me to have a common script base for people to base mods off of.

That brings up my final question, I noticed that I can have the engine (and I forget where I found this at) execute the actual .cs scripts rather than executing the compiled .dso files. Is there a performance penalty for using one or the other? I am going to do some research on this. I am planning on just distributing my compiled .dso scripts, but it is an interesting little piece of information that I ran into and would like to learn more about.

Edit 1: Fixed broken links
Edit 2: Forgot to discuss missing editors

#1
11/08/2007 (7:06 pm)
Nice job working from scratch.

For your editors, i think they should be in a folder called creator, in the default torque game directory.

For the .cs and .dso thing, i am pretty sure u just delete the .cs files after all the .dso files were generated, to prevent people from stealing your scripts. For developement you want your .cs files for easy code access. But executing a .cs file should execute the same compiled .cs.dso file, after the .cs is deleted.
#2
11/10/2007 (11:05 am)
Thank's for the advice, I was thinking maybe leave the .cs files uncompiled so people can make modifications if they wanted to, but I have since decided to just distribute the compiled versions instead.
#3
11/10/2007 (2:33 pm)
I forgot to mention, I can't use the creator directory because it loads 'common' scripts, which I am not using. I'm going to mess around with hand coding some gui components to get a feel for it, and then work on an external gui editor in C# or something.