[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [17012] trunk/blender/source/gameengine: Fix for a relative paths issue in the game engine.

Brecht Van Lommel brecht at blender.org
Sat Oct 11 02:56:50 CEST 2008


Revision: 17012
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17012
Author:   blendix
Date:     2008-10-11 02:56:49 +0200 (Sat, 11 Oct 2008)

Log Message:
-----------
Fix for a relative paths issue in the game engine. G.sce was being
kept as the original file, but that can't work correct for solving
relative paths once a .blend in another directory is loaded. The
reason it went OK with the apricot tech demo is that the images there
were lib linked into the level file, which still worked.

Now it sets G.sce to the current loaded .blend file. Note that the
python config file path still uses the first loaded .blend file so it
looks in the same location each time.

Also added some NULL pointer checks in the joystick code because it
was crashing there on Mac, there's similar checks in related functions
so I'm assuming this was just a missed case.

Modified Paths:
--------------
    trunk/blender/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp
    trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_JoystickManager.cpp
    trunk/blender/source/gameengine/GamePlayer/ghost/GPG_ghost.cpp
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.h

Modified: trunk/blender/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp
===================================================================
--- trunk/blender/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp	2008-10-10 22:49:17 UTC (rev 17011)
+++ trunk/blender/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp	2008-10-11 00:56:49 UTC (rev 17012)
@@ -70,6 +70,7 @@
 #include "DNA_view3d_types.h"
 #include "DNA_screen_types.h"
 #include "BKE_global.h"
+#include "BKE_utildefines.h"
 #include "BIF_screen.h"
 #include "BIF_scrarea.h"
 
@@ -110,11 +111,14 @@
 	Main* blenderdata = maggie1;
 
 	char* startscenename = scenename;
-	char pathname[160];
-	strcpy (pathname, blenderdata->name);
+	char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE];
 	STR_String exitstring = "";
 	BlendFileData *bfd= NULL;
 
+	BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
+	BLI_strncpy(oldsce, G.sce, sizeof(oldsce));
+	setGamePythonPath(G.sce);
+
 	// Acquire Python's GIL (global interpreter lock)
 	// so we can safely run Python code and API calls
 	PyGILState_STATE gilstate = PyGILState_Ensure();
@@ -240,9 +244,14 @@
 			char basedpath[240];
 			// base the actuator filename with respect
 			// to the original file working directory
+
 			if (exitstring != "")
 				strcpy(basedpath, exitstring.Ptr());
 
+			// load relative to the last loaded file, this used to be relative
+			// to the first file but that makes no sense, relative paths in
+			// blend files should be relative to that file, not some other file
+			// that happened to be loaded first
 			BLI_convertstringcode(basedpath, pathname);
 			bfd = load_game_data(basedpath);
 			
@@ -263,6 +272,11 @@
 			{
 				blenderdata = bfd->main;
 				startscenename = bfd->curscene->id.name + 2;
+
+				if(blenderdata) {
+					BLI_strncpy(G.sce, blenderdata->name, sizeof(G.sce));
+					BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
+				}
 			}
 			// else forget it, we can't find it
 			else
@@ -507,6 +521,8 @@
 
 	if (bfd) BLO_blendfiledata_free(bfd);
 
+	BLI_strncpy(G.sce, oldsce, sizeof(G.sce));
+
 	// Release Python's GIL
 	PyGILState_Release(gilstate);
 }
@@ -522,11 +538,11 @@
 	Main* blenderdata = maggie;
 
 	char* startscenename = scenename;
-	char pathname[160];
-	strcpy (pathname, maggie->name);
+	char pathname[FILE_MAXDIR+FILE_MAXFILE];
 	STR_String exitstring = "";
-	BlendFileData *bfd= NULL;
 
+	BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
+
 	// Acquire Python's GIL (global interpreter lock)
 	// so we can safely run Python code and API calls
 	PyGILState_STATE gilstate = PyGILState_Ensure();
@@ -583,20 +599,17 @@
 		KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
 
 		Scene *blscene = NULL;
-		if (!bfd)
+
+		blscene = (Scene*) maggie->scene.first;
+		for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
 		{
-			blscene = (Scene*) maggie->scene.first;
-			for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
+			if (startscenename == (sce->id.name+2))
 			{
-				if (startscenename == (sce->id.name+2))
-				{
-					blscene = sce;
-					break;
-				}
+				blscene = sce;
+				break;
 			}
-		} else {
-			blscene = bfd->curscene;
 		}
+
         int cframe = 1, startFrame;
 		if (blscene)
 		{
@@ -717,7 +730,6 @@
 		SND_DeviceManager::Unsubscribe();
 
 	} while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
-	if (bfd) BLO_blendfiledata_free(bfd);
 
 	// Release Python's GIL
 	PyGILState_Release(gilstate);

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp	2008-10-10 22:49:17 UTC (rev 17011)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp	2008-10-11 00:56:49 UTC (rev 17012)
@@ -89,7 +89,8 @@
 	
 	int i;
 	for (i=0; i<JOYINDEX_MAX; i++) {
-		SCA_Joystick::m_instance[i]->OnNothing(&sdl_event);
+		if(SCA_Joystick::m_instance[i])
+			SCA_Joystick::m_instance[i]->OnNothing(&sdl_event);
 	}
 	
 	if(SDL_PollEvent(&sdl_event))

Modified: trunk/blender/source/gameengine/GameLogic/SCA_JoystickManager.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_JoystickManager.cpp	2008-10-10 22:49:17 UTC (rev 17011)
+++ trunk/blender/source/gameengine/GameLogic/SCA_JoystickManager.cpp	2008-10-11 00:56:49 UTC (rev 17012)
@@ -51,7 +51,8 @@
 {
 	int i;
 	for (i=0; i<JOYINDEX_MAX; i++) {
-		m_joystick[i]->ReleaseInstance();
+		if(m_joystick[i])
+			m_joystick[i]->ReleaseInstance();
 	}
 }
 

Modified: trunk/blender/source/gameengine/GamePlayer/ghost/GPG_ghost.cpp
===================================================================
--- trunk/blender/source/gameengine/GamePlayer/ghost/GPG_ghost.cpp	2008-10-10 22:49:17 UTC (rev 17011)
+++ trunk/blender/source/gameengine/GamePlayer/ghost/GPG_ghost.cpp	2008-10-11 00:56:49 UTC (rev 17012)
@@ -44,6 +44,7 @@
 #endif // __APPLE__
 #include "GEN_messaging.h"
 #include "KX_KetsjiEngine.h"
+#include "KX_PythonInit.h"
 
 /**********************************
 * Begin Blender include block
@@ -599,8 +600,8 @@
 				GPG_Application app(system);
 				bool firstTimeRunning = true;
 				char filename[FILE_MAXDIR + FILE_MAXFILE];
+				char pathname[FILE_MAXDIR + FILE_MAXFILE];
 				char *titlename;
-				char pathname[160];
 
 				get_filename(argc, argv, filename);
 				if(filename[0])
@@ -616,8 +617,7 @@
 					{
 						char basedpath[240];
 						
-						// base the actuator filename with respect
-						// to the original file working directory
+						// base the actuator filename relative to the last file
 						strcpy(basedpath, exitstring.Ptr());
 						BLI_convertstringcode(basedpath, pathname);
 						
@@ -700,15 +700,14 @@
 						//					GPG_Application app (system, maggie, startscenename);
 						app.SetGameEngineData(maggie, scene);
 						
+						BLI_strncpy(pathname, maggie->name, sizeof(pathname));
+						BLI_strncpy(G.sce, maggie->name, sizeof(G.sce));
+
 						if (firstTimeRunning)
 						{
+							setGamePythonPath(G.sce);
 							firstTimeRunning = false;
 
-							// set the filename only the first time as in KetsjiEmbedded
-							strcpy (pathname, maggie->name);
-							// also copy here (used by GameLogic.getBaseDirectory)
-							strcpy (G.sce, maggie->name);
-
 							if (fullScreen)
 							{
 #ifdef WIN32

Modified: trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2008-10-10 22:49:17 UTC (rev 17011)
+++ trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2008-10-11 00:56:49 UTC (rev 17012)
@@ -97,6 +97,7 @@
 static KX_Scene*	gp_KetsjiScene = NULL;
 static KX_KetsjiEngine*	gp_KetsjiEngine = NULL;
 static RAS_IRasterizer* gp_Rasterizer = NULL;
+static char gp_GamePythonPath[FILE_MAXDIR + FILE_MAXFILE] = "";
 
 void	KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,const MT_Vector3& color)
 {
@@ -155,7 +156,7 @@
 		return NULL;
 
 	BLI_strncpy(expanded, filename, FILE_MAXDIR + FILE_MAXFILE);
-	BLI_convertstringcode(expanded, G.sce);
+	BLI_convertstringcode(expanded, gp_GamePythonPath);
 	return PyString_FromString(expanded);
 }
 
@@ -281,7 +282,7 @@
 
 static PyObject* gPyGetBlendFileList(PyObject*, PyObject* args)
 {
-	char cpath[sizeof(G.sce)];
+	char cpath[sizeof(gp_GamePythonPath)];
 	char *searchpath = NULL;
 	PyObject* list, *value;
 	
@@ -295,10 +296,10 @@
 	
 	if (searchpath) {
 		BLI_strncpy(cpath, searchpath, FILE_MAXDIR + FILE_MAXFILE);
-		BLI_convertstringcode(cpath, G.sce);
+		BLI_convertstringcode(cpath, gp_GamePythonPath);
 	} else {
 		/* Get the dir only */
-		BLI_split_dirfile_basic(G.sce, cpath, NULL);
+		BLI_split_dirfile_basic(gp_GamePythonPath, cpath, NULL);
 	}
 	
     if((dp  = opendir(cpath)) == NULL) {
@@ -1541,9 +1542,10 @@
 
 void pathGamePythonConfig( char *path )
 {
-	int len = strlen(G.sce);
+	int len = strlen(gp_GamePythonPath);
 	
-	strncpy(path, G.sce, sizeof(G.sce));
+	BLI_strncpy(path, gp_GamePythonPath, sizeof(gp_GamePythonPath));
+
 	/* replace extension */
 	if (BLI_testextensie(path, ".blend")) {
 		strcpy(path+(len-6), ".bgeconf");
@@ -1551,3 +1553,9 @@
 		strcpy(path+len, ".bgeconf");
 	}
 }
+
+void setGamePythonPath(char *path)
+{
+	BLI_strncpy(gp_GamePythonPath, path, sizeof(gp_GamePythonPath));
+}
+

Modified: trunk/blender/source/gameengine/Ketsji/KX_PythonInit.h
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PythonInit.h	2008-10-10 22:49:17 UTC (rev 17011)
+++ trunk/blender/source/gameengine/Ketsji/KX_PythonInit.h	2008-10-11 00:56:49 UTC (rev 17012)
@@ -49,6 +49,7 @@
 PyObject*	initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level);
 void		exitGamePythonScripting();
 
+void		setGamePythonPath(char *path);
 void		pathGamePythonConfig( char *path );
 int			saveGamePythonConfig( char **marshal_buffer);
 int			loadGamePythonConfig(char *marshal_buffer, int marshal_length);





More information about the Bf-blender-cvs mailing list