2009-05-08 13:28:41 +00:00
/*
* OpenClonk , http : //www.openclonk.org
*
2013-12-17 20:01:09 +00:00
* Copyright ( c ) 2001 - 2009 , RedWolf Design GmbH , http : //www.clonk.de/
2016-04-03 18:18:29 +00:00
* Copyright ( c ) 2009 - 2016 , The OpenClonk Team and contributors
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* Distributed under the terms of the ISC license ; see accompanying file
* " COPYING " for details .
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* " Clonk " is a registered trademark of Matthes Bender , used with permission .
* See accompanying file " TRADEMARK " for details .
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* To redistribute this file separately , substitute the full license texts
* for the above references .
2009-05-08 13:28:41 +00:00
*/
// a wrapper class to DirectDraw surfaces
# ifndef INC_StdSurface2
# define INC_StdSurface2
2016-10-22 11:19:22 +00:00
# include "C4ForbidLibraryCompilation.h"
2016-04-03 18:07:56 +00:00
# include "lib/C4Rect.h"
2009-05-08 13:28:41 +00:00
# include <list>
// blitting modes
# define C4GFXBLIT_NORMAL 0 // regular blit
# define C4GFXBLIT_ADDITIVE 1 // all blits additive
# define C4GFXBLIT_MOD2 2 // additive color modulation
# define C4GFXBLIT_CLRSFC_OWNCLR 4 // do not apply global modulation to ColorByOwner-surface
# define C4GFXBLIT_CLRSFC_MOD2 8 // additive color modulation for ClrByOwner-surface
2012-04-17 15:29:29 +00:00
# define C4GFXBLIT_WIREFRAME 16 // draws a mesh as wireframe
2009-05-08 13:28:41 +00:00
2012-04-17 15:29:29 +00:00
# define C4GFXBLIT_ALL 31 // bist mask covering all blit modes
# define C4GFXBLIT_NOADD 30 // bit mask covering all blit modes except additive
2009-05-08 13:28:41 +00:00
# define C4GFXBLIT_CUSTOM 128 // custom blitting mode - ignored by gfx system
# define C4GFXBLIT_PARENT 256 // blitting mode inherited by parent - ignored by gfx system
// bit depth for emulated surfaces
2010-03-28 17:58:21 +00:00
# define C4GFX_NOGFX_CLRDEPTH 24
2009-05-08 13:28:41 +00:00
const int ALeft = 0 , ACenter = 1 , ARight = 2 ;
2013-10-29 13:27:18 +00:00
# ifndef USE_CONSOLE
2009-05-08 13:28:41 +00:00
class CStdGL ;
2010-03-06 14:07:30 +00:00
class CStdGLCtx ;
2009-05-08 13:28:41 +00:00
extern CStdGL * pGL ;
# endif
2015-09-19 01:10:39 +00:00
const int C4SF_Tileable = 1 ;
const int C4SF_MipMap = 2 ;
2016-01-01 02:31:09 +00:00
const int C4SF_Unlocked = 4 ;
2015-09-19 01:10:39 +00:00
2011-10-03 14:06:41 +00:00
class C4Surface
2010-03-28 17:58:21 +00:00
{
private :
2016-02-08 13:38:34 +00:00
C4Surface ( const C4Surface & cpy ) = delete ;
C4Surface & operator = ( const C4Surface & rCpy ) = delete ;
2010-03-28 17:58:21 +00:00
public :
2011-10-03 14:06:41 +00:00
C4Surface ( ) ;
~ C4Surface ( ) ;
2015-09-19 01:10:39 +00:00
C4Surface ( int iWdt , int iHgt , int iFlags ) ; // create new surface and init it
2011-10-03 14:06:41 +00:00
C4Surface ( C4AbstractApp * pApp , C4Window * pWindow ) ; // create new surface for a window
2010-03-28 17:58:21 +00:00
public :
int Wdt , Hgt ; // size of surface
int Scale ; // scale of image; divide coordinates by this value to get the "original" image size
int PrimarySurfaceLockPitch ; BYTE * PrimarySurfaceLockBits ; // lock data if primary surface is locked
int iTexSize ; // size of textures
int ClipX , ClipY , ClipX2 , ClipY2 ;
bool fIsBackground ; // background surfaces fill unused pixels with black, rather than transparency - must be set prior to loading
2009-05-08 13:28:41 +00:00
# ifdef _DEBUG
2016-02-08 13:44:25 +00:00
unsigned int dbg_idx ;
2009-05-08 13:28:41 +00:00
# endif
2013-10-29 13:27:18 +00:00
# ifndef USE_CONSOLE
2016-01-17 22:05:44 +00:00
unsigned int Format ; // used color format in textures
CStdGLCtx * pCtx ;
2009-05-08 13:28:41 +00:00
# endif
2016-02-08 15:14:59 +00:00
std : : unique_ptr < C4TexRef > texture ;
2014-11-07 20:28:52 +00:00
C4Surface * pMainSfc ; // main surface for simple ColorByOwner-surfaces
2016-11-02 23:58:02 +00:00
C4Surface * pNormalSfc ; // normal map; can be nullptr
2010-03-28 17:58:21 +00:00
DWORD ClrByOwnerClr ; // current color to be used for ColorByOwner-blits
2011-10-03 14:06:41 +00:00
void MoveFrom ( C4Surface * psfcFrom ) ; // grab data from other surface - invalidates other surface
2010-03-28 17:58:21 +00:00
bool IsRenderTarget ( ) ; // surface can be used as a render target?
protected :
2011-08-27 21:12:01 +00:00
C4Window * pWindow ;
2010-03-28 17:58:21 +00:00
int Locked ;
bool Attached ;
bool fPrimary ;
public :
void SetBackground ( ) { fIsBackground = true ; }
int IsLocked ( ) const { return Locked ; }
// Note: This uses partial locks, anything but SetPixDw and Unlock is undefined afterwards until unlock.
void ClearBoxDw ( int iX , int iY , int iWdt , int iHgt ) ;
bool Unlock ( ) ;
bool Lock ( ) ;
DWORD GetPixDw ( int iX , int iY , bool fApplyModulation ) ; // get 32bit-px
2010-04-05 12:38:25 +00:00
bool IsPixTransparent ( int iX , int iY ) ; // is pixel's alpha value <= 0x7f?
2010-03-28 17:58:21 +00:00
bool SetPixDw ( int iX , int iY , DWORD dwCol ) ; // set pix in surface only
2011-10-03 14:06:41 +00:00
bool BltPix ( int iX , int iY , C4Surface * sfcSource , int iSrcX , int iSrcY , bool fTransparency ) ; // blit pixel from source to this surface (assumes clipped coordinates!)
2016-01-10 00:18:28 +00:00
bool Create ( int iWdt , int iHgt , int iFlags = 0 ) ;
2011-10-03 14:06:41 +00:00
bool Copy ( C4Surface & fromSfc ) ;
bool CreateColorByOwner ( C4Surface * pBySurface ) ; // create ColorByOwner-surface
bool SetAsClrByOwnerOf ( C4Surface * pOfSurface ) ; // assume that ColorByOwner-surface has been created, and just assign it; fails if the size doesn't match
2013-10-29 13:27:18 +00:00
# ifndef USE_CONSOLE
2010-03-28 17:58:21 +00:00
bool CreatePrimaryGLTextures ( ) ; // create primary textures from back buffer
2009-05-08 13:28:41 +00:00
# endif
2010-03-28 17:58:21 +00:00
// Only for surfaces which map to a window
bool UpdateSize ( int wdt , int hgt ) ;
2016-11-02 23:58:02 +00:00
bool PageFlip ( C4Rect * pSrcRt = nullptr , C4Rect * pDstRt = nullptr ) ;
2010-03-28 17:58:21 +00:00
void Clear ( ) ;
void Default ( ) ;
void Clip ( int iX , int iY , int iX2 , int iY2 ) ;
void NoClip ( ) ;
2011-10-03 14:06:41 +00:00
// In C4SurfaceLoaders.cpp
2015-09-19 01:10:39 +00:00
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 ) ;
2011-10-03 14:06:41 +00:00
bool Save ( C4Group & hGroup , const char * szFilename ) ;
2015-09-28 22:54:34 +00:00
bool SavePNG ( C4Group & hGroup , const char * szFilename , bool fSaveAlpha = true , bool fSaveOverlayOnly = false ) ;
2015-09-30 00:48:34 +00:00
bool SavePNG ( const char * szFilename , bool fSaveAlpha , bool fSaveOverlayOnly , bool use_background_thread ) ;
2015-09-19 01:10:39 +00:00
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 ) ;
2011-10-03 14:06:41 +00:00
2010-03-28 17:58:21 +00:00
bool AttachPalette ( ) ;
bool GetSurfaceSize ( int & irX , int & irY ) ; // get surface size
2010-11-15 11:22:08 +00:00
void SetClr ( DWORD toClr ) { ClrByOwnerClr = toClr ; }
2010-03-28 17:58:21 +00:00
DWORD GetClr ( ) { return ClrByOwnerClr ; }
2015-02-22 16:06:35 +00:00
private :
2010-03-28 17:58:21 +00:00
void MapBytes ( BYTE * bpMap ) ;
bool ReadBytes ( BYTE * * lpbpData , void * bpTarget , int iSize ) ;
2015-02-22 16:06:35 +00:00
2011-10-03 14:34:08 +00:00
friend class C4Draw ;
2011-09-30 20:15:51 +00:00
friend class C4Pattern ;
2009-05-08 13:28:41 +00:00
friend class CStdGL ;
2010-03-28 17:58:21 +00:00
} ;
2009-05-08 13:28:41 +00:00
2013-11-08 09:07:00 +00:00
typedef struct _LOCKED_RECT
2009-05-08 13:28:41 +00:00
{
2010-03-28 17:58:21 +00:00
int Pitch ;
unsigned char * pBits ;
2013-11-08 09:07:00 +00:00
} LOCKED_RECT ;
2009-05-08 13:28:41 +00:00
// one texture encapsulation
2011-10-03 14:16:38 +00:00
class C4TexRef
2010-03-28 17:58:21 +00:00
{
public :
2013-11-08 09:07:00 +00:00
LOCKED_RECT texLock ; // current lock-data
2013-10-29 13:27:18 +00:00
# ifndef USE_CONSOLE
2016-01-17 22:05:44 +00:00
unsigned int texName ;
2010-03-28 17:58:21 +00:00
# endif
2010-09-04 17:30:45 +00:00
int iSizeX ;
int iSizeY ;
2010-03-28 17:58:21 +00:00
bool fIntLock ; // if set, texref is locked internally only
2015-09-19 01:10:39 +00:00
int iFlags ;
2011-03-13 19:32:21 +00:00
C4Rect LockSize ;
2010-03-28 17:58:21 +00:00
2015-09-19 01:10:39 +00:00
C4TexRef ( int iSizeX , int iSizeY , int iFlags ) ; // create texture with given size
2011-10-03 14:16:38 +00:00
~ C4TexRef ( ) ; // release texture
2010-03-28 17:58:21 +00:00
bool Lock ( ) ; // lock texture
2016-02-08 15:14:59 +00:00
// Lock a part of the rect, discarding the content
// Note: Calling Lock afterwards without an Unlock first is undefined
2011-03-13 19:32:21 +00:00
bool LockForUpdate ( C4Rect & rtUpdate ) ;
2010-03-28 17:58:21 +00:00
void Unlock ( ) ; // unlock texture
2011-03-13 19:32:21 +00:00
bool ClearRect ( C4Rect & rtClear ) ; // clear rect in texture to transparent
2010-03-28 17:58:21 +00:00
bool FillBlack ( ) ; // fill complete texture in black
2016-02-01 22:59:38 +00:00
void SetPix ( int iX , int iY , DWORD v )
2010-03-28 17:58:21 +00:00
{
2016-02-08 15:14:59 +00:00
* ( ( DWORD * ) ( ( ( BYTE * ) texLock . pBits ) + ( iY - LockSize . y ) * texLock . Pitch + ( iX - LockSize . x ) * 4 ) ) = v ;
2010-03-28 17:58:21 +00:00
}
2016-01-01 22:45:56 +00:00
private :
void CreateTexture ( ) ;
friend class C4TexMgr ;
2010-03-28 17:58:21 +00:00
} ;
2009-05-08 13:28:41 +00:00
// texture management
2011-10-03 14:16:38 +00:00
class C4TexMgr
2010-03-28 17:58:21 +00:00
{
public :
2011-10-03 14:16:38 +00:00
std : : list < C4TexRef * > Textures ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
public :
2011-10-03 14:16:38 +00:00
C4TexMgr ( ) ; // ctor
~ C4TexMgr ( ) ; // dtor
2009-05-08 13:28:41 +00:00
2011-10-03 14:16:38 +00:00
void RegTex ( C4TexRef * pTex ) ;
void UnregTex ( C4TexRef * pTex ) ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
void IntLock ( ) ; // do an internal lock
void IntUnlock ( ) ; // undo internal lock
} ;
2009-05-08 13:28:41 +00:00
2011-10-03 14:16:38 +00:00
extern C4TexMgr * pTexMgr ;
2009-05-08 13:28:41 +00:00
# endif