Click here to Skip to main content
14,265,979 members

Writing a Platform and GUI Toolkit Independent OpenGL Class

Rate this:
4.95 (30 votes)
Please Sign up or sign in to vote.
4.95 (30 votes)
1 Nov 2010CPOL
Article showing how to write OS and GUI toolkit portable OpenGL view class
basecode
linux
MultiGL
GLView
GLViewGTKApp
.project
AUTHORS
autom4te.cache
output.0
requests
traces.0
ChangeLog
config.guess
config.h.in
config.sub
configure
configure.in
COPYING
depcomp
glviewgtkapp.gladep
INSTALL
install-sh
Makefile.am
Makefile.in
missing
mkinstalldirs
NEWS
po
ChangeLog
Makefile.in.in
POTFILES.in
README
src
Makefile.am
Makefile.in
stamp-h.in
GLViewMotifApp
.project
autom4te.cache
output.0
requests
traces.0
config.h.in
configure
configure.ac
depcomp
install-sh
Makefile.am
Makefile.in
missing
src
Makefile.am
Makefile.in
GLViewQtApp
.project
GLViewQtApp.ico
GLViewQtApp.pro.user
glviewqtapp.qrc
Makefile
Resources
.project
autom4te.cache
output.0
requests
traces.0
config.guess
config.h.in
config.sub
configure
configure.ac
depcomp
install-sh
m4
Makefile.am
Makefile.in
missing
src
Makefile.am
Makefile.in
win32
MultiGL
GLView
GLViewGTKApp
AUTHORS
ChangeLog
configure.in
devcpp
glviewgtkapp.dev
glviewgtkapp.gladep
Makefile.am
msvc
glviewgtkapp.suo
glviewgtkapp.vcproj
NEWS
po
ChangeLog
POTFILES.in
README
src
Makefile.am
stamp-h.in
GLViewMFCDlgApp
GLViewMFCDlgApp.aps
GLViewMFCDlgApp.vcproj
res
GLViewMFCDlgApp.ico
GLViewMFCMDIApp
GLViewMFCMDIApp.vcproj
res
buttons.bmp
filelarge.bmp
filesmall.bmp
GLViewMFCMDIApp.ico
GLViewMFCMDIAppDoc.ico
main.bmp
Toolbar.bmp
Toolbar256.bmp
writelarge.bmp
writesmall.bmp
GLViewQtApp
GLViewQtApp
GLViewQtApp.ico
glviewqtapp.qrc
GLViewQtApp.vcproj
Makefile
moc_glviewqtapp.o
Resources
GLView.cpp.bak
GLView.suo
GLView.vcproj
MultiGL.suo
#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)

Share

About the Author

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

Stats

73.1K views
6.9K downloads
88 bookmarked