Post removed.

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
TheRLG
Posts: 372
Joined: Thu Oct 07, 2004 11:20 pm

Post removed.

Post by TheRLG »

Post removed.
Last edited by TheRLG on Fri Dec 28, 2007 7:34 am, edited 2 times in total.
andrei25ni
Posts: 326
Joined: Wed Dec 14, 2005 10:08 pm

Post by andrei25ni »

Simple yet powerful. Nice work ! :wink:
TheRLG
Posts: 372
Joined: Thu Oct 07, 2004 11:20 pm

Post by TheRLG »

Post removed.
Last edited by TheRLG on Fri Dec 28, 2007 7:35 am, edited 1 time in total.
AndyCR
Posts: 110
Joined: Tue Nov 08, 2005 2:51 pm
Location: Colorado, USA
Contact:

Post by AndyCR »

or, even better: :D

Code: Select all

irr::core::rect<irr::s32> EasyRect(irr::s32 X, irr::s32 Y, irr::s32 Width, irr::s32 Height) 
{ 
    return irr::core::rect<irr::s32>(X,Y,X+Width,Y+Height); 
}
Baal Cadar
Posts: 377
Joined: Fri Oct 28, 2005 10:28 am
Contact:

Post by Baal Cadar »

Or yet even better to not camoflage this with a seperate function at all. Initialise the rect directly with X,Y,X+Width,Y+Height.

Seriously, using EasyRect makes the code harder to read in the end.
TheRLG
Posts: 372
Joined: Thu Oct 07, 2004 11:20 pm

Post by TheRLG »

Post removed.
Last edited by TheRLG on Fri Dec 28, 2007 7:35 am, edited 1 time in total.
Baal Cadar
Posts: 377
Joined: Fri Oct 28, 2005 10:28 am
Contact:

Post by Baal Cadar »

Actually I proposed (and still do) to not create a seperate function at all for this task. Instead do what EasyRect does in-place. This little complexity doesn't warrant a dedicated function imho. I am all for functional decomposition, but this drives it too far.

Rationale: Source code has to be obvious. One should see on the first glance what is done. Question is, whether it is easier to read:

Code: Select all

guiEnv->addButton(rect<s32>(left, top, left + width, top + height));
or

Code: Select all

guiEnv->addButton(EasyRect(left, top, width, height));
Without prior knowledge about what EasyRect does, the code above is easier to read. Else it is equal. So you win nothing and loose a bit in this aspect.
Performance is equal when the function is inlined, less else.
Other aspects don't play a role here, imho.
So at the end of the day EasyRect usage makes the code slightly worse.

In an effort to be a bit constructive here, I propose two changes. Make it inline and make it a template function.

Code: Select all

template<typename T> inline  irr::core::rect<T> EasyRect(T x, T y, T width, T height)
{
    return irr::core::rect<T>(x, y, x + width, y + height);
}
AndyCR
Posts: 110
Joined: Tue Nov 08, 2005 2:51 pm
Location: Colorado, USA
Contact:

Post by AndyCR »

As it turns out, rect has a constructor for position2d, dimension2d which does the exact same thing (position2d, position2d+dimension2d).
TheRLG
Posts: 372
Joined: Thu Oct 07, 2004 11:20 pm

Post by TheRLG »

Post removed.
Last edited by TheRLG on Fri Dec 28, 2007 7:35 am, edited 1 time in total.
xterminhate
Posts: 206
Joined: Thu Sep 01, 2005 9:26 pm
Location: France

Post by xterminhate »

Avoid multiple copy, use constness/references :

Code: Select all

irr::core::rect<irr::s32> EasyRect( irr::s32 const & X, irr::s32 const & Y, irr::s32 const & Width, irr::s32 const & Height) 
{ 
    return irr::core::rect<irr::s32>(X,Y,X+Width,Y+Height); 
}
Return to Irrlicht after years... I'm lovin it.
It's hard to be a Man !
Si vis pacem para belum
TheRLG
Posts: 372
Joined: Thu Oct 07, 2004 11:20 pm

Post by TheRLG »

Post removed.
Last edited by TheRLG on Fri Dec 28, 2007 7:35 am, edited 1 time in total.
Baal Cadar
Posts: 377
Joined: Fri Oct 28, 2005 10:28 am
Contact:

Post by Baal Cadar »

RabidLockerGnome, both are correct, though your proposed order is more common and thus to be preferred.

Anyway, I'd not use const& here. There is no positive effect whatsoever. Whether pointers are put on the stack or their contents is irrelevant here, since s32 is the same size as a pointer anyway. You also don't safe something on execution. Const references are useful, when the argument type is bigger than a pointer. Const correctness is a nice thing and can be done on the value type arguments too. Though I'd ommit those for the function prototype for readability reasons. There it doesn't matter anyway, since the function has call-by-value semantics. You can use const in the actual implementation then if you want.
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

That's really useful. From time to time I wish I had decided to make the rect work with a position and a dimension rather than two positions. But when I wrote the software renderer and the gui (which was the first thing I did, before even doing D3D or OpenGL), it was better that way. But that solution is a very nice workaround.
Post Reply