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 | #include "fsCNativeRenderTexture.h"
#include <2d/CCRenderTexture.h>
#include <2d/CCSprite.h>
#include <base/CCDirector.h>
#include <fsCore/fsPoint.h>
#include <fsCore/fsAffineMtx2d.h>
#include <fsAppCore/fsIDisplay.h>
#include "fsCore/src/fsNMaths.h"
fsCNativeRenderTexture::fsSMutableTexture* fsCNativeRenderTexture::mCurrMutableTexture;
#define SET_UNIFORM(ps, name, value) \
do \
{ \
decltype(value) __v = value; \
auto __loc = (ps)->getUniformLocation(name); \
(ps)->setUniform(__loc, &__v, sizeof(__v)); \
} while (false)
void fsCNativeRenderTexture::saveDo(fsCResourceName const& pFilename)
{
mDynamicTexture->saveToFile(pFilename.nameGet().utf8Get(), true);
}
void fsCNativeRenderTexture::pixelCallback(const unsigned char* pPixels, std::size_t pWidth, std::size_t pHeigth)
{
memcpy(mCurrMutableTexture->mPixels, pPixels, mCurrMutableTexture->len());
}
void fsCNativeRenderTexture::getDynamicTexturePixels()
{
mCurrMutableTexture = mMutableTexture.get();
auto const dynamicTex = mDynamicTexture->getSprite()->getTexture();
// dynamicTex->getBackendTexture()->getBytes(0, 0,
// dynamicTex->getPixelsWide(),dynamicTex->getPixelsHigh(), false, &pixelCallback);
}
void fsCNativeRenderTexture::updateDynamicTexturePixels() const
{
auto const dynamicTex = mDynamicTexture->getSprite()->getTexture();
// static_cast<cocos2d::backend::Texture2DBackend*>(
// dynamicTex->getBackendTexture())->updateData(
// mMutableTexture->mPixels,dynamicTex->getPixelsWide(), dynamicTex->getPixelsHigh(), 0);
}
void fsCNativeRenderTexture::recolourDo(fsSColour4B const& pOld, fsSColour4B const& pNew)
{
getDynamicTexturePixels();
mMutableTexture->mPaintColour = pNew;
for (fsS32 j = 0; j < mMutableTexture->mHeight; ++j)
{
for (fsS32 i = 0; i < mMutableTexture->mWidth; ++i)
{
if (mMutableTexture->get(i,j) == pOld)
{
mMutableTexture->paint(i, j);
}
}
}
updateDynamicTexturePixels();
}
void fsCNativeRenderTexture::paintDo(std::vector<fsPoint> const& pPos, fsSColour4B const& pColour)
{
auto const dynamicTex = mDynamicTexture->getSprite()->getTexture();
getDynamicTexturePixels();
mMutableTexture->mPaintColour = pColour;
std::for_each(std::begin(pPos),
std::end(pPos),
[&](fsPoint p)
{
const fsPoint pt {p.x, dynamicTex->getPixelsHigh() - 1 - p.y};
mMutableTexture->paint(pt.x, pt.y);
});
updateDynamicTexturePixels();
}
void fsCNativeRenderTexture::paintWithSpriteBrushDo(fsPoint const& pPos)
{
mDynamicTexture->begin();
mBrush->setPosition(pPos.x, pPos.y);
mBrush->visit(cocos2d::Director::getInstance()->getRenderer(), mBrushMatrix, cocos2d::Node::FLAGS_TRANSFORM_DIRTY);
mDynamicTexture->end();
}
void fsCNativeRenderTexture::matrixApplyDo(fsAffineMtx2d const& pTransform)
{
mCanvasMatrix.m[12] = pTransform.get(0, 2);
mCanvasMatrix.m[13] = pTransform.get(1, 2);
mCanvasMatrix.m[0] = pTransform.get(0, 0);
mCanvasMatrix.m[1] = pTransform.get(1, 0);
mCanvasMatrix.m[4] = pTransform.get(0, 1);
mCanvasMatrix.m[5] = pTransform.get(1, 1);
}
void fsCNativeRenderTexture::renderDo()
{
mDynamicTexture->visit(cocos2d::Director::getInstance()->getRenderer(), mCanvasMatrix,
cocos2d::Node::FLAGS_TRANSFORM_DIRTY);
}
void fsCNativeRenderTexture::brushSetDo(fsCResourceName const& pBrushFilename)
{
mBrush->initWithFile(pBrushFilename.nameGet().utf8Get());
}
void fsCNativeRenderTexture::targetSetDo()
{
// SET_UNIFORM(mDynamicTexture->getSprite()->getProgramState(), "u_mousePos",
// (cocos2d::Vec2{
// float(fsIInputManager::instanceGet()->mousePosGet().x),
// float(fsIInputManager::instanceGet()->mousePosGet().y)}));
// mDynamicTexture->beginWithClear(0.0, 0.0, 0.0, 1.0);
}
void fsCNativeRenderTexture::targetClearDo()
{
// fsIRenderer::instanceGet()->flush();
// mDynamicTexture->end();
// mDynamicTexture->visit(cocos2d::Director::getInstance()->getRenderer(),
// mCanvasMatrix,cocos2d::Node::FLAGS_DIRTY_MASK);
}
void fsCNativeRenderTexture::shaderCreate() const
{
// const auto fragStr = cocos2d::FileUtils::getInstance()->getStringFromFile(
// cocos2d::FileUtils::getInstance()->fullPathForFilename("shaders/zoomTorch_cx.fsh"));
//
// const auto program = cocos2d::backend::Device::getInstance()->newProgram(
// cocos2d::positionTextureColor_vert, fragStr.c_str());
// const auto programState = new cocos2d::backend::ProgramState(program);
// const auto director = cocos2d::Director::getInstance();
// const auto& screenSizeLocation = programState->getUniformLocation("u_mousePos");
// auto frameSize= director->getOpenGLView()->getFrameSize();
// frameSize.width = static_cast<fsF32>(fsIDisplay::instanceGet()->designCanvasWidthGet());
// frameSize.height = static_cast<fsF32>(fsIDisplay::instanceGet()->designCanvasHeightGet());
// const auto retinaFactor = static_cast<fsF32>(director->getOpenGLView()->getRetinaFactor());
// auto screenSizeInPixels = frameSize * retinaFactor;
// SET_UNIFORM(programState, "u_mousePos", (fsIInputManager::instanceGet()->mousePosGet()));
// SET_UNIFORM(programState, "u_resolution", screenSizeInPixels);
//
// mDynamicTexture->getSprite()->setProgramState(programState);
// CC_SAFE_RELEASE(program);
}
fsCNativeRenderTexture::fsCNativeRenderTexture():<--- Member variable 'fsCNativeRenderTexture::mDynamicScreenTexture' is not initialized in the constructor. [+]Member variable 'fsCNativeRenderTexture::mDynamicScreenTexture' is not initialized in the constructor. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
fsIRenderableTexture(),
mDynamicTexture(nullptr),
mBrush(nullptr)
{
}
fsCNativeRenderTexture::~fsCNativeRenderTexture()
{
}
cocos2d::Node* fsCNativeRenderTexture::implGet() const
{
return mDynamicTexture;
}
fsCNativeRenderTexture::fsSMutableTexture::fsSMutableTexture(fsS32 pW, fsS32 pH, const fsSColour4B& pCol, fsS32 pBpp):
mWidth(fsNMaths::nextPow2(pW)),
mHeight(fsNMaths::nextPow2(pH)),
mPaintColour(pCol),
mBpp(pBpp)
{
mPixels = new fsU8[mWidth * mHeight * mBpp / 8];
for (fsS32 currRow = 0; currRow < mHeight; ++currRow)
{
for (fsS32 currCol = 0; currCol < mWidth; ++currCol)
{
paint(currCol, currRow);
}
}
}
fsCNativeRenderTexture::fsSMutableTexture::~fsSMutableTexture()
{
delete []mPixels;
}
|