Skip to content

Commit dfe01e0

Browse files
committed
Merge branch 'img' of github.com:RobLoach/raylib-nuklear
2 parents 7a88fc1 + 0b0521c commit dfe01e0

File tree

6 files changed

+74
-50
lines changed

6 files changed

+74
-50
lines changed

README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,30 @@ int main() {
7171
## API
7272

7373
``` c
74-
struct nk_context* InitNuklear(int fontSize); // Initialize the Nuklear context
75-
struct nk_context* InitNuklearEx(Font font, float fontSize); // Initialize the Nuklear context, with a custom font
76-
void UpdateNuklear(struct nk_context * ctx); // Update the input state and internal components
77-
void DrawNuklear(struct nk_context * ctx); // Render the Nuklear GUI on the screen
78-
void UnloadNuklear(struct nk_context * ctx); // Unload the GUI
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
74+
struct nk_context* InitNuklear(int fontSize); // Initialize the Nuklear GUI context
75+
struct nk_context* InitNuklearEx(Font font, float fontSize); // Initialize the Nuklear GUI context, with a custom font
76+
void UpdateNuklear(struct nk_context * ctx); // Update the input state and internal components for Nuklear
77+
void DrawNuklear(struct nk_context * ctx); // Render the Nuklear GUI on the screen
78+
void UnloadNuklear(struct nk_context * ctx); // Deinitialize the Nuklear context
79+
struct nk_color ColorToNuklear(Color color); // Convert a raylib Color to a Nuklear color object
80+
struct nk_colorf ColorToNuklearF(Color color); // Convert a raylib Color to a Nuklear floating color
81+
struct Color ColorFromNuklear(struct nk_color color); // Convert a Nuklear color to a raylib Color
82+
struct Color ColorFromNuklearF(struct nk_colorf color); // Convert a Nuklear floating color to a raylib Color
83+
struct Rectangle RectangleFromNuklear(struct nk_rect rect); // Convert a Nuklear rectangle to a raylib Rectangle
84+
struct nk_rect RectangleToNuklear(Rectangle rect); // Convert a raylib Rectangle to a Nuklear Rectangle
85+
struct nk_image TextureToNuklear(Texture tex); // Convert a raylib Texture to A Nuklear image
86+
struct 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
9090
```
9191
9292
See the [Nuklear API documenation](https://immediate-mode-ui.github.io/Nuklear/doc/nuklear.html) for more how to use Nuklear.
9393
9494
## Development
9595
96+
While this project uses CMake, CMake is not required in order to use *raylib-nuklear*.
97+
9698
```
9799
git submodule update --init
98100
mkdir build

examples/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,34 @@ target_link_libraries(raylib-nuklear-font PUBLIC
4444
raylib_nuklear
4545
)
4646

47+
# raylib-nuklear-texture
48+
add_executable(raylib-nuklear-texture
49+
raylib-nuklear-texture.c
50+
)
51+
target_link_libraries(raylib-nuklear-texture PUBLIC
52+
raylib
53+
raylib_nuklear
54+
)
55+
4756
# Target C99
4857
set_property(TARGET raylib-nuklear-example PROPERTY C_STANDARD 99)
4958
set_property(TARGET raylib-nuklear-demo PROPERTY C_STANDARD 99)
5059
set_property(TARGET raylib-nuklear-font PROPERTY C_STANDARD 99)
60+
set_property(TARGET raylib-nuklear-texture PROPERTY C_STANDARD 99)
5161

5262
# Enable warnings
5363
if(MSVC)
5464
target_compile_options(raylib-nuklear-example PRIVATE /W4 /WX)
5565
target_compile_options(raylib-nuklear-demo PRIVATE /W4 /WX)
5666
target_compile_options(raylib-nuklear-font PRIVATE /W4 /WX)
67+
target_compile_options(raylib-nuklear-texture PRIVATE /W4 /WX)
5768
else()
5869
target_compile_options(raylib-nuklear-example PRIVATE -Wall -Wextra -Wpedantic -Werror)
5970
target_compile_options(raylib-nuklear-demo PRIVATE -Wall -Wextra -Wpedantic -Werror)
6071
target_compile_options(raylib-nuklear-font PRIVATE -Wall -Wextra -Wpedantic -Werror)
72+
target_compile_options(raylib-nuklear-texture PRIVATE -Wall -Wextra -Wpedantic -Werror)
6173
endif()
6274

6375
# Resources
6476
configure_file(resources/anonymous_pro_bold.ttf resources/anonymous_pro_bold.ttf COPYONLY)
77+
configure_file(resources/test-image.png resources/test-image.png COPYONLY)
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
int main()
1919
{
2020
InitWindow(1280, 720, "[raylib-nuklear] - Texture/Image");
21-
21+
2222
// Initialize the context
23-
struct nk_context* ctx = InitNuklear(16);
23+
struct nk_context* ctx = InitNuklear(20);
2424
// Load the nk_image
2525
struct nk_image img = LoadNuklearImage("resources/test-image.png");
26-
26+
2727
while(!WindowShouldClose())
2828
{
2929
// Input
3030
UpdateNuklear(ctx);
31-
31+
3232
// The window called "Image example" is opend
3333
if(nk_begin(ctx, "Image example", nk_rect(200, 200, 420, 320), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE))
3434
{
@@ -38,17 +38,17 @@ int main()
3838
nk_image(ctx, img);
3939
}
4040
nk_end(ctx);
41-
41+
4242
// Draw the GUI
4343
BeginDrawing();
4444
ClearBackground(RAYWHITE);
4545
DrawNuklear(ctx);
4646
EndDrawing();
4747
}
48-
48+
4949
// Unload the Nuklear image
5050
UnloadNuklearImage(img);
51-
51+
5252
CloseWindow();
5353
return 0;
5454
}

include/raylib-nuklear.h

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@
4848
extern "C" {
4949
#endif
5050

51-
NK_API struct nk_context* InitNuklear(int fontSize); // Initialize the Nuklear GUI context.
52-
NK_API struct nk_context* InitNuklearEx(Font font, float fontSize); // Initialize the Nuklear GUI context, with a custom font.
53-
NK_API void UpdateNuklear(struct nk_context * ctx); // Update the input state and internal components for Nuklear.
54-
NK_API void DrawNuklear(struct nk_context * ctx); // Render the Nuklear GUI on the screen.
55-
NK_API void UnloadNuklear(struct nk_context * ctx); // Deinitialize the Nuklear context.
56-
NK_API struct nk_color ColorToNuklear(Color color); // Convert a raylib Color to a Nuklear color object.
57-
NK_API struct nk_colorf ColorToNuklearF(Color color); // Convert a raylib Color to a Nuklear floating color.
58-
NK_API struct Color ColorFromNuklear(struct nk_color color); // Convert a Nuklear color to a raylib Color.
59-
NK_API struct Color ColorFromNuklearF(struct nk_colorf color); // Convert a Nuklear floating color to a raylib Color.
60-
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.
62-
NK_API struct nk_image TextureToNuklear(Texture tex); // Convert a raylib Texture to A Nuklear image.
51+
NK_API struct nk_context* InitNuklear(int fontSize); // Initialize the Nuklear GUI context
52+
NK_API struct nk_context* InitNuklearEx(Font font, float fontSize); // Initialize the Nuklear GUI context, with a custom font
53+
NK_API void UpdateNuklear(struct nk_context * ctx); // Update the input state and internal components for Nuklear
54+
NK_API void DrawNuklear(struct nk_context * ctx); // Render the Nuklear GUI on the screen
55+
NK_API void UnloadNuklear(struct nk_context * ctx); // Deinitialize the Nuklear context
56+
NK_API struct nk_color ColorToNuklear(Color color); // Convert a raylib Color to a Nuklear color object
57+
NK_API struct nk_colorf ColorToNuklearF(Color color); // Convert a raylib Color to a Nuklear floating color
58+
NK_API struct Color ColorFromNuklear(struct nk_color color); // Convert a Nuklear color to a raylib Color
59+
NK_API struct Color ColorFromNuklearF(struct nk_colorf color); // Convert a Nuklear floating color to a raylib Color
60+
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
62+
NK_API struct nk_image TextureToNuklear(Texture tex); // Convert a raylib Texture to A Nuklear image
6363
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.
64+
NK_API struct nk_image LoadNuklearImage(const char* path); // Load a Nuklear image
6565
NK_API void UnloadNuklearImage(struct nk_image img); // Unload a Nuklear image. And free its data
6666
NK_API void CleanupNuklearImage(struct nk_image img); // Frees the data stored by the Nuklear image
6767

@@ -750,19 +750,19 @@ NK_API struct nk_image TextureToNuklear(Texture tex)
750750
// For the texture
751751
struct nk_image img;
752752
Texture* stored_tex = malloc(sizeof(Texture));
753-
753+
754754
// Copy the data from the texture given into the new texture
755755
stored_tex->id = tex.id;
756756
stored_tex->width = tex.width;
757757
stored_tex->height = tex.height;
758758
stored_tex->mipmaps = tex.mipmaps;
759759
stored_tex->format = tex.format;
760-
760+
761761
// Initialize the nk_image struct
762762
img.handle.ptr = stored_tex;
763-
img.w = stored_tex->width;
764-
img.h = stored_tex->height;
765-
763+
img.w = (nk_ushort)stored_tex->width;
764+
img.h = (nk_ushort)stored_tex->height;
765+
766766
return img;
767767
}
768768

@@ -775,14 +775,14 @@ NK_API struct Texture TextureFromNuklear(struct nk_image img)
775775
// And get back the stored texture
776776
Texture tex;
777777
Texture* stored_tex = (Texture*)img.handle.ptr;
778-
778+
779779
// Copy the data from the stored texture to the texture
780780
tex.id = stored_tex->id;
781781
tex.width = stored_tex->width;
782782
tex.height = stored_tex->height;
783783
tex.mipmaps = stored_tex->mipmaps;
784784
tex.format = stored_tex->format;
785-
785+
786786
return tex;
787787
}
788788

@@ -793,8 +793,7 @@ NK_API struct Texture TextureFromNuklear(struct nk_image img)
793793
*/
794794
NK_API struct nk_image LoadNuklearImage(const char* path)
795795
{
796-
Texture tex = LoadTexture(path);
797-
return TextureToNuklear(tex);
796+
return TextureToNuklear(LoadTexture(path));
798797
}
799798

800799
/**
@@ -806,7 +805,7 @@ NK_API void UnloadNuklearImage(struct nk_image img)
806805
{
807806
Texture tex = TextureFromNuklear(img);
808807
UnloadTexture(tex);
809-
free(img.handle.ptr);
808+
CleanupNuklearImage(img);
810809
}
811810

812811
/**

test/raylib-nuklear-test.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@ int main(int argc, char *argv[]) {
2424
struct nk_context *ctx = InitNuklear(10);
2525
assert(ctx);
2626

27+
// Image
28+
struct nk_image image = LoadNuklearImage("resources/test-image.png");
29+
assert(image.handle.ptr);
30+
Texture texture = TextureFromNuklear(image);
31+
assert(texture.width > 0);
32+
2733
// UpdateNuklear()
2834
UpdateNuklear(ctx);
2935

3036
// Nuklear GUI Code
3137
// https://github.com/Immediate-Mode-UI/Nuklear/wiki/Window
32-
if (nk_begin(ctx, "Nuklear", nk_rect(100, 100, 220, 220),
38+
if (nk_begin(ctx, "Nuklear", nk_rect(50, 50, 400, 400),
3339
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
34-
if (nk_button_label(ctx, "Button")) {
35-
/* event handling */
36-
}
40+
nk_button_label(ctx, "Button");
41+
nk_layout_row_static(ctx, 256, 256, 1);
42+
nk_image(ctx, image);
3743
}
3844
nk_end(ctx);
3945

@@ -45,6 +51,10 @@ int main(int argc, char *argv[]) {
4551
DrawNuklear(ctx);
4652

4753
EndDrawing();
54+
WaitTime(500);
55+
56+
// UnloadNuklearImage()
57+
UnloadNuklearImage(image);
4858

4959
// UnloadNuklear()
5060
UnloadNuklear(ctx);

test/resources/test-image.png

798 Bytes
Loading

0 commit comments

Comments
 (0)