global variables & vectors

Started by
27 comments, last by 3Dgonewild 16 years, 10 months ago
Hi from a bad programmer ... In my games im using global variables...for almost everything. I know it sucks , but I HAVE NO IDEA what else to try. Also , im using ARRAYS -ready to EXPLODE- for images/textures ETC. IM ASKING for your help! What else can i try ? i need to improve my skills , please HELP! Also , can you tell me how to use vectors? Thanks..
Advertisement
The fundamental thing you have to do is forget about "the". There is no such thing as "the renderer", "the world", "the resource manager". Think in terms of "one of the many": "one of the many renderers", "one of the many worlds", "one of the many resource managers". Just like you would think "one of the many enemy characters", for instance.

If an entity needs to interact with another entity (entities being functions, objects, or anything else), simply pass one entity to the other by reference.

For instance, if your player needs to draw something, then you will have to give it "one of the many resource managers", from which it will extract the correct model, and the "one of the many renderers", into which it will insert the model to be rendered.

The fact that, in the end, your game will use only one renderer is only a coincidence that you should not rely on when writing the rest of your code: always assume that you have many different objects of the same type lying around at any given time, so that every object needs its users to specify which precise renderer/world/manager it needs to use.

As for vectors, I suggest that you go read an online tutorial (there are many around) and come back with more precise questions.
Quote:Original post by ToohrVyk
The fundamental thing you have to do is forget about "the". There is no such thing as "the renderer", "the world", "the resource manager". Think in terms of "one of the many": "one of the many renderers", "one of the many worlds", "one of the many resource managers". Just like you would think "one of the many enemy characters", for instance.

If an entity needs to interact with another entity (entities being functions, objects, or anything else), simply pass one entity to the other by reference.

For instance, if your player needs to draw something, then you will have to give it "one of the many resource managers", from which it will extract the correct model, and the "one of the many renderers", into which it will insert the model to be rendered.

The fact that, in the end, your game will use only one renderer is only a coincidence that you should not rely on when writing the rest of your code: always assume that you have many different objects of the same type lying around at any given time, so that every object needs its users to specify which precise renderer/world/manager it needs to use.

As for vectors, I suggest that you go read an online tutorial (there are many around) and come back with more precise questions.

Thanks for the reply...

I think i understand now , but thats just theory!
Are there any tutorials about this?


Ok , i think i found the right way(classes).

Now i have another problem..with vector..

How can i create a vector surface array?

Something like this:


vector <SDL_SURFACE> test;

and then somehow i want to be able to add more surfaces...

any help?
std::vector<SDL_Surface*> surfaces;surfaces.push_back(SDL_GetScreen());

Quote:Original post by ToohrVyk
std::vector<SDL_Surface*> surfaces;surfaces.push_back(SDL_GetScreen());



thanks!
Is there any problem if replace "sdl_getscreen" with a counter(integer)?
also , how can i force the selected item to load an image?

example:

surfaces.selectedITEM(0).LOADIMAGE="b.png";


thanks for the great help so far ! .
~delete me
..double post
Well, there is obviously a problem, since an integer counter is not a surface. The vector contains surfaces, not integers!

You can do whatever you wish with the contents of the vector—they're images, after all.

However, loading functions usually return a new surface, so you would push that surface into the vector.
Quote:Original post by ToohrVyk
Well, there is obviously a problem, since an integer counter is not a surface. The vector contains surfaces, not integers!

You can do whatever you wish with the contents of the vector—they're images, after all.

However, loading functions usually return a new surface, so you would push that surface into the vector.


Sorry , im confused . ..

So , when you call "push_back" , you're actually adding a new slot in the surface array? or im wrong?
What i dont understand , is "SDL_GetScreen()" function.
I cant find any information for this function!

thanks for your time.
It doesn't add a new slot: it adds a new element.

I don't think the SDL_GetScreen even exists. But if it did, it would return the screen surface.

This topic is closed to new replies.

Advertisement