Blender V2.61 - r43446

KX_GameActuator.cpp

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