Tute1

ContextGL++ tute1: Howto create a window as the OpenGL context.

First things first. I assume you all know about how to compile a program using either VC++ or gcc. I am also assuming that you have installed ContextGL++ in the appropriate directory. OK, now lets get down to business. ContextGL++ is a C++ class library for OpenGL context creation. To be able to render any OpenGL commands we need a context to render to, this can be quite an issue for the novice and has of course led to many libraries that can open contexts in a simple and platform independent fashion. So why use ContextGL++ instead of all those other ones?
   ContextGL++ provides context and event notification as well as a simple interface into the Window Manager's (Win, X11 ...) core bitmap fonts. That's all, there are no plans to ever expand beyond those limits. If you want a library that holds your hand all the way, use OpenGLUT or SDL or Allegro.
   Lets now have a look at a simple example that opens up a window ready for rendering.

The correct way to include the ContextGL++ project into a gcc project is to use 
`contextglpp-config --cxxflags`
and
`contextglpp-config --libs`
 
as appropriate in your makefiles.

Now, in a skeleton .cpp file enter the following lines:

#include <ContextGL++/GLWindow.h> #include <ContextGL++/WMhelper.h> using namespace contextglpp; #ifdef _WIN32 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { #else int main() { void* hInstance = 0; #endif //user code goes from here //to here return 0; }
That should provide a simple base to build upon.

Lets now add a context to the program.


//user code goes from here //Prepare a depth & double buffered window at 0,0 with dimensions 400 by 400 GLWindow* context = new GLWindow(GLContext::DOUBLE | GLContext::DEPTH,0,0, 400,400, hInstance); //create it, Windows doesn't like to play fair with window creation and requires a callback so //we have to wait for the WndProc to recieve the WM_CREATE message. Therefore all creating is //in the create method. context->create("Simple Example"); //Display it on the screen context->show(); //Associate OpenGL calls with this context context->makeCurrent(); //You all know this one glClear(GL_COLOR_BUFFER_BIT); //Same for single and double buffered windows. context->swapBuffers(); //You will have to kill the window by hand with a loop like this. while(1) { } //to here
Compile and run and you should have something like this ScreenShot1

OK, so the window isn't very cooperative (yet) but you have at least created a context to draw onto. Lets now add some WindowManager event support so that the app at least responds to closing the window.


//You will have to kill the window by hand with a loop like this. //Not any more :-) WMEvent* event = new WMEvent(*context); bool done = false; while(!done) { //Draw with OpenGL here //Check for and iterate through events while(event->nextEvent()) { //Filter the event switch(event->type()) { case WMEvent::CLOSE: done = true; break; default: } //To satisfy Windows' messaging system event->passAlong(); } } //clean up delete event; delete context; //to here

OK, that concludes the first tutorial on ContextGL++, the source for the program can be found under the src/tutorial directory in the source tree of contextglpp. Happy Coding everyone.