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

#ifndef fsCNativeRenderTexturehdr
#define fsCNativeRenderTexturehdr

#include <math/Mat4.h>
#include <fsCore/fsPoint.h>

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

namespace cocos2d
{
	class RenderTexture;
	class Sprite;
	class Texture2D;
}

class fsAffineMtx2d;
class fsIImage;
class fsRect;


class fsCNativeRenderTexture : public fsIRenderableTexture
{
	friend class fsITexture;

public:

	void initialiseScreenDo(fsPoint pDimensions);
	void initialiseSolutionDo(fsIImage* pSolution);

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

protected:

	void resizeDo(fsPoint const& pSize) override;

	void screenShotStartDo() override;
	void screenShotEndDo(const fsStr& pSceneName) override;<--- Function in derived class

	void shadersCreateDo(const fsStr& pVertex, const fsStr& pFragment) override;

	void targetSetDo() override;<--- Function in derived class
	void partialEndDo() override;
	void partialStartDo() override;<--- Function in derived class
	void targetClearDo() override;<--- Function in derived class

	void brushSetDo(fsCResourceName const& pBrushFilename) override;
	void saveDo(fsCResourceName const& pFilename) override;<--- Function in derived class
	void captureRegionSaveDo(const fsRect& pCaptureRect, fsPoint const& pOutputDimensions, fsCResourceName const& pFilename) override;

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

private:

	struct fsSMutableTexture
	{
        ~fsSMutableTexture();
		fsSMutableTexture(fsIImage* pCanvas);
		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 updateShaderParams() const;
	fsPoint screenEffectFocusPosGet() const;

	cocos2d::Node* implGet() const override;

	void updateDynamicTexturePixels() const;
	void getDynamicTexturePixels();
	void screenEffectParamsSetDo(fsPoint const& pFocusPos, fsF32 pRadius) override;
	void screenEffectParamsClearDo() override;

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

	fsCResourceName mSaveFilename;
	fsStr mFragmentShader;
	fsPoint mScreenEffectFocusPos;
	fsF32 mScreenEffectRadius;
	fsBool mScreenEffectFocusOverrideActive;
	
	cocos2d::Mat4 mCanvasMatrix;
	cocos2d::Mat4 mBrushMatrix;

	cocos2d::RenderTexture* mDynamicScreenTexture;
	cocos2d::RenderTexture* mDynamicTexture;
	cocos2d::Sprite* mBrush;

	std::unique_ptr<fsSMutableTexture> mMutableTexture;	
	static fsSMutableTexture* mCurrMutableTexture;	
};

#endif