2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/SpriteSheetLoader.h"
|
|
|
|
#include "base/Director.h"
|
2021-08-13 14:52:58 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2024-08-26 00:25:33 +08:00
|
|
|
namespace ax
|
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
|
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)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
|
|
|
const auto vertexCount = vertices.size();
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto indexCount = triangleIndices.size();
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
const auto scaleFactor = AX_CONTENT_SCALE_FACTOR();
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
auto* vertexData = new V3F_C4B_T2F[vertexCount];
|
2021-08-13 14:52:58 +08:00
|
|
|
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);
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-08-13 14:52:58 +08:00
|
|
|
info.triangles.indexCount = static_cast<int>(indexCount);
|
2021-12-25 10:04:45 +08:00
|
|
|
info.triangles.indices = indexData;
|
2021-08-13 14:52:58 +08:00
|
|
|
info.setRect(Rect(0, 0, spriteSize.width, spriteSize.height));
|
|
|
|
}
|
|
|
|
|
2024-08-26 00:25:33 +08:00
|
|
|
}
|