Skip to content

Commit

Permalink
Update SpriteRenderer support render raw texture
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Sep 25, 2024
1 parent d2a8440 commit 4974e39
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 82 deletions.
7 changes: 7 additions & 0 deletions Projects/Skylicht/Components/SpriteDraw/CSprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ namespace Skylicht

}

void CSprite::setTexture(ITexture* texture, float scale, const SColor& color)
{
m_data->Texture = texture;
m_data->Scale = scale;
m_data->Color = color;
}

void CSprite::setFrame(SFrame* frame, float scale, const SColor& color)
{
m_data->Frame = frame;
Expand Down
12 changes: 12 additions & 0 deletions Projects/Skylicht/Components/SpriteDraw/CSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ namespace Skylicht
m_data->Color = color;
}

inline const SColor& getColor()
{
return m_data->Color;
}

inline void setScale(float scale)
{
m_data->Scale = scale;
}

inline float getScale()
{
return m_data->Scale;
Expand All @@ -58,6 +68,8 @@ namespace Skylicht
return m_data->ViewScale;
}

void setTexture(ITexture* texture, float scale, const SColor& color);

void setFrame(SFrame* frame, float scale, const SColor& color);

void setCenter(bool b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Skylicht
IMPLEMENT_DATA_TYPE_INDEX(CSpriteDrawData);

CSpriteDrawData::CSpriteDrawData() :
Texture(NULL),
Frame(NULL),
Scale(1.0f),
ViewScale(0.0f),
Expand Down
2 changes: 2 additions & 0 deletions Projects/Skylicht/Components/SpriteDraw/CSpriteDrawData.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace Skylicht

virtual ~CSpriteDrawData();

ITexture* Texture;

SFrame* Frame;

SColor Color;
Expand Down
177 changes: 145 additions & 32 deletions Projects/Skylicht/Components/SpriteDraw/CSpriteRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace Skylicht
CEntity* entity = entities[i];

CSpriteDrawData* spriteData = GET_ENTITY_DATA(entity, CSpriteDrawData);
if (spriteData != NULL && spriteData->Frame != NULL)
if (spriteData != NULL && (spriteData->Frame != NULL || spriteData->Texture != NULL))
m_sprites.push(spriteData);
}
}
Expand All @@ -95,11 +95,14 @@ namespace Skylicht
CSpriteDrawData* a = *((CSpriteDrawData**)ca);
CSpriteDrawData* b = *((CSpriteDrawData**)cb);

if (a->Frame->Image->Texture == b->Frame->Image->Texture)
ITexture* textureA = a->Texture != NULL ? a->Texture : a->Frame->Image->Texture;
ITexture* textureB = b->Texture != NULL ? b->Texture : b->Frame->Image->Texture;

if (textureA == textureB)
{
return 0;
}
else if (a->Frame->Image->Texture > b->Frame->Image->Texture)
else if (textureA > textureB)
{
return 1;
}
Expand Down Expand Up @@ -173,8 +176,6 @@ namespace Skylicht
{
CSpriteDrawData* sprite = sprites[i];

SFrame* frame = sprite->Frame;

// position
CWorldTransformData* transformData = GET_ENTITY_DATA(sprite->Entity, CWorldTransformData);
core::vector3df pos = transformData->World.getTranslation();
Expand All @@ -189,35 +190,22 @@ namespace Skylicht
}

// render buffer
currentTexture = frame->Image->Texture;

core::dimension2du size = frame->Image->Texture->getSize();
float texWidth = (float)size.Width;
float texHeight = (float)size.Height;

// batch sprite
std::vector<SModuleOffset>::iterator it = frame->ModuleOffset.begin(), end = frame->ModuleOffset.end();
while (it != end)
if (sprite->Texture)
{
SModuleOffset& module = (*it);

vb->set_used(numVertex + 4);
ib->set_used(numIndex + 6);

video::S3DVertex* vertices = (video::S3DVertex*)vb->getVertices();
vertices += numVertex;
currentTexture = sprite->Texture;

u16* indices = (u16*)ib->getIndices();
indices += numIndex;
const core::matrix4* mat = NULL;
core::dimension2du size = currentTexture->getSize();
float texWidth = (float)size.Width;
float texHeight = (float)size.Height;

// center
float offsetX = 0.0f;
float offsetY = 0.0f;

if (sprite->Center)
{
offsetX = module.Module->W * 0.5f * scale;
offsetY = module.Module->H * 0.5f * scale;
offsetX = -texWidth * 0.5f * scale;
offsetY = -texHeight * 0.5f * scale;
}

if (sprite->Billboard)
Expand All @@ -227,24 +215,149 @@ namespace Skylicht
matData[14] = pos.Z;
matData[15] = 1.0f;

module.getPositionBuffer(vertices, indices, numVertex, -offsetX, offsetY, transform, scale, -scale);
mat = &transform;
}
else
{
module.getPositionBuffer(vertices, indices, numVertex, -offsetX, offsetY, transformData->World, scale, -scale);
mat = &transformData->World;
}

module.getTexCoordBuffer(vertices, texWidth, texHeight);
module.getColorBuffer(vertices, sprite->Color);
vb->set_used(numVertex + 4);
ib->set_used(numIndex + 6);

video::S3DVertex* vertices = (video::S3DVertex*)vb->getVertices();
vertices += numVertex;

u16* indices = (u16*)ib->getIndices();
indices += numIndex;

float x1 = (float)offsetX;
float y1 = (float)offsetY;
float x2 = (float)(offsetX + texWidth * scale);
float y2 = (float)(offsetY + texHeight * scale);

vertices[0].Pos.X = x1;
vertices[0].Pos.Y = y1;
vertices[0].Pos.Z = 0.0f;
mat->transformVect(vertices[0].Pos);

vertices[1].Pos.X = x2;
vertices[1].Pos.Y = y1;
vertices[1].Pos.Z = 0.0f;
mat->transformVect(vertices[1].Pos);

vertices[2].Pos.X = x2;
vertices[2].Pos.Y = y2;
vertices[2].Pos.Z = 0.0f;
mat->transformVect(vertices[2].Pos);

vertices[3].Pos.X = x1;
vertices[3].Pos.Y = y2;
vertices[3].Pos.Z = 0.0f;
mat->transformVect(vertices[3].Pos);

vertices[0].TCoords.X = 0.0f;
vertices[0].TCoords.Y = 1.0f;
vertices[0].Normal.X = 0.0f;
vertices[0].Normal.Y = 1.0f;

vertices[1].TCoords.X = 1.0f;
vertices[1].TCoords.Y = 1.0f;
vertices[1].Normal.X = 1.0f;
vertices[1].Normal.Y = 1.0f;

vertices[2].TCoords.X = 1.0f;
vertices[2].TCoords.Y = 0.0f;
vertices[2].Normal.X = 1.0f;
vertices[2].Normal.Y = 0.0f;

vertices[3].TCoords.X = 0.0f;
vertices[3].TCoords.Y = 0.0f;
vertices[3].Normal.X = 0.0f;
vertices[3].Normal.Y = 0.0f;

vertices[0].Color = sprite->Color;
vertices[1].Color = sprite->Color;
vertices[2].Color = sprite->Color;
vertices[3].Color = sprite->Color;

indices[0] = numVertex;
indices[1] = numVertex + 1;
indices[2] = numVertex + 2;
indices[3] = numVertex;
indices[4] = numVertex + 2;
indices[5] = numVertex + 3;

numVertex += 4;
numIndex += 6;
}
else
{
currentTexture = sprite->Frame->Image->Texture;

core::dimension2du size = currentTexture->getSize();
float texWidth = (float)size.Width;
float texHeight = (float)size.Height;

// batch sprite
SFrame* frame = sprite->Frame;
std::vector<SModuleOffset>::iterator it = frame->ModuleOffset.begin(), end = frame->ModuleOffset.end();
while (it != end)
{
SModuleOffset& module = (*it);

vb->set_used(numVertex + 4);
ib->set_used(numIndex + 6);

video::S3DVertex* vertices = (video::S3DVertex*)vb->getVertices();
vertices += numVertex;

u16* indices = (u16*)ib->getIndices();
indices += numIndex;

// center
float offsetX = 0.0f;
float offsetY = 0.0f;

++it;
if (sprite->Center)
{
offsetX = module.Module->W * 0.5f * scale;
offsetY = module.Module->H * 0.5f * scale;
}

if (sprite->Billboard)
{
matData[12] = pos.X;
matData[13] = pos.Y;
matData[14] = pos.Z;
matData[15] = 1.0f;

module.getPositionBuffer(vertices, indices, numVertex, -offsetX, offsetY, transform, scale, -scale);
}
else
{
module.getPositionBuffer(vertices, indices, numVertex, -offsetX, offsetY, transformData->World, scale, -scale);
}

module.getTexCoordBuffer(vertices, texWidth, texHeight);
module.getColorBuffer(vertices, sprite->Color);

numVertex += 4;
numIndex += 6;

++it;
}
}

// if change texture or last sprite
if (i == n - 1 || currentTexture != sprites[i + 1]->Frame->Image->Texture)
ITexture* nextTexture = NULL;
if (i < n - 1)
{
CSpriteDrawData* nextSprite = sprites[i + 1];
nextTexture = nextSprite->Texture != NULL ? nextSprite->Texture : nextSprite->Frame->Image->Texture;
}

if (i == n - 1 || currentTexture != nextTexture)
{
m_meshBuffer->setDirty();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ namespace Skylicht
vertices[1].TCoords.Y = y1;

vertices[1].Normal.X = 1.0f;
vertices[1].Normal.Y = 0;
vertices[1].Normal.Y = 0.0f;

vertices[2].TCoords.X = x2;
vertices[2].TCoords.Y = y2;
Expand Down
14 changes: 0 additions & 14 deletions Samples/Audio/Source/CTestSoundComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include "GameObject/CGameObject.h"

CTestSoundComponent::CTestSoundComponent() :
m_sprite(NULL),
m_frame(NULL),
m_sound(NULL),
m_angle(90.0f),
m_radius(5.0f)
Expand All @@ -14,19 +12,7 @@ CTestSoundComponent::CTestSoundComponent() :

CTestSoundComponent::~CTestSoundComponent()
{
m_sprite->drop();
}

void CTestSoundComponent::setFrame(CSpriteAtlas* sprite, SFrame* frame)
{
m_sprite = sprite;
m_frame = frame;

m_sprite->grab();

CSprite::setFrame(m_frame, 0.005f, SColor(255, 255, 255, 255));
setBillboard(true);
setCenter(true);
}

void CTestSoundComponent::initComponent()
Expand Down
4 changes: 0 additions & 4 deletions Samples/Audio/Source/CTestSoundComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
class CTestSoundComponent : public Skylicht::CSprite
{
protected:
CSpriteAtlas* m_sprite;
SFrame* m_frame;
Audio::CAudioEmitter* m_sound;

float m_angle;
Expand All @@ -24,8 +22,6 @@ class CTestSoundComponent : public Skylicht::CSprite

virtual void updateComponent();

void setFrame(CSpriteAtlas* sprite, SFrame* frame);

inline void setAudioEmitter(Audio::CAudioEmitter* sound)
{
m_sound = sound;
Expand Down
12 changes: 4 additions & 8 deletions Samples/Audio/Source/CViewDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
CViewDemo::CViewDemo() :
m_music(NULL),
m_sound(NULL),
m_sprite(NULL),
m_streamFactory(NULL)
{
Audio::initSkylichtAudio();
Expand All @@ -22,9 +21,6 @@ CViewDemo::CViewDemo() :

CViewDemo::~CViewDemo()
{
if (m_sprite)
m_sprite->drop();

Audio::CAudioEngine::getSoundEngine()->unRegisterStreamFactory(m_streamFactory);
delete m_streamFactory;

Expand Down Expand Up @@ -68,17 +64,17 @@ void CViewDemo::onInit()
}

// atlas texture
m_sprite = new CSpriteAtlas(video::ECF_A8R8G8B8, 512, 512);
SFrame* frame = m_sprite->addFrame("Helicopter", "SampleAudio/helicopter.png");
m_sprite->updateTexture();
ITexture* texture = CTextureManager::getInstance()->getTexture("SampleAudio/helicopter.png");

// create helicoper obj & sound
CScene* scene = context->getScene();
CZone* zone = scene->getZone(0);

CGameObject* helicoper = zone->createEmptyObject();
CTestSoundComponent* testSound = helicoper->addComponent<CTestSoundComponent>();
testSound->setFrame(m_sprite, frame);
testSound->setTexture(texture, 0.005f, SColor(255, 255, 255, 255));
testSound->setBillboard(true);
testSound->setCenter(true);
testSound->setAudioEmitter(m_sound);
}

Expand Down
Loading

0 comments on commit 4974e39

Please sign in to comment.