Skip to content

Commit

Permalink
Updatable TextDrawing
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Nov 14, 2024
1 parent ad7573f commit bdca9ee
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
3 changes: 3 additions & 0 deletions swCommonLib/Common/Buffers/BufferRaw.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "swCommonLib/Common/TypesDefinitions.h"
#include "swCommonLib/Common/RTTR.h"
#include "swCommonLib/Common/Buffers/BufferRange.h"

#include <memory>

Expand Down Expand Up @@ -52,6 +53,8 @@ class BufferRaw
const uint8* GetData () const { return reinterpret_cast< const uint8* >( m_data ); }
bool IsValid () const { return m_data != nullptr; }

BufferRange AsRange () const { return BufferRange( m_data, m_size ); }

public:

template< typename ContentType, class Aloc > friend class BufferTyped;
Expand Down
39 changes: 34 additions & 5 deletions swGUI/Core/System/Rendering/Drawings/Drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,44 @@ ReturnResult Drawing::UpdateGeometry ( ResourceManagerAPI rm, Geometry* geo
ReturnIfInvalid( result );
GeometryData& data = result.Get();

// Create new buffers if they didn't existed.
if( !vertexBuffer || !indexBuffer )
// Create new buffers if they didn't exist.
if( !vertexBuffer )
{
auto vertexSize = data.VertexBuffer.GetType().get_sizeof();
auto result = rm.CreateVertexBuffer( vbName, data.VertexBuffer, (uint32)vertexSize );
ReturnIfInvalid( result );

vertexBuffer = result.Get();
}
else if( data.VertexBuffer.GetSize() > vertexBuffer->GetBytesSize() )
{
ReturnIfInvalid( vertexBuffer->Resize( data.VertexBuffer.AsRange() ) );
}
else
{
// Buffer is large enough for the new data.
// Note that we never resize buffer to smaller size. Maybe we should?
ReturnIfInvalid( vertexBuffer->UpdateData( data.VertexBuffer.AsRange(), 0 ) );
}

if( !indexBuffer )
{
auto indexSize = data.ExtendedIB ? sizeof( Index32 ) : sizeof( Index16 );
auto vertexSize = data.VertexBuffer.GetType().get_sizeof();
auto result = rm.CreateIndexBuffer( ibName, data.IndexBuffer, (uint32)indexSize );
ReturnIfInvalid( result );

vertexBuffer = rm.CreateVertexBuffer( vbName, data.VertexBuffer, (uint32)vertexSize ).Get(); /// @todo What in case of error?
indexBuffer = rm.CreateIndexBuffer( ibName, data.IndexBuffer, (uint32)indexSize ).Get(); /// @todo What in case of error?
indexBuffer = result.Get();
}
else if( data.IndexBuffer.GetSize() > indexBuffer->GetBytesSize() )
{
ReturnIfInvalid( indexBuffer->Resize( data.IndexBuffer.AsRange() ) );
}
else
{
// Buffer is large enough for the new data.
// Note that we never resize buffer to smaller size. Maybe we should?
ReturnIfInvalid( indexBuffer->UpdateData( data.IndexBuffer.AsRange(), 0 ) );
}

m_geometryData.VertexBuffer = vertexBuffer;
m_geometryData.IndexBuffer = indexBuffer;
Expand Down
2 changes: 1 addition & 1 deletion swGUI/Prototypes/TextShowcase/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void Application::AddControls ( HostWindow* host )
{
auto pen = std::make_shared< SolidColorBrush >( Colors::BurlyWood );
auto background = std::make_shared< SolidColorBrush >( Colors::Transparent );
auto textBlock = AddText( host, background, pen, 300, 100, Position( 350, 350 ), L"" );
auto textBlock = AddText( host, background, pen, 400, 100, Position( 350, 350 ), L"" );
textBlock->SetTextAlignment( sw::TextAlignment::Justify );
textBlock->SetFontSize( 60 );
textBlock->SetDataContext( &m_viewModel );
Expand Down
2 changes: 1 addition & 1 deletion swGraphicAPI/Resources/Buffers/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace sw

// ================================ //
//
Buffer::Buffer( const AssetPath& assetPath, unsigned int elementSize, unsigned int elementCount )
Buffer::Buffer( const AssetPath& assetPath, uint32 elementSize, uint32 elementCount )
: IBuffer( assetPath )
, m_elementSize( elementSize )
, m_elementCount( elementCount )
Expand Down
13 changes: 7 additions & 6 deletions swGraphicAPI/Resources/Buffers/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ class Buffer : public IBuffer
RTTR_ENABLE( IBuffer )
protected:

unsigned int m_elementSize; ///< Element size.
unsigned int m_elementCount; ///< Number of elements.
uint32 m_elementSize; ///< Element size.
uint32 m_elementCount; ///< Number of elements.

virtual ~Buffer () = default;
public:

explicit Buffer ( const AssetPath& assetPath, unsigned int elementSize, unsigned int elementCount );
explicit Buffer ( const AssetPath& assetPath, uint32 elementSize, uint32 elementCount );

inline unsigned int GetStride () const { return m_elementSize; }
inline unsigned int GetElementSize () const { return m_elementSize; }
inline unsigned int GetElementCount () const { return m_elementCount; }
inline uint32 GetStride () const { return m_elementSize; }
inline uint32 GetElementSize () const { return m_elementSize; }
inline uint32 GetElementCount () const { return m_elementCount; }
inline Size GetBytesSize () const { return m_elementCount * m_elementSize; }
};

DEFINE_RESOURCE_PTR_TYPE( Buffer );
Expand Down

0 comments on commit bdca9ee

Please sign in to comment.