Update CCDirector.h

This commit is contained in:
DelinWorks 2022-07-04 04:58:13 +03:00
parent 38743291e2
commit 6a7a77c604
28 changed files with 538 additions and 444 deletions

View File

@ -46,28 +46,6 @@ struct particle_point
float y;
};
/**
* Particle emission shapes.
* Current supported shapes are Point, Rectangle, RectangularTorus, Circle, Torus, Cone, Cone Torus, Texture alpha
* emission mask
* @since adxe-1.0.0b8
*/
enum class EmissionShapeType
{
// Emission shape of type point
POINT,
// Emission shape of type rectangle
RECT,
// Emission shape of type rectangular torus
RECTTORUS,
// Emission shape of type circle or cone
CIRCLE,
// Emission shape of type torus or cone torus
TORUS,
// Emission shape of type texture alpha mask
TEXTURE_ALPHA_MASK
};
/**
* Particle emission shapes.
* Current supported shapes are Point, Rectangle, RectangularTorus, Circle, Torus, Cone, Cone Torus, Texture alpha

View File

@ -298,14 +298,14 @@ bool Bundle3D::loadObj(MeshDatas& meshdatas,
}
// split into submesh according to material
std::map<int, IndexArray> subMeshMap;
std::map<int, std::vector<unsigned short>> subMeshMap;
for (size_t k = 0, size = mesh.material_ids.size(); k < size; ++k)
{
int id = mesh.material_ids[k];
size_t idx = k * 3;
subMeshMap[id].push_back(mesh.indices[idx], std::true_type{});
subMeshMap[id].push_back(mesh.indices[idx + 1], std::true_type{});
subMeshMap[id].push_back(mesh.indices[idx + 2], std::true_type{});
subMeshMap[id].push_back(mesh.indices[idx]);
subMeshMap[id].push_back(mesh.indices[idx + 1]);
subMeshMap[id].push_back(mesh.indices[idx + 2]);
}
auto node = new NodeData();
@ -447,7 +447,7 @@ bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
for (unsigned int k = 0; k < meshPartCount; ++k)
{
IndexArray indexArray{CustomCommand::IndexFormat::U_SHORT}; // TODO: _version == 1.3 use U_INT?
std::vector<unsigned short> indexArray;
std::string meshPartid = _binaryReader.readString();
meshData->subMeshIds.push_back(meshPartid);
unsigned int nIndexCount;
@ -457,7 +457,7 @@ bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
goto FAILED;
}
indexArray.resize(nIndexCount);
if (_binaryReader.read(indexArray.data(), 2, nIndexCount) != nIndexCount)
if (_binaryReader.read(&indexArray[0], 2, nIndexCount) != nIndexCount)
{
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
goto FAILED;
@ -599,9 +599,9 @@ bool Bundle3D::loadMeshDatasBinary_0_1(MeshDatas& meshdatas)
return false;
}
IndexArray indices;
std::vector<unsigned short> indices;
indices.resize(nIndexCount);
if (_binaryReader.read(indices.data(), 2, nIndexCount) != nIndexCount)
if (_binaryReader.read(&indices[0], 2, nIndexCount) != nIndexCount)
{
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
CC_SAFE_DELETE(meshdata);
@ -720,9 +720,9 @@ bool Bundle3D::loadMeshDatasBinary_0_2(MeshDatas& meshdatas)
return false;
}
IndexArray indices{CustomCommand::IndexFormat::U_SHORT}; /* TODO: _version == 1.3 use U_INT?*/
std::vector<unsigned short> indices;
indices.resize(nIndexCount);
if (_binaryReader.read(indices.data(), 2, nIndexCount) != nIndexCount)
if (_binaryReader.read(&indices[0], 2, nIndexCount) != nIndexCount)
{
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
CC_SAFE_DELETE(meshdata);
@ -777,14 +777,14 @@ bool Bundle3D::loadMeshDatasJson(MeshDatas& meshdatas)
const rapidjson::Value& mesh_part_array = mesh_data[PARTS];
for (rapidjson::SizeType i = 0, mesh_part_array_size = mesh_part_array.Size(); i < mesh_part_array_size; ++i)
{
IndexArray indexArray;
std::vector<unsigned short> indexArray;
const rapidjson::Value& mesh_part = mesh_part_array[i];
meshData->subMeshIds.push_back(mesh_part[ID].GetString());
// index_number
const rapidjson::Value& indices_val_array = mesh_part[INDICES];
for (rapidjson::SizeType j = 0, indices_val_array_size = indices_val_array.Size();
j < indices_val_array_size; ++j)
indexArray.push_back((unsigned int)indices_val_array[j].GetUint());
indexArray.push_back((unsigned short)indices_val_array[j].GetUint());
meshData->subMeshIndices.push_back(indexArray);
meshData->numIndex = (int)meshData->subMeshIndices.size();
@ -810,8 +810,8 @@ bool Bundle3D::loadMeshDatasJson(MeshDatas& meshdatas)
}
else
{
meshData->subMeshAABB.push_back(calculateAABB(meshData->vertex, meshData->getPerVertexSize(),
indexArray));
meshData->subMeshAABB.push_back(
calculateAABB(meshData->vertex, meshData->getPerVertexSize(), indexArray));
}
}
meshdatas.meshDatas.push_back(meshData);
@ -1185,16 +1185,15 @@ bool Bundle3D::loadMeshDataJson_0_1(MeshDatas& meshdatas)
unsigned int indexnum = mesh_data_body_array_0[INDEXNUM].GetUint();
// indices
IndexArray indices;
std::vector<unsigned short> indices;
indices.resize(indexnum);
const rapidjson::Value& indices_val_array = mesh_data_body_array_0[INDICES];
for (rapidjson::SizeType i = 0; i < indices_val_array.Size(); ++i)
indices.at<uint16_t>(i) = (unsigned short)indices_val_array[i].GetUint();
indices[i] = (unsigned short)indices_val_array[i].GetUint();
meshdata->subMeshIndices.push_back(indices);
meshdata->subMeshAABB.push_back(
calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
meshdatas.meshDatas.push_back(meshdata);
return true;
}
@ -1240,12 +1239,12 @@ bool Bundle3D::loadMeshDataJson_0_2(MeshDatas& meshdatas)
unsigned int indexnum = mesh_submesh_val[INDEXNUM].GetUint();
// indices
IndexArray indices;
std::vector<unsigned short> indices;
indices.resize(indexnum);
const rapidjson::Value& indices_val_array = mesh_submesh_val[INDICES];
for (rapidjson::SizeType j = 0; j < indices_val_array.Size(); ++j)
indices.at<uint16_t>(j) = (unsigned short)indices_val_array[j].GetUint();
indices[j] = (unsigned short)indices_val_array[j].GetUint();
meshdata->subMeshIndices.push_back(indices);
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
@ -2325,16 +2324,15 @@ Bundle3D::~Bundle3D()
cocos2d::AABB Bundle3D::calculateAABB(const std::vector<float>& vertex,
int stride,
const IndexArray& indices)
const std::vector<unsigned short>& index)
{
AABB aabb;
stride /= 4;
indices.for_each ([&](uint32_t i) {
Vec3 point(vertex[i * stride], vertex[i * stride + 1], vertex[i * stride + 2]);
aabb.updateMinMax(&point, 1);
});
for (const auto& it : index)
{
Vec3 point(vertex[it * stride], vertex[it * stride + 1], vertex[it * stride + 2]);
aabb.updateMinMax(&point, 1);
}
return aabb;
}

View File

@ -113,8 +113,7 @@ public:
const char* mtl_basepath = nullptr);
// calculate aabb
static AABB calculateAABB(const std::vector<float>& vertex,
int stride, const IndexArray& indices);
static AABB calculateAABB(const std::vector<float>& vertex, int stride, const std::vector<unsigned short>& index);
Bundle3D();
virtual ~Bundle3D();

View File

@ -39,123 +39,8 @@
#include "3d/CC3DProgramInfo.h"
#include "yasio/detail/byte_buffer.hpp"
NS_CC_BEGIN
class IndexArray
{
public:
IndexArray() : _format(backend::IndexFormat::U_SHORT) {}
IndexArray(backend::IndexFormat format) : _format(format) {}
IndexArray(std::initializer_list<unsigned short> rhs) : _format(backend::IndexFormat::U_SHORT), _buffer(rhs) {}
IndexArray(std::initializer_list<unsigned int> rhs, std::true_type /*U_INT*/)
: _format(backend::IndexFormat::U_INT), _buffer(rhs)
{}
IndexArray(const IndexArray& rhs) : _format(rhs._format), _buffer(rhs._buffer) {}
IndexArray(IndexArray&& rhs) : _format(rhs._format), _buffer(std::move(rhs._buffer)) {}
IndexArray& operator=(const IndexArray& rhs) {
_format = rhs._format;
_buffer = rhs._buffer;
return *this;
}
IndexArray& operator=(IndexArray&& rhs) {
this->swap(rhs);
return *this;
}
void swap(IndexArray& rhs) {
std::swap(_format, rhs._format);
_buffer.swap(rhs._buffer);
}
void clear(backend::IndexFormat format = backend::IndexFormat::UNSPEC)
{
_buffer.clear();
if (format != backend::IndexFormat::UNSPEC)
_format = format;
}
void push_back(u_short val)
{
assert(_format == backend::IndexFormat::U_SHORT);
_buffer.append_n((uint8_t*)&val, sizeof(val));
}
void push_back(unsigned int val, std::true_type /*U_INT*/)
{
assert(_format == backend::IndexFormat::U_INT);
_buffer.append_n((uint8_t*)&val, sizeof(val));
}
void insert(uint8_t* where, std::initializer_list<unsigned short> ilist)
{
assert(_format == backend::IndexFormat::U_SHORT);
_buffer.insert(where, (uint8_t*)ilist.begin(), (uint8_t*)ilist.end());
}
void insert(uint8_t* where, std::initializer_list<unsigned int> ilist, std::true_type)
{
assert(_format == backend::IndexFormat::U_INT);
_buffer.insert(where, (uint8_t*)ilist.begin(), (uint8_t*)ilist.end());
}
//template <typename _Iter>
//void insert(uint8_t* where, _Iter first, const _Iter last)
//{
// _buffer.insert(where, first, last);
//}
template<typename _Ty>
_Ty& at(size_t idx)
{
assert((sizeof(_Ty) == sizeof(uint16_t) && _format == backend::IndexFormat::U_SHORT) ||
(sizeof(_Ty) == sizeof(uint32_t) && _format == backend::IndexFormat::U_INT));
return (_Ty&)_buffer[idx * sizeof(_Ty)];
}
uint8_t* begin() noexcept { return _buffer.begin(); }
uint8_t* end() noexcept { return _buffer.end(); }
const uint8_t* begin() const noexcept { return _buffer.begin(); }
const uint8_t* end() const noexcept { return _buffer.end(); }
uint8_t* data() noexcept { return _buffer.data(); }
const uint8_t* data() const noexcept { return _buffer.data(); }
size_t size() const { return _buffer.size(); }
bool empty() const { return _buffer.empty(); }
void resize(size_t size) { _buffer.resize(size * sizeof(uint16_t)); }
void resize(size_t size, std::true_type) { _buffer.resize(size * sizeof(uint32_t)); }
backend::IndexFormat format() const { return _format; }
template<typename _Fty>
void for_each(_Fty cb) const
{
if (_format == backend::IndexFormat::U_SHORT)
{
for (auto it = (uint16_t*)_buffer.begin(); it != (uint16_t*)_buffer.end(); ++it)
{
cb(static_cast<uint32_t>(*it));
}
}
else if (_format == backend::IndexFormat::U_INT)
{
for (auto it = (uint32_t*)_buffer.begin(); it != (uint32_t*)_buffer.end(); ++it)
{
cb(*it);
}
}
}
protected:
backend::IndexFormat _format;
yasio::byte_buffer _buffer;
};
/**mesh vertex attribute
* @js NA
* @lua NA
@ -243,14 +128,13 @@ struct NodeDatas
}
};
/**mesh data
* @js NA
* @lua NA
*/
struct MeshData
{
using IndexArray = ::cocos2d::IndexArray;
typedef std::vector<unsigned short> IndexArray;
std::vector<float> vertex;
int vertexSizeInFloat;
std::vector<IndexArray> subMeshIndices;
@ -263,7 +147,7 @@ struct MeshData
public:
/**
* Get per vertex size
* @return return the sum size of all vertex attributes.
* @return return the sum of each vertex's all attribute size.
*/
int getPerVertexSize() const
{

View File

@ -113,7 +113,6 @@ Mesh::Mesh()
, _visible(true)
, _isTransparent(false)
, _force2DQueue(false)
, meshIndexFormat(CustomCommand::IndexFormat::U_SHORT)
, _meshIndexData(nullptr)
, _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
, _blendDirty(true)
@ -223,13 +222,10 @@ Mesh* Mesh::create(const std::vector<float>& vertices,
meshdata.vertex = vertices;
meshdata.subMeshIndices.push_back(indices);
meshdata.subMeshIds.push_back("");
auto meshvertexdata = MeshVertexData::create(meshdata, indices.format());
auto meshvertexdata = MeshVertexData::create(meshdata);
auto indexData = meshvertexdata->getMeshIndexDataByIndex(0);
auto mesh = create("", indexData);
mesh->setIndexFormat(indices.format());
return mesh;
return create("", indexData);
}
Mesh* Mesh::create(std::string_view name, MeshIndexData* indexData, MeshSkin* skin)
@ -732,18 +728,12 @@ CustomCommand::PrimitiveType Mesh::getPrimitiveType() const
ssize_t Mesh::getIndexCount() const
{
return _meshIndexData->getIndexBuffer()->getSize() /
(meshIndexFormat == CustomCommand::IndexFormat::U_SHORT ? sizeof(uint16_t) : sizeof(uint32_t));
return _meshIndexData->getIndexBuffer()->getSize() / sizeof(uint16_t);
}
CustomCommand::IndexFormat Mesh::getIndexFormat() const
{
return meshIndexFormat;
}
void Mesh::setIndexFormat(CustomCommand::IndexFormat indexFormat)
{
meshIndexFormat = indexFormat;
return CustomCommand::IndexFormat::U_SHORT;
}
backend::Buffer* Mesh::getIndexBuffer() const

View File

@ -64,7 +64,7 @@ class CC_DLL Mesh : public Ref
friend class Sprite3D;
public:
typedef std::vector<unsigned short> IndexArray;
/**create mesh from positions, normals, and so on, single SubMesh*/
static Mesh* create(const std::vector<float>& positions,
const std::vector<float>& normals,
@ -191,12 +191,6 @@ public:
* @lua NA
*/
CustomCommand::IndexFormat getIndexFormat() const;
/**
* set index format
*
* @lua NA
*/
void setIndexFormat(CustomCommand::IndexFormat indexFormat);
/**
* get index buffer
*
@ -259,7 +253,6 @@ protected:
bool _visible; // is the submesh visible
bool _isTransparent; // is this mesh transparent, it is a property of material in fact
bool _force2DQueue; // add this mesh to 2D render queue
CustomCommand::IndexFormat meshIndexFormat;
std::string _name;
MeshIndexData* _meshIndexData;

View File

@ -75,7 +75,7 @@ MeshIndexData::MeshIndexData()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) {
_indexBuffer->updateData((void*)_indexData.data(), _indexData.size());
_indexBuffer->updateData((void*)_indexData.data(), _indexData.size() * sizeof(_indexData[0]));
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
#endif
@ -108,7 +108,7 @@ void MeshVertexData::setVertexData(const std::vector<float>& vertexData)
#endif
}
MeshVertexData* MeshVertexData::create(const MeshData& meshdata, CustomCommand::IndexFormat format)
MeshVertexData* MeshVertexData::create(const MeshData& meshdata)
{
auto vertexdata = new MeshVertexData();
vertexdata->_vertexBuffer = backend::Device::getInstance()->newBuffer(
@ -132,29 +132,28 @@ MeshVertexData* MeshVertexData::create(const MeshData& meshdata, CustomCommand::
bool needCalcAABB = (meshdata.subMeshAABB.size() != meshdata.subMeshIndices.size());
for (size_t i = 0, size = meshdata.subMeshIndices.size(); i < size; ++i)
{
auto& indices = meshdata.subMeshIndices[i];
// auto indexSize = format == CustomCommand::IndexFormat::U_SHORT ? sizeof(uint16_t) : sizeof(uint32_t);
auto& index = meshdata.subMeshIndices[i];
auto indexBuffer = backend::Device::getInstance()->newBuffer(
indices.size()/* * indexSize*/, backend::BufferType::INDEX, backend::BufferUsage::STATIC);
index.size() * sizeof(index[0]), backend::BufferType::INDEX, backend::BufferUsage::STATIC);
indexBuffer->autorelease();
#if CC_ENABLE_CACHE_TEXTURE_DATA
indexBuffer->usingDefaultStoredData(false);
#endif
indexBuffer->updateData((void*)indices.data(), indices.size() /* * indexSize*/);
indexBuffer->updateData((void*)index.data(), index.size() * sizeof(index[0]));
std::string id = (i < meshdata.subMeshIds.size() ? meshdata.subMeshIds[i] : "");
MeshIndexData* indexdata = nullptr;
if (needCalcAABB)
{
auto aabb = Bundle3D::calculateAABB(meshdata.vertex, meshdata.getPerVertexSize(), indices);
auto aabb = Bundle3D::calculateAABB(meshdata.vertex, meshdata.getPerVertexSize(), index);
indexdata = MeshIndexData::create(id, vertexdata, indexBuffer, aabb);
}
else
indexdata = MeshIndexData::create(id, vertexdata, indexBuffer, meshdata.subMeshAABB[i]);
#if CC_ENABLE_CACHE_TEXTURE_DATA
indexdata->setIndexData(indices);
indexdata->setIndexData(index);
#endif
vertexdata->_indices.pushBack(indexdata);
vertexdata->_indexs.pushBack(indexdata);
}
vertexdata->autorelease();
@ -163,7 +162,7 @@ MeshVertexData* MeshVertexData::create(const MeshData& meshdata, CustomCommand::
MeshIndexData* MeshVertexData::getMeshIndexDataById(std::string_view id) const
{
for (auto it : _indices)
for (auto it : _indexs)
{
if (it->getId() == id)
return it;
@ -194,7 +193,7 @@ MeshVertexData::MeshVertexData()
MeshVertexData::~MeshVertexData()
{
CC_SAFE_RELEASE(_vertexBuffer);
_indices.clear();
_indexs.clear();
_vertexData.clear();
#if CC_ENABLE_CACHE_TEXTURE_DATA
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);

View File

@ -112,7 +112,7 @@ class CC_DLL MeshVertexData : public Ref
public:
/**create*/
static MeshVertexData* create(const MeshData& meshdata, CustomCommand::IndexFormat format = CustomCommand::IndexFormat::U_SHORT);
static MeshVertexData* create(const MeshData& meshdata);
/** get vertexbuffer */
backend::Buffer* getVertexBuffer() const { return _vertexBuffer; }
@ -124,9 +124,9 @@ public:
const MeshVertexAttrib& getMeshVertexAttrib(ssize_t index) const { return _attribs[index]; }
/** get index data count */
ssize_t getMeshIndexDataCount() const { return _indices.size(); }
ssize_t getMeshIndexDataCount() const { return _indexs.size(); }
/** get index data by index */
MeshIndexData* getMeshIndexDataByIndex(int index) const { return _indices.at(index); }
MeshIndexData* getMeshIndexDataByIndex(int index) const { return _indexs.at(index); }
/** get index data by id */
MeshIndexData* getMeshIndexDataById(std::string_view id) const;
@ -144,8 +144,8 @@ public:
protected:
backend::Buffer* _vertexBuffer = nullptr; // vertex buffer
ssize_t _sizePerVertex = -1;
Vector<MeshIndexData*> _indices; // index data
std::vector<MeshVertexAttrib> _attribs; // vertex attributes
Vector<MeshIndexData*> _indexs; // index data
std::vector<MeshVertexAttrib> _attribs; // vertex attributes
int _vertexCount = 0; // vertex count
std::vector<float> _vertexData;

View File

@ -51,9 +51,6 @@ Sprite3DMaterial* Sprite3DMaterial::_vertexLitMaterialSkin = nullptr;
Sprite3DMaterial* Sprite3DMaterial::_diffuseMaterialSkin = nullptr;
Sprite3DMaterial* Sprite3DMaterial::_bumpedDiffuseMaterialSkin = nullptr;
Sprite3DMaterial* Sprite3DMaterial::_quadTextureMaterial = nullptr;
Sprite3DMaterial* Sprite3DMaterial::_quadColorMaterial = nullptr;
backend::ProgramState* Sprite3DMaterial::_unLitMaterialProgState = nullptr;
backend::ProgramState* Sprite3DMaterial::_unLitNoTexMaterialProgState = nullptr;
backend::ProgramState* Sprite3DMaterial::_vertexLitMaterialProgState = nullptr;
@ -66,9 +63,6 @@ backend::ProgramState* Sprite3DMaterial::_vertexLitMaterialSkinProgState = n
backend::ProgramState* Sprite3DMaterial::_diffuseMaterialSkinProgState = nullptr;
backend::ProgramState* Sprite3DMaterial::_bumpedDiffuseMaterialSkinProgState = nullptr;
backend::ProgramState* Sprite3DMaterial::_quadTextureMaterialProgState = nullptr;
backend::ProgramState* Sprite3DMaterial::_quadColorMaterialProgState = nullptr;
void Sprite3DMaterial::createBuiltInMaterial()
{
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::SKINPOSITION_TEXTURE_3D);
@ -135,22 +129,6 @@ void Sprite3DMaterial::createBuiltInMaterial()
{
_bumpedDiffuseMaterialSkin->_type = Sprite3DMaterial::MaterialType::BUMPED_DIFFUSE;
}
program = backend::Program::getBuiltinProgram(backend::ProgramType::QUAD_TEXTURE_2D);
_quadTextureMaterialProgState = new backend::ProgramState(program);
_quadTextureMaterial = new Sprite3DMaterial();
if (_quadTextureMaterial && _quadTextureMaterial->initWithProgramState(_quadTextureMaterialProgState))
{
_quadTextureMaterial->_type = Sprite3DMaterial::MaterialType::QUAD_TEXTURE;
}
program = backend::Program::getBuiltinProgram(backend::ProgramType::QUAD_COLOR_2D);
_quadColorMaterialProgState = new backend::ProgramState(program);
_quadColorMaterial = new Sprite3DMaterial();
if (_quadColorMaterial && _quadColorMaterial->initWithProgramState(_quadColorMaterialProgState))
{
_quadColorMaterial->_type = Sprite3DMaterial::MaterialType::QUAD_COLOR;
}
}
void Sprite3DMaterial::releaseBuiltInMaterial()
@ -236,7 +214,7 @@ Sprite3DMaterial* Sprite3DMaterial::createBuiltInMaterial(MaterialType type, boo
break;
case Sprite3DMaterial::MaterialType::VERTEX_LIT:
CCASSERT(0, "not implemented");
CCASSERT(0, "not implement");
break;
case Sprite3DMaterial::MaterialType::DIFFUSE:
@ -251,14 +229,6 @@ Sprite3DMaterial* Sprite3DMaterial::createBuiltInMaterial(MaterialType type, boo
material = skinned ? _bumpedDiffuseMaterialSkin : _bumpedDiffuseMaterial;
break;
case Sprite3DMaterial::MaterialType::QUAD_TEXTURE:
material = _quadTextureMaterial;
break;
case Sprite3DMaterial::MaterialType::QUAD_COLOR:
material = _quadColorMaterial;
break;
default:
break;
}

View File

@ -63,8 +63,6 @@ public:
DIFFUSE, // diffuse (pixel lighting)
DIFFUSE_NOTEX, // diffuse (without texture)
BUMPED_DIFFUSE, // bumped diffuse
QUAD_TEXTURE, // textured quad material
QUAD_COLOR, // colored quad material (without texture)
// Custom material
CUSTOM, // Create from material file
@ -136,9 +134,6 @@ protected:
static Sprite3DMaterial* _diffuseMaterialSkin;
static Sprite3DMaterial* _bumpedDiffuseMaterialSkin;
static Sprite3DMaterial* _quadTextureMaterial;
static Sprite3DMaterial* _quadColorMaterial;
static backend::ProgramState* _unLitMaterialProgState;
static backend::ProgramState* _unLitNoTexMaterialProgState;
static backend::ProgramState* _vertexLitMaterialProgState;
@ -150,9 +145,6 @@ protected:
static backend::ProgramState* _vertexLitMaterialSkinProgState;
static backend::ProgramState* _diffuseMaterialSkinProgState;
static backend::ProgramState* _bumpedDiffuseMaterialSkinProgState;
static backend::ProgramState* _quadTextureMaterialProgState;
static backend::ProgramState* _quadColorMaterialProgState;
};
/**

View File

@ -1303,11 +1303,92 @@ void Director::createStatsLabel()
_drawnVerticesLabel->setIgnoreContentScaleFactor(true);
_drawnVerticesLabel->setScale(scaleFactor);
auto safeOrigin = getSafeAreaRect().origin;
const int height_spacing = (int)(22 / CC_CONTENT_SCALE_FACTOR());
_drawnVerticesLabel->setPosition(Vec2(0, height_spacing * 2.0f) + safeOrigin);
_drawnBatchesLabel->setPosition(Vec2(0, height_spacing * 1.0f) + safeOrigin);
_FPSLabel->setPosition(Vec2(0, height_spacing * 0.0f) + safeOrigin);
setStatsAnchor();
}
void Director::setStatsAnchor(AnchorPreset anchor)
{
if (!_displayStats)
return;
// Initialize stat counters
if (!_FPSLabel)
showStats();
{
static Vec2 _fpsPosition = {0, 0};
auto safeOrigin = getSafeAreaRect().origin;
auto safeSize = getSafeAreaRect().size;
const int height_spacing = (int)(22 / CC_CONTENT_SCALE_FACTOR());
switch (anchor)
{
case AnchorPreset::BOTTOM_LEFT:
_fpsPosition = Vec2(0, 0);
_drawnVerticesLabel->setAnchorPoint({0, 0});
_drawnBatchesLabel->setAnchorPoint({0, 0});
_FPSLabel->setAnchorPoint({0, 0});
break;
case AnchorPreset::CENTER_LEFT:
_fpsPosition = Vec2(0, safeSize.height / 2 - height_spacing * 1.5);
_drawnVerticesLabel->setAnchorPoint({0, 0.0});
_drawnBatchesLabel->setAnchorPoint({0, 0.0});
_FPSLabel->setAnchorPoint({0, 0});
break;
case AnchorPreset::TOP_LEFT:
_fpsPosition = Vec2(0, safeSize.height - height_spacing * 3);
_drawnVerticesLabel->setAnchorPoint({0, 0});
_drawnBatchesLabel->setAnchorPoint({0, 0});
_FPSLabel->setAnchorPoint({0, 0});
break;
case AnchorPreset::BOTTOM_RIGHT:
_fpsPosition = Vec2(safeSize.width, 0);
_drawnVerticesLabel->setAnchorPoint({1, 0});
_drawnBatchesLabel->setAnchorPoint({1, 0});
_FPSLabel->setAnchorPoint({1, 0});
break;
case AnchorPreset::CENTER_RIGHT:
_fpsPosition = Vec2(safeSize.width, safeSize.height / 2 - height_spacing * 1.5);
_drawnVerticesLabel->setAnchorPoint({1, 0.0});
_drawnBatchesLabel->setAnchorPoint({1, 0.0});
_FPSLabel->setAnchorPoint({1, 0.0});
break;
case AnchorPreset::TOP_RIGHT:
_fpsPosition = Vec2(safeSize.width, safeSize.height - height_spacing * 3);
_drawnVerticesLabel->setAnchorPoint({1, 0});
_drawnBatchesLabel->setAnchorPoint({1, 0});
_FPSLabel->setAnchorPoint({1, 0});
break;
case AnchorPreset::BOTTOM_CENTER:
_fpsPosition = Vec2(safeSize.width / 2, 0);
_drawnVerticesLabel->setAnchorPoint({0.5, 0});
_drawnBatchesLabel->setAnchorPoint({0.5, 0});
_FPSLabel->setAnchorPoint({0.5, 0});
break;
case AnchorPreset::CENTER:
_fpsPosition = Vec2(safeSize.width / 2, safeSize.height / 2 - height_spacing * 1.5);
_drawnVerticesLabel->setAnchorPoint({0.5, 0.0});
_drawnBatchesLabel->setAnchorPoint({0.5, 0.0});
_FPSLabel->setAnchorPoint({0.5, 0.0});
break;
case AnchorPreset::TOP_CENTER:
_fpsPosition = Vec2(safeSize.width / 2, safeSize.height - height_spacing * 3);
_drawnVerticesLabel->setAnchorPoint({0.5, 0});
_drawnBatchesLabel->setAnchorPoint({0.5, 0});
_FPSLabel->setAnchorPoint({0.5, 0});
break;
default: // FPSPosition::BOTTOM_LEFT
_fpsPosition = Vec2(0, 0);
_drawnVerticesLabel->setAnchorPoint({0, 0});
_drawnBatchesLabel->setAnchorPoint({0, 0});
_FPSLabel->setAnchorPoint({0, 0});
break;
}
_drawnVerticesLabel->setPosition(Vec2(0, height_spacing * 2.0f) + _fpsPosition + safeOrigin);
_drawnBatchesLabel->setPosition(Vec2(0, height_spacing * 1.0f) + _fpsPosition + safeOrigin);
_FPSLabel->setPosition(Vec2(0, height_spacing * 0.0f) + _fpsPosition + safeOrigin);
}
}
#endif // #if !CC_STRIP_FPS

View File

@ -62,21 +62,6 @@ class Camera;
class Console;
/**
* @brief Matrix stack type.
*/
enum class MATRIX_STACK_TYPE
{
/// Model view matrix stack
MATRIX_STACK_MODELVIEW,
/// projection matrix stack
MATRIX_STACK_PROJECTION,
/// texture matrix stack
MATRIX_STACK_TEXTURE
};
/**
@brief Class that creates and handles the main Window and manages how
and when to execute the Scenes.
@ -158,14 +143,19 @@ public:
/** Sets the FPS value. FPS = 1/interval. */
void setAnimationInterval(float interval);
/** Whether or not displaying the FPS on the bottom-left corner of the screen is enabled or not. */
bool isDisplayStats() { return _displayStats; }
/** Whether or not the FPS on the bottom-left corner of the screen is shown. */
bool isStatsDisplay() { return _displayStats; }
/** Display the FPS on the bottom-left corner of the screen. */
void setDisplayStats(bool displayStats) { _displayStats = displayStats; }
void setStatsDisplay(bool displayStats) { _displayStats = displayStats; }
/** Gets the seconds per frame. */
float getSecondsPerFrame() { return _secondsPerFrame; }
/** Sets the stats corner displayed on screen if display stats is enabled. */
void setStatsAnchor(AnchorPreset anchor = (AnchorPreset)0);
/** Sets the FPS value. */
/**
* Get the GLView.
* @lua NA

View File

@ -38,6 +38,7 @@ set(COCOS_BASE_HEADER
base/ccConstants.h
base/CCEvent.h
base/ccTypes.h
base/ccEnums.h
base/CCAsyncTaskPool.h
base/ccRandom.h
base/CCRef.h

140
core/base/ccEnums.h Normal file
View File

@ -0,0 +1,140 @@
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2020 C4games Ltd.
https://adxeproject.github.io/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#pragma once
NS_CC_BEGIN
/**
* @brief Effects used by `Label`
*/
enum class LabelEffect
{
// FIXME: Covert them to bitwise. More than one effect should be supported
NORMAL,
OUTLINE,
SHADOW,
GLOW,
ITALICS,
BOLD,
UNDERLINE,
STRIKETHROUGH,
ALL
};
/**
* @brief Interval change reason.
*/
enum class SetIntervalReason : char
{
BY_GAME = 0,
BY_ENGINE,
BY_SYSTEM,
BY_SCENE_CHANGE,
BY_DIRECTOR_PAUSE
};
/**
* @brief Texture flags.
*/
struct TextureFlag
{
enum
{
NONE = 0,
ANTIALIAS_ENABLED = 1 << 1,
PREMULTIPLIEDALPHA = 1 << 2,
RENDERTARGET = 1 << 3,
};
};
/**
* @brief Texture sampler flags.
*/
struct TextureSamplerFlag
{
enum
{
DEFAULT = 0,
DUAL_SAMPLER = 1 << 1,
};
};
/**
* @brief Matrix stack type.
*/
enum class MATRIX_STACK_TYPE
{
/// Model view matrix stack
MATRIX_STACK_MODELVIEW,
/// projection matrix stack
MATRIX_STACK_PROJECTION,
/// texture matrix stack
MATRIX_STACK_TEXTURE
};
/**
* @brief Anchor presets used to position nodes in corners
*/
enum class AnchorPreset
{
BOTTOM_LEFT = 0,
BOTTOM_CENTER = 1,
BOTTOM_RIGHT = 2,
CENTER_LEFT = 3,
CENTER = 4,
CENTER_RIGHT = 5,
TOP_LEFT = 6,
TOP_CENTER = 7,
TOP_RIGHT = 8
};
/**
* Particle emission shapes.
* Supported shapes are Point, Rectangle, RectangularTorus, Circle, Torus, Cone, Cone Torus, Texture alpha emission mask
* @since adxe-1.0.0b8
*/
enum class EmissionShapeType
{
// Emission shape of type point
POINT,
// Emission shape of type rectangle
RECT,
// Emission shape of type rectangular torus
RECTTORUS,
// Emission shape of type circle or cone
CIRCLE,
// Emission shape of type torus or cone torus
TORUS,
// Emission shape of type texture alpha mask
TEXTURE_ALPHA_MASK
};
NS_CC_END

View File

@ -33,6 +33,7 @@ THE SOFTWARE.
#include "math/CCMath.h"
#include "base/CCRef.h"
#include "renderer/backend/Types.h"
#include "ccEnums.h"
/**
* @addtogroup base
@ -622,24 +623,6 @@ struct CC_DLL FontDefinition
int _overflow = 0;
};
/**
* @brief Effects used by `Label`
*
*/
enum class LabelEffect
{
// FIXME: Covert them to bitwise. More than one effect should be supported
NORMAL,
OUTLINE,
SHADOW,
GLOW,
ITALICS,
BOLD,
UNDERLINE,
STRIKETHROUGH,
ALL
};
/** @struct Acceleration
* The device accelerometer reports values for each axis in units of g-force.
*/
@ -656,15 +639,6 @@ public:
extern const std::string CC_DLL STD_STRING_EMPTY;
extern const ssize_t CC_DLL CC_INVALID_INDEX;
enum class SetIntervalReason : char
{
BY_GAME = 0,
BY_ENGINE,
BY_SYSTEM,
BY_SCENE_CHANGE,
BY_DIRECTOR_PAUSE
};
struct CC_DLL Viewport
{
int x = 0;
@ -681,26 +655,6 @@ struct CC_DLL ScissorRect
float height = 0;
};
struct TextureFlag
{
enum
{
NONE = 0,
ANTIALIAS_ENABLED = 1 << 1,
PREMULTIPLIEDALPHA = 1 << 2,
RENDERTARGET = 1 << 3,
};
};
struct TextureSamplerFlag
{
enum
{
DEFAULT = 0,
DUAL_SAMPLER = 1 << 1,
};
};
using TextureUsage = backend::TextureUsage;
using PixelFormat = backend::PixelFormat;

View File

@ -12,6 +12,7 @@ set(COCOS_MATH_HEADER
math/MathUtil.h
math/CCMath.h
math/Rect.h
math/RngSeed.hpp
)
set(COCOS_MATH_SRC

View File

@ -1,7 +1,10 @@
#pragma warning(suppress : 4244)
#ifndef RNGSEED_H_
#define RNGSEED_H_
/** A more effective seeded random number generator struct, made by kiss rng.
* The numbers that are generated are inclusive of rhs value
* @since adxe-1.0.0b8
*/
struct RngSeed

View File

@ -53,14 +53,13 @@ public:
using PrimitiveType = backend::PrimitiveType;
/**
Buffer usage of vertex/index buffer. If the contents are not updated every frame,
then STATIC should be used. Otherwise, DYNAMIC should be used.
This flag is not improtant because most GPU drivers ignore it, so it's best left to STATIC.
Buffer usage of vertex/index buffer. If the contents is not updated every frame,
then use STATIC, other use DYNAMIC.
*/
using BufferUsage = backend::BufferUsage;
/**
The index format that determines the size of index data. U_SHORT (65535 vertices) is enough for most
cases, But support for U_INT (4294967295 vertices) has been added.
The index format determine the size for index data. U_SHORT is enough for most
cases.
*/
using IndexFormat = backend::IndexFormat;
@ -209,8 +208,6 @@ TODO: should remove it.
inline IndexFormat getIndexFormat() const { return _indexFormat; }
inline void setIndexFormat(IndexFormat format) { _indexFormat = format; }
/**
* set a callback which will be invoke before rendering
*/

View File

@ -123,8 +123,6 @@ bool ProgramCache::init()
registerProgramFactory(ProgramType::TERRAIN_3D, CC3D_terrain_vert, CC3D_terrain_frag);
registerProgramFactory(ProgramType::PARTICLE_TEXTURE_3D, CC3D_particle_vert, CC3D_particleTexture_frag);
registerProgramFactory(ProgramType::PARTICLE_COLOR_3D, CC3D_particle_vert, CC3D_particleColor_frag);
registerProgramFactory(ProgramType::QUAD_COLOR_2D, CC2D_quad_vert, CC2D_quadColor_frag);
registerProgramFactory(ProgramType::QUAD_TEXTURE_2D, CC2D_quad_vert, CC2D_quadTexture_frag);
registerProgramFactory(ProgramType::HSV, positionTextureColor_vert, hsv_frag);
registerProgramFactory(ProgramType::HSV_DUAL_SAMPLER, positionTextureColor_vert, dualSampler_hsv_frag);

View File

@ -160,8 +160,7 @@ enum class TextureUsage : uint32_t
enum class IndexFormat : uint32_t
{
U_SHORT,
U_INT,
UNSPEC = 0xff,
U_INT
};
enum class VertexStepMode : uint32_t
@ -432,9 +431,6 @@ struct ProgramType
PARTICLE_TEXTURE_3D, // CC3D_particle_vert, CC3D_particleTexture_frag
PARTICLE_COLOR_3D, // CC3D_particle_vert, CC3D_particleColor_frag
QUAD_COLOR_2D, // CC2D_quad_vert, CC2D_quadColor_frag
QUAD_TEXTURE_2D, // CC2D_quad_vert, CC2D_quadTexture_frag
HSV,
HSV_DUAL_SAMPLER,
HSV_ETC1 = HSV_DUAL_SAMPLER,

View File

@ -73,7 +73,4 @@ NS_CC_BEGIN
#include "renderer/shaders/hsv.frag"
#include "renderer/shaders/dualSampler_hsv.frag"
#include "renderer/shaders/quad.vert"
#include "renderer/shaders/quad.frag"
NS_CC_END

View File

@ -78,10 +78,6 @@ extern CC_DLL const char* CC3D_skybox_vert;
extern CC_DLL const char* CC3D_terrain_frag;
extern CC_DLL const char* CC3D_terrain_vert;
extern CC_DLL const char* CC2D_quadTexture_frag;
extern CC_DLL const char* CC2D_quadColor_frag;
extern CC_DLL const char* CC2D_quad_vert;
extern CC_DLL const char* hsv_frag;
extern CC_DLL const char* dualSampler_hsv_frag;
NS_CC_END

View File

@ -1,58 +0,0 @@
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
https://adxeproject.github.io/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
const char* CC2D_quadTexture_frag = R"(
#ifdef GL_ES
varying mediump vec2 TextureCoordOut;
varying mediump vec4 ColorOut;
#else
varying vec4 ColorOut;
varying vec2 TextureCoordOut;
#endif
uniform vec4 u_color;
uniform sampler2D u_texture;
void main(void)
{
gl_FragColor = texture2D(u_texture, TextureCoordOut) * ColorOut * u_color;
}
)";
const char* CC2D_quadColor_frag = R"(
#ifdef GL_ES
varying mediump vec4 ColorOut;
#else
varying vec4 ColorOut;
#endif
uniform vec4 u_color;
void main(void)
{
gl_FragColor = ColorOut * u_color;
}
)";

View File

@ -1,44 +0,0 @@
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
https://adxeproject.github.io/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
const char* CC2D_quad_vert = R"(
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;
varying vec2 TextureCoordOut;
varying vec4 ColorOut;
uniform mat4 u_PMatrix;
void main()
{
ColorOut = a_color;
TextureCoordOut = a_texCoord;
TextureCoordOut.y = 1.0 - TextureCoordOut.y;
gl_Position = u_PMatrix * a_position;
}
)";

View File

@ -188,22 +188,49 @@ void SimpleSnake::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{
switch (keyCode)
{
case cocos2d::EventKeyboard::KeyCode::KEY_LEFT_ARROW:
case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
if (dir != 2)
dir = 1;
break;
case cocos2d::EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
if (dir != 1)
dir = 2;
break;
case cocos2d::EventKeyboard::KeyCode::KEY_UP_ARROW:
case EventKeyboard::KeyCode::KEY_UP_ARROW:
if (dir != 3)
dir = 0;
break;
case cocos2d::EventKeyboard::KeyCode::KEY_DOWN_ARROW:
case EventKeyboard::KeyCode::KEY_DOWN_ARROW:
if (dir != 0)
dir = 3;
break;
case EventKeyboard::KeyCode::KEY_1:
Director::getInstance()->setFPSPos(FPSPosition::BOTTOM_LEFT);
break;
case EventKeyboard::KeyCode::KEY_4:
Director::getInstance()->setFPSPos(FPSPosition::CENTER_LEFT);
break;
case EventKeyboard::KeyCode::KEY_7:
Director::getInstance()->setFPSPos(FPSPosition::TOP_LEFT);
break;
case EventKeyboard::KeyCode::KEY_8:
Director::getInstance()->setFPSPos(FPSPosition::TOP_CENTER);
break;
case EventKeyboard::KeyCode::KEY_9:
Director::getInstance()->setFPSPos(FPSPosition::TOP_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_6:
Director::getInstance()->setFPSPos(FPSPosition::CENTER_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_3:
Director::getInstance()->setFPSPos(FPSPosition::BOTTOM_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_2:
Director::getInstance()->setFPSPos(FPSPosition::BOTTOM_CENTER);
break;
case EventKeyboard::KeyCode::KEY_5:
Director::getInstance()->setFPSPos(FPSPosition::CENTER);
break;
default:
break;
}

View File

@ -16851,6 +16851,67 @@ int lua_cocos2dx_Director_drawScene(lua_State* tolua_S)
return 0;
}
int lua_cocos2dx_Director_setStatsAnchor(lua_State* tolua_S)
{
int argc = 0;
cocos2d::Director* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(tolua_S,1,"cc.Director",0,&tolua_err)) goto tolua_lerror;
#endif
cobj = (cocos2d::Director*)tolua_tousertype(tolua_S,1,0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_Director_setStatsAnchor'", nullptr);
return 0;
}
#endif
argc = lua_gettop(tolua_S)-1;
if (argc == 0)
{
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_Director_setStatsAnchor'", nullptr);
return 0;
}
cobj->setStatsAnchor();
lua_settop(tolua_S, 1);
return 1;
}
if (argc == 1)
{
cocos2d::AnchorPreset arg0;
ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0, "cc.Director:setStatsAnchor");
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_Director_setStatsAnchor'", nullptr);
return 0;
}
cobj->setStatsAnchor(arg0);
lua_settop(tolua_S, 1);
return 1;
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.Director:setStatsAnchor",argc, 0);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_Director_setStatsAnchor'.",&tolua_err);
#endif
return 0;
}
int lua_cocos2dx_Director_restart(lua_State* tolua_S)
{
int argc = 0;
@ -17914,6 +17975,7 @@ int lua_register_cocos2dx_Director(lua_State* tolua_S)
tolua_function(tolua_S,"setNotificationNode",lua_cocos2dx_Director_setNotificationNode);
tolua_function(tolua_S,"setChildrenIndexerEnabled",lua_cocos2dx_Director_setChildrenIndexerEnabled);
tolua_function(tolua_S,"drawScene",lua_cocos2dx_Director_drawScene);
tolua_function(tolua_S,"setStatsAnchor",lua_cocos2dx_Director_setStatsAnchor);
tolua_function(tolua_S,"restart",lua_cocos2dx_Director_restart);
tolua_function(tolua_S,"popScene",lua_cocos2dx_Director_popScene);
tolua_function(tolua_S,"loadIdentityMatrix",lua_cocos2dx_Director_loadIdentityMatrix);

View File

@ -2406,6 +2406,7 @@ int register_all_cocos2dx(lua_State* tolua_S);
#endif // __cocos2dx_h__

View File

@ -35,6 +35,92 @@
USING_NS_CC;
namespace {
class TextButton : public Label {
public:
static TextButton *
create(std::string_view text, const std::function<void(TextButton *)> &onTriggered) {
auto ret = new TextButton();
TTFConfig ttfconfig("fonts/arial.ttf", 25);
if (ret->setTTFConfig(ttfconfig)) {
ret->setString(text);
ret->_onTriggered = onTriggered;
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
void setEnabled(bool enabled) {
_enabled = enabled;
if (_enabled) {
this->setColor(Color3B::WHITE);
} else {
this->setColor(Color3B::GRAY);
}
}
private:
TextButton() : _onTriggered(nullptr), _enabled(true) {
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(TextButton::onTouchBegan, this);
listener->onTouchEnded = CC_CALLBACK_2(TextButton::onTouchEnded, this);
listener->onTouchCancelled = CC_CALLBACK_2(TextButton::onTouchCancelled, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
bool touchHits(Touch *touch) {
auto hitPos = this->convertToNodeSpace(touch->getLocation());
if (hitPos.x >= 0 && hitPos.y >= 0 && hitPos.x <= _contentSize.width &&
hitPos.y <= _contentSize.height) {
return true;
}
return false;
}
bool onTouchBegan(Touch *touch, Event *event) {
auto hits = touchHits(touch);
if (hits) {
scaleButtonTo(0.95f);
}
return hits;
}
void onTouchEnded(Touch *touch, Event *event) {
if (_enabled) {
auto hits = touchHits(touch);
if (hits && _onTriggered) {
_onTriggered(this);
}
}
scaleButtonTo(1);
}
void onTouchCancelled(Touch *touch, Event *event) { scaleButtonTo(1); }
void scaleButtonTo(float scale) {
auto action = ScaleTo::create(0.05f, scale);
action->setTag(10000);
stopActionByTag(10000);
runAction(action);
}
std::function<void(TextButton *)> _onTriggered;
bool _enabled;
};
}
EventDispatcherTests::EventDispatcherTests()
{
ADD_TEST_CASE(TouchableSpriteTest);
@ -458,6 +544,7 @@ void LabelKeyboardEventTest::onEnter()
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
#ifdef CC_PLATFORM_PC
auto statusLabel = Label::createWithSystemFont("No keyboard event received!", "", 20);
statusLabel->setPosition(origin + Vec2(size.width / 2, size.height / 2));
addChild(statusLabel);
@ -468,6 +555,39 @@ void LabelKeyboardEventTest::onEnter()
sprintf(buf, "Key %d was pressed!", (int)keyCode);
auto label = static_cast<Label*>(event->getCurrentTarget());
label->setString(buf);
switch (keyCode)
{
case EventKeyboard::KeyCode::KEY_1:
Director::getInstance()->setStatsAnchor(AnchorPreset::BOTTOM_LEFT);
break;
case EventKeyboard::KeyCode::KEY_4:
Director::getInstance()->setStatsAnchor(AnchorPreset::CENTER_LEFT);
break;
case EventKeyboard::KeyCode::KEY_7:
Director::getInstance()->setStatsAnchor(AnchorPreset::TOP_LEFT);
break;
case EventKeyboard::KeyCode::KEY_8:
Director::getInstance()->setStatsAnchor(AnchorPreset::TOP_CENTER);
break;
case EventKeyboard::KeyCode::KEY_9:
Director::getInstance()->setStatsAnchor(AnchorPreset::TOP_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_6:
Director::getInstance()->setStatsAnchor(AnchorPreset::CENTER_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_3:
Director::getInstance()->setStatsAnchor(AnchorPreset::BOTTOM_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_2:
Director::getInstance()->setStatsAnchor(AnchorPreset::BOTTOM_CENTER);
break;
case EventKeyboard::KeyCode::KEY_5:
Director::getInstance()->setStatsAnchor(AnchorPreset::CENTER);
break;
default:
break;
}
};
listener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event) {
@ -478,6 +598,31 @@ void LabelKeyboardEventTest::onEnter()
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, statusLabel);
#else
auto& layerSize = this->getContentSize();
static AnchorPreset anchor = AnchorPreset::BOTTOM_LEFT;
anchor = AnchorPreset::BOTTOM_LEFT;
auto playPrev = TextButton::create("Show Fps Prev Pos", [=](TextButton* button) {
if (anchor > AnchorPreset::BOTTOM_LEFT)
{
anchor = static_cast<AnchorPreset>((int)anchor - 1);
Director::getInstance()->setStatsAnchor(anchor);
}
});
playPrev->setPosition(layerSize.width * 0.35f, layerSize.height * 0.5f);
addChild(playPrev);
auto playNext = TextButton::create("Show Fps Next Pos", [=](TextButton* button) {
if (anchor < AnchorPreset::TOP_RIGHT)
{
anchor = static_cast<AnchorPreset>((int)anchor + 1);
Director::getInstance()->setStatsAnchor(anchor);
}
});
playNext->setPosition(layerSize.width * 0.65f, layerSize.height * 0.5f);
addChild(playNext);
Director::getInstance()->setStatsAnchor(AnchorPreset::BOTTOM_LEFT);
#endif
}
std::string LabelKeyboardEventTest::title() const
@ -487,7 +632,11 @@ std::string LabelKeyboardEventTest::title() const
std::string LabelKeyboardEventTest::subtitle() const
{
return "Please click keyboard\n(Only available on Desktop, Android\nand Windows Universal Apps)";
#ifdef CC_PLATFORM_PC
return "Press keys 1 through 9 to change the stats anchor on the screen.";
#else
return "Change the stats anchor [1-9]";
#endif
}
// SpriteAccelerationEventTest