SelectDevice Configuration Tool

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
silver.slade
Posts: 17
Joined: Fri Jan 29, 2010 8:54 am
Contact:

SelectDevice Configuration Tool

Post by silver.slade »

Hi all, Irrlicht users!

I've released a simple tool to manage the Irrlicht device's configuration.
The tool (at this moment it's only an header file with static function) shows an Irrlicht Interface to manage device's parameters in a visual way.
It stores and loads data to/from a text file, so it's possible to run an Irrlicht application with the possibility to change device's parameters on-the-fly.

So:
1) in the main.cpp file, simply run a new function to create the Irrlicht Device
2) this function will show a GUI: you can choose Driver Type, resolution, antialiasing...and so on
3) then the new Device will be passed to your main file to use
4) every "launch" action will save your configuration to file ("irrlicht.ini" file)

This is a simple "launcher", it compiles and runs in windows_xp_pro and ubuntu 10.04 (should works in other environments...I suppose)

Here's the code.

main.cpp

Code: Select all

#include <irrlicht.h>
#include "selectdevice.h"

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace gui;
using namespace io;

int main(int argc, char** argv) {

	IrrlichtDevice* device = selectDevice();
	if (!device) {
		return 1;
	}
	device->setWindowCaption(L"SelectDevice test");
	IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();

	device->getCursorControl()->setVisible(false);

	ISceneNode* cube = smgr->addCubeSceneNode();
	cube->setPosition(vector3df(0, 0, 50));
	cube->setMaterialTexture(0, device->getVideoDriver()->getTexture("wood.jpg"));
	cube->setMaterialFlag(video::EMF_LIGHTING, false);

	smgr->addCameraSceneNodeFPS();

	while (device->run()) {
		driver->beginScene(true, true, SColor(255, 100, 101, 140));
		smgr->drawAll();
		guienv->drawAll();
		driver->endScene();
	}
	device->drop();
	return 0;
}

Note: This is a simple-standard main.cpp file, that includes the "selectdevice.h" file and runs the selectDevice() function.

This function will manage the device parameters for you, and it'll show the last configuration before create the Device.
Take a look into "selectdevice.h" source:

"selectdevice.h"

Code: Select all

/* =================
 * SELECTDEVICE TOOL
 * =================
 *
 * Overview:
 * this source provides a device configuration tool for an
 * Irrlicht application
 *
 * The static irr::IrrlichtDevice* selectDevice() function,
 * loads a ini file (if exists) and shows a configuration
 * interface where the user can set the following variables:
 *
 * - Driver Type
 * - Resolution
 * - Driver params:
 * 			Bits, Fullscreen, Stencilbuffer, Vsync, AntiAlias
 * 			HighPrecisionFPU, Doublebuffer
 *
 * When the user click on "Launch" button, this configuration
 * is saved to file, for next loading.
 *
 * On the next run of the "selectDevice()" function, the
 * configuration saved is loaded from file and shown in the
 * configuration gui.
 * In this way, the main program will always show the last
 * configuration loaded from file.
 *
 *
 * USAGE
 * =====
 * 1) copy the selectdevice.h in your source directory
 * 2) add in your main c++ file the include:
 *
 * 		#include "selectdevice.h"
 *
 * 3) use following code to show the gui and let the user choose
 *    video parameters:
 *
 *     IrrlichtDevice* device = selectDevice();
 *
 * 4) enjoy
 *
 * NOTE
 * ====
 * - tested on Ubuntu 10.04 and Windows XP PRO
 * - this file uses the built-in font (which is small), but
 *   there's no need to use external files
 * - the resolution listbox is just an example, fill it with
 *   your resolutions
 *
 * LICENCE
 * =======
 * This file is free and open: it can be used for free and commercial purpose.
 * If you modify this file you must keep the original author in this note
 * If you use it in your projects (commercial or free) a credit to me is very
 * appreciated (but not mandatory).
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the author be held liable for any damages arising
 * from the use of this software.
 *
 * AUTHOR
 * ======
 * SelectDevice version 0.1 (2011-05-01)
 * Author: silver.slade
 * Email: silver.slade@tiscali.it
 * Web: http://www.slade.altervista.org
 *
 */

#ifndef SELECTDEVICE_H_
#define SELECTDEVICE_H_

#include <irrlicht.h>

#define CONFIGURATION_FILE "irrlicht.ini"

enum
{
	GUI_ID_QUIT_BUTTON = 101,
	GUI_ID_LAUNCH_BUTTON,
	GUI_ID_ABOUT_BUTTON
};

bool quit = false;
bool launch = false;
irr::IrrlichtDevice *nulldevice;

/**
 * ConfiguratorEventReceiver
 * Managed Action only for "Quit","Launch"
 * and "About" buttons
 */
class ConfiguratorEventReceiver : public irr::IEventReceiver
{
public:
	virtual bool OnEvent(const irr::SEvent& event)
	{
		if (event.EventType == irr::EET_GUI_EVENT)
		{
			irr::s32 id = event.GUIEvent.Caller->getID();
			switch(event.GUIEvent.EventType)
			{
				case irr::gui::EGET_BUTTON_CLICKED:
				{
					switch(id)
					{
						case GUI_ID_QUIT_BUTTON:
							quit=true;
							return true;
						case GUI_ID_LAUNCH_BUTTON:
							launch=true;
							nulldevice->closeDevice();
							return true;
						case GUI_ID_ABOUT_BUTTON:
							nulldevice->getGUIEnvironment()->addMessageBox(L"SelectDevice Utility", L"SelectDevice by silver.slade");
							return true;
						default:
							return false;
					}
				}
				default:
					return false;
			}
		}
		return false;
	}
};

namespace irr
{
	// Type Driver ------------------------------
	const wchar_t* const driverstrings[] = {
		L"NullDriver",
		L"Software Renderer",
		L"Burning's Video",
		L"Direct3D 8.1",
		L"Direct3D 9.0c",
		L"OpenGL 1.x/2.x/3.x"
	};
   irr::video::E_DRIVER_TYPE drivervalues[]= {
	   irr::video::EDT_NULL,
	   irr::video::EDT_SOFTWARE,
	   irr::video::EDT_BURNINGSVIDEO,
	   irr::video::EDT_DIRECT3D8,
	   irr::video::EDT_DIRECT3D9,
	   irr::video::EDT_OPENGL
   };

   // Video Resolutions -------------------------
   const wchar_t* const resolutionstrings[] = {
		L"640x480",
		L"800x600",
		L"1280x800"
	};
	core::dimension2d<u32> const resolutionvalues[] = {
		core::dimension2d<u32>(640,480),
		core::dimension2d<u32>(800,600),
		core::dimension2d<u32>(1280,800)
	};

	// Bits -------------------------------------
	const wchar_t* const bitsstrings[] = {
		L"16",
		L"32"
	};
	int const bitsvalues[] = {
		16,
		32,
	};

	// Gui Checkbox -----------------------------
	gui::IGUICheckBox* cfullscreen;
	gui::IGUICheckBox* cstencilbuffer;
	gui::IGUICheckBox* cvsync;
	gui::IGUICheckBox* cantialiasing;
	gui::IGUICheckBox* chpf;
	gui::IGUICheckBox* cdoublebuffer;

	// Configuration variables ------------------
	int drivercount=0;
	int resolutioncount=0;
	int bitscount=0;
	int bfullscreen=0;
	int bstencilbuffer=0;
	int bvsync=0;
	int bantialiasing=0;
	int bhpf=0;
	int bdoublebuffer=0;

	static irr::IrrlichtDevice* selectDevice()
	{
		// Try to Load configuration from file is exits
		FILE *fp;
		if((fp=fopen(CONFIGURATION_FILE, "r"))!=NULL) {
			fscanf(fp, "driver=%d\n", &drivercount);
			fscanf(fp, "resolution=%d\n", &resolutioncount);
			fscanf(fp, "bits=%d\n", &bitscount);
			fscanf(fp, "fullscreen=%d\n", &bfullscreen);
			fscanf(fp, "stencilbuffer=%d\n", &bstencilbuffer);
			fscanf(fp, "vsync=%d\n", &bvsync);
			fscanf(fp, "antialiasing=%d\n", &bantialiasing);
			fscanf(fp, "hpf=%d\n", &bhpf);
			fscanf(fp, "doublebuffer=%d\n", &bdoublebuffer);
			fclose(fp);
		}

		// Main Configuration Gui
		nulldevice = createDevice(video::EDT_SOFTWARE,
				core::dimension2d<u32>(500, 260), 32, false, false, false);
		nulldevice->setWindowCaption(L"Irrlicht SelectDevice Utility");
		ConfiguratorEventReceiver* cer = new ConfiguratorEventReceiver();
		nulldevice->setEventReceiver(cer);

		video::IVideoDriver* driver = nulldevice->getVideoDriver();
		gui::IGUIEnvironment* guienv = nulldevice->getGUIEnvironment();

		// Gui - Buttons
		guienv->addButton(core::rect<s32>(10,220,110,220 + 30), 0, GUI_ID_QUIT_BUTTON,L"Quit", L"Exits Program");
		guienv->addButton(core::rect<s32>(120,220,220,220 + 30), 0, GUI_ID_LAUNCH_BUTTON,L"Launch", L"Launch Program");
		guienv->addButton(core::rect<s32>(230,220,330,220 + 30), 0, GUI_ID_ABOUT_BUTTON,L"About", L"About SelectDevice Utility");

		// GUI - driver listbox
		guienv->addStaticText(L"Choose driver:",core::rect<int>(10,10,120,30));
		gui::IGUIListBox* ldriver = guienv->addListBox(core::rect<int>(10,30,120,200));
		for (int var = 0; var < irr::video::EDT_COUNT; ++var) {
			ldriver->addItem(driverstrings[var]);
		}
		ldriver->setSelected(drivercount);
		ldriver->setDrawBackground(true);

		// GUI - resolution listbox
		guienv->addStaticText(L"Choose resolution:",core::rect<int>(130,10,230,30));
		gui::IGUIListBox* lresolution = guienv->addListBox(core::rect<int>(140,30,220,200));
		for (int var = 0; var < 3; ++var) {
			lresolution->addItem(resolutionstrings[var]);
		}
		lresolution->setSelected(resolutioncount);
		lresolution->setDrawBackground(true);

		// GUI - bits listbox
		guienv->addStaticText(L"Choose bits:",core::rect<int>(240,10,330,30));
		gui::IGUIListBox* lbits = guienv->addListBox(core::rect<int>(240,30,320,200));
		for (int var = 0; var < 2; ++var) {
			lbits->addItem(bitsstrings[var]);
		}
		lbits->setSelected(bitscount);
		lbits->setDrawBackground(true);

		// GUI - Checkboxes
		int x = 20;  // offset over x
		int y = 340; // offset over y for checkboxes
		guienv->addStaticText(L"Choose flags:",core::rect<int>(y,10,y+90,30));
		cfullscreen =    guienv->addCheckBox(bfullscreen, core::rect<int>(y,30,y+100,30+x),0, 1, L"Fullscreen");
		cstencilbuffer = guienv->addCheckBox(bstencilbuffer, core::rect<int>(y,60,y+100,60+x),0, 2, L"Stencilbuffer");
		cvsync =         guienv->addCheckBox(bvsync, core::rect<int>(y,90,y+100,90+x),0, 3, L"Vsync");
		cantialiasing =  guienv->addCheckBox(bantialiasing, core::rect<int>(y,120,y+100,120+x),0, 4, L"AntiAliasin");
		chpf =           guienv->addCheckBox(bhpf, core::rect<int>(y,150,y+100,150+x),0, 5, L"HighPrecisionFPU");
		cdoublebuffer =  guienv->addCheckBox(bdoublebuffer, core::rect<int>(y,180,y+100,180+x),0, 6, L"Doublebuffer");

		while (nulldevice->run() && !quit)
		{
			if (nulldevice->isWindowActive())
			{
				driver->beginScene(true, true, video::SColor(0,200,200,200));
				guienv->drawAll();
				driver->endScene();
			}
		}

		if (quit)
		{
			nulldevice->drop();
			return 0;
		}

		if (launch)
		{
			// Saving configuration to file...
			bfullscreen    = cfullscreen->isChecked();
			bstencilbuffer = cstencilbuffer->isChecked();
			bvsync         = cvsync->isChecked();
			bantialiasing  = cantialiasing->isChecked();
			bhpf           = chpf->isChecked();
			bdoublebuffer  = cdoublebuffer->isChecked();

			// Saving
			FILE *fp;
			if((fp=fopen(CONFIGURATION_FILE, "w+"))==NULL) {
				printf("Error: cannot open file.\n");
				if (nulldevice) nulldevice->drop();
				return 0;
			}
			fprintf(fp, "driver=%d\n",        ldriver->getSelected());
			fprintf(fp, "resolution=%d\n",    lresolution->getSelected());
			fprintf(fp, "bits=%d\n",          lbits->getSelected());
			fprintf(fp, "fullscreen=%d\n",    bfullscreen);
			fprintf(fp, "stencilbuffer=%d\n", bstencilbuffer);
			fprintf(fp, "vsync=%d\n",         bvsync);
			fprintf(fp, "antialiasing=%d\n",  bantialiasing);
			fprintf(fp, "hpf=%d\n",           bhpf);
			fprintf(fp, "doublebuffer=%d\n",  bdoublebuffer);
			fclose(fp);

			// Prepare and return the driver pointer
			irr::SIrrlichtCreationParameters params;
			params.DriverType = drivervalues[ldriver->getSelected()];
			params.WindowSize = resolutionvalues[lresolution->getSelected()];
			params.Bits = bitsvalues[lbits->getSelected()];
			params.Fullscreen = bfullscreen;
			params.Stencilbuffer = bstencilbuffer;
			params.Vsync = bvsync;
			params.AntiAlias = bantialiasing;
			params.HighPrecisionFPU = bhpf;
			params.LoggingLevel = ELL_ERROR;
			params.Doublebuffer = bdoublebuffer;
			if (nulldevice) nulldevice->drop();
			return createDeviceEx(params);
		}
		return 0;
	}
}
#endif /* SELECTDEVICE_H_ */

The result is:

Image


The source is public and free for any purpose, I hope it'll be useful for someone.
Comments and ideas are welcome.

To download a bin example (with full code) see here:
http://www.slade.altervista.org
in the download section.

Long live and prosper
:D
silver.slade
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Thanks. We also have such an example in the next Irrlicht SDK http://irrlicht.svn.sourceforge.net/vie ... iew=markup
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

The original binary crashes because of missing libgcc_s_dw2-1.dll (I`m using MSVC on WinXP SP3). Thanks for sharing.

@hybrid: Cool. I was wondering when we`d get it in the SDK. :)
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
Dareltibus
Posts: 115
Joined: Mon May 17, 2010 7:42 am

Post by Dareltibus »

there are already 3 similiar code snippets in there that does exactly the same.... why not spending time in something new? ;-)
silver.slade
Posts: 17
Joined: Fri Jan 29, 2010 8:54 am
Contact:

Post by silver.slade »

shadowslair wrote:The original binary crashes because of missing libgcc_s_dw2-1.dll (I`m using MSVC on WinXP SP3). Thanks for sharing.

@hybrid: Cool. I was wondering when we`d get it in the SDK. :)
Hi shadowslair!

It's a MinGW problem (and a fault of mine, with static linking...)
Try now please, I've uploaded a new version. Let me know if the dll is missing again.

About the SDK, I hope I haven't ruined a surprise!! ^_^

Long live
silver.slade
:-D
silver.slade
Posts: 17
Joined: Fri Jan 29, 2010 8:54 am
Contact:

Post by silver.slade »

Dareltibus wrote:there are already 3 similiar code snippets in there that does exactly the same.... why not spending time in something new? ;-)
Hi Dareltibus!
So, Did I reinvented the (a) wheel ?!?!? ;-)

Something new....mmm....

Take a look at this:
http://www.youtube.com/watch?v=ScniyBdEUlE

bb
silver.slade
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

silver.slade wrote: So, Did I reinvented the (a) wheel ?!?!? ;-)
Yes you did ;)

The video is nice, though I don't see anything special in it. It could be hacked up in a few days, or even hours, as it looks like bunch of code snippets joined into an app.
Working on game: Marrbles (Currently stopped).
ChaiRuiPeng
Posts: 363
Joined: Thu Dec 16, 2010 8:50 pm
Location: Somewhere in the clouds.. drinking pink lemonade and sunshine..

Post by ChaiRuiPeng »

silver.slade wrote:
Dareltibus wrote: Take a look at this:
http://www.youtube.com/watch?v=ScniyBdEUlE
meh... :roll:


something special would be like seamless lod, or streaming terrain with no hiccups, or maybe CUDA to do real time custom fluids simulation :)
ent1ty wrote: success is a matter of concentration and desire
Butler Lampson wrote: all problems in Computer Science can be solved by another level of indirection
at a cost measure in computer resources ;)
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

It would be special for you, special for me, but for someone who is just starting with Irrlicht this might be special too. Don't forget how relative the point of view is :wink:
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Grumpy
Posts: 77
Joined: Wed Dec 30, 2009 7:17 pm
Location: Montana, Usa

Post by Grumpy »

hybrid Wrote:
Thanks. We also have such an example in the next Irrlicht SDK http://irrlicht.svn.sourceforge.net/vie ... iew=markup
Any chance we can take a peek at more of the new tuts... ???
code happens
silver.slade
Posts: 17
Joined: Fri Jan 29, 2010 8:54 am
Contact:

Post by silver.slade »

serengeor wrote: The video is nice, though I don't see anything special in it. It could be hacked up in a few days, or even hours, as it looks like bunch of code snippets joined into an app.
Yes it was.
It took hours, but it has compiled, at the end.
;-)


BTW, for me it was "special".
Every c++ application that runs, it's "special".

silver.slade
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

ent1ty wrote:It would be special for you, special for me, but for someone who is just starting with Irrlicht this might be special too. Don't forget how relative the point of view is
Yeah, I guess you're right :roll:
Grumpy wrote:hybrid Wrote:
Thanks. We also have such an example in the next Irrlicht SDK http://irrlicht.svn.sourceforge.net/vie ... iew=markup
Any chance we can take a peek at more of the new tuts... ???
They should be in svn I think.
Working on game: Marrbles (Currently stopped).
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

[quote="serengeor]The video is nice, though I don't see anything special in it. It could be hacked up in a few days, or even hours, as it looks like bunch of code snippets joined into an app.[/quote]


This forum has really taken a turn for the worse with all the kids that are on it..............
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

Yes, but i reckon it's happening all over the world. So maybe the end of the world in 2012 == kids overthrow us? :D

(us == adults, i am 18 already :P :lol: )
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

I actually started looking at other engines because of the way this forum has changed over the past few years. It has taken a serious negative turn, to the point where it isnt even fun anymore. Imagine this post, "hey guys, I spent my time writing this and thought you might want to look at it!" answered with, "that shouldnt have taken more than an hour to write" wtf? I would keep typing but I am too tired of it to bother.........
Post Reply