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