Archived

This topic is now archived and is closed to further replies.

skyfire360

Pointers question, not OGL related...

Recommended Posts

Sorry for the off-topic post. I''m creating a console in OpenGL that uses CVars. I''ve got the entire system working and I must say it works pretty well. However, there is a downfall: every time it redraws the scene, it assigns every variable used in the program to the value of it''s CVar counterpart. Is there any way to use a pointer to assign the values directly? What I mean is this:
  
typedef struct cvar_s {
     float value;
} cvar_t

float A = 5.5f;
cvar_t CVarA;
  
CVarA is the console-variable counterpart to the variable A, which is used elsewhere in the program. I have CVarA.value = A to begin with (so CVarA.value = 5.5f). Through the console I may change the value of CVarA.value to 2.2f. Since I only changed the CVar, I must have a function that sets the value of A to CVarA.value. Using only a few variables this might be a decent method to use, but when dealing with 1,000''s of variables the loop can cost a lot of time. Is there any way to change the "float value;" to a "float *value" and have "value" point to A? I.E. Any time I change CVarA.value, A is also changed. Sorry if this doesn''t make much sense, but is there any way to do this? -SkyFire

Share this post


Link to post
Share on other sites
Yes. Assuming your variables are static (which they must be, I would think), you can easily so what you are thinking about.

typedef struct cvar_s
{
float *value;
} cvar_t;


float A = 5.5f;
cvar_t CVarA;


to initialize, you''d do this:

CVarA.value = &A;

instead of using "CVarA.value", you''d need to use "*CVarA.value" to dereference or you''ll be trying to do math on the pointer to the value instead of the value itself.


You could optionally implement a "changed" flag in your cvar_t type which could be set when you''ve changed a variable. This would be good because you''d have knowledge of what was changed so you can affect the appropriate changes elsewhere (like if you changed the resolution, you might to rebuild some things).

Share this post


Link to post
Share on other sites
Thanks a bunch!

I thought about the "Changed" boolean flag, but then I realized I would still be going through a loop that cycles through all of the cvars checking if any had changed...

::shrugs::
Thanks for your help!
-SkyFire

Share this post


Link to post
Share on other sites