CUGL 4.0
Cornell University Game Library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Friends | List of all members
cugl::graphics::SpriteMesh Class Reference

#include <CUSpriteMesh.h>

Public Member Functions

 SpriteMesh ()
 
 ~SpriteMesh ()
 
void dispose ()
 
bool init ()
 
bool initWithMesh (const Mesh< SpriteVertex > &mesh, bool buffer=true)
 
bool initWithMesh (Mesh< SpriteVertex > &&mesh, bool buffer=true)
 
bool initWithData (const std::shared_ptr< JsonValue > &data, bool buffer=true)
 
const Mesh< SpriteVertexgetMesh () const
 
void setMesh (const Mesh< SpriteVertex > &mesh)
 
void setMesh (Mesh< SpriteVertex > &&mesh)
 
const std::shared_ptr< VertexBuffergetVertexBuffer () const
 
const std::shared_ptr< IndexBuffergetIndexBuffer () const
 
void draw (Uint32 group, const std::shared_ptr< GraphicsShader > &shader)
 

Static Public Member Functions

static std::shared_ptr< SpriteMeshalloc ()
 
static std::shared_ptr< SpriteMeshallocWithMesh (const Mesh< SpriteVertex > &mesh, bool buffer=true)
 
static std::shared_ptr< SpriteMeshallocWithMesh (Mesh< SpriteVertex > &&mesh, bool buffer=true)
 
static std::shared_ptr< SpriteMeshallocWithData (const std::shared_ptr< JsonValue > &data, bool buffer=true)
 
static void declareAttributes (Uint32 group, const std::shared_ptr< GraphicsShader > &shader)
 

Friends

class SpriteMeshLoader
 

Detailed Description

This class is represents a sprite with a static mesh.

Sprites are typically drawn with a SpriteBatch. Sprite batches work best when geometric complexity is low, as they are constantly updating their VertexBuffer. However, if a sprite does not change much, it sometimes makes sense to assign it its own VertexBuffer. That is the purpose of this class.

Because a sprite mesh has its own buffer, it is intended to be used directly with a GraphicsShader. With that said, the vertices are all instances of SpriteVertex, and thus this class can be used with the default SpriteBatch#getShader.

Constructor & Destructor Documentation

◆ SpriteMesh()

cugl::graphics::SpriteMesh::SpriteMesh ( )
inline

Creates a degenerate sprite mesh

This sprite mesh has degenerate values for all attributes. Nothing will rendered to the screen until it is initialized.

◆ ~SpriteMesh()

cugl::graphics::SpriteMesh::~SpriteMesh ( )
inline

Deletes this sprite mesh, disposing all of the resources.

Member Function Documentation

◆ alloc()

static std::shared_ptr< SpriteMesh > cugl::graphics::SpriteMesh::alloc ( )
inlinestatic

Returns a newly allocated empty sprite mesh.

The sprite mesh will have an empty mesh and therefore not create an a graphics buffer. It will need a mesh provided to setMesh.

Returns
a newly allocated empty sprite mesh.

◆ allocWithData()

static std::shared_ptr< SpriteMesh > cugl::graphics::SpriteMesh::allocWithData ( const std::shared_ptr< JsonValue > &  data,
bool  buffer = true 
)
inlinestatic

Returns a newly allocated sprite mesh from the given JsonValue

This JSON object can either be an array or an object. If it is an array, it should contain JSON values compatible with the SpriteVertex constructor. If it is a JSON object, then it supports the following attributes:

"vertices":      An array {@link SpriteVertex} descriptions
"indices":       An intenger list of triangle indices (in multiples of 3)
"triangulator":  One of 'monotone', 'earclip', 'delaunay', 'fan', or 'strip'

All attributes are optional. If "vertices" are missing, the mesh will be empty. If both "indices" and "triangulator" are missing, the mesh will use a triangle fan. The "triangulator" choice will only be applied if the "indices" are missing.

The sprite mesh will only create a graphics buffer if buffer is true. This is to handle cases where the sprite mesh is created in a separate thread (as OpenGL only allows graphics buffers to be made on the main thread).

Parameters
dataThe JSON object specifying the sprite mesh
bufferWhether to create a graphics buffer
Returns
a newly allocated sprite mesh from the given JsonValue

◆ allocWithMesh() [1/2]

static std::shared_ptr< SpriteMesh > cugl::graphics::SpriteMesh::allocWithMesh ( const Mesh< SpriteVertex > &  mesh,
bool  buffer = true 
)
inlinestatic

Returns a newly allocated sprite mesh with the given vertex mesh.

The sprite mesh will only create a graphics buffer if buffer is true. This is to handle cases where the sprite mesh is created in a separate thread (as OpenGL only allows graphics buffers to be made on the main thread).

Parameters
meshThe sprite vertex mesh
bufferWhether to create a graphics buffer
Returns
a newly allocated sprite mesh with the given vertex mesh.

◆ allocWithMesh() [2/2]

static std::shared_ptr< SpriteMesh > cugl::graphics::SpriteMesh::allocWithMesh ( Mesh< SpriteVertex > &&  mesh,
bool  buffer = true 
)
inlinestatic

Returns a newly allocated sprite mesh with the given vertex mesh.

The sprite mesh will only create a graphics buffer if buffer is true. This is to handle cases where the sprite mesh is created in a separate thread (as OpenGL only allows graphics buffers to be made on the main thread).

Parameters
meshThe sprite vertex mesh
bufferWhether to create a graphics buffer
Returns
a newly allocated sprite mesh with the given vertex mesh.

◆ declareAttributes()

static void cugl::graphics::SpriteMesh::declareAttributes ( Uint32  group,
const std::shared_ptr< GraphicsShader > &  shader 
)
static

Declares the attributes necessary for this mesh

This is a convenience method that allows us to set up a shader to receive the contents of this mesh. It should be called before compiling the shader.

Parameters
groupThe attribute group for this mesh
shaderThe shader to configure

◆ dispose()

void cugl::graphics::SpriteMesh::dispose ( )

Disposes the mesh and vertex buffer for this sprite mesh.

You must reinitialize the sprite mesh to use it.

◆ draw()

void cugl::graphics::SpriteMesh::draw ( Uint32  group,
const std::shared_ptr< GraphicsShader > &  shader 
)

Draws the sprite mesh with the given shader.

Note that sprite meshes to do not necessarily come with an texture or a gradient; they are just a mesh. Those values should be passed to the shader separately as resources.

To work properly, this shader must support attributes with the given group that match this mesh. Use declareAttributes before compiling the shader to guarantee this.

Parameters
groupThe attribute group for this mesh
shaderThe shader to draw with

◆ getIndexBuffer()

const std::shared_ptr< IndexBuffer > cugl::graphics::SpriteMesh::getIndexBuffer ( ) const
inline

Returns the vertex buffer for this sprite mesh.

This buffer is used to render the mesh. It should be combined with a GraphicsShader, preferably one compatible with SpriteBatch. To guarantee this, use declareAttributes before compiling the shader.

Returns
the vertex buffer for this mesh.

◆ getMesh()

const Mesh< SpriteVertex > cugl::graphics::SpriteMesh::getMesh ( ) const
inline

Returns the mesh associated with sprite mesh.

This mesh can be safely changed at any time. However, changing the mesh affects the getVertexBuffer. Therefore, the mesh should never be modified directly. Changes should go through setMesh.

Returns
the mesh associated with sprite mesh.

◆ getVertexBuffer()

const std::shared_ptr< VertexBuffer > cugl::graphics::SpriteMesh::getVertexBuffer ( ) const
inline

Returns the vertex buffer for this sprite mesh.

This buffer is used to render the mesh. It should be combined with a GraphicsShader, preferably one compatible with SpriteBatch. To guarantee this, use declareAttributes before compiling the shader.

Returns
the vertex buffer for this mesh.

◆ init()

bool cugl::graphics::SpriteMesh::init ( )
inline

Initializes an empty sprite mesh.

The sprite mesh will have an empty mesh and therefore not create an a graphics buffer. It will need a mesh provided to setMesh.

Returns
true if initialization was successful

◆ initWithData()

bool cugl::graphics::SpriteMesh::initWithData ( const std::shared_ptr< JsonValue > &  data,
bool  buffer = true 
)

Initializes a sprite mesh from the given JsonValue

This JSON object can either be an array or an object. If it is an array, it should contain JSON values compatible with the SpriteVertex constructor. If it is a JSON object, then it supports the following attributes:

"vertices":      An array {@link SpriteVertex} descriptions
"indices":       An intenger list of triangle indices (in multiples of 3)
"triangulator":  One of 'monotone', 'earclip', 'delaunay', 'fan', or 'strip'

All attributes are optional. If "vertices" are missing, the mesh will be empty. If both "indices" and "triangulator" are missing, the mesh will use a triangle fan. The "triangulator" choice will only be applied if the "indices" are missing.

The sprite mesh will only create a graphics buffer if buffer is true. This is to handle cases where the sprite mesh is created in a separate thread (as OpenGL only allows graphics buffers to be made on the main thread).

Parameters
dataThe JSON object specifying the sprite mesh
bufferWhether to create a graphics buffer
Returns
true if initialization was successful

◆ initWithMesh() [1/2]

bool cugl::graphics::SpriteMesh::initWithMesh ( const Mesh< SpriteVertex > &  mesh,
bool  buffer = true 
)

Initializes a sprite mesh with the given vertex mesh.

The sprite mesh will only create a graphics buffer if buffer is true. This is to handle cases where the sprite mesh is created in a separate thread (as OpenGL only allows graphics buffers to be made on the main thread).

Parameters
meshThe sprite vertex mesh
bufferWhether to create a graphics buffer
Returns
true if initialization was successful

◆ initWithMesh() [2/2]

bool cugl::graphics::SpriteMesh::initWithMesh ( Mesh< SpriteVertex > &&  mesh,
bool  buffer = true 
)

Initializes a sprite mesh with the given vertex mesh.

The sprite mesh will only create a graphics buffer if buffer is true. This is to handle cases where the sprite mesh is created in a separate thread (as OpenGL only allows graphics buffers to be made on the main thread).

Parameters
meshThe sprite vertex mesh
bufferWhether to create a graphics buffer
Returns
true if initialization was successful

◆ setMesh() [1/2]

void cugl::graphics::SpriteMesh::setMesh ( const Mesh< SpriteVertex > &  mesh)
inline

Sets the mesh associated with sprite mesh.

This mesh can be safely changed at any time. However, changing the mesh affects the getVertexBuffer. Therefore, the mesh should never be modified directly. Changes should go through setMesh.

This method will always create a graphics buffer, even if one did not previously exist. It should not be called outside the main thread.

Parameters
meshThe mesh associated with sprite mesh.

◆ setMesh() [2/2]

void cugl::graphics::SpriteMesh::setMesh ( Mesh< SpriteVertex > &&  mesh)
inline

Sets the mesh associated with sprite mesh.

This mesh can be safely changed at any time. However, changing the mesh affects the getVertexBuffer. Therefore, the mesh should never be modified directly. Changes should go through setMesh.

This method will always create a graphics buffer, even if one did not previously exist. It should not be called outside the main thread.

Parameters
meshThe mesh associated with sprite mesh.

The documentation for this class was generated from the following file: