diff --git a/include/cpp3ds/Graphics/RenderTarget.hpp b/include/cpp3ds/Graphics/RenderTarget.hpp index 39e5f11..1344daf 100644 --- a/include/cpp3ds/Graphics/RenderTarget.hpp +++ b/include/cpp3ds/Graphics/RenderTarget.hpp @@ -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 }; //////////////////////////////////////////////////////////// diff --git a/include/cpp3ds/Graphics/Sprite.hpp b/include/cpp3ds/Graphics/Sprite.hpp index 5c78235..596eaed 100644 --- a/include/cpp3ds/Graphics/Sprite.hpp +++ b/include/cpp3ds/Graphics/Sprite.hpp @@ -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 }; diff --git a/include/cpp3ds/Graphics/Vertex.hpp b/include/cpp3ds/Graphics/Vertex.hpp index 80eee38..5089fa2 100644 --- a/include/cpp3ds/Graphics/Vertex.hpp +++ b/include/cpp3ds/Graphics/Vertex.hpp @@ -30,6 +30,7 @@ //////////////////////////////////////////////////////////// #include #include +#include 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 //////////////////////////////////////////////////////////// diff --git a/include/cpp3ds/Graphics/VertexArray.hpp b/include/cpp3ds/Graphics/VertexArray.hpp index b5fcdc0..0be15a4 100644 --- a/include/cpp3ds/Graphics/VertexArray.hpp +++ b/include/cpp3ds/Graphics/VertexArray.hpp @@ -32,6 +32,9 @@ #include #include #include +#ifndef EMULATION +#include +#endif #include @@ -186,7 +189,11 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// + #ifdef EMULATION std::vector m_vertices; ///< Vertices contained in the array + #else + std::vector> m_vertices; + #endif PrimitiveType m_primitiveType; ///< Type of primitives to draw }; diff --git a/include/cpp3ds/System/LinearAllocator.hpp b/include/cpp3ds/System/LinearAllocator.hpp new file mode 100644 index 0000000..cf3e436 --- /dev/null +++ b/include/cpp3ds/System/LinearAllocator.hpp @@ -0,0 +1,72 @@ +#ifndef CPP3DS_LINEARALLOCATOR_HPP +#define CPP3DS_LINEARALLOCATOR_HPP + +#include <3ds.h> +#include +#if __cplusplus >= 201103L +#include +#endif + +namespace cpp3ds { + + template + class LinearAllocator: public std::__allocator_base + { + 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 + struct rebind + { typedef LinearAllocator 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(__a) { } + + template + LinearAllocator(const LinearAllocator&) 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(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 diff --git a/src/cpp3ds/Graphics/Sprite.cpp b/src/cpp3ds/Graphics/Sprite.cpp index 01f8ad5..900d86b 100644 --- a/src/cpp3ds/Graphics/Sprite.cpp +++ b/src/cpp3ds/Graphics/Sprite.cpp @@ -29,6 +29,9 @@ #include #include #include +#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) { diff --git a/src/cpp3ds/Graphics/Vertex.cpp b/src/cpp3ds/Graphics/Vertex.cpp index d976088..5a13e52 100644 --- a/src/cpp3ds/Graphics/Vertex.cpp +++ b/src/cpp3ds/Graphics/Vertex.cpp @@ -26,6 +26,10 @@ // Headers //////////////////////////////////////////////////////////// #include +#ifndef EMULATION +#include <3ds.h> +#include +#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 + }