Sign in to follow this  

OpenGL Basics: Glut vs. Nehe's framework

This topic is 4290 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

Hi all, Looking for a little generic advice here. I'm coding a tetris clone for my first graphical C++ game. I've got my data structures almost all coded, and I'm starting to think about how I'm going to link everything together with the graphics display. I've looked at two basic methods to get the graphics set up. The first would be to modify the framework which is listed in Nehe's tutorials. The second would be to use the Glut utilities which are described in the red book. I'm leaning towards using the glut because it just seems cleaner and simpler to get everything set up, but I have some concerns. I will be coding up a 'game state' structure which will contain (among other things) whether the game is active, the options, the score, the board, the active piece, the next piece etc. One thing which I was planning on putting in there is a function pointer to a display function so I can pass the game state to the main "display" routine and then call a seperate display function using that pointer depending on whether the game is in the startup splash, the options menu, the actual game itself, etc. From the red book, the function definition for the display function is:
void glutDisplayFunc(void (*func)(void));
so I can see no way to feed in a method for checking which screen to display unless I just declare the game state as a global, which I'd rather not do if I can help it. Is using global variables how that is typically done, or am I overlooking something? Thanks again! This site has made the process significantly less frustrating.

Share this post


Link to post
Share on other sites
Glut is mostly used for smaller projects because as you've seen, its callback system is kind of limiting for larger programs.

Another option you can look into is SDL for this. On NeHe's site, you can scroll down on lesson 1, and grab the Linux/SDL port. (It will work on Windows too though).

SDL is not so limiting as Glut, but you still don't have to deal with all the awful Win32 setup code.

Good Luck.

Share this post


Link to post
Share on other sites
Nehe's framework uses Win32 doesnt it? If i was going to use OpenGL id make my best effort to keep it cross platform, so using SDL or Glut might be best.

I seem to remember having similar problems with Glut and its lack of flexibility. I have never build an application that uses both SDL and OpenGL, but the work required to setup an SDL window is so easy its got to be worth a look =)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
GLFW is a really nice framework for OpenGL.

Share this post


Link to post
Share on other sites
Are you going to have multiple windows in your game or just one?

What is wrong with in your display function, calling other functions through a switch or even through a second function pointer?

I, uh, used a global variable for my game state whenever I made a game.

Share this post


Link to post
Share on other sites
Quote:
Original post by nprz
Are you going to have multiple windows in your game or just one?

What is wrong with in your display function, calling other functions through a switch or even through a second function pointer?

I, uh, used a global variable for my game state whenever I made a game.


Thanks for the input, all!

I'm planning on just one window for this game. It's going to be yet another tetris clone.

I could use a switch, but I'd still need to use global variables to get the information to switch against into glutDisplayFunc. I personally don't like using global variables unless there is no other option. I've botched up entirely too many Matlab assignments by using scripts instead of functions and inadvertantly overwriting my globals.

I saw SDL a few days ago, and was planning on checking it out a bit once I got a bit further with NeHe's tutorials and the Redbook (found them first after all...). I'll look at GLFW also, thanks!

I'm probably going to modify the NeHe code for this game. The code I've got now creates a window and has seperate functions for update graphics and check for inputs. That should be all I need. There is always the next game for the other stuff.

Share this post


Link to post
Share on other sites
You can work around global variables by having a global function which returns a a file scope variable. It is ok to have one filescope pointer to some kind of singleton application root structure or class, and a single global function to get that pointer. In C++ you could use static class member instead of filescope variables.

Share this post


Link to post
Share on other sites
I would recommend against writing your own OpenGL Framework like NeHe has done. Some of the benefits of using an already written dedicated library include:
  • Cross platformability
    Many already written libraries provide dedicated cross platform. This means you can usually compile for Windows, Linux and Mac without having to change much in your code.
  • Easily upgradable
    All you'd have to do to update the library would be to overwrite your existing LIB and DLL files.
  • Documentation
    There is loads of tutorials and documentation available for dedicated libraries.
Although it might be tempting to roll your own framework or base it off of NeHe's, the best solution is to use an already existing dedicated framework.

I've had experience with GLFW, FreeGLUT and SDL when it comes to rendering in OpenGL and have to say all three are great solutions. Try a number of different ones and see which one you like. The great thing about the framework is that it allows you to seperate your game and engine code into a nice managable interface. Good luck!

Share this post


Link to post
Share on other sites
OK. I feel like a n00b again.

I can't figure out how to compile GLFW.

From the readme.html:
Quote:

A top level makefile can be found in the root directory of the GLFW distribution that has been designed to work with several compilers. If you simply enter the GLFW root directory in a shell and type make (or nmake or gmake, depending on the name of your make tool), and a list should appear with the currently supported options for systems and compilers.

For example, one of the options is to compile GLFW for Windows with the LCC-Win32 C compiler. To do that, type make win32-lcc in the shell (as specified in the previously mentioned list). That will compile the GLFW static link library and the supplied example programs. For Windows compilers, a Win32 DLL will also be compiled.


I tried make, nmake, and gmake from a dos prompt, as well as simply running the compile.bat file. The instructions aren't exactly superclear for non-CS types.

Can someone explain in kiddy terms what I'm supposed to do?

Thanks,

EDIT: Running Windows XP with Microsoft Visual Studio .net if that makes a difference.
EDIT2: uhm. Looks like it only supports MSVC 6.X
Am I SOOL?

Share this post


Link to post
Share on other sites
Afaik GLFW comes with prebuild libraries. Did you change the GLFW code?
There should be a dll and/or lib that you can use.

I think I downloaded the gnu stuff just for building the example programs.

Share this post


Link to post
Share on other sites
I didn't change any of the code.

The instructions say that it needs to be compiled with a makefile (whatever that means). Can I just put the relevant files somewhere for it to work? If so, where?

Share this post


Link to post
Share on other sites
*bump*

So, I think I may have misunderstood the directions, but I'm not sure...

Do I just need to move the various files which came with the download into the /lib, /include, /etc directories I find in my /C++/vc7/... directory and then #include and compile as I normally would?

I'm afraid of messing things up...

Share this post


Link to post
Share on other sites
You could try this out :)


///////////////////////
// In the .h file

class SomeClass
{
public:
bool Init();
static SomeClass* GetInstance();


private:
SomeClass();

static SomeClass* ms_ptrSingleton;
static void DisplayFunc();

int SomeVariable;

};

////////////////////////
// In the .cpp file

SomeClass* SomeClass::ms_pSingletonPtr = NULL;

SomeClass* SomeClass::GetInstance()
{
if (!ms_pSingletonPtr)
ms_pSingletonPtr = new SomeClass;

return ms_pSingletonPtr;
}

bool SomeClass::Init()
{
glutDisplayFunc(DisplayFunc)
}

void SomeClass::DisplayFunc()
{
// You can access a class variable here
//
// No Globals :)
//
ms_pSingletonPtr->SomeVariable = 3;
}



Share this post


Link to post
Share on other sites

This topic is 4290 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this