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

#include <C4Surface.h>

Collaboration diagram for C4Surface:
[legend]

Public Member Functions

 C4Surface ()
 
 ~C4Surface ()
 
 C4Surface (int iWdt, int iHgt, int iFlags)
 
 C4Surface (C4AbstractApp *pApp, C4Window *pWindow)
 
void MoveFrom (C4Surface *psfcFrom)
 
bool IsRenderTarget ()
 
void SetBackground ()
 
int IsLocked () const
 
void ClearBoxDw (int iX, int iY, int iWdt, int iHgt)
 
bool Unlock ()
 
bool Lock ()
 
DWORD GetPixDw (int iX, int iY, bool fApplyModulation)
 
bool IsPixTransparent (int iX, int iY)
 
bool SetPixDw (int iX, int iY, DWORD dwCol)
 
bool BltPix (int iX, int iY, C4Surface *sfcSource, int iSrcX, int iSrcY, bool fTransparency)
 
bool Create (int iWdt, int iHgt, int iFlags=0)
 
bool Copy (C4Surface &fromSfc)
 
bool CreateColorByOwner (C4Surface *pBySurface)
 
bool SetAsClrByOwnerOf (C4Surface *pOfSurface)
 
bool CreatePrimaryGLTextures ()
 
bool UpdateSize (int wdt, int hgt)
 
bool PageFlip (C4Rect *pSrcRt=nullptr, C4Rect *pDstRt=nullptr)
 
void Clear ()
 
void Default ()
 
void Clip (int iX, int iY, int iX2, int iY2)
 
void NoClip ()
 
bool LoadAny (C4Group &hGroup, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags)
 
bool LoadAny (C4GroupSet &hGroupset, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags)
 
bool Load (C4Group &hGroup, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags)
 
bool Save (C4Group &hGroup, const char *szFilename)
 
bool SavePNG (C4Group &hGroup, const char *szFilename, bool fSaveAlpha=true, bool fSaveOverlayOnly=false)
 
bool SavePNG (const char *szFilename, bool fSaveAlpha, bool fSaveOverlayOnly, bool use_background_thread)
 
bool Read (CStdStream &hGroup, const char *extension, int iFlags)
 
bool ReadPNG (CStdStream &hGroup, int iFlags)
 
bool ReadJPEG (CStdStream &hGroup, int iFlags)
 
bool ReadBMP (CStdStream &hGroup, int iFlags)
 
bool AttachPalette ()
 
bool GetSurfaceSize (int &irX, int &irY)
 
void SetClr (DWORD toClr)
 
DWORD GetClr ()
 

Public Attributes

int Wdt
 
int Hgt
 
int Scale
 
int PrimarySurfaceLockPitch
 
BYTEPrimarySurfaceLockBits
 
int iTexSize
 
int ClipX
 
int ClipY
 
int ClipX2
 
int ClipY2
 
bool fIsBackground {false}
 
unsigned int Format
 
CStdGLCtxpCtx
 
std::unique_ptr< C4TexReftexture
 
C4SurfacepMainSfc
 
C4SurfacepNormalSfc
 
DWORD ClrByOwnerClr
 

Protected Attributes

C4WindowpWindow
 
int Locked
 
bool Attached
 
bool fPrimary
 

Friends

class C4Draw
 
class C4Pattern
 
class CStdGL
 

Detailed Description

Definition at line 53 of file C4Surface.h.

Constructor & Destructor Documentation

C4Surface::C4Surface ( )

Definition at line 37 of file C4Surface.cpp.

References Default().

38 {
39  Default();
40 }
void Default()
Definition: C4Surface.cpp:71

Here is the call graph for this function:

C4Surface::~C4Surface ( )

Definition at line 63 of file C4Surface.cpp.

References Clear().

64 {
65  /* for (C4ObjectLink *lnk = ::Objects.First; lnk; lnk=lnk->Next)
66  if (lnk->Obj->Menu)
67  lnk->Obj->Menu->AssertSurfaceNotUsed(this);*/
68  Clear();
69 }
void Clear()
Definition: C4Surface.cpp:126

Here is the call graph for this function:

C4Surface::C4Surface ( int  iWdt,
int  iHgt,
int  iFlags 
)

Definition at line 42 of file C4Surface.cpp.

References Create(), and Default().

43 {
44  Default();
45  // create
46  Create(iWdt, iHgt, iFlags);
47 }
void Default()
Definition: C4Surface.cpp:71
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:161

Here is the call graph for this function:

C4Surface::C4Surface ( C4AbstractApp pApp,
C4Window pWindow 
)

Definition at line 49 of file C4Surface.cpp.

References CStdGL::CreateContext(), Default(), fPrimary, NoClip(), pCtx, pGL, and pWindow.

49  :
50  Wdt(0), Hgt(0)
51 {
52  Default();
53  fPrimary=true;
54  this->pWindow=pWindow;
55  // create rendering context
56 #ifndef USE_CONSOLE
57  pCtx = pGL->CreateContext(pWindow, pApp);
58 #endif
59  // reset clipping
60  NoClip();
61 }
int Wdt
Definition: C4Surface.h:65
C4Window * pWindow
Definition: C4Surface.h:86
CStdGLCtx * pCtx
Definition: C4Surface.h:76
void Default()
Definition: C4Surface.cpp:71
CStdGLCtx * CreateContext(C4Window *pWindow, C4AbstractApp *pApp) override
Definition: C4DrawGL.cpp:282
int Hgt
Definition: C4Surface.h:65
CStdGL * pGL
Definition: C4DrawGL.cpp:905
bool fPrimary
Definition: C4Surface.h:89
void NoClip()
Definition: C4Surface.cpp:150

Here is the call graph for this function:

Member Function Documentation

bool C4Surface::AttachPalette ( )

Definition at line 434 of file C4Surface.cpp.

435 {
436  return true;
437 }
bool C4Surface::BltPix ( int  iX,
int  iY,
C4Surface sfcSource,
int  iSrcX,
int  iSrcY,
bool  fTransparency 
)

Definition at line 591 of file C4Surface.cpp.

References BltAlpha(), BltAlphaAdd(), C4GFXBLIT_ADDITIVE, C4Draw::dwBlitMode, GetPixDw(), pDraw, and texture.

Referenced by C4Draw::Blit8(), and C4Draw::BlitSimple().

592 {
593  // 32bit-blit. lock target
594  if (!texture) return false;
595  texture->Lock();
596  DWORD *pPix32 = (DWORD *)(((BYTE *)texture->texLock.pBits) + iY*texture->texLock.Pitch + iX * 4);
597  // get source pix as dword
598  DWORD srcPix=sfcSource->GetPixDw(iSrcX, iSrcY, true);
599  // merge
600  if (!fTransparency)
601  {
602  // set it
603  *pPix32=srcPix;
604  }
605  else
606  {
608  BltAlphaAdd(*pPix32, srcPix);
609  else
610  BltAlpha(*pPix32, srcPix);
611  }
612  // done
613  return true;
614 }
#define C4GFXBLIT_ADDITIVE
Definition: C4Surface.h:26
uint8_t BYTE
C4Draw * pDraw
Definition: C4Draw.cpp:42
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
Definition: C4Surface.cpp:491
void BltAlpha(DWORD &dwDst, DWORD dwSrc)
Definition: StdColors.h:31
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
void BltAlphaAdd(DWORD &dwDst, DWORD dwSrc)
Definition: StdColors.h:42
DWORD dwBlitMode
Definition: C4Draw.h:110
uint32_t DWORD

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Surface::Clear ( )

Definition at line 126 of file C4Surface.cpp.

References Locked, pCtx, texture, and Unlock().

Referenced by C4FacetSurface::Clear(), C4GraphicsResource::Clear(), Copy(), Create(), MoveFrom(), ReadBMP(), and ~C4Surface().

127 {
128  // Undo all locks
129  while (Locked) Unlock();
130  // release surface
131 #ifndef USE_CONSOLE
132  if (pCtx)
133  {
134  delete pCtx;
135  pCtx = nullptr;
136  }
137 #endif
138  texture.reset();
139 #ifdef _DEBUG
140  dbg_idx = 0;
141 #endif
142 }
CStdGLCtx * pCtx
Definition: C4Surface.h:76
bool Unlock()
Definition: C4Surface.cpp:464
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
int Locked
Definition: C4Surface.h:87

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Surface::ClearBoxDw ( int  iX,
int  iY,
int  iWdt,
int  iHgt 
)

Definition at line 616 of file C4Surface.cpp.

References Hgt, Locked, pMainSfc, texture, and Wdt.

617 {
618  // lock
619  if (!Locked) return;
620  // clip to target size
621  if (iX<0) { iWdt+=iX; iX=0; }
622  if (iY<0) { iHgt+=iY; iY=0; }
623  int iOver;
624  iOver=Wdt-(iX+iWdt); if (iOver<0) iWdt+=iOver;
625  iOver=Hgt-(iY+iHgt); if (iOver<0) iHgt+=iOver;
626  C4Rect rtClear{ iX, iY, iWdt, iHgt };
627  if (pMainSfc && pMainSfc->texture)
628  {
629  // assuming this is only valid as long as there's no texture management,
630  // organizing partially used textures together!
631  pMainSfc->texture->ClearRect(rtClear);
632  }
633  // clear this texture
634  texture->ClearRect(rtClear);
635 }
int Wdt
Definition: C4Surface.h:65
Definition: C4Rect.h:27
int Hgt
Definition: C4Surface.h:65
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
int Locked
Definition: C4Surface.h:87
C4Surface * pMainSfc
Definition: C4Surface.h:79
void C4Surface::Clip ( int  iX,
int  iY,
int  iX2,
int  iY2 
)

Definition at line 155 of file C4Surface.cpp.

References Clamp(), ClipX, ClipX2, ClipY, ClipY2, Hgt, and Wdt.

156 {
157  ClipX=Clamp(iX,0,Wdt-1); ClipY=Clamp(iY,0,Hgt-1);
158  ClipX2=Clamp(iX2,0,Wdt-1); ClipY2=Clamp(iY2,0,Hgt-1);
159 }
int Wdt
Definition: C4Surface.h:65
int ClipX
Definition: C4Surface.h:69
int Hgt
Definition: C4Surface.h:65
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:44
int ClipX2
Definition: C4Surface.h:69
int ClipY
Definition: C4Surface.h:69
int ClipY2
Definition: C4Surface.h:69

Here is the call graph for this function:

bool C4Surface::Copy ( C4Surface fromSfc)

Definition at line 184 of file C4Surface.cpp.

References C4Draw::BlitSurface(), Clear(), Create(), Default(), Hgt, pDraw, and Wdt.

Referenced by C4FacetSurface::CopyFromSfcMaxSize().

185 {
186  // Clear anything old
187  Clear();
188  // Default to other surface's color depth
189  Default();
190  // Create surface (TODO: copy flags)
191  if (!Create(fromSfc.Wdt, fromSfc.Hgt)) return false;
192  // Blit copy
193  if (!pDraw->BlitSurface(&fromSfc, this, 0, 0, false))
194  { Clear(); return false; }
195  // Success
196  return true;
197 }
int Wdt
Definition: C4Surface.h:65
void Default()
Definition: C4Surface.cpp:71
int Hgt
Definition: C4Surface.h:65
bool BlitSurface(C4Surface *sfcSurface, C4Surface *sfcTarget, int tx, int ty, bool fBlitBase)
Definition: C4Draw.cpp:517
void Clear()
Definition: C4Surface.cpp:126
C4Draw * pDraw
Definition: C4Draw.cpp:42
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:161

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::Create ( int  iWdt,
int  iHgt,
int  iFlags = 0 
)

Definition at line 161 of file C4Surface.cpp.

References Clear(), Default(), C4Draw::DeviceReady(), Format, Hgt, iTexSize, NoClip(), pDraw, pGL, CStdGL::sfcFmt, texture, and Wdt.

Referenced by C4Surface(), Copy(), C4FacetSurface::Create(), CreateColorByOwner(), ReadBMP(), ReadJPEG(), and ReadPNG().

162 {
163  Clear(); Default();
164  // check size
165  if (!iWdt || !iHgt) return false;
166  Wdt=iWdt; Hgt=iHgt;
167  // create texture: check gfx system
168  if (!pDraw) return false;
169  if (!pDraw->DeviceReady()) return false;
170 
171  // store color format that will be used
172 #ifndef USE_CONSOLE
173  Format=pGL->sfcFmt;
174 #endif
175  // create texture
176  iTexSize = std::max(iWdt, iHgt);
177  texture = std::make_unique<C4TexRef>(iWdt, iHgt, iFlags);
178  // update clipping
179  NoClip();
180  // success
181  return true;
182 }
int Wdt
Definition: C4Surface.h:65
virtual bool DeviceReady()=0
void Default()
Definition: C4Surface.cpp:71
int Hgt
Definition: C4Surface.h:65
GLenum sfcFmt
Definition: C4DrawGL.h:177
unsigned int Format
Definition: C4Surface.h:75
void Clear()
Definition: C4Surface.cpp:126
int iTexSize
Definition: C4Surface.h:68
C4Draw * pDraw
Definition: C4Draw.cpp:42
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
CStdGL * pGL
Definition: C4DrawGL.cpp:905
void NoClip()
Definition: C4Surface.cpp:150

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::CreateColorByOwner ( C4Surface pBySurface)

Definition at line 255 of file C4Surface.cpp.

References ClrByOwner(), Create(), GetPixDw(), Hgt, Lock(), pMainSfc, Scale, SetPixDw(), texture, Unlock(), and Wdt.

Referenced by C4FacetSurface::CreateClrByOwner().

256 {
257  // safety
258  if (!pBySurface) return false;
259  if (!pBySurface->texture) return false;
260  // create in same size
261  if (!Create(pBySurface->Wdt, pBySurface->Hgt)) return false;
262  // copy scale
263  Scale = pBySurface->Scale;
264  // set main surface
265  pMainSfc=pBySurface;
266  // lock it
267  if (!pMainSfc->Lock()) return false;
268  if (!Lock()) { pMainSfc->Unlock(); return false; }
269  // set ColorByOwner-pixels
270  for (int iY=0; iY<Hgt; ++iY)
271  for (int iX=0; iX<Wdt; ++iX)
272  {
273  // get pixel
274  DWORD dwPix=pMainSfc->GetPixDw(iX, iY, false);
275  // is it a ClrByOwner-px?
276  if (!ClrByOwner(dwPix)) continue;
277  // set in this surface
278  SetPixDw(iX, iY, dwPix);
279  // clear in the other
280  pMainSfc->SetPixDw(iX, iY, 0x00ffffff);
281  }
282  // unlock
283  Unlock();
284  pMainSfc->Unlock();
285  // success
286  return true;
287 }
int Wdt
Definition: C4Surface.h:65
bool Lock()
Definition: C4Surface.cpp:453
bool ClrByOwner(DWORD &dwClr)
Definition: C4Surface.cpp:203
bool Unlock()
Definition: C4Surface.cpp:464
int Hgt
Definition: C4Surface.h:65
int Scale
Definition: C4Surface.h:66
bool SetPixDw(int iX, int iY, DWORD dwCol)
Definition: C4Surface.cpp:576
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
Definition: C4Surface.cpp:491
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:161
uint32_t DWORD
C4Surface * pMainSfc
Definition: C4Surface.h:79

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::CreatePrimaryGLTextures ( )
void C4Surface::Default ( )

Definition at line 71 of file C4Surface.cpp.

References Attached, ClipX, ClipX2, ClipY, ClipY2, ClrByOwnerClr, fIsBackground, fPrimary, Hgt, iTexSize, Locked, pCtx, pMainSfc, pNormalSfc, PrimarySurfaceLockBits, PrimarySurfaceLockPitch, pWindow, Scale, and Wdt.

Referenced by C4Surface(), Copy(), C4FacetSurface::Create(), Create(), C4FacetSurface::Default(), C4GraphicsResource::Default(), and MoveFrom().

72 {
73  Wdt=Hgt=0;
74  Scale=1;
77  Locked=0;
78  Attached=false;
79  fPrimary=false;
80  pMainSfc=nullptr;
81  pNormalSfc=nullptr;
82 #ifndef USE_CONSOLE
83  pCtx=nullptr;
84 #endif
85  pWindow=nullptr;
86  ClrByOwnerClr=0;
87  iTexSize=0;
88  fIsBackground=false;
89 #ifdef _DEBUG
90  dbg_idx = 0;
91 #endif
92 }
int Wdt
Definition: C4Surface.h:65
C4Window * pWindow
Definition: C4Surface.h:86
CStdGLCtx * pCtx
Definition: C4Surface.h:76
DWORD ClrByOwnerClr
Definition: C4Surface.h:81
int ClipX
Definition: C4Surface.h:69
int Hgt
Definition: C4Surface.h:65
int Scale
Definition: C4Surface.h:66
C4Surface * pNormalSfc
Definition: C4Surface.h:80
int iTexSize
Definition: C4Surface.h:68
bool fIsBackground
Definition: C4Surface.h:70
int ClipX2
Definition: C4Surface.h:69
bool Attached
Definition: C4Surface.h:88
BYTE * PrimarySurfaceLockBits
Definition: C4Surface.h:67
int PrimarySurfaceLockPitch
Definition: C4Surface.h:67
int ClipY
Definition: C4Surface.h:69
int Locked
Definition: C4Surface.h:87
bool fPrimary
Definition: C4Surface.h:89
int ClipY2
Definition: C4Surface.h:69
C4Surface * pMainSfc
Definition: C4Surface.h:79

Here is the caller graph for this function:

DWORD C4Surface::GetClr ( )
inline

Definition at line 133 of file C4Surface.h.

References ClrByOwnerClr.

Referenced by C4MenuItem::GrabSymbol(), and C4MenuItem::RefSymbol().

133 { return ClrByOwnerClr; }
DWORD ClrByOwnerClr
Definition: C4Surface.h:81

Here is the caller graph for this function:

DWORD C4Surface::GetPixDw ( int  iX,
int  iY,
bool  fApplyModulation 
)

Definition at line 491 of file C4Surface.cpp.

References C4Draw::BlitModulateClr, C4Draw::BlitModulated, BltAlpha(), C4GFXBLIT_CLRSFC_MOD2, C4GFXBLIT_CLRSFC_OWNCLR, C4GFXBLIT_MOD2, ClrByOwnerClr, C4Draw::dwBlitMode, fPrimary, GetPixDw(), Hgt, ModulateClr(), ModulateClrMOD2(), pDraw, pMainSfc, PrimarySurfaceLockBits, PrimarySurfaceLockPitch, texture, and Wdt.

Referenced by C4TextureMap::AddTexture(), BltPix(), CreateColorByOwner(), C4GraphicsSystem::DoSaveScreenshot(), GetPixDw(), C4Draw::Grayscale(), IsPixTransparent(), C4ScenarioListLoader::Entry::Load(), SavePNG(), and C4Pattern::Set().

492 {
493  BYTE *pBuf = nullptr; int iPitch = 0; // TODO: are those initialised to something sensible?
494  // backup pos
495  int iX2=iX; int iY2=iY;
496  // primary?
497  if (fPrimary)
498  {
499 #ifndef USE_CONSOLE
501  {
502  PrimarySurfaceLockBits = new unsigned char[Wdt*Hgt*3];
503  glPixelStorei(GL_PACK_ALIGNMENT, 1);
504  glReadPixels( 0, 0, Wdt, Hgt, GL_BGR, GL_UNSIGNED_BYTE, PrimarySurfaceLockBits);
506  }
507  return * (DWORD *) (PrimarySurfaceLockBits+(Hgt-iY-1)*PrimarySurfaceLockPitch+iX*3);
508 #endif
509  }
510  else
511  {
512  // get+lock affected texture
513  if (!texture) return 0;
514  texture->Lock();
515  pBuf=(BYTE *) texture->texLock.pBits;
516  iPitch=texture->texLock.Pitch;
517  }
518  // get pix of surface
519  DWORD dwPix;
520  DWORD *pPix=(DWORD *) (pBuf+iY*iPitch+iX*4);
521  dwPix = *pPix;
522  // this is a ColorByOwner-surface?
523  if (pMainSfc)
524  {
525  BYTE byAlpha=BYTE(dwPix>>24);
526  // pix is fully transparent?
527  if (byAlpha==0x00)
528  // then get the main surfaces's pixel
529  dwPix = pMainSfc->GetPixDw(iX2, iY2, fApplyModulation);
530  else
531  {
532  // otherwise, it's a ColorByOwner-pixel: adjust the color
533  if (fApplyModulation)
534  {
537  else
538  ModulateClr(dwPix, ClrByOwnerClr);
541  }
542  else
543  ModulateClr(dwPix, ClrByOwnerClr);
544  // does it contain transparency? then blit on main sfc
545  if (byAlpha)
546  {
547  DWORD dwMainPix = pMainSfc->GetPixDw(iX2, iY2, fApplyModulation);
548  BltAlpha(dwMainPix, dwPix); dwPix=dwMainPix;
549  }
550  }
551  }
552  else
553  {
554  // single main surface
555  // apply color modulation if desired
556  if (fApplyModulation && pDraw->BlitModulated)
557  {
560  else
562  }
563  }
564  // return pixel value
565  return dwPix;
566 }
#define C4GFXBLIT_MOD2
Definition: C4Surface.h:27
int Wdt
Definition: C4Surface.h:65
DWORD ClrByOwnerClr
Definition: C4Surface.h:81
uint8_t BYTE
void ModulateClr(DWORD &dwDst, DWORD dwMod)
Definition: StdColors.h:53
int Hgt
Definition: C4Surface.h:65
void ModulateClrMOD2(DWORD &dwDst, DWORD dwMod)
Definition: StdColors.h:73
#define C4GFXBLIT_CLRSFC_OWNCLR
Definition: C4Surface.h:28
C4Draw * pDraw
Definition: C4Draw.cpp:42
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
Definition: C4Surface.cpp:491
void BltAlpha(DWORD &dwDst, DWORD dwSrc)
Definition: StdColors.h:31
DWORD BlitModulateClr
Definition: C4Draw.h:109
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
#define C4GFXBLIT_CLRSFC_MOD2
Definition: C4Surface.h:29
BYTE * PrimarySurfaceLockBits
Definition: C4Surface.h:67
int PrimarySurfaceLockPitch
Definition: C4Surface.h:67
DWORD dwBlitMode
Definition: C4Draw.h:110
bool fPrimary
Definition: C4Surface.h:89
uint32_t DWORD
bool BlitModulated
Definition: C4Draw.h:108
C4Surface * pMainSfc
Definition: C4Surface.h:79

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::GetSurfaceSize ( int &  irX,
int &  irY 
)

Definition at line 444 of file C4Surface.cpp.

References Hgt, and Wdt.

Referenced by C4Draw::GetSurfaceSize(), and C4Sky::Init().

445 {
446  // simply assign stored values
447  irX=Wdt;
448  irY=Hgt;
449  // success
450  return true;
451 }
int Wdt
Definition: C4Surface.h:65
int Hgt
Definition: C4Surface.h:65

Here is the caller graph for this function:

int C4Surface::IsLocked ( ) const
inline

Definition at line 93 of file C4Surface.h.

References Locked.

Referenced by CStdFont::AddSurface().

93 { return Locked; }
int Locked
Definition: C4Surface.h:87

Here is the caller graph for this function:

bool C4Surface::IsPixTransparent ( int  iX,
int  iY 
)

Definition at line 568 of file C4Surface.cpp.

References GetPixDw().

569 {
570  // get pixel value
571  DWORD dwPix=GetPixDw(iX, iY, false);
572  // get alpha value
573  return (dwPix>>24) < 128;
574 }
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
Definition: C4Surface.cpp:491
uint32_t DWORD

Here is the call graph for this function:

bool C4Surface::IsRenderTarget ( )

Definition at line 144 of file C4Surface.cpp.

References fPrimary.

Referenced by C4Draw::Blit8Fast(), C4Draw::BlitSimple(), C4Draw::BlitSurfaceTile(), C4Draw::BlitUnscaled(), C4Draw::DrawBoxDw(), C4Draw::DrawPatternedCircle(), CStdGL::PerformMultiBlt(), CStdGL::PrepareRendering(), and C4Draw::RenderMesh().

145 {
146  // primary is always OK...
147  return fPrimary;
148 }
bool fPrimary
Definition: C4Surface.h:89

Here is the caller graph for this function:

bool C4Surface::Load ( C4Group hGroup,
const char *  szFilename,
bool  fOwnPal,
bool  fNoErrIfNotFound,
int  iFlags 
)

Definition at line 76 of file C4SurfaceLoaders.cpp.

References _MAX_FNAME, _MAX_PATH, C4Group::AccessEntry(), StdStrBuf::Copy(), DirectorySeparator, C4Group::FindEntry(), C4Group::FindNextEntry(), StdStrBuf::getData(), GetExtension(), C4Group::GetFullName(), LoadResStr(), LogF(), Read(), RemoveExtension(), Scale, SCharLastPos(), and SCopy().

Referenced by C4FacetSurface::Load(), LoadAny(), and C4GraphicsResource::LoadFile().

77 {
78  int ScaleToSet = 1;
79  // Image is scaled?
80  StdStrBuf strFilename;
81  char strBasename[_MAX_FNAME + 1]; SCopy(szFilename, strBasename, _MAX_FNAME); RemoveExtension(strBasename);
82  int32_t extpos; int scale;
83  if (((extpos = SCharLastPos('.', strBasename)) > -1) && (sscanf(strBasename+extpos+1, "%d", &scale) == 1))
84  {
85  // Filename with scale information was passed. Just store scale.
86  ScaleToSet = scale;
87  }
88  else
89  {
90  // a filename with out scale information was passed in
91  // Look for scaled images
92  const size_t base_length = std::strlen(strBasename);
93  char strExtension[128 + 1]; SCopy(GetExtension(szFilename), strExtension, 128);
94  if (strExtension[0])
95  {
96  char scaled_name[_MAX_PATH+1];
97  std::string wildcard(strBasename);
98  wildcard += ".*.";
99  wildcard += strExtension;
100  int max_scale = -1;
101  if (hGroup.FindEntry(wildcard.c_str(), scaled_name))
102  {
103  do
104  {
105  int scale = -1;
106  if (sscanf(scaled_name + base_length + 1, "%d", &scale) == 1)
107  if (scale > max_scale)
108  {
109  max_scale = scale;
110  ScaleToSet = max_scale;
111  strFilename.Copy(scaled_name);
112  szFilename = strFilename.getData();
113  }
114  }
115  while (hGroup.FindNextEntry(wildcard.c_str(), scaled_name));
116  }
117  }
118  }
119  // Access entry
120  if (!hGroup.AccessEntry(szFilename))
121  {
122  // file not found
123  if (!fNoErrIfNotFound) LogF("%s: %s%c%s", LoadResStr("IDS_PRC_FILENOTFOUND"), hGroup.GetFullName().getData(), (char) DirectorySeparator, szFilename);
124  return false;
125  }
126  bool fSuccess = Read(hGroup, GetExtension(szFilename), iFlags);
127  // loading error? log!
128  if (!fSuccess)
129  LogF("%s: %s%c%s", LoadResStr("IDS_ERR_NOFILE"), hGroup.GetFullName().getData(), (char) DirectorySeparator, szFilename);
130  // Work around the broken Default()-convention
131  Scale = ScaleToSet;
132  // done, success
133  return fSuccess;
134 }
const char * getData() const
Definition: StdBuf.h:442
bool FindEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr)
Definition: C4Group.cpp:1774
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
bool AccessEntry(const char *szWildCard, size_t *iSize=nullptr, char *sFileName=nullptr, bool NeedsToBeAGroup=false)
Definition: C4Group.cpp:1695
#define _MAX_PATH
int Scale
Definition: C4Surface.h:66
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
int SCharLastPos(char cTarget, const char *szInStr)
Definition: Standard.cpp:225
StdStrBuf GetFullName() const
Definition: C4Group.cpp:2078
#define _MAX_FNAME
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:121
void RemoveExtension(char *szFilename)
Definition: StdFile.cpp:306
bool FindNextEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr, bool fStartAtFilename=false)
Definition: C4Group.cpp:1780
bool Read(CStdStream &hGroup, const char *extension, int iFlags)
#define DirectorySeparator
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:250
void Copy()
Definition: StdBuf.h:467

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::LoadAny ( C4Group hGroup,
const char *  szFilename,
bool  fOwnPal,
bool  fNoErrIfNotFound,
int  iFlags 
)

Definition at line 28 of file C4SurfaceLoaders.cpp.

References _MAX_FNAME, EnforceExtension(), C4Group::FindEntry(), GetExtension(), Load(), and SCopy().

Referenced by C4Sky::Init().

29 {
30  // Entry name
31  char szFilename[_MAX_FNAME+1];
32  SCopy(szName,szFilename,_MAX_FNAME);
33  char *szExt = GetExtension(szFilename);
34  if (!*szExt)
35  {
36  // no extension: Default to extension that is found as file in group
37  const char * const extensions[] = { "png", "bmp", "jpeg", "jpg", nullptr };
38  int i = 0; const char *szExt;
39  while ((szExt = extensions[i++]))
40  {
41  EnforceExtension(szFilename, szExt);
42  if (hGroup.FindEntry(szFilename)) break;
43  }
44  }
45  // Load surface
46  return Load(hGroup,szFilename,fOwnPal,fNoErrIfNotFound,iFlags);
47 }
bool FindEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr)
Definition: C4Group.cpp:1774
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
#define _MAX_FNAME
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:121
bool Load(C4Group &hGroup, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags)
void EnforceExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:289

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::LoadAny ( C4GroupSet hGroupset,
const char *  szFilename,
bool  fOwnPal,
bool  fNoErrIfNotFound,
int  iFlags 
)

Definition at line 50 of file C4SurfaceLoaders.cpp.

References _MAX_FNAME, EnforceExtension(), C4GroupSet::FindEntry(), GetExtension(), Load(), and SCopy().

51 {
52  // Entry name
53  char szFilename[_MAX_FNAME+1];
54  SCopy(szName,szFilename,_MAX_FNAME);
55  char *szExt = GetExtension(szFilename);
56  C4Group * pGroup;
57  if (!*szExt)
58  {
59  // no extension: Default to extension that is found as file in group
60  const char * const extensions[] = { "png", "bmp", "jpeg", "jpg", nullptr };
61  int i = 0; const char *szExt;
62  while ((szExt = extensions[i++]))
63  {
64  EnforceExtension(szFilename, szExt);
65  pGroup = hGroupset.FindEntry(szFilename);
66  if (pGroup) break;
67  }
68  }
69  else
70  pGroup = hGroupset.FindEntry(szFilename);
71  if (!pGroup) return false;
72  // Load surface
73  return Load(*pGroup,szFilename,fOwnPal,fNoErrIfNotFound,iFlags);
74 }
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
C4Group * FindEntry(const char *szWildcard, int32_t *pPriority=nullptr, int32_t *pID=nullptr)
Definition: C4GroupSet.cpp:175
#define _MAX_FNAME
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:121
bool Load(C4Group &hGroup, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags)
void EnforceExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:289

Here is the call graph for this function:

bool C4Surface::Lock ( )

Definition at line 453 of file C4Surface.cpp.

References fPrimary, Lock(), Locked, pMainSfc, and texture.

Referenced by CStdFont::AddRenderedChar(), C4TextureMap::AddTexture(), C4Draw::Blit8(), C4Draw::Blit8Fast(), C4Draw::BlitSimple(), CreateColorByOwner(), C4GraphicsSystem::DoSaveScreenshot(), C4Draw::DrawBoxDw(), C4Draw::DrawPatternedCircle(), C4Draw::Grayscale(), CStdFont::Init(), C4ScenarioListLoader::Entry::Load(), Lock(), C4Pattern::operator=(), ReadBMP(), ReadJPEG(), ReadPNG(), SavePNG(), and C4Pattern::Set().

454 {
455  // lock main sfc
456  if (pMainSfc) if (!pMainSfc->Lock()) return false;
457  // lock texture
458  if (!Locked && !fPrimary && !texture)
459  return false;
460  // count lock
461  Locked++; return true;
462 }
bool Lock()
Definition: C4Surface.cpp:453
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
int Locked
Definition: C4Surface.h:87
bool fPrimary
Definition: C4Surface.h:89
C4Surface * pMainSfc
Definition: C4Surface.h:79

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Surface::MoveFrom ( C4Surface psfcFrom)

Definition at line 94 of file C4Surface.cpp.

References Attached, Clear(), ClipX, ClipX2, ClipY, ClipY2, ClrByOwnerClr, Default(), fIsBackground, Format, fPrimary, Hgt, iTexSize, Locked, pMainSfc, pNormalSfc, PrimarySurfaceLockBits, PrimarySurfaceLockPitch, texture, and Wdt.

Referenced by C4FacetSurface::GrabFrom().

95 {
96  // clear own
97  Clear();
98  // safety
99  if (!psfcFrom) return;
100  // grab data from other sfc
101 #ifdef _DEBUG
102  dbg_idx = psfcFrom->dbg_idx;
103 #endif
104  Wdt=psfcFrom->Wdt; Hgt=psfcFrom->Hgt;
107  psfcFrom->PrimarySurfaceLockBits=nullptr;
108  ClipX=psfcFrom->ClipX; ClipY=psfcFrom->ClipY;
109  ClipX2=psfcFrom->ClipX2; ClipY2=psfcFrom->ClipY2;
110  Locked=psfcFrom->Locked;
111  Attached=psfcFrom->Attached;
112  fPrimary=psfcFrom->fPrimary; // shouldn't be true!
113  texture = std::move(psfcFrom->texture);
114  pMainSfc=psfcFrom->pMainSfc;
115  pNormalSfc=psfcFrom->pNormalSfc;
116  ClrByOwnerClr=psfcFrom->ClrByOwnerClr;
117  iTexSize=psfcFrom->iTexSize;
118 #ifndef USE_CONSOLE
119  Format=psfcFrom->Format;
120 #endif
121  fIsBackground = psfcFrom->fIsBackground;
122  // default other sfc
123  psfcFrom->Default();
124 }
int Wdt
Definition: C4Surface.h:65
void Default()
Definition: C4Surface.cpp:71
DWORD ClrByOwnerClr
Definition: C4Surface.h:81
int ClipX
Definition: C4Surface.h:69
int Hgt
Definition: C4Surface.h:65
unsigned int Format
Definition: C4Surface.h:75
C4Surface * pNormalSfc
Definition: C4Surface.h:80
void Clear()
Definition: C4Surface.cpp:126
int iTexSize
Definition: C4Surface.h:68
bool fIsBackground
Definition: C4Surface.h:70
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
int ClipX2
Definition: C4Surface.h:69
bool Attached
Definition: C4Surface.h:88
BYTE * PrimarySurfaceLockBits
Definition: C4Surface.h:67
int PrimarySurfaceLockPitch
Definition: C4Surface.h:67
int ClipY
Definition: C4Surface.h:69
int Locked
Definition: C4Surface.h:87
bool fPrimary
Definition: C4Surface.h:89
int ClipY2
Definition: C4Surface.h:69
C4Surface * pMainSfc
Definition: C4Surface.h:79

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Surface::NoClip ( )

Definition at line 150 of file C4Surface.cpp.

References ClipX, ClipX2, ClipY, ClipY2, Hgt, and Wdt.

Referenced by C4Surface(), and Create().

151 {
152  ClipX=0; ClipY=0; ClipX2=Wdt-1; ClipY2=Hgt-1;
153 }
int Wdt
Definition: C4Surface.h:65
int ClipX
Definition: C4Surface.h:69
int Hgt
Definition: C4Surface.h:65
int ClipX2
Definition: C4Surface.h:69
int ClipY
Definition: C4Surface.h:69
int ClipY2
Definition: C4Surface.h:69

Here is the caller graph for this function:

bool C4Surface::PageFlip ( C4Rect pSrcRt = nullptr,
C4Rect pDstRt = nullptr 
)

Definition at line 310 of file C4Surface.cpp.

References C4AbstractApp::AssertMainThread(), fPrimary, CStdGLCtx::PageFlip(), C4Draw::pApp, pCtx, and pDraw.

Referenced by C4GraphicsSystem::DoSaveScreenshot(), C4GUI::Dialog::Draw(), C4GraphicsSystem::FinishDrawing(), and C4MessageBoard::LogNotify().

311 {
312  assert(fPrimary);
313  if (!fPrimary)
314  return false;
315  // call from gfx thread only!
316  if (!pDraw->pApp || !pDraw->pApp->AssertMainThread()) return false;
317 #ifndef USE_CONSOLE
318  return pCtx->PageFlip();
319 #endif
320  return true;
321 }
bool AssertMainThread()
Definition: C4App.h:123
CStdGLCtx * pCtx
Definition: C4Surface.h:76
C4Draw * pDraw
Definition: C4Draw.cpp:42
virtual bool PageFlip()
bool fPrimary
Definition: C4Surface.h:89
C4AbstractApp * pApp
Definition: C4Draw.h:95

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::Read ( CStdStream hGroup,
const char *  extension,
int  iFlags 
)

Definition at line 136 of file C4SurfaceLoaders.cpp.

References ReadBMP(), ReadJPEG(), ReadPNG(), and SEqualNoCase().

Referenced by Load(), C4DefAdditionalResourcesLoader::LoadTexture(), and C4TextureMap::LoadTextures().

137 {
138  // determine file type by file extension and load accordingly
139  if (SEqualNoCase(extension, "png"))
140  return ReadPNG(hGroup, iFlags);
141  else if (SEqualNoCase(extension, "jpeg")
142  || SEqualNoCase(extension, "jpg"))
143  return ReadJPEG(hGroup, iFlags);
144  else if (SEqualNoCase(extension, "bmp"))
145  return ReadBMP(hGroup, iFlags);
146  else
147  return false;
148 }
bool ReadBMP(CStdStream &hGroup, int iFlags)
Definition: C4Surface.cpp:323
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:185
bool ReadPNG(CStdStream &hGroup, int iFlags)
bool ReadJPEG(CStdStream &hGroup, int iFlags)

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::ReadBMP ( CStdStream hGroup,
int  iFlags 
)

Definition at line 323 of file C4Surface.cpp.

References CStdStream::Advance(), tagBITMAPINFOHEADER::biBitCount, tagBITMAPINFOHEADER::biHeight, tagBITMAPINFOHEADER::biWidth, C4RGB, Clear(), C4BMP256Info::Colors, Create(), DWordAligned(), C4BMP256Info::FileBitsOffset(), Hgt, C4BMPInfo::Info, Lock(), CStdStream::Read(), tagRGBQUAD::rgbBlue, tagRGBQUAD::rgbGreen, tagRGBQUAD::rgbRed, SetPixDw(), and Unlock().

Referenced by Read().

324 {
325  int lcnt;
326  C4BMP256Info BitmapInfo;
327  // read bmpinfo-header
328  if (!hGroup.Read(&BitmapInfo,sizeof(C4BMPInfo))) return false;
329  // is it 8bpp?
330  if (BitmapInfo.Info.biBitCount == 8)
331  {
332  if (!hGroup.Read(((BYTE *) &BitmapInfo)+sizeof(C4BMPInfo),
333  std::min(sizeof(BitmapInfo)-sizeof(C4BMPInfo),sizeof(BitmapInfo)-sizeof(C4BMPInfo)+BitmapInfo.FileBitsOffset())))
334  return false;
335  if (!hGroup.Advance(BitmapInfo.FileBitsOffset())) return false;
336  }
337  else
338  {
339  // read 24bpp
340  if (BitmapInfo.Info.biBitCount != 24) return false;
341  if (!hGroup.Advance(((C4BMPInfo) BitmapInfo).FileBitsOffset())) return false;
342  }
343 
344  // Create and lock surface
345  if (!Create(BitmapInfo.Info.biWidth,BitmapInfo.Info.biHeight, iFlags)) return false;
346  if (!Lock()) { Clear(); return false; }
347 
348  // create line buffer
349  int iBufSize=DWordAligned(BitmapInfo.Info.biWidth*BitmapInfo.Info.biBitCount/8);
350  BYTE *pBuf = new BYTE[iBufSize];
351  // Read lines
352  for (lcnt=Hgt-1; lcnt>=0; lcnt--)
353  {
354  if (!hGroup.Read(pBuf, iBufSize))
355  { Clear(); delete [] pBuf; return false; }
356  BYTE *pPix=pBuf;
357  for (int x=0; x<BitmapInfo.Info.biWidth; ++x)
358  switch (BitmapInfo.Info.biBitCount)
359  {
360  case 8:
361  SetPixDw(x, lcnt, C4RGB(
362  BitmapInfo.Colors[*pPix].rgbRed,
363  BitmapInfo.Colors[*pPix].rgbGreen,
364  BitmapInfo.Colors[*pPix].rgbBlue));
365  ++pPix;
366  break;
367  case 24:
368  SetPixDw(x, lcnt, C4RGB(pPix[0], pPix[1], pPix[2]));
369  pPix+=3;
370  break;
371  }
372  }
373  // free buffer again
374  delete [] pBuf;
375 
376  Unlock();
377 
378  return true;
379 }
BYTE rgbRed
Definition: Bitmap256.h:54
int DWordAligned(int val)
Definition: Standard.h:47
bool Lock()
Definition: C4Surface.cpp:453
BITMAPINFOHEADER Info
Definition: Bitmap256.h:67
bool Unlock()
Definition: C4Surface.cpp:464
uint8_t BYTE
int Hgt
Definition: C4Surface.h:65
BYTE rgbGreen
Definition: Bitmap256.h:53
bool SetPixDw(int iX, int iY, DWORD dwCol)
Definition: C4Surface.cpp:576
void Clear()
Definition: C4Surface.cpp:126
#define C4RGB(r, g, b)
Definition: StdColors.h:26
BYTE rgbBlue
Definition: Bitmap256.h:52
virtual bool Advance(int iOffset)=0
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:161
virtual bool Read(void *pBuffer, size_t iSize)=0
int FileBitsOffset()
Definition: Bitmap256.cpp:60
RGBQUAD Colors[256]
Definition: Bitmap256.h:76

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::ReadJPEG ( CStdStream hGroup,
int  iFlags 
)

Definition at line 284 of file C4SurfaceLoaders.cpp.

References CStdStream::AccessedEntrySize(), C4RGB, Create(), Lock(), my_error_mgr::pub, CStdStream::Read(), my_error_mgr::setjmp_buffer, SetPixDw(), and Unlock().

Referenced by Read().

285 {
286  // create mem block
287  size_t size=hGroup.AccessedEntrySize();
288  unsigned char *pData=new unsigned char[size];
289  // load file into mem
290  hGroup.Read(pData, size);
291  // stuff for libjpeg
292  struct jpeg_decompress_struct cinfo;
293  struct my_error_mgr jerr;
294  JSAMPARRAY buffer; /* Output row buffer */
295  int row_stride; /* physical row width in output buffer */
296  /* We set up the normal JPEG error routines, then override error_exit. */
297  cinfo.err = jpeg_std_error(&jerr.pub);
298  jerr.pub.error_exit = my_error_exit;
299  jerr.pub.output_message = my_output_message;
300  // apparantly, this is needed so libjpeg does not exit() the engine away
301  if (setjmp(jerr.setjmp_buffer))
302  {
303  // some fatal error
304  jpeg_destroy_decompress(&cinfo);
305  delete [] pData;
306  return false;
307  }
308  jpeg_create_decompress(&cinfo);
309 
310  // no fancy function calling
311  jpeg_source_mgr blub;
312  cinfo.src = &blub;
313  blub.next_input_byte = pData;
314  blub.bytes_in_buffer = size;
315  blub.init_source = jpeg_noop;
316  blub.fill_input_buffer = fill_input_buffer;
317  blub.skip_input_data = skip_input_data;
318  blub.resync_to_restart = jpeg_resync_to_restart;
319  blub.term_source = jpeg_noop;
320 
321  // a missing image is an error
322  jpeg_read_header(&cinfo, (boolean)true);
323 
324  // Let libjpeg convert for us
325  cinfo.out_color_space = JCS_RGB;
326  jpeg_start_decompress(&cinfo);
327 
328  // create surface(s) - do not create an 8bit-buffer!
329  if (!Create(cinfo.output_width, cinfo.output_height, iFlags)) return false;
330  // JSAMPLEs per row in output buffer
331  row_stride = cinfo.output_width * cinfo.output_components;
332  // Make a one-row-high sample array that will go away at jpeg_destroy_decompress
333  buffer = (*cinfo.mem->alloc_sarray)
334  ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
335  // lock for writing data
336  if (!Lock()) return false;
337  while (cinfo.output_scanline < cinfo.output_height)
338  {
339  // read an 1-row-array of scanlines
340  jpeg_read_scanlines(&cinfo, buffer, 1);
341  // put the data in the image
342  for (unsigned int i = 0; i < cinfo.output_width; ++i)
343  {
344  const unsigned char * const start = buffer[0] + i * cinfo.output_components;
345  const uint32_t c = C4RGB(*start, *(start + 1), *(start + 2));
346  SetPixDw(i, cinfo.output_scanline - 1, c);
347  }
348  }
349  // unlock
350  Unlock();
351  // clean up
352  jpeg_finish_decompress(&cinfo);
353  jpeg_destroy_decompress(&cinfo);
354  // free data
355  delete [] pData;
356  // return if successful
357  return true;
358 }
virtual size_t AccessedEntrySize() const =0
bool Lock()
Definition: C4Surface.cpp:453
bool Unlock()
Definition: C4Surface.cpp:464
bool SetPixDw(int iX, int iY, DWORD dwCol)
Definition: C4Surface.cpp:576
#define C4RGB(r, g, b)
Definition: StdColors.h:26
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:161
virtual bool Read(void *pBuffer, size_t iSize)=0

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::ReadPNG ( CStdStream hGroup,
int  iFlags 
)

Definition at line 150 of file C4SurfaceLoaders.cpp.

References CStdStream::AccessedEntrySize(), Create(), CPNGFile::GetPix(), CPNGFile::GetRow(), Hgt, CPNGFile::iClrType, CPNGFile::iHgt, iSize, iTexSize, CPNGFile::iWdt, CPNGFile::Load(), Lock(), CStdStream::Read(), texture, Unlock(), and Wdt.

Referenced by Read().

151 {
152  // create mem block
153  int iSize=hGroup.AccessedEntrySize();
154  BYTE *pData=new BYTE[iSize];
155  // load file into mem
156  hGroup.Read((void *) pData, iSize);
157  // load as png file
158  CPNGFile png;
159  bool fSuccess=png.Load(pData, iSize);
160  // free data
161  delete [] pData;
162  // abort if loading wasn't successful
163  if (!fSuccess) return false;
164  // create surface(s) - do not create an 8bit-buffer!
165  if (!Create(png.iWdt, png.iHgt, iFlags)) return false;
166  // lock for writing data
167  if (!Lock()) return false;
168  if (!texture)
169  {
170  Unlock();
171  return false;
172  }
173  // write pixels
174  // Get Texture and lock it
175  if (!texture->Lock()) return false;
176  int maxX = std::min(Wdt, iTexSize);
177  int maxY = std::min(Hgt, iTexSize);
178  for (int iY = 0; iY < maxY; ++iY)
179  {
180 #ifndef __BIG_ENDIAN__
181  if (png.iClrType == PNG_COLOR_TYPE_RGB_ALPHA)
182  {
183  // Optimize the easy case of a png in the same format as the display
184  // 32 bit
185  DWORD *pPix=(DWORD *) (((char *) texture->texLock.pBits) + iY * texture->texLock.Pitch);
186  memcpy (pPix, png.GetRow(iY), maxX * sizeof(*pPix));
187  int iX = maxX;
188  while (iX--) { if (((BYTE *)pPix)[3] == 0x00) *pPix = 0x00000000; ++pPix; }
189  }
190  else
191 #endif
192  {
193  // Loop through every pixel and convert
194  for (int iX = 0; iX < maxX; ++iX)
195  {
196  uint32_t dwCol = png.GetPix(iX, iY);
197  // if color is fully transparent, ensure it's black
198  if (dwCol>>24 == 0x00) dwCol=0x00000000;
199  // set pix in surface
200  DWORD *pPix=(DWORD *) (((char *) texture->texLock.pBits) + iY * texture->texLock.Pitch + iX * 4);
201  *pPix=dwCol;
202  }
203  }
204  }
205 
206  // unlock
207  texture->Unlock();
208  Unlock();
209  // return if successful
210  return fSuccess;
211 }
DWORD GetPix(int iX, int iY)
Definition: StdPNG.cpp:173
int Wdt
Definition: C4Surface.h:65
virtual size_t AccessedEntrySize() const =0
bool Lock()
Definition: C4Surface.cpp:453
bool Unlock()
Definition: C4Surface.cpp:464
uint8_t BYTE
int Hgt
Definition: C4Surface.h:65
int iClrType
Definition: StdPNG.h:45
unsigned long iHgt
Definition: StdPNG.h:44
int iTexSize
Definition: C4Surface.h:68
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
unsigned long iWdt
Definition: StdPNG.h:44
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:161
uint32_t DWORD
bool Load(BYTE *pFile, int iSize)
Definition: StdPNG.cpp:153
virtual bool Read(void *pBuffer, size_t iSize)=0
uint32_t * GetRow(int iY)
Definition: StdPNG.h:56
int iSize
Definition: TstC4NetIO.cpp:32

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::Save ( C4Group hGroup,
const char *  szFilename 
)
bool C4Surface::SavePNG ( C4Group hGroup,
const char *  szFilename,
bool  fSaveAlpha = true,
bool  fSaveOverlayOnly = false 
)

Definition at line 213 of file C4SurfaceLoaders.cpp.

References _MAX_PATH, C4Group_GetTempPath(), GetFilename(), MakeTempFilename(), C4Group::Move(), SAppend(), and SCopy().

Referenced by C4GraphicsSystem::DoSaveScreenshot(), C4MapFolderData::Load(), C4StartupPlrPropertiesDlg::OnClosed(), C4ObjectInfo::Save(), and C4Game::SaveGameTitle().

214 {
215  // Using temporary file at C4Group temp path
216  char szTemp[_MAX_PATH+1];
217  SCopy(C4Group_GetTempPath(),szTemp);
218  SAppend(GetFilename(szFilename),szTemp);
219  MakeTempFilename(szTemp);
220  // Save to temporary file
221  if (!C4Surface::SavePNG(szTemp, fSaveAlpha, fSaveOverlayOnly, false)) return false;
222  // Move temp file to group
223  if (!hGroup.Move(szTemp,GetFilename(szFilename))) return false;
224  // Success
225  return true;
226 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:45
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:235
bool SavePNG(C4Group &hGroup, const char *szFilename, bool fSaveAlpha=true, bool fSaveOverlayOnly=false)
#define _MAX_PATH
bool Move(const char *szFile, const char *szAddAs)
Definition: C4Group.cpp:1325
const char * C4Group_GetTempPath()
Definition: C4Group.cpp:80
void MakeTempFilename(char *szFilename)
Definition: StdFile.cpp:323

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::SavePNG ( const char *  szFilename,
bool  fSaveAlpha,
bool  fSaveOverlayOnly,
bool  use_background_thread 
)

Definition at line 381 of file C4Surface.cpp.

References fPrimary, GetPixDw(), Hgt, Lock(), pMainSfc, CPNGFile::ScheduleSaving(), Unlock(), and Wdt.

382 {
383  // Lock - WARNING - maybe locking primary surface here...
384  if (!Lock()) return false;
385 
386  // create png file
387  std::unique_ptr<CPNGFile> png(new CPNGFile());
388  if (!png->Create(Wdt, Hgt, fSaveAlpha)) { Unlock(); return false; }
389 
390  // reset overlay if desired
391  C4Surface *pMainSfcBackup = nullptr;
392  if (fSaveOverlayOnly) { pMainSfcBackup=pMainSfc; pMainSfc=nullptr; }
393 
394 #ifndef USE_CONSOLE
395  if (fPrimary)
396  {
397  // Take shortcut. FIXME: Check Endian
398  for (int y = 0; y < Hgt; ++y)
399  glReadPixels(0, Hgt - y - 1, Wdt, 1, fSaveAlpha ? GL_BGRA : GL_BGR, GL_UNSIGNED_BYTE, png->GetRow(y));
400  }
401  else
402 #endif
403  {
404  // write pixel values
405  for (int y=0; y<Hgt; ++y)
406  for (int x=0; x<Wdt; ++x)
407  {
408  DWORD dwClr = GetPixDw(x, y, false);
409  png->SetPix(x, y, dwClr);
410  }
411  }
412 
413  // reset overlay
414  if (fSaveOverlayOnly) pMainSfc=pMainSfcBackup;
415 
416  // Unlock
417  Unlock();
418 
419  // save png - either directly or delayed in a background thread if desired
420  if (use_background_thread)
421  {
422  CPNGFile::ScheduleSaving(png.release(), szFilename);
423  }
424  else
425  {
426  if (!png->Save(szFilename)) return false;
427  }
428 
429  // Success
430  return true;
431 }
int Wdt
Definition: C4Surface.h:65
bool Lock()
Definition: C4Surface.cpp:453
bool Unlock()
Definition: C4Surface.cpp:464
static void ScheduleSaving(CPNGFile *png, const char *filename)
Definition: StdPNG.cpp:352
int Hgt
Definition: C4Surface.h:65
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
Definition: C4Surface.cpp:491
bool fPrimary
Definition: C4Surface.h:89
uint32_t DWORD
C4Surface * pMainSfc
Definition: C4Surface.h:79

Here is the call graph for this function:

bool C4Surface::SetAsClrByOwnerOf ( C4Surface pOfSurface)

Definition at line 289 of file C4Surface.cpp.

References Hgt, pMainSfc, and Wdt.

290 {
291  // safety
292  if (!pOfSurface) return false;
293  if (Wdt != pOfSurface->Wdt || Hgt != pOfSurface->Hgt)
294  return false;
295  // set main surface
296  pMainSfc=pOfSurface;
297  // success
298  return true;
299 }
int Wdt
Definition: C4Surface.h:65
int Hgt
Definition: C4Surface.h:65
C4Surface * pMainSfc
Definition: C4Surface.h:79
void C4Surface::SetBackground ( )
inline

Definition at line 92 of file C4Surface.h.

References fIsBackground.

Referenced by C4LoaderScreen::Init(), and C4StartupGraphics::Init().

92 { fIsBackground = true; }
bool fIsBackground
Definition: C4Surface.h:70

Here is the caller graph for this function:

void C4Surface::SetClr ( DWORD  toClr)
inline

Definition at line 132 of file C4Surface.h.

Referenced by C4FacetSurface::CopyFromSfcMaxSize(), C4GraphicsOverlay::Draw(), C4Facet::DrawClr(), C4GUI::IconButton::DrawElement(), C4Facet::DrawValue2Clr(), C4Facet::DrawXClr(), and C4DefGraphics::GetBitmap().

132 { ClrByOwnerClr=toClr; }
DWORD ClrByOwnerClr
Definition: C4Surface.h:81

Here is the caller graph for this function:

bool C4Surface::SetPixDw ( int  iX,
int  iY,
DWORD  dwCol 
)

Definition at line 576 of file C4Surface.cpp.

References ClipX, ClipX2, ClipY, ClipY2, and texture.

Referenced by CStdFont::AddRenderedChar(), CreateColorByOwner(), C4Draw::DrawBoxDw(), C4Draw::DrawPatternedCircle(), C4Draw::Grayscale(), C4ScenarioListLoader::Entry::Load(), ReadBMP(), and ReadJPEG().

577 {
578  // clip
579  if ((iX<ClipX) || (iX>ClipX2) || (iY<ClipY) || (iY>ClipY2)) return true;
580  // get+lock affected texture
581  if (!texture) return false;
582  texture->Lock();
583  // if color is fully transparent, ensure it's black
584  if (dwClr>>24 == 0x00) dwClr=0x00000000;
585  // ...and set in actual surface
586  texture->SetPix(iX, iY, dwClr);
587  // success
588  return true;
589 }
int ClipX
Definition: C4Surface.h:69
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
int ClipX2
Definition: C4Surface.h:69
int ClipY
Definition: C4Surface.h:69
int ClipY2
Definition: C4Surface.h:69

Here is the caller graph for this function:

bool C4Surface::Unlock ( )

Definition at line 464 of file C4Surface.cpp.

References fPrimary, Locked, pMainSfc, PrimarySurfaceLockBits, texture, and Unlock().

Referenced by CStdFont::AddRenderedChar(), CStdFont::AddSurface(), C4TextureMap::AddTexture(), C4Draw::Blit8(), C4Draw::Blit8Fast(), C4Draw::BlitSimple(), C4Pattern::Clear(), Clear(), CreateColorByOwner(), C4GraphicsSystem::DoSaveScreenshot(), C4Draw::DrawBoxDw(), C4Draw::DrawPatternedCircle(), C4Draw::Grayscale(), CStdFont::Init(), C4ScenarioListLoader::Entry::Load(), ReadBMP(), ReadJPEG(), ReadPNG(), SavePNG(), and Unlock().

465 {
466  // unlock main sfc
467  if (pMainSfc) pMainSfc->Unlock();
468  // locked?
469  if (!Locked) return false;
470  // decrease lock counter; check if zeroed and unlock then
471  Locked--;
472  if (!Locked)
473  {
474  if (fPrimary)
475  {
476  // emulated primary locks in OpenGL
477  delete[] PrimarySurfaceLockBits;
478  PrimarySurfaceLockBits = nullptr;
479  return true;
480  }
481  else
482  {
483  // non-primary unlock: unlock all texture surfaces (if locked)
484  if (texture)
485  texture->Unlock();
486  }
487  }
488  return true;
489 }
bool Unlock()
Definition: C4Surface.cpp:464
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
BYTE * PrimarySurfaceLockBits
Definition: C4Surface.h:67
int Locked
Definition: C4Surface.h:87
bool fPrimary
Definition: C4Surface.h:89
C4Surface * pMainSfc
Definition: C4Surface.h:79

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::UpdateSize ( int  wdt,
int  hgt 
)

Definition at line 301 of file C4Surface.cpp.

References fPrimary, Hgt, and Wdt.

Referenced by C4Application::OnResolutionChanged().

302 {
303  assert(fPrimary);
304  if (!fPrimary)
305  return false;
306  this->Wdt = wdt; this->Hgt = hgt;
307  return true;
308 }
int Wdt
Definition: C4Surface.h:65
int Hgt
Definition: C4Surface.h:65
bool fPrimary
Definition: C4Surface.h:89

Here is the caller graph for this function:

Friends And Related Function Documentation

friend class C4Draw
friend

Definition at line 138 of file C4Surface.h.

friend class C4Pattern
friend

Definition at line 139 of file C4Surface.h.

friend class CStdGL
friend

Definition at line 140 of file C4Surface.h.

Member Data Documentation

bool C4Surface::Attached
protected

Definition at line 88 of file C4Surface.h.

Referenced by Default(), and MoveFrom().

int C4Surface::ClipX

Definition at line 69 of file C4Surface.h.

Referenced by Clip(), Default(), MoveFrom(), NoClip(), and SetPixDw().

int C4Surface::ClipX2

Definition at line 69 of file C4Surface.h.

Referenced by Clip(), Default(), MoveFrom(), NoClip(), and SetPixDw().

int C4Surface::ClipY

Definition at line 69 of file C4Surface.h.

Referenced by Clip(), Default(), MoveFrom(), NoClip(), and SetPixDw().

int C4Surface::ClipY2

Definition at line 69 of file C4Surface.h.

Referenced by Clip(), Default(), MoveFrom(), NoClip(), and SetPixDw().

DWORD C4Surface::ClrByOwnerClr

Definition at line 81 of file C4Surface.h.

Referenced by C4Draw::BlitUnscaled(), Default(), GetClr(), GetPixDw(), and MoveFrom().

bool C4Surface::fIsBackground {false}

Definition at line 70 of file C4Surface.h.

Referenced by Default(), MoveFrom(), and SetBackground().

unsigned int C4Surface::Format

Definition at line 75 of file C4Surface.h.

Referenced by Create(), and MoveFrom().

bool C4Surface::fPrimary
protected
int C4Surface::iTexSize

Definition at line 68 of file C4Surface.h.

Referenced by C4Draw::BlitUnscaled(), Create(), Default(), MoveFrom(), and ReadPNG().

int C4Surface::Locked
protected
CStdGLCtx* C4Surface::pCtx

Definition at line 76 of file C4Surface.h.

Referenced by C4Surface(), Clear(), Default(), PageFlip(), and CStdGL::PrepareRendering().

C4Surface* C4Surface::pNormalSfc

Definition at line 80 of file C4Surface.h.

Referenced by C4Draw::BlitUnscaled(), Default(), and MoveFrom().

BYTE* C4Surface::PrimarySurfaceLockBits

Definition at line 67 of file C4Surface.h.

Referenced by Default(), GetPixDw(), MoveFrom(), and Unlock().

int C4Surface::PrimarySurfaceLockPitch

Definition at line 67 of file C4Surface.h.

Referenced by Default(), GetPixDw(), and MoveFrom().

C4Window* C4Surface::pWindow
protected

Definition at line 86 of file C4Surface.h.

Referenced by C4Surface(), and Default().

int C4Surface::Scale

Definition at line 66 of file C4Surface.h.

Referenced by C4Draw::Blit(), CreateColorByOwner(), Default(), and Load().


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