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
/********************************************************************
*
*	File:		fsCRenderTexture.h
*
*	Purpose:	Cocos2d-x implementation of a render texture.
*
*********************************************************************/

#ifndef fsCNativeRenderTexturehdr
#define fsCNativeRenderTexturehdr

#include "../../../fsIRenderableTexture.h"
#include "fsINative.h"


class fsCNativeRenderTexture : public fsIRenderableTexture
{
	friend class fsITexture;

public:

	virtual ~fsCNativeRenderTexture();<--- Destructor in derived class
	fsCNativeRenderTexture();

protected:

	void brushSetDo(fsCResourceName const& pBrushFilename) override;<--- Function in derived class
	void saveDo(fsCResourceName const& pFilename) override;<--- Function in derived class

	
	void recolourDo(fsSColour4B const& pOld, fsSColour4B const& pNew) override;<--- Function in derived class
	void paintWithSpriteBrushDo(fsPoint const& pPos) override;<--- Function in derived class
	void paintDo(std::vector<fsPoint> const& pPos, fsSColour4B const& pColour) override;<--- Function in derived class
	
	void renderDo() override;
	void matrixApplyDo(fsAffineMtx2d const& pTransform) override;

private:

	struct fsSMutableTexture
	{
		~fsSMutableTexture();
		fsSMutableTexture(fsS32 pW, fsS32 pH, const fsSColour4B& pCol = {0,0,0,0}, fsS32 pBpp = 32);
		void paint(fsS32 x, fsS32 y)
		{
			mPixels[y * stride() + x * bytesPerPixel() + 0] = mPaintColour.mR;
			mPixels[y * stride() + x * bytesPerPixel() + 1] = mPaintColour.mG;
			mPixels[y * stride() + x * bytesPerPixel() + 2] = mPaintColour.mB;
			mPixels[y * stride() + x * bytesPerPixel() + 3] = mPaintColour.mA;
		}

		fsSColour4B get(fsS32 x, fsS32 y) const
		{
			return fsSColour4B(
					mPixels[y * stride() + x * bytesPerPixel() + 0],
					mPixels[y * stride() + x * bytesPerPixel() + 1],
					mPixels[y * stride() + x * bytesPerPixel() + 2],
					mPixels[y * stride() + x * bytesPerPixel() + 3]);
		}

		fsU32 bytesPerPixel() const { return mBpp / 8; } 
		fsS32 stride() const { return mWidth * bytesPerPixel(); }
		fsS32 len() const { return stride() * mHeight; }
		fsS32 mWidth;
		fsS32 mHeight;
		fsU32 mBpp;
		fsU8* mPixels;
		fsSColour4B mPaintColour;
	};

	void updateDynamicTexturePixels() const;
	void getDynamicTexturePixels();

	static void pixelCallback(const unsigned char* pPixels, std::size_t pWidth, std::size_t pHeigth);

	static fsSMutableTexture* mCurrMutableTexture;	
};

#endif