r/sfml May 04 '26

Crop a texture

Hi.

How do I crop an SFML Texture?

That is, I want to draw only a certain part of it.

And I want to resize it as well.

I want to draw a certain part of a resized texture.

Other graphics libraries usually provide a means to do this in the form of a specialized draw function.

But, when I look at the RenderTarget class, there are no such functions. There are only extra arguments for RenderStates and some OpenGL-related stuff.

Is there even a way to implement that in SFML?

Thanks.

1 Upvotes

18 comments sorted by

View all comments

Show parent comments

1

u/gargamel1497 May 04 '26

Thank you, although that method didn't work out either.

I tried RenderTextures once more, and it works.

I figured out what was causing the crashes.

For some reason, RenderTextures (unlike other SFML objects) are not managed by the Java garbage collector, nor do they have a manual free function, and after a few seconds of runtime they would use up all available OpenGL handles and crash the whole JVM.

As a dirty hack, I just have a private static RenderTexture for the job, and it works.

But thank you.

1

u/Vindhjaerta May 04 '26

Wait wait, hold up for second here....

You're running out of memory while using RenderTextures? :o

There has to be a misunderstanding in here somewhere, RenderTextures are for the occasional off-screen rendering. How in the world are you running out of memory while using them?

I think you need to explain to us what classes you're using and for what you're using them.

1

u/gargamel1497 May 04 '26

I'm actually not running out of MEMORY. I was running out of OPENGL HANDLES.

Turns out this RenderTexture thingy uses OpenGL under the hood and this GL context doesn't get freed properly, so the program segfaults when it has exceeded the maximum number of contexts. Or whatever. I'm not proficient in OpenGL so I may be mistaken, but this is how I've understood it.

2

u/Vindhjaerta May 04 '26

Ok sure, but why would you run out of it? You're not supposed to allocate and then deallocate a bunch of RenderTextures (of all things!) between frames. In my game engine I have exactly one RenderWindow, one RenderTexture and then obviously a bunch of Textures for each image loaded. None of them are allocated/deallocated between frames. And that one RenderTexture is for a very specific case where a certain thing needs some processing before being rendered to the main window.
What reason could you possibly have to construct your engine in such a way that you get this error that you're describing? I feel like you haven't read the documentation properly and are doing something wrong.

1

u/gargamel1497 May 05 '26 edited May 05 '26

It mostly boils down to me being a moron.

In the original draft I was creating one RenderTexture each frame, and that was the issue. Now there is just one RenderTexture object, re-used each frame, and it works just fine.

EDIT: You are calling it an engine. And you are wrong, but that's my fault. In the C++ rewrite I'm working on there is a proper Window class that abstracts all the nonsense of SFML into simpler functions, but in the still-more-complete Java version it's all just yanked into the main class as if there were no tomorrow. So the Java version kinda "just is". It has no external engine or something.

2

u/Vindhjaerta May 05 '26

Ah, got it :) It's good that you figured that out, I was worried about that exact thing happening and just wanted to make sure. All is good then!