Skip to content

Commit

Permalink
Improve code quality (stage1.Oct) (axmolengine#916)
Browse files Browse the repository at this point in the history
* Improve code quality

* Add env PULL_REQUEST [skip ci]

* Update build.ps1

* Fix ci [skip ci]

* Update ZipUtils.cpp

* Update build.ps1

* Resolve reviews

* Improve

* Improve code style

* Resolve reviews

* Use pod_vector

* Improve resize growth

* Update CCAnimation3D.cpp

* Update axstd.h [skip ci]
  • Loading branch information
halx99 authored Oct 13, 2022
1 parent ba95ca9 commit 25d93bd
Show file tree
Hide file tree
Showing 25 changed files with 449 additions and 136 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release
PYENV_VERSION: 2.7.18
PULL_REQUEST: yes

jobs:
build-windows:
Expand Down
6 changes: 2 additions & 4 deletions core/2d/CCMotionStreak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,8 @@ bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Co
_colorPointer = (uint8_t*)malloc(sizeof(uint8_t) * 4 * _vertexCount);
_customCommand.createVertexBuffer(VERTEX_SIZE, _vertexCount, CustomCommand::BufferUsage::DYNAMIC);

std::vector<uint8_t> zeros;
zeros.resize(VERTEX_SIZE * _vertexCount);
std::fill(zeros.begin(), zeros.end(), 0);
_customCommand.updateVertexBuffer(zeros.data(), zeros.size());
auto zeros = std::make_unique<uint8_t[]>(VERTEX_SIZE * _vertexCount);
_customCommand.updateVertexBuffer(zeros.get(), VERTEX_SIZE * _vertexCount);

setTexture(texture);
setColor(color);
Expand Down
8 changes: 4 additions & 4 deletions core/2d/CCSpriteFrameCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void SpriteFrameCache::removeSpriteFrames()
void SpriteFrameCache::removeUnusedSpriteFrames()
{
auto removed = false;
std::vector<std::string> toRemoveFrames;
std::vector<std::string_view> toRemoveFrames;

const auto& frames = getSpriteFrames();
for (auto&& iter : frames)
Expand Down Expand Up @@ -211,7 +211,7 @@ void SpriteFrameCache::removeSpriteFramesFromDictionary(ValueMap& dictionary)
return;

const auto& framesDict = dictionary["frames"].asValueMap();
std::vector<std::string> keysToRemove;
std::vector<std::string_view> keysToRemove;

for (const auto& iter : framesDict)
{
Expand All @@ -226,7 +226,7 @@ void SpriteFrameCache::removeSpriteFramesFromDictionary(ValueMap& dictionary)

void SpriteFrameCache::removeSpriteFramesFromTexture(Texture2D* texture)
{
std::vector<std::string> keysToRemove;
std::vector<std::string_view> keysToRemove;

for (auto&& iter : getSpriteFrames())
{
Expand Down Expand Up @@ -321,7 +321,7 @@ bool SpriteFrameCache::eraseFrame(std::string_view frameName)
return false;
}

bool SpriteFrameCache::eraseFrames(const std::vector<std::string>& frames)
bool SpriteFrameCache::eraseFrames(const std::vector<std::string_view>& frames)
{
auto ret = false;
for (const auto& frame : frames)
Expand Down
2 changes: 1 addition & 1 deletion core/2d/CCSpriteFrameCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class AX_DLL SpriteFrameCache : public Ref

/** Delete a list of frames from cache, rebuild index
*/
bool eraseFrames(const std::vector<std::string>& frame);
bool eraseFrames(const std::vector<std::string_view>& frame);
/** Delete frame from index and SpriteFrame is kept.
*/
bool removeSpriteSheet(std::string_view spriteSheetFileName);
Expand Down
126 changes: 64 additions & 62 deletions core/3d/CCAnimation3D.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2022 Bytedance Inc.
https://axmolengine.github.io/
Expand All @@ -26,6 +27,7 @@
#include "3d/CCAnimation3D.h"
#include "3d/CCBundle3D.h"
#include "platform/CCFileUtils.h"
#include "base/axstd.h"

NS_AX_BEGIN

Expand Down Expand Up @@ -102,83 +104,83 @@ bool Animation3D::init(const Animation3DData& data)
{
_duration = data._totalTime;

for (const auto& iter : data._translationKeys)
{
Curve* curve = _boneCurves[iter.first];
if (curve == nullptr)
axstd::pod_vector<float> keys;
axstd::pod_vector<Vec3> values;
for (const auto& iter : data._translationKeys)
{
curve = new Curve();
_boneCurves[iter.first] = curve;
}
Curve* curve = _boneCurves[iter.first];
if (curve == nullptr)
{
curve = new Curve();
_boneCurves[iter.first] = curve;
}

if (iter.second.empty())
continue;
std::vector<float> keys;
std::vector<float> values;
for (const auto& keyIter : iter.second)
{
keys.emplace_back(keyIter._time);
values.emplace_back(keyIter._key.x);
values.emplace_back(keyIter._key.y);
values.emplace_back(keyIter._key.z);
}
if (iter.second.empty())
continue;

curve->translateCurve = Curve::AnimationCurveVec3::create(&keys[0], &values[0], (int)keys.size());
if (curve->translateCurve)
curve->translateCurve->retain();
}

for (const auto& iter : data._rotationKeys)
{
Curve* curve = _boneCurves[iter.first];
if (curve == nullptr)
{
curve = new Curve();
_boneCurves[iter.first] = curve;
}
axstd::resize_and_transform(iter.second.begin(), iter.second.end(), keys,
[](const auto& keyIter) { return keyIter._time; });
axstd::resize_and_transform(iter.second.begin(), iter.second.end(), values,
[](const auto& keyIter) { return keyIter._key; });

if (iter.second.empty())
continue;
std::vector<float> keys;
std::vector<float> values;
for (const auto& keyIter : iter.second)
{
keys.emplace_back(keyIter._time);
values.emplace_back(keyIter._key.x);
values.emplace_back(keyIter._key.y);
values.emplace_back(keyIter._key.z);
values.emplace_back(keyIter._key.w);
curve->translateCurve = Curve::AnimationCurveVec3::create(&keys[0], &values[0].x, (int)keys.size());
if (curve->translateCurve)
curve->translateCurve->retain();
}

curve->rotCurve = Curve::AnimationCurveQuat::create(&keys[0], &values[0], (int)keys.size());
if (curve->rotCurve)
curve->rotCurve->retain();
}

for (const auto& iter : data._scaleKeys)
{
Curve* curve = _boneCurves[iter.first];
if (curve == nullptr)
axstd::pod_vector<float> keys;
axstd::pod_vector<Quaternion> values;
for (const auto& iter : data._rotationKeys)
{
curve = new Curve();
_boneCurves[iter.first] = curve;
Curve* curve = _boneCurves[iter.first];
if (curve == nullptr)
{
curve = new Curve();
_boneCurves[iter.first] = curve;
}

if (iter.second.empty())
continue;

axstd::resize_and_transform(iter.second.begin(), iter.second.end(), keys,
[](const auto& keyIter) { return keyIter._time; });
axstd::resize_and_transform(iter.second.begin(), iter.second.end(), values,
[](const auto& keyIter) { return keyIter._key; });

curve->rotCurve = Curve::AnimationCurveQuat::create(&keys[0], &values[0].x, (int)keys.size());
if (curve->rotCurve)
curve->rotCurve->retain();
}
}

if (iter.second.empty())
continue;
std::vector<float> keys;
std::vector<float> values;
for (const auto& keyIter : iter.second)
{
axstd::pod_vector<float> keys;
axstd::pod_vector<Vec3> values;
for (const auto& iter : data._scaleKeys)
{
keys.emplace_back(keyIter._time);
values.emplace_back(keyIter._key.x);
values.emplace_back(keyIter._key.y);
values.emplace_back(keyIter._key.z);
Curve* curve = _boneCurves[iter.first];
if (curve == nullptr)
{
curve = new Curve();
_boneCurves[iter.first] = curve;
}

if (iter.second.empty())
continue;

axstd::resize_and_transform(iter.second.begin(), iter.second.end(), keys,
[](const auto& keyIter) { return keyIter._time; });
axstd::resize_and_transform(iter.second.begin(), iter.second.end(), values,
[](const auto& keyIter) { return keyIter._key; });

curve->scaleCurve = Curve::AnimationCurveVec3::create(&keys[0], &values[0].x, (int)keys.size());
if (curve->scaleCurve)
curve->scaleCurve->retain();
}

curve->scaleCurve = Curve::AnimationCurveVec3::create(&keys[0], &values[0], (int)keys.size());
if (curve->scaleCurve)
curve->scaleCurve->retain();
}

return true;
Expand Down
18 changes: 12 additions & 6 deletions core/3d/CCMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,33 +162,39 @@ Mesh* Mesh::create(const std::vector<float>& positions,
int perVertexSizeInFloat = 0;
std::vector<float> vertices;
std::vector<MeshVertexAttrib> attribs;

MeshVertexAttrib att;
att.type = backend::VertexFormat::FLOAT3;

if (positions.size())
attribs.reserve(3);

size_t hasNormal = 0;
size_t hasTexCoord = 0;
if (!positions.empty())
{
perVertexSizeInFloat += 3;
att.vertexAttrib = shaderinfos::VertexKey::VERTEX_ATTRIB_POSITION;
attribs.emplace_back(att);
}
if (normals.size())
if (!normals.empty())
{
perVertexSizeInFloat += 3;
att.vertexAttrib = shaderinfos::VertexKey::VERTEX_ATTRIB_NORMAL;
attribs.emplace_back(att);
hasNormal = 1;
}
if (texs.size())
if (!texs.empty())
{
perVertexSizeInFloat += 2;
att.type = backend::VertexFormat::FLOAT2;
att.vertexAttrib = shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD;
attribs.emplace_back(att);
hasTexCoord = 1;
}

bool hasNormal = (normals.size() != 0);
bool hasTexCoord = (texs.size() != 0);
// position, normal, texCoordinate into _vertexs
size_t vertexNum = positions.size() / 3;
vertices.reserve(positions.size() + hasNormal * 3 + hasTexCoord * 2);
for (size_t i = 0; i < vertexNum; i++)
{
vertices.emplace_back(positions[i * 3]);
Expand Down Expand Up @@ -341,8 +347,8 @@ void Mesh::setMaterial(Material* material)
{
// allocate MeshCommand vector for technique
// allocate MeshCommand for each pass
_meshCommands[technique->getName()] = std::vector<MeshCommand>(technique->getPasses().size());
auto& list = _meshCommands[technique->getName()];
list.resize(technique->getPasses().size());

int i = 0;
for (auto&& pass : technique->getPasses())
Expand Down
8 changes: 3 additions & 5 deletions core/3d/CCTerrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ USING_NS_AX;
#include "renderer/backend/Buffer.h"
#include "base/CCDirector.h"
#include "base/ccTypes.h"
#include "base/axstd.h"
#include "base/CCEventType.h"
#include "2d/CCCamera.h"
#include "platform/CCImage.h"
Expand Down Expand Up @@ -1322,11 +1323,8 @@ void Terrain::Chunk::updateIndicesLOD()

void Terrain::Chunk::calculateAABB()
{
std::vector<Vec3> pos;
for (size_t i = 0, size = _originalVertices.size(); i < size; ++i)
{
pos.emplace_back(_originalVertices[i]._position);
}
auto pos = axstd::pod_vector_from<Vec3>(_originalVertices.begin(), _originalVertices.end(),
[](const auto& it) { return it._position; });
_aabb.updateMinMax(&pos[0], pos.size());
}

Expand Down
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ endif()
add_subdirectory(${_AX_ROOT_PATH}/extensions ${ENGINE_BINARY_PATH}/extensions)

if(MSVC)
target_sources(${_AX_CORE_LIB} PRIVATE ./axmol.natvis)
target_sources(${_AX_CORE_LIB} PRIVATE ../thirdparty/yasio/yasio.natvis)
target_sources(${_AX_CORE_LIB} PRIVATE ../thirdparty/robin-map/tsl-robin-map.natvis)
target_compile_options(${_AX_CORE_LIB} PUBLIC "/Zm2000")
Expand Down
14 changes: 14 additions & 0 deletions core/axmol.natvis
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="axstd::pod_vector&lt;*&gt;">
<DisplayString>{{ size={_Mylast - _Myfirst} capacity={_Myend - _Myfirst} }}</DisplayString>
<Expand>
<Item Name="[size]" ExcludeView="simple">_Mylast - _Myfirst</Item>
<Item Name="[capacity]" ExcludeView="simple">_Myend - _Myfirst</Item>
<ArrayItems>
<Size>_Mylast - _Myfirst</Size>
<ValuePointer>_Myfirst</ValuePointer>
</ArrayItems>
</Expand>
</Type>
</AutoVisualizer>
5 changes: 2 additions & 3 deletions core/base/CCEventDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event)
bool isNeedsMutableSet = (oneByOneListeners && allAtOnceListeners);

const std::vector<Touch*>& originalTouches = event->getTouches();
std::vector<Touch*> mutableTouches(originalTouches.size());
std::copy(originalTouches.begin(), originalTouches.end(), mutableTouches.begin());
auto mutableTouches = originalTouches;

//
// process the target handlers 1st
Expand Down Expand Up @@ -1486,7 +1485,7 @@ void EventDispatcher::removeCustomEventListeners(std::string_view customEventNam
void EventDispatcher::removeAllEventListeners()
{
bool cleanMap = true;
std::vector<EventListener::ListenerID> types;
std::vector<std::string_view> types;
types.reserve(_listenerMap.size());

for (const auto& e : _listenerMap)
Expand Down
Loading

0 comments on commit 25d93bd

Please sign in to comment.