Sfoglia il codice sorgente

- Bug fixes
- Making a few classes singletons

Panagiotis Christopoulos Charitos 15 anni fa
parent
commit
a80b8a8f77

File diff suppressed because it is too large
+ 0 - 1
build/debug/Makefile


+ 10 - 11
src/Core/App.cpp

@@ -23,15 +23,16 @@ bool App::isCreated = false;
 //======================================================================================================================
 // handleMessageHanlderMsgs                                                                                            =
 //======================================================================================================================
-void App::handleMessageHanlderMsgs(const char* file, int line, const char* func, const std::string& msg)
+void App::handleMessageHanlderMsgs(const char* file, int line, const char* func, const char* msg)
 {
+	//printf("gooooooooot.....\n");
 	if(boost::find_first(msg, "Warning") || boost::find_first(msg, "Error"))
 	{
-		std::cerr << "(" << file << ":" << line << " "<< func << ") " << msg << std::endl;
+		std::cerr << "(" << file << ":" << line << " "<< func << ") " << msg << std::flush;
 	}
 	else
 	{
-		std::cout << "(" << file << ":" << line << " "<< func << ") " << msg << std::endl;
+		std::cout << "(" << file << ":" << line << " "<< func << ") " << msg << std::flush;
 	}
 }
 
@@ -106,19 +107,18 @@ App::App(int argc, char* argv[], Object* parent):
 	boost::filesystem::create_directory(cachePath);
 
 	// create the subsystems. WATCH THE ORDER
-	scriptingEngine = new ScriptingEngine(this);
-	scriptingEngine->exposeVar("app", this);
-	/// @todo
+	ScriptingEngine::getInstance().exposeVar("app", this);
 	const char* commonPythonCode =
 	"import sys\n"
 	"from Anki import *\n"
 	"\n"
 	"class StdoutCatcher:\n"
 	"\tdef write(self, str_):\n"
+	"\t\tif str_ == \"\\n\": return\n"
 	"\t\tline = sys._getframe(1).f_lineno\n"
 	"\t\tfile = sys._getframe(1).f_code.co_filename\n"
 	"\t\tfunc = sys._getframe(1).f_code.co_name\n"
-	"\t\tLogger.getInstance().write(file, line, func, str_)\n"
+	"\t\tLogger.getInstance().write(file, line, func, str_ + \"\\n\")\n"
 	"\n"
 	"class StderrCatcher:\n"
 	"\tdef write(self, str_):\n"
@@ -128,14 +128,13 @@ App::App(int argc, char* argv[], Object* parent):
 	"\t\tLogger.getInstance().write(file, line, func, str_)\n"
 	"\n"
 	"sys.stdout = StdoutCatcher()\n"
-	"sys.stderr = StderrCatcher()\n";
-	scriptingEngine->execScript(commonPythonCode);
+	"#sys.stderr = StderrCatcher()\n";
+	ScriptingEngine::getInstance().execScript(commonPythonCode);
 
 	mainRenderer = new MainRenderer(this);
 	scene = new Scene(this);
 	stdinListener = new StdinListener(this);
 	stdinListener->start();
-	input = new Input(this);
 
 	// other
 	activeCam = NULL;
@@ -336,7 +335,7 @@ void App::execStdinScpripts()
 
 		try
 		{
-			app->getScriptingEngine().execScript(cmd.c_str(), "command line input");
+			ScriptingEngine::getInstance().execScript(cmd.c_str(), "command line input");
 		}
 		catch(Exception& e)
 		{

+ 1 - 20
src/Core/App.h

@@ -9,7 +9,6 @@
 #include "Exception.h"
 
 
-class ScriptingEngine;
 class StdinListener;
 class Scene;
 class MainRenderer;
@@ -54,12 +53,10 @@ class App: public Object
 		/// @{
 		bool isTerminalColoringEnabled() const;
 		Scene& getScene();
-		ScriptingEngine& getScriptingEngine();
 		StdinListener& getStdinLintener();
 		MainRenderer& getMainRenderer();
 		Camera* getActiveCam() {return activeCam;}
 		void setActiveCam(Camera* cam) {activeCam = cam;}
-		Input& getInput();
 		/// @}
 
 		/// @return Returns the number of milliseconds since SDL library initialization
@@ -78,16 +75,14 @@ class App: public Object
 		/// @name Pointers to serious subsystems
 		/// @{
 		Scene* scene;
-		ScriptingEngine* scriptingEngine;
 		MainRenderer* mainRenderer;
 		StdinListener* stdinListener;
-		Input* input;
 		/// @}
 
 		void parseCommandLineArgs(int argc, char* argv[]);
 
 		/// A slot to handle the messageHandler's signal
-		void handleMessageHanlderMsgs(const char* file, int line, const char* func, const std::string& msg);
+		void handleMessageHanlderMsgs(const char* file, int line, const char* func, const char* msg);
 };
 
 
@@ -104,13 +99,6 @@ inline Scene& App::getScene()
 }
 
 
-inline ScriptingEngine& App::getScriptingEngine()
-{
-	RASSERT_THROW_EXCEPTION(scriptingEngine == NULL);
-	return *scriptingEngine;
-}
-
-
 inline StdinListener& App::getStdinLintener()
 {
 	RASSERT_THROW_EXCEPTION(stdinListener == NULL);
@@ -125,11 +113,4 @@ inline MainRenderer& App::getMainRenderer()
 }
 
 
-inline Input& App::getInput()
-{
-	RASSERT_THROW_EXCEPTION(input == NULL);
-	return *input;
-}
-
-
 #endif

+ 5 - 0
src/Core/Logger.cpp

@@ -35,6 +35,11 @@ Logger& Logger::operator<<(Logger& (*funcPtr)(Logger&))
 		append("\n", 1);
 		flush();
 	}
+	else if(funcPtr == ::flush)
+	{
+		flush();
+	}
+
 	return *this;
 }
 

+ 7 - 0
src/Core/Logger.h

@@ -118,6 +118,13 @@ inline Logger& endl(Logger& logger)
 }
 
 
+/// Flush the Logger
+inline Logger& flush(Logger& logger)
+{
+	return logger;
+}
+
+
 /// Record the sender
 struct LoggerSender
 {

+ 13 - 13
src/Input/Input.cpp

@@ -1,20 +1,22 @@
 #include "Input.h"
 #include <SDL/SDL.h>
 #include "App.h"
+#include "Logger.h"
+
+
+Input* Input::instance = NULL;
 
 
 //======================================================================================================================
-// Constructor                                                                                                         =
+// init                                                                                                                =
 //======================================================================================================================
-Input::Input(Object* parent):
-	Object(parent),
-	keys(SDL_NUM_SCANCODES, 0),
-	mouseBtns(8, 0),
-	warpMouse(false),
-	hideCursor(true),
-	parentApp(parent)
+void Input::init()
 {
+	INFO("Initializing input...");
+	warpMouse = false;
+	hideCursor = true;
 	reset();
+	INFO("Input initialized");
 }
 
 
@@ -23,10 +25,8 @@ Input::Input(Object* parent):
 //======================================================================================================================
 void Input::reset(void)
 {
-	RASSERT_THROW_EXCEPTION(keys.size() < 1);
-	RASSERT_THROW_EXCEPTION(mouseBtns.size() < 1);
-	memset(&keys[0], 0, keys.size()*sizeof(short));
-	memset(&mouseBtns[0], 0, mouseBtns.size()*sizeof(short));
+	memset(&keys[0], 0, keys.size() * sizeof(short));
+	memset(&mouseBtns[0], 0, mouseBtns.size() * sizeof(short));
 	mousePosNdc = Vec2(0.0);
 	mouseVelocity = Vec2(0.0);
 }
@@ -58,7 +58,7 @@ void Input::handleEvents()
 
 
 	mouseVelocity = Vec2(0.0);
-	App* app_ = static_cast<App*>(parentApp);
+	App* app_ = app;
 
 	SDL_Event event_;
 	while(SDL_PollEvent(&event_))

+ 26 - 7
src/Input/Input.h

@@ -2,20 +2,20 @@
 #define INPUT_H
 
 #include <SDL/SDL_scancode.h>
-#include "Vec.h"
+#include <boost/array.hpp>
 #include "Math.h"
-#include "Object.h"
 
 
 /// Handle the SDL input
-class Input: public Object
+class Input
 {
 	public:
-		Input(Object* parent);
+		/// Singleton stuff
+		static Input& getInstance();
 
 		// keys and btns
-		Vec<short> keys;  ///< Shows the current key state. 0: unpressed, 1: pressed once, n is >1: kept pressed 'n' times continuously
-		Vec<short> mouseBtns; ///< Mouse btns. Supporting 3 btns & wheel. @see keys
+		boost::array<short, SDL_NUM_SCANCODES> keys;  ///< Shows the current key state. 0: unpressed, 1: pressed once, n is >1: kept pressed 'n' times continuously
+		boost::array<short, 8> mouseBtns; ///< Mouse btns. Supporting 3 btns & wheel. @see keys
 
 		void reset();
 		void handleEvents();
@@ -28,8 +28,27 @@ class Input: public Object
 		bool hideCursor;
 
 	private:
-		Object* parentApp; ///< Hold the parrent here cause we use him
+		static Input* instance;
+
+		/// @name Ensure its singleton
+		/// @{
+		Input() {init();}
+		Input(const Input&) {init();}
+		void operator=(const Input&) {}
+		/// @}
+
+		void init();
 };
 
 
+inline Input& Input::getInstance()
+{
+	if(instance == NULL)
+	{
+		instance = new Input();
+	}
+	return *instance;
+}
+
+
 #endif

+ 35 - 35
src/Main.cpp

@@ -264,7 +264,7 @@ void mainLoop()
 	do
 	{
 		float crntTime = App::getTicks() / 1000.0;
-		app->getInput().handleEvents();
+		Input::getInstance().handleEvents();
 
 		float dist = 0.2;
 		float ang = toRad(3.0);
@@ -273,45 +273,45 @@ void mainLoop()
 		// move the camera
 		static SceneNode* mover = app->getActiveCam();
 
-		if(app->getInput().keys[SDL_SCANCODE_1]) mover = app->getActiveCam();
-		if(app->getInput().keys[SDL_SCANCODE_2]) mover = point_lights[0];
-		if(app->getInput().keys[SDL_SCANCODE_3]) mover = spot_lights[0];
-		if(app->getInput().keys[SDL_SCANCODE_4]) mover = point_lights[1];
-		if(app->getInput().keys[SDL_SCANCODE_5]) mover = spot_lights[1];
-		if(app->getInput().keys[SDL_SCANCODE_6]) mover = partEmitter;
-		if(app->getInput().keys[SDL_SCANCODE_M] == 1) app->getInput().warpMouse = !app->getInput().warpMouse;
-
-		if(app->getInput().keys[SDL_SCANCODE_A]) mover->moveLocalX(-dist);
-		if(app->getInput().keys[SDL_SCANCODE_D]) mover->moveLocalX(dist);
-		if(app->getInput().keys[SDL_SCANCODE_LSHIFT]) mover->moveLocalY(dist);
-		if(app->getInput().keys[SDL_SCANCODE_SPACE]) mover->moveLocalY(-dist);
-		if(app->getInput().keys[SDL_SCANCODE_W]) mover->moveLocalZ(-dist);
-		if(app->getInput().keys[SDL_SCANCODE_S]) mover->moveLocalZ(dist);
-		if(!app->getInput().warpMouse)
+		if(Input::getInstance().keys[SDL_SCANCODE_1]) mover = app->getActiveCam();
+		if(Input::getInstance().keys[SDL_SCANCODE_2]) mover = point_lights[0];
+		if(Input::getInstance().keys[SDL_SCANCODE_3]) mover = spot_lights[0];
+		if(Input::getInstance().keys[SDL_SCANCODE_4]) mover = point_lights[1];
+		if(Input::getInstance().keys[SDL_SCANCODE_5]) mover = spot_lights[1];
+		if(Input::getInstance().keys[SDL_SCANCODE_6]) mover = partEmitter;
+		if(Input::getInstance().keys[SDL_SCANCODE_M] == 1) Input::getInstance().warpMouse = !Input::getInstance().warpMouse;
+
+		if(Input::getInstance().keys[SDL_SCANCODE_A]) mover->moveLocalX(-dist);
+		if(Input::getInstance().keys[SDL_SCANCODE_D]) mover->moveLocalX(dist);
+		if(Input::getInstance().keys[SDL_SCANCODE_LSHIFT]) mover->moveLocalY(dist);
+		if(Input::getInstance().keys[SDL_SCANCODE_SPACE]) mover->moveLocalY(-dist);
+		if(Input::getInstance().keys[SDL_SCANCODE_W]) mover->moveLocalZ(-dist);
+		if(Input::getInstance().keys[SDL_SCANCODE_S]) mover->moveLocalZ(dist);
+		if(!Input::getInstance().warpMouse)
 		{
-			if(app->getInput().keys[SDL_SCANCODE_UP]) mover->rotateLocalX(ang);
-			if(app->getInput().keys[SDL_SCANCODE_DOWN]) mover->rotateLocalX(-ang);
-			if(app->getInput().keys[SDL_SCANCODE_LEFT]) mover->rotateLocalY(ang);
-			if(app->getInput().keys[SDL_SCANCODE_RIGHT]) mover->rotateLocalY(-ang);
+			if(Input::getInstance().keys[SDL_SCANCODE_UP]) mover->rotateLocalX(ang);
+			if(Input::getInstance().keys[SDL_SCANCODE_DOWN]) mover->rotateLocalX(-ang);
+			if(Input::getInstance().keys[SDL_SCANCODE_LEFT]) mover->rotateLocalY(ang);
+			if(Input::getInstance().keys[SDL_SCANCODE_RIGHT]) mover->rotateLocalY(-ang);
 		}
 		else
 		{
 			float accel = 44.0;
-			mover->rotateLocalX(ang * app->getInput().mouseVelocity.y * accel);
-			mover->rotateLocalY(-ang * app->getInput().mouseVelocity.x * accel);
+			mover->rotateLocalX(ang * Input::getInstance().mouseVelocity.y * accel);
+			mover->rotateLocalY(-ang * Input::getInstance().mouseVelocity.x * accel);
 		}
-		if(app->getInput().keys[SDL_SCANCODE_Q]) mover->rotateLocalZ(ang);
-		if(app->getInput().keys[SDL_SCANCODE_E]) mover->rotateLocalZ(-ang);
-		if(app->getInput().keys[SDL_SCANCODE_PAGEUP]) mover->getLocalTransform().scale += scale ;
-		if(app->getInput().keys[SDL_SCANCODE_PAGEDOWN]) mover->getLocalTransform().scale -= scale ;
+		if(Input::getInstance().keys[SDL_SCANCODE_Q]) mover->rotateLocalZ(ang);
+		if(Input::getInstance().keys[SDL_SCANCODE_E]) mover->rotateLocalZ(-ang);
+		if(Input::getInstance().keys[SDL_SCANCODE_PAGEUP]) mover->getLocalTransform().scale += scale ;
+		if(Input::getInstance().keys[SDL_SCANCODE_PAGEDOWN]) mover->getLocalTransform().scale -= scale ;
 
-		if(app->getInput().keys[SDL_SCANCODE_K]) app->getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().origin);
+		if(Input::getInstance().keys[SDL_SCANCODE_K]) app->getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().origin);
 
-		if(app->getInput().keys[SDL_SCANCODE_I])
+		if(Input::getInstance().keys[SDL_SCANCODE_I])
 			character->moveForward(0.1);
 
 
-		if(app->getInput().keys[SDL_SCANCODE_O] == 1)
+		if(Input::getInstance().keys[SDL_SCANCODE_O] == 1)
 		{
 			btRigidBody* body = static_cast<btRigidBody*>(boxes[0]);
 			//body->getMotionState()->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
@@ -320,11 +320,11 @@ void mainLoop()
 			body->forceActivationState(ACTIVE_TAG);
 		}
 
-		if(app->getInput().keys[SDL_SCANCODE_Y] == 1)
+		if(Input::getInstance().keys[SDL_SCANCODE_Y] == 1)
 		{
 			INFO("Exec script");
-			app->getScriptingEngine().exposeVar("app", app);
-			app->getScriptingEngine().execScript(Util::readFile("test.py").c_str());
+			ScriptingEngine::getInstance().exposeVar("app", app);
+			ScriptingEngine::getInstance().execScript(Util::readFile("test.py").c_str());
 		}
 
 		mover->getLocalTransform().rotation.reorthogonalize();
@@ -347,11 +347,11 @@ void mainLoop()
 		/*Ui::printf("Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translationWspace.x, mover->translationWspace.y, mover->translationWspace.z,
 								 toDegrees(Euler(mover->rotationWspace).x), toDegrees(Euler(mover->rotationWspace).y), toDegrees(Euler(mover->rotationWspace).z));*/
 
-		if(app->getInput().keys[SDL_SCANCODE_ESCAPE])
+		if(Input::getInstance().keys[SDL_SCANCODE_ESCAPE])
 			break;
-		if(app->getInput().keys[SDL_SCANCODE_F11])
+		if(Input::getInstance().keys[SDL_SCANCODE_F11])
 			app->togleFullScreen();
-		if(app->getInput().keys[SDL_SCANCODE_F12] == 1)
+		if(Input::getInstance().keys[SDL_SCANCODE_F12] == 1)
 			app->getMainRenderer().takeScreenshot("gfx/screenshot.jpg");
 
 		/*char str[128];

+ 10 - 8
src/Scripting/BoostPythonInterfaces.cpp

@@ -2,7 +2,7 @@
 #include "App.h"
 
 
-#define CALL_WRAP(x) extern void boostPythonWrap##x(); boostPythonWrap##x();
+#define CALL_WRAP(x) extern void boostPythonWrap##x(); boostPythonWrap##x()
 
 
 BOOST_PYTHON_MODULE(Anki)
@@ -11,13 +11,15 @@ BOOST_PYTHON_MODULE(Anki)
 	CALL_WRAP(Vec3);
 	CALL_WRAP(Vec4);
 
-	CALL_WRAP(Scene)
+	CALL_WRAP(Logger);
 
-	CALL_WRAP(Hdr)
-	CALL_WRAP(Pps)
-	CALL_WRAP(Renderer)
-	CALL_WRAP(Dbg)
-	CALL_WRAP(MainRenderer)
+	CALL_WRAP(Scene);
 
-	CALL_WRAP(App)
+	CALL_WRAP(Hdr);
+	CALL_WRAP(Pps);
+	CALL_WRAP(Renderer);
+	CALL_WRAP(Dbg);
+	CALL_WRAP(MainRenderer);
+
+	CALL_WRAP(App);
 }

+ 6 - 4
src/Scripting/ScriptingEngine.cpp

@@ -1,17 +1,21 @@
 #include <Python.h>
 #include "ScriptingEngine.h"
 #include "Exception.h"
+#include "Logger.h"
 
 
 extern "C" void initAnki(); /// Defined in BoostPythonInterfaces.cpp
 
 
+ScriptingEngine* ScriptingEngine::instance = NULL;
+
+
 //======================================================================================================================
 // init                                                                                                                =
 //======================================================================================================================
 void ScriptingEngine::init()
 {
-	//INFO("Initializing scripting engine...");
+	INFO("Initializing scripting engine...");
 
 	PyImport_AppendInittab((char*)("Anki"), &initAnki);
 	Py_Initialize();
@@ -19,9 +23,7 @@ void ScriptingEngine::init()
 	mainNamespace = mainModule.attr("__dict__");
 	ankiModule = boost::python::object(boost::python::handle<>(PyImport_ImportModule("Anki")));
 
-	//execScript("import Anki\n");
-
-	//INFO("Scripting engine initialized");
+	INFO("Scripting engine initialized");
 }
 
 

+ 17 - 10
src/Scripting/ScriptingEngine.h

@@ -2,18 +2,14 @@
 #define SCRIPTING_ENGINE_H
 
 #include <boost/python.hpp>
-#include "Object.h"
 
 
 /// The scripting engine using Python
-class ScriptingEngine: public Object
+class ScriptingEngine
 {
 	public:
-		/// Default constructor
-		ScriptingEngine(Object* parent = NULL);
-
-		/// Destructor
-		~ScriptingEngine() {}
+		/// Singleton stuff
+		static ScriptingEngine& getInstance();
 
 		/// Execute python script
 		/// @param script Script source
@@ -27,19 +23,30 @@ class ScriptingEngine: public Object
 		void exposeVar(const char* varName, Type* var);
 
 	private:
+		static ScriptingEngine* instance;
 		boost::python::object mainModule;
 		boost::python::object ankiModule;
 		boost::python::object mainNamespace;
 
+		/// @name Ensure its singleton
+		/// @{
+		ScriptingEngine() {init();}
+		ScriptingEngine(const ScriptingEngine&) {init();}
+		void operator=(const ScriptingEngine&) {}
+		/// @}
+
 		/// Init python and modules
 		void init();
 };
 
 
-inline ScriptingEngine::ScriptingEngine(Object* parent):
-	Object(parent)
+inline ScriptingEngine& ScriptingEngine::getInstance()
 {
-	init();
+	if(instance == NULL)
+	{
+		instance = new ScriptingEngine();
+	}
+	return *instance;
 }
 
 

Some files were not shown because too many files changed in this diff