Skip to content

Commit

Permalink
Reimplement sfTexture
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Sep 24, 2024
1 parent 7b583b1 commit 80c362c
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/CSFML/Graphics/CircleShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ sfTransform sfCircleShape_getInverseTransform(const sfCircleShape* shape)
void sfCircleShape_setTexture(sfCircleShape* shape, const sfTexture* texture, bool resetRect)
{
assert(shape);
shape->setTexture(texture ? texture->This : nullptr, resetRect);
shape->setTexture(texture, resetRect);
shape->Texture = texture;
}

Expand Down
2 changes: 1 addition & 1 deletion src/CSFML/Graphics/ConvertRenderStates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
renderStates.blendMode.alphaDstFactor = static_cast<sf::BlendMode::Factor>(states->blendMode.alphaDstFactor);
renderStates.blendMode.alphaEquation = static_cast<sf::BlendMode::Equation>(states->blendMode.alphaEquation);
renderStates.transform = convertTransform(states->transform);
renderStates.texture = states->texture ? states->texture->This : nullptr;
renderStates.texture = states->texture;
renderStates.shader = states->shader;
return renderStates;
}
2 changes: 1 addition & 1 deletion src/CSFML/Graphics/ConvexShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ sfTransform sfConvexShape_getInverseTransform(const sfConvexShape* shape)
void sfConvexShape_setTexture(sfConvexShape* shape, const sfTexture* texture, bool resetRect)
{
assert(shape);
shape->setTexture(texture ? texture->This : nullptr, resetRect);
shape->setTexture(texture, resetRect);
shape->Texture = texture;
}

Expand Down
2 changes: 1 addition & 1 deletion src/CSFML/Graphics/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const sfTexture* sfFont_getTexture(sfFont* font, unsigned int characterSize)
{
assert(font);

*font->Textures[characterSize].This = font->getTexture(characterSize);
font->Textures[characterSize] = sfTexture{font->getTexture(characterSize)};

return &font->Textures[characterSize];
}
Expand Down
2 changes: 1 addition & 1 deletion src/CSFML/Graphics/RectangleShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ sfTransform sfRectangleShape_getInverseTransform(const sfRectangleShape* shape)
void sfRectangleShape_setTexture(sfRectangleShape* shape, const sfTexture* texture, bool resetRect)
{
assert(shape);
shape->setTexture(texture ? texture->This : nullptr, resetRect);
shape->setTexture(texture, resetRect);
shape->Texture = texture;
}

Expand Down
4 changes: 2 additions & 2 deletions src/CSFML/Graphics/RenderTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ sfRenderTexture* sfRenderTexture_create(sfVector2u size, const sfContextSettings
if (!renderTexture->resize(convertVector2(size), params))
return nullptr;

renderTexture->Target = std::make_unique<sfTexture>(const_cast<sf::Texture*>(&renderTexture->getTexture()));
renderTexture->Target = sfTexture{renderTexture->getTexture()};
renderTexture->DefaultView = sfView{renderTexture->getDefaultView()};
renderTexture->CurrentView = sfView{renderTexture->getView()};

Expand Down Expand Up @@ -300,7 +300,7 @@ void sfRenderTexture_resetGLStates(sfRenderTexture* renderTexture)
const sfTexture* sfRenderTexture_getTexture(const sfRenderTexture* renderTexture)
{
assert(renderTexture);
return renderTexture->Target.get();
return &renderTexture->Target;
}


Expand Down
8 changes: 3 additions & 5 deletions src/CSFML/Graphics/RenderTextureStruct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@

#include <SFML/Graphics/RenderTexture.hpp>

#include <memory>


////////////////////////////////////////////////////////////
// Internal structure of sfRenderTexture
////////////////////////////////////////////////////////////
struct sfRenderTexture : sf::RenderTexture
{
std::unique_ptr<const sfTexture> Target;
sfView DefaultView;
sfView CurrentView;
sfTexture Target;
sfView DefaultView;
sfView CurrentView;
};
5 changes: 3 additions & 2 deletions src/CSFML/Graphics/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ void sfShader_setTextureUniform(sfShader* shader, const char* name, const sfText
{
assert(shader);
assert(name);
shader->setUniform(name, *texture->This);
assert(texture);
shader->setUniform(name, *texture);
}


Expand Down Expand Up @@ -419,7 +420,7 @@ void sfShader_setTextureParameter(sfShader* shader, const char* name, const sfTe
assert(shader);
assert(name);
assert(texture);
shader->setUniform(name, *texture->This);
shader->setUniform(name, *texture);
}


Expand Down
2 changes: 1 addition & 1 deletion src/CSFML/Graphics/Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ sfTransform sfShape_getInverseTransform(const sfShape* shape)
void sfShape_setTexture(sfShape* shape, const sfTexture* texture, bool resetRect)
{
assert(shape);
shape->setTexture(texture ? texture->This : nullptr, resetRect);
shape->setTexture(texture, resetRect);
shape->Texture = texture;
}

Expand Down
7 changes: 3 additions & 4 deletions src/CSFML/Graphics/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
sfSprite* sfSprite_create(const sfTexture* texture)
{
assert(texture);
assert(texture->This);
auto sprite = new sfSprite(*texture->This);
auto sprite = new sfSprite(*texture);
sprite->Texture = texture;
return sprite;
}
Expand Down Expand Up @@ -171,10 +170,10 @@ sfTransform sfSprite_getInverseTransform(const sfSprite* sprite)
////////////////////////////////////////////////////////////
void sfSprite_setTexture(sfSprite* sprite, const sfTexture* texture, bool resetRect)
{
if (texture && texture->This)
if (texture)
{
assert(sprite);
sprite->setTexture(*texture->This, resetRect);
sprite->setTexture(*texture, resetRect);
sprite->Texture = texture;
}
}
Expand Down
56 changes: 26 additions & 30 deletions src/CSFML/Graphics/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
sfTexture* sfTexture_create(sfVector2u size)
{
auto texture = std::make_unique<sfTexture>();
if (!texture->This->resize(convertVector2(size)))
if (!texture->resize(convertVector2(size)))
return nullptr;

return texture.release();
Expand All @@ -52,7 +52,7 @@ sfTexture* sfTexture_create(sfVector2u size)
sfTexture* sfTexture_createSrgb(sfVector2u size)
{
auto texture = std::make_unique<sfTexture>();
if (!texture->This->resize(convertVector2(size), true))
if (!texture->resize(convertVector2(size), true))
return nullptr;

return texture.release();
Expand All @@ -65,7 +65,7 @@ sfTexture* sfTexture_createFromFile(const char* filename, const sfIntRect* area)
assert(filename);

auto texture = std::make_unique<sfTexture>();
if (!texture->This->loadFromFile(filename, false, area ? convertRect(*area) : sf::IntRect()))
if (!texture->loadFromFile(filename, false, area ? convertRect(*area) : sf::IntRect()))
return nullptr;

return texture.release();
Expand All @@ -77,7 +77,7 @@ sfTexture* sfTexture_createSrgbFromFile(const char* filename, const sfIntRect* a
assert(filename);

auto texture = std::make_unique<sfTexture>();
if (!texture->This->loadFromFile(filename, true, area ? convertRect(*area) : sf::IntRect()))
if (!texture->loadFromFile(filename, true, area ? convertRect(*area) : sf::IntRect()))
return nullptr;

return texture.release();
Expand All @@ -87,7 +87,7 @@ sfTexture* sfTexture_createSrgbFromFile(const char* filename, const sfIntRect* a
sfTexture* sfTexture_createFromMemory(const void* data, size_t sizeInBytes, const sfIntRect* area)
{
auto texture = std::make_unique<sfTexture>();
if (!texture->This->loadFromMemory(data, sizeInBytes, false, area ? convertRect(*area) : sf::IntRect()))
if (!texture->loadFromMemory(data, sizeInBytes, false, area ? convertRect(*area) : sf::IntRect()))
return nullptr;

return texture.release();
Expand All @@ -97,7 +97,7 @@ sfTexture* sfTexture_createFromMemory(const void* data, size_t sizeInBytes, cons
sfTexture* sfTexture_createSrgbFromMemory(const void* data, size_t sizeInBytes, const sfIntRect* area)
{
auto texture = std::make_unique<sfTexture>();
if (!texture->This->loadFromMemory(data, sizeInBytes, true, area ? convertRect(*area) : sf::IntRect()))
if (!texture->loadFromMemory(data, sizeInBytes, true, area ? convertRect(*area) : sf::IntRect()))
return nullptr;

return texture.release();
Expand All @@ -111,7 +111,7 @@ sfTexture* sfTexture_createFromStream(sfInputStream* stream, const sfIntRect* ar

CallbackStream sfmlStream(stream);
auto texture = std::make_unique<sfTexture>();
if (!texture->This->loadFromStream(sfmlStream, false, area ? convertRect(*area) : sf::IntRect()))
if (!texture->loadFromStream(sfmlStream, false, area ? convertRect(*area) : sf::IntRect()))
return nullptr;

return texture.release();
Expand All @@ -124,7 +124,7 @@ sfTexture* sfTexture_createSrgbFromStream(sfInputStream* stream, const sfIntRect

CallbackStream sfmlStream(stream);
auto texture = std::make_unique<sfTexture>();
if (!texture->This->loadFromStream(sfmlStream, true, area ? convertRect(*area) : sf::IntRect()))
if (!texture->loadFromStream(sfmlStream, true, area ? convertRect(*area) : sf::IntRect()))
return nullptr;

return texture.release();
Expand All @@ -137,7 +137,7 @@ sfTexture* sfTexture_createFromImage(const sfImage* image, const sfIntRect* area
assert(image);

auto texture = std::make_unique<sfTexture>();
if (!texture->This->loadFromImage(*image, false, area ? convertRect(*area) : sf::IntRect()))
if (!texture->loadFromImage(*image, false, area ? convertRect(*area) : sf::IntRect()))
return nullptr;

return texture.release();
Expand All @@ -149,7 +149,7 @@ sfTexture* sfTexture_createSrgbFromImage(const sfImage* image, const sfIntRect*
assert(image);

auto texture = std::make_unique<sfTexture>();
if (!texture->This->loadFromImage(*image, true, area ? convertRect(*area) : sf::IntRect()))
if (!texture->loadFromImage(*image, true, area ? convertRect(*area) : sf::IntRect()))
return nullptr;

return texture.release();
Expand All @@ -174,24 +174,23 @@ void sfTexture_destroy(const sfTexture* texture)
sfVector2u sfTexture_getSize(const sfTexture* texture)
{
assert(texture);
return convertVector2(texture->This->getSize());
return convertVector2(texture->getSize());
}


////////////////////////////////////////////////////////////
sfImage* sfTexture_copyToImage(const sfTexture* texture)
{
assert(texture);
assert(texture->This);
return new sfImage{texture->This->copyToImage()};
return new sfImage{texture->copyToImage()};
}


////////////////////////////////////////////////////////////
void sfTexture_updateFromPixels(sfTexture* texture, const uint8_t* pixels, sfVector2u size, sfVector2u offset)
{
assert(texture);
texture->This->update(pixels, convertVector2(size), convertVector2(offset));
texture->update(pixels, convertVector2(size), convertVector2(offset));
}


Expand All @@ -200,8 +199,7 @@ void sfTexture_updateFromTexture(sfTexture* destination, const sfTexture* textur
{
assert(destination);
assert(texture);
assert(texture->This);
destination->This->update(*texture->This, convertVector2(offset));
destination->update(*texture, convertVector2(offset));
}


Expand All @@ -210,7 +208,7 @@ void sfTexture_updateFromImage(sfTexture* texture, const sfImage* image, sfVecto
{
assert(texture);
assert(image);
texture->This->update(*image, convertVector2(offset));
texture->update(*image, convertVector2(offset));
}


Expand All @@ -219,7 +217,7 @@ void sfTexture_updateFromWindow(sfTexture* texture, const sfWindow* window, sfVe
{
assert(texture);
assert(window);
texture->This->update(*window, convertVector2(offset));
texture->update(*window, convertVector2(offset));
}


Expand All @@ -228,56 +226,54 @@ void sfTexture_updateFromRenderWindow(sfTexture* texture, const sfRenderWindow*
{
assert(texture);
assert(renderWindow);
texture->This->update(*renderWindow, convertVector2(offset));
texture->update(*renderWindow, convertVector2(offset));
}


////////////////////////////////////////////////////////////
void sfTexture_setSmooth(sfTexture* texture, bool smooth)
{
assert(texture);
texture->This->setSmooth(smooth);
texture->setSmooth(smooth);
}


////////////////////////////////////////////////////////////
bool sfTexture_isSmooth(const sfTexture* texture)
{
assert(texture);
assert(texture->This);
return texture->This->isSmooth();
return texture->isSmooth();
}

////////////////////////////////////////////////////////////
bool sfTexture_isSrgb(const sfTexture* texture)
{
assert(texture);
return texture->This->isSrgb();
return texture->isSrgb();
}


////////////////////////////////////////////////////////////
void sfTexture_setRepeated(sfTexture* texture, bool repeated)
{
assert(texture);
texture->This->setRepeated(repeated);
texture->setRepeated(repeated);
}


////////////////////////////////////////////////////////////
bool sfTexture_isRepeated(const sfTexture* texture)
{
assert(texture);
assert(texture->This);
return texture->This->isRepeated();
return texture->isRepeated();
}


////////////////////////////////////////////////////////////
bool sfTexture_generateMipmap(sfTexture* texture)
{
assert(texture);
return texture->This->generateMipmap();
return texture->generateMipmap();
}


Expand All @@ -286,22 +282,22 @@ void sfTexture_swap(sfTexture* left, sfTexture* right)
{
assert(left);
assert(right);
left->This->swap(*right->This);
left->swap(*right);
}


////////////////////////////////////////////////////////////
unsigned int sfTexture_getNativeHandle(const sfTexture* texture)
{
assert(texture);
return texture->This->getNativeHandle();
return texture->getNativeHandle();
}


////////////////////////////////////////////////////////////
void sfTexture_bind(const sfTexture* texture, sfTextureCoordinateType type)
{
sf::Texture::bind(texture ? texture->This : nullptr, static_cast<sf::CoordinateType>(type));
sf::Texture::bind(texture, static_cast<sf::CoordinateType>(type));
}


Expand Down
20 changes: 1 addition & 19 deletions src/CSFML/Graphics/TextureStruct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,6 @@
////////////////////////////////////////////////////////////
// Internal structure of sfTexture
////////////////////////////////////////////////////////////
struct sfTexture
struct sfTexture : sf::Texture
{
sfTexture() = default;

sfTexture(sf::Texture* texture) : This(texture), OwnInstance(false)
{
}

sfTexture(const sfTexture& texture) : This(texture.This ? new sf::Texture(*texture.This) : nullptr)
{
}

~sfTexture()
{
if (OwnInstance)
delete This;
}

sf::Texture* This{new sf::Texture};
bool OwnInstance{true};
};

0 comments on commit 80c362c

Please sign in to comment.