Page 1 of 1

2D drawing helper function

Posted: Wed Feb 04, 2015 7:52 pm
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?

Re: 2D drawing helper function

Posted: Thu Feb 05, 2015 1:45 pm
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...

Re: 2D drawing helper function

Posted: Fri Feb 06, 2015 12:46 am
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)?

Re: 2D drawing helper function

Posted: Fri Feb 06, 2015 11:16 am
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.

Re: 2D drawing helper function

Posted: Mon Feb 09, 2015 3:16 pm
by CuteAlien
New rect constructor added in svn trunk r5036.