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

Musings and whining about SDL_Log floating point support #132

Open
Markvy opened this issue Sep 18, 2021 · 0 comments
Open

Musings and whining about SDL_Log floating point support #132

Markvy opened this issue Sep 18, 2021 · 0 comments

Comments

@Markvy
Copy link

Markvy commented Sep 18, 2021

I'm new to SDL. I saw a neat function called SDL_Log, which seemed to do exactly what I wanted.
Until one one fine day I tried SDL_Log("%g", 1.0") and got a blank line, and was very confused.
After some time spent trying to see whether anyone on the internet had the same issue, I eventually gave up and asked this question on stack overflow, which was answered surprisingly quickly:
https://stackoverflow.com/questions/69216803/sdl-log-doesnt-seem-to-support-g-and-e-specifiers
As a result of this mini-(mis)adventure, I think I can offer several suggestions that would have made using SDL easier.
The suggestions are in two categories, code and docs. Let's start with code, because I have fewer things to say there:

First, perhaps it's worth defining HAVE_VSNPRINTF for sufficiently recent versions of VC++ (maybe 2015 or so?)

Second, perhaps it's worth improving SDL's homegrown vsnprintf so it vaguely conforms to the ISO standard, or at least supports the most common flags. (E.g., it supports %f, but not the uppercase variant %F, and it supports neither %e nor %g, which must be pretty common.)

Okay, that's it for code suggestions. I also have some docs suggestions. I give some specific wording here, but my wording is mostly a strawman proposal; the info is mostly right I think (or hope).
First, for SDL_Log, the wiki page should mention something like

This function delegates to SDL_vsnprintf in order to interpret the format string. See the documentation for that function for more info.

Actually it looks like the SDL wiki doesn't yet document its stdio "wrappers" so that should be fixed too I guess. Once that's done, the documentation for SDL_vsnprintf should probably mostly be a stub that delegates to SDL_snprintf, since the "v" variant is mostly for advanced users. Alternatively, you could do it the other way around, and have the docs for SDL_snprintf delegate to those of SDL_vsnprintf, which is more faithful to how the code itself is structured. Or you could have both functions documented independently; duplication is annoying but probably in this case mostly harmless.

For the sake of concreteness, let's suppose we're documenting SDL_vsnprintf. Then its wiki page should mention something like this:

This function either delegates to the vsnprintf provided by your standard library (if SDL thinks your standard library has one that SDL wants to use) , or else uses a version of vsnprintf provided by SDL itself. To determine if your platform has a vsnprintf that SDL knows about and likes/trusts, simply check whether the macro HAVE_VSNPRINTF is defined, eg, by using #ifdef or by seeing whether your IDE auto-completes the name for you. There is usually no need to care about how SDL_vsnprintf is implemented, but here are some reasons why you might care after all:

  1. Performance (SDL's version might be faster or slower than other implementations of vsnprintf)
  2. Reproducible output for floating point numbers. E.g., should printf (“%.1f\n”,0.25) be 0.2 or 0.3? Different implementations of printf don't always agree on such corner cases.
  3. Feature support: some standard libraries support non-standard extensions for snprintf (e.g., extra % specifiers), or support features that only became standardized in relatively recent versions of C. (See the documentation for your standard library to see what yours supports.) On the other hand, the one provided by SDL is pretty bare bones. In fact, up until version ABC, it only supported the type specifiers %i and %c; below is a table of all the specifiers as of version CURRENT; the second column is the version of SDL that first supported the given specifier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant