-
Notifications
You must be signed in to change notification settings - Fork 69
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
blit::random() cannot be used in global initializers (except on device) #484
Comments
Yeah, I've hit this one before, not sure there's actually a way to fix this... The only reason this works on device is because the API is initialised before the game even exists. (API is setup by the firmware. which then starts loading the game, for SDL the constructors have already happened before we can do anything.) |
I’m not totally averse to quietly “ifdef”ing this issue under the rug. I don’t think there’s any other way it could be fixed? The whole concept of the API is a little contrived for SDL, where there’s no real separation between the “HAL” and game. It feels like it should not be intrinsic to the “engine” but constitutes something that’s really neither “engine” nor “HAL”. Maybe these a fundamental architectural issue behind this bug? |
Hmm, an idea: add a API_FUNC macro //SDL
#define API_FUNC(name) name
//STM32
#define API_FUNC(name) (*name) Then all the API functions could be implemented for SDL as uint32_t API::random()
{
...
} Going even further, they could all be regular function declarations, implemented by either 32blit-sdl32, or a new 32blit-stm32-user library (that just forwards trough the API section). Though that would end up with some stuff having multiple levels of wrapper... (I think these were function pointers even before the API stuff, so it's always had this issue... It just got accidentally fixed on device by the user/firmware split) |
Hmm, first idea is not so bad master...Daft-Freak:sdl-no-ptrs (needs some indent/namespace tidying) I had an attempt at going further which... did not go so well. |
Found out that this also applies to things like "screen.bounds.w" which are all too easy to assume would "just work" if you use them inside - for example- a class constructor. However - Since we can't know the screen resolution ahead of time (it's potentially changed in |
(Although, since there's no guarantee on the order of global constructors across files, you should really avoid using anything you didn't declare in them...) |
Hmm, thinking about the original issue more... The SDL version of |
In this case should we document a "don't do this" and suggest people initialize things they want to be random at runtime? I don't think it's super wrong to accept and document compromises. |
If I want to store a random number that's globally initialized, I can't do that with
blit::random()
untilinit()
is called. This works on a 32blit device though, which I think is becauseapi
is initialized differently there.This can be worked around by removing the
const
modifier and assigning ininit()
. However, it becomes more complicated whenblit::random()
is used in other parts of the code:Right now my interim solution is to use a pointer like so, and initializing
Flower
later ininit()
:The text was updated successfully, but these errors were encountered: