OpenClonk
C4Surface.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // a wrapper class to DirectDraw surfaces
17 
18 #ifndef INC_StdSurface2
19 #define INC_StdSurface2
20 
22 #include "lib/C4Rect.h"
23 
24 // blitting modes
25 #define C4GFXBLIT_NORMAL 0 // regular blit
26 #define C4GFXBLIT_ADDITIVE 1 // all blits additive
27 #define C4GFXBLIT_MOD2 2 // additive color modulation
28 #define C4GFXBLIT_CLRSFC_OWNCLR 4 // do not apply global modulation to ColorByOwner-surface
29 #define C4GFXBLIT_CLRSFC_MOD2 8 // additive color modulation for ClrByOwner-surface
30 #define C4GFXBLIT_WIREFRAME 16 // draws a mesh as wireframe
31 
32 #define C4GFXBLIT_ALL 31 // bist mask covering all blit modes
33 #define C4GFXBLIT_NOADD 30 // bit mask covering all blit modes except additive
34 
35 #define C4GFXBLIT_CUSTOM 128 // custom blitting mode - ignored by gfx system
36 #define C4GFXBLIT_PARENT 256 // blitting mode inherited by parent - ignored by gfx system
37 
38 // bit depth for emulated surfaces
39 #define C4GFX_NOGFX_CLRDEPTH 24
40 
41 const int ALeft=0,ACenter=1,ARight=2;
42 
43 #ifndef USE_CONSOLE
44 class CStdGL;
45 class CStdGLCtx;
46 extern CStdGL *pGL;
47 #endif
48 
49 const int C4SF_Tileable = 1;
50 const int C4SF_MipMap = 2;
51 const int C4SF_Unlocked = 4;
52 
53 class C4Surface
54 {
55 private:
56  C4Surface(const C4Surface &cpy) = delete;
57  C4Surface &operator = (const C4Surface &rCpy) = delete;
58 
59 public:
60  C4Surface();
61  ~C4Surface();
62  C4Surface(int iWdt, int iHgt, int iFlags); // create new surface and init it
63  C4Surface(C4AbstractApp * pApp, C4Window * pWindow); // create new surface for a window
64 public:
65  int Wdt,Hgt; // size of surface
66  int Scale; // scale of image; divide coordinates by this value to get the "original" image size
67  int PrimarySurfaceLockPitch; BYTE *PrimarySurfaceLockBits; // lock data if primary surface is locked
68  int iTexSize; // size of textures
70  bool fIsBackground{false}; // background surfaces fill unused pixels with black, rather than transparency - must be set prior to loading
71 #ifdef _DEBUG
72  unsigned int dbg_idx;
73 #endif
74 #ifndef USE_CONSOLE
75  unsigned int Format; // used color format in textures
77 #endif
78  std::unique_ptr<C4TexRef> texture;
79  C4Surface *pMainSfc; // main surface for simple ColorByOwner-surfaces
80  C4Surface *pNormalSfc; // normal map; can be nullptr
81  DWORD ClrByOwnerClr; // current color to be used for ColorByOwner-blits
82 
83  void MoveFrom(C4Surface *psfcFrom); // grab data from other surface - invalidates other surface
84  bool IsRenderTarget(); // surface can be used as a render target?
85 protected:
87  int Locked;
88  bool Attached;
89  bool fPrimary;
90 
91 public:
92  void SetBackground() { fIsBackground = true; }
93  int IsLocked() const { return Locked; }
94  // Note: This uses partial locks, anything but SetPixDw and Unlock is undefined afterwards until unlock.
95  void ClearBoxDw(int iX, int iY, int iWdt, int iHgt);
96  bool Unlock();
97  bool Lock();
98  DWORD GetPixDw(int iX, int iY, bool fApplyModulation); // get 32bit-px
99  bool IsPixTransparent(int iX, int iY); // is pixel's alpha value <= 0x7f?
100  bool SetPixDw(int iX, int iY, DWORD dwCol); // set pix in surface only
101  bool BltPix(int iX, int iY, C4Surface *sfcSource, int iSrcX, int iSrcY, bool fTransparency); // blit pixel from source to this surface (assumes clipped coordinates!)
102  bool Create(int iWdt, int iHgt, int iFlags = 0);
103  bool Copy(C4Surface &fromSfc);
104  bool CreateColorByOwner(C4Surface *pBySurface); // create ColorByOwner-surface
105  bool SetAsClrByOwnerOf(C4Surface *pOfSurface); // assume that ColorByOwner-surface has been created, and just assign it; fails if the size doesn't match
106 #ifndef USE_CONSOLE
107  bool CreatePrimaryGLTextures(); // create primary textures from back buffer
108 #endif
109  // Only for surfaces which map to a window
110  bool UpdateSize(int wdt, int hgt);
111  bool PageFlip(C4Rect *pSrcRt=nullptr, C4Rect *pDstRt=nullptr);
112 
113  void Clear();
114  void Default();
115  void Clip(int iX, int iY, int iX2, int iY2);
116  void NoClip();
117 
118  // In C4SurfaceLoaders.cpp
119  bool LoadAny(C4Group &hGroup, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags);
120  bool LoadAny(C4GroupSet &hGroupset, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags);
121  bool Load(C4Group &hGroup, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags);
122  bool Save(C4Group &hGroup, const char *szFilename);
123  bool SavePNG(C4Group &hGroup, const char *szFilename, bool fSaveAlpha=true, bool fSaveOverlayOnly=false);
124  bool SavePNG(const char *szFilename, bool fSaveAlpha, bool fSaveOverlayOnly, bool use_background_thread);
125  bool Read(CStdStream &hGroup, const char * extension, int iFlags);
126  bool ReadPNG(CStdStream &hGroup, int iFlags);
127  bool ReadJPEG(CStdStream &hGroup, int iFlags);
128  bool ReadBMP(CStdStream &hGroup, int iFlags);
129 
130  bool AttachPalette();
131  bool GetSurfaceSize(int &irX, int &irY); // get surface size
132  void SetClr(DWORD toClr) { ClrByOwnerClr=toClr; }
133  DWORD GetClr() { return ClrByOwnerClr; }
134 private:
135  void MapBytes(BYTE *bpMap);
136  bool ReadBytes(BYTE **lpbpData, void *bpTarget, int iSize);
137 
138  friend class C4Draw;
139  friend class C4Pattern;
140  friend class CStdGL;
141 };
142 
143 typedef struct _LOCKED_RECT
144 {
145  int Pitch;
146  std::unique_ptr<unsigned char[]> pBits;
148 
149 // one texture encapsulation
150 class C4TexRef
151 {
152 public:
153  LOCKED_RECT texLock; // current lock-data
154 #ifndef USE_CONSOLE
155  unsigned int texName;
156 #endif
157  int iSizeX;
158  int iSizeY;
159  bool fIntLock; // if set, texref is locked internally only
160  int iFlags;
162 
163  C4TexRef(int iSizeX, int iSizeY, int iFlags); // create texture with given size
164  ~C4TexRef(); // release texture
165  bool Lock(); // lock texture
166  // Lock a part of the rect, discarding the content
167  // Note: Calling Lock afterwards without an Unlock first is undefined
168  bool LockForUpdate(C4Rect &rtUpdate);
169  void Unlock(); // unlock texture
170  bool ClearRect(C4Rect &rtClear); // clear rect in texture to transparent
171  bool FillBlack(); // fill complete texture in black
172  void SetPix(int iX, int iY, DWORD v)
173  {
174  *((DWORD *)(((BYTE *)texLock.pBits.get()) + (iY - LockSize.y) * texLock.Pitch + (iX - LockSize.x) * 4)) = v;
175  }
176 private:
177  void CreateTexture();
178  friend class C4TexMgr;
179 };
180 
181 // texture management
182 class C4TexMgr
183 {
184 public:
185  std::list<C4TexRef *> Textures;
186 
187 public:
188  C4TexMgr(); // ctor
189  ~C4TexMgr(); // dtor
190 
191  void RegTex(C4TexRef *pTex);
192  void UnregTex(C4TexRef *pTex);
193 
194  void IntLock(); // do an internal lock
195  void IntUnlock(); // undo internal lock
196 };
197 
198 extern C4TexMgr *pTexMgr;
199 
200 #endif
std::unique_ptr< unsigned char[]> pBits
Definition: C4Surface.h:146
const int ARight
Definition: C4Surface.h:41
const int C4SF_Unlocked
Definition: C4Surface.h:51
struct _LOCKED_RECT LOCKED_RECT
const int ALeft
Definition: C4Surface.h:41
C4TexMgr * pTexMgr
Definition: C4Surface.cpp:879
const int C4SF_MipMap
Definition: C4Surface.h:50
const int C4SF_Tileable
Definition: C4Surface.h:49
const int ACenter
Definition: C4Surface.h:41
CStdGL * pGL
Definition: C4DrawGL.cpp:907
uint8_t BYTE
uint32_t DWORD
int iSize
Definition: TstC4NetIO.cpp:32
Definition: C4Draw.h:85
Definition: C4Rect.h:28
int32_t y
Definition: C4Rect.h:30
int32_t x
Definition: C4Rect.h:30
bool CreateColorByOwner(C4Surface *pBySurface)
Definition: C4Surface.cpp:255
bool Copy(C4Surface &fromSfc)
Definition: C4Surface.cpp:184
int ClipY2
Definition: C4Surface.h:69
bool SetPixDw(int iX, int iY, DWORD dwCol)
Definition: C4Surface.cpp:576
void NoClip()
Definition: C4Surface.cpp:150
bool Load(C4Group &hGroup, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags)
bool UpdateSize(int wdt, int hgt)
Definition: C4Surface.cpp:301
int Wdt
Definition: C4Surface.h:65
int iTexSize
Definition: C4Surface.h:68
int Locked
Definition: C4Surface.h:87
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
Definition: C4Surface.cpp:491
bool AttachPalette()
Definition: C4Surface.cpp:434
int Scale
Definition: C4Surface.h:66
void SetClr(DWORD toClr)
Definition: C4Surface.h:132
unsigned int Format
Definition: C4Surface.h:75
C4Surface * pNormalSfc
Definition: C4Surface.h:80
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
void Clear()
Definition: C4Surface.cpp:126
bool LoadAny(C4Group &hGroup, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags)
bool GetSurfaceSize(int &irX, int &irY)
Definition: C4Surface.cpp:444
bool ReadBMP(CStdStream &hGroup, int iFlags)
Definition: C4Surface.cpp:323
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
CStdGLCtx * pCtx
Definition: C4Surface.h:76
bool Read(CStdStream &hGroup, const char *extension, int iFlags)
bool IsPixTransparent(int iX, int iY)
Definition: C4Surface.cpp:568
int IsLocked() const
Definition: C4Surface.h:93
int ClipY
Definition: C4Surface.h:69
bool fPrimary
Definition: C4Surface.h:89
C4Window * pWindow
Definition: C4Surface.h:86
int ClipX
Definition: C4Surface.h:69
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:161
bool ReadJPEG(CStdStream &hGroup, int iFlags)
DWORD ClrByOwnerClr
Definition: C4Surface.h:81
bool Unlock()
Definition: C4Surface.cpp:464
bool SetAsClrByOwnerOf(C4Surface *pOfSurface)
Definition: C4Surface.cpp:289
bool ReadPNG(CStdStream &hGroup, int iFlags)
bool Save(C4Group &hGroup, const char *szFilename)
void MoveFrom(C4Surface *psfcFrom)
Definition: C4Surface.cpp:94
C4Surface * pMainSfc
Definition: C4Surface.h:79
bool Lock()
Definition: C4Surface.cpp:453
bool BltPix(int iX, int iY, C4Surface *sfcSource, int iSrcX, int iSrcY, bool fTransparency)
Definition: C4Surface.cpp:591
bool CreatePrimaryGLTextures()
void SetBackground()
Definition: C4Surface.h:92
bool PageFlip(C4Rect *pSrcRt=nullptr, C4Rect *pDstRt=nullptr)
Definition: C4Surface.cpp:310
bool IsRenderTarget()
Definition: C4Surface.cpp:144
void ClearBoxDw(int iX, int iY, int iWdt, int iHgt)
Definition: C4Surface.cpp:616
void Clip(int iX, int iY, int iX2, int iY2)
Definition: C4Surface.cpp:155
bool SavePNG(C4Group &hGroup, const char *szFilename, bool fSaveAlpha=true, bool fSaveOverlayOnly=false)
int Hgt
Definition: C4Surface.h:65
DWORD GetClr()
Definition: C4Surface.h:133
void Default()
Definition: C4Surface.cpp:71
void RegTex(C4TexRef *pTex)
Definition: C4Surface.cpp:825
void IntUnlock()
Definition: C4Surface.cpp:863
std::list< C4TexRef * > Textures
Definition: C4Surface.h:185
void UnregTex(C4TexRef *pTex)
Definition: C4Surface.cpp:831
void IntLock()
Definition: C4Surface.cpp:839
bool FillBlack()
Definition: C4Surface.cpp:796
unsigned int texName
Definition: C4Surface.h:155
bool LockForUpdate(C4Rect &rtUpdate)
Definition: C4Surface.cpp:700
int iFlags
Definition: C4Surface.h:160
C4Rect LockSize
Definition: C4Surface.h:161
C4TexRef(int iSizeX, int iSizeY, int iFlags)
Definition: C4Surface.cpp:637
void SetPix(int iX, int iY, DWORD v)
Definition: C4Surface.h:172
void Unlock()
Definition: C4Surface.cpp:754
LOCKED_RECT texLock
Definition: C4Surface.h:153
bool fIntLock
Definition: C4Surface.h:159
bool ClearRect(C4Rect &rtClear)
Definition: C4Surface.cpp:781
int iSizeX
Definition: C4Surface.h:157
bool Lock()
Definition: C4Surface.cpp:728
int iSizeY
Definition: C4Surface.h:158