mirror of
https://github.com/love2d/love.git
synced 2026-08-19 12:14:20 +02:00
Clean up some Mesh code
This commit is contained in:
@@ -158,23 +158,11 @@ public:
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Mapper(Buffer &buffer)
|
Mapper(Buffer &buffer) : buffer(buffer) { data = buffer.map(); }
|
||||||
: buf(buffer)
|
~Mapper() { buffer.unmap(); }
|
||||||
{
|
|
||||||
elems = buf.map();
|
|
||||||
}
|
|
||||||
|
|
||||||
~Mapper()
|
Buffer &buffer;
|
||||||
{
|
void *data;
|
||||||
buf.unmap();
|
|
||||||
}
|
|
||||||
|
|
||||||
void *get() { return elems; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
Buffer &buf;
|
|
||||||
void *elems;
|
|
||||||
|
|
||||||
}; // Mapper
|
}; // Mapper
|
||||||
|
|
||||||
|
|||||||
@@ -176,7 +176,7 @@ void Graphics::createQuadIndexBuffer()
|
|||||||
quadIndexBuffer = newIndexBuffer(INDEX_UINT16, nullptr, size, BUFFERUSAGE_STATIC, 0);
|
quadIndexBuffer = newIndexBuffer(INDEX_UINT16, nullptr, size, BUFFERUSAGE_STATIC, 0);
|
||||||
|
|
||||||
Buffer::Mapper map(*quadIndexBuffer);
|
Buffer::Mapper map(*quadIndexBuffer);
|
||||||
fillIndices(TRIANGLEINDEX_QUADS, 0, LOVE_UINT16_MAX, (uint16 *) map.get());
|
fillIndices(TRIANGLEINDEX_QUADS, 0, LOVE_UINT16_MAX, (uint16 *) map.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
Quad *Graphics::newQuad(Quad::Viewport v, double sw, double sh)
|
Quad *Graphics::newQuad(Quad::Viewport v, double sw, double sh)
|
||||||
|
|||||||
@@ -118,12 +118,6 @@ Mesh::Mesh(graphics::Graphics *gfx, const std::vector<Buffer::DataDeclaration> &
|
|||||||
Mesh::~Mesh()
|
Mesh::~Mesh()
|
||||||
{
|
{
|
||||||
delete vertexScratchBuffer;
|
delete vertexScratchBuffer;
|
||||||
|
|
||||||
for (const auto &attrib : attachedAttributes)
|
|
||||||
{
|
|
||||||
if (attrib.second.buffer != nullptr && attrib.second.buffer != vertexBuffer.get())
|
|
||||||
attrib.second.buffer->release();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::setupAttachedAttributes()
|
void Mesh::setupAttachedAttributes()
|
||||||
@@ -282,28 +276,22 @@ void Mesh::attachAttribute(const std::string &name, Buffer *buffer, const std::s
|
|||||||
throw love::Exception("A maximum of %d attributes can be attached at once.", VertexAttributes::MAX);
|
throw love::Exception("A maximum of %d attributes can be attached at once.", VertexAttributes::MAX);
|
||||||
|
|
||||||
newattrib.buffer = buffer;
|
newattrib.buffer = buffer;
|
||||||
newattrib.enabled = oldattrib.buffer ? oldattrib.enabled : true;
|
newattrib.enabled = oldattrib.buffer.get() ? oldattrib.enabled : true;
|
||||||
newattrib.index = buffer->getDataMemberIndex(attachname);
|
newattrib.index = buffer->getDataMemberIndex(attachname);
|
||||||
newattrib.step = step;
|
newattrib.step = step;
|
||||||
|
|
||||||
if (newattrib.index < 0)
|
if (newattrib.index < 0)
|
||||||
throw love::Exception("The specified vertex buffer does not have a vertex attribute named '%s'", attachname.c_str());
|
throw love::Exception("The specified vertex buffer does not have a vertex attribute named '%s'", attachname.c_str());
|
||||||
|
|
||||||
newattrib.buffer->retain();
|
|
||||||
|
|
||||||
attachedAttributes[name] = newattrib;
|
attachedAttributes[name] = newattrib;
|
||||||
|
|
||||||
if (oldattrib.buffer)
|
|
||||||
oldattrib.buffer->release();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Mesh::detachAttribute(const std::string &name)
|
bool Mesh::detachAttribute(const std::string &name)
|
||||||
{
|
{
|
||||||
auto it = attachedAttributes.find(name);
|
auto it = attachedAttributes.find(name);
|
||||||
|
|
||||||
if (it != attachedAttributes.end() && it->second.buffer != vertexBuffer.get())
|
if (it != attachedAttributes.end())
|
||||||
{
|
{
|
||||||
it->second.buffer->release();
|
|
||||||
attachedAttributes.erase(it);
|
attachedAttributes.erase(it);
|
||||||
|
|
||||||
if (getAttributeIndex(name) != -1)
|
if (getAttributeIndex(name) != -1)
|
||||||
@@ -340,7 +328,7 @@ void Mesh::flush()
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
static void copyToIndexBuffer(const std::vector<uint32> &indices, Buffer::Mapper &buffermap, size_t maxval)
|
static void copyToIndexBuffer(const std::vector<uint32> &indices, Buffer::Mapper &buffermap, size_t maxval)
|
||||||
{
|
{
|
||||||
T *elems = (T *) buffermap.get();
|
T *elems = (T *) buffermap.data;
|
||||||
|
|
||||||
for (size_t i = 0; i < indices.size(); i++)
|
for (size_t i = 0; i < indices.size(); i++)
|
||||||
{
|
{
|
||||||
@@ -405,7 +393,7 @@ void Mesh::setVertexMap(IndexDataType datatype, const void *data, size_t datasiz
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
Buffer::Mapper ibomap(*indexBuffer);
|
Buffer::Mapper ibomap(*indexBuffer);
|
||||||
memcpy(ibomap.get(), data, datasize);
|
memcpy(ibomap.data, data, datasize);
|
||||||
|
|
||||||
useIndexBuffer = true;
|
useIndexBuffer = true;
|
||||||
indexDataType = datatype;
|
indexDataType = datatype;
|
||||||
@@ -541,7 +529,7 @@ void Mesh::drawInstanced(Graphics *gfx, const Matrix4 &m, int instancecount)
|
|||||||
if (!attrib.second.enabled)
|
if (!attrib.second.enabled)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Buffer *buffer = attrib.second.buffer;
|
Buffer *buffer = attrib.second.buffer.get();
|
||||||
int attributeindex = -1;
|
int attributeindex = -1;
|
||||||
|
|
||||||
// If the attribute is one of the LOVE-defined ones, use the constant
|
// If the attribute is one of the LOVE-defined ones, use the constant
|
||||||
|
|||||||
@@ -174,7 +174,7 @@ private:
|
|||||||
|
|
||||||
struct AttachedAttribute
|
struct AttachedAttribute
|
||||||
{
|
{
|
||||||
Buffer *buffer;
|
StrongRef<Buffer> buffer;
|
||||||
int index;
|
int index;
|
||||||
AttributeStep step;
|
AttributeStep step;
|
||||||
bool enabled;
|
bool enabled;
|
||||||
|
|||||||
Reference in New Issue
Block a user