OpenClonk
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 150 of file C4Surface.h.

Constructor & Destructor Documentation

◆ C4TexRef()

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

Definition at line 637 of file C4Surface.cpp.

638 {
639  // zero fields
640 #ifndef USE_CONSOLE
641  texName = 0;
642 #endif
643  texLock.pBits.reset(); fIntLock=false;
644  // store size
645  this->iSizeX=iSizeX;
646  this->iSizeY=iSizeY;
647  this->iFlags=iFlags;
648  // add to texture manager
649  if (!pTexMgr) pTexMgr = new C4TexMgr();
650  pTexMgr->RegTex(this);
651  // create texture: check ddraw
652  if (!pDraw) return;
653  if (!pDraw->DeviceReady()) return;
654  // create it!
655  // Reserve video memory
656  CreateTexture();
657 
658  if ((iFlags & C4SF_Unlocked) == 0 && pDraw)
659  {
660  texLock.pBits = std::make_unique<unsigned char[]>(iSizeX * iSizeY * C4Draw::COLOR_DEPTH_BYTES);
662  memset(texLock.pBits.get(), 0x00, texLock.Pitch*iSizeY);
663  // Always locked
664  LockSize.x = LockSize.y = 0;
666  }
667 }
C4Draw * pDraw
Definition: C4Draw.cpp:42
C4TexMgr * pTexMgr
Definition: C4Surface.cpp:879
std::unique_ptr< unsigned char[]> pBits
Definition: C4Surface.h:146
const int C4SF_Unlocked
Definition: C4Surface.h:51
static constexpr int COLOR_DEPTH_BYTES
Definition: C4Draw.h:90
virtual bool DeviceReady()=0
int32_t y
Definition: C4Rect.h:30
int32_t Hgt
Definition: C4Rect.h:30
int32_t Wdt
Definition: C4Rect.h:30
int32_t x
Definition: C4Rect.h:30
void RegTex(C4TexRef *pTex)
Definition: C4Surface.cpp:825
unsigned int texName
Definition: C4Surface.h:155
friend class C4TexMgr
Definition: C4Surface.h:178
int iFlags
Definition: C4Surface.h:160
C4Rect LockSize
Definition: C4Surface.h:161
LOCKED_RECT texLock
Definition: C4Surface.h:153
bool fIntLock
Definition: C4Surface.h:159
int iSizeX
Definition: C4Surface.h:157
int iSizeY
Definition: C4Surface.h:158

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.

Here is the call graph for this function:

◆ ~C4TexRef()

C4TexRef::~C4TexRef ( )

Definition at line 669 of file C4Surface.cpp.

670 {
671  fIntLock=false;
672  // free texture
673 #ifndef USE_CONSOLE
674  if (pGL && pGL->pCurrCtx) glDeleteTextures(1, &texName);
675 #endif
676  if (pDraw) texLock.pBits = nullptr;
677  // remove from texture manager
678  pTexMgr->UnregTex(this);
679 }
CStdGL * pGL
Definition: C4DrawGL.cpp:907
void UnregTex(C4TexRef *pTex)
Definition: C4Surface.cpp:831
CStdGLCtx * pCurrCtx
Definition: C4DrawGL.h:180

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

Here is the call graph for this function:

Member Function Documentation

◆ ClearRect()

bool C4TexRef::ClearRect ( C4Rect rtClear)

Definition at line 781 of file C4Surface.cpp.

782 {
783  // ensure locked
784  if (!LockForUpdate(rtClear)) return false;
785  // clear pixels
786  int y;
787  for (y = rtClear.y; y < rtClear.y + rtClear.Hgt; ++y)
788  {
789  for (int x = rtClear.x; x < rtClear.x + rtClear.Wdt; ++x)
790  SetPix(x, y, 0x00000000);
791  }
792  // success
793  return true;
794 }
bool LockForUpdate(C4Rect &rtUpdate)
Definition: C4Surface.cpp:700
void SetPix(int iX, int iY, DWORD v)
Definition: C4Surface.h:172

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

Here is the call graph for this function:

◆ FillBlack()

bool C4TexRef::FillBlack ( )

Definition at line 796 of file C4Surface.cpp.

797 {
798  // ensure locked
799  if (!Lock()) return false;
800  // clear pixels
801  int y;
802  for (y=0; y<iSizeY; ++y)
803  {
804  for (int x = 0; x < iSizeX; ++x)
805  SetPix(x, y, 0xff000000);
806  }
807  // success
808  return true;
809 }
bool Lock()
Definition: C4Surface.cpp:728

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

Here is the call graph for this function:

◆ Lock()

bool C4TexRef::Lock ( )

Definition at line 728 of file C4Surface.cpp.

729 {
730  // already locked?
731  if (texLock.pBits) return true;
733  LockSize.x = LockSize.y = 0;
734  // lock
735 #ifndef USE_CONSOLE
736  if (texName)
737  {
738  if (!pGL->pCurrCtx) return false;
739  // get texture
740  texLock.pBits = std::make_unique<unsigned char[]>(iSizeX * iSizeY * C4Draw::COLOR_DEPTH_BYTES);
742  glBindTexture(GL_TEXTURE_2D, texName);
743  glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, texLock.pBits.get());
744  return true;
745  }
746 #endif
747  {
748  // nothing to do
749  }
750  // failure
751  return false;
752 }

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().

Here is the caller graph for this function:

◆ LockForUpdate()

bool C4TexRef::LockForUpdate ( C4Rect rtUpdate)

Definition at line 700 of file C4Surface.cpp.

701 {
702  // already locked?
703  if (texLock.pBits)
704  {
705  // fully locked
706  if (LockSize.x == 0 && LockSize.Wdt == iSizeX && LockSize.y == 0 && LockSize.Hgt == iSizeY)
707  {
708  return true;
709  }
710  else
711  {
712  // Commit previous changes to the texture
713  Unlock();
714  }
715  }
716  // lock
717 #ifndef USE_CONSOLE
718  // prepare texture data
719  texLock.pBits = std::make_unique<unsigned char[]>(rtUpdate.Wdt * rtUpdate.Hgt * C4Draw::COLOR_DEPTH_BYTES);
721  LockSize = rtUpdate;
722  return true;
723 #endif
724  // failure
725  return false;
726 }
void Unlock()
Definition: C4Surface.cpp:754

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetPix()

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

Definition at line 172 of file C4Surface.h.

173  {
174  *((DWORD *)(((BYTE *)texLock.pBits.get()) + (iY - LockSize.y) * texLock.Pitch + (iX - LockSize.x) * 4)) = v;
175  }
uint8_t BYTE
uint32_t DWORD

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

Referenced by ClearRect(), and FillBlack().

Here is the caller graph for this function:

◆ Unlock()

void C4TexRef::Unlock ( )

Definition at line 754 of file C4Surface.cpp.

755 {
756  // locked?
757  if (!texLock.pBits || fIntLock) return;
758 #ifndef USE_CONSOLE
759  if (!pGL->pCurrCtx)
760  {
761 // BREAKPOINT_HERE;
762  assert(pGL->pMainCtx);
763  pGL->pMainCtx->Select();
764  }
765 
766  const bool fMipMap = (iFlags & C4SF_MipMap) != 0;
767 
768  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
769 
770  // reuse the existing texture
771  glBindTexture(GL_TEXTURE_2D, texName);
772  glTexSubImage2D(GL_TEXTURE_2D, 0,
774  GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, texLock.pBits.get());
775 
776  texLock.pBits.reset();
777  if (fMipMap) glGenerateMipmap(GL_TEXTURE_2D);
778 #endif
779 }
const int C4SF_MipMap
Definition: C4Surface.h:50
virtual bool Select(bool verbose=false)
CStdGLCtx * pMainCtx
Definition: C4DrawGL.h:179

References C4SF_MipMap, 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().

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ C4TexMgr

friend class C4TexMgr
friend

Definition at line 178 of file C4Surface.h.

Referenced by C4TexRef().

Member Data Documentation

◆ fIntLock

bool C4TexRef::fIntLock

Definition at line 159 of file C4Surface.h.

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

◆ iFlags

int C4TexRef::iFlags

Definition at line 160 of file C4Surface.h.

Referenced by C4TexRef(), and Unlock().

◆ iSizeX

int C4TexRef::iSizeX

Definition at line 157 of file C4Surface.h.

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

◆ iSizeY

int C4TexRef::iSizeY

Definition at line 158 of file C4Surface.h.

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

◆ LockSize

C4Rect C4TexRef::LockSize

Definition at line 161 of file C4Surface.h.

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

◆ texLock

LOCKED_RECT C4TexRef::texLock

Definition at line 153 of file C4Surface.h.

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

◆ texName

unsigned int C4TexRef::texName

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