Create LinearAllocator for STL containers and override Vertex new/delete operators

This commit is contained in:
Cruel
2015-09-02 16:40:29 -04:00
parent 1e125c7ddc
commit 87fa1f0cf3
7 changed files with 140 additions and 2 deletions
+1 -1
View File
@@ -403,7 +403,7 @@ private:
BlendMode lastBlendMode; ///< Cached blending mode
Uint64 lastTextureId; ///< Cached texture
bool useVertexCache; ///< Did we previously use the vertex cache?
Vertex vertexCache[VertexCacheSize]; ///< Pre-transformed vertices cache
Vertex* vertexCache; ///< Pre-transformed vertices cache
};
////////////////////////////////////////////////////////////
+3 -1
View File
@@ -76,6 +76,8 @@ public :
////////////////////////////////////////////////////////////
Sprite(const Texture& texture, const IntRect& rectangle);
~Sprite();
////////////////////////////////////////////////////////////
/// \brief Change the source texture of the sprite
///
@@ -214,7 +216,7 @@ private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
Vertex m_vertices[4]; ///< Vertices defining the sprite's geometry
Vertex* m_vertices; ///< Vertices defining the sprite's geometry
const Texture* m_texture; ///< Texture of the sprite
IntRect m_textureRect; ///< Rectangle defining the area of the source texture to display
};
+8
View File
@@ -30,6 +30,7 @@
////////////////////////////////////////////////////////////
#include <cpp3ds/Graphics/Color.hpp>
#include <cpp3ds/System/Vector2.hpp>
#include <new>
namespace cpp3ds
{
@@ -89,6 +90,13 @@ public :
////////////////////////////////////////////////////////////
Vertex(const Vector2f& thePosition, const Color& theColor, const Vector2f& theTexCoords);
#ifndef EMULATION
static void* operator new (std::size_t size);
static void* operator new[] (std::size_t size);
static void operator delete (void *p);
static void operator delete[] (void *p);
#endif
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
+7
View File
@@ -32,6 +32,9 @@
#include <cpp3ds/Graphics/PrimitiveType.hpp>
#include <cpp3ds/Graphics/Rect.hpp>
#include <cpp3ds/Graphics/Drawable.hpp>
#ifndef EMULATION
#include <cpp3ds/System/LinearAllocator.hpp>
#endif
#include <vector>
@@ -186,7 +189,11 @@ private:
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
#ifdef EMULATION
std::vector<Vertex> m_vertices; ///< Vertices contained in the array
#else
std::vector<Vertex, LinearAllocator<Vertex>> m_vertices;
#endif
PrimitiveType m_primitiveType; ///< Type of primitives to draw
};
+72
View File
@@ -0,0 +1,72 @@
#ifndef CPP3DS_LINEARALLOCATOR_HPP
#define CPP3DS_LINEARALLOCATOR_HPP
#include <3ds.h>
#include <bits/c++allocator.h>
#if __cplusplus >= 201103L
#include <type_traits>
#endif
namespace cpp3ds {
template<typename T>
class LinearAllocator: public std::__allocator_base<T>
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template<typename T1>
struct rebind
{ typedef LinearAllocator<T1> other; };
#if __cplusplus >= 201103L
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2103. std::allocator propagate_on_container_move_assignment
typedef std::true_type propagate_on_container_move_assignment;
#endif
LinearAllocator() throw() { }
LinearAllocator(const LinearAllocator& __a) throw()
: std::__allocator_base<T>(__a) { }
template<typename T1>
LinearAllocator(const LinearAllocator<T1>&) throw() { }
~LinearAllocator() throw() { }
// NB: __n is permitted to be 0. The C++ standard says nothing
// about what the return value is when __n == 0.
pointer
allocate(size_type __n, const void* = 0)
{
if (__n > this->max_size())
std::__throw_bad_alloc();
return static_cast<T*>(linearAlloc(__n * sizeof(T)));
}
// __p is not permitted to be a null pointer.
void
deallocate(pointer __p, size_type)
{
linearFree(__p);
}
size_type
max_size() const
{
return size_t(-1) / sizeof(T);
}
// Inherit everything else.
};
} // namespace cpp3ds
#endif // CPP3DS_LINEARALLOCATOR_HPP
+13
View File
@@ -29,6 +29,9 @@
#include <cpp3ds/Graphics/Texture.hpp>
#include <cpp3ds/Graphics/RenderTarget.hpp>
#include <cstdlib>
#ifndef EMULATION
#include <3ds.h>
#endif
namespace cpp3ds
@@ -38,6 +41,7 @@ Sprite::Sprite() :
m_texture (NULL),
m_textureRect()
{
m_vertices = new Vertex[4];
}
@@ -46,6 +50,7 @@ Sprite::Sprite(const Texture& texture) :
m_texture (NULL),
m_textureRect()
{
m_vertices = new Vertex[4];
setTexture(texture);
}
@@ -55,11 +60,19 @@ Sprite::Sprite(const Texture& texture, const IntRect& rectangle) :
m_texture (NULL),
m_textureRect()
{
m_vertices = new Vertex[4];
setTexture(texture);
setTextureRect(rectangle);
}
////////////////////////////////////////////////////////////
Sprite::~Sprite()
{
delete[] m_vertices;
}
////////////////////////////////////////////////////////////
void Sprite::setTexture(const Texture& texture, bool resetRect)
{
+36
View File
@@ -26,6 +26,10 @@
// Headers
////////////////////////////////////////////////////////////
#include <cpp3ds/Graphics/Vertex.hpp>
#ifndef EMULATION
#include <3ds.h>
#include <bits/functexcept.h>
#endif
namespace cpp3ds
@@ -74,4 +78,36 @@ texCoords(theTexCoords)
{
}
#ifndef EMULATION
////////////////////////////////////////////////////////////
void* Vertex::operator new (std::size_t size)
{
void *p = linearAlloc(size);
if (!p)
std::__throw_bad_alloc();
return p;
}
////////////////////////////////////////////////////////////
void Vertex::operator delete (void *p)
{
linearFree(p);
}
////////////////////////////////////////////////////////////
void* Vertex::operator new[] (std::size_t size)
{
void *p = linearAlloc(size);
if (!p)
std::__throw_bad_alloc();
return p;
}
////////////////////////////////////////////////////////////
void Vertex::operator delete[] (void *p)
{
linearFree(p);
}
#endif
}