Skip to content

Commit

Permalink
Merge branch 'img' of github.com:RobLoach/raylib-nuklear
Browse files Browse the repository at this point in the history
  • Loading branch information
RobLoach committed Apr 14, 2022
2 parents 7a88fc1 + 0b0521c commit dfe01e0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 50 deletions.
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,30 @@ int main() {
## API

``` c
struct nk_context* InitNuklear(int fontSize); // Initialize the Nuklear context
struct nk_context* InitNuklearEx(Font font, float fontSize); // Initialize the Nuklear context, with a custom font
void UpdateNuklear(struct nk_context * ctx); // Update the input state and internal components
void DrawNuklear(struct nk_context * ctx); // Render the Nuklear GUI on the screen
void UnloadNuklear(struct nk_context * ctx); // Unload the GUI
nk_color ColorToNuklear(Color color); // Converts raylib Color to nk_color
nk_colorf ColorToNuklearF(Color color); // Converts raylib Color to nk_colorf
Color ColorFromNuklear(struct nk_color color); // Converts nk_color to raylib Color
Color ColorFromNuklearF(struct nk_colorf color); // Converts nk_colorf to raylib Color
Rectangle RectangleFromNuklear(struct nk_rect rect); // Converts nk_rect to raylib Rectangle
nk_rect RectangleToNuklear(Rectangle rect); // Converts raylib Rectangle to nk_rect
struct nk_image TextureToNuklear(Texture tex); // Convert a raylib Texture to A Nuklear image.
Texture TextureFromNuklear(struct nk_image img); // Convert a Nuklear image to a raylib Texture
struct nk_image LoadNuklearImage(const char* path); // Load a Nuklear image.
void UnloadNuklearImage(struct nk_image img); // Unload a Nuklear image. And free its data
void CleanupNuklearImage(struct nk_image img); // Frees the data stored by the Nuklear image
struct nk_context* InitNuklear(int fontSize); // Initialize the Nuklear GUI context
struct nk_context* InitNuklearEx(Font font, float fontSize); // Initialize the Nuklear GUI context, with a custom font
void UpdateNuklear(struct nk_context * ctx); // Update the input state and internal components for Nuklear
void DrawNuklear(struct nk_context * ctx); // Render the Nuklear GUI on the screen
void UnloadNuklear(struct nk_context * ctx); // Deinitialize the Nuklear context
struct nk_color ColorToNuklear(Color color); // Convert a raylib Color to a Nuklear color object
struct nk_colorf ColorToNuklearF(Color color); // Convert a raylib Color to a Nuklear floating color
struct Color ColorFromNuklear(struct nk_color color); // Convert a Nuklear color to a raylib Color
struct Color ColorFromNuklearF(struct nk_colorf color); // Convert a Nuklear floating color to a raylib Color
struct Rectangle RectangleFromNuklear(struct nk_rect rect); // Convert a Nuklear rectangle to a raylib Rectangle
struct nk_rect RectangleToNuklear(Rectangle rect); // Convert a raylib Rectangle to a Nuklear Rectangle
struct nk_image TextureToNuklear(Texture tex); // Convert a raylib Texture to A Nuklear image
struct Texture TextureFromNuklear(struct nk_image img); // Convert a Nuklear image to a raylib Texture
struct nk_image LoadNuklearImage(const char* path); // Load a Nuklear image
void UnloadNuklearImage(struct nk_image img); // Unload a Nuklear image. And free its data
void CleanupNuklearImage(struct nk_image img); // Frees the data stored by the Nuklear image
```
See the [Nuklear API documenation](https://immediate-mode-ui.github.io/Nuklear/doc/nuklear.html) for more how to use Nuklear.
## Development
While this project uses CMake, CMake is not required in order to use *raylib-nuklear*.
```
git submodule update --init
mkdir build
Expand Down
13 changes: 13 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,34 @@ target_link_libraries(raylib-nuklear-font PUBLIC
raylib_nuklear
)

# raylib-nuklear-texture
add_executable(raylib-nuklear-texture
raylib-nuklear-texture.c
)
target_link_libraries(raylib-nuklear-texture PUBLIC
raylib
raylib_nuklear
)

# Target C99
set_property(TARGET raylib-nuklear-example PROPERTY C_STANDARD 99)
set_property(TARGET raylib-nuklear-demo PROPERTY C_STANDARD 99)
set_property(TARGET raylib-nuklear-font PROPERTY C_STANDARD 99)
set_property(TARGET raylib-nuklear-texture PROPERTY C_STANDARD 99)

# Enable warnings
if(MSVC)
target_compile_options(raylib-nuklear-example PRIVATE /W4 /WX)
target_compile_options(raylib-nuklear-demo PRIVATE /W4 /WX)
target_compile_options(raylib-nuklear-font PRIVATE /W4 /WX)
target_compile_options(raylib-nuklear-texture PRIVATE /W4 /WX)
else()
target_compile_options(raylib-nuklear-example PRIVATE -Wall -Wextra -Wpedantic -Werror)
target_compile_options(raylib-nuklear-demo PRIVATE -Wall -Wextra -Wpedantic -Werror)
target_compile_options(raylib-nuklear-font PRIVATE -Wall -Wextra -Wpedantic -Werror)
target_compile_options(raylib-nuklear-texture PRIVATE -Wall -Wextra -Wpedantic -Werror)
endif()

# Resources
configure_file(resources/anonymous_pro_bold.ttf resources/anonymous_pro_bold.ttf COPYONLY)
configure_file(resources/test-image.png resources/test-image.png COPYONLY)
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
int main()
{
InitWindow(1280, 720, "[raylib-nuklear] - Texture/Image");

// Initialize the context
struct nk_context* ctx = InitNuklear(16);
struct nk_context* ctx = InitNuklear(20);
// Load the nk_image
struct nk_image img = LoadNuklearImage("resources/test-image.png");

while(!WindowShouldClose())
{
// Input
UpdateNuklear(ctx);

// The window called "Image example" is opend
if(nk_begin(ctx, "Image example", nk_rect(200, 200, 420, 320), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE))
{
Expand All @@ -38,17 +38,17 @@ int main()
nk_image(ctx, img);
}
nk_end(ctx);

// Draw the GUI
BeginDrawing();
ClearBackground(RAYWHITE);
DrawNuklear(ctx);
EndDrawing();
}

// Unload the Nuklear image
UnloadNuklearImage(img);

CloseWindow();
return 0;
}
45 changes: 22 additions & 23 deletions include/raylib-nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@
extern "C" {
#endif

NK_API struct nk_context* InitNuklear(int fontSize); // Initialize the Nuklear GUI context.
NK_API struct nk_context* InitNuklearEx(Font font, float fontSize); // Initialize the Nuklear GUI context, with a custom font.
NK_API void UpdateNuklear(struct nk_context * ctx); // Update the input state and internal components for Nuklear.
NK_API void DrawNuklear(struct nk_context * ctx); // Render the Nuklear GUI on the screen.
NK_API void UnloadNuklear(struct nk_context * ctx); // Deinitialize the Nuklear context.
NK_API struct nk_color ColorToNuklear(Color color); // Convert a raylib Color to a Nuklear color object.
NK_API struct nk_colorf ColorToNuklearF(Color color); // Convert a raylib Color to a Nuklear floating color.
NK_API struct Color ColorFromNuklear(struct nk_color color); // Convert a Nuklear color to a raylib Color.
NK_API struct Color ColorFromNuklearF(struct nk_colorf color); // Convert a Nuklear floating color to a raylib Color.
NK_API struct Rectangle RectangleFromNuklear(struct nk_rect rect); // Convert a Nuklear rectangle to a raylib Rectangle.
NK_API struct nk_rect RectangleToNuklear(Rectangle rect); // Convert a raylib Rectangle to a Nuklear Rectangle.
NK_API struct nk_image TextureToNuklear(Texture tex); // Convert a raylib Texture to A Nuklear image.
NK_API struct nk_context* InitNuklear(int fontSize); // Initialize the Nuklear GUI context
NK_API struct nk_context* InitNuklearEx(Font font, float fontSize); // Initialize the Nuklear GUI context, with a custom font
NK_API void UpdateNuklear(struct nk_context * ctx); // Update the input state and internal components for Nuklear
NK_API void DrawNuklear(struct nk_context * ctx); // Render the Nuklear GUI on the screen
NK_API void UnloadNuklear(struct nk_context * ctx); // Deinitialize the Nuklear context
NK_API struct nk_color ColorToNuklear(Color color); // Convert a raylib Color to a Nuklear color object
NK_API struct nk_colorf ColorToNuklearF(Color color); // Convert a raylib Color to a Nuklear floating color
NK_API struct Color ColorFromNuklear(struct nk_color color); // Convert a Nuklear color to a raylib Color
NK_API struct Color ColorFromNuklearF(struct nk_colorf color); // Convert a Nuklear floating color to a raylib Color
NK_API struct Rectangle RectangleFromNuklear(struct nk_rect rect); // Convert a Nuklear rectangle to a raylib Rectangle
NK_API struct nk_rect RectangleToNuklear(Rectangle rect); // Convert a raylib Rectangle to a Nuklear Rectangle
NK_API struct nk_image TextureToNuklear(Texture tex); // Convert a raylib Texture to A Nuklear image
NK_API struct Texture TextureFromNuklear(struct nk_image img); // Convert a Nuklear image to a raylib Texture
NK_API struct nk_image LoadNuklearImage(const char* path); // Load a Nuklear image.
NK_API struct nk_image LoadNuklearImage(const char* path); // Load a Nuklear image
NK_API void UnloadNuklearImage(struct nk_image img); // Unload a Nuklear image. And free its data
NK_API void CleanupNuklearImage(struct nk_image img); // Frees the data stored by the Nuklear image

Expand Down Expand Up @@ -750,19 +750,19 @@ NK_API struct nk_image TextureToNuklear(Texture tex)
// For the texture
struct nk_image img;
Texture* stored_tex = malloc(sizeof(Texture));

// Copy the data from the texture given into the new texture
stored_tex->id = tex.id;
stored_tex->width = tex.width;
stored_tex->height = tex.height;
stored_tex->mipmaps = tex.mipmaps;
stored_tex->format = tex.format;

// Initialize the nk_image struct
img.handle.ptr = stored_tex;
img.w = stored_tex->width;
img.h = stored_tex->height;
img.w = (nk_ushort)stored_tex->width;
img.h = (nk_ushort)stored_tex->height;

return img;
}

Expand All @@ -775,14 +775,14 @@ NK_API struct Texture TextureFromNuklear(struct nk_image img)
// And get back the stored texture
Texture tex;
Texture* stored_tex = (Texture*)img.handle.ptr;

// Copy the data from the stored texture to the texture
tex.id = stored_tex->id;
tex.width = stored_tex->width;
tex.height = stored_tex->height;
tex.mipmaps = stored_tex->mipmaps;
tex.format = stored_tex->format;

return tex;
}

Expand All @@ -793,8 +793,7 @@ NK_API struct Texture TextureFromNuklear(struct nk_image img)
*/
NK_API struct nk_image LoadNuklearImage(const char* path)
{
Texture tex = LoadTexture(path);
return TextureToNuklear(tex);
return TextureToNuklear(LoadTexture(path));
}

/**
Expand All @@ -806,7 +805,7 @@ NK_API void UnloadNuklearImage(struct nk_image img)
{
Texture tex = TextureFromNuklear(img);
UnloadTexture(tex);
free(img.handle.ptr);
CleanupNuklearImage(img);
}

/**
Expand Down
18 changes: 14 additions & 4 deletions test/raylib-nuklear-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ int main(int argc, char *argv[]) {
struct nk_context *ctx = InitNuklear(10);
assert(ctx);

// Image
struct nk_image image = LoadNuklearImage("resources/test-image.png");
assert(image.handle.ptr);
Texture texture = TextureFromNuklear(image);
assert(texture.width > 0);

// UpdateNuklear()
UpdateNuklear(ctx);

// Nuklear GUI Code
// https://github.com/Immediate-Mode-UI/Nuklear/wiki/Window
if (nk_begin(ctx, "Nuklear", nk_rect(100, 100, 220, 220),
if (nk_begin(ctx, "Nuklear", nk_rect(50, 50, 400, 400),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
if (nk_button_label(ctx, "Button")) {
/* event handling */
}
nk_button_label(ctx, "Button");
nk_layout_row_static(ctx, 256, 256, 1);
nk_image(ctx, image);
}
nk_end(ctx);

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

EndDrawing();
WaitTime(500);

// UnloadNuklearImage()
UnloadNuklearImage(image);

// UnloadNuklear()
UnloadNuklear(ctx);
Expand Down
Binary file added test/resources/test-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dfe01e0

Please sign in to comment.