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

Constructor & Destructor Documentation

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

Definition at line 637 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.

638 {
639  // zero fields
640 #ifndef USE_CONSOLE
641  texName = 0;
642 #endif
643  texLock.pBits=nullptr; 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 = new unsigned char[iSizeX * iSizeY * C4Draw::COLOR_DEPTH_BYTES];
662  memset(texLock.pBits, 0x00, texLock.Pitch*iSizeY);
663  // Always locked
664  LockSize.x = LockSize.y = 0;
666  }
667 }
static constexpr int COLOR_DEPTH_BYTES
Definition: C4Draw.h:90
virtual bool DeviceReady()=0
void RegTex(C4TexRef *pTex)
Definition: C4Surface.cpp:826
int iSizeY
Definition: C4Surface.h:158
unsigned char * pBits
Definition: C4Surface.h:146
int iSizeX
Definition: C4Surface.h:157
const int C4SF_Unlocked
Definition: C4Surface.h:51
int32_t Wdt
Definition: C4Rect.h:30
C4TexMgr * pTexMgr
Definition: C4Surface.cpp:880
int iFlags
Definition: C4Surface.h:160
int32_t y
Definition: C4Rect.h:30
bool fIntLock
Definition: C4Surface.h:159
C4Draw * pDraw
Definition: C4Draw.cpp:42
LOCKED_RECT texLock
Definition: C4Surface.h:153
unsigned int texName
Definition: C4Surface.h:155
int32_t x
Definition: C4Rect.h:30
C4Rect LockSize
Definition: C4Surface.h:161
int32_t Hgt
Definition: C4Rect.h:30
friend class C4TexMgr
Definition: C4Surface.h:178

Here is the call graph for this function:

C4TexRef::~C4TexRef ( )

Definition at line 669 of file C4Surface.cpp.

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

670 {
671  fIntLock=false;
672  // free texture
673 #ifndef USE_CONSOLE
674  if (pGL && pGL->pCurrCtx) glDeleteTextures(1, &texName);
675 #endif
676  if (pDraw) delete [] static_cast<unsigned char*>(texLock.pBits); texLock.pBits = nullptr;
677  // remove from texture manager
678  pTexMgr->UnregTex(this);
679 }
CStdGLCtx * pCurrCtx
Definition: C4DrawGL.h:179
unsigned char * pBits
Definition: C4Surface.h:146
C4TexMgr * pTexMgr
Definition: C4Surface.cpp:880
bool fIntLock
Definition: C4Surface.h:159
C4Draw * pDraw
Definition: C4Draw.cpp:42
void UnregTex(C4TexRef *pTex)
Definition: C4Surface.cpp:832
LOCKED_RECT texLock
Definition: C4Surface.h:153
unsigned int texName
Definition: C4Surface.h:155
CStdGL * pGL
Definition: C4DrawGL.cpp:905

Here is the call graph for this function:

Member Function Documentation

bool C4TexRef::ClearRect ( C4Rect rtClear)

Definition at line 782 of file C4Surface.cpp.

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

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

Here is the call graph for this function:

bool C4TexRef::FillBlack ( )

Definition at line 797 of file C4Surface.cpp.

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

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

Here is the call graph for this function:

bool C4TexRef::Lock ( )

Definition at line 728 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().

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 = new 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);
744  return true;
745  }
746 #endif
747  {
748  // nothing to do
749  }
750  // failure
751  return false;
752 }
static constexpr int COLOR_DEPTH_BYTES
Definition: C4Draw.h:90
CStdGLCtx * pCurrCtx
Definition: C4DrawGL.h:179
int iSizeY
Definition: C4Surface.h:158
unsigned char * pBits
Definition: C4Surface.h:146
int iSizeX
Definition: C4Surface.h:157
int32_t Wdt
Definition: C4Rect.h:30
int32_t y
Definition: C4Rect.h:30
LOCKED_RECT texLock
Definition: C4Surface.h:153
unsigned int texName
Definition: C4Surface.h:155
int32_t x
Definition: C4Rect.h:30
C4Rect LockSize
Definition: C4Surface.h:161
CStdGL * pGL
Definition: C4DrawGL.cpp:905
int32_t Hgt
Definition: C4Rect.h:30

Here is the caller graph for this function:

bool C4TexRef::LockForUpdate ( C4Rect rtUpdate)

Definition at line 700 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().

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 = new unsigned char[rtUpdate.Wdt * rtUpdate.Hgt * C4Draw::COLOR_DEPTH_BYTES];
721  LockSize = rtUpdate;
722  return true;
723 #endif
724  // failure
725  return false;
726 }
static constexpr int COLOR_DEPTH_BYTES
Definition: C4Draw.h:90
int iSizeY
Definition: C4Surface.h:158
unsigned char * pBits
Definition: C4Surface.h:146
int iSizeX
Definition: C4Surface.h:157
int32_t Wdt
Definition: C4Rect.h:30
int32_t y
Definition: C4Rect.h:30
LOCKED_RECT texLock
Definition: C4Surface.h:153
int32_t x
Definition: C4Rect.h:30
C4Rect LockSize
Definition: C4Surface.h:161
int32_t Hgt
Definition: C4Rect.h:30
void Unlock()
Definition: C4Surface.cpp:754

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

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

Referenced by ClearRect(), and FillBlack().

173  {
174  *((DWORD *)(((BYTE *)texLock.pBits) + (iY - LockSize.y) * texLock.Pitch + (iX - LockSize.x) * 4)) = v;
175  }
uint8_t BYTE
unsigned char * pBits
Definition: C4Surface.h:146
int32_t y
Definition: C4Rect.h:30
LOCKED_RECT texLock
Definition: C4Surface.h:153
int32_t x
Definition: C4Rect.h:30
C4Rect LockSize
Definition: C4Surface.h:161
uint32_t DWORD

Here is the caller graph for this function:

void C4TexRef::Unlock ( )

Definition at line 754 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().

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 fTileable = (iFlags & C4SF_Tileable) != 0;
767  const bool fMipMap = (iFlags & C4SF_MipMap) != 0;
768 
769  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
770 
771  // reuse the existing texture
772  glBindTexture(GL_TEXTURE_2D, texName);
773  glTexSubImage2D(GL_TEXTURE_2D, 0,
775  GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, texLock.pBits);
776 
777  delete[] static_cast<unsigned char*>(texLock.pBits); texLock.pBits=nullptr;
778  if (fMipMap) glGenerateMipmap(GL_TEXTURE_2D);
779 #endif
780 }
virtual bool Select(bool verbose=false)
CStdGLCtx * pCurrCtx
Definition: C4DrawGL.h:179
CStdGLCtx * pMainCtx
Definition: C4DrawGL.h:178
unsigned char * pBits
Definition: C4Surface.h:146
int32_t Wdt
Definition: C4Rect.h:30
int iFlags
Definition: C4Surface.h:160
int32_t y
Definition: C4Rect.h:30
bool fIntLock
Definition: C4Surface.h:159
const int C4SF_Tileable
Definition: C4Surface.h:49
const int C4SF_MipMap
Definition: C4Surface.h:50
LOCKED_RECT texLock
Definition: C4Surface.h:153
unsigned int texName
Definition: C4Surface.h:155
int32_t x
Definition: C4Rect.h:30
C4Rect LockSize
Definition: C4Surface.h:161
CStdGL * pGL
Definition: C4DrawGL.cpp:905
int32_t Hgt
Definition: C4Rect.h:30

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

Referenced by C4TexRef().

Member Data Documentation

bool C4TexRef::fIntLock

Definition at line 159 of file C4Surface.h.

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

int C4TexRef::iFlags

Definition at line 160 of file C4Surface.h.

Referenced by C4TexRef(), and Unlock().

int C4TexRef::iSizeX

Definition at line 157 of file C4Surface.h.

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

int C4TexRef::iSizeY

Definition at line 158 of file C4Surface.h.

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

C4Rect C4TexRef::LockSize

Definition at line 161 of file C4Surface.h.

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

LOCKED_RECT C4TexRef::texLock

Definition at line 153 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: