X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4b764db3795a5f202f379ee3b0314b2de91c15fa..fa699cbaaf217af186cd04dd10d6ec67c8667136:/samples/opengl/penguin/penguin.cpp diff --git a/samples/opengl/penguin/penguin.cpp b/samples/opengl/penguin/penguin.cpp index f753b518bc..b60619ed77 100644 --- a/samples/opengl/penguin/penguin.cpp +++ b/samples/opengl/penguin/penguin.cpp @@ -25,14 +25,10 @@ #endif #include "penguin.h" -#ifdef __WXMAC__ -# ifdef __DARWIN__ -# include -# else -# include -# endif +#ifdef __DARWIN__ + #include #else -# include + #include #endif #include "../../sample.xpm" @@ -44,6 +40,9 @@ // `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); @@ -79,7 +78,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxFrame(frame, wxID_ANY, title, pos, size, style) { - SetIcon(wxIcon(sample_xpm)); + SetIcon(wxICON(sample)); // Make the "File" menu wxMenu *fileMenu = new wxMenu; @@ -88,15 +87,17 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, fileMenu->Append(wxID_EXIT, wxT("E&xit\tALT-X")); // Make the "Help" menu wxMenu *helpMenu = new wxMenu; - helpMenu->Append(wxID_HELP, wxT("&About...")); + helpMenu->Append(wxID_HELP, wxT("&About")); wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(fileMenu, wxT("&File")); 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 @@ -108,7 +109,7 @@ void MyFrame::OnMenuFileOpen( wxCommandEvent& WXUNUSED(event) ) #else wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"), #endif - wxOPEN); + wxFD_OPEN); if (!filename.IsEmpty()) { m_canvas->LoadDXF(filename); @@ -123,7 +124,7 @@ void MyFrame::OnMenuFileExit( wxCommandEvent& WXUNUSED(event) ) Close(true); } -// Help|About... command +// Help|About command void MyFrame::OnMenuHelpAbout( wxCommandEvent& WXUNUSED(event) ) { wxMessageBox(wxT("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al")); @@ -140,10 +141,18 @@ BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas) 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); + m_gldata.initialized = false; // initialize view matrix @@ -155,6 +164,7 @@ TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id, TestGLCanvas::~TestGLCanvas() { + delete m_glRC; } void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) @@ -162,11 +172,7 @@ 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) @@ -196,11 +202,11 @@ void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) 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(); } @@ -214,7 +220,7 @@ void TestGLCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) void TestGLCanvas::LoadDXF(const wxString& filename) { wxFileInputStream stream(filename); - if (stream.Ok()) + if (stream.IsOk()) #if wxUSE_ZLIB { if (filename.Right(3).Lower() == wxT(".gz")) @@ -270,7 +276,7 @@ void TestGLCanvas::InitGL() static const GLfloat light1_color[4] = { 0.4f, 0.4f, 1.0f, 1.0f }; /* remove back faces */ - glDisable(GL_CULL_FACE); + glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); /* speedups */ @@ -294,18 +300,24 @@ void TestGLCanvas::InitGL() void TestGLCanvas::ResetProjectionMode() { + if ( !IsShownOnScreen() ) + return; + + // 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(); }