Sign in to follow this  

Including OpenGL in a header?

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

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.

Share this post


Link to post
Share on other sites
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++

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I still can't do it. :(

If I have opengl.h (for example) and include gl.h and the others there and then include these in the main.cpp and opengl.cpp I get errors in gl.h like:

Windows error C2144: syntax error : 'void' should be preceded by ';'

The line with above error:
WINGDIAPI void APIENTRY glAccum (GLenum op, GLfloat value);


What would I do so it doesn't do this?

Share this post


Link to post
Share on other sites
Quote:
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*.


Well I don't use the Microsoft version so that might be why hehe.

I say just organize the way you feel works best for you but if your just starting out I'd stick to one file until you learn how to work with everything to simplify everything a bit. Working with headers can get rough at times when just starting out with new api.

One thing you could do is have:
opengl.h
#ifndef _OPENGL_H
#define _OPENGL_H

#include <windows.h>
#include <gl.h>

(function declarations)

#endif

opengl.c
#include "opengl.h"

(whatever opengl functions you want to use)

main.c
#include "opengl.h"

(windows stuff)

This would remove the platform independency though, I use Dev-C++ and I incorporate their version of gl.h and glu.h which doesn't require windows.h to be included before it. So instead main.c would have #include <windows.h>.

If you're still having problems just paste some source code so we might be able to help you.

[Edited by - DreamGhost on January 27, 2006 9:40:58 AM]

Share this post


Link to post
Share on other sites

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