IGUIEnvironment::addImage() Image size

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
rewb0rn
Posts: 15
Joined: Thu May 25, 2006 9:26 pm
Location: Berlin, Germany

IGUIEnvironment::addImage() Image size

Post by rewb0rn »

Hi, is there a way to easily change the size of an Image created with the method IGUIEnvironment::addImage()? I already searched the forums and found that texture resize post, but i think thats way to much effort. It should be as easy as in the driver method draw2DImage where you can specify the size.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

As you noticed, the current IGUIImage interface does not provide a mechanism for resizing the image. I see three choices.
  1. Patch the existing CGUIImage implementation
  2. Make your own CGUIImage implementation that is a copy of CGUIImage and fixes the problem.
  3. Make direct calls to draw2DImage
The change to fix it is just one line, so it is pretty simple. Just change the call to draw2DImage() in CGUIImage::draw() to the following.

Code: Select all

      driver->draw2DImage(Texture, RelativeRect, 
         core::rect<s32>(core::position2d<s32>(0,0), Texture->getOriginalSize()),
         &AbsoluteClippingRect, 0, UseAlphaChannel);
Travis
rewb0rn
Posts: 15
Joined: Thu May 25, 2006 9:26 pm
Location: Berlin, Germany

Post by rewb0rn »

Ok, thx so far. I think Im gonna derive my own Image class, because we are working in a team so I cant just change the engines code.
Atm im not quite sure if that will work as I hope because I dont know how I can add my own class to the environment, I will check on that later and post results.
rewb0rn
Posts: 15
Joined: Thu May 25, 2006 9:26 pm
Location: Berlin, Germany

Post by rewb0rn »

Ok I made a class that makes resizing possible.
If anyone is interrested:

Code: Select all

//************************************************
//HEADER
#ifndef IRRDYNIMAGE
#define IRRDYNIMAGE

#include "main.h"

class IrrDynImage : public irr::gui::IGUIImage
{
private:
	irr::video::ITexture* Texture;
	bool UseAlphaChannel;
public:
	
	IrrDynImage(irr::gui::IGUIEnvironment* Environment, IGUIElement* Parent, irr::core::rect<irr::s32> Rectangle, 
		irr::s32 ID = -1, irr::video::ITexture* Image = 0, bool useAlphaChannel = true, const wchar_t* Text = 0);
	~IrrDynImage();

	void setImage(irr::video::ITexture* image);
	void setUseAlphaChannel(bool use = true);
	void draw(void);
};

#endif


//************************************************
//CPP
#include "irrdynimage.h"


//! constructor
IrrDynImage::IrrDynImage(irr::gui::IGUIEnvironment* Environment, IGUIElement* Parent, irr::core::rect<irr::s32> Rectangle, 
						 irr::s32 ID, irr::video::ITexture* Image, bool useAlphaChannel, const wchar_t* Text)
	: IGUIImage(Environment, Parent, ID, Rectangle), Texture(0), UseAlphaChannel(false)
{
	setImage(Image);
	setUseAlphaChannel(useAlphaChannel);
	setText(Text);

	drop();

	#ifdef _DEBUG
	setDebugName("CGUIImage");
	#endif
}

//! destructor
IrrDynImage::~IrrDynImage()
{
	if (Texture)
		Texture->drop();
}

//! sets an image
void IrrDynImage::setImage(irr::video::ITexture* image)
{
	if (Texture)
		Texture->drop();

	Texture = image;

	if (Texture)
		Texture->grab();
}

//! draws the element and its children
void IrrDynImage::draw()
{
	if (!IsVisible)
		return;

	irr::gui::IGUISkin* skin = Environment->getSkin();
	irr::video::IVideoDriver* driver = Environment->getVideoDriver();

	if (Texture)
		driver->draw2DImage(Texture, AbsoluteRect,
		irr::core::rect<irr::s32>(irr::core::position2d<irr::s32>(0,0), Texture->getOriginalSize()),
         &AbsoluteClippingRect, 0, UseAlphaChannel);
	else
		driver->draw2DRectangle(skin->getColor(irr::gui::EGDC_3D_DARK_SHADOW), AbsoluteRect, &AbsoluteClippingRect);

	IGUIElement::draw();
}

//! sets if the image should use its alpha channel to draw itself
void IrrDynImage::setUseAlphaChannel(bool use)
{
	UseAlphaChannel = use;
}

//*****************************************
//Example:
Elements[i] = new IrrDynImage(Env, Parent, irr::core::rect<irr::s32>(0,0,100,100), ID, Driver->getTexture(Image.Source), UseAlpha, Text);

//instead of 
Elements[i] = Env->addImage(blah);
If anyone has suggestions to make it better Id be glad to hear them.
Post Reply