can you assign an enum to a memory address of a shader?

Started by
4 comments, last by _the_phantom_ 12 years, 5 months ago
im loading my shaders in and storing them into a stl map , with there name as an index

.
is there a way to store the address in an enum ie

enum EShader
{

phong = &addressOfShader("phong");
normal

}

any way of doing this
Advertisement
You can only assign constants to the identifiers in enumerated types so that doesn't work.

You can define the enumerated type then force the loaded shaders to that index.



enum EShader
{
phong,
normal
};

//Then when you load it

if (name == "phong")
{
assignAddress(phong);
}

My current game project Platform RPG
No. that's what associative containers are for.

Stephen M. Webb
Professional Free Software Developer


You can only assign constants to the identifiers in enumerated types so that doesn't work.

You can define the enumerated type then force the loaded shaders to that index.



enum EShader
{
phong,
normal
};

//Then when you load it

if (name == "phong")
{
assignAddress(phong);
}





not sure how that works

,
with the way i did it it has to do a search of the map stl each time,
i would like to only do that to begin with , and then it can do it via the enums, or im i going about this wring ?

How about something like this:



enum EShader
{
phong=0,
normal,
bumpmapped,
flatshaded,
NUM_SHADERS
};

//and an array of shaders:

shader* shaders[NUM_SHADERS];

//Then you can assign each one:

shaders[ phong] = addressOfShader("phong");
shaders[ normal] = addressOfShader("normal");



Then throughout your code you can just pass around the enum. You can even write the enum to disk (since its just an int), but I would be careful because if you add/remove things from the enum list as the code matures, your values might change. It might be safer to do:


#define SHADER_PHONG 0
#define SHADER_NORMAL 1


shaders[ SHADER_PHONG] = addressOfShader("phong");
shaders[ SHADER_NORMAL] = addressOfShader("normal");



Note, my style is a bit 'C' but works for C++ too. If you really want to use a stl map, then you can use it instead of an array. But an array is the simplest way to map integers to things, IMHO.

with the way i did it it has to do a search of the map stl each time,
i would like to only do that to begin with , and then it can do it via the enums, or im i going about this wring ?


You are going about it wrong; assuming you have some sort of data structure which allows you to hold the details of the thing being rendered at load time you lookup the shader you want to use from the map and then store the pointer to it in that structure. Then at rendering time you can just directly access that pointer to the data which holds the shader information; you only ever look up the shader the once at load time.

This topic is closed to new replies.

Advertisement