Skip to content

Add sfBuffer wrapper and implement sf::Image::saveToMemory#205

Merged
eXpl0it3r merged 2 commits intoSFML:masterfrom
kimci86:feature/image_save_to_memory
Sep 9, 2023
Merged

Add sfBuffer wrapper and implement sf::Image::saveToMemory#205
eXpl0it3r merged 2 commits intoSFML:masterfrom
kimci86:feature/image_save_to_memory

Conversation

@kimci86
Copy link
Copy Markdown
Contributor

@kimci86 kimci86 commented Sep 7, 2023

Alternative to #195 using a C wrapper over an std::vector.

Copy link
Copy Markdown
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 appreciate you putting this together with all the right docs and everything.

@kimci86 kimci86 changed the title Add sfBuffer wrapper and implement sf::Image::saveToMemory Add sfBuffer wrapper and implement sf::Image::saveToMemory Sep 7, 2023
@kimci86 kimci86 force-pushed the feature/image_save_to_memory branch from 11c69d7 to 855e1de Compare September 7, 2023 20:25
@kimci86 kimci86 force-pushed the feature/image_save_to_memory branch from 76afd71 to 8e0741b Compare September 7, 2023 21:07
@kimci86 kimci86 requested a review from eXpl0it3r September 7, 2023 21:13
@kimci86 kimci86 force-pushed the feature/image_save_to_memory branch from 8e0741b to a529368 Compare September 7, 2023 21:27
@kimci86
Copy link
Copy Markdown
Contributor Author

kimci86 commented Sep 7, 2023

I used the following code to test the API.
It made me notice the #include <SFML/Graphics/Buffer.h> was missing from SFML/Graphics.h file and fixed it.

#include <SFML/Graphics.h>
#include <stdio.h>

int main(void)
{
    const int WIDTH = 256;
    sfImage* image = sfImage_createFromColor(WIDTH, 1, sfMagenta);
    for (int x = 0; x < WIDTH; ++x)
        sfImage_setPixel(image, x, 0, sfColor_fromRGB(x, 255, x));

    sfBuffer* buffer = sfBuffer_create();
    sfBool success = sfImage_saveToMemory(image, buffer, "png");
    printf("Succes: %d\n", success);

    sfImage_destroy(image);

    FILE* f = fopen("out.png", "wb");
    fwrite(sfBuffer_getData(buffer), 1, sfBuffer_getSize(buffer), f);
    fclose(f);

    sfBuffer_destroy(buffer);

    return 0;
}

@eXpl0it3r
Copy link
Copy Markdown
Member

@oprypin any comment from your side? 🙂

@oprypin
Copy link
Copy Markdown
Member

oprypin commented Sep 7, 2023

It's good.
One thing is: putting sfBuffer into graphics module is strange because then we can't add a sfBuffer into system or window if we need it later. But it may be also strange to put it into system right away.

@eXpl0it3r
Copy link
Copy Markdown
Member

Hmmm I put it off as "we can always move it later", but then we'd be breaking code. While I don't really hold myself to the same non-breaking standard for CSFML as for SFML, I think it might after all be beneficial to reduce it where possible.

So I think, it does after all matter and we should move sfBuffer to the system module. So it could be reused e.g. in the audio module in the future, without "breaking" the API.

@ChrisThrasher
Copy link
Copy Markdown
Member

There are no other saveToMemory functions in SFML and if we added more they'd be added in SFML 3 and thus CSFML 3 so we can get away with leaving it where it is if we want. I'm fine moving it to System though. Either is fine with me.

@oprypin
Copy link
Copy Markdown
Member

oprypin commented Sep 7, 2023

Hm but wait, if sfBuffer is first in graphics but then is moved to system, can't it just be re-exposed in graphics and then not break anything?

@oprypin
Copy link
Copy Markdown
Member

oprypin commented Sep 7, 2023

But anyway probably system is better. Sorry to cause you work 😅

@eXpl0it3r
Copy link
Copy Markdown
Member

Hm but wait, if sfBuffer is first in graphics but then is moved to system, can't it just be re-exposed in graphics and then not break anything?

It could, but I wouldn't want such a workaround, just to avoid some potential breaking code. No need to add technical debt.

@kimci86 kimci86 marked this pull request as draft September 8, 2023 06:52
@kimci86 kimci86 marked this pull request as ready for review September 9, 2023 15:19
@kimci86 kimci86 force-pushed the feature/image_save_to_memory branch from f759600 to 0a6b875 Compare September 9, 2023 15:22
@kimci86
Copy link
Copy Markdown
Contributor Author

kimci86 commented Sep 9, 2023

I moved sfBuffer into csfml-system.

Copy link
Copy Markdown
Member

@eXpl0it3r eXpl0it3r left a comment

Choose a reason for hiding this comment

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

Thank you ❤️

@eXpl0it3r eXpl0it3r merged commit 725811b into SFML:master Sep 9, 2023
@kimci86 kimci86 deleted the feature/image_save_to_memory branch September 9, 2023 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants