Click here to Skip to main content
15,896,111 members
Articles / Desktop Programming / MFC

Writing a Platform and GUI Toolkit Independent OpenGL Class

Rate me:
Please Sign up or sign in to vote.
4.92/5 (33 votes)
1 Nov 2010CPOL13 min read 98.1K   7.5K   90  
Article showing how to write OS and GUI toolkit portable OpenGL view class
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"

#include <GLView.h>

static CGLView *pGLView = NULL;

void
UpdateDrawingArea (GtkWidget * widget)
{
  GtkWidget *drw = lookup_widget (GTK_WIDGET (widget), "drawingarea1");
  gdk_window_invalidate_rect (GTK_WIDGET (drw)->window,
			      &GTK_WIDGET (drw)->allocation, FALSE);
}

void
on_new1_activate (GtkMenuItem * menuitem, gpointer user_data)
{

}


void
on_open1_activate (GtkMenuItem * menuitem, gpointer user_data)
{

}


void
on_save1_activate (GtkMenuItem * menuitem, gpointer user_data)
{

}


void
on_save_as1_activate (GtkMenuItem * menuitem, gpointer user_data)
{

}


void
on_quit1_activate (GtkMenuItem * menuitem, gpointer user_data)
{
  if (pGLView)
    {
      delete pGLView;
      pGLView = NULL;
    }
  gtk_main_quit ();
}


void
on_cut1_activate (GtkMenuItem * menuitem, gpointer user_data)
{

}


void
on_copy1_activate (GtkMenuItem * menuitem, gpointer user_data)
{

}


void
on_paste1_activate (GtkMenuItem * menuitem, gpointer user_data)
{

}


void
on_delete1_activate (GtkMenuItem * menuitem, gpointer user_data)
{

}


void
on_about1_activate (GtkMenuItem * menuitem, gpointer user_data)
{

}

#ifdef WIN32
#include <gdk/gdkwin32.h>
#else
#include <gdk/gdkx.h>
#endif
void
on_drawingarea1_realize (GtkWidget * widget, gpointer user_data)
{

  // Unset Double Buffering on DrawingArea to 
  // eliminate flickering of glview
  GTK_WIDGET_UNSET_FLAGS (widget, GTK_DOUBLE_BUFFERED);
  pGLView = new CGLView ();
  if (pGLView)
    {
      GdkWindow *gdkwin = GTK_WIDGET (widget)->window;
	  
#ifdef WIN32
	  glong hWnd;
      hWnd = (glong) gdk_win32_drawable_get_handle (gdkwin);	// get HWND
	  pGLView->SetWindow ((HWND) hWnd);
#else
	  Display* pDisplay = gdk_x11_drawable_get_xdisplay(gdkwin);
	  Window window = (Window) gdk_x11_drawable_get_xid(gdkwin);
	  pGLView->SetWindow (pDisplay, window);
#endif

     
      pGLView->SetupGLContext (TRUE);
      gint w, h;
      gtk_widget_get_size_request (widget, &w, &h);
      pGLView->Resize (w, h);
    }
}


gboolean
on_drawingarea1_configure_event (GtkWidget * widget,
				 GdkEventConfigure * event,
				 gpointer user_data)
{
  if (pGLView)
    {
      pGLView->Resize (event->width, event->height);
    }
  return FALSE;
}


gboolean
on_drawingarea1_expose_event (GtkWidget * widget,
			      GdkEventExpose * event, gpointer user_data)
{
  if (pGLView)
    {
      pGLView->RenderScene ();
      UpdateDrawingArea (GTK_WIDGET (widget));
    }
  return FALSE;
}

void
on_MainWindow_realize (GtkWidget * widget, gpointer user_data)
{
  gtk_widget_set_size_request (widget, 800, 600);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager Mahindra & Mahindra
India India
Sharjith is a Mechanical Engineer with strong passion for Automobiles, Aircrafts and Software development.

Comments and Discussions