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

Can somebody give a demo example about how to use STB's rect pack? #1050

Open
Hubert-Yang opened this issue Mar 17, 2025 · 1 comment
Open

Comments

@Hubert-Yang
Copy link

Question

Hi,

I try to understand how to user stb's STBRectPack, does it working, here I add a unit test like:

STBRPNode.Buffer nodes_buffer = STBRPNode.malloc(3);

STBRPRect.Buffer rect_buffer = STBRPRect.malloc(3);

int index = 0;
for(STBRPRect rect : rect_buffer)
{
rect.set(index++, 0, 0, 30, 40, false);
}

atlas.setup_atlas_map(nodes_buffer, rect_buffer);

public void setup_atlas_map(STBRPNode.Buffer buffers, STBRPRect.Buffer rects)
{

STBRPContext pc =  STBRPContext.malloc(); 
if(pc != null)
{
	STBRectPack.stbrp_init_target(pc, atlas_w, atlas_h, buffers);	
	STBRectPack.stbrp_setup_heuristic(pc, STBRectPack.STBRP_HEURISTIC_Skyline_BF_sortHeight); 
	STBRectPack.stbrp_setup_allow_out_of_mem(pc, true);			
					
	STBRectPack.stbrp_pack_rects(pc, rects); 
}
	
// first we get altas with uv. 

	
for(STBRPNode node : buffers)
{
	System.out.println("get nodes as " + node.x() + " " + node.Y); 
}

}

but seems the node result is nothing. any where is wrong?

Thanks

Hubert Yang

@knokko
Copy link
Contributor

knokko commented Mar 21, 2025

It sounds like a good idea to create a proper sample that demonstrates how to use STB rect pack. I might make one when I have considerably more time.

In the meantime, I do have some old code that uses STB rect pack, which you can use as reference: https://github.com/knokko/gui-stuff/blob/46f178278ba5f83c3af8c6e129fd8a092de276c6/graviks2d/src/main/kotlin/graviks2d/resource/text/TextShapeCache.kt#L68

Unfortunately, I haven't used it in a while, and I don't see any obvious mistakes in your code. Maybe try to print index after this code?

int index = 0;
for(STBRPRect rect : rect_buffer)
{
rect.set(index++, 0, 0, 30, 40, false);
}

and double-check the position() and limit() of rect_buffer! It would not be the first case where the position/limit screws someone over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants