Blender  V2.59
KX_GameActuator.cpp
Go to the documentation of this file.
00001 /*
00002 * global game stuff
00003 *
00004 * $Id: KX_GameActuator.cpp 35171 2011-02-25 13:35:59Z jesterking $
00005 *
00006  * ***** BEGIN GPL LICENSE BLOCK *****
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00023  * All rights reserved.
00024  *
00025  * The Original Code is: all of this file.
00026  *
00027  * Contributor(s): none yet.
00028  *
00029  * ***** END GPL LICENSE BLOCK *****
00030 */
00031 
00037 #include <stddef.h>
00038 
00039 #include "SCA_IActuator.h"
00040 #include "KX_GameActuator.h"
00041 //#include <iostream>
00042 #include "KX_Scene.h"
00043 #include "KX_KetsjiEngine.h"
00044 #include "KX_PythonInit.h" /* for config load/saving */
00045 
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 
00049 /* ------------------------------------------------------------------------- */
00050 /* Native functions                                                          */
00051 /* ------------------------------------------------------------------------- */
00052 
00053 KX_GameActuator::KX_GameActuator(SCA_IObject *gameobj, 
00054                                                                    int mode,
00055                                                                    const STR_String& filename,
00056                                                                    const STR_String& loadinganimationname,
00057                                                                    SCA_IScene* scene,
00058                                                                    KX_KetsjiEngine* ketsjiengine)
00059                                                                    : SCA_IActuator(gameobj, KX_ACT_GAME)
00060 {
00061         m_mode = mode;
00062         m_filename = filename;
00063         m_loadinganimationname = loadinganimationname;
00064         m_scene = scene;
00065         m_ketsjiengine = ketsjiengine;
00066 } /* End of constructor */
00067 
00068 
00069 
00070 KX_GameActuator::~KX_GameActuator()
00071 { 
00072         // there's nothing to be done here, really....
00073 } /* end of destructor */
00074 
00075 
00076 
00077 CValue* KX_GameActuator::GetReplica()
00078 {
00079         KX_GameActuator* replica = new KX_GameActuator(*this);
00080         replica->ProcessReplica();
00081         
00082         return replica;
00083 }
00084 
00085 
00086 
00087 bool KX_GameActuator::Update()
00088 {
00089         // bool result = false;  /*unused*/
00090         bool bNegativeEvent = IsNegativeEvent();
00091         RemoveAllEvents();
00092 
00093         if (bNegativeEvent)
00094                 return false; // do nothing on negative events
00095 
00096         switch (m_mode)
00097         {
00098         case KX_GAME_LOAD:
00099         case KX_GAME_START:
00100                 {
00101                         if (m_ketsjiengine)
00102                         {
00103                                 STR_String exitstring = "start other game";
00104                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_START_OTHER_GAME);
00105                                 m_ketsjiengine->SetNameNextGame(m_filename);
00106                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
00107                         }
00108 
00109                         break;
00110                 }
00111         case KX_GAME_RESTART:
00112                 {
00113                         if (m_ketsjiengine)
00114                         {
00115                                 STR_String exitstring = "restarting game";
00116                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_RESTART_GAME);
00117                                 m_ketsjiengine->SetNameNextGame(m_filename);
00118                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
00119                         }
00120                         break;
00121                 }
00122         case KX_GAME_QUIT:
00123                 {
00124                         if (m_ketsjiengine)
00125                         {
00126                                 STR_String exitstring = "quiting game";
00127                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_QUIT_GAME);
00128                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
00129                         }
00130                         break;
00131                 }
00132         case KX_GAME_SAVECFG:
00133                 {
00134 #ifdef WITH_PYTHON
00135                         if (m_ketsjiengine)
00136                         {
00137                                 char mashal_path[512];
00138                                 char *marshal_buffer = NULL;
00139                                 unsigned int marshal_length;
00140                                 FILE *fp = NULL;
00141                                 
00142                                 pathGamePythonConfig(mashal_path);
00143                                 marshal_length = saveGamePythonConfig(&marshal_buffer);
00144                                 
00145                                 if (marshal_length && marshal_buffer) {
00146                                         fp = fopen(mashal_path, "wb");
00147                                         if (fp) {
00148                                                 if (fwrite(marshal_buffer, 1, marshal_length, fp) != marshal_length) {
00149                                                         printf("Warning: could not write marshal data\n");
00150                                                 }
00151                                                 fclose(fp);
00152                                         } else {
00153                                                 printf("Warning: could not open marshal file\n");
00154                                         }
00155                                 } else {
00156                                         printf("Warning: could not create marshal buffer\n");
00157                                 }
00158                                 if (marshal_buffer)
00159                                         delete [] marshal_buffer;
00160                         }
00161                         break;
00162 #endif // WITH_PYTHON
00163                 }
00164         case KX_GAME_LOADCFG:
00165                 {
00166 #ifdef WITH_PYTHON
00167                         if (m_ketsjiengine)
00168                         {
00169                                 char mashal_path[512];
00170                                 char *marshal_buffer;
00171                                 int marshal_length;
00172                                 FILE *fp = NULL;
00173                                 int result;
00174                                 
00175                                 pathGamePythonConfig(mashal_path);
00176                                 
00177                                 fp = fopen(mashal_path, "rb");
00178                                 if (fp) {
00179                                         // obtain file size:
00180                                         fseek (fp , 0 , SEEK_END);
00181                                         marshal_length = ftell(fp);
00182                                         rewind(fp);
00183                                         
00184                                         marshal_buffer = (char*) malloc (sizeof(char)*marshal_length);
00185                                         
00186                                         result = fread (marshal_buffer, 1, marshal_length, fp);
00187                                         
00188                                         if (result == marshal_length) {
00189                                                 loadGamePythonConfig(marshal_buffer, marshal_length);
00190                                         } else {
00191                                                 printf("warning: could not read all of '%s'\n", mashal_path);
00192                                         }
00193                                         
00194                                         free(marshal_buffer);
00195                                         fclose(fp);
00196                                 } else {
00197                                         printf("warning: could not open '%s'\n", mashal_path);
00198                                 }
00199                         }
00200                         break;
00201 #endif // WITH_PYTHON
00202                 }
00203         default:
00204                 ; /* do nothing? this is an internal error !!! */
00205         }
00206         
00207         return false;
00208 }
00209 
00210 
00211 #ifdef WITH_PYTHON
00212 
00213 /* ------------------------------------------------------------------------- */
00214 /* Python functions                                                          */
00215 /* ------------------------------------------------------------------------- */
00216 
00217 /* Integration hooks ------------------------------------------------------- */
00218 PyTypeObject KX_GameActuator::Type = {
00219         PyVarObject_HEAD_INIT(NULL, 0)
00220         "KX_GameActuator",
00221         sizeof(PyObjectPlus_Proxy),
00222         0,
00223         py_base_dealloc,
00224         0,
00225         0,
00226         0,
00227         0,
00228         py_base_repr,
00229         0,0,0,0,0,0,0,0,0,
00230         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00231         0,0,0,0,0,0,0,
00232         Methods,
00233         0,
00234         0,
00235         &SCA_IActuator::Type,
00236         0,0,0,0,0,0,
00237         py_base_new
00238 };
00239 
00240 PyMethodDef KX_GameActuator::Methods[] =
00241 {
00242         {NULL,NULL} //Sentinel
00243 };
00244 
00245 PyAttributeDef KX_GameActuator::Attributes[] = {
00246         KX_PYATTRIBUTE_STRING_RW("fileName",0,100,false,KX_GameActuator,m_filename),
00247         KX_PYATTRIBUTE_INT_RW("mode", KX_GAME_NODEF+1, KX_GAME_MAX-1, true, KX_GameActuator, m_mode),
00248         { NULL }        //Sentinel
00249 };
00250 
00251 #endif // WITH_PYTHON