#endif
#include "penguin.h"
-#ifdef __WXMAC__
-# ifdef __DARWIN__
-# include <OpenGL/glu.h>
-# else
-# include <glu.h>
-# endif
+#ifdef __DARWIN__
+ #include <OpenGL/glu.h>
#else
-# include <GL/glu.h>
+ #include <GL/glu.h>
#endif
#include "../../sample.xpm"
// `Main program' equivalent, creating windows and returning main app frame
bool MyApp::OnInit()
{
+ if ( !wxApp::OnInit() )
+ return false;
+
// Create the main frame window
MyFrame *frame = new MyFrame(NULL, wxT("wxWidgets Penguin Sample"),
wxDefaultPosition, wxDefaultSize);
menuBar->Append(helpMenu, wxT("&Help"));
SetMenuBar(menuBar);
+ Show(true);
+
m_canvas = new TestGLCanvas(this, wxID_ANY, wxDefaultPosition,
- wxSize(300, 300), wxSUNKEN_BORDER);
+ GetClientSize(), wxSUNKEN_BORDER);
}
// File|Open... command
#else
wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"),
#endif
- wxOPEN);
+ wxFD_OPEN);
if (!filename.IsEmpty())
{
m_canvas->LoadDXF(filename);
EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse)
END_EVENT_TABLE()
-TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
- const wxPoint& pos, const wxSize& size, long style, const wxString& name)
- : wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name)
+TestGLCanvas::TestGLCanvas(wxWindow *parent,
+ wxWindowID id,
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxString& name)
+ : wxGLCanvas(parent, id, NULL, pos, size,
+ style | wxFULL_REPAINT_ON_RESIZE, name)
{
+ // Explicitly create a new rendering context instance for this canvas.
+ m_glRC = new wxGLContext(this);
+
+ // Make the new context current (activate it for use) with this canvas.
+ SetCurrent(*m_glRC);
+
m_gldata.initialized = false;
// initialize view matrix
TestGLCanvas::~TestGLCanvas()
{
+ delete m_glRC;
}
void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
// must always be here
wxPaintDC dc(this);
-#ifndef __WXMOTIF__
- if (!GetContext()) return;
-#endif
-
- SetCurrent();
+ SetCurrent(*m_glRC);
// Initialize OpenGL
if (!m_gldata.initialized)
SwapBuffers();
}
-void TestGLCanvas::OnSize(wxSizeEvent& event)
+void TestGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
{
- // this is also necessary to update the context on some platforms
- wxGLCanvas::OnSize(event);
- // Reset the OpenGL view aspect
+ // Reset the OpenGL view aspect.
+ // This is OK only because there is only one canvas that uses the context.
+ // See the cube sample for that case that multiple canvases are made current with one context.
ResetProjectionMode();
}
void TestGLCanvas::ResetProjectionMode()
{
+ // This is normally only necessary if there is more than one wxGLCanvas
+ // or more than one wxGLContext in the application.
+ SetCurrent(*m_glRC);
+
int w, h;
GetClientSize(&w, &h);
-#ifndef __WXMOTIF__
- if ( GetContext() )
-#endif
- {
- SetCurrent();
- glViewport(0, 0, (GLint) w, (GLint) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0f, (GLfloat)w/h, 1.0, 100.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
+
+ // It's up to the application code to update the OpenGL viewport settings.
+ // In order to avoid extensive context switching, consider doing this in
+ // OnPaint() rather than here, though.
+ glViewport(0, 0, (GLint) w, (GLint) h);
+
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluPerspective(45.0f, (GLfloat)w/h, 1.0, 100.0);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
}