Restart devices (resizing, etc. at runtime)

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
-insane-
Posts: 18
Joined: Tue Aug 04, 2009 4:41 pm
Location: Germany

Restart devices (resizing, etc. at runtime)

Post by -insane- »

With this code you can change resolution, bpp, fullscreen, etc. To change resolution and bpp, press Space and to quit this sample, press ESC and then Space. Hope it helps you.

Code: Select all

#include <irrlicht.h>

#pragma comment(lib, "Irrlicht.lib")

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

bool resize = false, stop = false;
int width = 640, height = 480, bpp = 16;

class Receiver : public IEventReceiver
{
protected:
	virtual bool OnEvent (const SEvent& event);
};

bool Receiver::OnEvent (const SEvent& event)
{
	if (event.EventType == EET_KEY_INPUT_EVENT)
	{
		if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown)
		{
			stop = true;
			return true;
		}
		else if (event.KeyInput.Key == KEY_SPACE && event.KeyInput.PressedDown)
		{
			width = 800, height = 600, bpp = 32;
			resize = true;
			return true;
		}
	}

	return false;
}

int main (void)
{
	Receiver* receiver = NULL;

	while (!stop)
	{
		IrrlichtDevice* device = createDevice (EDT_OPENGL, dimension2d<s32>(width, height), bpp, false, false, false, NULL);

		if (!device)
			return 0;

		IVideoDriver* driver = device->getVideoDriver();
		ISceneManager* smgr = device->getSceneManager();

		receiver = new Receiver;

		device->setEventReceiver(receiver);

		while (device->run() && !resize)
		{
			driver->beginScene(true, true, SColor(255,100,101,140));
			smgr->drawAll();
			driver->endScene();
		}

		resize = false;
		
		if (receiver)
		{
			delete receiver;
			receiver = NULL;
		}

		if (device)
		{
			device->closeDevice();
			device->run(); // Very important to do this here!
			device->drop();
		}
	}

	return 0;
}
Don't forget to reload all objects.
cheshirekow
Posts: 105
Joined: Mon Jul 27, 2009 4:06 pm
Location: Cambridge, MA

Post by cheshirekow »

A very nice, simple example of how to do this. Thank you.
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

But the scene and all the loaded data will be lost anyways or not?
d3jake
Posts: 198
Joined: Sat Mar 22, 2008 7:49 pm
Location: United States of America

Post by d3jake »

From what I"m reading (and it could just be that it's midnight where I"m sitting) the device is being created and dropped every frame?
The Open Descent Foundation is always looking for programmers! http://www.odf-online.org
"I'll find out if what I deleted was vital here shortly..." -d3jake
Brainsaw
Posts: 1183
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

@Halan: yes, all Irrlicht data will be lost, so you have to think of your own way to store these information. Or don't give the user the option to change resolution during gameplay (that's how I did it in my current project).
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
zerochen
Posts: 273
Joined: Wed Jan 07, 2009 1:17 am
Location: Germany

Post by zerochen »

@d3jake
no there are two while loops
Post Reply