2D drawing helper function

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
ngc92
Posts: 11
Joined: Thu Nov 19, 2009 3:56 pm

2D drawing helper function

Post by ngc92 »

when doing 2d drawing, I often find myself writing code like

Code: Select all

 
driver>draw2DImage(texture, position, core::recti(core::vector2d<s32>(0,0), texture->getSize()), 0, color, true);
 
the part to create the rect that contains the rect creation takes up a significant portion of the parameters, even though i want the simple "draw everything" behaviour. Therefore, i would suggest adding to ITexture.h the lines [+ the rect.h include]

Code: Select all

 
//! Gets source rectangle (0,0,Width,Hwight) to draw the whole texture.
/** \return rectangle that contains the whole texture */
core::recti getRect() const
{
    return core::recti( 0, 0, Size.Width, Size.Height );
}
 
to make the abouve code look clearer (and, at least in my experience, most of the time i want to draw the whole texture, so this should be a pretty common usecase)

Code: Select all

 
driver>draw2DImage(texture, position, texture->getRect(), 0, color, true);
 
Is there a special reason this is not already in irrlicht (except the obvious "nobody has had the time to do it")?

any thoughts?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: 2D drawing helper function

Post by CuteAlien »

It's a function that's not absolutely necessary and returns redundant information. And that for a minor use-case which is already solvable. Adding functions for that kind of situations would blow up interfaces and that makes the engine harder to understand than the occasional long line in the code. Notice that you could also add a "using core;" in the file of your code above to reduce the line somewhat.

But I think we have another solution here to make it shorter. We can add another rect constructor which takes only a dimension2d parameter and set left-top to 0,0 in that case.
Which would reduce the line to:

Code: Select all

 
driver>draw2DImage(texture, position, recti(texture->getSize()), 0, color, true);
 
I think that's generally useful enough to be worth adding. Will do that on the weekend unless someone sees a good reason to not do that...
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
ngc92
Posts: 11
Joined: Thu Nov 19, 2009 3:56 pm

Re: 2D drawing helper function

Post by ngc92 »

yeah, you are right, that change would have a relatively specific use case, but at least for me that is something i write quite often.
Your solution seems better, wouldn't it even be possible to write

Code: Select all

 
driver>draw2DImage(texture, position, texture->getSize(), 0, color, true);
 
because the constructor would automatically be called here (unless you make it explicit)?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: 2D drawing helper function

Post by CuteAlien »

Hehe, you put your finger on just the question I was asking myself yesterday. If this would be better explicit or not...
I tend to use explicit nearly always for single constructor parameters as automatic conversions can be really confusing. It would be kinda nice in this case. But I think I prefer the explicit conversion.
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
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: 2D drawing helper function

Post by CuteAlien »

New rect constructor added in svn trunk r5036.
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
Post Reply