Skip to main content
gamedev.net
🔒 Locked

Template!?

Started by Crocmort Sep 25, 2000 at 7:19 PM 1 replies 350+ views
Original Post
Crocmort
Crocmort
I was just wondering if templates are good in game programming?? I didnt see anything about it in my book of C++ (unless a little thing at the end) and a lot of specialized magazines like C++ Journal are always giving exemples in templates... Like a vector template class or something like that... Do you have some links about templates that explain them? I didnt find any in the articles... And it is worth to learn them for game programming??
mossmoss
mossmoss
Templates are, in a sense, glorified type-safe macros. You can write code to implement a generic stack class, for example, and using templates have an integer stack, floating-point stack, or stack of any struct/class you like.

One of the drawbacks to C++ templates is that each time you instantiate a template with a different type, a whole new set of code is built. So, if you have stack<int> and stack<float>, the stack templates actually get compiled twice and generate two sets of code. There are ways to deal with this, but nothing automatic.

Templates can also require massive resources (ie, memory!) but can be twisted into doing some neat tricks (look up ''generative programming'' in your favorite search engine).

Also, the C++ standard library includes the STL (Standard Template Library) which has a great collection of containers (vectors, stacks, lists, maps, sets, multimaps) and general algorithms. Some of the STL (and some of templates in general) can be used in game programming, but with care. Make sure you understand how they work and what kind of code is generated.


---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
neocron
neocron

Templates are great for implementing reliable efficient code. If you have, say, several different pixel formats you need to deal with and you have a complex function, CF, that manipulates a bunch of pixels you have several choices:

[1] Write the function for each pixel format.
[2] Write a DrawPixel function to deal with each pixel format, and call this beast from CF.
[3] Write a case statement (perhaps in an inner loop) with each case taking care of the pixel format.

(...these are other cases of course but this is just for illustration...)

The problem with [1] is if you need to change the function, you need to change it for each pixel format. [2]s problem is efficiency. [3] is getting better but we still have jumps therefore we''re compromising efficency.

If you use a template parameter in the switch statement you have the advantage of [3] without the overhead of the switch statement as the template parameter will become a constant in the code and the compiler will strip out all the impossible cases.

However as mossmoss points out you do get code bloat, so I''d use templates sparingly.

I do believe that templates are valuable for game programming.



Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.