Index: CaWE/AppCaWE.cpp
===================================================================
--- CaWE/AppCaWE.cpp	(revision 40)
+++ CaWE/AppCaWE.cpp	(working copy)
@@ -31,12 +31,6 @@
 #include "wx/splash.h"
 #include "wx/stdpaths.h"
 
-#if defined(_WIN32)
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <direct.h>
-#endif
-
 #include "AppCaWE.hpp"
 #include "ChildFrameViewWin2D.hpp"
 #include "ChildFrameViewWin3D.hpp"
@@ -50,6 +44,8 @@
 #include "ToolbarMaterials.hpp"
 #include "Tool.hpp"
 #include "ToolManager.hpp"
+
+#include "ClipSys/CollisionModelMan_impl.hpp"
 #include "ConsoleCommands/Console.hpp"
 #include "ConsoleCommands/ConsoleStdout.hpp"
 #include "ConsoleCommands/ConsoleInterpreter.hpp"
@@ -57,27 +53,12 @@
 #include "ConsoleCommands/ConVar.hpp"
 #include "ConsoleCommands/ConFunc.hpp"
 #include "FileSys/FileManImpl.hpp"
-#include "MaterialSystem/MapComposition.hpp"
-#include "MaterialSystem/MaterialManagerImpl.hpp"
-#include "MaterialSystem/Renderer.hpp"
-#include "MaterialSystem/TextureMap.hpp"
-#include "PlatformAux.hpp"
 #include "GuiSys/GuiMan.hpp"
-#include "GuiSys/GuiManImpl.hpp"
 #include "GuiSys/Window.hpp"
+#include "MaterialSystem/MaterialManagerImpl.hpp"
 #include "TypeSys.hpp"
-#include "ClipSys/CollisionModelMan_impl.hpp"
 
-#ifdef _WIN32
-#elif __linux__
-#include <dirent.h>
-#include <dlfcn.h>
-#define __stdcall
-#define GetProcAddress dlsym
-#define FreeLibrary dlclose
-#endif
 
-
 static cf::ConsoleStdoutT ConsoleStdout;
 cf::ConsoleI* Console=&ConsoleStdout;
 
@@ -102,8 +83,7 @@
 AppCaWE::AppCaWE()
     : wxApp(),
       m_FileConfig(NULL),
-      m_ParentFrame(NULL),
-      m_RendererDLL(NULL)
+      m_ParentFrame(NULL)
 {
     static ConsoleInterpreterImplT ConInterpreterImpl;
     ConsoleInterpreter=&ConInterpreterImpl;
@@ -232,105 +212,10 @@
 
     MaterialManager=&MatManImpl;
 
-
-    // Prepare the name strings.
-#ifdef SCONS_BUILD_DIR
-    #define QUOTE(str) QUOTE_HELPER(str)
-    #define QUOTE_HELPER(str) #str
-
-    #ifdef _WIN32
-    const wxString MatSysRendererDLLName=wxString("Libs/")+QUOTE(SCONS_BUILD_DIR)+"/MaterialSystem/RendererOpenGL12.dll";
-    #else
-    const wxString MatSysRendererDLLName=wxString("Libs/")+QUOTE(SCONS_BUILD_DIR)+"/MaterialSystem/libRendererOpenGL12.so";
-    #endif
-
-    #undef QUOTE
-    #undef QUOTE_HELPER
-#else
-    const wxString MatSysRendererDLLName=wxString("Renderers/RendererOpenGL12")+PlatformAux::GetEnvFileSuffix().c_str()+".dll";
-#endif
-
-
-    // Load the DLL.
-    #ifdef _WIN32
-        m_RendererDLL=LoadLibrary(L"./RendererOpenGL12.dll");
-
-        if (m_RendererDLL==NULL)
-            m_RendererDLL=LoadLibrary(MatSysRendererDLLName);
-    #else
-        // Note that RTLD_GLOBAL must *not* be passed-in here, or else we get in trouble with subsequently loaded libraries.
-        // (E.g. dlsym(RendererDLL, "GetRenderer") return identical results for different RendererDLLs.)
-        // Please refer to the man page of dlopen for more details.
-        m_RendererDLL=dlopen("./libRendererOpenGL12.so", RTLD_NOW);
-        if (!m_RendererDLL) m_RendererDLL=dlopen(MatSysRendererDLLName.c_str(), RTLD_NOW);
-
-        if (!m_RendererDLL) printf("%s\n", dlerror());
-    #endif
-
-    if (m_RendererDLL==NULL) { wxMessageBox("FAILED - could not load the library at "+MatSysRendererDLLName, "ERROR"); return false; }
-
-
-    // Get the renderer.
-    typedef MatSys::RendererI* (__stdcall *GetRendererT)(cf::ConsoleI* Console_, cf::FileSys::FileManI* FileMan_);
-
-    #ifdef _WIN32
-        GetRendererT GetRenderer=(GetRendererT)GetProcAddress(m_RendererDLL, "_GetRenderer@8");
-    #else
-        GetRendererT GetRenderer=(GetRendererT)GetProcAddress(m_RendererDLL, "GetRenderer");
-    #endif
-
-    if (!GetRenderer) { wxMessageBox("FAILED - could not get the address of the GetRenderer() function.", "ERROR"); FreeLibrary(m_RendererDLL); return false; }
-
-    // When we get here, the console and the file man must already have been initialized.
-    assert(Console!=NULL);
-    assert(cf::FileSys::FileMan!=NULL);
-    MatSys::Renderer=GetRenderer(Console, cf::FileSys::FileMan);
-
-    if (MatSys::Renderer==NULL) { wxMessageBox("FAILED - could not get the renderer.", "ERROR"); FreeLibrary(m_RendererDLL); return false; }
-
-
-    // Check if we already have OpenGL errors here.
-    // Shouldn't be the case though, because any errors here must have been caused by the ParentFrames wxCanvas ctor.
-    GLenum Error=glGetError();
-    if (Error!=GL_NO_ERROR) wxMessageBox(wxString::Format("glGetError() reported error %i!", Error));
-
-    if (!MatSys::Renderer->IsSupported())
-    {
-        wxMessageBox("Renderer "+MatSysRendererDLLName+" says that it's not supported.\n\n"
-                     "(This may be caused by your desktop being set to 16 BPP (or less).\n"
-                     "Please set your desktop bit-depth to 32 BPP (True Color), and try again.)");
-        FreeLibrary(m_RendererDLL);
-        return false;
-    }
-
-    MatSys::Renderer->Initialize();
-
-
-    // Get the texture manager.
-    typedef MatSys::TextureMapManagerI* (__stdcall *GetTMMT)();
-
-    #ifdef _WIN32
-        GetTMMT GetTMM=(GetTMMT)GetProcAddress(m_RendererDLL, "_GetTextureMapManager@0");
-    #else
-        GetTMMT GetTMM=(GetTMMT)GetProcAddress(m_RendererDLL, "GetTextureMapManager");
-    #endif
-
-    if (!GetTMM) { wxMessageBox("FAILED - could not get the address of the GetTextureMapManager() function.", "ERROR"); FreeLibrary(m_RendererDLL); return false; }
-    MatSys::TextureMapManager=GetTMM();
-    if (MatSys::TextureMapManager==NULL) { wxMessageBox("No TextureMapManager obtained.", "ERROR"); FreeLibrary(m_RendererDLL); return false; }
-
     // Register the material script with the CaWE materials definitions.
     if (MaterialManager->RegisterMaterialScript(AppDir+"/res/CaWE.cmat", AppDir+"/res/").Size()==0)
         wxMessageBox("CaWE.cmat not found in \""+AppDir+"\".", "WARNING");
 
-    // Create a very simple lightmap for the materials that need one, and register it with the renderer.
-    char Data[]={ 255, 255, 255, 255, 255, 255, 0, 0,
-                  255, 255, 255, 255, 255, 255, 0, 0 };
-
-    MatSys::Renderer->SetCurrentLightMap(MatSys::TextureMapManager->GetTextureMap2D(Data, 2, 2, 3, true, MapCompositionT(MapCompositionT::Linear, MapCompositionT::Linear)));
-    MatSys::Renderer->SetCurrentLightDirMap(NULL);      // The MatSys provides a default for LightDirMaps when NULL is set.
-
-
     // Initialize the global options from the CaWE config files.
     Options.Init();
 
@@ -338,10 +223,6 @@
     wxASSERT(CursorMan==NULL);
     CursorMan=new CursorManT;
 
-    // Initialize the GUI managager.
-    // This has to be done after all materials are loaded (during Options initialization) so the GuiManager finds its default material.
-    cf::GuiSys::GuiMan=new cf::GuiSys::GuiManImplT();
-
     return wxApp::OnInit();
 }
 
@@ -355,18 +236,6 @@
     delete m_FileConfig;
     m_FileConfig=NULL;
 
-    // Release the GuiManager BEFORE the renderer.
-    delete cf::GuiSys::GuiMan;
-    cf::GuiSys::GuiMan=NULL;
-
-    // Unload the Cafu Material System.
-    // Checks for NULL are required to gracefully handle (DLL loading, and other) failures in InitInstance().
-    if (MatSys::Renderer!=NULL && m_RendererDLL)
-    {
-        MatSys::Renderer->Release();
-        FreeLibrary(m_RendererDLL);
-    }
-
     // TODO: delete cf::FileSys::FileMan;   // Shoud have   cf::FileSys::FileMan=new cf::FileSys::FileManImplT;   in OnInit().
     cf::FileSys::FileMan=NULL;
 
Index: CaWE/ParentFrame.cpp
===================================================================
--- CaWE/ParentFrame.cpp	(revision 40)
+++ CaWE/ParentFrame.cpp	(working copy)
@@ -34,8 +34,15 @@
 #include "ModelEditor/ChildFrame.hpp"
 #include "ModelEditor/Document.hpp"
 
+#include "ConsoleCommands/Console.hpp"
+#include "FileSys/FileManImpl.hpp"
 #include "GuiSys/GuiImpl.hpp"   // Needed to catch InitErrorT if GUI document creation fails.
+#include "GuiSys/GuiManImpl.hpp"
+#include "MaterialSystem/MapComposition.hpp"
+#include "MaterialSystem/Renderer.hpp"
+#include "MaterialSystem/TextureMap.hpp"
 #include "TextParser/TextParser.hpp"
+#include "PlatformAux.hpp"
 
 #include "wx/wx.h"
 #include "wx/aboutdlg.h"
@@ -49,11 +56,24 @@
 
 #include <fstream>
 
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <direct.h>
+#elif __linux__
+#include <dirent.h>
+#include <dlfcn.h>
+#define __stdcall
+#define GetProcAddress dlsym
+#define FreeLibrary dlclose
+#endif
 
+
 BEGIN_EVENT_TABLE(ParentFrameT, wxMDIParentFrame)
 #ifdef __WXGTK__
     EVT_SIZE(ParentFrameT::OnSize)
 #endif
+    EVT_SHOW(ParentFrameT::OnShow)
     EVT_CLOSE(ParentFrameT::OnClose)
     EVT_MENU_RANGE(ID_MENU_FILE_NEW_MAP,  ID_MENU_FILE_EXIT,  ParentFrameT::OnMenuFile)
     EVT_MENU_RANGE(wxID_FILE1,            wxID_FILE9,         ParentFrameT::OnMenuFile)
@@ -84,7 +104,8 @@
     : wxMDIParentFrame(NULL /*parent*/, -1 /*id*/, wxString("Cafu World Editor - ") + __DATE__, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE | wxMAXIMIZE),
 #endif
       m_GLCanvas(NULL),
-      m_GLContext(NULL)
+      m_GLContext(NULL),
+      m_RendererDLL(NULL)
 {
     wxMenuBar *item0 = new wxMenuBar;
 
@@ -141,30 +162,40 @@
 #endif
 
 
-    // Create the parent GL canvas.
-    m_GLCanvas=new wxGLCanvas(this, -1, OpenGLAttributeList, wxPoint(600, 5), wxSize(10, 10), 0, "ParentGLCanvas");
-
-
-    Centre(wxBOTH);
-    Maximize(true);             // The wxMAXIMIZE frame style does not seem to have any effect for this frame...(?)
-    Show(true);
+    // Create the parent GL canvas and a GL context.
+    m_GLCanvas =new wxGLCanvas(this, -1, OpenGLAttributeList, wxPoint(600, 5), wxSize(10, 10), 0, "ParentGLCanvas");
     m_GLContext=new wxGLContext(m_GLCanvas);
-    wxASSERT(m_GLContext);
-    m_GLCanvas->SetCurrent(*m_GLContext);   // Aha - under X11 this must be *after* Show(true), not before the window is opened!
 
-    // The above attribute list can certainly NOT be met on 16 BPP desktops.
-    // However, we do not check if the RC of the m_GLCanvas is valid here (it should),
-    // as the IsSupported() method of each MatSys Renderer does the same check, too,
-    // and I have adjusted the error message appropriately there (see AppCaWE.cpp,
-    // the user is told to check whether his desktop is set to 32 BPP if IsSupported() fails).
-    // const char* Version=(char const*)glGetString(GL_VERSION);     // Another way to see if the RC is valid: Version!=NULL <==> RC valid.
-    // printf("%s (%u): GL_VERSION string is \"%s\".\n", __FILE__, __LINE__, Version==NULL ? "NULL" : Version);
+    Maximize();     // The wxMAXIMIZE frame style does not seem to suffice...
+    Show();
 }
 
 
 ParentFrameT::~ParentFrameT()
 {
     m_FileHistory.Save(*wxConfigBase::Get());
+
+    // Release the GuiManager (BEFORE the renderer).
+    if (cf::GuiSys::GuiMan!=NULL)
+    {
+        delete cf::GuiSys::GuiMan;
+        cf::GuiSys::GuiMan=NULL;
+    }
+
+    // Release the Cafu Material System.
+    MatSys::TextureMapManager=NULL;
+
+    if (MatSys::Renderer!=NULL)
+    {
+        MatSys::Renderer->Release();
+        MatSys::Renderer=NULL;
+    }
+
+    if (m_RendererDLL!=NULL)
+    {
+        FreeLibrary(m_RendererDLL);
+        m_RendererDLL=NULL;
+    }
 }
 
 
@@ -205,6 +236,117 @@
 #endif
 
 
+void ParentFrameT::OnShow(wxShowEvent& SE)
+{
+    if (SE.IsShown() && m_RendererDLL==NULL)
+    {
+        wxASSERT(m_GLCanvas->IsShownOnScreen());
+
+        // Aha - under X11 this must be *after* Show(true), not before the window is opened!
+        m_GLCanvas->SetCurrent(*m_GLContext);
+
+        // Prepare the name strings.
+        #ifdef SCONS_BUILD_DIR
+            #define QUOTE(str) QUOTE_HELPER(str)
+            #define QUOTE_HELPER(str) #str
+
+            #ifdef _WIN32
+            const wxString MatSysRendererDLLName=wxString("Libs/")+QUOTE(SCONS_BUILD_DIR)+"/MaterialSystem/RendererOpenGL12.dll";
+            #else
+            const wxString MatSysRendererDLLName=wxString("Libs/")+QUOTE(SCONS_BUILD_DIR)+"/MaterialSystem/libRendererOpenGL12.so";
+            #endif
+
+            #undef QUOTE
+            #undef QUOTE_HELPER
+        #else
+            const wxString MatSysRendererDLLName=wxString("Renderers/RendererOpenGL12")+PlatformAux::GetEnvFileSuffix().c_str()+".dll";
+        #endif
+
+
+        // Load the DLL.
+        #ifdef _WIN32
+            m_RendererDLL=LoadLibrary(L"./RendererOpenGL12.dll");
+
+            if (m_RendererDLL==NULL)
+                m_RendererDLL=LoadLibrary(MatSysRendererDLLName);
+        #else
+            // Note that RTLD_GLOBAL must *not* be passed-in here, or else we get in trouble with subsequently loaded libraries.
+            // (E.g. dlsym(RendererDLL, "GetRenderer") return identical results for different RendererDLLs.)
+            // Please refer to the man page of dlopen for more details.
+            m_RendererDLL=dlopen("./libRendererOpenGL12.so", RTLD_NOW);
+            if (!m_RendererDLL) m_RendererDLL=dlopen(MatSysRendererDLLName.c_str(), RTLD_NOW);
+
+            if (!m_RendererDLL) printf("%s\n", dlerror());
+        #endif
+
+        if (m_RendererDLL==NULL) { wxMessageBox("FAILED - could not load the library at "+MatSysRendererDLLName, "ERROR"); Destroy(); return; }
+
+
+        // Get the renderer.
+        typedef MatSys::RendererI* (__stdcall *GetRendererT)(cf::ConsoleI* Console_, cf::FileSys::FileManI* FileMan_);
+
+        #ifdef _WIN32
+            GetRendererT GetRenderer=(GetRendererT)GetProcAddress(m_RendererDLL, "_GetRenderer@8");
+        #else
+            GetRendererT GetRenderer=(GetRendererT)GetProcAddress(m_RendererDLL, "GetRenderer");
+        #endif
+
+        if (!GetRenderer) { wxMessageBox("FAILED - could not get the address of the GetRenderer() function.", "ERROR"); Destroy(); return; }
+
+        // When we get here, the console and the file man must already have been initialized.
+        wxASSERT(Console!=NULL);
+        wxASSERT(cf::FileSys::FileMan!=NULL);
+        MatSys::Renderer=GetRenderer(Console, cf::FileSys::FileMan);
+
+        if (MatSys::Renderer==NULL) { wxMessageBox("FAILED - could not get the renderer.", "ERROR"); Destroy(); return; }
+
+
+        // Check if we already have OpenGL errors here.
+        // Shouldn't be the case though, because any errors here must have been caused by the ParentFrames wxCanvas ctor.
+        GLenum Error=glGetError();
+        if (Error!=GL_NO_ERROR) wxMessageBox(wxString::Format("glGetError() reported error %i!", Error));
+
+        if (!MatSys::Renderer->IsSupported())
+        {
+            wxMessageBox("Renderer "+MatSysRendererDLLName+" says that it's not supported.\n\n"
+                         "This may be caused by your desktop being set to 16 BPP (or less).\n"
+                         "Please set your desktop bit-depth to 24 or 32 BPP (True Color), and try again.");
+            Destroy();
+            return;
+        }
+
+        MatSys::Renderer->Initialize();
+
+
+        // Get the texture manager.
+        typedef MatSys::TextureMapManagerI* (__stdcall *GetTMMT)();
+
+        #ifdef _WIN32
+            GetTMMT GetTMM=(GetTMMT)GetProcAddress(m_RendererDLL, "_GetTextureMapManager@0");
+        #else
+            GetTMMT GetTMM=(GetTMMT)GetProcAddress(m_RendererDLL, "GetTextureMapManager");
+        #endif
+
+        if (!GetTMM) { wxMessageBox("FAILED - could not get the address of the GetTextureMapManager() function.", "ERROR"); Destroy(); return; }
+        MatSys::TextureMapManager=GetTMM();
+        if (MatSys::TextureMapManager==NULL) { wxMessageBox("No TextureMapManager obtained.", "ERROR"); Destroy(); return; }
+
+        // Create a very simple lightmap for the materials that need one, and register it with the renderer.
+        char Data[]={ 255, 255, 255, 255, 255, 255, 0, 0,
+                      255, 255, 255, 255, 255, 255, 0, 0 };
+
+        MatSys::Renderer->SetCurrentLightMap(MatSys::TextureMapManager->GetTextureMap2D(Data, 2, 2, 3, true, MapCompositionT(MapCompositionT::Linear, MapCompositionT::Linear)));
+        MatSys::Renderer->SetCurrentLightDirMap(NULL);      // The MatSys provides a default for LightDirMaps when NULL is set.
+
+
+        // Initialize the GUI managager.
+        // This has to be done after all materials are loaded (AppCaWE::OnInit()) and after the MatSys::Renderer has been initialized,
+        // so that the GuiManager finds its default material and can register it for rendering.
+        cf::GuiSys::GuiMan=new cf::GuiSys::GuiManImplT();
+    }
+}
+
+
 void ParentFrameT::OnClose(wxCloseEvent& CE)
 {
     if (!CE.CanVeto())
Index: CaWE/AppCaWE.hpp
===================================================================
--- CaWE/AppCaWE.hpp	(revision 40)
+++ CaWE/AppCaWE.hpp	(working copy)
@@ -26,11 +26,6 @@
 
 #include "wx/wx.h"
 
-#if __linux__
-#define HMODULE void*
-#endif
-
-
 class ParentFrameT;
 class wxFileConfig;
 
@@ -53,7 +48,6 @@
 
     wxFileConfig* m_FileConfig;
     ParentFrameT* m_ParentFrame;
-    HMODULE       m_RendererDLL;
 };
 
 
Index: CaWE/ParentFrame.hpp
===================================================================
--- CaWE/ParentFrame.hpp	(revision 40)
+++ CaWE/ParentFrame.hpp	(working copy)
@@ -28,7 +28,11 @@
 #include "wx/mdi.h"
 #include "Templates/Array.hpp"
 
+#if __linux__
+#define HMODULE void*
+#endif
 
+
 class wxGLCanvas;
 class wxGLContext;
 class wxFileName;
@@ -109,10 +113,13 @@
 #ifdef __WXGTK__
     void OnSize    (wxSizeEvent&    SE);
 #endif
+    void OnShow    (wxShowEvent&    SE);    ///< Event handler for "has been shown" events.
     void OnClose   (wxCloseEvent&   CE);    ///< Event handler for close events, e.g. after a system close button or command or a call to Close(). See wx Window Deletion Overview for more details.
     void OnMenuFile(wxCommandEvent& CE);    ///< Event handler for File menu events.
     void OnMenuHelp(wxCommandEvent& CE);    ///< Event handler for Help menu events.
 
+    HMODULE m_RendererDLL;
+
     DECLARE_EVENT_TABLE()
 };
 

