Commits

David Lin  committed b1bab5e

Add Thread, FileStream

  • Participants
  • Parent commits 3acb4cd

Comments (0)

Files changed (17)

File engine/include/Mocha/Mocha.h

 #include "MochaDataStream.h"
 
 ////////////////////////////////////////////////////////////////////////////////
+
+/*
+ http://en.wikipedia.org/wiki/Special_member_functions
+ http://en.wikipedia.org/wiki/UTF-8
+ 
+ */

File engine/include/Mocha/MochaDataStream.h

 
 namespace Mocha{
   
-  class MOCHA_EXPORT DataStream : public Object{
+  class MOCHA_EXPORT InputStream{
   public:
-    enum AccessMode{
-      READ = 1, 
-      WRITE = 2 
-    };
+    InputStream();
+    virtual ~InputStream();
     
+    // Read Data and moving the file pointer
+    virtual bool read( void* buffer, Size bufSize, Size& readSize )=0;
+    
+    // Read Data from specific block, without moving the file pointer
+    virtual bool read( void* buffer, Size bufSize, Offset pos, Size& readSize );
+    
+  };
+  
+  class MOCHA_EXPORT OutputStream{
   public:
-    DataStream( AccessMode mode );
-    virtual ~DataStream();
+    OutputStream();
+    virtual ~OutputStream();
     
+    // Write Data and moving the file pointer
+    virtual bool write( const void* buffer, Size bufSize )=0;
     
+    // Write Data from specific block, without moving the file pointer
+    virtual bool write( const void* buffer, Size bufSize, Offset pos );
     
-  public:
-    
-    
-  private:
-    const AccessMode m_Mode;
   };
   
 }
+
+namespace Mocha{
+  
+  class MOCHA_EXPORT FileStream : public InputStream, public OutputStream{
+  public:
+    FileStream();
+    virtual ~FileStream();
+    
+    void openAndRead( const char* filename );
+    void createAndWrite( const char* filename );
+    void close();
+    bool isOpen()const;
+    
+    virtual bool read( void* buffer, Size bufSize, Size& readSize );
+    virtual bool read( void* buffer, Size bufSize, Offset pos, Size& readSize );
+    virtual bool write( const void* buffer, Size bufSize );
+    virtual bool write( const void* buffer, Size bufSize, Offset pos );
+    
+  private:
+    int m_File;
+  };
+  
+}
+
+namespace Mocha{
+  
+  
+  
+}

File engine/include/Mocha/MochaMath.h

     return min( maxVal, max( minVal, x ) );
   }
   
+  template<typename T>
+  inline T div( T a, T n ){
+    return mul( a, recip(n) );
+  }
+  
+  template<typename T>
+  inline T mod( T a, T n ){
+    return a - n * floor(div(a, n));
+  }
+  
 }
 
 ////////////////////////////////////////////////////////////////////////////////
   };
   
   template<> struct Const<F32>{
-    static const F32 zero(){ return (0.0f); }
-    static const F32 one(){ return (1.0f); }
-    static const F32 half(){ return (0.5f); }
-    static const F32 negOne(){ return (-1.0f); }
-    //static const F32 PI = 3.141592653589793f;
+    static const F32 ZERO;
+    static const F32 ONE;
+    static const F32 HALF_ONE;
+    static const F32 NEG_ONE;
+    static const F32 PI;
+    static const F32 TWO_PI;
+    static const F32 RECIP_PI;
   };
+  const F32 Const<F32>::ZERO = 0.0f;
+  const F32 Const<F32>::ONE = 1.0f;
+  const F32 Const<F32>::HALF_ONE = 0.5f;
+  const F32 Const<F32>::NEG_ONE = -1.0f;
+  const F32 Const<F32>::PI = 3.141592653589793f;
+  const F32 Const<F32>::TWO_PI = 6.283185307179586f;
+  const F32 Const<F32>::RECIP_PI = 0.318309886183791;
   
   template<typename T> class Vector3;
   template<typename T> class Vector4;
 
 ////////////////////////////////////////////////////////////////////////////////
 
+namespace Mocha{
+  
+  template<typename T> class Degree;
+  template<typename T> class Radian;
+  
+  template<typename T>
+  class Degree{
+  public:
+    explicit Degree(T val):m_Value(val){}
+    explicit Degree(const Radian<T>& rad);
+    
+    const Degree normalize()const{
+      return Degree( mod(m_Value, T(360)) );
+    }
+    
+    T get()const{ return m_Value; }
+    
+  private:
+    T m_Value;
+  };
+  
+  template<typename T>
+  class Radian{
+  public:
+    explicit Radian(T val):m_Value(val){}
+    explicit Radian(const Degree<T>& deg);
+    
+    const Radian normalize()const{
+      return Radian( mod(m_Value, Const<T>::TWO_PI) );
+    }
+    
+    T get()const{ return m_Value; }
+    
+  private:
+    T m_Value;
+  };
+  
+  template<typename T>
+  Degree<T>::Degree(const Radian<T>& rad)
+  :m_Value( mul(rad.get(), mul(Const<T>::RECIP_PI, T(180))) )
+  {
+  }
+  
+  template<typename T>
+  Radian<T>::Radian(const Degree<T>& deg)
+  :m_Value( mul(deg.get(), mul(Const<T>::PI, T(0.005555555555556))) )
+  {
+  }
+  
+  template<typename T>
+  std::ostream& operator<<( std::ostream& o, const Degree<T>& d ){
+    return o << d.get() << "\xC2\xB0";
+  }
+  
+  template<typename T>
+  std::ostream& operator<<( std::ostream& o, const Radian<T>& r ){
+    return o << r.get() << " Rad";
+  }
+  
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 
 ////////////////////////////////////////////////////////////////////////////////

File engine/include/Mocha/MochaMathMatrix.h

     const T qyqw2 = mul( qw, qy2 );
     const T qzqz2 = mul( qz, qz2 ), qzqw2 = mul( qw, qz2 );
     
-    m_Col0 = Vector3<T>(sub( sub( Const<T>::one(), qyqy2 ), qzqz2 ), 
+    m_Col0 = Vector3<T>(sub( sub( Const<T>::ONE, qyqy2 ), qzqz2 ), 
                         add( qxqy2, qzqw2 ), 
                         sub( qxqz2, qyqw2 ) );
     
     m_Col1 = Vector3<T>(sub( qxqy2, qzqw2 ), 
-                        sub( sub( Const<T>::one(), qxqx2 ), qzqz2 ), 
+                        sub( sub( Const<T>::ONE, qxqx2 ), qzqz2 ), 
                         add( qyqz2, qxqw2 ) );
     m_Col2 = Vector3<T>(add( qxqz2, qyqw2 ), 
                         sub( qyqz2, qxqw2 ), 
-                        sub( sub( Const<T>::one(), qxqx2 ), qyqy2 ) );
+                        sub( sub( Const<T>::ONE, qxqx2 ), qyqy2 ) );
     
   }
   
   
   template<typename T>
   const Matrix3<T> Matrix3<T>::scale( const Vector3<T>& scaleVec ){
-    const Vector3<T> tmp0( scaleVec.getX(), Const<T>::zero(), Const<T>::zero() );
-    const Vector3<T> tmp1( Const<T>::zero(), scaleVec.getY(), Const<T>::zero() );
-    const Vector3<T> tmp2( Const<T>::zero(), Const<T>::zero(), scaleVec.getZ() );
+    const Vector3<T> tmp0( scaleVec.getX(), Const<T>::ZERO, Const<T>::ZERO );
+    const Vector3<T> tmp1( Const<T>::ZERO, scaleVec.getY(), Const<T>::ZERO );
+    const Vector3<T> tmp2( Const<T>::ZERO, Const<T>::ZERO, scaleVec.getZ() );
     return Matrix3<T>( tmp0, tmp1, tmp2 );
   }
   
     sincos( radians, s, c );
     const T x = unitVec.getX(), y = unitVec.getY(), z = unitVec.getZ();
     const T xy = mul( x, y ), yz = mul( y, z ), zx = mul( z, x );
-    const T oneMinusC = sub( Const<T>::one(), c );
+    const T oneMinusC = sub( Const<T>::ONE, c );
     return Matrix3(Vector3<T>(add( mul( mul( x, x ), oneMinusC ), c ), 
                               add( mul( xy, oneMinusC ), mul( z, s ) ), 
                               sub( mul( zx, oneMinusC ), mul( y, s ) ) ),
   template<typename T>
   inline Matrix4<T>::Matrix4(const Matrix3<T>& upperleftMat,
                              const Vector3<T>& transVec)
-  :m_Col0(upperleftMat.getCol0(), Const<T>::zero()),
-  m_Col1(upperleftMat.getCol1(), Const<T>::zero()),
-  m_Col2(upperleftMat.getCol2(), Const<T>::zero()),
-  m_Col3(transVec, Const<T>::one())
+  :m_Col0(upperleftMat.getCol0(), Const<T>::ZERO),
+  m_Col1(upperleftMat.getCol1(), Const<T>::ZERO),
+  m_Col2(upperleftMat.getCol2(), Const<T>::ZERO),
+  m_Col3(transVec, Const<T>::ONE)
   {
   }
   
     const T rl = recip(sub( xRight, xLeft ));
     const T tb = recip(sub( yTop, yBottom ));
     const T nf = recip(sub( zNear, zFar ));
-    const T zero = Const<T>::zero();
+    const T zero = Const<T>::ZERO;
     
     const Vector4<T> col0( mul(n2, rl), zero, zero, zero );
     
     const Vector4<T> col2(mul( add( xRight, xLeft ), rl ), 
                           mul( add( yTop, yBottom ), tb ),
                           mul( add( zFar, zNear ), nf ), 
-                          Const<T>::negOne() );
+                          Const<T>::NEG_ONE );
     
     const Vector4<T> col3( zero, zero, mul(mul(n2, zFar), nf), zero );
     

File engine/include/Mocha/MochaMathQuaternion.h

   
   template<typename T>
   const Quaternion<T> Quaternion<T>::identity(){
-    return Quaternion<T>( Vector3<T>::zero(), Const<T>::one() );
+    return Quaternion<T>( Vector3<T>::zero(), Const<T>::ONE );
   }
   
   template<typename T>
   const Quaternion<T> 
   Quaternion<T>::rotation( T radians, const Vector3<T>& vec ){
     T s, c;
-    sincos( mul(radians, Const<T>::half()), s, c );
+    sincos( mul(radians, Const<T>::HALF_ONE), s, c );
     return Quaternion<T>( vec * s, c );
   }
   
   const DualQuaternion<T> DualQuaternion<T>::build(const Quaternion<T>& rota,
                                                    const Vector3<T>& trans)
   {
-    const Quaternion<T> t( trans * Const<T>::half(), Const<T>::zero() );
+    const Quaternion<T> t( trans * Const<T>::HALF_ONE, Const<T>::ZERO );
     
     return DualQuaternion<T>( rota, t * rota );
     // q0 = rot, qE = 0.5 * trans * q0

File engine/include/Mocha/MochaMathVector.h

   
   template<typename T>
   const Vector3<T> Vector3<T>::zero(){
-    return Vector3<T>( Const<T>::zero() );
+    return Vector3<T>( Const<T>::ZERO );
   }
   
   template<typename T>
   const Vector3<T> Vector3<T>::xAxis(){
-    return Vector3<T>( Const<T>::one(), Const<T>::zero(), Const<T>::zero() );
+    return Vector3<T>( Const<T>::ONE, Const<T>::ZERO, Const<T>::ZERO );
   }
   
   template<typename T>
   const Vector3<T> Vector3<T>::yAxis(){
-    return Vector3<T>( Const<T>::zero(), Const<T>::one(), Const<T>::zero() );
+    return Vector3<T>( Const<T>::ZERO, Const<T>::ONE, Const<T>::ZERO );
   }
   
   template<typename T>
   const Vector3<T> Vector3<T>::zAxis(){
-    return Vector3<T>( Const<T>::zero(), Const<T>::zero(), Const<T>::one() );
+    return Vector3<T>( Const<T>::ZERO, Const<T>::ZERO, Const<T>::ONE );
   }
   
 }
   
   template<typename T>
   const Vector4<T> Vector4<T>::zero(){
-    return Vector4<T>( Const<T>::zero() );
+    return Vector4<T>( Const<T>::ZERO );
   }
   
   template<typename T>
   const Vector4<T> Vector4<T>::xAxis(){
-    return Vector4<T>( Const<T>::one(), Const<T>::zero(), 
-                      Const<T>::zero(), Const<T>::zero() );
+    return Vector4<T>( Const<T>::ONE, Const<T>::ZERO, 
+                      Const<T>::ZERO, Const<T>::ZERO );
   }
   
   template<typename T>
   const Vector4<T> Vector4<T>::yAxis(){
-    return Vector4<T>( Const<T>::zero(), Const<T>::one(), 
-                      Const<T>::zero(), Const<T>::zero() );
+    return Vector4<T>( Const<T>::ZERO, Const<T>::ONE, 
+                      Const<T>::ZERO, Const<T>::ZERO );
   }
   
   template<typename T>
   const Vector4<T> Vector4<T>::zAxis(){
-    return Vector4<T>( Const<T>::zero(), Const<T>::zero(), 
-                      Const<T>::one(), Const<T>::zero() );
+    return Vector4<T>( Const<T>::ZERO, Const<T>::ZERO, 
+                      Const<T>::ONE, Const<T>::ZERO );
   }
   
   template<typename T>
   const Vector4<T> Vector4<T>::wAxis(){
-    return Vector4<T>( Const<T>::zero(), Const<T>::zero(), 
-                      Const<T>::zero(), Const<T>::one() );
+    return Vector4<T>( Const<T>::ZERO, Const<T>::ZERO, 
+                      Const<T>::ZERO, Const<T>::ONE );
   }
   
 }

File engine/include/Mocha/MochaObject.h

 
 namespace Mocha{
   
+  // Issue: Can Non-Copyable impact Sub Classes ?
+  class MOCHA_EXPORT NonCopyable{
+  public:
+    NonCopyable(){
+    }
+    
+  private:
+    NonCopyable( const NonCopyable& );
+    void operator=( const NonCopyable& );
+    
+  };
+  
   class MOCHA_EXPORT Object{
   public:
     

File engine/include/Mocha/MochaPlatform.h

 ////////////////////////////////////////////////////////////////////////////////
 
 #undef MOCHA_CPU
+#undef MOCHA_WORDBITS
+#undef MOCHA_ADDRBITS
 
 #define MOCHA_CPU_I386 1
 #define MOCHA_CPU_X86_64 2
 
 #if (defined(__i386__)||defined(_M_IX86))
 #  define MOCHA_CPU MOCHA_CPU_I386
+#  define MOCHA_WORDBITS 32
+#  define MOCHA_ADDRBITS 32
 
 #elif (defined(__x86_64__)||defined(_M_X64)||defined(_M_AMD64))
 #  define MOCHA_CPU MOCHA_CPU_X86_64
+#  define MOCHA_WORDBITS 64
+#  define MOCHA_ADDRBITS 64
 
 #elif (__arm__)
 #  define MOCHA_CPU MOCHA_CPU_ARM
+#  define MOCHA_WORDBITS 32
+#  define MOCHA_ADDRBITS 32
 
 #endif
 
 #ifndef MOCHA_CPU
 #  error Unknown CPU
 #endif
+#ifndef MOCHA_WORDBITS
+#  error Unknown Bits of Word
+#endif
+#ifndef MOCHA_ADDRBITS
+#  error Unknown Bits of Address
+#endif
 
 ////////////////////////////////////////////////////////////////////////////////
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
-
-
 ////////////////////////////////////////////////////////////////////////////////

File engine/include/Mocha/MochaPrerequisites.h

 ////////////////////////////////////////////////////////////////////////////////
 
 namespace Mocha{
+  
+  // 8-bit Char Type
+#if (CHAR_BIT==8)
+  typedef char C8;
+#else
+#  error Cannot Define C8
+#endif
+  
+  // 8-bit Int Type
+#if (UCHAR_MAX==0xff)
+  typedef unsigned char U8;
+#else
+#  error Cannot Define U8
+#endif
+  
+  // 8-bit Int Type
+#if (SCHAR_MAX==0x7f)
   typedef signed char I8;
+#else
+#  error Cannot Define I8
+#endif
+  
+  // 16-bit Int Type
+#if (UINT_MAX==0xffff)
+  typedef unsigned int U16;
+#elif (USHRT_MAX==0xffff)
+  typedef unsigned short U16;
+#else
+#  error Cannot Define U16
+#endif
+  
+  // 16-bit Int Type
+#if (SINT_MAX==0x7fff)
+  typedef signed int I16;
+#elif (SHRT_MAX==0x7fff)
   typedef signed short I16;
+#else
+#  error Cannot Define I16
+#endif
+  
+  // 32-bit Int Type
+#if (ULONG_MAX==0xffffffffUL)
+  typedef unsigned long U32;
+#elif (UINT_MAX==0xffffffff)
+  typedef unsigned int U32;
+#else
+#  error Cannot Define U32
+#endif
+  
+  // 32-bit Int Type
+#if (LONG_MAX==0x7fffffffL)
+  typedef signed long I32;
+#elif (INT_MAX==0x7fffffff)
   typedef signed int I32;
+#else
+#  error Cannot Define I32
+#endif
+  
+  // 64-bit Int Type
+#if (ULONG_MAX==0xffffffffffffffffUL)
+  typedef unsigned long U64;
+#elif (ULLONG_MAX==0xffffffffffffffffULL)
+  typedef unsigned long long U64;
+#else
+#  error Cannot Define U64
+#endif
+  
+  // 64-bit Int Type
+#if (LONG_MAX==0x7fffffffffffffffL)
+  typedef signed long I64;
+#elif (LLONG_MAX==0x7fffffffffffffffLL)
   typedef signed long long I64;
+#else
+#  error Cannot Define I64
+#endif
   
-  typedef unsigned char U8;
-  typedef unsigned short U16;
-  typedef unsigned int U32;
-  typedef unsigned long long U64;
+  // Int Pointer Type
+#if MOCHA_ADDRBITS == 64
+  typedef U64 UnsignedPtr;
+  typedef I64 SignedPtr;
+#elif MOCHA_ADDRBITS == 32
+  typedef U32 UnsignedPtr;
+  typedef I32 SignedPtr;
+#else
+#  error Cannot Define UnsignedPtr and SignedPtr
+#endif
   
+  // Size Type
   typedef std::size_t Size;
+  
+  // PitDiff Type
   typedef std::ptrdiff_t Offset;
   
+  // Single Float Type
   typedef float F32;
   
+  // Double Float Type
+  typedef double F64;
+  
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 
-#if defined(_MSC_VER)
+namespace Mocha{
+  namespace Atomic{
+    
+    // Assign
+    inline I32 exchange( volatile I32* target, I32 value ){
+#if defined(__GNUC__)
+      return __sync_lock_test_and_set( target, value );
+#elif defined(_MSC_VER)
+      return _InterlockedExchange( (long*)target, value );
+#endif
+    }
+    
+    // Fetch and Add
+    inline I32 exchagneAdd( volatile I32* target, I32 value ){
+#if defined(__GNUC__)
+      return __sync_fetch_and_add( target, value );
+#elif defined(_MSC_VER)
+      return _InterlockedExchangeAdd( (long*)target, value );
+#endif
+    }
+    
+    // Compare And Swap
+    inline I32 compareExchange( volatile I32* target, I32 exchg, I32 old ){
+#if defined(__GNUC__)
+      return __sync_val_compare_and_swap( target, old, exchg );
+#elif defined(_MSC_VER)
+      return _InterlockedCompareExchange( (long*)target, exchg, old );
+#endif
+    }
+    
+    // Increment
+    inline I32 increase( volatile I32* target ){
+#if defined(__GNUC__)
+      return __sync_add_and_fetch( target, 1 );
+#elif defined(_MSC_VER)
+      return _InterlockedIncrement( (long*)target );
+#endif
+    }
+    
+    // Decrement
+    inline I32 decrease( volatile I32* target ){
+#if defined(__GNUC__)
+      return __sync_add_and_fetch( target, -1 );
+#elif defined(_MSC_VER)
+      return _InterlockedDecrement( (long*)target );
+#endif
+    }
+    
+  }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
 namespace Mocha{
   
-  inline I32 atomicExchange( volatile I32* target, I32 value ){
-    return _InterlockedExchange( (long*)target, value );
+  namespace GL{
+    typedef unsigned int GLenum;
+    typedef unsigned char GLboolean;
+    typedef unsigned int GLbitfield;
+    typedef signed char GLbyte;
+    typedef short GLshort;
+    typedef int GLint;
+    typedef int GLsizei;
+    typedef unsigned char GLubyte;
+    typedef unsigned short GLushort;
+    typedef unsigned int GLuint;
+    typedef float GLfloat;
+    typedef float GLclampf;
+    typedef double GLdouble;
+    typedef double GLclampd;
+    typedef void GLvoid;
+    typedef ptrdiff_t GLintptr;
+    typedef ptrdiff_t GLsizeiptr;
+    typedef char GLchar;
+    typedef I64 GLint64;
+    typedef U64 GLuint64;
   }
   
-  inline I32 atomicExchangeAdd( volatile I32* target, I32 value ){
-    return _InterlockedExchangeAdd( (long*)target, value );
-  }
-  
-  inline I32 atomicCompareExchange( volatile I32* target, I32 exchg, I32 old ){
-    return _InterlockedCompareExchange( (long*)target, exchg, old );
-  }
-  
-  inline I32 atomicAdd( volatile I32* target, I32 value ){
-    return _InterlockedExchangeAdd( (long*)target, value )+1;
-  }
-  
-  inline I32 atomicInc( volatile I32* target ){
-    return _InterlockedIncrement( (long*)target );
-  }
-  
-  inline I32 atomicDec( volatile I32* target ){
-    return _InterlockedDecrement( (long*)target );
+  namespace Win32{
+    typedef void* Handle;
   }
   
 }
 
-#elif defined(__GNUC__)
-namespace Mocha{
-  
-  inline I32 atomicExchange( volatile I32* target, I32 value ){
-    return __sync_lock_test_and_set( target, value );
-  }
-  
-  inline I32 atomicExchangeAdd( volatile I32* target, I32 value ){
-    return __sync_fetch_and_add( target, value );
-  }
-  
-  inline I32 atomicCompareExchange( volatile I32* target, I32 exchg, I32 old ){
-    return __sync_val_compare_and_swap( target, old, exchg );
-  }
-  
-  inline I32 atomicAdd( volatile I32* target, I32 value ){
-    return __sync_add_and_fetch( target, value );
-  }
-  
-  inline I32 atomicInc( volatile I32* target ){
-    return __sync_add_and_fetch( target, 1 );
-  }
-  
-  inline I32 atomicDec( volatile I32* target ){
-    return __sync_add_and_fetch( target, -1 );
-  }
-  
-}
-
-#endif
-
-
-
 ////////////////////////////////////////////////////////////////////////////////

File engine/include/Mocha/MochaResource.h

     virtual ~Resource();
     
     // Enter 
-    void enterCriticalState();
+    void lock();
     
     // Leave
-    void leaveCriticalState();
+    void unlock();
     
     // Query Critical Count
-    I32 getCriticalStateCount()const;
+    I32 getLockCount()const;
     
     // Get Content State
     CONTENT_STATE getContentState()const;
   private:
     U32 m_Name;
     U32 m_Kind;
-    volatile I32 m_CriticalCount;
+    volatile I32 m_LockCount;
     volatile I32 m_ContentState;
   };
   

File engine/include/Mocha/MochaString.h

     //
     // 
     //
-    static inline bool isAlpha( char ch ){
+    static inline bool isAlpha( C8 ch ){
       return ( ch >= 'A' && ch <= 'Z' ) || ( ch >= 'a' && ch <= 'z' );
     }
     
     //
     //
     //
-    static inline bool isDigit( char ch ){
+    static inline bool isDigit( C8 ch ){
       return ( ch >= '0' && ch <= '9' );
     }
     
     //
     //
     //
-    static inline bool isHexDigit( char ch ){
+    static inline bool isHexDigit( C8 ch ){
       return isDigit(ch) || (ch >='a' && ch<='f') || (ch >='A' && ch<='F');
     }
     
     //
     //
     //
-    static inline char toLower( char ch ){
+    static inline C8 toLower( C8 ch ){
       if(ch>='A' && ch<='Z')
-        return (char)(ch + ((int)'a'-(int)'A'));
+        return (C8)(ch + ((int)'a'-(int)'A'));
       return ch;
     }
     
     //
     //
     //
-    static inline char toUpper( char ch ){
+    static inline C8 toUpper( C8 ch ){
       if(ch>='a' && ch<='z')
-        return (char)(ch + ((int)'A'-(int)'a'));
+        return (C8)(ch + ((int)'A'-(int)'a'));
       return ch;
     }
     
     
     //
-    // the first unicode char that str has
+    // the first unicode C8 that str has
     //
-    static U32 firstChar( const char* str );
+    static U32 firstChar( const C8* str );
     
     //
-    // bytes of the first unicode char that str has
+    // bytes of the first unicode C8 that str has
     //
-    static std::size_t firstCharBytes( const char* str );
+    static std::size_t firstCharBytes( const C8* str );
     
     //
     // number of bytes that str has (NUL is not included)
     //
-    static std::size_t byteLength( const char* str );
+    static std::size_t byteLength( const C8* str );
     
     //
     // number of unicode chars that str has (NUL is not included)
     //
-    static std::size_t charLength( const char* str );
+    static std::size_t charLength( const C8* str );
     
     //
     // Copy src to string dst of size siz.  At most siz-1 characters
     // Returns strlen(src); if retval >= siz, truncation occurred.
     // (usage: strlcpy)
     //
-    static std::size_t copy( char* dst, const char* src, std::size_t siz );
+    static std::size_t copy( C8* dst, const C8* src, std::size_t siz );
     
     //
     // Appends src to string dst of size siz (unlike strncat, siz is the
     // If retval >= siz, truncation occurred.
     // (usage: strlcat)
     //
-    static std::size_t concate( char* dst, const char* src, std::size_t siz );
+    static std::size_t concate( C8* dst, const C8* src, std::size_t siz );
     
     //
-    // Find the first occurance char
+    // Find the first occurance C8
     //
-    static const char* findFirstChar( const char* str, char ch );
-    static const char* findFirstChar( const char* str, const char* str2 );
+    static const C8* findFirstChar( const C8* str, C8 ch );
+    static const C8* findFirstChar( const C8* str, const C8* str2 );
     
     //
-    // Find the last occurance char
+    // Find the last occurance C8
     //
-    static const char* findLastChar( const char* str, char ch );
+    static const C8* findLastChar( const C8* str, C8 ch );
     
     // 
     // Find the first occurance of pat in str
     //
-    static const char* findStr( const char* str, const char* pat );
+    static const C8* findStr( const C8* str, const C8* pat );
     
     //
     // Compare the two string
     //
-    static int compare( const char* s1, const char* s2, std::size_t n );
-    static int compare( const char* s1, const char* s2 );
+    static int compare( const C8* s1, const C8* s2, std::size_t n );
+    static int compare( const C8* s1, const C8* s2 );
     
     //
     // Tokenize the string s with characters in the string delim
     // (usage: strtok_r)
     //
-    static char* token( char* s, const char* delim, char** last );
+    static C8* token( C8* s, const C8* delim, C8** last );
     
     //
     // Get String Hash Code
     //
-    static U32 hash( const char* s );
+    static U32 hash( const C8* s );
     
     
   private:

File engine/include/Mocha/MochaThread.h

+#pragma once
+
+#include "MochaPrerequisites.h"
+
+namespace Mocha{
+  
+  class MOCHA_EXPORT Thread{
+  public:
+    Thread();
+    ~Thread();
+    
+    
+    
+  private:
+    
+  };
+  
+}

File engine/source/MochaDataStream.cpp

 
 namespace Mocha{
   
+  InputStream::InputStream(){
+  }
   
+  InputStream::~InputStream(){
+  }
+  
+  bool InputStream::read
+  ( void* buffer, Size bufSize, Offset pos, Size& readSize )
+  {
+    return false;
+  }
+  
+  OutputStream::OutputStream(){
+  }
+  
+  OutputStream::~OutputStream(){
+  }
+  
+  bool OutputStream::write( const void* buffer, Size bufSize, Offset pos ){
+    return false;
+  }
   
 }
+
+#if MOCHA_SYSTEM == MOCHA_SYSTEM_WINDOWS
+#  include <io.h>
+#  define NOMINMAX
+#  define WIN32_LEAN_AND_MEAN
+#  include <Windows.h>
+
+#elif defined(_POSIX_VERSION)
+#  include <unistd.h>
+#  include <fcntl.h>
+#  define _read read
+#  define _write write
+#  define _close close
+
+#endif
+
+namespace Mocha{
+  
+  FileStream::FileStream()
+  :m_File(-1)
+  {
+  }
+  
+  FileStream::~FileStream(){
+    if(isOpen())
+      close();
+  }
+  
+  void FileStream::openAndRead( const char* filename ){
+    if(m_File < 0){
+      
+#if MOCHA_SYSTEM == MOCHA_SYSTEM_WINDOWS
+      _sopen_s( &m_File, filename, _O_RDONLY|_O_BINARY, _SH_DENYWR, 0 );
+      
+#elif defined(_POSIX_VERSION)
+      m_File = ::open( filename, O_RDONLY );
+      
+#endif
+      
+      if(m_File == -1){
+        std::perror("FileStream::openAndRead");
+      }
+    }
+  }
+  
+  void FileStream::createAndWrite( const char* filename ){
+    if(m_File < 0){
+      
+#if MOCHA_SYSTEM == MOCHA_SYSTEM_WINDOWS
+      _sopen_s( &m_File, filename, _O_CREAT|_O_BINARY|_O_WRONLY|_O_TRUNC, 
+               _SH_DENYRW, _S_IREAD | _S_IWRITE );
+      
+#elif defined(_POSIX_VERSION)
+      m_File = ::open( filename, O_WRONLY | O_CREAT | O_TRUNC, 0777 );
+      
+#endif
+      
+      if(m_File == -1){
+        std::perror("FileStream::createAndWrite");
+      }
+    }
+  }
+  
+  void FileStream::close(){
+    if(m_File >= 0){
+      if(::_close( m_File ) == -1){
+        std::perror("FileStream::close");
+      }
+      m_File = -1;
+    }
+  }
+  
+  bool FileStream::isOpen()const{
+    return m_File >= 0;
+  }
+  
+  bool FileStream::read( void* buffer, Size bufSize, Size& readSize ){
+    if(!isOpen())
+      return false;
+    
+    const ::ssize_t ret = ::_read( m_File, buffer, bufSize );
+    if(ret == -1)
+      return false;
+    
+    readSize = ret;
+    return true;
+  }
+  
+  bool FileStream::read
+  ( void* buffer, Size bufSize, Offset pos, Size& readSize )
+  {
+    if(!isOpen())
+      return false;
+    
+#if defined(_POSIX_VERSION)
+    const ssize_t ret = ::pread( m_File, buffer, bufSize, pos );
+    
+#elif MOCHA_SYSTEM == MOCHA_SYSTEM_WINDOWS
+    ::HANDLE hFile = (::HANDLE)_get_osfhandle( m_File );
+    if(hFile == INVALID_HANDLE_VALUE)
+      return false;
+    
+    ::DWORD bytesRead = 0;
+    
+    ::OVERLAPPED overlapped = {0};
+    overlapped.Offset = pos & 0xffffffffUL;
+    overlapped.OffsetHigh = (pos >> 32) & 0xffffffffUL;
+    
+    ::BOOL ok = ::ReadFile( hFile, buffer, bufSize, &bytesRead, &overlapped );
+    // GetOverlappedResult can wait
+    
+    ::ssize_t ret = -1;
+    if(ok){
+      ret = bytesRead;
+    }
+    
+#endif
+    
+    if(ret == -1)
+      return false;
+    
+    readSize = ret;
+    return true;
+  }
+  
+  bool FileStream::write( const void* buffer, Size bufSize ){
+    if(!isOpen())
+      return false;
+    
+    const ::ssize_t ret = ::_write( m_File, buffer, bufSize );
+    if(ret == -1)
+      return false;
+    
+    return true;
+  }
+  
+  bool FileStream::write( const void* buffer, Size bufSize, Offset pos ){
+    if(!isOpen())
+      return false;
+    
+#if defined(_POSIX_VERSION)
+    const ssize_t ret = ::pwrite( m_File, buffer, bufSize, pos );
+    
+#elif MOCHA_SYSTEM == MOCHA_SYSTEM_WINDOWS
+    ::HANDLE hFile = (::HANDLE)_get_osfhandle( m_File );
+    if(hFile == INVALID_HANDLE_VALUE)
+      return false;
+    
+    ::DWORD bytesWtn = 0;
+    
+    ::OVERLAPPED overlapped = {0};
+    overlapped.Offset = pos & 0xffffffffUL;
+    overlapped.OffsetHigh = (pos >> 32) & 0xffffffffUL;
+    
+    ::BOOL ok = ::WriteFile( hFile, buffer, bufSize, &bytesWtn, &overlapped );
+    
+    ::ssize_t ret = -1;
+    if(ok){
+      ret = bytesWtn;
+    }
+    
+#endif
+    
+    if(ret == -1)
+      return false;
+    
+    return true;
+  }
+  
+}

File engine/source/MochaObject.cpp

   }
   
   void Object::retain(){
-    const I32 value = atomicInc(&m_RefCount);
+    const I32 value = Atomic::increase(&m_RefCount);
     if(value < 2){
       Log::cerr().fatal() << "Object{" << this << "}." << 
       "retain(): m_RefCount should be 1.";
   }
   
   void Object::release(){
-    const I32 value = atomicDec(&m_RefCount);
+    const I32 value = Atomic::decrease(&m_RefCount);
     if( value < 0 ){
       Log::cerr().fatal() << "Object{" << this << "}." << 
       "release(): m_RefCount should not be 0.";

File engine/source/MochaResource.cpp

   
   Resource::Resource( U32 name, U32 kind )
   :m_Name(name), m_Kind(kind),
-  m_CriticalCount(0), m_ContentState(STATE_UNLOADED)
+  m_LockCount(0), m_ContentState(STATE_UNLOADED)
   {
   }
   
   Resource::~Resource(){
-    if( m_CriticalCount != 0 ){
+    if( m_LockCount != 0 ){
       Log::cerr().fatal() << "Resource{" << this << "}." << 
-      "~Resource(): m_CriticalCount should be 0.";
+      "~Resource(): m_LockCount should be 0.";
     }
     if( m_ContentState != STATE_UNLOADED ){
       Log::cerr().fatal() << "Resource{" << this << "}." << 
     }
   }
   
-  void Resource::enterCriticalState(){
+  void Resource::lock(){
     if( m_ContentState != STATE_LOADED ){
       Log::cerr().fatal() << "Resource{" << this << "}." << 
       "enterCriticalState(): content should be loaded.";
     }
     
-    atomicInc(&m_CriticalCount);
+    Atomic::increase(&m_LockCount);
   }
   
-  void Resource::leaveCriticalState(){
-    const I32 value = atomicDec(&m_CriticalCount);
+  void Resource::unlock(){
+    const I32 value = Atomic::decrease(&m_LockCount);
     if( value < 0 ){
       Log::cerr().fatal() << "Resource{" << this << "}." << 
-      "leaveCriticalState(): m_CriticalCount initially should >= 1.";
+      "leaveCriticalState(): m_LockCount initially should >= 1.";
     }
   }
   
-  I32 Resource::getCriticalStateCount()const{
-    return m_CriticalCount;
+  I32 Resource::getLockCount()const{
+    return m_LockCount;
   }
   
   Resource::CONTENT_STATE Resource::getContentState()const{
   }
   
   bool Resource::prepare(){
-    if( m_CriticalCount != 0 ){
+    if( m_LockCount != 0 ){
       Log::cerr().fatal() << "Resource{" << this << "}." << 
-      "prepare(): m_CriticalCount should be 0.";
+      "prepare(): m_LockCount should be 0.";
     }
     
-    if(atomicCompareExchange(&m_ContentState, STATE_PREPARING, STATE_UNLOADED)
+    if(Atomic::compareExchange(&m_ContentState, STATE_PREPARING, STATE_UNLOADED)
        != STATE_UNLOADED)
       return false;
     
       result = _prepareImpl();
     }
     if(result)
-      atomicExchange(&m_ContentState, STATE_PREPARED);
+      Atomic::exchange(&m_ContentState, STATE_PREPARED);
     else{
       Log::cerr().fatal() << "Resource{" << this << "}." << 
       "prepare(): failed to prepare.";
   }
   
   bool Resource::load(){
-    if( m_CriticalCount != 0 ){
+    if( m_LockCount != 0 ){
       Log::cerr().fatal() << "Resource{" << this << "}." << 
-      "load(): m_CriticalCount should be 0.";
+      "load(): m_LockCount should be 0.";
     }
     
-    if(atomicCompareExchange(&m_ContentState, STATE_LOADING, STATE_PREPARED)
+    if(Atomic::compareExchange(&m_ContentState, STATE_LOADING, STATE_PREPARED)
        != STATE_PREPARED )
       return false;
     
     }
     
     if(result)
-      atomicExchange(&m_ContentState, STATE_LOADED);
+      Atomic::exchange(&m_ContentState, STATE_LOADED);
     else{
       Log::cerr().fatal() << "Resource{" << this << "}." << 
       "load(): failed to load.";
   }
   
   bool Resource::unload(){
-    if( m_CriticalCount != 0 ){
+    if( m_LockCount != 0 ){
       Log::cerr().fatal() << "Resource{" << this << "}." << 
-      "unload(): m_CriticalCount should be 0.";
+      "unload(): m_LockCount should be 0.";
     }
     
     I32 oldState = m_ContentState;
        oldState != STATE_PREPARED)
       return false;
     
-    if(atomicCompareExchange(&m_ContentState, STATE_UNLOADING, oldState)
+    if(Atomic::compareExchange(&m_ContentState, STATE_UNLOADING, oldState)
        != oldState )
       return false;
     
       }
     }
     
-    atomicExchange(&m_ContentState, STATE_UNLOADED);
+    Atomic::exchange(&m_ContentState, STATE_UNLOADED);
     
     return true;
   }

File engine/source/MochaString.cpp

   static const U32 _content_mask[] = { 0x7F, 0x1F, 0x0F, 0x07 };
   static const U32 _char_bytes[] = { 1, 2, 2, 3 };
   
-  static inline int _utf8char_class( const char* ch ){
-    const char c = *ch;
+  static inline int _utf8char_class( const C8* ch ){
+    const C8 c = *ch;
     
     for(int i=0;i<4;i++){
       if( (c & _prefix_mask[i]) == _prefix_value[i] )
     return -1;
   }
   
-  static inline std::size_t _utf8char_bytes( const char* ch ){
+  static inline std::size_t _utf8char_bytes( const C8* ch ){
     const int index = _utf8char_class(ch);
     if(index<0)
       return 0;
     return _char_bytes[ index ];
   }
   
-  static inline U32 _utf8char_value( const char* ch ){
+  static inline U32 _utf8char_value( const C8* ch ){
     int index = _utf8char_class(ch);
     if(index<0)
       return UTF8String::BADCODE;
   
   // MARK: Public
   
-  U32 UTF8String::firstChar( const char* str ){
+  U32 UTF8String::firstChar( const C8* str ){
     return _utf8char_value(str);
   }
   
-  std::size_t UTF8String::firstCharBytes( const char* str ){
+  std::size_t UTF8String::firstCharBytes( const C8* str ){
     return _utf8char_bytes(str);
   }
   
-  std::size_t UTF8String::byteLength( const char* str ){
-    const char *s;
+  std::size_t UTF8String::byteLength( const C8* str ){
+    const C8 *s;
     for (s = str; *s; ++s);
     return (s - str);
   }
   
-  std::size_t UTF8String::charLength( const char* str ){
+  std::size_t UTF8String::charLength( const C8* str ){
     std::size_t result = 0;
     
-    for(const char* p = str; *p; ){
+    for(const C8* p = str; *p; ){
       
       const int index = _utf8char_class(p);
       if(index<0)
     return result;
   }
   
-  std::size_t UTF8String::copy( char* dst, const char* src, std::size_t siz ){
-    char* d = dst;
-    const char* s = src;
+  std::size_t UTF8String::copy( C8* dst, const C8* src, std::size_t siz ){
+    C8* d = dst;
+    const C8* s = src;
     std::size_t n = siz;
     
     /* Copy as many bytes as will fit */
   }
   
   std::size_t UTF8String::concate
-  ( char* dst, const char* src, std::size_t siz )
+  ( C8* dst, const C8* src, std::size_t siz )
   {
-    char* d = dst;
-    const char* s = src;
+    C8* d = dst;
+    const C8* s = src;
     std::size_t n = siz, dlen;
     
     /* Find the end of dst and adjust bytes left but don't go past end */
     /* count does not include NUL */
   }
   
-  const char* UTF8String::findFirstChar( const char* str, char ch ){
-    char c = ch;
-    const char*p = str;
+  const C8* UTF8String::findFirstChar( const C8* str, C8 ch ){
+    C8 c = ch;
+    const C8*p = str;
 	for (;; ++p) {
       if (*p == c)
         return (p);
     return NULL;
   }
   
-  const char* UTF8String::findFirstChar( const char* str, const char* str2 ){
-    const char *scanp;
+  const C8* UTF8String::findFirstChar( const C8* str, const C8* str2 ){
+    const C8 *scanp;
     int c, sc;
     
     while ((c = *str++) != 0) {
     return (NULL);
   }
   
-  const char* UTF8String::findLastChar( const char* str, char ch ){
-    char c = ch;
-    const char*p = str;
-	for (const char* save = NULL;; ++p){
+  const C8* UTF8String::findLastChar( const C8* str, C8 ch ){
+    C8 c = ch;
+    const C8*p = str;
+	for (const C8* save = NULL;; ++p){
       if (*p == c)
         save = p;
       if (*p == 0)
     return NULL;
   }
   
-  const char* UTF8String::findStr( const char* str, const char* pat ){
-    char c, sc;
+  const C8* UTF8String::findStr( const C8* str, const C8* pat ){
+    C8 c, sc;
     std::size_t len;
     
     if ((c = *pat++) != 0) {
     return (str);
   }
   
-  int UTF8String::compare( const char* s1, const char* s2, std::size_t n ){
+  int UTF8String::compare( const C8* s1, const C8* s2, std::size_t n ){
     if (n == 0)
       return (0);
     do {
       if (*s1 != *s2++)
-        return (*(const unsigned char *)s1 - *(const unsigned char *)(s2-1));
+        return (*(const unsigned C8 *)s1 - *(const unsigned C8 *)(s2-1));
       if (*s1++ == 0)
         break;
     } while (--n != 0);
     return (0);
   }
   
-  int UTF8String::compare( const char* s1, const char* s2 ){
+  int UTF8String::compare( const C8* s1, const C8* s2 ){
     while (*s1 == *s2++)
       if (*s1++ == 0)
         return (0);
-	return (*(const unsigned char *)s1 - *(const unsigned char *)--s2);
+	return (*(const unsigned C8 *)s1 - *(const unsigned C8 *)--s2);
   }
   
-  char* UTF8String::token( char* s, const char* delim, char** last ){
-    char *spanp, *tok;
+  C8* UTF8String::token( C8* s, const C8* delim, C8** last ){
+    C8 *spanp, *tok;
 	int c, sc;
     
 	if (s == NULL && (s = *last) == NULL)
 	 */
   cont:
 	c = *s++;
-	for (spanp = const_cast<char*>(delim); (sc = *spanp++) != 0;) {
+	for (spanp = const_cast<C8*>(delim); (sc = *spanp++) != 0;) {
       if (c == sc)
         goto cont;
 	}
      */
 	for (;;) {
       c = *s++;
-      spanp = const_cast<char*>(delim);
+      spanp = const_cast<C8*>(delim);
       do {
         if ((sc = *spanp++) == c) {
           if (c == 0)
     return NULL;
   }
   
-  U32 UTF8String::hash( const char* s ){
+  U32 UTF8String::hash( const C8* s ){
     return hashCRC32( s, byteLength(s) );
   }
   

File engine/source/MochaThread.cpp

+#include <Mocha/MochaThread.h>
+
+