Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
rendering_engine::Sprite2D Class Reference

2D drawable component for rendering textured quad. More...

#include <sprite_2d.hpp>

Inherits rendering_engine::Drawable2D.

Public Member Functions

 Sprite2D (RenderResourceContext renderContext, Scene &scene, std::string textureName)
 Constructs the Sprite2D with a render context. More...
 
void Initialize () override
 Initializes render resource pointers (material, mesh, etc.). Must be called after setting material and mesh names. More...
 
void Update (float deltaTime) override
 Updates logic (animation, movement, etc.) for this drawable. More...
 
void Draw (const Camera2D &camera) override
 Submits this quad to the renderer for drawing. More...
 
void SetSpriteScale (float scale)
 Sets a uniform scale relative to the sprite's texture size. More...
 
 Sprite2D (const Sprite2D &rhs)=delete
 
Sprite2Doperator= (const Sprite2D &rhs)=delete
 
- Public Member Functions inherited from rendering_engine::Drawable2D
 Drawable2D (RenderResourceContext renderContext, Scene &scene)
 Constructs the Drawable2D with a resource context. More...
 
void Initialize () override
 Initializes render resources. More...
 
void Update (float deltaTime) override
 Updates model matrix (and any other logic). More...
 
virtual void Draw (const Camera2D &camera)=0
 Submits this quad to the renderer for drawing. More...
 
void SetPosition (const glm::vec2 &position)
 Sets the quad position in 2D space. More...
 
void SetRotation (float angleDegrees)
 Sets the quad rotation. More...
 
void SetScale (const glm::vec2 &scale)
 Sets the quad scale along each axis. More...
 
const glm::vec2 & GetPosition () const
 Gets the quad position. More...
 
float GetRotation () const
 Gets the quad rotation angle (degrees). More...
 
const glm::vec2 & GetScale () const
 Gets the quad scale. More...
 
SceneComponent2DGetTransform ()
 Access to the underlying SceneComponent2D (transform). More...
 
const SceneComponent2DGetTransform () const
 
void Destroy () override
 Requests destruction of this drawable. More...
 
- Public Member Functions inherited from rendering_engine::DrawableComponent
 DrawableComponent (RenderResourceContext renderContext, Scene &scene)
 Constructs the DrawableComponent with a resource context. More...
 
virtual ~DrawableComponent ()=default
 Virtual destructor. More...
 
virtual void Initialize ()
 Initializes render resource pointers (material, mesh, etc.). Must be called after setting material and mesh names. More...
 
virtual void Update (float deltaTime)=0
 Updates logic (animation, movement, etc.) for this drawable. More...
 
virtual void Shutdown ()
 Releases all render resources owned by this drawable. More...
 
virtual void Destroy ()
 Requests destruction of this drawable. More...
 
void UpdateOnTick (bool in)
 
 DrawableComponent (const DrawableComponent &)=delete
 
DrawableComponentoperator= (const DrawableComponent &)=delete
 

Protected Attributes

std::string mTextureName
 
glm::vec2 mTextureRespectiveScale
 
- Protected Attributes inherited from rendering_engine::Drawable2D
SceneComponent2D mSceneComponent
 
- Protected Attributes inherited from rendering_engine::DrawableComponent
RenderResourceContext mRenderContext
 
ScenemScene
 
std::vector< RenderBatchmRenderBatches
 
bool bUpdateOnTick
 

Additional Inherited Members

- Protected Member Functions inherited from rendering_engine::DrawableComponent
void AddRenderBatch (std::string meshName, std::string materialName)
 

Detailed Description

2D drawable component for rendering textured quad.

Note
Not copyable or assignable.
See also
Quad2D, Drawable2D, DrawableComponent, SceneComponent2D, Camera2D

Definition at line 21 of file sprite_2d.hpp.

Constructor & Destructor Documentation

◆ Sprite2D() [1/2]

rendering_engine::Sprite2D::Sprite2D ( RenderResourceContext  renderContext,
Scene scene,
std::string  textureName 
)

Constructs the Sprite2D with a render context.

Parameters
renderContextResource context.

Definition at line 15 of file sprite_2d.cpp.

16 :
17 Drawable2D(renderContext, scene),
18 mTextureName(textureName)
19{}
Drawable2D(RenderResourceContext renderContext, Scene &scene)
Constructs the Drawable2D with a resource context.
Definition: drawable_2d.cpp:7

◆ Sprite2D() [2/2]

rendering_engine::Sprite2D::Sprite2D ( const Sprite2D rhs)
delete

Member Function Documentation

◆ Draw()

void rendering_engine::Sprite2D::Draw ( const Camera2D camera)
overridevirtual

Submits this quad to the renderer for drawing.

Implements rendering_engine::Drawable2D.

Definition at line 62 of file sprite_2d.cpp.

63{
64 Transformations2D transformations;
65 transformations.model = GetTransform().GetWorldMatrix();
66 transformations.view = camera.GetWorldView();
67 transformations.proj = camera.GetProjectionMatrix();
68
69 for (auto& renderBatch : mRenderBatches)
70 {
71 renderBatch.renderResources->SubmitResources(transformations, renderBatch.materialParameters);
72 }
73}
SceneComponent2D & GetTransform()
Access to the underlying SceneComponent2D (transform).
Definition: drawable_2d.cpp:55
std::vector< RenderBatch > mRenderBatches
const glm::mat4 & GetWorldMatrix()
Returns the world transformation matrix.

◆ Initialize()

void rendering_engine::Sprite2D::Initialize ( )
overridevirtual

Initializes render resource pointers (material, mesh, etc.). Must be called after setting material and mesh names.

Reimplemented from rendering_engine::Drawable2D.

Definition at line 21 of file sprite_2d.cpp.

22{
24 const float width = static_cast<float>(texture->GetCpuImageData().GetWidth());
25 const float height = static_cast<float>(texture->GetCpuImageData().GetHeight());
26
27 const std::string materialName = "Sprite2D_" + mTextureName + "Mat";
28 auto materialCache = mRenderContext.materialCache;
29
30 Material* material = materialCache->GetMaterial(materialName);
31
32 // CREATE MATERIAL ONLY ONCE
33 if (!material)
34 {
35 MaterialSettings materialSettings;
36 materialSettings.parentMaterialName = "Quad2D";
37 materialSettings.materialName = materialName;
38 materialSettings.materialDomain = MaterialDomain::Sprite2D;
39 materialSettings.shadingModel = ShadingModel::Unlit;
40 materialSettings.blendMode = BlendMode::Opaque;
41
42 materialCache->AddMaterial(materialSettings);
43
44 material = materialCache->GetMaterial(materialName);
45 material->AddTexture(mTextureName);
46 material->InitializeRenderResources();
47 }
48
49 AddRenderBatch("Quad2D", materialName);
50
52
53 mTextureRespectiveScale = glm::vec2(width, height);
55}
void Initialize() override
Initializes render resources.
Definition: drawable_2d.cpp:12
void SetScale(const glm::vec2 &scale)
Sets the quad scale along each axis.
Definition: drawable_2d.cpp:35
void AddRenderBatch(std::string meshName, std::string materialName)
Material * GetMaterial(std::string materialName)
Retrieves a pointer to a Material instance by name.
glm::vec2 mTextureRespectiveScale
Definition: sprite_2d.hpp:61
std::shared_ptr< ImageDataGpu > GetTextureResources(std::string filename)
Retrieves the full texture resource wrapper from cache.

◆ operator=()

Sprite2D & rendering_engine::Sprite2D::operator= ( const Sprite2D rhs)
delete

◆ SetSpriteScale()

void rendering_engine::Sprite2D::SetSpriteScale ( float  scale)

Sets a uniform scale relative to the sprite's texture size.

The scale factor is applied to the original texture dimensions, allowing convenient uniform resizing of the sprite.

Parameters
scaleUniform scale factor (1.0 = original texture size).

Definition at line 75 of file sprite_2d.cpp.

76{
78}

◆ Update()

void rendering_engine::Sprite2D::Update ( float  deltaTime)
overridevirtual

Updates logic (animation, movement, etc.) for this drawable.

Parameters
deltaTimeTime step (seconds).

Reimplemented from rendering_engine::Drawable2D.

Definition at line 57 of file sprite_2d.cpp.

58{
59 Drawable2D::Update(deltaTime);
60}
void Update(float deltaTime) override
Updates model matrix (and any other logic).
Definition: drawable_2d.cpp:17

Member Data Documentation

◆ mTextureName

std::string rendering_engine::Sprite2D::mTextureName
protected

Definition at line 60 of file sprite_2d.hpp.

◆ mTextureRespectiveScale

glm::vec2 rendering_engine::Sprite2D::mTextureRespectiveScale
protected

Definition at line 61 of file sprite_2d.hpp.


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