Blender V2.61 - r43446

GPC_Engine.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #ifdef WIN32
00034     #pragma warning (disable:4786) // suppress stl-MSVC debug info warning
00035 #endif // WIN32
00036 
00037 #include <iostream>
00038 
00039 #include "BKE_blender.h"  // initglobals()
00040 #include "BKE_global.h"  // Global G
00041 #include "BKE_report.h"
00042 #include "DNA_scene_types.h"
00043 #include "DNA_camera_types.h"  // Camera
00044 #include "DNA_object_types.h"  // Object
00045 
00046 #include "BLO_readfile.h"
00047 #include "BLI_blenlib.h"
00048 
00049 // include files needed by "KX_BlenderSceneConverter.h"
00050 
00051 #include "CTR_Map.h"
00052 #include "SCA_IActuator.h"
00053 #include "RAS_MeshObject.h"
00054 
00055 #include "KX_BlenderSceneConverter.h"
00056 #include "KX_KetsjiEngine.h"
00057 #include "NG_LoopBackNetworkDeviceInterface.h"
00058 
00059 #include "RAS_IRenderTools.h"
00060 
00061 #include "GPC_Engine.h"
00062 #include "GPC_KeyboardDevice.h"
00063 #include "GPC_MouseDevice.h"
00064 #include "GPC_RawImage.h"
00065 #include "GPC_RawLoadDotBlendArray.h"
00066 
00067 
00068 
00069 GPC_Engine::GPC_Engine(char *customLoadingAnimationURL,
00070         int foregroundColor, int backgroundColor, int frameRate) :
00071         m_initialized(false), m_running(false), m_loading(false),
00072         m_customLoadingAnimation(false), m_previousProgress(0.0),
00073         m_system(NULL), m_keyboarddev(NULL),
00074         m_mousedev(NULL), m_canvas(NULL), m_rendertools(NULL),
00075         m_portal(NULL), m_sceneconverter(NULL), m_networkdev(NULL),
00076         m_curarea(NULL), m_customLoadingAnimationURL(NULL),
00077         m_foregroundColor(foregroundColor), m_backgroundColor(backgroundColor),
00078         m_frameRate(frameRate),
00079         m_BlenderLogo(0), m_Blender3DLogo(0)/*, m_NaNLogo(0)*/
00080 {
00081     if(customLoadingAnimationURL[0] != '\0')
00082     {
00083         m_customLoadingAnimationURL = new char[sizeof(customLoadingAnimationURL)];
00084 // not yet, need to be implemented first...     m_customLoadingAnimation = true;
00085     }
00086 
00087     // load the Blender logo into memory
00088     m_BlenderLogo = new GPC_RawImage();
00089     // blender3d size is 115 x 32 so make resulting texture 128 x 128
00090     if(!m_BlenderLogo->Load("BlenderLogo", 128, 128, GPC_RawImage::alignTopLeft, 8, 8))
00091         m_BlenderLogo = 0;
00092 
00093     // load the Blender3D logo into memory
00094     m_Blender3DLogo = new GPC_RawImage();
00095     // blender3d size is 136 x 11 so make resulting texture 256 x 256
00096     if(!m_Blender3DLogo->Load("Blender3DLogo", 256, 256, GPC_RawImage::alignBottomRight, 8, 8))
00097         m_Blender3DLogo = 0;
00098 
00099 #if 0
00100     // obsolete logo
00101     // load the NaN logo into memory
00102     m_NaNLogo = new GPC_RawImage();
00103     // blender3d size is 32 x 31 so make resulting texture 64 x 64
00104     if(!m_NaNLogo->Load("NaNLogo", 64, 64, GPC_RawImage::alignBottomRight, 8, 8))
00105         m_NaNLogo = 0;
00106 #endif
00107 }
00108 
00109 
00110 GPC_Engine::~GPC_Engine()
00111 {
00112     // deleting everything in reverse order of creation
00113 #if 0
00114 // hmm deleted in Stop()    delete m_portal;
00115 // hmm deleted in Stop()    delete m_sceneconverter;
00116     delete m_system;
00117     delete m_networkdev;
00118     delete m_rendertools;
00119     delete m_canvas;
00120     delete m_mousedev;
00121     delete m_keyboarddev;
00122 // not yet used so be careful and not delete them
00123 //  delete m_WaveCache;
00124 //  delete m_curarea;  // for future use, not used yet
00125 #endif
00126     delete m_BlenderLogo;
00127     delete m_Blender3DLogo;
00128 #if 0
00129     delete m_NaNLogo;
00130 #endif
00131 }
00132 
00133 
00134 bool GPC_Engine::Start(const char *filename)
00135 {
00136     ReportList reports;
00137     BlendFileData *bfd;
00138     
00139     BKE_reports_init(&reports, RPT_STORE);
00140     bfd= BLO_read_from_file(filename, &reports);
00141     BKE_reports_clear(&reports);
00142 
00143     if (!bfd) {
00144             // XXX, deal with error here
00145         cout << "Unable to load: " << filename << endl;
00146         return false;
00147     }
00148 
00149     StartKetsji();
00150 
00151     if(bfd->type == BLENFILETYPE_PUB)
00152         m_canvas->SetBannerDisplayEnabled(false);
00153 
00154     return true;
00155 }
00156 
00157 
00158 bool GPC_Engine::Start(unsigned char *blenderDataBuffer,
00159         unsigned int blenderDataBufferSize)
00160 {
00161     ReportList reports;
00162     BlendFileData *bfd;
00163     
00164     BKE_reports_init(&reports, RPT_STORE);
00165     bfd= BLO_read_from_memory(blenderDataBuffer, blenderDataBufferSize, &reports);
00166     BKE_reports_clear(&reports);
00167 
00168     if (!bfd) {
00169             // XXX, deal with error here
00170         cout << "Unable to load. " << endl;
00171         return false;
00172     }
00173     
00174     StartKetsji();
00175 
00176     if(bfd->type == BLENFILETYPE_PUB)
00177         m_canvas->SetBannerDisplayEnabled(false);
00178 
00179     return true;
00180 }
00181 
00182 
00183 bool GPC_Engine::StartKetsji(void)
00184 {
00185     STR_String startSceneName = ""; // XXX scene->id.name + 2;
00186 /*
00187     KX_KetsjiEngine* ketsjieng = new KX_KetsjiEngine(m_system);
00188     m_portal = new KetsjiPortal(ketsjieng);
00189     m_portal->setSecurity(psl_Highest);
00190         
00191     KX_ISceneConverter *sceneconverter = new KX_BlenderSceneConverter(&G, ketsjieng);
00192         
00193     m_portal->Enter(
00194             startSceneName,
00195             sceneconverter,
00196             m_canvas,
00197             m_rendertools,
00198             m_keyboarddev,
00199             m_mousedev,
00200             m_networkdev,
00201             m_system);
00202 
00203     m_system->SetMainLoop(m_portal->m_ketsjieng);
00204 
00205     m_running = true;
00206     */
00207     return true;
00208 }
00209 
00210 
00211 void GPC_Engine::StartLoadingAnimation()
00212 {
00213     if(m_customLoadingAnimation)
00214     {
00215     }
00216     else
00217     {
00218         unsigned char *blenderDataBuffer;
00219         int blenderDataBufferSize;
00220         GetRawLoadingAnimation(&blenderDataBuffer, &blenderDataBufferSize);
00221         if(!Start(blenderDataBuffer, blenderDataBufferSize))
00222             cout << "something went wrong when starting the engine" << endl;
00223         delete blenderDataBuffer;  // created with 'new' in GetRawLoadingAnimation()
00224     }
00225 }
00226 
00227     
00228 // will be platform dependant
00229 float GPC_Engine::DetermineProgress(void)
00230 {
00231 #if 0
00232     float progress;
00233     if ((m_blenderData.m_ulProgress > 0) &&
00234             (m_blenderData.m_ulProgressMax != m_blenderData.m_ulProgress)) {
00235         progress = (float)m_blenderData.m_ulProgress;
00236         progress /= (float)m_blenderData.m_ulProgressMax;
00237     }
00238     else {
00239         progress = 0.f;
00240     }
00241     progress *= 100.f;
00242     return (unsigned int) progress ;
00243 #endif
00244     return m_previousProgress + 0.01;  // temporary TODO
00245 }
00246 
00247     
00248 void GPC_Engine::UpdateLoadingAnimation(void)
00249 {
00250     //int delta;
00251 
00252     float progress = DetermineProgress();
00253 
00254     if(progress > m_previousProgress)
00255     {
00256 //      delta = progress - m_previousProgress;
00257         m_previousProgress = progress;
00258         if(m_previousProgress > 1.0)
00259             m_previousProgress = 1.0;  // limit to 1.0 (has to change !)
00260 //          m_engine->m_previousProgress = 0.0;
00261     }
00262 
00263     STR_String to = "";
00264     STR_String from = "";
00265     STR_String subject = "progress";
00266     STR_String body;
00267     body.Format("%f", progress);  // a number between 0.0 and 1.0
00268 
00269     if(m_networkdev)
00270     {
00271         // Store a progress message in the network device.
00272         NG_NetworkMessage* msg = new NG_NetworkMessage(to, from, subject, body);
00273         m_networkdev->SendNetworkMessage(msg);
00274         msg->Release();
00275     }
00276 }
00277 
00278 
00279 void GPC_Engine::Stop()
00280 {
00281     // only delete things that are created in StartKetsji()
00282 /*  if(m_portal)
00283     {
00284         m_portal->Leave();
00285         delete m_portal;  // also gets rid of KX_KetsjiEngine (says Maarten)
00286         m_portal = 0;
00287     }
00288 */  if(m_sceneconverter)
00289     {
00290         delete m_sceneconverter;
00291         m_sceneconverter = 0;
00292     }
00293 #if 0
00294     if(m_frameTimerID)
00295     {
00296         ::KillTimer(0, m_frameTimerID);
00297         m_frameTimerID = 0;
00298     }
00299     m_engineRunning = false;
00300 #endif
00301 
00302     m_running = false;
00303 }
00304 
00305 
00306 void GPC_Engine::Exit()
00307 {
00308     if(m_running)
00309         Stop();
00310 
00311     if (m_system) {
00312         delete m_system;
00313         m_system = 0;
00314     }
00315     if (m_keyboarddev) {
00316         delete m_keyboarddev;
00317         m_keyboarddev = 0;
00318     }
00319     if (m_mousedev) {
00320         delete m_mousedev;
00321         m_mousedev = 0;
00322     }
00323     if (m_canvas) {
00324         delete m_canvas;
00325         m_canvas = 0;
00326     }
00327     if (m_rendertools) {
00328         delete m_rendertools;
00329         m_rendertools = 0;
00330     }
00331     if (m_networkdev) {
00332         delete m_networkdev;
00333         m_networkdev = 0;
00334     }
00335 
00336     m_initialized = false;
00337 }
00338