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

[rcore] InitWindow() should check InitPlatform() returned value #4164

Conversation

SuperUserNameMan
Copy link
Contributor

@SuperUserNameMan SuperUserNameMan commented Jul 15, 2024

In case PlatformInit() fails, InitWindow() should quit properly and set CORE.Window.ready = false.

Also fix rcore_template.c which returned false instead of -1, as false might be defined to 0 in raylib.h.

Note that if the user forget to check IsWindowReady() before continuing, the program might still crash in a segfault if InitPlatform() failed.

@raysan5 : could we change void InitWindow() to bool InitWindow() instead of hoping the user think about checking IsWindowReady() in case of InitPlatform() failure ?

src/rcore.c Outdated
@@ -631,7 +631,12 @@ void InitWindow(int width, int height, const char *title)

// Initialize platform
//--------------------------------------------------------------
InitPlatform();
if ( InitPlatform() != 0 )
Copy link
Owner

Choose a reason for hiding this comment

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

I try to avoid this kind of structure on raylib, also, not following conventions, raylib does not place spaces between parenthesis. In any case it should be:

int result = InitPlatform();
if (result == -1)  // Platform initialization failed
{
    TRACELOG(LOG_WARNING, "WINDOW: Platform initialization failed");
    //CORE.Window.ready = false;  // If platform has not been correctly initialized, this should be already "false"
    return;  // In araylib I try to avoid/minimize early returns
}

@@ -476,15 +476,15 @@ int InitPlatform(void)
if (platform.device == EGL_NO_DISPLAY)
{
TRACELOG(LOG_WARNING, "DISPLAY: Failed to initialize EGL device");
return false;
return -1;
Copy link
Owner

Choose a reason for hiding this comment

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

Actually, afaik, there is no clear convention defined on the function return values when used for errors detection... I tried to avoid that approach for raylib but I can consider it...

@raysan5
Copy link
Owner

raysan5 commented Jul 16, 2024

@SuperUserNameMan There is no clear convention on the function return values when used for errors detection... Actually, I tried to avoid that approach for most raylib functions (depite other libraries like SDL use it). I can consider it but it will require analyzing afected functions and conventions more carefully. For example, for InitWindow(), I like the void return approach for simplicity, maybe error codes could be queried a-part (like OpenGL does)... or, in case, InitWindow() returned an int what other uses it could have, other than a pure error message, i.e. for future multi-window id support.

All those questions require a review and redesign of the raylib library conventions and I prefer to consider them carefully.

SuperUserNameMan referenced this pull request in SuperUserNameMan/raylib Jul 23, 2024
I did not want to touch `rcore.c`, but it was required for cross-paltform consistency, so that `SetMouseScale()` and `SetMouseOffset()` have same effect on every platform, and don't interfere with mouse offset and scaling required and preset by a platform which should, instead, use the new `CORE.Input.Mouse.platformOffset` and `CORE.Input.Mouse.platformScale`.
@SuperUserNameMan SuperUserNameMan deleted the 2024-07-15-InitWindow-check-InitPlatform-return branch July 26, 2024 13:40
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

Successfully merging this pull request may close these issues.

None yet

2 participants