Ticket #3: fix_matsys_init.patch
| File fix_matsys_init.patch, 18.1 KB (added by Carsten, 2 years ago) |
|---|
-
CaWE/AppCaWE.cpp
31 31 #include "wx/splash.h" 32 32 #include "wx/stdpaths.h" 33 33 34 #if defined(_WIN32)35 #define WIN32_LEAN_AND_MEAN36 #include <windows.h>37 #include <direct.h>38 #endif39 40 34 #include "AppCaWE.hpp" 41 35 #include "ChildFrameViewWin2D.hpp" 42 36 #include "ChildFrameViewWin3D.hpp" … … 50 44 #include "ToolbarMaterials.hpp" 51 45 #include "Tool.hpp" 52 46 #include "ToolManager.hpp" 47 48 #include "ClipSys/CollisionModelMan_impl.hpp" 53 49 #include "ConsoleCommands/Console.hpp" 54 50 #include "ConsoleCommands/ConsoleStdout.hpp" 55 51 #include "ConsoleCommands/ConsoleInterpreter.hpp" … … 57 53 #include "ConsoleCommands/ConVar.hpp" 58 54 #include "ConsoleCommands/ConFunc.hpp" 59 55 #include "FileSys/FileManImpl.hpp" 60 #include "MaterialSystem/MapComposition.hpp"61 #include "MaterialSystem/MaterialManagerImpl.hpp"62 #include "MaterialSystem/Renderer.hpp"63 #include "MaterialSystem/TextureMap.hpp"64 #include "PlatformAux.hpp"65 56 #include "GuiSys/GuiMan.hpp" 66 #include "GuiSys/GuiManImpl.hpp"67 57 #include "GuiSys/Window.hpp" 58 #include "MaterialSystem/MaterialManagerImpl.hpp" 68 59 #include "TypeSys.hpp" 69 #include "ClipSys/CollisionModelMan_impl.hpp"70 60 71 #ifdef _WIN3272 #elif __linux__73 #include <dirent.h>74 #include <dlfcn.h>75 #define __stdcall76 #define GetProcAddress dlsym77 #define FreeLibrary dlclose78 #endif79 61 80 81 62 static cf::ConsoleStdoutT ConsoleStdout; 82 63 cf::ConsoleI* Console=&ConsoleStdout; 83 64 … … 102 83 AppCaWE::AppCaWE() 103 84 : wxApp(), 104 85 m_FileConfig(NULL), 105 m_ParentFrame(NULL), 106 m_RendererDLL(NULL) 86 m_ParentFrame(NULL) 107 87 { 108 88 static ConsoleInterpreterImplT ConInterpreterImpl; 109 89 ConsoleInterpreter=&ConInterpreterImpl; … … 232 212 233 213 MaterialManager=&MatManImpl; 234 214 235 236 // Prepare the name strings.237 #ifdef SCONS_BUILD_DIR238 #define QUOTE(str) QUOTE_HELPER(str)239 #define QUOTE_HELPER(str) #str240 241 #ifdef _WIN32242 const wxString MatSysRendererDLLName=wxString("Libs/")+QUOTE(SCONS_BUILD_DIR)+"/MaterialSystem/RendererOpenGL12.dll";243 #else244 const wxString MatSysRendererDLLName=wxString("Libs/")+QUOTE(SCONS_BUILD_DIR)+"/MaterialSystem/libRendererOpenGL12.so";245 #endif246 247 #undef QUOTE248 #undef QUOTE_HELPER249 #else250 const wxString MatSysRendererDLLName=wxString("Renderers/RendererOpenGL12")+PlatformAux::GetEnvFileSuffix().c_str()+".dll";251 #endif252 253 254 // Load the DLL.255 #ifdef _WIN32256 m_RendererDLL=LoadLibrary(L"./RendererOpenGL12.dll");257 258 if (m_RendererDLL==NULL)259 m_RendererDLL=LoadLibrary(MatSysRendererDLLName);260 #else261 // Note that RTLD_GLOBAL must *not* be passed-in here, or else we get in trouble with subsequently loaded libraries.262 // (E.g. dlsym(RendererDLL, "GetRenderer") return identical results for different RendererDLLs.)263 // Please refer to the man page of dlopen for more details.264 m_RendererDLL=dlopen("./libRendererOpenGL12.so", RTLD_NOW);265 if (!m_RendererDLL) m_RendererDLL=dlopen(MatSysRendererDLLName.c_str(), RTLD_NOW);266 267 if (!m_RendererDLL) printf("%s\n", dlerror());268 #endif269 270 if (m_RendererDLL==NULL) { wxMessageBox("FAILED - could not load the library at "+MatSysRendererDLLName, "ERROR"); return false; }271 272 273 // Get the renderer.274 typedef MatSys::RendererI* (__stdcall *GetRendererT)(cf::ConsoleI* Console_, cf::FileSys::FileManI* FileMan_);275 276 #ifdef _WIN32277 GetRendererT GetRenderer=(GetRendererT)GetProcAddress(m_RendererDLL, "_GetRenderer@8");278 #else279 GetRendererT GetRenderer=(GetRendererT)GetProcAddress(m_RendererDLL, "GetRenderer");280 #endif281 282 if (!GetRenderer) { wxMessageBox("FAILED - could not get the address of the GetRenderer() function.", "ERROR"); FreeLibrary(m_RendererDLL); return false; }283 284 // When we get here, the console and the file man must already have been initialized.285 assert(Console!=NULL);286 assert(cf::FileSys::FileMan!=NULL);287 MatSys::Renderer=GetRenderer(Console, cf::FileSys::FileMan);288 289 if (MatSys::Renderer==NULL) { wxMessageBox("FAILED - could not get the renderer.", "ERROR"); FreeLibrary(m_RendererDLL); return false; }290 291 292 // Check if we already have OpenGL errors here.293 // Shouldn't be the case though, because any errors here must have been caused by the ParentFrames wxCanvas ctor.294 GLenum Error=glGetError();295 if (Error!=GL_NO_ERROR) wxMessageBox(wxString::Format("glGetError() reported error %i!", Error));296 297 if (!MatSys::Renderer->IsSupported())298 {299 wxMessageBox("Renderer "+MatSysRendererDLLName+" says that it's not supported.\n\n"300 "(This may be caused by your desktop being set to 16 BPP (or less).\n"301 "Please set your desktop bit-depth to 32 BPP (True Color), and try again.)");302 FreeLibrary(m_RendererDLL);303 return false;304 }305 306 MatSys::Renderer->Initialize();307 308 309 // Get the texture manager.310 typedef MatSys::TextureMapManagerI* (__stdcall *GetTMMT)();311 312 #ifdef _WIN32313 GetTMMT GetTMM=(GetTMMT)GetProcAddress(m_RendererDLL, "_GetTextureMapManager@0");314 #else315 GetTMMT GetTMM=(GetTMMT)GetProcAddress(m_RendererDLL, "GetTextureMapManager");316 #endif317 318 if (!GetTMM) { wxMessageBox("FAILED - could not get the address of the GetTextureMapManager() function.", "ERROR"); FreeLibrary(m_RendererDLL); return false; }319 MatSys::TextureMapManager=GetTMM();320 if (MatSys::TextureMapManager==NULL) { wxMessageBox("No TextureMapManager obtained.", "ERROR"); FreeLibrary(m_RendererDLL); return false; }321 322 215 // Register the material script with the CaWE materials definitions. 323 216 if (MaterialManager->RegisterMaterialScript(AppDir+"/res/CaWE.cmat", AppDir+"/res/").Size()==0) 324 217 wxMessageBox("CaWE.cmat not found in \""+AppDir+"\".", "WARNING"); 325 218 326 // Create a very simple lightmap for the materials that need one, and register it with the renderer.327 char Data[]={ 255, 255, 255, 255, 255, 255, 0, 0,328 255, 255, 255, 255, 255, 255, 0, 0 };329 330 MatSys::Renderer->SetCurrentLightMap(MatSys::TextureMapManager->GetTextureMap2D(Data, 2, 2, 3, true, MapCompositionT(MapCompositionT::Linear, MapCompositionT::Linear)));331 MatSys::Renderer->SetCurrentLightDirMap(NULL); // The MatSys provides a default for LightDirMaps when NULL is set.332 333 334 219 // Initialize the global options from the CaWE config files. 335 220 Options.Init(); 336 221 … … 338 223 wxASSERT(CursorMan==NULL); 339 224 CursorMan=new CursorManT; 340 225 341 // Initialize the GUI managager.342 // This has to be done after all materials are loaded (during Options initialization) so the GuiManager finds its default material.343 cf::GuiSys::GuiMan=new cf::GuiSys::GuiManImplT();344 345 226 return wxApp::OnInit(); 346 227 } 347 228 … … 355 236 delete m_FileConfig; 356 237 m_FileConfig=NULL; 357 238 358 // Release the GuiManager BEFORE the renderer.359 delete cf::GuiSys::GuiMan;360 cf::GuiSys::GuiMan=NULL;361 362 // Unload the Cafu Material System.363 // Checks for NULL are required to gracefully handle (DLL loading, and other) failures in InitInstance().364 if (MatSys::Renderer!=NULL && m_RendererDLL)365 {366 MatSys::Renderer->Release();367 FreeLibrary(m_RendererDLL);368 }369 370 239 // TODO: delete cf::FileSys::FileMan; // Shoud have cf::FileSys::FileMan=new cf::FileSys::FileManImplT; in OnInit(). 371 240 cf::FileSys::FileMan=NULL; 372 241 -
CaWE/ParentFrame.cpp
34 34 #include "ModelEditor/ChildFrame.hpp" 35 35 #include "ModelEditor/Document.hpp" 36 36 37 #include "ConsoleCommands/Console.hpp" 38 #include "FileSys/FileManImpl.hpp" 37 39 #include "GuiSys/GuiImpl.hpp" // Needed to catch InitErrorT if GUI document creation fails. 40 #include "GuiSys/GuiManImpl.hpp" 41 #include "MaterialSystem/MapComposition.hpp" 42 #include "MaterialSystem/Renderer.hpp" 43 #include "MaterialSystem/TextureMap.hpp" 38 44 #include "TextParser/TextParser.hpp" 45 #include "PlatformAux.hpp" 39 46 40 47 #include "wx/wx.h" 41 48 #include "wx/aboutdlg.h" … … 49 56 50 57 #include <fstream> 51 58 59 #ifdef _WIN32 60 #define WIN32_LEAN_AND_MEAN 61 #include <windows.h> 62 #include <direct.h> 63 #elif __linux__ 64 #include <dirent.h> 65 #include <dlfcn.h> 66 #define __stdcall 67 #define GetProcAddress dlsym 68 #define FreeLibrary dlclose 69 #endif 52 70 71 53 72 BEGIN_EVENT_TABLE(ParentFrameT, wxMDIParentFrame) 54 73 #ifdef __WXGTK__ 55 74 EVT_SIZE(ParentFrameT::OnSize) 56 75 #endif 76 EVT_SHOW(ParentFrameT::OnShow) 57 77 EVT_CLOSE(ParentFrameT::OnClose) 58 78 EVT_MENU_RANGE(ID_MENU_FILE_NEW_MAP, ID_MENU_FILE_EXIT, ParentFrameT::OnMenuFile) 59 79 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, ParentFrameT::OnMenuFile) … … 84 104 : wxMDIParentFrame(NULL /*parent*/, -1 /*id*/, wxString("Cafu World Editor - ") + __DATE__, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE | wxMAXIMIZE), 85 105 #endif 86 106 m_GLCanvas(NULL), 87 m_GLContext(NULL) 107 m_GLContext(NULL), 108 m_RendererDLL(NULL) 88 109 { 89 110 wxMenuBar *item0 = new wxMenuBar; 90 111 … … 141 162 #endif 142 163 143 164 144 // Create the parent GL canvas. 145 m_GLCanvas=new wxGLCanvas(this, -1, OpenGLAttributeList, wxPoint(600, 5), wxSize(10, 10), 0, "ParentGLCanvas"); 146 147 148 Centre(wxBOTH); 149 Maximize(true); // The wxMAXIMIZE frame style does not seem to have any effect for this frame...(?) 150 Show(true); 165 // Create the parent GL canvas and a GL context. 166 m_GLCanvas =new wxGLCanvas(this, -1, OpenGLAttributeList, wxPoint(600, 5), wxSize(10, 10), 0, "ParentGLCanvas"); 151 167 m_GLContext=new wxGLContext(m_GLCanvas); 152 wxASSERT(m_GLContext);153 m_GLCanvas->SetCurrent(*m_GLContext); // Aha - under X11 this must be *after* Show(true), not before the window is opened!154 168 155 // The above attribute list can certainly NOT be met on 16 BPP desktops. 156 // However, we do not check if the RC of the m_GLCanvas is valid here (it should), 157 // as the IsSupported() method of each MatSys Renderer does the same check, too, 158 // and I have adjusted the error message appropriately there (see AppCaWE.cpp, 159 // the user is told to check whether his desktop is set to 32 BPP if IsSupported() fails). 160 // const char* Version=(char const*)glGetString(GL_VERSION); // Another way to see if the RC is valid: Version!=NULL <==> RC valid. 161 // printf("%s (%u): GL_VERSION string is \"%s\".\n", __FILE__, __LINE__, Version==NULL ? "NULL" : Version); 169 Maximize(); // The wxMAXIMIZE frame style does not seem to suffice... 170 Show(); 162 171 } 163 172 164 173 165 174 ParentFrameT::~ParentFrameT() 166 175 { 167 176 m_FileHistory.Save(*wxConfigBase::Get()); 177 178 // Release the GuiManager (BEFORE the renderer). 179 if (cf::GuiSys::GuiMan!=NULL) 180 { 181 delete cf::GuiSys::GuiMan; 182 cf::GuiSys::GuiMan=NULL; 183 } 184 185 // Release the Cafu Material System. 186 MatSys::TextureMapManager=NULL; 187 188 if (MatSys::Renderer!=NULL) 189 { 190 MatSys::Renderer->Release(); 191 MatSys::Renderer=NULL; 192 } 193 194 if (m_RendererDLL!=NULL) 195 { 196 FreeLibrary(m_RendererDLL); 197 m_RendererDLL=NULL; 198 } 168 199 } 169 200 170 201 … … 205 236 #endif 206 237 207 238 239 void ParentFrameT::OnShow(wxShowEvent& SE) 240 { 241 if (SE.IsShown() && m_RendererDLL==NULL) 242 { 243 wxASSERT(m_GLCanvas->IsShownOnScreen()); 244 245 // Aha - under X11 this must be *after* Show(true), not before the window is opened! 246 m_GLCanvas->SetCurrent(*m_GLContext); 247 248 // Prepare the name strings. 249 #ifdef SCONS_BUILD_DIR 250 #define QUOTE(str) QUOTE_HELPER(str) 251 #define QUOTE_HELPER(str) #str 252 253 #ifdef _WIN32 254 const wxString MatSysRendererDLLName=wxString("Libs/")+QUOTE(SCONS_BUILD_DIR)+"/MaterialSystem/RendererOpenGL12.dll"; 255 #else 256 const wxString MatSysRendererDLLName=wxString("Libs/")+QUOTE(SCONS_BUILD_DIR)+"/MaterialSystem/libRendererOpenGL12.so"; 257 #endif 258 259 #undef QUOTE 260 #undef QUOTE_HELPER 261 #else 262 const wxString MatSysRendererDLLName=wxString("Renderers/RendererOpenGL12")+PlatformAux::GetEnvFileSuffix().c_str()+".dll"; 263 #endif 264 265 266 // Load the DLL. 267 #ifdef _WIN32 268 m_RendererDLL=LoadLibrary(L"./RendererOpenGL12.dll"); 269 270 if (m_RendererDLL==NULL) 271 m_RendererDLL=LoadLibrary(MatSysRendererDLLName); 272 #else 273 // Note that RTLD_GLOBAL must *not* be passed-in here, or else we get in trouble with subsequently loaded libraries. 274 // (E.g. dlsym(RendererDLL, "GetRenderer") return identical results for different RendererDLLs.) 275 // Please refer to the man page of dlopen for more details. 276 m_RendererDLL=dlopen("./libRendererOpenGL12.so", RTLD_NOW); 277 if (!m_RendererDLL) m_RendererDLL=dlopen(MatSysRendererDLLName.c_str(), RTLD_NOW); 278 279 if (!m_RendererDLL) printf("%s\n", dlerror()); 280 #endif 281 282 if (m_RendererDLL==NULL) { wxMessageBox("FAILED - could not load the library at "+MatSysRendererDLLName, "ERROR"); Destroy(); return; } 283 284 285 // Get the renderer. 286 typedef MatSys::RendererI* (__stdcall *GetRendererT)(cf::ConsoleI* Console_, cf::FileSys::FileManI* FileMan_); 287 288 #ifdef _WIN32 289 GetRendererT GetRenderer=(GetRendererT)GetProcAddress(m_RendererDLL, "_GetRenderer@8"); 290 #else 291 GetRendererT GetRenderer=(GetRendererT)GetProcAddress(m_RendererDLL, "GetRenderer"); 292 #endif 293 294 if (!GetRenderer) { wxMessageBox("FAILED - could not get the address of the GetRenderer() function.", "ERROR"); Destroy(); return; } 295 296 // When we get here, the console and the file man must already have been initialized. 297 wxASSERT(Console!=NULL); 298 wxASSERT(cf::FileSys::FileMan!=NULL); 299 MatSys::Renderer=GetRenderer(Console, cf::FileSys::FileMan); 300 301 if (MatSys::Renderer==NULL) { wxMessageBox("FAILED - could not get the renderer.", "ERROR"); Destroy(); return; } 302 303 304 // Check if we already have OpenGL errors here. 305 // Shouldn't be the case though, because any errors here must have been caused by the ParentFrames wxCanvas ctor. 306 GLenum Error=glGetError(); 307 if (Error!=GL_NO_ERROR) wxMessageBox(wxString::Format("glGetError() reported error %i!", Error)); 308 309 if (!MatSys::Renderer->IsSupported()) 310 { 311 wxMessageBox("Renderer "+MatSysRendererDLLName+" says that it's not supported.\n\n" 312 "This may be caused by your desktop being set to 16 BPP (or less).\n" 313 "Please set your desktop bit-depth to 24 or 32 BPP (True Color), and try again."); 314 Destroy(); 315 return; 316 } 317 318 MatSys::Renderer->Initialize(); 319 320 321 // Get the texture manager. 322 typedef MatSys::TextureMapManagerI* (__stdcall *GetTMMT)(); 323 324 #ifdef _WIN32 325 GetTMMT GetTMM=(GetTMMT)GetProcAddress(m_RendererDLL, "_GetTextureMapManager@0"); 326 #else 327 GetTMMT GetTMM=(GetTMMT)GetProcAddress(m_RendererDLL, "GetTextureMapManager"); 328 #endif 329 330 if (!GetTMM) { wxMessageBox("FAILED - could not get the address of the GetTextureMapManager() function.", "ERROR"); Destroy(); return; } 331 MatSys::TextureMapManager=GetTMM(); 332 if (MatSys::TextureMapManager==NULL) { wxMessageBox("No TextureMapManager obtained.", "ERROR"); Destroy(); return; } 333 334 // Create a very simple lightmap for the materials that need one, and register it with the renderer. 335 char Data[]={ 255, 255, 255, 255, 255, 255, 0, 0, 336 255, 255, 255, 255, 255, 255, 0, 0 }; 337 338 MatSys::Renderer->SetCurrentLightMap(MatSys::TextureMapManager->GetTextureMap2D(Data, 2, 2, 3, true, MapCompositionT(MapCompositionT::Linear, MapCompositionT::Linear))); 339 MatSys::Renderer->SetCurrentLightDirMap(NULL); // The MatSys provides a default for LightDirMaps when NULL is set. 340 341 342 // Initialize the GUI managager. 343 // This has to be done after all materials are loaded (AppCaWE::OnInit()) and after the MatSys::Renderer has been initialized, 344 // so that the GuiManager finds its default material and can register it for rendering. 345 cf::GuiSys::GuiMan=new cf::GuiSys::GuiManImplT(); 346 } 347 } 348 349 208 350 void ParentFrameT::OnClose(wxCloseEvent& CE) 209 351 { 210 352 if (!CE.CanVeto()) -
CaWE/AppCaWE.hpp
26 26 27 27 #include "wx/wx.h" 28 28 29 #if __linux__30 #define HMODULE void*31 #endif32 33 34 29 class ParentFrameT; 35 30 class wxFileConfig; 36 31 … … 53 48 54 49 wxFileConfig* m_FileConfig; 55 50 ParentFrameT* m_ParentFrame; 56 HMODULE m_RendererDLL;57 51 }; 58 52 59 53 -
CaWE/ParentFrame.hpp
28 28 #include "wx/mdi.h" 29 29 #include "Templates/Array.hpp" 30 30 31 #if __linux__ 32 #define HMODULE void* 33 #endif 31 34 35 32 36 class wxGLCanvas; 33 37 class wxGLContext; 34 38 class wxFileName; … … 109 113 #ifdef __WXGTK__ 110 114 void OnSize (wxSizeEvent& SE); 111 115 #endif 116 void OnShow (wxShowEvent& SE); ///< Event handler for "has been shown" events. 112 117 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. 113 118 void OnMenuFile(wxCommandEvent& CE); ///< Event handler for File menu events. 114 119 void OnMenuHelp(wxCommandEvent& CE); ///< Event handler for Help menu events. 115 120 121 HMODULE m_RendererDLL; 122 116 123 DECLARE_EVENT_TABLE() 117 124 }; 118 125
