Panagiotis Christopoulos Charitos před 15 roky
rodič
revize
c26f079313

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 1
build/debug/Makefile


+ 132 - 0
src/Core/AsyncLoader.cpp

@@ -1 +1,133 @@
+#include <fstream>
 #include "AsyncLoader.h"
+#include "Logger.h"
+
+
+//======================================================================================================================
+// start                                                                                                               =
+//======================================================================================================================
+void AsyncLoader::start()
+{
+	thread = boost::thread(&AsyncLoader::workingFunc, this);
+}
+
+
+//======================================================================================================================
+// loadInPreallocatedBuff                                                                                              =
+//======================================================================================================================
+void AsyncLoader::loadInPreallocatedBuff(const char* filename, void* buff, size_t size)
+{
+	//std::cout << "pushing " << filename << "..." << std::endl;
+	boost::mutex::scoped_lock lock(mutexIn);
+	File f = {filename, buff, size};
+	in.push_back(f);
+	lock.unlock();
+
+	condVar.notify_one();
+}
+
+
+//======================================================================================================================
+// loadInNewBuff                                                                                                       =
+//======================================================================================================================
+void AsyncLoader::loadInNewBuff(const char* filename)
+{
+	loadInPreallocatedBuff(filename, NULL, 0);
+}
+
+
+//======================================================================================================================
+// workingFunc                                                                                                         =
+//======================================================================================================================
+void AsyncLoader::workingFunc()
+{
+	while(1)
+	{
+		File f;
+
+		// Wait for something
+		{
+			boost::mutex::scoped_lock lock(mutexIn);
+			while(in.empty())
+			{
+				std::cout << "waiting..." << std::endl;
+				condVar.wait(lock);
+			}
+
+			f = in.front();
+			in.pop_front();
+		}
+
+		// Load the file
+		std::cout << "loading " << f.filename << "..." << std::endl;
+
+		std::ifstream is;
+		is.open(f.filename.c_str(), std::ios::binary);
+
+		if(!is.good())
+		{
+			std::cerr << "error opening" << f.filename << std::endl;
+			continue;
+		}
+
+		// Get size of file
+		is.seekg(0, std::ios::end);
+		size_t size = is.tellg();
+		is.seekg(0, std::ios::beg);
+
+		// Alloc (if needed)
+		if(f.data == NULL && f.size == 0)
+		{
+			f.size = size;
+			f.data = new char[f.size];
+			std::cout << "allocating " << f.size << " " << f.data << std::endl;
+		}
+		else if(f.size != size)
+		{
+			std::cerr << "error: size mismatch " << f.filename << std::endl;
+			is.close();
+			continue;
+		}
+
+		is.read((char*)f.data, f.size);
+
+		if(!is.good())
+		{
+			std::cerr << "error reading" << f.filename << std::endl;
+			is.close();
+			continue;
+		}
+
+		is.close();
+		std::cout << "" << f.filename << " loaded" << std::endl;
+
+		// Put the data in the out
+		{
+			boost::mutex::scoped_lock lock(mutexOut);
+			out.push_back(f);
+		}
+	} // end thread loop
+}
+
+
+//======================================================================================================================
+// getLoaded                                                                                                           =
+//======================================================================================================================
+bool AsyncLoader::getLoaded(std::string& filename, void*& buff, size_t& size)
+{
+	boost::mutex::scoped_lock lock(mutexOut);
+	if(out.empty())
+	{
+		return false;
+	}
+
+	File f = out.back();
+	out.pop_back();
+	lock.unlock();
+
+	filename = f.filename;
+	buff = f.data;
+	size = f.size;
+	return true;
+}
+

+ 37 - 5
src/Core/AsyncLoader.h

@@ -1,8 +1,9 @@
 #ifndef ASYNC_LOADER_H
 #define ASYNC_LOADER_H
 
-#include <queue>
+#include <list>
 #include <string>
+#include <vector>
 #include <boost/thread/thread.hpp>
 #include <boost/thread/mutex.hpp>
 
@@ -11,13 +12,44 @@
 class AsyncLoader
 {
 	public:
-		void loadThis(const char* filename);
+		/// Default constructor starts the thread
+		AsyncLoader() {start();}
+		
+		// Do nothing
+		~AsyncLoader() {}
+
+		/// Load a binary file and put the data in the preallocated buffer
+		void loadInPreallocatedBuff(const char* filename, void* buff, size_t size);
+
+		/// Load in a new buff
+		void loadInNewBuff(const char* filename);
+
+		/// Query the loader and see if its got something
+		/// @param[out] filename The file that finished loading
+		/// @param[out] buff The data are stored in this buffer
+		/// @param[out] size The buffer size
+		/// @return Return true if there is something that finished loading
+		bool getLoaded(std::string& filename, void*& buff, size_t& size);
 
 	private:
-		std::queue<std::string> in;
-		std::queue<std::string> out;
-		boost::mutex mutex;
+		struct File
+		{
+			std::string filename;
+			void* data;
+			size_t size;
+		};
+
+		std::list<File> in;
+		std::list<File> out;
+		boost::mutex mutexIn;
+		boost::mutex mutexOut;
 		boost::thread thread;
+		boost::condition_variable condVar;
+
+		void workingFunc(); ///< The thread function
+
+		/// Start thread
+		void start();
 };
 
 

+ 2 - 4
src/Renderer/Is.cpp

@@ -332,14 +332,12 @@ void Is::run()
 	calcPlanes();
 
 	// for all lights
-	BOOST_FOREACH(const PointLight* light,
-	              r.getCamera().getVisiblePointLights())
+	BOOST_FOREACH(const PointLight* light, r.getCamera().getVisiblePointLights())
 	{
 		pointLightPass(*light);
 	}
 	
-	BOOST_FOREACH(const SpotLight* light,
-	              r.getCamera().getVisibleSpotLights())
+	BOOST_FOREACH(const SpotLight* light, r.getCamera().getVisibleSpotLights())
 	{
 		spotLightPass(*light);
 	}

+ 25 - 4
src/Util/Exception.cpp

@@ -1,13 +1,34 @@
-#include <cstdio>
-#include <cstring>
 #include <boost/lexical_cast.hpp>
 #include "Exception.h"
 
 
+//======================================================================================================================
+// Constructor                                                                                                         =
+//======================================================================================================================
+Exception::Exception(const std::string& err_, const char* file_, int line_, const char* func_):
+	err(err_),
+	file(file_),
+	line(line_),
+	func(func_)
+{}
+
+
+//======================================================================================================================
+// Copy constructor                                                                                                    =
+//======================================================================================================================
+Exception::Exception(const Exception& e):
+	err(e.err),
+	file(e.file),
+	line(e.line),
+	func(e.func)
+{}
+
+
 //======================================================================================================================
 // init                                                                                                                =
 //======================================================================================================================
-void Exception::init(const char* err_, const char* file, int line, const char* func)
+const char* Exception::what() const throw()
 {
-	err = std::string("\n(") + file + ":" + boost::lexical_cast<std::string>(line) + " " + func + ") " + err_;
+	err = std::string("\n(") + file + ":" + boost::lexical_cast<std::string>(line) + " " + func + ") " + err;
+	return err.c_str();
 }

+ 11 - 18
src/Util/Exception.h

@@ -9,36 +9,29 @@
 class Exception: public std::exception
 {
 	public:
-		/// Constructor #1
-		Exception(const char* err_, const char* file, int line, const char* func) {init(err_, file, line, func);}
-
-		/// Constructor #2
-		Exception(const std::string& err_, const char* file, int line, const char* func);
+		/// Constructor
+		Exception(const std::string& err, const char* file = "unknown", int line = -1, const char* func = "unknown");
 
 		/// Copy constructor
-		Exception(const Exception& e): err(e.err) {}
+		Exception(const Exception& e);
 
 		/// Destructor. Do nothing
 		~Exception() throw() {}
 
 		/// Return the error code
-		const char* what() const throw() {return err.c_str();}
+		const char* what() const throw();
 
 	private:
-		std::string err;
-
-		/// Common construction code
-		void init(const char* err_, const char* file, int line, const char* func);
+		mutable std::string err;
+		const char* file;
+		int line;
+		const char* func;
 };
 
 
-inline Exception::Exception(const std::string& err_, const char* file, int line, const char* func)
-{
-	init(err_.c_str(), file, line, func);
-}
-
-
-// A few macros
+//======================================================================================================================
+// Macros                                                                                                              =
+//======================================================================================================================
 
 #define EXCEPTION(x) Exception(std::string() + x, __FILE__, __LINE__, __func__)
 

Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů