IGUIImage Source Rect

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
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

IGUIImage Source Rect

Post by LunaRebirth »

Hello,

I'm trying to make a 3D game with some 2D qualities as well. The 2D part of the game is building your own level out of tiles as a sort of map.
Anyways, I want to stick to using IGUIImage for a function I am using a ton of, and cannot get the tiles part to work.
I currently have so you can select a tile on a tilemap, then place that tile down. But thing is, the tile is not selecting properly.

Here's my code:

Code: Select all

tiles.push_back(env->addImage(core::rect<s32>(srcX,srcY,srcX+tilesWidth,srcY+tilesHeight)));
tiles[tiles.size()-1]->setImage(driver->getTexture((char*)IMG.c_str()));
I'm using vectors to store the tiles separately. What should happen that I want is if you select a tile on X:32,Y:32, you place that tile down. But instead, what's happening is it will always place an X:0,Y:0 tile down.
Each tile is 16x16, and no matter what, I can't get it to drop the tile you selected.

If I change it to something like core::rect<s32>(0,0,32,32) the tile will be a rectangle from the tilemap of the square starting at 0,0 and ending 32,32, which is fine.
But if I change it to something like core::rect<s32>(32,32,64,64) It's the same tile as 0,0,32,32???
That doesn't make sense to me.

Any suggestions on how to fix this issue?
Thanks!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: IGUIImage Source Rect

Post by CuteAlien »

Not sure that you mean by *it*. Which function are you calling for that? The rect passed to addImage set's the position where the image will be drawn on the screen. If you want to select a rectangle-part from a texture as source then you have to use Irrlicht svn trunk as Irrlicht 1.8 didn't have a function for that yet. In trunk we have now a function IGUIImage::setSourceRect which can be used.

If that wasn't it I need a little more code or explanation to understand the problem.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: IGUIImage Source Rect

Post by LunaRebirth »

What I want is to basically take a full tileset map, and "crop" it to the size of one tile (16x16) so I can get just a piece of the entire tileset to be one tile.
I believe you understand, but I made an image in case:
Image
As you clarified, that makes the image go to that position, but if I change the rect to 0,0,10,10 the size of the tile is a 10x10 in pixels. If I change it to 0,0,20,20 the size of the tile is 20x20. If I change it to 10,10,15,15 the size of the tile is 5x5. So I don't think that's changing the position, I believe that is the rectangle size.

Sorry, I'm trying to explain this in as much detail as possible, but not sure how.
When I used to use SDL for 2D graphics, we had a thing called SDL_BlitSurface which allowed us to do what this topic is about. (http://lazyfoo.net/SDL_tutorials/lesson06/index.php)

How can I get this trunk for setSourceRect?

Thanks a bunch!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: IGUIImage Source Rect

Post by CuteAlien »

You find svn sources here: http://sourceforge.net/p/irrlicht/code/HEAD/tree/
You need a svn client to check them out. On Windows you can use subversion, on Linux install a subversion package and then type in that svn command you'll see at my link.
Project&Build files for Irrlicht can then be found in the source/Irrlicht folder.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: IGUIImage Source Rect

Post by LunaRebirth »

Hello,
We couldn't figure it out. Both, a friend and I, tried downloading TortoiseSVN to access the files, but didn't work. Cannot view the sources from the trunk on sourceforge directly, and Tortoise is not working.
Is there maybe a work-around? Can ITextures have source rectangles? IGUIElements? Found nothing on it online...

I'd really like to get this going, but IGUIImage is not allowing me to do what I would like.

Thanks!
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: IGUIImage Source Rect

Post by mongoose7 »

Just download a ZIP file of 'trunk' and use that.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: IGUIImage Source Rect

Post by CuteAlien »

Maybe we can help if you tell us what happened exactly. Just to make sure, this is the tortoise we are talking about: http://tortoisesvn.net/

It installs directly into the Windows-explorer. Meaning you can right-click folders and select something like check-out there (I'm not on Windows right now, but I suppose it's called check-out). Then you have to enter the path of the server as by my link.

Unfortunately Sourceforge servers are having troubles since a week or so and connections break down all the time :-( The forum also constantly fails to work the last days. I think their servers are simply overloaded. So if you get errors - just wait a few minutes and try again. It tends to work after a few tries *sigh*.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

Re: IGUIImage Source Rect

Post by chronologicaldot »

Some helpful info for you LunaRebirth:

Looking at the source code for CGUIEnvironment (the implementation of IGUIEnvironment) shows this:

Code: Select all

 
IGUIImage* CGUIEnvironment::addImage(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text, bool useAlphaChannel)
{
    IGUIImage* img = new CGUIImage(this, parent ? parent : this,
        id, rectangle);
 
    if (text)
        img->setText(text);
 
    if ( useAlphaChannel )
        img->setUseAlphaChannel(true);
 
    img->drop();
    return img;
}
 
It turns out, CGUIImage inherits IGUIImage which in turn inherits IGUIElement. The rectangle you are passing in is, thus, the position of the image, not the source rectangle of the image.

The CGUIImage::draw() function is this:

Code: Select all

 
void CGUIImage::draw()
{
    if (!IsVisible)
        return;
 
    IGUISkin* skin = Environment->getSkin();
    video::IVideoDriver* driver = Environment->getVideoDriver();
 
    if (Texture)
    {
        if (ScaleImage)
        {
            const video::SColor Colors[] = {Color,Color,Color,Color};
 
            driver->draw2DImage(Texture, AbsoluteRect,
                core::rect<s32>(core::position2d<s32>(0,0), core::dimension2di(Texture->getOriginalSize())),
                &AbsoluteClippingRect, Colors, UseAlphaChannel);
        }
        else
        {
            driver->draw2DImage(Texture, AbsoluteRect.UpperLeftCorner,
                core::rect<s32>(core::position2d<s32>(0,0), core::dimension2di(Texture->getOriginalSize())),
                &AbsoluteClippingRect, Color, UseAlphaChannel);
        }
    }
    else
    {
        skin->draw2DRectangle(this, skin->getColor(EGDC_3D_DARK_SHADOW), AbsoluteRect, &AbsoluteClippingRect);
    }
 
    IGUIElement::draw();
}
 
Since it is drawing the full-size image, my suggestion would be to make the image fullsize to begin with and simply change the absolute clipping rectangle, which would need to be done by creating a custom GUI element for the parent of the image element and whose clipping rectangle is the section of the image you want. If you want to start moving things, you need to synchronize moving the image and the GUI elements with their clipping rectangles.
That's how'd I'd do it, at least on first glance.
Post Reply