CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

rbuflock.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2004 by Jorrit Tyberghein
00003               (C) 2004 by Frank Richter
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_CSTOOL_RBUFLOCK_H__
00021 #define __CS_CSTOOL_RBUFLOCK_H__
00022 
00027 #include "csutil/ref.h"
00028 #include "ivideo/rndbuf.h"
00029 
00035 template <class T>
00036 class csRenderBufferLock
00037 {
00038   csRef<iRenderBuffer> buffer;
00039   csRenderBufferLockType lockType;
00040   bool isLocked;
00041   T* lockBuf;
00042   size_t bufOfs, bufStride;
00043   
00044   csRenderBufferLock() {}
00045 public:
00049   csRenderBufferLock (iRenderBuffer* buf, 
00050     csRenderBufferLockType lock = CS_BUF_LOCK_NORMAL) : buffer(buf),
00051     lockType(lock), isLocked(false), lockBuf(0), bufOfs(buf->GetOffset()),
00052     bufStride(buf->GetElementDistance())
00053   {
00054   }
00055   
00059   ~csRenderBufferLock()
00060   {
00061     Unlock();
00062   }
00063   
00068   T* Lock ()
00069   {
00070     if (!isLocked)
00071     {
00072       lockBuf = (T*)((uint8*)buffer->Lock (lockType) + bufOfs);
00073       isLocked = true;
00074     }
00075     return lockBuf;
00076   }
00077   
00079   void Unlock ()
00080   {
00081     if (isLocked)
00082     {
00083       buffer->Release();
00084       isLocked = false;
00085     }
00086   }
00087   
00092   operator T* ()
00093   {
00094     return Lock();
00095   }
00096 
00098   T& operator [] (size_t n)
00099   {
00100     return Get (n);
00101   }
00102 
00104   T& Get (size_t n)
00105   {
00106     return *((T*)((uint8*)Lock() + n * bufStride));
00107   }
00108   
00110   size_t GetSize() const
00111   {
00112     return buffer->GetElementCount();
00113   }
00114 };
00115 
00116 #endif // __CS_CSTOOL_RBUFLOCK_H__

Generated for Crystal Space by doxygen 1.4.4