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

#ifndef fsCNativeRenderTextureV3hdr
#define fsCNativeRenderTextureV3hdr

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

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

namespace ax
{
	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 updateShaderParams();
	void shaderEffectRefresh(const fsStr& pFragmentShader);
	fsPoint screenEffectFocusPosGet() const;

	void shadersCreateDo(const fsStr& pVertex, const fsStr& pFragment) override;
	void screenEffectParamsSetDo(fsPoint const& pFocusPos, fsF32 pRadius) override;
	void screenEffectParamsClearDo() override;

	void targetSetDo() override;
	void partialEndDo() override;
	void partialStartDo() override;
	void targetClearDo() override;

	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;
	};

	ax::Node* implGet() const override;

	void updateDynamicTexturePixels() const;
	fsCAxmolRenderTexture* screenshotTargetGet() const;
	void screenTextureDestroy();
	void dynamicTextureDestroy();

	enum class eScreenEffect
	{
		eNONE,
		eMAGNIFY,
		eTORCH
	};

	fsCResourceName mSaveFilename;
	fsStr mFragmentShader;
	eScreenEffect mScreenEffect;
	fsPoint mScreenEffectFocusPos;
	fsF32 mScreenEffectRadius;
	fsBool mScreenEffectFocusOverrideActive;

	ax::Mat4 mCanvasMatrix;
	ax::Mat4 mBrushMatrix;

	fsCAxmolRenderTexture* mDynamicScreenTexture;
	ax::Sprite* mDynamicScreenSprite;
	fsCAxmolRenderTexture* mDynamicTexture;
	ax::Sprite* mDynamicTextureSprite;

	ax::Sprite* mBrush;

	std::unique_ptr<fsSMutableTexture> mMutableTexture;
};

#endif