PDA

View Full Version : just getting started...



08-21-2002, 10:17 PM
I want to use C with OpenGL which seems pretty common. But I want to make the source files portable. In other words, I want people to be able to compile it on their platforms without worrying about if they have a glut.h file here or a glut.lib file there. I also don't want to add windows.h or any other platform-specific header file to my source code. What do I need to do to have this happen, if possible? Or does everybody have to jerryrig their system to get all the library and header files in the right place when compiling?

Bob
08-21-2002, 11:05 PM
It depends on what you mean by platform independent, like how independent, and what range of platforms.

For different Windows platforms, there shouldn't be much problem with portability. But if you intend to suport both Windows and UNIX-like platforms, there are some problems. Also, if you want to write 100% portable code, you should ONLY use stuff from the C specification. That would literally mean only text input and output via the console. Any third party library (like OpenGL, a windowing API) will introduce some degree of platform dependancy.

In the case of OpenGL, the OpenGL code is more or less 100% portable. But the problem is creating the proper environment for OpenGL to live in. OpenGL does not manage rendering buffers or anything like that. You must use the windowing API for your platform to create buffers and attach an OpenGL rendering context to them, and that is different between most platforms. This is where libraries like GLUT and SDL comes into the picture. They give you a common way of doing this in YOUR code, but the code "underneath" is still platform specific.

Don't be afraid of using platform specific functions to do what you want, even if you intend to write portable code. You can always use preprocessor directives (#ifdef, #ifndef, and so on) to tell the compiler what code to compile for what platform. Most of the code will be common, but some pieces won't, and there's nothing wrong with that.

nexusone
08-22-2002, 07:06 AM
You make your source portable by using portable library's.
Yes you still need to have the gl.h headers and lib's to compile the file on another computer.
Also glu, glut, are protable library's in which they are supported on multiple platforms.
But you can not use a glut lib made for windows on a linux machine, you have to have one built for linux.

You may want to think about using openAL or SDL (Sound Device library), both are protable library's for sound support.

Example SDL can be downloaded for diffent operating systems.
The windows SDL lib's for example translate the SDL sound commands to a windows sound commands(Direct sound).
and the linux SDL lib's translate the SDL commands to a linux command format for sound creation.

But for you the programmer, you dont need to know the windows or linux sound commands, you just use the SDL commands, and the SDL lib does the rest...



Originally posted by Billy Idol:
I want to use C with OpenGL which seems pretty common. But I want to make the source files portable. In other words, I want people to be able to compile it on their platforms without worrying about if they have a glut.h file here or a glut.lib file there. I also don't want to add windows.h or any other platform-specific header file to my source code. What do I need to do to have this happen, if possible? Or does everybody have to jerryrig their system to get all the library and header files in the right place when compiling?

08-22-2002, 09:11 AM
[/B]You can always use preprocessor directives (#ifdef, #ifndef, and so on) to tell the compiler what code to compile for what platform. [B]

You would use #ifdef if let's say, a macro was available in one platform and not in another, right? But the macros in glut.h are found on all platforms, just with different code. Using #ifdef would still not allow the user not to have the glut libraries and headers, right? This has still be added manually at their end.

Deiussum
08-22-2002, 09:59 AM
Unless you recode all of OpenGL yourself and include that code, you can't control the setup of libs and headers on the user's platform.

So far as #ifdefs are concerned you can also do stuff like this with them..

#ifdef WIN32
#include <windows.h>
#else
// do whatever you need to for other platforms
#endif

nexusone
08-22-2002, 10:17 AM
I am not sure what point your are trying to make.

Of course anyone who would compile an opengl based program, would still need the header and library files for opengl.
The same goes for glut, you have to have the headers and library files for that system.

You could write a program in pure C, that would compile only any system.
But the catch is that C does not have any standard graphics and windowing routines.
Each system has its own system calls for these function.

That is where things like openGL,GLUT,SDL come into play, by having these standard crossplatform library's it makes it posible to write a program that will compile on any system.

example (not real code)code of opening a window.

the glut.lib for windows

glutCreateWindow(int *argv)
{
// windows create window code
All the code to create a window in Windows
would be put here.

}


the glut.lib for linux

glutCreateWindow(int *argv)
{
// Linux create window code
All the code to create a window in Linux
would be put here.

}


This way when we call glutCreateWindow in our code we do not care what code is needed to open a window in any operating system we are programming for. The glut library for that system takes care of it.


Originally posted by Billy Idol:
You would use #ifdef if let's say, a macro was available in one platform and not in another, right? But the macros in glut.h are found on all platforms, just with different code. Using #ifdef would still not allow the user not to have the glut libraries and headers, right? This has still be added manually at their end.

08-22-2002, 01:24 PM
Thanks guys for the help. I have to do some OpenGL for a homework assignment and I was wondering how portable I could make it and what the TA needed to do on his end for it to compile. Anyway, thanks you answered my question.