Skip to content

Commit d902e51

Browse files
PatriceJiangminggo
authored and
minggo
committed
BlendFunc type refactor, GLenum -> backend::BlendFactor (cocos2d#167)
* BlendFunc use backend::BlendFactor as inner field * update comments * use int to replace GLenum * update xcode project fiel * rename to GLBlendConst * add ccConstants.h * update xcode project file * update copyright
1 parent eef00a9 commit d902e51

36 files changed

+251
-128
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,4 @@ cmake-build-*/
169169
.vs
170170
cmake_build*
171171
metal-support*.zip
172+
**/.project

build/cocos2d_libs.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,9 @@
20902090
ED682BC1213F63CB001BF6CB /* libuv_a.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ED682BC0213F63CB001BF6CB /* libuv_a.a */; };
20912091
ED682BC3213F6C7A001BF6CB /* libwebsockets.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ED682BC2213F6C7A001BF6CB /* libwebsockets.a */; };
20922092
ED682BC5213F6CBF001BF6CB /* libuv_a.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ED682BC4213F6CBE001BF6CB /* libuv_a.a */; };
2093+
ED75D42021E3030E0052B87E /* ccConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = ED75D41F21E3030E0052B87E /* ccConstants.h */; };
2094+
ED75D42121E303160052B87E /* ccConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = ED75D41F21E3030E0052B87E /* ccConstants.h */; };
2095+
ED75D42221E303160052B87E /* ccConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = ED75D41F21E3030E0052B87E /* ccConstants.h */; };
20932096
ED9C6A9418599AD8000A5232 /* CCNodeGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */; };
20942097
ED9C6A9518599AD8000A5232 /* CCNodeGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */; };
20952098
ED9C6A9618599AD8000A5232 /* CCNodeGrid.h in Headers */ = {isa = PBXBuildFile; fileRef = ED9C6A9318599AD8000A5232 /* CCNodeGrid.h */; };
@@ -3340,6 +3343,7 @@
33403343
ED682BC0213F63CB001BF6CB /* libuv_a.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libuv_a.a; path = ../external/uv/prebuilt/ios/libuv_a.a; sourceTree = "<group>"; };
33413344
ED682BC2213F6C7A001BF6CB /* libwebsockets.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libwebsockets.a; path = ../external/websockets/prebuilt/tvos/libwebsockets.a; sourceTree = "<group>"; };
33423345
ED682BC4213F6CBE001BF6CB /* libuv_a.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libuv_a.a; path = ../external/uv/prebuilt/tvos/libuv_a.a; sourceTree = "<group>"; };
3346+
ED75D41F21E3030E0052B87E /* ccConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ccConstants.h; path = ../base/ccConstants.h; sourceTree = "<group>"; };
33433347
ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = CCNodeGrid.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
33443348
ED9C6A9318599AD8000A5232 /* CCNodeGrid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCNodeGrid.h; sourceTree = "<group>"; };
33453349
EDC789E021CA19FA00E1863C /* CCComAudio.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCComAudio.cpp; sourceTree = "<group>"; };
@@ -3841,6 +3845,7 @@
38413845
1A5700A2180BC5E60088DEC7 /* base */ = {
38423846
isa = PBXGroup;
38433847
children = (
3848+
ED75D41F21E3030E0052B87E /* ccConstants.h */,
38443849
461FE08321C34EEB00E5378F /* CCEnumClass.h */,
38453850
291901411B05895600F8B4BA /* CCNinePatchImageParser.h */,
38463851
291901421B05895600F8B4BA /* CCNinePatchImageParser.cpp */,
@@ -6282,6 +6287,7 @@
62826287
B24AA987195A675C007B4522 /* CCFastTMXLayer.h in Headers */,
62836288
1A40D16C1E8E56C7002E363A /* stream.h in Headers */,
62846289
EDC78C5821CA19FC00E1863C /* CCActionFrameEasing.h in Headers */,
6290+
ED75D42021E3030E0052B87E /* ccConstants.h in Headers */,
62856291
4631CBD121AFD9FA0000D408 /* UIScale9Sprite.h in Headers */,
62866292
50ABBEBD1925AB6F00A911A9 /* ccUtils.h in Headers */,
62876293
A0534A651B872FFD006B03E5 /* CCDownloader-apple.h in Headers */,
@@ -6597,6 +6603,7 @@
65976603
507B3F691C31BDD30067B53E /* CCAnimationCache.h in Headers */,
65986604
50864CC31C7BC1B100B3BAB1 /* cpPolyline.h in Headers */,
65996605
507B3F6E1C31BDD30067B53E /* CCTouch.h in Headers */,
6606+
ED75D42221E303160052B87E /* ccConstants.h in Headers */,
66006607
50864C961C7BC1B000B3BAB1 /* chipmunk_types.h in Headers */,
66016608
507B3F7A1C31BDD30067B53E /* CCEventListenerMouse.h in Headers */,
66026609
1A40D13E1E8E56C7002E363A /* regex.h in Headers */,
@@ -6759,6 +6766,7 @@
67596766
EDC78BEB21CA19FB00E1863C /* CCDisplayManager.h in Headers */,
67606767
460D4CD421A5666E0075726E /* CCRenderer.h in Headers */,
67616768
50ABBDB41925AB4100A911A9 /* ccShaders.h in Headers */,
6769+
ED75D42121E303160052B87E /* ccConstants.h in Headers */,
67626770
50ABBE861925AB6F00A911A9 /* ccFPSImages.h in Headers */,
67636771
50ABBE2E1925AB6F00A911A9 /* ccCArray.h in Headers */,
67646772
1A40D10D1E8E56C7002E363A /* document-wrapper.h in Headers */,

cocos/2d/CCParticleBatchNode.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,8 @@ void ParticleBatchNode::draw(Renderer* renderer, const Mat4 & transform, uint32_
437437
//TODO: minggo: don't set blend factor every frame.
438438
auto& blendDescriptor = _customCommand.getPipelineDescriptor().blendDescriptor;
439439
blendDescriptor.blendEnabled = true;
440-
blendDescriptor.sourceRGBBlendFactor = blendDescriptor.sourceAlphaBlendFactor = utils::toBackendBlendFactor(_blendFunc.src);
441-
blendDescriptor.destinationRGBBlendFactor = blendDescriptor.destinationAlphaBlendFactor = utils::toBackendBlendFactor(_blendFunc.dst);
440+
blendDescriptor.sourceRGBBlendFactor = blendDescriptor.sourceAlphaBlendFactor =_blendFunc.src;
441+
blendDescriptor.destinationRGBBlendFactor = blendDescriptor.destinationAlphaBlendFactor =_blendFunc.dst;
442442

443443
if (_textureAtlas->isDirty())
444444
{

cocos/2d/CCParticleSystem.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ THE SOFTWARE.
5454
#include "base/CCDirector.h"
5555
#include "base/CCProfiling.h"
5656
#include "base/ccUTF8.h"
57+
#include "base/ccUtils.h"
5758
#include "renderer/CCTextureCache.h"
5859
#include "platform/CCFileUtils.h"
5960

@@ -336,13 +337,13 @@ bool ParticleSystem::initWithDictionary(ValueMap& dictionary, const std::string&
336337
// blend function
337338
if (!_configName.empty())
338339
{
339-
_blendFunc.src = dictionary["blendFuncSource"].asFloat();
340+
_blendFunc.src = utils::toBackendBlendFactor((int)dictionary["blendFuncSource"].asFloat());
340341
}
341342
else
342343
{
343-
_blendFunc.src = dictionary["blendFuncSource"].asInt();
344+
_blendFunc.src = utils::toBackendBlendFactor(dictionary["blendFuncSource"].asInt());
344345
}
345-
_blendFunc.dst = dictionary["blendFuncDestination"].asInt();
346+
_blendFunc.dst = utils::toBackendBlendFactor(dictionary["blendFuncDestination"].asInt());
346347

347348
// color
348349
_startColor.r = dictionary["startColorRed"].asFloat();
@@ -1112,7 +1113,7 @@ void ParticleSystem::setBlendAdditive(bool additive)
11121113

11131114
bool ParticleSystem::isBlendAdditive() const
11141115
{
1115-
return( _blendFunc.src == GL_SRC_ALPHA && _blendFunc.dst == GL_ONE);
1116+
return( _blendFunc.src == backend::BlendFactor::SRC_ALPHA && _blendFunc.dst == backend::BlendFactor::ONE);
11161117
}
11171118

11181119
// ParticleSystem - Properties of Gravity Mode

cocos/2d/CCParticleSystem.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -833,8 +833,8 @@ class CC_DLL ParticleSystem : public Node, public TextureProtocol, public Playab
833833
/** whether or not the particles are using blend additive.
834834
If enabled, the following blending function will be used.
835835
@code
836-
source blend function = GL_SRC_ALPHA;
837-
dest blend function = GL_ONE;
836+
source blend function = BlendFactor::SRC_ALPHA;
837+
dest blend function = BlendFactor::ONE;
838838
@endcode
839839
*/
840840
bool _isBlendAdditive;

cocos/2d/CCRenderTexture.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ class CC_DLL RenderTexture : public Node
346346
ClearFlag _clearFlags;
347347

348348
/** The Sprite being used.
349-
The sprite, by default, will use the following blending function: GL_ONE, GL_ONE_MINUS_SRC_ALPHA.
349+
The sprite, by default, will use the following blending function: BlendFactor::ONE, BlendFactor::ONE_MINUS_SRC_ALPHA.
350350
The blending function can be changed in runtime by calling:
351-
- renderTexture->getSprite()->setBlendFunc((BlendFunc){GL_ONE, GL_ONE_MINUS_SRC_ALPHA});
351+
- renderTexture->getSprite()->setBlendFunc((BlendFunc){BlendFactor::ONE, BlendFactor::ONE_MINUS_SRC_ALPHA});
352352
*/
353353
Sprite* _sprite = nullptr;
354354

cocos/base/CCDirector.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ void Director::setAlphaBlending(bool on)
720720
// }
721721
// else
722722
// {
723-
// utils::setBlending(GL_ONE, GL_ZERO);
723+
// utils::setBlending(BlendFactor::ONE, BlendFactor::ZERO);
724724
// }
725725
//
726726
// CHECK_GL_ERROR_DEBUG();

cocos/base/CCProtocols.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class CC_DLL BlendProtocol
177177
* Sets the source blending function.
178178
*
179179
* @param blendFunc A structure with source and destination factor to specify pixel arithmetic,
180-
* e.g. {GL_ONE, GL_ONE}, {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA}.
180+
* e.g. {BlendFactor::ONE, BlendFactor::ONE}, {BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA}.
181181
* @js NA
182182
* @lua NA
183183
*/
@@ -197,9 +197,9 @@ class CC_DLL BlendProtocol
197197
* Node objects that uses a Texture2D to render the images.
198198
* The texture can have a blending function.
199199
* If the texture has alpha premultiplied the default blending function is:
200-
* src=GL_ONE dst= GL_ONE_MINUS_SRC_ALPHA
200+
* src=BlendFactor::ONE dst= BlendFactor::ONE_MINUS_SRC_ALPHA
201201
* else
202-
* src=GL_SRC_ALPHA dst= GL_ONE_MINUS_SRC_ALPHA
202+
* src=BlendFactor::SRC_ALPHA dst= BlendFactor::ONE_MINUS_SRC_ALPHA
203203
* But you can change the blending function at any time.
204204
*/
205205
class CC_DLL TextureProtocol : public BlendProtocol

cocos/base/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ set(COCOS_BASE_HEADER
3535
base/etc1.h
3636
base/CCGameController.h
3737
base/CCConsole.h
38+
base/ccConstants.h
3839
base/CCEvent.h
3940
base/ccTypes.h
4041
base/CCAsyncTaskPool.h

cocos/base/ccConstants.h

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/****************************************************************************
2+
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
3+
http://www.cocos2d-x.org
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+
****************************************************************************/
23+
24+
#pragma once
25+
26+
#include "base/ccMacros.h"
27+
28+
NS_CC_BEGIN
29+
30+
namespace GLBlendConst {
31+
const int ZERO = 0; //GL_ZERO;
32+
const int ONE = 1; //GL_ONE;
33+
const int SRC_COLOR = 0x0300; //GL_SRC_COLOR;
34+
const int ONE_MINUS_SRC_COLOR = 0x0301; //GL_ONE_MINUS_SRC_COLOR;
35+
const int DST_COLOR = 0x0306; //GL_DST_COLOR;
36+
const int ONE_MINUS_DST_COLOR = 0x0307; //GL_ONE_MINUS_DST_COLOR;
37+
const int SRC_ALPHA = 0x0302; //GL_SRC_ALPHA;
38+
const int ONE_MINUS_SRC_ALPHA = 0x0303; //GL_ONE_MINUS_SRC_ALPHA;
39+
const int DST_ALPHA = 0x0304; //GL_DST_ALPHA;
40+
const int ONE_MINUS_DST_ALPHA = 0x0305; //GL_ONE_MINUS_DST_ALPHA;
41+
const int CONSTANT_ALPHA = 0x8003; //GL_CONSTANT_ALPHA;
42+
const int ONE_MINUS_CONSTANT_ALPHA = 0x8004; //GL_ONE_MINUS_CONSTANT_ALPHA;
43+
const int SRC_ALPHA_SATURATE = 0x0308; //GL_SRC_ALPHA_SATURATE;
44+
const int BLEND_COLOR = 0x8005; //GL_BLEND_COLOR
45+
}
46+
47+
48+
NS_CC_END

cocos/base/ccMacros.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ simple macro that swaps 2 variables
9999
/** @def CC_BLEND_SRC
100100
default gl blend src function. Compatible with premultiplied alpha images.
101101
*/
102-
#define CC_BLEND_SRC GL_ONE
103-
#define CC_BLEND_DST GL_ONE_MINUS_SRC_ALPHA
102+
#define CC_BLEND_SRC cocos2d::backend::BlendFactor::ONE
103+
#define CC_BLEND_DST cocos2d::backend::BlendFactor::ONE_MINUS_SRC_ALPHA
104104

105105

106106
/** @def CC_NODE_DRAW_SETUP

cocos/base/ccTypes.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2727
****************************************************************************/
2828

2929
#include "base/ccTypes.h"
30+
#include "renderer/backend/Types.h"
3031

3132
NS_CC_BEGIN
3233

@@ -303,9 +304,9 @@ const Color4F Color4F::BLACK ( 0, 0, 0, 1);
303304
const Color4F Color4F::ORANGE ( 1, 0.5f, 0, 1);
304305
const Color4F Color4F::GRAY (0.65f, 0.65f, 0.65f, 1);
305306

306-
const BlendFunc BlendFunc::DISABLE = {GL_ONE, GL_ZERO};
307-
const BlendFunc BlendFunc::ALPHA_PREMULTIPLIED = {GL_ONE, GL_ONE_MINUS_SRC_ALPHA};
308-
const BlendFunc BlendFunc::ALPHA_NON_PREMULTIPLIED = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA};
309-
const BlendFunc BlendFunc::ADDITIVE = {GL_SRC_ALPHA, GL_ONE};
307+
const BlendFunc BlendFunc::DISABLE = {backend::BlendFactor::ONE, backend::BlendFactor::ZERO};
308+
const BlendFunc BlendFunc::ALPHA_PREMULTIPLIED = { backend::BlendFactor::ONE, backend::BlendFactor::ONE_MINUS_SRC_ALPHA};
309+
const BlendFunc BlendFunc::ALPHA_NON_PREMULTIPLIED = { backend::BlendFactor::SRC_ALPHA, backend::BlendFactor::ONE_MINUS_SRC_ALPHA};
310+
const BlendFunc BlendFunc::ADDITIVE = { backend::BlendFactor::SRC_ALPHA, backend::BlendFactor::ONE};
310311

311312
NS_CC_END

cocos/base/ccTypes.h

+11-6
Original file line numberDiff line numberDiff line change
@@ -404,23 +404,28 @@ struct CC_DLL V3F_T2F_Quad
404404
V3F_T2F tr;
405405
};
406406

407+
408+
namespace backend {
409+
enum class BlendFactor: uint32_t;
410+
}
411+
407412
/** @struct BlendFunc
408413
* Blend Function used for textures.
409414
*/
410415
struct CC_DLL BlendFunc
411416
{
412417
/** source blend function */
413-
GLenum src;
418+
backend::BlendFactor src;
414419
/** destination blend function */
415-
GLenum dst;
420+
backend::BlendFactor dst;
416421

417-
/** Blending disabled. Uses {GL_ONE, GL_ZERO} */
422+
/** Blending disabled. Uses {BlendFactor::ONE, BlendFactor::ZERO} */
418423
static const BlendFunc DISABLE;
419-
/** Blending enabled for textures with Alpha premultiplied. Uses {GL_ONE, GL_ONE_MINUS_SRC_ALPHA} */
424+
/** Blending enabled for textures with Alpha premultiplied. Uses {BlendFactor::ONE, BlendFactor::ONE_MINUS_SRC_ALPHA} */
420425
static const BlendFunc ALPHA_PREMULTIPLIED;
421-
/** Blending enabled for textures with Alpha NON premultiplied. Uses {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA} */
426+
/** Blending enabled for textures with Alpha NON premultiplied. Uses {BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA} */
422427
static const BlendFunc ALPHA_NON_PREMULTIPLIED;
423-
/** Enables Additive blending. Uses {GL_SRC_ALPHA, GL_ONE} */
428+
/** Enables Additive blending. Uses {BlendFactor::SRC_ALPHA, BlendFactor::ONE} */
424429
static const BlendFunc ADDITIVE;
425430

426431
bool operator==(const BlendFunc &a) const

0 commit comments

Comments
 (0)