@@ -58,7 +58,12 @@ NK_API struct nk_colorf ColorToNuklearF(Color color); // Convert a
5858NK_API struct Color ColorFromNuklear (struct nk_color color ); // Convert a Nuklear color to a raylib Color.
5959NK_API struct Color ColorFromNuklearF (struct nk_colorf color ); // Convert a Nuklear floating color to a raylib Color.
6060NK_API struct Rectangle RectangleFromNuklear (struct nk_rect rect ); // Convert a Nuklear rectangle to a raylib Rectangle.
61- NK_API struct nk_rect RectangleToNuklear (Rectangle rect ); // Convert a raylib Rectangle to a nuklear Rectangle.
61+ NK_API struct nk_rect RectangleToNuklear (Rectangle rect ); // Convert a raylib Rectangle to a Nuklear Rectangle.
62+ NK_API struct nk_image TextureToNuklear (Texture tex ); // Convert a raylib Texture to A Nuklear image.
63+ NK_API struct Texture TextureFromNuklear (struct nk_image img ); // Convert a Nuklear image to a raylib Texture
64+ NK_API struct nk_image LoadNuklearImage (const char * path ); // Load a Nuklear image.
65+ NK_API void UnloadNuklearImage (struct nk_image img ); // Unload a Nuklear image. And free its data
66+ NK_API void CleanupNuklearImage (struct nk_image img ); // Frees the data stored by the Nuklear image
6267
6368#ifdef __cplusplus
6469}
@@ -503,22 +508,13 @@ DrawNuklear(struct nk_context * ctx)
503508 } break ;
504509
505510 case NK_COMMAND_IMAGE : {
506- // TODO: Verify NK_COMMAND_IMAGE
507- TraceLog (LOG_WARNING , "NUKLEAR: Broken implementation NK_COMMAND_IMAGE" );
508511 const struct nk_command_image * i = (const struct nk_command_image * )cmd ;
509- struct Image image ;
510- image .data = i -> img .handle .ptr ;
511- image .width = i -> w ;
512- image .height = i -> h ;
513- image .mipmaps = 1 ;
514- image .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 ;
515- Texture texture = LoadTextureFromImage (image );
516- Rectangle source = {0 , 0 , (float )image .width , (float )image .height };
512+ Texture texture = * (Texture * )i -> img .handle .ptr ;
513+ Rectangle source = {0 , 0 , (float )texture .width , (float )texture .height };
517514 Rectangle dest = {(float )i -> x , (float )i -> y , (float )i -> w , (float )i -> h };
518515 Vector2 origin = {0 , 0 };
519516 Color tint = ColorFromNuklear (i -> col );
520517 DrawTexturePro (texture , source , dest , origin , 0 , tint );
521- UnloadTexture (texture );
522518 } break ;
523519
524520 case NK_COMMAND_CUSTOM : {
@@ -745,6 +741,85 @@ nk_rect RectangleToNuklear(Rectangle rect)
745741 return nk_rect (rect .x , rect .y , rect .width , rect .height );
746742}
747743
744+ /**
745+ * Convert the given raylib texture to a Nuklear image
746+ */
747+ NK_API struct nk_image TextureToNuklear (Texture tex )
748+ {
749+ // Declare the img to store data and allocate memory
750+ // For the texture
751+ struct nk_image img ;
752+ Texture * stored_tex = malloc (sizeof (Texture ));
753+
754+ // Copy the data from the texture given into the new texture
755+ stored_tex -> id = tex .id ;
756+ stored_tex -> width = tex .width ;
757+ stored_tex -> height = tex .height ;
758+ stored_tex -> mipmaps = tex .mipmaps ;
759+ stored_tex -> format = tex .format ;
760+
761+ // Initialize the nk_image struct
762+ img .handle .ptr = stored_tex ;
763+ img .w = stored_tex -> width ;
764+ img .h = stored_tex -> height ;
765+
766+ return img ;
767+ }
768+
769+ /**
770+ * Convert the given Nuklear image to a raylib Texture
771+ */
772+ NK_API struct Texture TextureFromNuklear (struct nk_image img )
773+ {
774+ // Declare texture for storage
775+ // And get back the stored texture
776+ Texture tex ;
777+ Texture * stored_tex = (Texture * )img .handle .ptr ;
778+
779+ // Copy the data from the stored texture to the texture
780+ tex .id = stored_tex -> id ;
781+ tex .width = stored_tex -> width ;
782+ tex .height = stored_tex -> height ;
783+ tex .mipmaps = stored_tex -> mipmaps ;
784+ tex .format = stored_tex -> format ;
785+
786+ return tex ;
787+ }
788+
789+ /**
790+ * Load a Nuklear image directly
791+ *
792+ * @param path The path to the image
793+ */
794+ NK_API struct nk_image LoadNuklearImage (const char * path )
795+ {
796+ Texture tex = LoadTexture (path );
797+ return TextureToNuklear (tex );
798+ }
799+
800+ /**
801+ * Unload a loaded Nuklear image
802+ *
803+ * @param img The Nuklear image to unload
804+ */
805+ NK_API void UnloadNuklearImage (struct nk_image img )
806+ {
807+ Texture tex = TextureFromNuklear (img );
808+ UnloadTexture (tex );
809+ free (img .handle .ptr );
810+ }
811+
812+ /**
813+ * Cleans up memory used by a Nuklear image
814+ * Does not unload the image.
815+ *
816+ * @param img The Nuklear image to cleanup
817+ */
818+ NK_API void CleanupNuklearImage (struct nk_image img )
819+ {
820+ free (img .handle .ptr );
821+ }
822+
748823#ifdef __cplusplus
749824}
750825#endif
0 commit comments