OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4TexRef Class Reference

#include <C4Surface.h>

Collaboration diagram for C4TexRef:
[legend]

Public Member Functions

 C4TexRef (int iSizeX, int iSizeY, int iFlags)
 
 ~C4TexRef ()
 
bool Lock ()
 
bool LockForUpdate (C4Rect &rtUpdate)
 
void Unlock ()
 
bool ClearRect (C4Rect &rtClear)
 
bool FillBlack ()
 
void SetPix (int iX, int iY, DWORD v)
 

Public Attributes

LOCKED_RECT texLock
 
unsigned int texName
 
int iSizeX
 
int iSizeY
 
bool fIntLock
 
int iFlags
 
C4Rect LockSize
 

Friends

class C4TexMgr
 

Detailed Description

Definition at line 152 of file C4Surface.h.

Constructor & Destructor Documentation

C4TexRef::C4TexRef ( int  iSizeX,
int  iSizeY,
int  iFlags 
)

Definition at line 647 of file C4Surface.cpp.

References C4SF_Unlocked, C4TexMgr, C4Draw::COLOR_DEPTH_BYTES, C4Draw::DeviceReady(), fIntLock, C4Rect::Hgt, iFlags, iSizeX, iSizeY, LockSize, _LOCKED_RECT::pBits, pDraw, _LOCKED_RECT::Pitch, pTexMgr, C4TexMgr::RegTex(), texLock, texName, C4Rect::Wdt, C4Rect::x, and C4Rect::y.

648 {
649  // zero fields
650 #ifndef USE_CONSOLE
651  texName = 0;
652 #endif
653  texLock.pBits=nullptr; fIntLock=false;
654  // store size
655  this->iSizeX=iSizeX;
656  this->iSizeY=iSizeY;
657  this->iFlags=iFlags;
658  // add to texture manager
659  if (!pTexMgr) pTexMgr = new C4TexMgr();
660  pTexMgr->RegTex(this);
661  // create texture: check ddraw
662  if (!pDraw) return;
663  if (!pDraw->DeviceReady()) return;
664  // create it!
665  // Reserve video memory
666  CreateTexture();
667 
668  if ((iFlags & C4SF_Unlocked) == 0 && pDraw)
669  {
670  texLock.pBits = new unsigned char[iSizeX * iSizeY * C4Draw::COLOR_DEPTH_BYTES];
672  memset(texLock.pBits, 0x00, texLock.Pitch*iSizeY);
673  // Always locked
674  LockSize.x = LockSize.y = 0;
676  }
677 }
static constexpr int COLOR_DEPTH_BYTES
Definition: C4Draw.h:90
virtual bool DeviceReady()=0
void RegTex(C4TexRef *pTex)
Definition: C4Surface.cpp:836
int iSizeY
Definition: C4Surface.h:160
unsigned char * pBits
Definition: C4Surface.h:148
int iSizeX
Definition: C4Surface.h:159
const int C4SF_Unlocked
Definition: C4Surface.h:53
int32_t Wdt
Definition: C4Rect.h:32
C4TexMgr * pTexMgr
Definition: C4Surface.cpp:890
int iFlags
Definition: C4Surface.h:162
int32_t y
Definition: C4Rect.h:32
bool fIntLock
Definition: C4Surface.h:161
C4Draw * pDraw
Definition: C4Draw.cpp:45
LOCKED_RECT texLock
Definition: C4Surface.h:155
unsigned int texName
Definition: C4Surface.h:157
int32_t x
Definition: C4Rect.h:32
C4Rect LockSize
Definition: C4Surface.h:163
int32_t Hgt
Definition: C4Rect.h:32
friend class C4TexMgr
Definition: C4Surface.h:180

Here is the call graph for this function:

C4TexRef::~C4TexRef ( )

Definition at line 679 of file C4Surface.cpp.

References fIntLock, _LOCKED_RECT::pBits, CStdGL::pCurrCtx, pDraw, pGL, pTexMgr, texLock, texName, and C4TexMgr::UnregTex().

680 {
681  fIntLock=false;
682  // free texture
683 #ifndef USE_CONSOLE
684  if (pGL && pGL->pCurrCtx) glDeleteTextures(1, &texName);
685 #endif
686  if (pDraw) delete [] static_cast<unsigned char*>(texLock.pBits); texLock.pBits = 0;
687  // remove from texture manager
688  pTexMgr->UnregTex(this);
689 }
CStdGLCtx * pCurrCtx
Definition: C4DrawGL.h:179
unsigned char * pBits
Definition: C4Surface.h:148
C4TexMgr * pTexMgr
Definition: C4Surface.cpp:890
bool fIntLock
Definition: C4Surface.h:161
C4Draw * pDraw
Definition: C4Draw.cpp:45
void UnregTex(C4TexRef *pTex)
Definition: C4Surface.cpp:842
LOCKED_RECT texLock
Definition: C4Surface.h:155
unsigned int texName
Definition: C4Surface.h:157
CStdGL * pGL
Definition: C4DrawGL.cpp:914

Here is the call graph for this function:

Member Function Documentation

bool C4TexRef::ClearRect ( C4Rect rtClear)

Definition at line 792 of file C4Surface.cpp.

References C4Rect::Hgt, LockForUpdate(), SetPix(), C4Rect::Wdt, C4Rect::x, and C4Rect::y.

793 {
794  // ensure locked
795  if (!LockForUpdate(rtClear)) return false;
796  // clear pixels
797  int y;
798  for (y = rtClear.y; y < rtClear.y + rtClear.Hgt; ++y)
799  {
800  for (int x = rtClear.x; x < rtClear.x + rtClear.Wdt; ++x)
801  SetPix(x, y, 0x00000000);
802  }
803  // success
804  return true;
805 }
bool LockForUpdate(C4Rect &rtUpdate)
Definition: C4Surface.cpp:710
void SetPix(int iX, int iY, DWORD v)
Definition: C4Surface.h:174
int32_t Wdt
Definition: C4Rect.h:32
int32_t y
Definition: C4Rect.h:32
int32_t x
Definition: C4Rect.h:32
int32_t Hgt
Definition: C4Rect.h:32

Here is the call graph for this function:

bool C4TexRef::FillBlack ( )

Definition at line 807 of file C4Surface.cpp.

References iSizeX, iSizeY, Lock(), and SetPix().

808 {
809  // ensure locked
810  if (!Lock()) return false;
811  // clear pixels
812  int y;
813  for (y=0; y<iSizeY; ++y)
814  {
815  for (int x = 0; x < iSizeX; ++x)
816  SetPix(x, y, 0xff000000);
817  }
818  // success
819  return true;
820 }
bool Lock()
Definition: C4Surface.cpp:738
void SetPix(int iX, int iY, DWORD v)
Definition: C4Surface.h:174
int iSizeY
Definition: C4Surface.h:160
int iSizeX
Definition: C4Surface.h:159

Here is the call graph for this function:

bool C4TexRef::Lock ( )

Definition at line 738 of file C4Surface.cpp.

References C4Draw::COLOR_DEPTH_BYTES, C4Rect::Hgt, iSizeX, iSizeY, LockSize, _LOCKED_RECT::pBits, CStdGL::pCurrCtx, pGL, _LOCKED_RECT::Pitch, texLock, texName, C4Rect::Wdt, C4Rect::x, and C4Rect::y.

Referenced by FillBlack(), and C4TexMgr::IntLock().

739 {
740  // already locked?
741  if (texLock.pBits) return true;
743  LockSize.x = LockSize.y = 0;
744  // lock
745 #ifndef USE_CONSOLE
746  if (texName)
747  {
748  if (!pGL->pCurrCtx) return false;
749  // get texture
750  texLock.pBits = new unsigned char[iSizeX * iSizeY * C4Draw::COLOR_DEPTH_BYTES];
752  glBindTexture(GL_TEXTURE_2D, texName);
753  glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, texLock.pBits);
754  return true;
755  }
756 #endif
757  {
758  // nothing to do
759  }
760  // failure
761  return false;
762 }
static constexpr int COLOR_DEPTH_BYTES
Definition: C4Draw.h:90
CStdGLCtx * pCurrCtx
Definition: C4DrawGL.h:179
int iSizeY
Definition: C4Surface.h:160
unsigned char * pBits
Definition: C4Surface.h:148
int iSizeX
Definition: C4Surface.h:159
int32_t Wdt
Definition: C4Rect.h:32
int32_t y
Definition: C4Rect.h:32
LOCKED_RECT texLock
Definition: C4Surface.h:155
unsigned int texName
Definition: C4Surface.h:157
int32_t x
Definition: C4Rect.h:32
C4Rect LockSize
Definition: C4Surface.h:163
CStdGL * pGL
Definition: C4DrawGL.cpp:914
int32_t Hgt
Definition: C4Rect.h:32

Here is the caller graph for this function:

bool C4TexRef::LockForUpdate ( C4Rect rtUpdate)

Definition at line 710 of file C4Surface.cpp.

References C4Draw::COLOR_DEPTH_BYTES, C4Rect::Hgt, iSizeX, iSizeY, LockSize, _LOCKED_RECT::pBits, _LOCKED_RECT::Pitch, texLock, Unlock(), C4Rect::Wdt, C4Rect::x, and C4Rect::y.

Referenced by ClearRect().

711 {
712  // already locked?
713  if (texLock.pBits)
714  {
715  // fully locked
716  if (LockSize.x == 0 && LockSize.Wdt == iSizeX && LockSize.y == 0 && LockSize.Hgt == iSizeY)
717  {
718  return true;
719  }
720  else
721  {
722  // Commit previous changes to the texture
723  Unlock();
724  }
725  }
726  // lock
727 #ifndef USE_CONSOLE
728  // prepare texture data
729  texLock.pBits = new unsigned char[rtUpdate.Wdt * rtUpdate.Hgt * C4Draw::COLOR_DEPTH_BYTES];
731  LockSize = rtUpdate;
732  return true;
733 #endif
734  // failure
735  return false;
736 }
static constexpr int COLOR_DEPTH_BYTES
Definition: C4Draw.h:90
int iSizeY
Definition: C4Surface.h:160
unsigned char * pBits
Definition: C4Surface.h:148
int iSizeX
Definition: C4Surface.h:159
int32_t Wdt
Definition: C4Rect.h:32
int32_t y
Definition: C4Rect.h:32
LOCKED_RECT texLock
Definition: C4Surface.h:155
int32_t x
Definition: C4Rect.h:32
C4Rect LockSize
Definition: C4Surface.h:163
int32_t Hgt
Definition: C4Rect.h:32
void Unlock()
Definition: C4Surface.cpp:764

Here is the call graph for this function:

Here is the caller graph for this function:

void C4TexRef::SetPix ( int  iX,
int  iY,
DWORD  v 
)
inline

Definition at line 174 of file C4Surface.h.

References _LOCKED_RECT::pBits, _LOCKED_RECT::Pitch, C4Rect::x, and C4Rect::y.

Referenced by ClearRect(), and FillBlack().

175  {
176  *((DWORD *)(((BYTE *)texLock.pBits) + (iY - LockSize.y) * texLock.Pitch + (iX - LockSize.x) * 4)) = v;
177  }
uint8_t BYTE
unsigned char * pBits
Definition: C4Surface.h:148
int32_t y
Definition: C4Rect.h:32
LOCKED_RECT texLock
Definition: C4Surface.h:155
int32_t x
Definition: C4Rect.h:32
C4Rect LockSize
Definition: C4Surface.h:163
uint32_t DWORD

Here is the caller graph for this function:

void C4TexRef::Unlock ( )

Definition at line 764 of file C4Surface.cpp.

References C4SF_MipMap, C4SF_Tileable, fIntLock, C4Rect::Hgt, iFlags, LockSize, _LOCKED_RECT::pBits, CStdGL::pCurrCtx, pGL, CStdGL::pMainCtx, CStdGLCtx::Select(), texLock, texName, C4Rect::Wdt, C4Rect::x, and C4Rect::y.

Referenced by C4TexMgr::IntUnlock(), and LockForUpdate().

765 {
766  // locked?
767  if (!texLock.pBits || fIntLock) return;
768 #ifndef USE_CONSOLE
769  if (!pGL->pCurrCtx)
770  {
771 // BREAKPOINT_HERE;
772  assert(pGL->pMainCtx);
773  pGL->pMainCtx->Select();
774  }
775 
776  const bool fTileable = (iFlags & C4SF_Tileable) != 0;
777  const bool fMipMap = (iFlags & C4SF_MipMap) != 0;
778 
779  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
780 
781  // reuse the existing texture
782  glBindTexture(GL_TEXTURE_2D, texName);
783  glTexSubImage2D(GL_TEXTURE_2D, 0,
785  GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, texLock.pBits);
786 
787  delete[] static_cast<unsigned char*>(texLock.pBits); texLock.pBits=nullptr;
788  if (fMipMap) glGenerateMipmap(GL_TEXTURE_2D);
789 #endif
790 }
virtual bool Select(bool verbose=false)
CStdGLCtx * pCurrCtx
Definition: C4DrawGL.h:179
CStdGLCtx * pMainCtx
Definition: C4DrawGL.h:178
unsigned char * pBits
Definition: C4Surface.h:148
int32_t Wdt
Definition: C4Rect.h:32
int iFlags
Definition: C4Surface.h:162
int32_t y
Definition: C4Rect.h:32
bool fIntLock
Definition: C4Surface.h:161
const int C4SF_Tileable
Definition: C4Surface.h:51
const int C4SF_MipMap
Definition: C4Surface.h:52
LOCKED_RECT texLock
Definition: C4Surface.h:155
unsigned int texName
Definition: C4Surface.h:157
int32_t x
Definition: C4Rect.h:32
C4Rect LockSize
Definition: C4Surface.h:163
CStdGL * pGL
Definition: C4DrawGL.cpp:914
int32_t Hgt
Definition: C4Rect.h:32

Here is the call graph for this function:

Here is the caller graph for this function:

Friends And Related Function Documentation

friend class C4TexMgr
friend

Definition at line 180 of file C4Surface.h.

Referenced by C4TexRef().

Member Data Documentation

bool C4TexRef::fIntLock

Definition at line 161 of file C4Surface.h.

Referenced by C4TexRef(), C4TexMgr::IntLock(), C4TexMgr::IntUnlock(), Unlock(), and ~C4TexRef().

int C4TexRef::iFlags

Definition at line 162 of file C4Surface.h.

Referenced by C4TexRef(), and Unlock().

int C4TexRef::iSizeX

Definition at line 159 of file C4Surface.h.

Referenced by C4Draw::BlitUnscaled(), C4TexRef(), FillBlack(), Lock(), and LockForUpdate().

int C4TexRef::iSizeY

Definition at line 160 of file C4Surface.h.

Referenced by C4Draw::BlitUnscaled(), C4TexRef(), FillBlack(), Lock(), and LockForUpdate().

C4Rect C4TexRef::LockSize

Definition at line 163 of file C4Surface.h.

Referenced by C4TexRef(), Lock(), LockForUpdate(), and Unlock().

LOCKED_RECT C4TexRef::texLock

Definition at line 155 of file C4Surface.h.

Referenced by C4TexRef(), C4TexMgr::IntLock(), Lock(), LockForUpdate(), Unlock(), and ~C4TexRef().

unsigned int C4TexRef::texName

The documentation for this class was generated from the following files: