forked from haxenme/nme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSurface.h
232 lines (177 loc) · 8.06 KB
/
Surface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#ifndef NME_SURFACE_H
#define NME_SURFACE_H
#include <Graphics.h>
#include <Utils.h>
#include <ByteArray.h>
#include <Filters.h>
#include "Hardware.h"
#include <nme/Texture.h>
#include <nme/ImageBuffer.h>
// ---- Surface API --------------
namespace nme
{
void HintColourOrder(bool inRedFirst);
extern int gTextureContextVersion;
class Surface : public ImageBuffer
{
public:
// Non-PO2 will generate dodgy repeating anyhow...
Surface() : mTexture(0), mVersion(0), mFlags(surfNotRepeatIfNonPO2), mAllowTrans(true) { };
// Implementation depends on platform.
static Surface *Load(const OSChar *inFilename);
static Surface *LoadFromBytes(const uint8 *inBytes,int inLen);
bool Encode( nme::ByteArray *outBytes,bool inPNG,double inQuality);
Surface *IncRef() { mRefCount++; return this; }
virtual unsigned int GetFlags() const { return mFlags; }
virtual void SetFlags(unsigned int inFlags) { mFlags = inFlags; }
virtual int GPUFormat() const { return Format(); }
virtual bool GetAllowTrans() const { return mAllowTrans; }
virtual void SetAllowTrans(bool inAllowTrans) { mAllowTrans = inAllowTrans; }
virtual void Clear(uint32 inColour,const Rect *inRect=0) = 0;
virtual void Zero() { Clear(0); }
virtual void createHardwareSurface() { }
virtual void destroyHardwareSurface() { }
virtual void dispose() { }
virtual void MakeTextureOnly() { /*printf("Dumping bits from Surface\n");*/ }
virtual void setGPUFormat( PixelFormat pf ) {}
virtual void multiplyAlpha () {}
virtual void unmultiplyAlpha() { }
int BytesPP() const { return Format()==pfAlpha ? 1 : 4; }
virtual RenderTarget BeginRender(const Rect &inRect,bool inForHitTest=false)=0;
virtual void EndRender()=0;
virtual void BlitTo(const RenderTarget &outTarget, const Rect &inSrcRect,int inPosX, int inPosY,
BlendMode inBlend, const BitmapCache *inMask,
uint32 inTint=0xffffff ) const = 0;
virtual void StretchTo(const RenderTarget &outTarget,
const Rect &inSrcRect, const DRect &inDestRect) const = 0;
virtual void BlitChannel(const RenderTarget &outTarget, const Rect &inSrcRect,
int inPosX, int inPosY,
int inSrcChannel, int inDestChannel ) const = 0;
Texture *GetTexture(HardwareContext *inHardware,int inPlane=0);
void Bind(HardwareContext &inHardware,int inSlot=0);
virtual Surface *clone() { return 0; }
virtual void getPixels(const Rect &inRect,uint32 *outPixels,bool inIgnoreOrder=false,bool inLittleEndian=false) { }
virtual void setPixels(const Rect &inRect,const uint32 *intPixels,bool inIgnoreOrder=false,bool inLittleEndian=false) { }
virtual void getColorBoundsRect(int inMask, int inCol, bool inFind, Rect &outRect)
{
outRect = Rect(0,0,Width(),Height());
}
virtual uint32 getPixel(int inX,int inY) { return 0; }
virtual void setPixel(int inX,int inY,uint32 inRGBA,bool inAlphaToo=false) { }
virtual void scroll(int inDX,int inDY) { }
virtual void colorTransform(const Rect &inRect, ColorTransform &inTransform) { }
virtual void applyFilter(Surface *inSrc, const Rect &inRect, ImagePoint inOffset, Filter *inFilter) { }
virtual void noise(unsigned int randomSeed, unsigned int low, unsigned int high, int channelOptions, bool grayScale) { }
void OnChanged() { mVersion++; }
int Version() const { return mVersion; }
protected:
mutable int mVersion;
Texture *mTexture;
virtual ~Surface();
unsigned int mFlags;
bool mAllowTrans;
};
// Helper class....
class AutoSurfaceRender
{
Surface *mSurface;
RenderTarget mTarget;
public:
AutoSurfaceRender(Surface *inSurface) : mSurface(inSurface),
mTarget(inSurface->BeginRender( Rect(inSurface->Width(),inSurface->Height()),false ) ) { }
AutoSurfaceRender(Surface *inSurface,const Rect &inRect) : mSurface(inSurface),
mTarget(inSurface->BeginRender(inRect,false)) { }
~AutoSurfaceRender() { mSurface->EndRender(); }
const RenderTarget &Target() { return mTarget; }
};
class SimpleSurface : public Surface
{
public:
SimpleSurface(int inWidth,int inHeight,PixelFormat inPixelFormat,int inByteAlign=4,int inGPUPixelFormat=-1);
PixelFormat Format() const { return mPixelFormat; }
int Width() const { return mWidth; }
int Height() const { return mHeight; }
const uint8 *GetBase() const { return mBase; }
uint8 *Edit(const Rect *inRect);
void Commit() { };
int GetStride() const { return mStride; }
int GPUFormat() const { return mGPUPixelFormat; }
void Clear(uint32 inColour,const Rect *inRect);
void Zero();
RenderTarget BeginRender(const Rect &inRect,bool inForHitTest);
void EndRender();
virtual void BlitTo(const RenderTarget &outTarget, const Rect &inSrcRect,int inPosX, int inPosY,
BlendMode inBlend, const BitmapCache *inMask,
uint32 inTint=0xffffff ) const;
virtual void StretchTo(const RenderTarget &outTarget,
const Rect &inSrcRect, const DRect &inDestRect) const;
virtual void BlitChannel(const RenderTarget &outTarget, const Rect &inSrcRect,
int inPosX, int inPosY,
int inSrcChannel, int inDestChannel ) const;
virtual void colorTransform(const Rect &inRect, ColorTransform &inTransform);
virtual void setGPUFormat( PixelFormat pf ) { mGPUPixelFormat = pf; }
void unmultiplyAlpha();
Surface *clone();
void getPixels(const Rect &inRect,uint32 *outPixels,bool inIgnoreOrder=false, bool inLittleEndian=false);
void setPixels(const Rect &inRect,const uint32 *intPixels,bool inIgnoreOrder=false, bool inLittleEndian=false);
void getColorBoundsRect(int inMask, int inCol, bool inFind, Rect &outRect);
uint32 getPixel(int inX,int inY);
void setPixel(int inX,int inY,uint32 inRGBA,bool inAlphaToo=false);
void scroll(int inDX,int inDY);
void applyFilter(Surface *inSrc, const Rect &inRect, ImagePoint inOffset, Filter *inFilter);
void noise(unsigned int randomSeed, unsigned int low, unsigned int high, int channelOptions, bool grayScale);
void createHardwareSurface();
void destroyHardwareSurface();
void dispose();
void MakeTextureOnly();
protected:
int mWidth;
int mHeight;
PixelFormat mPixelFormat;
int mGPUPixelFormat;
int mStride;
uint8 *mBase;
~SimpleSurface();
private:
SimpleSurface(const SimpleSurface &inRHS);
void operator=(const SimpleSurface &inRHS);
};
class HardwareSurface : public Surface
{
public:
HardwareSurface(HardwareRenderer *inContext);
PixelFormat Format() const { return pfHardware; }
int Width() const { return mHardware->Width(); }
int Height() const { return mHardware->Height(); }
const uint8 *GetBase() const { return 0; }
uint8 *Edit(const Rect *inRect=0) { return 0; }
void Commit() { }
int GetStride() const { return 0; }
void Clear(uint32 inColour,const Rect *inRect=0) { mHardware->Clear(inColour,inRect); }
RenderTarget BeginRender(const Rect &inRect,bool inForHitTest)
{
mHardware->BeginRender(inRect,inForHitTest);
return RenderTarget(inRect,mHardware);
}
void EndRender()
{
mHardware->EndRender();
}
void BlitTo(const RenderTarget &outTarget, const Rect &inSrcRect,int inPosX, int inPosY,
BlendMode inBlend, const BitmapCache *inMask,
uint32 inTint ) const { }
void StretchTo(const RenderTarget &outTarget,
const Rect &inSrcRect, const DRect &inDestRect) const { }
void BlitChannel(const RenderTarget &outTarget, const Rect &inSrcRect,
int inPosX, int inPosY,
int inSrcChannel, int inDestChannel ) const { }
Surface *clone();
void getPixels(const Rect &inRect,uint32 *outPixels,bool inIgnoreOrder=false);
void setPixels(const Rect &inRect,const uint32 *intPixels,bool inIgnoreOrder=false);
protected:
~HardwareSurface();
private:
HardwareRenderer *mHardware;
};
} // end namespace nme
#endif