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

Constructor & Destructor Documentation

C4Surface::C4Surface ( )

Definition at line 47 of file C4Surface.cpp.

References Default().

47  : fIsBackground(false)
48 {
49  Default();
50 }
void Default()
Definition: C4Surface.cpp:81
bool fIsBackground
Definition: C4Surface.h:72

Here is the call graph for this function:

C4Surface::~C4Surface ( )

Definition at line 73 of file C4Surface.cpp.

References Clear().

74 {
75  /* for (C4ObjectLink *lnk = ::Objects.First; lnk; lnk=lnk->Next)
76  if (lnk->Obj->Menu)
77  lnk->Obj->Menu->AssertSurfaceNotUsed(this);*/
78  Clear();
79 }
void Clear()
Definition: C4Surface.cpp:136

Here is the call graph for this function:

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

Definition at line 52 of file C4Surface.cpp.

References Create(), and Default().

52  : fIsBackground(false)
53 {
54  Default();
55  // create
56  Create(iWdt, iHgt, iFlags);
57 }
void Default()
Definition: C4Surface.cpp:81
bool fIsBackground
Definition: C4Surface.h:72
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:171

Here is the call graph for this function:

C4Surface::C4Surface ( C4AbstractApp pApp,
C4Window pWindow 
)

Definition at line 59 of file C4Surface.cpp.

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

59  :
60  Wdt(0), Hgt(0)
61 {
62  Default();
63  fPrimary=true;
64  this->pWindow=pWindow;
65  // create rendering context
66 #ifndef USE_CONSOLE
67  pCtx = pGL->CreateContext(pWindow, pApp);
68 #endif
69  // reset clipping
70  NoClip();
71 }
int Wdt
Definition: C4Surface.h:67
C4Window * pWindow
Definition: C4Surface.h:88
CStdGLCtx * pCtx
Definition: C4Surface.h:78
void Default()
Definition: C4Surface.cpp:81
virtual CStdGLCtx * CreateContext(C4Window *pWindow, C4AbstractApp *pApp) override
Definition: C4DrawGL.cpp:291
int Hgt
Definition: C4Surface.h:67
CStdGL * pGL
Definition: C4DrawGL.cpp:914
bool fPrimary
Definition: C4Surface.h:91
void NoClip()
Definition: C4Surface.cpp:160

Here is the call graph for this function:

Member Function Documentation

bool C4Surface::AttachPalette ( )

Definition at line 444 of file C4Surface.cpp.

445 {
446  return true;
447 }
bool C4Surface::BltPix ( int  iX,
int  iY,
C4Surface sfcSource,
int  iSrcX,
int  iSrcY,
bool  fTransparency 
)

Definition at line 601 of file C4Surface.cpp.

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

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

602 {
603  // 32bit-blit. lock target
604  if (!texture) return false;
605  texture->Lock();
606  DWORD *pPix32 = (DWORD *)(((BYTE *)texture->texLock.pBits) + iY*texture->texLock.Pitch + iX * 4);
607  // get source pix as dword
608  DWORD srcPix=sfcSource->GetPixDw(iSrcX, iSrcY, true);
609  // merge
610  if (!fTransparency)
611  {
612  // set it
613  *pPix32=srcPix;
614  }
615  else
616  {
618  BltAlphaAdd(*pPix32, srcPix);
619  else
620  BltAlpha(*pPix32, srcPix);
621  }
622  // done
623  return true;
624 }
#define C4GFXBLIT_ADDITIVE
Definition: C4Surface.h:28
uint8_t BYTE
C4Draw * pDraw
Definition: C4Draw.cpp:45
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
Definition: C4Surface.cpp:501
void BltAlpha(DWORD &dwDst, DWORD dwSrc)
Definition: StdColors.h:33
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:80
void BltAlphaAdd(DWORD &dwDst, DWORD dwSrc)
Definition: StdColors.h:44
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 136 of file C4Surface.cpp.

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

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

137 {
138  // Undo all locks
139  while (Locked) Unlock();
140  // release surface
141 #ifndef USE_CONSOLE
142  if (pCtx)
143  {
144  delete pCtx;
145  pCtx = 0;
146  }
147 #endif
148  texture.reset();
149 #ifdef _DEBUG
150  dbg_idx = 0;
151 #endif
152 }
CStdGLCtx * pCtx
Definition: C4Surface.h:78
bool Unlock()
Definition: C4Surface.cpp:474
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:80
int Locked
Definition: C4Surface.h:89

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 626 of file C4Surface.cpp.

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

Referenced by C4LandscapeRenderGL::Update().

627 {
628  // lock
629  if (!Locked) return;
630  // clip to target size
631  if (iX<0) { iWdt+=iX; iX=0; }
632  if (iY<0) { iHgt+=iY; iY=0; }
633  int iOver;
634  iOver=Wdt-(iX+iWdt); if (iOver<0) iWdt+=iOver;
635  iOver=Hgt-(iY+iHgt); if (iOver<0) iHgt+=iOver;
636  C4Rect rtClear{ iX, iY, iWdt, iHgt };
637  if (pMainSfc && pMainSfc->texture)
638  {
639  // assuming this is only valid as long as there's no texture management,
640  // organizing partially used textures together!
641  pMainSfc->texture->ClearRect(rtClear);
642  }
643  // clear this texture
644  texture->ClearRect(rtClear);
645 }
int Wdt
Definition: C4Surface.h:67
Definition: C4Rect.h:29
int Hgt
Definition: C4Surface.h:67
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:80
int Locked
Definition: C4Surface.h:89
C4Surface * pMainSfc
Definition: C4Surface.h:81

Here is the caller graph for this function:

void C4Surface::Clip ( int  iX,
int  iY,
int  iX2,
int  iY2 
)

Definition at line 165 of file C4Surface.cpp.

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

166 {
167  ClipX=Clamp(iX,0,Wdt-1); ClipY=Clamp(iY,0,Hgt-1);
168  ClipX2=Clamp(iX2,0,Wdt-1); ClipY2=Clamp(iY2,0,Hgt-1);
169 }
int Wdt
Definition: C4Surface.h:67
int ClipX
Definition: C4Surface.h:71
int Hgt
Definition: C4Surface.h:67
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:46
int ClipX2
Definition: C4Surface.h:71
int ClipY
Definition: C4Surface.h:71
int ClipY2
Definition: C4Surface.h:71

Here is the call graph for this function:

bool C4Surface::Copy ( C4Surface fromSfc)

Definition at line 194 of file C4Surface.cpp.

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

Referenced by C4FacetSurface::CopyFromSfcMaxSize().

195 {
196  // Clear anything old
197  Clear();
198  // Default to other surface's color depth
199  Default();
200  // Create surface (TODO: copy flags)
201  if (!Create(fromSfc.Wdt, fromSfc.Hgt)) return false;
202  // Blit copy
203  if (!pDraw->BlitSurface(&fromSfc, this, 0, 0, false))
204  { Clear(); return false; }
205  // Success
206  return true;
207 }
int Wdt
Definition: C4Surface.h:67
void Default()
Definition: C4Surface.cpp:81
int Hgt
Definition: C4Surface.h:67
bool BlitSurface(C4Surface *sfcSurface, C4Surface *sfcTarget, int tx, int ty, bool fBlitBase)
Definition: C4Draw.cpp:520
void Clear()
Definition: C4Surface.cpp:136
C4Draw * pDraw
Definition: C4Draw.cpp:45
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:171

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

172 {
173  Clear(); Default();
174  // check size
175  if (!iWdt || !iHgt) return false;
176  Wdt=iWdt; Hgt=iHgt;
177  // create texture: check gfx system
178  if (!pDraw) return false;
179  if (!pDraw->DeviceReady()) return false;
180 
181  // store color format that will be used
182 #ifndef USE_CONSOLE
183  Format=pGL->sfcFmt;
184 #endif
185  // create texture
186  iTexSize = std::max(iWdt, iHgt);
187  texture = std::make_unique<C4TexRef>(iWdt, iHgt, iFlags);
188  // update clipping
189  NoClip();
190  // success
191  return true;
192 }
int Wdt
Definition: C4Surface.h:67
virtual bool DeviceReady()=0
void Default()
Definition: C4Surface.cpp:81
int Hgt
Definition: C4Surface.h:67
GLenum sfcFmt
Definition: C4DrawGL.h:177
unsigned int Format
Definition: C4Surface.h:77
void Clear()
Definition: C4Surface.cpp:136
int iTexSize
Definition: C4Surface.h:70
C4Draw * pDraw
Definition: C4Draw.cpp:45
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:80
CStdGL * pGL
Definition: C4DrawGL.cpp:914
void NoClip()
Definition: C4Surface.cpp:160

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Surface::CreateColorByOwner ( C4Surface pBySurface)

Definition at line 265 of file C4Surface.cpp.

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

Referenced by C4FacetSurface::CreateClrByOwner().

266 {
267  // safety
268  if (!pBySurface) return false;
269  if (!pBySurface->texture) return false;
270  // create in same size
271  if (!Create(pBySurface->Wdt, pBySurface->Hgt)) return false;
272  // copy scale
273  Scale = pBySurface->Scale;
274  // set main surface
275  pMainSfc=pBySurface;
276  // lock it
277  if (!pMainSfc->Lock()) return false;
278  if (!Lock()) { pMainSfc->Unlock(); return false; }
279  // set ColorByOwner-pixels
280  for (int iY=0; iY<Hgt; ++iY)
281  for (int iX=0; iX<Wdt; ++iX)
282  {
283  // get pixel
284  DWORD dwPix=pMainSfc->GetPixDw(iX, iY, false);
285  // is it a ClrByOwner-px?
286  if (!ClrByOwner(dwPix)) continue;
287  // set in this surface
288  SetPixDw(iX, iY, dwPix);
289  // clear in the other
290  pMainSfc->SetPixDw(iX, iY, 0x00ffffff);
291  }
292  // unlock
293  Unlock();
294  pMainSfc->Unlock();
295  // success
296  return true;
297 }
int Wdt
Definition: C4Surface.h:67
bool Lock()
Definition: C4Surface.cpp:463
bool ClrByOwner(DWORD &dwClr)
Definition: C4Surface.cpp:213
bool Unlock()
Definition: C4Surface.cpp:474
int Hgt
Definition: C4Surface.h:67
int Scale
Definition: C4Surface.h:68
bool SetPixDw(int iX, int iY, DWORD dwCol)
Definition: C4Surface.cpp:586
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
Definition: C4Surface.cpp:501
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:80
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:171
uint32_t DWORD
C4Surface * pMainSfc
Definition: C4Surface.h:81

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

82 {
83  Wdt=Hgt=0;
84  Scale=1;
87  Locked=0;
88  Attached=false;
89  fPrimary=false;
90  pMainSfc=nullptr;
91  pNormalSfc=nullptr;
92 #ifndef USE_CONSOLE
93  pCtx=nullptr;
94 #endif
95  pWindow=nullptr;
96  ClrByOwnerClr=0;
97  iTexSize=0;
98  fIsBackground=false;
99 #ifdef _DEBUG
100  dbg_idx = 0;
101 #endif
102 }
int Wdt
Definition: C4Surface.h:67
C4Window * pWindow
Definition: C4Surface.h:88
CStdGLCtx * pCtx
Definition: C4Surface.h:78
DWORD ClrByOwnerClr
Definition: C4Surface.h:83
int ClipX
Definition: C4Surface.h:71
int Hgt
Definition: C4Surface.h:67
int Scale
Definition: C4Surface.h:68
C4Surface * pNormalSfc
Definition: C4Surface.h:82
int iTexSize
Definition: C4Surface.h:70
bool fIsBackground
Definition: C4Surface.h:72
int ClipX2
Definition: C4Surface.h:71
bool Attached
Definition: C4Surface.h:90
BYTE * PrimarySurfaceLockBits
Definition: C4Surface.h:69
int PrimarySurfaceLockPitch
Definition: C4Surface.h:69
int ClipY
Definition: C4Surface.h:71
int Locked
Definition: C4Surface.h:89
bool fPrimary
Definition: C4Surface.h:91
int ClipY2
Definition: C4Surface.h:71
C4Surface * pMainSfc
Definition: C4Surface.h:81

Here is the caller graph for this function:

DWORD C4Surface::GetClr ( )
inline

Definition at line 135 of file C4Surface.h.

References ClrByOwnerClr.

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

135 { return ClrByOwnerClr; }
DWORD ClrByOwnerClr
Definition: C4Surface.h:83

Here is the caller graph for this function:

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

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

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

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 454 of file C4Surface.cpp.

References Hgt, and Wdt.

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

455 {
456  // simply assign stored values
457  irX=Wdt;
458  irY=Hgt;
459  // success
460  return true;
461 }
int Wdt
Definition: C4Surface.h:67
int Hgt
Definition: C4Surface.h:67

Here is the caller graph for this function:

int C4Surface::IsLocked ( ) const
inline

Definition at line 95 of file C4Surface.h.

References Locked.

Referenced by CStdFont::AddSurface().

95 { return Locked; }
int Locked
Definition: C4Surface.h:89

Here is the caller graph for this function:

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

Definition at line 578 of file C4Surface.cpp.

References GetPixDw().

579 {
580  // get pixel value
581  DWORD dwPix=GetPixDw(iX, iY, false);
582  // get alpha value
583  return (dwPix>>24) < 128;
584 }
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
Definition: C4Surface.cpp:501
uint32_t DWORD

Here is the call graph for this function:

bool C4Surface::IsRenderTarget ( )

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

155 {
156  // primary is always OK...
157  return fPrimary;
158 }
bool fPrimary
Definition: C4Surface.h:91

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

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

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 29 of file C4SurfaceLoaders.cpp.

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

Referenced by C4Sky::Init().

30 {
31  // Entry name
32  char szFilename[_MAX_FNAME+1];
33  SCopy(szName,szFilename,_MAX_FNAME);
34  char *szExt = GetExtension(szFilename);
35  if (!*szExt)
36  {
37  // no extension: Default to extension that is found as file in group
38  const char * const extensions[] = { "png", "bmp", "jpeg", "jpg", nullptr };
39  int i = 0; const char *szExt;
40  while ((szExt = extensions[i++]))
41  {
42  EnforceExtension(szFilename, szExt);
43  if (hGroup.FindEntry(szFilename)) break;
44  }
45  }
46  // Load surface
47  return Load(hGroup,szFilename,fOwnPal,fNoErrIfNotFound,iFlags);
48 }
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:122
#define _MAX_FNAME
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:131
bool Load(C4Group &hGroup, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags)
void EnforceExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:299

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 51 of file C4SurfaceLoaders.cpp.

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

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

Here is the call graph for this function:

bool C4Surface::Lock ( )

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

464 {
465  // lock main sfc
466  if (pMainSfc) if (!pMainSfc->Lock()) return false;
467  // lock texture
468  if (!Locked && !fPrimary && !texture)
469  return false;
470  // count lock
471  Locked++; return true;
472 }
bool Lock()
Definition: C4Surface.cpp:463
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:80
int Locked
Definition: C4Surface.h:89
bool fPrimary
Definition: C4Surface.h:91
C4Surface * pMainSfc
Definition: C4Surface.h:81

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Surface::MoveFrom ( C4Surface psfcFrom)

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

105 {
106  // clear own
107  Clear();
108  // safety
109  if (!psfcFrom) return;
110  // grab data from other sfc
111 #ifdef _DEBUG
112  dbg_idx = psfcFrom->dbg_idx;
113 #endif
114  Wdt=psfcFrom->Wdt; Hgt=psfcFrom->Hgt;
117  psfcFrom->PrimarySurfaceLockBits=nullptr;
118  ClipX=psfcFrom->ClipX; ClipY=psfcFrom->ClipY;
119  ClipX2=psfcFrom->ClipX2; ClipY2=psfcFrom->ClipY2;
120  Locked=psfcFrom->Locked;
121  Attached=psfcFrom->Attached;
122  fPrimary=psfcFrom->fPrimary; // shouldn't be true!
123  texture = std::move(psfcFrom->texture);
124  pMainSfc=psfcFrom->pMainSfc;
125  pNormalSfc=psfcFrom->pNormalSfc;
126  ClrByOwnerClr=psfcFrom->ClrByOwnerClr;
127  iTexSize=psfcFrom->iTexSize;
128 #ifndef USE_CONSOLE
129  Format=psfcFrom->Format;
130 #endif
131  fIsBackground = psfcFrom->fIsBackground;
132  // default other sfc
133  psfcFrom->Default();
134 }
int Wdt
Definition: C4Surface.h:67
void Default()
Definition: C4Surface.cpp:81
DWORD ClrByOwnerClr
Definition: C4Surface.h:83
int ClipX
Definition: C4Surface.h:71
int Hgt
Definition: C4Surface.h:67
unsigned int Format
Definition: C4Surface.h:77
C4Surface * pNormalSfc
Definition: C4Surface.h:82
void Clear()
Definition: C4Surface.cpp:136
int iTexSize
Definition: C4Surface.h:70
bool fIsBackground
Definition: C4Surface.h:72
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:80
int ClipX2
Definition: C4Surface.h:71
bool Attached
Definition: C4Surface.h:90
BYTE * PrimarySurfaceLockBits
Definition: C4Surface.h:69
int PrimarySurfaceLockPitch
Definition: C4Surface.h:69
int ClipY
Definition: C4Surface.h:71
int Locked
Definition: C4Surface.h:89
bool fPrimary
Definition: C4Surface.h:91
int ClipY2
Definition: C4Surface.h:71
C4Surface * pMainSfc
Definition: C4Surface.h:81

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Surface::NoClip ( )

Definition at line 160 of file C4Surface.cpp.

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

Referenced by C4Surface(), and Create().

161 {
162  ClipX=0; ClipY=0; ClipX2=Wdt-1; ClipY2=Hgt-1;
163 }
int Wdt
Definition: C4Surface.h:67
int ClipX
Definition: C4Surface.h:71
int Hgt
Definition: C4Surface.h:67
int ClipX2
Definition: C4Surface.h:71
int ClipY
Definition: C4Surface.h:71
int ClipY2
Definition: C4Surface.h:71

Here is the caller graph for this function:

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

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

321 {
322  assert(fPrimary);
323  if (!fPrimary)
324  return false;
325  // call from gfx thread only!
326  if (!pDraw->pApp || !pDraw->pApp->AssertMainThread()) return false;
327 #ifndef USE_CONSOLE
328  return pCtx->PageFlip();
329 #endif
330  return true;
331 }
bool AssertMainThread()
Definition: C4App.h:123
CStdGLCtx * pCtx
Definition: C4Surface.h:78
C4Draw * pDraw
Definition: C4Draw.cpp:45
virtual bool PageFlip()
bool fPrimary
Definition: C4Surface.h:91
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 137 of file C4SurfaceLoaders.cpp.

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

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

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

334 {
335  int lcnt;
336  C4BMP256Info BitmapInfo;
337  // read bmpinfo-header
338  if (!hGroup.Read(&BitmapInfo,sizeof(C4BMPInfo))) return false;
339  // is it 8bpp?
340  if (BitmapInfo.Info.biBitCount == 8)
341  {
342  if (!hGroup.Read(((BYTE *) &BitmapInfo)+sizeof(C4BMPInfo),
343  std::min(sizeof(BitmapInfo)-sizeof(C4BMPInfo),sizeof(BitmapInfo)-sizeof(C4BMPInfo)+BitmapInfo.FileBitsOffset())))
344  return false;
345  if (!hGroup.Advance(BitmapInfo.FileBitsOffset())) return false;
346  }
347  else
348  {
349  // read 24bpp
350  if (BitmapInfo.Info.biBitCount != 24) return false;
351  if (!hGroup.Advance(((C4BMPInfo) BitmapInfo).FileBitsOffset())) return false;
352  }
353 
354  // Create and lock surface
355  if (!Create(BitmapInfo.Info.biWidth,BitmapInfo.Info.biHeight, iFlags)) return false;
356  if (!Lock()) { Clear(); return false; }
357 
358  // create line buffer
359  int iBufSize=DWordAligned(BitmapInfo.Info.biWidth*BitmapInfo.Info.biBitCount/8);
360  BYTE *pBuf = new BYTE[iBufSize];
361  // Read lines
362  for (lcnt=Hgt-1; lcnt>=0; lcnt--)
363  {
364  if (!hGroup.Read(pBuf, iBufSize))
365  { Clear(); delete [] pBuf; return false; }
366  BYTE *pPix=pBuf;
367  for (int x=0; x<BitmapInfo.Info.biWidth; ++x)
368  switch (BitmapInfo.Info.biBitCount)
369  {
370  case 8:
371  SetPixDw(x, lcnt, C4RGB(
372  BitmapInfo.Colors[*pPix].rgbRed,
373  BitmapInfo.Colors[*pPix].rgbGreen,
374  BitmapInfo.Colors[*pPix].rgbBlue));
375  ++pPix;
376  break;
377  case 24:
378  SetPixDw(x, lcnt, C4RGB(pPix[0], pPix[1], pPix[2]));
379  pPix+=3;
380  break;
381  }
382  }
383  // free buffer again
384  delete [] pBuf;
385 
386  Unlock();
387 
388  return true;
389 }
BYTE rgbRed
Definition: Bitmap256.h:54
int DWordAligned(int val)
Definition: Standard.h:49
bool Lock()
Definition: C4Surface.cpp:463
BITMAPINFOHEADER Info
Definition: Bitmap256.h:67
bool Unlock()
Definition: C4Surface.cpp:474
uint8_t BYTE
int Hgt
Definition: C4Surface.h:67
BYTE rgbGreen
Definition: Bitmap256.h:53
bool SetPixDw(int iX, int iY, DWORD dwCol)
Definition: C4Surface.cpp:586
void Clear()
Definition: C4Surface.cpp:136
#define C4RGB(r, g, b)
Definition: StdColors.h:28
BYTE rgbBlue
Definition: Bitmap256.h:52
virtual bool Advance(int iOffset)=0
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:171
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 285 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().

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

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

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

215 {
216  // Using temporary file at C4Group temp path
217  char szTemp[_MAX_PATH+1];
218  SCopy(C4Group_GetTempPath(),szTemp);
219  SAppend(GetFilename(szFilename),szTemp);
220  MakeTempFilename(szTemp);
221  // Save to temporary file
222  if (!C4Surface::SavePNG(szTemp, fSaveAlpha, fSaveOverlayOnly, false)) return false;
223  // Move temp file to group
224  if (!hGroup.Move(szTemp,GetFilename(szFilename))) return false;
225  // Success
226  return true;
227 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:55
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:227
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:333

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 391 of file C4Surface.cpp.

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

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

Here is the call graph for this function:

bool C4Surface::SetAsClrByOwnerOf ( C4Surface pOfSurface)

Definition at line 299 of file C4Surface.cpp.

References Hgt, pMainSfc, and Wdt.

300 {
301  // safety
302  if (!pOfSurface) return false;
303  if (Wdt != pOfSurface->Wdt || Hgt != pOfSurface->Hgt)
304  return false;
305  // set main surface
306  pMainSfc=pOfSurface;
307  // success
308  return true;
309 }
int Wdt
Definition: C4Surface.h:67
int Hgt
Definition: C4Surface.h:67
C4Surface * pMainSfc
Definition: C4Surface.h:81
void C4Surface::SetBackground ( )
inline

Definition at line 94 of file C4Surface.h.

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

94 { fIsBackground = true; }
bool fIsBackground
Definition: C4Surface.h:72

Here is the caller graph for this function:

void C4Surface::SetClr ( DWORD  toClr)
inline

Definition at line 134 of file C4Surface.h.

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

134 { ClrByOwnerClr=toClr; }
DWORD ClrByOwnerClr
Definition: C4Surface.h:83

Here is the caller graph for this function:

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

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

587 {
588  // clip
589  if ((iX<ClipX) || (iX>ClipX2) || (iY<ClipY) || (iY>ClipY2)) return true;
590  // get+lock affected texture
591  if (!texture) return false;
592  texture->Lock();
593  // if color is fully transparent, ensure it's black
594  if (dwClr>>24 == 0x00) dwClr=0x00000000;
595  // ...and set in actual surface
596  texture->SetPix(iX, iY, dwClr);
597  // success
598  return true;
599 }
int ClipX
Definition: C4Surface.h:71
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:80
int ClipX2
Definition: C4Surface.h:71
int ClipY
Definition: C4Surface.h:71
int ClipY2
Definition: C4Surface.h:71

Here is the caller graph for this function:

bool C4Surface::Unlock ( )

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

475 {
476  // unlock main sfc
477  if (pMainSfc) pMainSfc->Unlock();
478  // locked?
479  if (!Locked) return false;
480  // decrease lock counter; check if zeroed and unlock then
481  Locked--;
482  if (!Locked)
483  {
484  if (fPrimary)
485  {
486  // emulated primary locks in OpenGL
487  delete[] PrimarySurfaceLockBits;
489  return true;
490  }
491  else
492  {
493  // non-primary unlock: unlock all texture surfaces (if locked)
494  if (texture)
495  texture->Unlock();
496  }
497  }
498  return true;
499 }
bool Unlock()
Definition: C4Surface.cpp:474
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:80
BYTE * PrimarySurfaceLockBits
Definition: C4Surface.h:69
int Locked
Definition: C4Surface.h:89
bool fPrimary
Definition: C4Surface.h:91
C4Surface * pMainSfc
Definition: C4Surface.h:81

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 311 of file C4Surface.cpp.

References fPrimary, Hgt, and Wdt.

Referenced by C4Application::OnResolutionChanged().

312 {
313  assert(fPrimary);
314  if (!fPrimary)
315  return false;
316  this->Wdt = wdt; this->Hgt = hgt;
317  return true;
318 }
int Wdt
Definition: C4Surface.h:67
int Hgt
Definition: C4Surface.h:67
bool fPrimary
Definition: C4Surface.h:91

Here is the caller graph for this function:

Friends And Related Function Documentation

friend class C4Draw
friend

Definition at line 140 of file C4Surface.h.

friend class C4Pattern
friend

Definition at line 141 of file C4Surface.h.

friend class CStdGL
friend

Definition at line 142 of file C4Surface.h.

Member Data Documentation

bool C4Surface::Attached
protected

Definition at line 90 of file C4Surface.h.

Referenced by Default(), and MoveFrom().

int C4Surface::ClipX

Definition at line 71 of file C4Surface.h.

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

int C4Surface::ClipX2

Definition at line 71 of file C4Surface.h.

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

int C4Surface::ClipY

Definition at line 71 of file C4Surface.h.

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

int C4Surface::ClipY2

Definition at line 71 of file C4Surface.h.

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

DWORD C4Surface::ClrByOwnerClr

Definition at line 83 of file C4Surface.h.

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

bool C4Surface::fIsBackground

Definition at line 72 of file C4Surface.h.

Referenced by Default(), and MoveFrom().

unsigned int C4Surface::Format

Definition at line 77 of file C4Surface.h.

Referenced by Create(), and MoveFrom().

bool C4Surface::fPrimary
protected
int C4Surface::iTexSize

Definition at line 70 of file C4Surface.h.

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

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

Definition at line 78 of file C4Surface.h.

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

C4Surface* C4Surface::pNormalSfc

Definition at line 82 of file C4Surface.h.

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

BYTE* C4Surface::PrimarySurfaceLockBits

Definition at line 69 of file C4Surface.h.

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

int C4Surface::PrimarySurfaceLockPitch

Definition at line 69 of file C4Surface.h.

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

C4Window* C4Surface::pWindow
protected

Definition at line 88 of file C4Surface.h.

Referenced by C4Surface(), and Default().

int C4Surface::Scale

Definition at line 68 of file C4Surface.h.

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


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