From 4400d8dd2d8c8910034f5f7f047688ca8a0fac54 Mon Sep 17 00:00:00 2001 From: ponchio Date: Fri, 9 Dec 2011 16:06:08 +0000 Subject: [PATCH] added multithreading --- wrap/gcache/cache.h | 12 +- wrap/gcache/controller.h | 4 +- wrap/gl/shaders.h | 1212 ++++++++--------- wrap/gui/cursorshapearea.cpp | 20 + wrap/gui/qglnxsscenehandler.cpp | 1 + wrap/gui/qgraphicsmatrix3d.cpp | 2 + wrap/system/multithreading/atomic_int.h | 16 + wrap/system/multithreading/atomic_int_apple.h | 118 ++ wrap/system/multithreading/base.h | 18 + wrap/system/multithreading/condition.h | 53 + wrap/system/multithreading/mt.h | 23 + wrap/system/multithreading/mutex.h | 51 + wrap/system/multithreading/rw_lock.h | 57 + .../system/multithreading/scoped_mutex_lock.h | 80 ++ wrap/system/multithreading/scoped_read_lock.h | 35 + .../system/multithreading/scoped_write_lock.h | 35 + wrap/system/multithreading/semaphore.h | 72 + wrap/system/multithreading/thread.h | 111 ++ wrap/system/multithreading/util.h | 41 + wrap/system/qgetopt.cpp | 2 +- wrap/system/time/chronometer.cpp | 175 +++ wrap/system/time/chronometer.h | 76 ++ wrap/system/time/clock.cpp | 77 ++ wrap/system/time/clock.h | 43 + 24 files changed, 1719 insertions(+), 615 deletions(-) create mode 100644 wrap/gui/cursorshapearea.cpp create mode 100644 wrap/gui/qglnxsscenehandler.cpp create mode 100644 wrap/gui/qgraphicsmatrix3d.cpp create mode 100755 wrap/system/multithreading/atomic_int.h create mode 100755 wrap/system/multithreading/atomic_int_apple.h create mode 100755 wrap/system/multithreading/base.h create mode 100755 wrap/system/multithreading/condition.h create mode 100755 wrap/system/multithreading/mt.h create mode 100755 wrap/system/multithreading/mutex.h create mode 100755 wrap/system/multithreading/rw_lock.h create mode 100755 wrap/system/multithreading/scoped_mutex_lock.h create mode 100755 wrap/system/multithreading/scoped_read_lock.h create mode 100755 wrap/system/multithreading/scoped_write_lock.h create mode 100755 wrap/system/multithreading/semaphore.h create mode 100755 wrap/system/multithreading/thread.h create mode 100755 wrap/system/multithreading/util.h create mode 100644 wrap/system/time/chronometer.cpp create mode 100644 wrap/system/time/chronometer.h create mode 100644 wrap/system/time/clock.cpp create mode 100644 wrap/system/time/clock.h diff --git a/wrap/gcache/cache.h b/wrap/gcache/cache.h index 7b752c8f..f4426195 100644 --- a/wrap/gcache/cache.h +++ b/wrap/gcache/cache.h @@ -31,8 +31,8 @@ public: //if true the cache will exit at the first opportunity bool quit; ///keeps track of changes (if 1 then something was loaded or dropped - QAtomicInt changed; - ///callback for changed + QAtomicInt new_data; + ///callback for new_data void (*callback)(void *data); ///data is fetched from here @@ -49,7 +49,7 @@ protected: public: Cache(uint64_t _capacity = INT_MAX): - final(false), quit(false), changed(false), input(NULL), s_max(_capacity), s_curr(0) {} + final(false), quit(false), new_data(false), input(NULL), s_max(_capacity), s_curr(0) {} virtual ~Cache() {} void setInputCache(Provider *p) { input = p; } @@ -58,8 +58,8 @@ public: void setCapacity(uint64_t c) { s_max = c; } ///return true if the cache is waiting for priority to change - bool isChanged() { - bool r = changed.testAndSetOrdered(1, 0); //if changed is 1, r is true + bool newData() { + bool r = new_data.testAndSetOrdered(1, 0); //if changed is 1, r is true return r; } @@ -145,7 +145,7 @@ protected: if(this->quit) break; if(unload() || load()) { - changed.testAndSetOrdered(0, 1); //if not changed, set as changed + new_data.testAndSetOrdered(0, 1); //if not changed, set as changed input->check_queue.open(); //we signal ourselves to check again } input->check_queue.leave(); diff --git a/wrap/gcache/controller.h b/wrap/gcache/controller.h index 8186a1be..f3a2e60f 100644 --- a/wrap/gcache/controller.h +++ b/wrap/gcache/controller.h @@ -160,10 +160,10 @@ class Controller { provider.heap.clear(); } - bool isChanged() { + bool newData() { bool c = false; for(int i = (int)caches.size() -1; i >= 0; i--) { - c |= caches[i]->isChanged(); + c |= caches[i]->newData(); } return c; } diff --git a/wrap/gl/shaders.h b/wrap/gl/shaders.h index cbd92441..4423ee1b 100644 --- a/wrap/gl/shaders.h +++ b/wrap/gl/shaders.h @@ -19,609 +19,609 @@ * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * for more details. * * * -****************************************************************************/ - -#ifndef __SHADERS_H__ -#define __SHADERS_H__ - -#include -#include -#include - -#include "gl_object.h" -#include "../../vcg/space/point2.h" -#include "../../vcg/space/point3.h" -#include "../../vcg/space/point4.h" - -class Shader : public GLObject, public Bindable -{ -public: - typedef enum - { - VERTEX, - FRAGMENT, - GEOMETRY - } ShaderType; - - Shader(void) : GLObject(), Bindable() - { - this->flags = 0; - this->flags |= SOURCE_DIRTY; - this->compiled = false; - } - - void Gen(void) - { - this->Del(); - GLenum t; - switch (this->Type()) - { - case Shader::VERTEX : t = GL_VERTEX_SHADER; break; - case Shader::FRAGMENT : t = GL_FRAGMENT_SHADER; break; - case Shader::GEOMETRY : t = GL_GEOMETRY_SHADER_EXT; break; - default: return; - }; - this->objectID = glCreateShader(t); - } - - void Del(void) - { - if (this->objectID == 0) return; - glDeleteShader(this->objectID); - this->objectID = 0; - } - - virtual ShaderType Type(void) const = 0; - - void SetSource(const char * src) - { - if (this->objectID==0) - Gen(); - - this->flags |= SOURCE_DIRTY; - this->compiled = false; - this->source = src; - - const char * pSrc = this->source.c_str(); - glShaderSource(this->objectID, 1, &pSrc, 0); - } - - bool LoadSource(const char * fileName) - { - if (this->objectID==0) - Gen(); - - this->flags |= SOURCE_DIRTY; - this->compiled = false; - FILE * f = fopen(fileName, "rb"); - if (f == 0) - { - this->source = ""; - return false; - } - fseek(f, 0, SEEK_END); - const size_t sz = (size_t)ftell(f); - rewind(f); - char * buff = new char[sz + 1]; - fread(buff, sizeof(char), sz, f); - fclose(f); - buff[sz] = '\0'; - - this->source = buff; - delete [] buff; - - const char * pSrc = this->source.c_str(); - glShaderSource(this->objectID, 1, &pSrc, 0); - - return true; - } - - bool Compile(void) - { - glCompileShader(this->objectID); - GLint cm = 0; - glGetShaderiv(this->objectID, GL_COMPILE_STATUS, &cm); - this->compiled = (cm != GL_FALSE); - this->flags = 0; - return this->compiled; - } - - bool IsCompiled(void) - { - return this->compiled; - } - - std::string InfoLog(void) - { - GLint len = 0; - glGetShaderiv(this->objectID, GL_INFO_LOG_LENGTH, &len); - char * ch = new char[len + 1]; - glGetShaderInfoLog(this->objectID, len, &len, ch); - std::string infoLog = ch; - delete [] ch; - return infoLog; - } - -protected: - enum - { - SOURCE_DIRTY - }; - - std::string source; - unsigned int flags; - bool compiled; - - void DoBind(void) - { - } - - void DoUnbind(void) - { - } -}; - -class VertexShader : public Shader -{ -public: - VertexShader(void) : Shader() - { - } - - ShaderType Type(void) const - { - return Shader::VERTEX; - } -}; - -class FragmentShader : public Shader -{ -public: - FragmentShader(void) : Shader() - { - } - - ShaderType Type(void) const - { - return Shader::FRAGMENT; - } -}; - -class GeometryShader : public Shader -{ -public: - GeometryShader(void) : Shader() - { - } - - ShaderType Type(void) const - { - return Shader::GEOMETRY; - } -}; - -#if 0 -class Program; - -class Uniform -{ -friend class Program; - -public: - /* - typedef enum - { - U_BOOL, - U_BVEC2, - U_BVEC3, - U_BVEC4, - U_BMAT2, - U_BMAT3, - U_BMAT4, - - U_INT, - U_IVEC2, - U_IVEC3, - U_IVEC4, - U_IMAT2, - U_IMAT3, - U_IMAT4, - - U_FLOAT, - U_FVEC2, - U_FVEC3, - U_FVEC4, - U_FMAT2, - U_FMAT3, - U_FMAT4, - - U_SAMPLER1D, - U_SAMPLER2D, - U_SAMPLER3D, - U_SAMPLERCUBE, - U_SAMPLER1DSHADOW, - U_SAMPLER2DSHADOW - } UniformType; - */ - - const std::string & Name(void) const - { - return this->name; - } - - virtual GLenum Type(void) const = 0; - -protected: - Program * prog; - GLint location; - std::string name; - - Uniform(Program * prog, GLint location, const std::string & name) - { - this->prog = prog; - this->location = location; - this->name = name; - } - - virtual void Apply(void) = 0; -}; - -class Uniform1b : public Uniform; -{ -public: - - void SetValue(GLboolean x) - { - this->value[0] = x; - } - - GLboolean GetValue(void) const - { - return this->value[0]; - } - -protected: - Program * prog; - GLboolean value[1]; - - Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) - { - this->value = GL_FALSE; - } -}; - -class Uniform2b : public Uniform; -{ -public: - - void SetValue(GLboolean x, GLboolean y) - { - this->value[0] = x; - this->value[1] = y; - } - - GLboolean GetValueX(void) const - { - return this->value[0]; - } - - GLboolean GetValueY(void) const - { - return this->value[1]; - } - -protected: - Program * prog; - GLboolean value[2]; - - Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) - { - this->value[0] = GL_FALSE; - this->value[1] = GL_FALSE; - } -}; - -class Uniform3b : public Uniform; -{ -public: - - void SetValue(GLboolean x, GLboolean y, GLboolean z) - { - this->value[0] = x; - this->value[1] = y; - this->value[2] = z; - } - - GLboolean GetValueX(void) const - { - return this->value[0]; - } - - GLboolean GetValueY(void) const - { - return this->value[1]; - } - - GLboolean GetValueZ(void) const - { - return this->value[2]; - } - -protected: - Program * prog; - GLboolean value[2]; - - Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) - { - this->value[0] = GL_FALSE; - this->value[1] = GL_FALSE; - } -}; - -class Uniform1i : public Uniform; -{ -public: - - void SetValue(GLint v) - { - this->value = v; - } - - GLint GetValue(void) const - { - return this->value; - } - -protected: - Program * prog; - GLint value; - - Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) - { - this->value = 0; - } -}; -#endif - -class Program : public GLObject, public Bindable -{ -public: - - Program(void) - { - this->linked = false; - } - - void Gen(void) - { - this->Del(); - this->objectID = glCreateProgram(); - } - - void Del(void) - { - if (this->objectID == 0) return; - glDeleteProgram(this->objectID); - this->objectID = 0; - } - - void Attach(Shader * shd) - { - if (this->objectID==0) - Gen(); - this->shaders.insert(shd); - this->linked = false; - glAttachShader(this->objectID, shd->ObjectID()); - } - - void Detach(Shader * shd) - { - this->shaders.erase(shd); - this->linked = false; - glDetachShader(this->objectID, shd->ObjectID()); - } - - GLsizei AttachedShaders(void) const - { - return ((GLsizei)(this->shaders.size())); - } - - Shader * AttachedShader(int i) - { - Shader * shd = 0; - int cnt = 0; - for (std::set::iterator it=this->shaders.begin(); (cnt < i) && (it!=this->shaders.end()); ++it) - { - shd = (*it); - } - return shd; - } - - bool Link(void) - { - bool ok = true; - for (std::set::iterator it=this->shaders.begin(); it!=this->shaders.end(); ++it) - { - Shader * shd = (*it); - if (!shd->IsCompiled()) - { - ok = shd->Compile() && ok; - } - } - - if (!ok) - return false; - - glLinkProgram(this->objectID); - - GLint cm = 0; - glGetProgramiv(this->objectID, GL_LINK_STATUS, &cm); - this->linked = (cm != GL_FALSE); - - return this->linked; - } - - bool IsLinked(void) const - { - return this->linked; - } - - std::string InfoLog(void) - { - GLint len = 0; - glGetProgramiv(this->objectID, GL_INFO_LOG_LENGTH, &len); - char * ch = new char[len + 1]; - glGetProgramInfoLog(this->objectID, len, &len, ch); - std::string infoLog = ch; - delete [] ch; - return infoLog; - } - - void Uniform(const char * name, GLint x) - { - const GLint loc = glGetUniformLocation(this->objectID, name); - glUniform1i(loc, x); - } - - void Uniform(const char * name, GLint x, GLint y) - { - const GLint loc = glGetUniformLocation(this->objectID, name); - glUniform2i(loc, x, y); - } - - void Uniform(const char * name, GLint x, GLint y, GLint z) - { - const GLint loc = glGetUniformLocation(this->objectID, name); - glUniform3i(loc, x, y, z); - } - - void Uniform(const char * name, GLint x, GLint y, GLint z, GLint w) - { - const GLint loc = glGetUniformLocation(this->objectID, name); - glUniform4i(loc, x, y, z, w); - } - - void Uniform(const char * name, GLfloat x) - { - const GLint loc = glGetUniformLocation(this->objectID, name); - glUniform1f(loc, x); - } - - void Uniform(const char * name, GLfloat x, GLfloat y) - { - const GLint loc = glGetUniformLocation(this->objectID, name); - glUniform2f(loc, x, y); - } - - void Uniform(const char * name, GLfloat x, GLfloat y, GLfloat z) - { - const GLint loc = glGetUniformLocation(this->objectID, name); - glUniform3f(loc, x, y, z); - } - - void Uniform(const char * name, GLfloat x, GLfloat y, GLfloat z, GLfloat w) - { - const GLint loc = glGetUniformLocation(this->objectID, name); - glUniform4f(loc, x, y, z, w); - } - - void Uniform(const char * name, const vcg::Point2f& p) - { - const GLint loc = glGetUniformLocation(this->objectID, name); - glUniform2fv(loc, 1, p.V()); - } - - void Uniform(const char * name, const vcg::Point3f& p) - { - const GLint loc = glGetUniformLocation(this->objectID, name); - glUniform3fv(loc, 1, p.V()); - } - - void Uniform(const char * name, const vcg::Point4f& p) - { - const GLint loc = glGetUniformLocation(this->objectID, name); - glUniform4fv(loc, 1, p.V()); - } - - void Parameter(GLenum pname, int value) - { - glProgramParameteriEXT(this->objectID, pname, value); - } - - void Attribute(int index, GLfloat x, GLfloat y, GLfloat z, GLfloat w) - { - glVertexAttrib4f(index, x, y, z, w); - } - - void BindAttribute(int index, const char * name) - { - glBindAttribLocation(this->objectID, index, name); - } - -protected: - std::set shaders; - bool linked; - - void DoBind(void) - { - if (!this->IsLinked()) - { - this->Link(); - } - glUseProgram(this->objectID); - } - - void DoUnbind(void) - { - glUseProgram(0); - } -}; - -class ProgramVF : public Bindable -{ -public: - Program prog; - VertexShader vshd; - FragmentShader fshd; - - ProgramVF(void) : Bindable() - { - } - - void SetSources(const char * vsrc, const char * fsrc) - { - if (vsrc) { - this->vshd.SetSource(vsrc); - this->prog.Attach(&(this->vshd)); - } - if (fsrc) { - this->fshd.SetSource(fsrc); - this->prog.Attach(&(this->fshd)); - } - } - - void LoadSources(const char * vfile, const char * ffile) - { - if (vfile) { - this->vshd.LoadSource(vfile); - this->prog.Attach(&(this->vshd)); - } - if (ffile) { - this->fshd.LoadSource(ffile); - this->prog.Attach(&(this->fshd)); - } - } - -protected: - void DoBind(void) - { - this->prog.Bind(); - } - - void DoUnbind(void) - { - this->prog.Unbind(); - } -}; - -#endif // __SHADERS_H__ +****************************************************************************/ + +#ifndef __SHADERS_H__ +#define __SHADERS_H__ + +#include +#include +#include + +#include "gl_object.h" +#include "../../vcg/space/point2.h" +#include "../../vcg/space/point3.h" +#include "../../vcg/space/point4.h" + +class Shader : public GLObject, public Bindable +{ +public: + typedef enum + { + VERTEX, + FRAGMENT, + GEOMETRY + } ShaderType; + + Shader(void) : GLObject(), Bindable() + { + this->flags = 0; + this->flags |= SOURCE_DIRTY; + this->compiled = false; + } + + void Gen(void) + { + this->Del(); + GLenum t; + switch (this->Type()) + { + case Shader::VERTEX : t = GL_VERTEX_SHADER; break; + case Shader::FRAGMENT : t = GL_FRAGMENT_SHADER; break; + case Shader::GEOMETRY : t = GL_GEOMETRY_SHADER_EXT; break; + default: return; + }; + this->objectID = glCreateShader(t); + } + + void Del(void) + { + if (this->objectID == 0) return; + glDeleteShader(this->objectID); + this->objectID = 0; + } + + virtual ShaderType Type(void) const = 0; + + void SetSource(const char * src) + { + if (this->objectID==0) + Gen(); + + this->flags |= SOURCE_DIRTY; + this->compiled = false; + this->source = src; + + const char * pSrc = this->source.c_str(); + glShaderSource(this->objectID, 1, &pSrc, 0); + } + + bool LoadSource(const char * fileName) + { + if (this->objectID==0) + Gen(); + + this->flags |= SOURCE_DIRTY; + this->compiled = false; + FILE * f = fopen(fileName, "rb"); + if (f == 0) + { + this->source = ""; + return false; + } + fseek(f, 0, SEEK_END); + const size_t sz = (size_t)ftell(f); + rewind(f); + char * buff = new char[sz + 1]; + fread(buff, sizeof(char), sz, f); + fclose(f); + buff[sz] = '\0'; + + this->source = buff; + delete [] buff; + + const char * pSrc = this->source.c_str(); + glShaderSource(this->objectID, 1, &pSrc, 0); + + return true; + } + + bool Compile(void) + { + glCompileShader(this->objectID); + GLint cm = 0; + glGetShaderiv(this->objectID, GL_COMPILE_STATUS, &cm); + this->compiled = (cm != GL_FALSE); + this->flags = 0; + return this->compiled; + } + + bool IsCompiled(void) + { + return this->compiled; + } + + std::string InfoLog(void) + { + GLint len = 0; + glGetShaderiv(this->objectID, GL_INFO_LOG_LENGTH, &len); + char * ch = new char[len + 1]; + glGetShaderInfoLog(this->objectID, len, &len, ch); + std::string infoLog = ch; + delete [] ch; + return infoLog; + } + +protected: + enum + { + SOURCE_DIRTY + }; + + std::string source; + unsigned int flags; + bool compiled; + + void DoBind(void) + { + } + + void DoUnbind(void) + { + } +}; + +class VertexShader : public Shader +{ +public: + VertexShader(void) : Shader() + { + } + + ShaderType Type(void) const + { + return Shader::VERTEX; + } +}; + +class FragmentShader : public Shader +{ +public: + FragmentShader(void) : Shader() + { + } + + ShaderType Type(void) const + { + return Shader::FRAGMENT; + } +}; + +class GeometryShader : public Shader +{ +public: + GeometryShader(void) : Shader() + { + } + + ShaderType Type(void) const + { + return Shader::GEOMETRY; + } +}; + +#if 0 +class Program; + +class Uniform +{ +friend class Program; + +public: + /* + typedef enum + { + U_BOOL, + U_BVEC2, + U_BVEC3, + U_BVEC4, + U_BMAT2, + U_BMAT3, + U_BMAT4, + + U_INT, + U_IVEC2, + U_IVEC3, + U_IVEC4, + U_IMAT2, + U_IMAT3, + U_IMAT4, + + U_FLOAT, + U_FVEC2, + U_FVEC3, + U_FVEC4, + U_FMAT2, + U_FMAT3, + U_FMAT4, + + U_SAMPLER1D, + U_SAMPLER2D, + U_SAMPLER3D, + U_SAMPLERCUBE, + U_SAMPLER1DSHADOW, + U_SAMPLER2DSHADOW + } UniformType; + */ + + const std::string & Name(void) const + { + return this->name; + } + + virtual GLenum Type(void) const = 0; + +protected: + Program * prog; + GLint location; + std::string name; + + Uniform(Program * prog, GLint location, const std::string & name) + { + this->prog = prog; + this->location = location; + this->name = name; + } + + virtual void Apply(void) = 0; +}; + +class Uniform1b : public Uniform; +{ +public: + + void SetValue(GLboolean x) + { + this->value[0] = x; + } + + GLboolean GetValue(void) const + { + return this->value[0]; + } + +protected: + Program * prog; + GLboolean value[1]; + + Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) + { + this->value = GL_FALSE; + } +}; + +class Uniform2b : public Uniform; +{ +public: + + void SetValue(GLboolean x, GLboolean y) + { + this->value[0] = x; + this->value[1] = y; + } + + GLboolean GetValueX(void) const + { + return this->value[0]; + } + + GLboolean GetValueY(void) const + { + return this->value[1]; + } + +protected: + Program * prog; + GLboolean value[2]; + + Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) + { + this->value[0] = GL_FALSE; + this->value[1] = GL_FALSE; + } +}; + +class Uniform3b : public Uniform; +{ +public: + + void SetValue(GLboolean x, GLboolean y, GLboolean z) + { + this->value[0] = x; + this->value[1] = y; + this->value[2] = z; + } + + GLboolean GetValueX(void) const + { + return this->value[0]; + } + + GLboolean GetValueY(void) const + { + return this->value[1]; + } + + GLboolean GetValueZ(void) const + { + return this->value[2]; + } + +protected: + Program * prog; + GLboolean value[2]; + + Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) + { + this->value[0] = GL_FALSE; + this->value[1] = GL_FALSE; + } +}; + +class Uniform1i : public Uniform; +{ +public: + + void SetValue(GLint v) + { + this->value = v; + } + + GLint GetValue(void) const + { + return this->value; + } + +protected: + Program * prog; + GLint value; + + Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) + { + this->value = 0; + } +}; +#endif + +class Program : public GLObject, public Bindable +{ +public: + + Program(void) + { + this->linked = false; + } + + void Gen(void) + { + this->Del(); + this->objectID = glCreateProgram(); + } + + void Del(void) + { + if (this->objectID == 0) return; + glDeleteProgram(this->objectID); + this->objectID = 0; + } + + void Attach(Shader * shd) + { + if (this->objectID==0) + Gen(); + this->shaders.insert(shd); + this->linked = false; + glAttachShader(this->objectID, shd->ObjectID()); + } + + void Detach(Shader * shd) + { + this->shaders.erase(shd); + this->linked = false; + glDetachShader(this->objectID, shd->ObjectID()); + } + + GLsizei AttachedShaders(void) const + { + return ((GLsizei)(this->shaders.size())); + } + + Shader * AttachedShader(int i) + { + Shader * shd = 0; + int cnt = 0; + for (std::set::iterator it=this->shaders.begin(); (cnt < i) && (it!=this->shaders.end()); ++it) + { + shd = (*it); + } + return shd; + } + + bool Link(void) + { + bool ok = true; + for (std::set::iterator it=this->shaders.begin(); it!=this->shaders.end(); ++it) + { + Shader * shd = (*it); + if (!shd->IsCompiled()) + { + ok = shd->Compile() && ok; + } + } + + if (!ok) + return false; + + glLinkProgram(this->objectID); + + GLint cm = 0; + glGetProgramiv(this->objectID, GL_LINK_STATUS, &cm); + this->linked = (cm != GL_FALSE); + + return this->linked; + } + + bool IsLinked(void) const + { + return this->linked; + } + + std::string InfoLog(void) + { + GLint len = 0; + glGetProgramiv(this->objectID, GL_INFO_LOG_LENGTH, &len); + char * ch = new char[len + 1]; + glGetProgramInfoLog(this->objectID, len, &len, ch); + std::string infoLog = ch; + delete [] ch; + return infoLog; + } + + void Uniform(const char * name, GLint x) + { + const GLint loc = glGetUniformLocation(this->objectID, name); + glUniform1i(loc, x); + } + + void Uniform(const char * name, GLint x, GLint y) + { + const GLint loc = glGetUniformLocation(this->objectID, name); + glUniform2i(loc, x, y); + } + + void Uniform(const char * name, GLint x, GLint y, GLint z) + { + const GLint loc = glGetUniformLocation(this->objectID, name); + glUniform3i(loc, x, y, z); + } + + void Uniform(const char * name, GLint x, GLint y, GLint z, GLint w) + { + const GLint loc = glGetUniformLocation(this->objectID, name); + glUniform4i(loc, x, y, z, w); + } + + void Uniform(const char * name, GLfloat x) + { + const GLint loc = glGetUniformLocation(this->objectID, name); + glUniform1f(loc, x); + } + + void Uniform(const char * name, GLfloat x, GLfloat y) + { + const GLint loc = glGetUniformLocation(this->objectID, name); + glUniform2f(loc, x, y); + } + + void Uniform(const char * name, GLfloat x, GLfloat y, GLfloat z) + { + const GLint loc = glGetUniformLocation(this->objectID, name); + glUniform3f(loc, x, y, z); + } + + void Uniform(const char * name, GLfloat x, GLfloat y, GLfloat z, GLfloat w) + { + const GLint loc = glGetUniformLocation(this->objectID, name); + glUniform4f(loc, x, y, z, w); + } + + void Uniform(const char * name, const vcg::Point2f& p) + { + const GLint loc = glGetUniformLocation(this->objectID, name); + glUniform2fv(loc, 1, p.V()); + } + + void Uniform(const char * name, const vcg::Point3f& p) + { + const GLint loc = glGetUniformLocation(this->objectID, name); + glUniform3fv(loc, 1, p.V()); + } + + void Uniform(const char * name, const vcg::Point4f& p) + { + const GLint loc = glGetUniformLocation(this->objectID, name); + glUniform4fv(loc, 1, p.V()); + } + + void Parameter(GLenum pname, int value) + { + glProgramParameteriEXT(this->objectID, pname, value); + } + + void Attribute(int index, GLfloat x, GLfloat y, GLfloat z, GLfloat w) + { + glVertexAttrib4f(index, x, y, z, w); + } + + void BindAttribute(int index, const char * name) + { + glBindAttribLocation(this->objectID, index, name); + } + +protected: + std::set shaders; + bool linked; + + void DoBind(void) + { + if (!this->IsLinked()) + { + this->Link(); + } + glUseProgram(this->objectID); + } + + void DoUnbind(void) + { + glUseProgram(0); + } +}; + +class ProgramVF : public Bindable +{ +public: + Program prog; + VertexShader vshd; + FragmentShader fshd; + + ProgramVF(void) : Bindable() + { + } + + void SetSources(const char * vsrc, const char * fsrc) + { + if (vsrc) { + this->vshd.SetSource(vsrc); + this->prog.Attach(&(this->vshd)); + } + if (fsrc) { + this->fshd.SetSource(fsrc); + this->prog.Attach(&(this->fshd)); + } + } + + void LoadSources(const char * vfile, const char * ffile) + { + if (vfile) { + this->vshd.LoadSource(vfile); + this->prog.Attach(&(this->vshd)); + } + if (ffile) { + this->fshd.LoadSource(ffile); + this->prog.Attach(&(this->fshd)); + } + } + +protected: + void DoBind(void) + { + this->prog.Bind(); + } + + void DoUnbind(void) + { + this->prog.Unbind(); + } +}; + +#endif // __SHADERS_H__ diff --git a/wrap/gui/cursorshapearea.cpp b/wrap/gui/cursorshapearea.cpp new file mode 100644 index 00000000..4a090b77 --- /dev/null +++ b/wrap/gui/cursorshapearea.cpp @@ -0,0 +1,20 @@ +#include "cursorshapearea.h" + +CursorShapeArea::CursorShapeArea(QDeclarativeItem *parent) : + QDeclarativeItem(parent) +{ +} + +Qt::CursorShape CursorShapeArea::cursorShape() const +{ + return cursor().shape(); +} + +void CursorShapeArea::setCursorShape(Qt::CursorShape cursorShape) +{ + if (cursor().shape() == cursorShape) + return; + + setCursor(cursorShape); + emit cursorShapeChanged(); +} diff --git a/wrap/gui/qglnxsscenehandler.cpp b/wrap/gui/qglnxsscenehandler.cpp new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/wrap/gui/qglnxsscenehandler.cpp @@ -0,0 +1 @@ + diff --git a/wrap/gui/qgraphicsmatrix3d.cpp b/wrap/gui/qgraphicsmatrix3d.cpp new file mode 100644 index 00000000..09dfc790 --- /dev/null +++ b/wrap/gui/qgraphicsmatrix3d.cpp @@ -0,0 +1,2 @@ +#inclue "qgraphicsmatrix3d.h" + diff --git a/wrap/system/multithreading/atomic_int.h b/wrap/system/multithreading/atomic_int.h new file mode 100755 index 00000000..38df8e89 --- /dev/null +++ b/wrap/system/multithreading/atomic_int.h @@ -0,0 +1,16 @@ +#include "../platform.h" + +#ifndef GC_ATOMIC_INT_H +#define GC_ATOMIC_INT_H + +#ifdef QT_CORE_LIB + +#include +typedef QAtomicInt GAtomincInt; + +#elif defined(__GC_APPLE__) ?? +# include "GCAtomicInt_apple.h" +#endif + +#endif + diff --git a/wrap/system/multithreading/atomic_int_apple.h b/wrap/system/multithreading/atomic_int_apple.h new file mode 100755 index 00000000..09f3c454 --- /dev/null +++ b/wrap/system/multithreading/atomic_int_apple.h @@ -0,0 +1,118 @@ +#ifndef GC_ATOMIC_INT_APPLE_H + +#define GC_ATOMIC_INT_APPLE_H + +#include + + +//http://developer.apple.com/library/mac/#documentation/Darwin/Reference/KernelIOKitFramework/OSAtomic_h/index.html + +class GCAtomicInt +{ +public: + GCAtomicInt() + { + _q_value = 0; + } + GCAtomicInt( int value ) + { + _q_value = value; + } + + // atomic API + + /** + Reads the current value of this QAtomicInt and then adds valueToAdd + to the current value, returning the original value. + + Unfortunately, MacOSX does not provide with fetch-and-add functions, + only add-and-fetch. Therefore, we have to simulate them. + + Implementation based on SDL: + //http://lists.libsdl.org/pipermail/commits-libsdl.org/2011-January/003568.html + */ + inline int fetchAndAddAcquire( int valueToAdd ) + { + //T *originalValue = currentValue; + //currentValue += valueToAdd; + //return originalValue; + + int originalValue; + do { + originalValue = _q_value; + } while (!OSAtomicCompareAndSwap32Barrier(originalValue, originalValue+valueToAdd, &_q_value)); + return originalValue; + } + + /** + Atomically increments the value of this GCAtomicInt. + Returns true if the new value is non-zero, false otherwise.*/ + inline bool ref() + { + return OSAtomicIncrement32Barrier(&_q_value) != 0; + } + + /* + Atomically decrements the value of this QAtomicInt. + Returns true if the new value is non-zero, false otherwise.*/ + inline bool deref() + { + return OSAtomicDecrement32Barrier(&_q_value) != 0; + } + + inline bool testAndSetOrdered(int expectedValue, int newValue) + { + //if (currentValue == expectedValue) { + // currentValue = newValue; + // return true; + // } + //return false; + + return OSAtomicCompareAndSwap32Barrier(expectedValue, newValue, &_q_value); + } + + // Non-atomic API + inline bool operator==(int value) const + { + return _q_value == value; + } + + inline bool operator!=(int value) const + { + return _q_value != value; + } + + inline bool operator!() const + { + return _q_value == 0; + } + + inline operator int() const + { + return _q_value; + } + + inline GCAtomicInt &operator=(int value) + { + _q_value = value; + return *this; + } + + inline bool operator>(int value) const + { + return _q_value > value; + } + + inline bool operator<(int value) const + { + return _q_value < value; + } + +private: + volatile int _q_value; + +}; + + +#endif + diff --git a/wrap/system/multithreading/base.h b/wrap/system/multithreading/base.h new file mode 100755 index 00000000..0bc224fc --- /dev/null +++ b/wrap/system/multithreading/base.h @@ -0,0 +1,18 @@ +#ifndef MT_BASE_H +#define MT_BASE_H + +#include + +#define MT_PREVENT_COPY(CLASS_NAME) \ + private: \ + CLASS_NAME (const CLASS_NAME &); \ + CLASS_NAME & operator = (const CLASS_NAME &); + +#define MT_ASSERT assert + +namespace mt +{ + +} + +#endif // MT_BASE_H diff --git a/wrap/system/multithreading/condition.h b/wrap/system/multithreading/condition.h new file mode 100755 index 00000000..cd07e283 --- /dev/null +++ b/wrap/system/multithreading/condition.h @@ -0,0 +1,53 @@ +#ifndef MT_CONDITION_H +#define MT_CONDITION_H + +#include "base.h" +#include "mutex.h" + +#include + +namespace mt +{ + +class condition +{ + MT_PREVENT_COPY(condition) + + public: + + typedef condition this_type; + typedef void base_type; + + condition(void) + { + pthread_cond_init(&(this->c), 0); + } + + ~condition(void) + { + pthread_cond_destroy(&(this->c)); + } + + void signal(void) + { + pthread_cond_signal(&(this->c)); + } + + void broadcast(void) + { + pthread_cond_broadcast(&(this->c)); + } + + void wait(mutex & m) + { + pthread_cond_wait(&(this->c), &(m.m)); + } + + private: + + pthread_cond_t c; +}; + +} + +#endif // MT_CONDITION_H diff --git a/wrap/system/multithreading/mt.h b/wrap/system/multithreading/mt.h new file mode 100755 index 00000000..5682c885 --- /dev/null +++ b/wrap/system/multithreading/mt.h @@ -0,0 +1,23 @@ +#ifndef MT_MT_H +#define MT_MT_H + + +#ifdef QT_CORE_LIB +#include + +tyepdef QSemaphore mt::Semaphore; + +#else + +#include "base.h" +#include "mutex.h" +#include "rw_lock.h" +#include "semaphore.h" +#include "thread.h" +#include "scoped_mutex_lock.h" +#include "scoped_read_lock.h" +#include "scoped_write_lock.h" + +#endif + +#endif // MT_MT_H diff --git a/wrap/system/multithreading/mutex.h b/wrap/system/multithreading/mutex.h new file mode 100755 index 00000000..e211bc23 --- /dev/null +++ b/wrap/system/multithreading/mutex.h @@ -0,0 +1,51 @@ +#ifndef MT_MUTEX_H +#define MT_MUTEX_H + +#include "base.h" + +#include + +namespace mt +{ + +class condition; + +class mutex +{ + MT_PREVENT_COPY(mutex) + + public: + + typedef mutex this_type; + typedef void base_type; + + mutex(void) + { + pthread_mutex_init(&(this->m), 0); + } + + ~mutex(void) + { + pthread_mutex_destroy(&(this->m)); + } + + void lock(void) + { + pthread_mutex_lock(&(this->m)); + } + + void unlock(void) + { + pthread_mutex_unlock(&(this->m)); + } + + private: + + friend class condition; + + pthread_mutex_t m; +}; + +} + +#endif // MT_MUTEX_H diff --git a/wrap/system/multithreading/rw_lock.h b/wrap/system/multithreading/rw_lock.h new file mode 100755 index 00000000..102d01f9 --- /dev/null +++ b/wrap/system/multithreading/rw_lock.h @@ -0,0 +1,57 @@ +#ifndef MT_RW_LOCK_H +#define MT_RW_LOCK_H + +#include "base.h" + +#include + +namespace mt +{ + +class rw_lock +{ + MT_PREVENT_COPY(rw_lock) + + public: + + typedef rw_lock this_type; + typedef void base_type; + + rw_lock(void) + { + pthread_rwlock_init(&(this->rw), 0); + } + + ~rw_lock(void) + { + pthread_rwlock_destroy(&(this->rw)); + } + + void lock_read(void) + { + pthread_rwlock_rdlock(&(this->rw)); + } + + void unlock_read(void) + { + pthread_rwlock_unlock(&(this->rw)); + } + + void lock_write(void) + { + pthread_rwlock_wrlock(&(this->rw)); + } + + void unlock_write(void) + { + pthread_rwlock_unlock(&(this->rw)); + } + + private: + + pthread_rwlock_t rw; +}; + +} + +#endif // MT_RW_LOCK_H diff --git a/wrap/system/multithreading/scoped_mutex_lock.h b/wrap/system/multithreading/scoped_mutex_lock.h new file mode 100755 index 00000000..c6f4d311 --- /dev/null +++ b/wrap/system/multithreading/scoped_mutex_lock.h @@ -0,0 +1,80 @@ +#ifndef MT_SCOPED_MUTEX_LOCK_H +#define MT_SCOPED_MUTEX_LOCK_H + +#include "mutex.h" + +namespace mt +{ + +class scoped_mutex_lock +{ + MT_PREVENT_COPY(scoped_mutex_lock) + + public: + + typedef scoped_mutex_lock this_type; + typedef void base_type; + + scoped_mutex_lock(mutex & m) : mtx(m) + { + this->mtx.lock(); + } + + ~scoped_mutex_lock(void) + { + this->mtx.unlock(); + } + + protected: + + mutex & mtx; +}; + +} + +#endif // MT_SCOPED_MUTEX_LOCK_H + + + + + +/* +#ifndef MT_SCOPED_MUTEX_LOCK_H +#define MT_SCOPED_MUTEX_LOCK_H + +#include "mutex.h" + +namespace mt +{ + +class scoped_mutex_lock +{ + MT_PREVENT_COPY(scoped_mutex_lock) + + public: + + typedef scoped_mutex_lock this_type; + typedef void base_type; + + scoped_mutex_lock(mutex & m) : mtx(m) + { + this->mtx.lock(); + } + + ~scoped_mutex_lock(void) + { + this->mtx.unlock(); + } + + protected: + + mutex & mtx; +}; + +} + +#endif // MT_SCOPED_MUTEX_LOCK_H + +*/ + + diff --git a/wrap/system/multithreading/scoped_read_lock.h b/wrap/system/multithreading/scoped_read_lock.h new file mode 100755 index 00000000..3686f884 --- /dev/null +++ b/wrap/system/multithreading/scoped_read_lock.h @@ -0,0 +1,35 @@ +#ifndef MT_SCOPED_READ_LOCK_H +#define MT_SCOPED_READ_LOCK_H + +#include "rw_lock.h" + +namespace mt +{ + +class scoped_read_lock +{ + MT_PREVENT_COPY(scoped_read_lock) + + public: + + typedef scoped_read_lock this_type; + typedef void base_type; + + scoped_read_lock(rw_lock & rwl) : rw(rwl) + { + this->rw.lock_read(); + } + + ~scoped_read_lock(void) + { + this->rw.unlock_read(); + } + + protected: + + rw_lock & rw; +}; + +} + +#endif // MT_SCOPED_READ_LOCK_H diff --git a/wrap/system/multithreading/scoped_write_lock.h b/wrap/system/multithreading/scoped_write_lock.h new file mode 100755 index 00000000..2a7a2672 --- /dev/null +++ b/wrap/system/multithreading/scoped_write_lock.h @@ -0,0 +1,35 @@ +#ifndef MT_SCOPED_WRITE_LOCK_H +#define MT_SCOPED_WRITE_LOCK_H + +#include "rw_lock.h" + +namespace mt +{ + +class scoped_write_lock +{ + MT_PREVENT_COPY(scoped_write_lock) + + public: + + typedef scoped_write_lock this_type; + typedef void base_type; + + scoped_write_lock(rw_lock & rwl) : rw(rwl) + { + this->rw.lock_write(); + } + + ~scoped_write_lock(void) + { + this->rw.unlock_write(); + } + + protected: + + rw_lock & rw; +}; + +} + +#endif // MT_SCOPED_WRITE_LOCK_H diff --git a/wrap/system/multithreading/semaphore.h b/wrap/system/multithreading/semaphore.h new file mode 100755 index 00000000..5ef96fb7 --- /dev/null +++ b/wrap/system/multithreading/semaphore.h @@ -0,0 +1,72 @@ +#ifndef MT_SEMAPHORE_H +#define MT_SEMAPHORE_H + +#include "base.h" + +#include + +namespace mt +{ + +class semaphore +{ + MT_PREVENT_COPY(semaphore) + + public: + + typedef semaphore this_type; + typedef void base_type; + + semaphore(void) + { + sem_init(&(this->s), 0, 0); + } + + semaphore(int value) + { + sem_init(&(this->s), 0, value); + } + + ~semaphore(void) + { + sem_destroy(&(this->s)); + } + + void post(void) + { + sem_post(&(this->s)); + } + + /* + void post(int n) + { + sem_post_multiple(&(this->s), n); + } + */ + + void wait(void) + { + sem_wait(&(this->s)); + } + + bool trywait(void) + { + return (sem_trywait(&(this->s)) == 0); + } + + //jnoguera 11-Nov-2011 + int available() + { + int value; + sem_getvalue( &(this->s), &value ); + return value; + } + + private: + + sem_t s; +}; + +} + +#endif // MT_SEMAPHORE_H diff --git a/wrap/system/multithreading/thread.h b/wrap/system/multithreading/thread.h new file mode 100755 index 00000000..fdb610d4 --- /dev/null +++ b/wrap/system/multithreading/thread.h @@ -0,0 +1,111 @@ +#ifndef MT_THREAD_H +#define MT_THREAD_H + +#include + +#include "base.h" + +#include + +namespace mt +{ + +class thread +{ + MT_PREVENT_COPY(thread) + + public: + + typedef thread this_type; + typedef void base_type; + + thread(void) : flags(0) + { + ; + } + + virtual ~thread(void) + { + ; + } + + virtual bool start(void) + { + if ((this->flags & thread_started) != 0) return false; + + pthread_create(&(this->tid), 0, this_type::thread_func, reinterpret_cast(this)); + + /* + sched_param sp; + memset(&sp, 0, sizeof(sched_param)); + sp.sched_priority = sched_get_priority_min(SCHED_OTHER); + sp.sched_priority = 0; // normal + pthread_setschedparam(this->tid, SCHED_OTHER, &sp); + */ + + this->flags |= thread_started; + + return true; + } + + virtual bool wait(void) + { + if ((this->flags & thread_started) == 0) return false; + pthread_join(this->tid, 0); + this->flags &= ~thread_started; + return true; + } + + virtual bool kill(void) + { + if ((this->flags & thread_started) == 0) return false; + pthread_kill(this->tid, 0); + this->flags &= ~(thread_started | thread_running); + return true; + } + + bool is_started(void) const + { + return ((this->flags & thread_started) != 0); + } + + bool is_running(void) const + { + return ((this->flags & thread_running) != 0); + } + + protected: + + virtual void run(void) + { + ; + } + + private: + + enum thread_flags + { + thread_none = ( 0), + thread_started = (1 << 0), + thread_running = (1 << 1) + }; + + volatile unsigned int flags; + pthread_t tid; + + static void * thread_func(void * param) + { + this_type * p_this = reinterpret_cast(param); + MT_ASSERT(p_this != 0); + + p_this->flags |= thread_running; + p_this->run(); + p_this->flags &= ~thread_running; + + return 0; + } +}; + +} + +#endif // MT_THREAD_H diff --git a/wrap/system/multithreading/util.h b/wrap/system/multithreading/util.h new file mode 100755 index 00000000..3a930327 --- /dev/null +++ b/wrap/system/multithreading/util.h @@ -0,0 +1,41 @@ +#ifndef MT_UTIL_H +#define MT_UTIL_H + +#if (defined(_MSC_VER)) +#include +#elif (defined(__MINGW32__)) +#include +#elif (defined(__linux__)) +#include +#else +#include +//#error "mt utils.h : unrecognized environment." +#endif + +namespace mt +{ + +inline void sleep_ms(unsigned int msecs) +{ +#if (defined(_MSC_VER)) + Sleep(DWORD(msecs)); +#elif (defined(__MINGW32__)) + Sleep(DWORD(msecs)); +#elif (defined(__linux__)) + const unsigned int secs = ((unsigned int)(msecs / 1000)); + const unsigned long usecs = ((unsigned long)((msecs % 1000) * 1000)); + + sleep(secs); + usleep(usecs); +#else + const unsigned int secs = ((unsigned int)(msecs / 1000)); + const unsigned long usecs = ((unsigned long)((msecs % 1000) * 1000)); + + sleep(secs); + usleep(usecs); +#endif +} + +} + +#endif // MT_UTIL_H diff --git a/wrap/system/qgetopt.cpp b/wrap/system/qgetopt.cpp index 48b36eea..52fca14b 100644 --- a/wrap/system/qgetopt.cpp +++ b/wrap/system/qgetopt.cpp @@ -224,7 +224,7 @@ bool GetOpt::parse(QString &error) { return false; } arg = args[i]; - if(i == args.size() || arg[0] == '-') { + if(i == args.size()) { error = "Missing argument after option '" + arg + "'"; return false; } diff --git a/wrap/system/time/chronometer.cpp b/wrap/system/time/chronometer.cpp new file mode 100644 index 00000000..cd5209c6 --- /dev/null +++ b/wrap/system/time/chronometer.cpp @@ -0,0 +1,175 @@ + +#include "L3DChronometer.h" + + +/****************************************************************************** + +Implementacion de metodos multiplataforma + +*******************************************************************************/ + +// ----------------------------------------------------------------------------- + +void L3DChronometer::stop () +{ + if (state == TIMER_RUNNING || state == TIMER_PAUSED) { + if (state == TIMER_RUNNING) + updateTime (); + state = TIMER_STOPPED; + } +} + +// ----------------------------------------------------------------------------- + +void L3DChronometer::pause () +{ + if (state == TIMER_RUNNING) { + updateTime (); + state = TIMER_PAUSED; + } +} + +/****************************************************************************** + +Implementacion de metodos para Windows + +*******************************************************************************/ + +#ifdef DEVICE_X86_WIN + +// ----------------------------------------------------------------------------- + +L3DChronometer::L3DChronometer() { + tacum.QuadPart = 0; + QueryPerformanceFrequency(&freq); + state = TIMER_STOPPED; + } + +// ----------------------------------------------------------------------------- + +float L3DChronometer::timeSecs () { + return (float) tacum.QuadPart / freq.QuadPart; +} + +// ----------------------------------------------------------------------------- + +float L3DChronometer::timeMSecs () { + return (float) tacum.QuadPart / ((float)freq.QuadPart / 1000); +} + +// ----------------------------------------------------------------------------- + +float L3DChronometer::timeUSecs () { + return (float) tacum.QuadPart / ((float)freq.QuadPart / 1000000u); +} + +// ----------------------------------------------------------------------------- + +void L3DChronometer::updateTime () +{ + LARGE_INTEGER tnow; + + QueryPerformanceCounter(&tnow); + tacum.QuadPart += (tnow.QuadPart - tini.QuadPart); + tini.QuadPart = tnow.QuadPart; +} + +// ----------------------------------------------------------------------------- + +void L3DChronometer::start () +{ + if (state == TIMER_STOPPED) { + tacum.QuadPart = 0; + QueryPerformanceCounter(&tini); + state = TIMER_RUNNING; + } +} + +// ----------------------------------------------------------------------------- + +void L3DChronometer::resume () +{ + if (state == TIMER_PAUSED) { + QueryPerformanceCounter(&tini); + state = TIMER_RUNNING; + } +} + +// ----------------------------------------------------------------------------- + + + +/****************************************************************************** + +Implementacion de metodos para sistemas operativos posix + +*******************************************************************************/ + +#else + +// ----------------------------------------------------------------------------- + +L3DChronometer::L3DChronometer() { + sec = 0; + usec = 0; + state = TIMER_STOPPED; +} + +// ----------------------------------------------------------------------------- + +float L3DChronometer::timeSecs () { + return (float) sec + (float) usec / 1000000.0f; +} + +// ----------------------------------------------------------------------------- + +float L3DChronometer::timeMSecs () { + return (float) sec * 1000.0f + (float) usec / 1000.0f; +} + +// ----------------------------------------------------------------------------- + +float L3DChronometer::timeUSecs () { + return (float) sec * 1000000.0f + (float) usec; +} + +// ----------------------------------------------------------------------------- + +void L3DChronometer::updateTime () +{ + timeval tnow; + gettimeofday (&tnow, 0); + + sec += tnow.tv_sec - tini.tv_sec; + usec += tnow.tv_usec - tini.tv_usec; + if (usec < 0) { + sec --; + usec += 1000000u; + } +} + +// ----------------------------------------------------------------------------- + +void L3DChronometer::start () +{ + if (state == TIMER_STOPPED) { + sec = 0; usec = 0; + gettimeofday (&tini, 0); + state = TIMER_RUNNING; + } +} + +// ----------------------------------------------------------------------------- + +void L3DChronometer::resume () +{ + if (state == TIMER_PAUSED) { + gettimeofday (&tini, 0); + state = TIMER_RUNNING; + } +} + +// ----------------------------------------------------------------------------- + +#endif + diff --git a/wrap/system/time/chronometer.h b/wrap/system/time/chronometer.h new file mode 100644 index 00000000..a5a6c5b9 --- /dev/null +++ b/wrap/system/time/chronometer.h @@ -0,0 +1,76 @@ +//******************************************************************* +// L3DChronometer.h -- Header for L3DChronometer.cpp +// Copyright (c) 2010 Jose Maria Noguera +// Aug 10, 2010 +// +// Jose Maria Noguera Rozua http://wwwdi.ujaen.es/~jnoguera/ +// +//******************************************************************* + + +#ifndef _L3D_CHRONOMETER_H_ +#define _L3D_CHRONOMETER_H_ + +#include "../../L3DPlatform.h" + +#ifdef DEVICE_X86_WIN + #include +#else + #include +#endif + + +#define TIMER_STOPPED 0 +#define TIMER_RUNNING 1 +#define TIMER_PAUSED 2 + +/** @brief Chronometer used to measure performance of algoritms */ + +class L3DChronometer{ + +public: + /** Initializes the Chronometer */ + L3DChronometer(); + /** Starts to measure time */ + void start (); + /** Pause without losing the measured time */ + void pause (); + /** Resume a previously paused Chronometer */ + void resume (); + /** Stops the Chronometer */ + void stop (); + float elapsed() { stop(); float r = timeMSecs(); resume(); return r } + + /** Gets time in seconds. The Chronometer must be stop */ + float timeSecs (); + /** Gets time in seconds. The Chronometer must be stop */ + float timeMSecs (); + /** Gets time in seconds. The Chronometer must be stop*/ + float timeUSecs (); + +private: + +#ifdef DEVICE_X86_WIN + /* Initial time */ + LARGE_INTEGER tini; + /* Seconds accumulator */ + LARGE_INTEGER tacum; + /* Frequency */ + LARGE_INTEGER freq; +#else + /** Initial time */ + timeval tini; + /* Seconds accumulator */ + unsigned sec; + /* MicroSeconds accumulator */ + long usec; +#endif + + /* Chronometer state: stopped, running, paused */ + int state; + /* Updates time accumulators */ + void updateTime (); + +}; + +#endif diff --git a/wrap/system/time/clock.cpp b/wrap/system/time/clock.cpp new file mode 100644 index 00000000..165b3758 --- /dev/null +++ b/wrap/system/time/clock.cpp @@ -0,0 +1,77 @@ +#include "L3DTimer.h" + + +#ifdef DEVICE_X86_WIN + #include +#else + #include +#endif + + +#include + + + + +L3DTimer::L3DTimer() +{/* + base = 0; + initialized = false; + basetime = 0;*/ +} + +//--------------------------------------------------------------------------- + +int L3DTimer::GetCurrentSystemTime(void) +{ + int iReturnValue = -1; + + #ifdef DEVICE_X86_WIN + iReturnValue = Win_GetCurrentSystemTime(); + #else + iReturnValue = Linux_GetCurrentSystemTime(); + #endif + + return iReturnValue; +} + +//--------------------------------------------------------------------------- + +#ifdef DEVICE_X86_WIN + +int L3DTimer::Win_GetCurrentSystemTime(void) +{ + int curtime; + static int base; + static bool initialized = false; + if(!initialized) + { + base = timeGetTime() & 0xffff0000; + initialized = true; + } + curtime = timeGetTime() - base; + return curtime; +} + +//--------------------------------------------------------------------------- + +#else + +//--------------------------------------------------------------------------- + +int L3DTimer::Linux_GetCurrentSystemTime(void) +{ + struct timeval tp; + struct timezone tzp; + static int basetime; + gettimeofday(&tp, &tzp); + if(!basetime) + { + basetime = tp.tv_sec; + return tp.tv_usec / 1000; + } + return (tp.tv_sec - basetime) * 1000 + tp.tv_usec / 1000; +} +#endif + +//--------------------------------------------------------------------------- diff --git a/wrap/system/time/clock.h b/wrap/system/time/clock.h new file mode 100644 index 00000000..3fcbbf8d --- /dev/null +++ b/wrap/system/time/clock.h @@ -0,0 +1,43 @@ +//******************************************************************* +// L3DTimer.h -- Header for L3DTimer.cpp +// Copyright (c) 2010 Jose Maria Noguera +// Aug 10, 2010 +// +// Jose Maria Noguera Rozua http://wwwdi.ujaen.es/~jnoguera/ +// +//******************************************************************* + + +#ifndef _L3D_TIMER_ +#define _L3D_TIMER_ + +#include "../../L3DPlatform.h" + +class L3DTimer +{ + +public: + L3DTimer(); + ~L3DTimer(){;} + + /** + This function retrieves the system time, in milliseconds. The system time is the time elapsed since the first call + to this function. + */ + static int GetCurrentSystemTime(void); + +private: +/* + static int base; + static bool initialized; + static int basetime; +*/ + #ifdef DEVICE_X86_WIN + static int Win_GetCurrentSystemTime(void); + #else + static int Linux_GetCurrentSystemTime(void); + #endif +}; + +#endif +