axmol/cocos/2d/CCSpriteSheetLoader.cpp

45 lines
1.6 KiB
C++
Raw Normal View History

#include "2d/CCSpriteSheetLoader.h"
#include "base/CCDirector.h"
#include <vector>
using namespace std;
NS_CC_BEGIN
2021-10-23 23:27:14 +08:00
void SpriteSheetLoader::initializePolygonInfo(const Vec2& textureSize,
2021-12-25 10:04:45 +08:00
const Vec2& spriteSize,
const std::vector<int>& vertices,
const std::vector<int>& verticesUV,
const std::vector<int>& triangleIndices,
PolygonInfo& info)
{
const auto vertexCount = vertices.size();
2021-12-25 10:04:45 +08:00
const auto indexCount = triangleIndices.size();
const auto scaleFactor = CC_CONTENT_SCALE_FACTOR();
2021-12-08 00:11:53 +08:00
auto* vertexData = new V3F_C4B_T2F[vertexCount];
for (size_t i = 0; i < vertexCount / 2; i++)
{
vertexData[i].colors = Color4B::WHITE;
2021-12-25 10:04:45 +08:00
vertexData[i].vertices =
Vec3(vertices[i * 2] / scaleFactor, (spriteSize.height - vertices[i * 2 + 1]) / scaleFactor, 0);
vertexData[i].texCoords =
Tex2F(verticesUV[i * 2] / textureSize.width, verticesUV[i * 2 + 1] / textureSize.height);
}
auto* indexData = new unsigned short[indexCount];
for (size_t i = 0; i < indexCount; i++)
{
indexData[i] = static_cast<unsigned short>(triangleIndices[i]);
}
2021-12-25 10:04:45 +08:00
info.triangles.vertCount = static_cast<int>(vertexCount);
info.triangles.verts = vertexData;
info.triangles.indexCount = static_cast<int>(indexCount);
2021-12-25 10:04:45 +08:00
info.triangles.indices = indexData;
info.setRect(Rect(0, 0, spriteSize.width, spriteSize.height));
}
NS_CC_END