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

Creating a nk_image with the sdl_renderer backend #599

Closed
zoogies opened this issue Dec 26, 2023 · 2 comments
Closed

Creating a nk_image with the sdl_renderer backend #599

zoogies opened this issue Dec 26, 2023 · 2 comments

Comments

@zoogies
Copy link

zoogies commented Dec 26, 2023

First, I would like to express my appreciation for Nuklear and its support of the sdl_renderer as a backend.

My question is pretty simple: how can I create an nk_image from a file (or from a SDL_Texture or SDL_Surface) using the sdl_renderer backend?

There are no such examples included in the repo (for this backend) and I've repeatedly come back every few weeks to this topic and tried persistently to no avail. Reading Nuklear and SDL source has not helped me figure this out, but I am more than willing to investigate any resources anyone is willing to throw my way! :)

@RobLoach
Copy link
Contributor

RobLoach commented Feb 7, 2024

nk_image is...

struct nk_image {nk_handle handle; nk_ushort w, h; nk_ushort region[4];};

handle.ptr should point to an SDL_Texture/SDL_Surface, and you'd interact with it from there.

@zoogies
Copy link
Author

zoogies commented Feb 21, 2024

It seems that my issue was attempting to pass nk_image_ptr an SDL_Surface, or the (void*) pixels field of the Surface, rather than a SDL_Texture * which does indeed work.

Here is a small snippet using SDL and SDL_Image to display an nk_image inside of a button (for anyone who stumbles upon this issue in the future.

// do this once, sometime at init:
SDL_Surface *icon_build_surface = IMG_Load(PATH_TO_YOUR_IMAGE_HERE);
SDL_Texture *icon_build_tex = SDL_CreateTextureFromSurface(POINTER_TO_YOUR_SDL_RENDERER, icon_build_surface);
icon_build_texture = nk_image_ptr(icon_build_tex);
SDL_FreeSurface(icon_build_surface);

// usage of the image during nuklear lifecycle:
nk_button_image_label(ctx, icon_build_texture, "Build", NK_TEXT_CENTERED);

// of course, free your texture before shutting down:
SDL_DestroyTexture(icon_build_tex);

Screenshot_20240221_140225

The surfaces probably don't work due to variable SDL_PixelFormat's, but I am not familiar enough with the implementation to know if that's truly the case.

Additionally, I've gone ahead and opened a PR to add an example of nk_button_image_label to the sdl_renderer backend.
Hopefully that will be a good resource in the future to illustrate this.

#613

@zoogies zoogies closed this as completed Feb 21, 2024
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

No branches or pull requests

2 participants