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