Jump to content
  • Advertisement
Sign in to follow this  
Shadowwoelf

Programming practices and opengl

This topic is 3895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering what is the best way to make a simple game in opengl. For example right now I'm using a lot of global variables which I don't think is a very good programming practice, but I'm not sure whats the best way to variables.

Share this post


Link to post
Share on other sites
Advertisement
So its a question of variables, not OpenGL. Global variables can be a religious issue, but in the name of hiding/decreasing complexity, I think its better not to use them unless necessary.

Share this post


Link to post
Share on other sites
Where to put variables? struct and classes if they are offered by the programing language you use.
PS : you need to post your question in beginner programming section.

Share this post


Link to post
Share on other sites
Oh no this isn't about whether to use global variables or not. I rather don't like them. I was asking as to how to get ride of the global variables in my opengl program for example some of nehe's tutorials have global variables. I just can not seem to figure out when to pass the information on.
My program right is this
1. initialize the global variables
2. get into the message loop
3. call the function draw screen.

Should I do this?

1. Initialize the variables in WinMain
2. get into the message loop
3. Process the information(Where to move next etc.)
4. Pass the information to the draw function
But how should I pass the information? especially if its a huge class like heroes or enemy. Pointers? and to streamline the draw function should I only pass what should be drawn on the screen? Which I have no idea of how to select what to pass to the function during runtime.

Edit: This might fit in the beginners section, but I was thinking that since it dealt more with opengl and winmain that I should post here. I was going to put it in the game programming one but I decided at the last second to put it here.

Share this post


Link to post
Share on other sites
It is better to first learn about forums.
Different people use different coding languages and different libs.

For example, a person might use GL, DirectInput, C#.
Posting questions here about DirectInput is not appropriate although there are people who can answer your question. That should go to the DirectX forum.

All programmers are faced with the decision of using global variables, even when not using GL. So your question is not tied to GL.
Your question is about "programming practices".

Share this post


Link to post
Share on other sites
Well, I use globals when needed, but I prefer using structs/classes (C++). I don't see a problem with globals except that they are not as neat and organized. I personally would not get rid of globals if it is to much of a hassle, but things like playerx and playery would be in a struct/class titled player, and have them as player.x, player.y, etc.

Share this post


Link to post
Share on other sites
In a simple game, you can often just collect everything in the game world (player, enemies, terrain) into one "world" structure that both the game logic and rendering have access to. It can either be global or passed by reference. So if you just want to "get rid of globals", the program could look like this:


world = initializeworld();

while (running)
{
gameupdate(world);
render(world);
}

destroyworld(world);



Of course, since you're most likely going to have only one "world" at any one time and (in this case) all parts of your code know about the world, it might as well be global.

Share this post


Link to post
Share on other sites
Quote:
For example right now I'm using a lot of global variables which I don't think is a very good programming practice, but I'm not sure whats the best way to variables.


Look up structures. In a typical game (say, a side-scrolling shooting type of game), you might have a "player" structure (which would contain the position, life and everything else important to the player), "enemy" structures, "bullet"s etc etc.

All these might belong to a larger structure called "level".

The player, enemies and bullets should definitely not be globals. For simplicity's shake, the level structure could be global (if you only have on level active at a time).

In any case, this is not something you can learn out of a book. Keep creating things. You'll make mistakes now (which will make you cringe in 3 months - "what the hell was I thinking?!"), but the important thing is to keep creating.

Once you finish a project and move on to the next one, ask yourself "what could I have done better?" Your skills will improve and your next project will work better. And the net one even better.

You just need to stay creative :)

Share this post


Link to post
Share on other sites
Another thing I could suggest to help out with better code organization/style/practice when using OpenGL, is try using a (multi-platform) library for OpenGL window creation, keyboard/mouse input, etc. such as GLFW. I can vouch for it being very good and easy to use.

This will simplify your code by a great deal when compared to what you get after following Nehe tutorials, with the WinMain stuff and all that Win32 bloatness in one big cpp file.

Just take a look at the first few pages of the User Guide for GLFW to get a better idea of what it offers.

Now you can just reduce your main to something like:
int main()
{
(new MyApplication()).run();

return 0;
}
And feel free to organize the rest in a much more OOP way.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!