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

Extending Font and Text to support multiple textures #2282

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

JonnyPtn
Copy link
Contributor

Rebase of #1645 as it seems GitHub has got it's knickers in a twist

@codecov
Copy link

codecov bot commented Nov 22, 2022

Codecov Report

Merging #2282 (7da2848) into master (8c6b578) will decrease coverage by 0.11%.
The diff coverage is 0.96%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2282      +/-   ##
==========================================
- Coverage   14.98%   14.87%   -0.12%     
==========================================
  Files         213      208       -5     
  Lines       15255    15285      +30     
  Branches     4072     4100      +28     
==========================================
- Hits         2286     2273      -13     
- Misses      12821    12879      +58     
+ Partials      148      133      -15     
Impacted Files Coverage Δ
include/SFML/Graphics/Font.hpp 0.00% <ø> (ø)
include/SFML/Graphics/Text.hpp 0.00% <ø> (ø)
src/SFML/Graphics/Font.cpp 0.00% <0.00%> (ø)
src/SFML/Graphics/Text.cpp 0.00% <0.00%> (ø)
include/SFML/Graphics/Glyph.hpp 75.00% <100.00%> (ø)
include/SFML/System/String.hpp 0.00% <0.00%> (-100.00%) ⬇️
include/SFML/System/Clock.hpp 50.00% <0.00%> (-50.00%) ⬇️
include/SFML/Graphics/Shape.hpp 0.00% <0.00%> (-50.00%) ⬇️
include/SFML/Network/Packet.hpp 0.00% <0.00%> (-50.00%) ⬇️
include/SFML/Graphics/Transformable.hpp 0.00% <0.00%> (-50.00%) ⬇️
... and 38 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8c6b578...7da2848. Read the comment docs.

@JonnyPtn
Copy link
Contributor Author

CI failure looks unrelated, think it just needs retrying

include/SFML/Graphics/Font.hpp Outdated Show resolved Hide resolved
include/SFML/Graphics/Font.hpp Outdated Show resolved Hide resolved
include/SFML/Graphics/Font.hpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Font.cpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Font.cpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Font.cpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Font.cpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Text.cpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Text.cpp Outdated Show resolved Hide resolved
@JonnyPtn
Copy link
Contributor Author

Great feedback @kimci86, thanks.

Think I've covered all the comments now

Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this! 👍

include/SFML/Graphics/Font.hpp Show resolved Hide resolved
src/SFML/Graphics/Font.cpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Font.cpp Show resolved Hide resolved
src/SFML/Graphics/Font.cpp Outdated Show resolved Hide resolved
Comment on lines 674 to 676
if (!page)
{
page = &*pages.insert(pages.end(), Page{m_isSmooth});
Copy link
Member

@Bromeon Bromeon Nov 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if (page == nullptr) for explicitness.

I have the feeling

pages.push_back(Page(m_isSmooth));
page = &pages.back();

might be clearer than this address-deref-insert-at-end expression.

Last, use constructor syntax Page(m_isSmooth).

Copy link
Member

@ChrisThrasher ChrisThrasher Nov 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why deref then take the address? Can't we just use the return value of std::list::emplace_back?

page = pages.emplace_back(m_isSmooth);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why deref then take the address? Can't we just use the return value of std::list::emplace_back?

page = pages.emplace_back(m_isSmooth);

I can't say why as I didn't originally write this, but using emplace_back seems like a good shout, will do

Maybe if (page == nullptr) for explicitness.

I don't think comparing to nullptr makes it more explicit, if anything it makes it less intuitive (we're checking if there's not a page, which is exactly how this code reads)

Last, use constructor syntax Page(m_isSmooth).

why?

src/SFML/Graphics/Font.cpp Outdated Show resolved Hide resolved
glyph.textureRect = findGlyphRect(page, {width, height});
// Find a page that can fit well the glyph
Page* page = NULL;
for (PageList::iterator it = pages.begin(); it != pages.end() && !page; ++it)
Copy link
Member

@Bromeon Bromeon Nov 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&& page == nullptr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, I don't consider this an improvement

Comment on lines 666 to 671
// Try to find a good position for the new glyph into the texture
glyph.textureRect = findGlyphRect(*it, {width, height});

if (glyph.textureRect.width > 0)
page = &*it;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assigns to glyph.textureRect even if no good rect is found, and leaves this assigned beyond the for loop. I see that you have another glyph.textureRect = findGlyphRect(*page, {width, height}); further down, so it's correct.

However, this flow is very prone to errors on refactorings. The function is also getting rather big, and variable mutations are mixed with "found" state. A separate function could make things easier:

struct MatchingPage {
    Page* page = nullptr;
    IntRect textureRect;
};

std::optional<MatchingPage> findMatchingPage(...);
// usage:
if (auto matching = findMatchingPage(...)) {
    page = matching->page;
    glyph.textureRect = matching->textureRect;
    ...
} else {
    ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think this would over-engineer it for little gain - the behaviour is only required in one place so I would prefer to use a lambda limited to this scope, but then I don't think that would really address any of your concerns.

Would like to hear other opinions on this though

src/SFML/Graphics/Text.cpp Outdated Show resolved Hide resolved
@JonnyPtn JonnyPtn requested review from Bromeon and ChrisThrasher and removed request for ChrisThrasher and Bromeon November 28, 2022 08:53
@Bromeon
Copy link
Member

Bromeon commented Nov 28, 2022

AFAIK there's no convention in SFML regarding pointer nullability checks.
To get a quick impression what people prefer, maybe react to this message...

with 🎉 for this style:

if (ptr)
if (!ptr)

and 👀 for this style:

if (ptr != nullptr)
if (ptr == nullptr)

I'm fine with either, but would be good to settle. I'll add 1 reaction each.

Similar topic: if (integer == 0), here I would advocate always explicit checks.

@SFML SFML deleted a comment from ChrisThrasher Dec 5, 2022
Copy link
Member

@ChrisThrasher ChrisThrasher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know enough about textures, fonts, and text to review the core logic in depth but I did try to comment on the parts that I do understand. This looks promising though!

include/SFML/Graphics/Font.hpp Show resolved Hide resolved
include/SFML/Graphics/Font.hpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Font.cpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Font.cpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Font.cpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Text.cpp Outdated Show resolved Hide resolved
src/SFML/Graphics/Text.cpp Outdated Show resolved Hide resolved
@JonnyPtn
Copy link
Contributor Author

What else are we waiting for on this?

@Bromeon
Copy link
Member

Bromeon commented Apr 3, 2023

@SFML/sfml Any more comments on this one? We should decide if we want to move ahead with this PR or not, as merge conflicts start accumulating.

@ChrisThrasher
Copy link
Member

@SFML/sfml Any more comments on this one? We should decide if we want to move ahead with this PR or not, as merge conflicts start accumulating.

I defer to the judgment of @binary1248 or anyone else who understands OpenGL since this is outside of my expertise. It seems like a good idea from what I can tell though.

@binary1248
Copy link
Member

My only concern in #1645 was that performance might take a hit, but that can be optimized after this gets merged. It would still be interesting to hear from text heavy users if/how this commit affects their performance.

@eXpl0it3r eXpl0it3r removed this from the 3.0 milestone Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Planned
Development

Successfully merging this pull request may close these issues.

7 participants