Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: add API to check if a texture is equal using the internal id #1816

Open
dgcor opened this issue Aug 12, 2021 · 2 comments
Open

Comments

@dgcor
Copy link

dgcor commented Aug 12, 2021

Some SFML classes use the m_cacheId to optimize the code. Because this is private, it can't be used from the outside.

One trick is to use the same name for a class (sf::Text) so you can friend sf::Texture and use this member internally. The downside is you get problems if someone or some dependency includes the original sf::Text (class redefinition).

My suggestion is to add some way to verify if a texture changed since its last use.

void Text::ensureGeometryUpdate() const
{
	if (!m_font)
	{
		return;
	}

	// Do nothing, if geometry has not changed and the font texture has not changed
	if (!m_geometryNeedUpdate && m_font->getTexture(m_characterSize).m_cacheId == m_fontTextureId)
	{
		return;
	}

related forum posts:
https://en.sfml-dev.org/forums/index.php?topic=14560.0

@eXpl0it3r
Copy link
Member

Can you provide some concrete use cases for exposing Cache ID?

@dgcor
Copy link
Author

dgcor commented Aug 16, 2021

I have a custom Text2 class which is a copy/paste of sf::text but with 2 extra styles for left and right text alignment:

https://github.com/dgcor/DGEngine.core/blob/master/src/SFML/Text2.cpp
https://github.com/dgcor/DGEngine.core/blob/master/src/SFML/Text2.h

enum Style
{
	Regular = 0,
	Bold = 1 << 0,
	Italic = 1 << 1,
	Underlined = 1 << 2,
	StrikeThrough = 1 << 3,
	HorizontalAlignCenter = 1 << 4,
	HorizontalAlignRight = 1 << 5
};

I can't compile it with Clang because of the existing sf::text conflict. msvc and gcc compile ok. I can remove the cache check and rename it to sf::Text2 and then it works on all compilers, but it will always be slower than the original implementation.

I only use it to draw this wall of text:

d2credits

Another reason is that I'm migrating to C++20 modules and I can't have this trick of using the existing name to get the friend access to m_cacheId as easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants