Game Development Community

Make the CD ROM tray open in Torque

by Quentin Headen · 02/04/2009 (6:45 am) · 2 comments

To do this code, we are going to use some built in functions of the SDL library. First of all, you have to download the SDL lib offline. You can get it at http://www.libsdl.org/.

Once you downloaded the libs, go into your TGE game project folder, go into the "lib" folder. once there, you should see a folder that says "sdl". Go into that folder, and create a new folder named "windows".

Place the SDL.lib and SDLmain.lib files that you downloaded in here.

I assume you know how to add new libs to a C++ project and link new header files. If you do, add the SDL.lib and SDLmain.lib library files into the "Torque Demo" project, and link the C++ compiler to the SDL header files located at where you installed SDL at. Now it is time to mod the code!

Go to the folder in your C++ solution called "platform". Open a file called "platform.h".

Near around line 401 to 404 is the end of the Platform class. We are going to add one more function to this class. Right before the ending curly bracket of the Platform class, add this:

static bool OpenCDTray(int driveIndex);

That is the prototype of our function. Now, go to the folder that says "platformWin32". Once there, open a file called "winFileio.cc". At the very top add this code:
#include <SDL.h>

Now at the very end of this file, at this code here:

bool Platform::OpenCDTray(int driveIndex)
{

    SDL_Init(SDL_INIT_CDROM);//Initialize SDL so it can do CDROM operations

	SDL_CD* cd = SDL_CDOpen(driveIndex);//Try to get access to the CD drive

	if(cd == NULL)//if we couldn't get access
	{
		SDL_CDClose(cd);//Close the handle to the drive

		SDL_Quit();//Unload SDL

		return false;//and just return false
	}
	else//if we could
	{
		//Open the drive
		SDL_CDEject(cd);

		SDL_CDClose(cd);//Close access to the cd
	}

	//Unload SDL
	SDL_Quit();

	return true;//Return true
}

That function defines the prototype version we made earlier. Now, we are going to make it a console function. Open the folder called "console" and open the file called "consoleFunctions.cc". At the very top of this file add:
#include <stdio.h>
#include <stdlib.h>

Now right under the first console function code add this:

ConsoleFunction(OpenCDTray, bool, 2, 2, "Open the cd tray. Usage: OpenCDTray(int driveIndex);")
{
	
	argc;
	return Platform::OpenCDTray(atoi(argv[1]));
}

That makes our function open to the console. Now recompile the engine!.

There is one more step before you can test it. When you downloaded the SDL lib, it should have come with a file called SDL.dll. You must place this file into the directory of your game project. It has to be in the same directory as "torqueDemo.exe" If you don't do that, it will give you an error on startup asking for SDL.dll. Once you do that, run Torque.

Now open the console and type: OpenCDTray(0);

If everything went well, your CD tray should open up. Also, if you have multiple CD drives in your computer try a higher number like: OpenCDTray(1); or OpenCDTray(2);

You remember we made that function a boolean? If it returns a 0, that means it couldn't find the drive, and if it returns a 1 that means that it could. To test that, type echo(OpenCDTray(0)); It should print "1" onto the console. Now try it again, but set the drive index to a number of drive that doesn't exist (e.g. 100). It should return 0 letting you know that the drive wasn't found.

NOTE: THIS RESOURCE HAS BEEN TESETD ONLY IN TGE 1.5.2, AND ONLY ON WINDOWS. I CAN'T GUARANTY IT WILL WORK ON ANOTHER OS OR ON ANOTHER VERSION OF TORQUE.

Enjoy :)

About the author

Just your average teenage programmer who tries to finish the projects he starts. :) I am currently focused on creating games with Torque engines. My website is http://phaseshiftsoftware.com


#1
02/04/2009 (6:48 am)
I have to edit this post. Just to let you know. In the winFileio.cc file, you have to add #include <SDL.h> at the top near the other includes. Also in consoleFunctions.cc, you have to add
#include <stdio.h> and #include <stdlib.h> at the top near the other includes. If you don't, the compiler will throw you errors.
#2
02/05/2009 (10:10 am)
Ok. You can disregard that last post I made. The changes are included in the resource now.