Programming practices and opengl

Started by
8 comments, last by shurcool 16 years, 1 month ago
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.
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.
Johnny was a chemist's son by Johnny is no more, for what Johnny thought was H2O was HO4
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.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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.
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".
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
As others have said, GL forum isn't really the place for this..
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.
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.
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 :)

[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]

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.

This topic is closed to new replies.

Advertisement