Including OpenGL in a header?

Started by
13 comments, last by DreamGhost 18 years, 2 months ago
How would I include OpenGl in a header file? At the moment my project looks like (I'm tryin to keep things seperate so its better organised): main.cpp - Included win32window.h here and call the functions. win32window.h - Windows Header Included here. win32window.cpp - Functions: RegisterWindow, CreateWindow I want to add (for example) openglstuff.h and openglstuff.cpp and include gl.h in this header. However when I do so the gl.h errors (Is this because it can't see the window.h stuff?) So how could I do it? Thanks.
Advertisement
Yes, this is indeed because the gl.h header file needs windows.h. So just include that above the place you include gl.h
A comment on your organization:

Don't organize your code by what it uses or what it references. Organize your code by what it does or what it is. Putting all code that references Win32 in one file and all code that references OpenGL in another won't get you very far (as you can see, you have already run into a problem).

Header files should be considered as an API for the source files they are associated with. Do not use them as repositories for other header files or a place to put declarations of private/internal types, functions, and data. That kind of organization will lead to problems.

Read this article: Organizing Code Files in C and C++
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Thanks for the replies.

I was planning to have a structure such as:

win32window
linuxwindow
macwindow
openglsetup
openglwhateverelse
openglwhateverelse2
... and so on ...

Would this be a bad way to do it? I wanted to keep things seperate so its easier to maintain and don't have lots of code in main.
Why not just use a wrapper for your windowing code that way you won't have to write out for each seporate os in my opinion that would be a pain.

Something like Glut maybe? Not sure if it will work with OSX though. I think it will. That will remove some overhead then you can implement the OpenGL stuff inside a seperate file and call the opengl stuff from the file that handles all the glut calls.
I do use glut.

I just wanted to do it like this so I actually learn the code (WinAPI etc.)
Yes but it is very involved and takes away from time that could be spent coding other things, was just trying to help but then also splitting it up using different sections for windows, linux, osx is kind of pointless if your using glut. If you want to learn windows api and other api's then I would suggest not using glut.

But I would stick it into one set of files (.c and .h) and depending on the implementation use the same function names that way your code will be universal and allow the same functions in the header files for the windows/linux/osx code instead of putting it all into a seperate file like you had planned on. Look at source code for other projects that do the same thing and see how they do it exactly. Using pre-processor commands will help too.

Hope this helps a little.
So can I use OpenGL commands in a different CPP file if I include it where windows.h is included?

EDIT:

So how do engines handle openGL? Do they just stick it all in the main file with windows.h? I'm just trying to keep as much out of main s I can change sperate bits around.

[Edited by - mengha on January 27, 2006 6:12:01 AM]
Quote:Original post by rick_appleton
Yes, this is indeed because the gl.h header file needs windows.h. So just include that above the place you include gl.h


Also gl.h does not need windows.h that would completely defeat the purpose of opengl...which is being os independent.

To answer your question usually there is a main.c and then glrender.h and glrender.c or something where ever you want to put your draw_scene() function and maybe some initialization code.
Quote:Original post by DreamGhost
Quote:Original post by rick_appleton
Yes, this is indeed because the gl.h header file needs windows.h. So just include that above the place you include gl.h


Also gl.h does not need windows.h that would completely defeat the purpose of opengl...which is being os independent.

The OpenGL code is platform independent, but everything around that is not. The gl.h that belongs to Microsofts implementation of OpenGL present on virtually all installation of Windows does requires windows.h to be included*. Nothing wrong with that, as you would need platform dependent calls anyway to even get to the point where you can start using OpenGL (create a window, create a rendering context and so on, which is not OpenGL's job to deal with).

* - Actually, it just needs a few symbols to define which are defined in windows.h. You can define them manually if you like and skip windows.h.

This topic is closed to new replies.

Advertisement