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

[rtextures] Fix LoadImageFromScreen scaling #3881

Merged
merged 1 commit into from
Mar 26, 2024

Conversation

proberge-dev
Copy link
Contributor

Before this PR, LoadImageFromScreen was not applying the scale DPI to both width and height. After this PR, the behaviour of LoadImageFromScreen is the same as TakeScreenshot, but returns an image instead. Not sure if this is the appropriate fix, but this was what I was expecting when calling the function. If there needs to be adjustments such as calling ImageResize to get the current width and height instead of the scaled one, let me know.

@raysan5
Copy link
Owner

raysan5 commented Mar 22, 2024

@proberge-dev did you try it? does it work as expected on a HiDPI monitor?

@proberge-dev
Copy link
Contributor Author

Yes, I've tried it on a Macbook Pro. It works as far as I can see. This code is functionally equivalent to the code in TakeScreenshot from rcore.c:

void TakeScreenshot(const char *fileName)
{
#if defined(SUPPORT_MODULE_RTEXTURES)
    // Security check to (partially) avoid malicious code
    if (strchr(fileName, '\'') != NULL) { TRACELOG(LOG_WARNING, "SYSTEM: Provided fileName could be potentially malicious, avoid [\'] character"); return; }

    Vector2 scale = GetWindowScaleDPI();
    unsigned char *imgData = rlReadScreenPixels((int)((float)CORE.Window.render.width*scale.x), (int)((float)CORE.Window.render.height*scale.y));
    Image image = { imgData, (int)((float)CORE.Window.render.width*scale.x), (int)((float)CORE.Window.render.height*scale.y), 1, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 };

    char path[512] = { 0 };
    strcpy(path, TextFormat("%s/%s", CORE.Storage.basePath, GetFileName(fileName)));
    
    ExportImage(image, path);           // WARNING: Module required: rtextures
    RL_FREE(imgData);

    if (FileExists(path)) TRACELOG(LOG_INFO, "SYSTEM: [%s] Screenshot taken successfully", path);
    else TRACELOG(LOG_WARNING, "SYSTEM: [%s] Screenshot could not be saved", path);
#else
    TRACELOG(LOG_WARNING,"IMAGE: ExportImage() requires module: rtextures");
#endif
}

@raysan5 raysan5 merged commit b905daf into raysan5:master Mar 26, 2024
@raysan5
Copy link
Owner

raysan5 commented Mar 26, 2024

@proberge-dev ok, thanks for the review

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.

2 participants