Skip to content

Commit 7a88fc1

Browse files
authored
Merge pull request #32 from 8-BIT-DEV/master
Add API for using nk_image
2 parents 9cfd6e2 + 2b3d65f commit 7a88fc1

File tree

4 files changed

+152
-18
lines changed

4 files changed

+152
-18
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ struct nk_context* InitNuklearEx(Font font, float fontSize); // Initialize the
7676
void UpdateNuklear(struct nk_context * ctx); // Update the input state and internal components
7777
void DrawNuklear(struct nk_context * ctx); // Render the Nuklear GUI on the screen
7878
void UnloadNuklear(struct nk_context * ctx); // Unload the GUI
79-
nk_color ColorToNuklear(Color color);
80-
nk_colorf ColorToNuklearF(Color color);
81-
Color ColorFromNuklear(struct nk_color color);
82-
Color ColorFromNuklearF(struct nk_colorf color);
83-
Rectangle RectangleFromNuklear(struct nk_rect rect);
84-
nk_rect RectangleToNuklear(Rectangle rect);
79+
nk_color ColorToNuklear(Color color); // Converts raylib Color to nk_color
80+
nk_colorf ColorToNuklearF(Color color); // Converts raylib Color to nk_colorf
81+
Color ColorFromNuklear(struct nk_color color); // Converts nk_color to raylib Color
82+
Color ColorFromNuklearF(struct nk_colorf color); // Converts nk_colorf to raylib Color
83+
Rectangle RectangleFromNuklear(struct nk_rect rect); // Converts nk_rect to raylib Rectangle
84+
nk_rect RectangleToNuklear(Rectangle rect); // Converts raylib Rectangle to nk_rect
85+
struct nk_image TextureToNuklear(Texture tex); // Convert a raylib Texture to A Nuklear image.
86+
Texture TextureFromNuklear(struct nk_image img); // Convert a Nuklear image to a raylib Texture
87+
struct nk_image LoadNuklearImage(const char* path); // Load a Nuklear image.
88+
void UnloadNuklearImage(struct nk_image img); // Unload a Nuklear image. And free its data
89+
void CleanupNuklearImage(struct nk_image img); // Frees the data stored by the Nuklear image
8590
```
8691
8792
See the [Nuklear API documenation](https://immediate-mode-ui.github.io/Nuklear/doc/nuklear.html) for more how to use Nuklear.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* ===============================================================
2+
*
3+
* EXAMPLE
4+
*
5+
* ===============================================================*/
6+
/*
7+
This example shows how to use the image API from raylib nuklear.
8+
And then display it.
9+
*/
10+
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <raylib.h>
14+
15+
#define RAYLIB_NUKLEAR_IMPLEMENTATION
16+
#include "raylib-nuklear.h"
17+
18+
int main()
19+
{
20+
InitWindow(1280, 720, "[raylib-nuklear] - Texture/Image");
21+
22+
// Initialize the context
23+
struct nk_context* ctx = InitNuklear(16);
24+
// Load the nk_image
25+
struct nk_image img = LoadNuklearImage("resources/test-image.png");
26+
27+
while(!WindowShouldClose())
28+
{
29+
// Input
30+
UpdateNuklear(ctx);
31+
32+
// The window called "Image example" is opend
33+
if(nk_begin(ctx, "Image example", nk_rect(200, 200, 420, 320), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE))
34+
{
35+
// Setup the layout
36+
nk_layout_row_static(ctx, 256, 256, 1);
37+
// Draw the image
38+
nk_image(ctx, img);
39+
}
40+
nk_end(ctx);
41+
42+
// Draw the GUI
43+
BeginDrawing();
44+
ClearBackground(RAYWHITE);
45+
DrawNuklear(ctx);
46+
EndDrawing();
47+
}
48+
49+
// Unload the Nuklear image
50+
UnloadNuklearImage(img);
51+
52+
CloseWindow();
53+
return 0;
54+
}

examples/resources/test-image.png

798 Bytes
Loading

include/raylib-nuklear.h

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ NK_API struct nk_colorf ColorToNuklearF(Color color); // Convert a
5858
NK_API struct Color ColorFromNuklear(struct nk_color color); // Convert a Nuklear color to a raylib Color.
5959
NK_API struct Color ColorFromNuklearF(struct nk_colorf color); // Convert a Nuklear floating color to a raylib Color.
6060
NK_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

Comments
 (0)