Using the C++ STL in graphics engine code?

Discussion about everything. New games, 3d math, development tips...
cr_itm
Posts: 57
Joined: Sun May 01, 2005 10:13 am

Using the C++ STL in graphics engine code?

Post by cr_itm »

Hello,

this should be an open discussion, I'm looking forward to your opinions. The question is, is it a good idea to use the c++ STL (standard template library) in projects like a graphics engine?

The question came up when I looked through several engines, like irrlicht or the Doom3 SDK. Containers like lists are all custom-implemented and also the std::string class is not used.

From my point of view, there are some things I might using the STL:

- portability
- it's part of the c++ standard
- the code is major (bug free) and optimized on speed


Is there any important point, not to use the STL classes in such a project like a graphics engine?


Thanks for your thoughts
zenaku
Posts: 212
Joined: Tue Jun 07, 2005 11:23 pm

Post by zenaku »

By irrlicht having it's own implementation, the classes like irrList can be optimized specifically for graphics.

I agree though. For the most part, you'd be better of using the STL whenever possible.

One major drawback would be the dependency on STL itself. You say it's standard, but has it been ported to PSP yet? ;) I know irrlicht has (http://www.irrlicht3d.org/pivot/entry.php?id=348). If irrlicht did depend upon the STL, porting to the PSP would have taken a lot more work.
-------------------------------------
IrrLua - a Lua binding for Irrlicht
http://irrlua.sourceforge.net/
jam
Posts: 409
Joined: Fri Nov 04, 2005 3:52 am

Post by jam »

In this thread people examined why Irrlicht does not use STL.
system-independent, adj.:
Works equally poorly on all systems.
-- unknown
cr_itm
Posts: 57
Joined: Sun May 01, 2005 10:13 am

Post by cr_itm »

zenaku wrote:One major drawback would be the dependency on STL itself. You say it's standard, but has it been ported to PSP yet? ;) I know irrlicht has (http://www.irrlicht3d.org/pivot/entry.php?id=348). If irrlicht did depend upon the STL, porting to the PSP would have taken a lot more work.

Good point, never thought of that.
jam wrote:In this thread people examined why Irrlicht does not use STL.
Thanks, I'll have a look.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I would be willing to bet that most STL implementations would work on PSP with little or no modification. Like other libraries, they usually provide some sort of config header that changes the features compiled in/out. The Standard C++ Library that I worked on for a while had been ported to several imbedded platforms with only changes to the configuration file.
Eternl Knight
Posts: 313
Joined: Tue Nov 01, 2005 5:01 am

Post by Eternl Knight »

While I understand Niko's desire to leave STL out of Irrlicht - I find it is a matter of preference only really. STL is simply a part of the C++ Standard (as has been mentioned by others) and any compiler that does not support it is not C++ Standard compliant. That said, platforms without a compliant C++ compiler are few and far between (and are not likely targets for apps using Irrlicht).

I personally like STL, and find it's speed just dandy in a "game engine". As such, I have a version of Irrlicht that uses STL instead of the Irrlicht supplied template classes. The only thing that needed 'custom' code was:
(a) converting strings between char* & w_char* representations, and
(b) getting a pointer to the data in a std::vector
The rest was simple class / function substitution.

--EK
zenaku
Posts: 212
Joined: Tue Jun 07, 2005 11:23 pm

Post by zenaku »

Eternl Knight wrote:While I understand Niko's desire to leave STL out
...
(b) getting a pointer to the data in a std::vector
...
--EK
That's definately not portable! It assumes the data is stored in a pointer, which it most likely is, but not in all cases of STL implementations. std::vector could be a linked list interally. Then there would be no pointer.
-------------------------------------
IrrLua - a Lua binding for Irrlicht
http://irrlua.sourceforge.net/
mandrav
Posts: 117
Joined: Sat Aug 27, 2005 8:29 pm
Contact:

Post by mandrav »

zenaku wrote:By irrlicht having it's own implementation, the classes like irrList can be optimized specifically for graphics.
Can you please describe how you can "optimize" a list for graphics?

zenaku wrote:
Eternl Knight wrote:While I understand Niko's desire to leave STL out
...
(b) getting a pointer to the data in a std::vector
...
--EK
That's definately not portable! It assumes the data is stored in a pointer, which it most likely is, but not in all cases of STL implementations. std::vector could be a linked list interally. Then there would be no pointer.
Again, unless I 'm misunderstanding everything, there's nothing more portable. A std::vector is guaranteed to put its contents in consecutive memory locations. It works and feels exactly like a C-array.

Yiannis.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, the description http://www.sgi.com/tech/stl/Vector.html which I frequently use does not impose such an requirement to the implementation. So I'd never rely on that, although most implementations will use some kind of array inside. But you cannot get a pointer to that even if it is used. Vector has some memory handling attributes aside, such that it is not just a C-array, but a class.
zenaku
Posts: 212
Joined: Tue Jun 07, 2005 11:23 pm

Post by zenaku »

Can you please describe how you can "optimize" a list for graphics?
You have hard coded, optimized versions instead of generic templates. There are lots of things you could do. Think something like a list of matrix4 optimized for SSE/3DNow/etc, or possibly a rendering display list abstracted as an irrList type. You beat the STL in speed by sacrificing code reuse and generic abstractions.
Again, unless I 'm misunderstanding everything, there's nothing more portable. A std::vector is guaranteed to put its contents in consecutive memory locations. It works and feels exactly like a C-array.
If they could do it portably, don't you think it would already be in the standard?
-------------------------------------
IrrLua - a Lua binding for Irrlicht
http://irrlua.sourceforge.net/
Eternl Knight
Posts: 313
Joined: Tue Nov 01, 2005 5:01 am

Post by Eternl Knight »

Actually, this has been clarified in the C++ STL FAQ and other places. Due to the way the std::vector is defined (in terms of the standard requirements), it can be assumed in all cases to be an array underneath. Basically, it can be guaranteed that if vec is a std::vector and it has more than one element:

(&vec[0] + 1) == (&vec[1])


This was clarified in an FAQ, not the standard but is accepted to be "canon". I'll see if I can dig up the reference for you

UPDATE: Reference here.
Note, it is now also mandated in the C++2003 standard that std::vectors are to store their elements in contiguous memory (i.e. an array).
Also Bjarne Stroustrup (the inventor of C++) expressed that it was always the intent here

--EK
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Ah, good to know. I guess this has been a major issue for using self defined data types.
zenaku
Posts: 212
Joined: Tue Jun 07, 2005 11:23 pm

Post by zenaku »

The great thing about standards is that they always change :) I've had this exact same argument in the mid 90's. Back then, I was right :) Thanks for the heads-up EK.
-------------------------------------
IrrLua - a Lua binding for Irrlicht
http://irrlua.sourceforge.net/
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

I'm thinking about writing a DX or OpenGL only version of Irrlicht/IrrSpintz, and using STL with this. I haven't yet decided on whether to use OpenGL or DX yet, I'm noticing that with IrrSpintz, OpenGL has been faster than DX, but I like HLSL more than GLSL at the moment. I have to say though, I'm leaning towards OpenGL.

Anyways, I definitely think STL is the way to go, no more problems with std::string or std::list anymore, so no reason not to use them. You can always use STL for common things and then if you need some kind of special container for matrix or whatever, you can always create it.
Image
Eternl Knight
Posts: 313
Joined: Tue Nov 01, 2005 5:01 am

Post by Eternl Knight »

zenaku: Well, you were "by the standard" correct at least. It has been acknowledged by Bjarne Stroustrup that the "contiguous" property of vectors was always what was intended (and just happened to be always what was implemented too!). Still, I used to make the same argument as yourself :)

Spintz: Personally, I would very much appreciate an OpenGL version only. This keeps the engine cross-platform as much as possible. I have already ditched DirectX (both versions) from my internal STL Irrlicht, but I like the idea of a software renderer for certian things, so I have kept it so far. I have been considering a "GLSL software renderer" for experimental purposes but as it isn't necessary, I haven't put much more than thought into it :)

--EK
Post Reply