Start on making it backend agnostic ...

With a little configuration/help
This commit is contained in:
Robert Winkler
2025-03-14 16:51:37 -07:00
parent 92a16bc53b
commit 0b4bfec009
6 changed files with 160 additions and 42 deletions
+88 -39
View File
@@ -2,23 +2,20 @@
#define STB_IMAGE_IMPLEMENTATION
#include "../../demo/common/libs/stb_image_c89.h"
struct testImage {
GLuint gpuTextureID;
int width;
int height;
};
struct testImage loadTextureOpenGL(const char *filepath)
#ifdef USING_OPENGL
#define load_nk_image loadTextureOpenGL
struct nk_image loadTextureOpenGL(const char *filepath)
{
int width, height, channels;
GLuint textureID;
unsigned char *data = stbi_load(filepath, &width, &height, &channels, 0);
if (!data)
{
fprintf(stderr, "Failed to load image: %s\n", filepath);
return (struct testImage){0, 0, 0};
return nk_image_type_id(0, 0, 0, 0);
}
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
@@ -27,24 +24,58 @@ struct testImage loadTextureOpenGL(const char *filepath)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
/* Use GL_RGB or GL_RGBA for internal format because GLES2 only supports
* GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA for both format args */
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
return (struct testImage){textureID, width, height};
return nk_image_type_id(textureID, width, height, NK_IMAGE_STRETCH);
}
#endif
#ifndef load_nk_image
#error "Must define load_nk_image() for the image demo!"
#endif
int imagesLoaded = nk_false;
struct testImage bunny_wide;
struct testImage bunny_portrait;
struct testImage tile;
struct nk_image bunny_wide;
struct nk_image bunny_portrait;
struct nk_image tile;
struct nk_image bw_fill, bw_fit, bw_center, bw_tile;
struct nk_image bp_fill, bp_fit, bp_center, bp_tile;
struct nk_image tile_fill, tile_fit, tile_center, tile_tile;
static void
image_demo(struct nk_context *ctx)
{
if(!imagesLoaded){
bunny_wide = loadTextureOpenGL("../../demo/common/img/bunny_wide.jpg");
bunny_portrait = loadTextureOpenGL("../../demo/common/img/bunny_portrait.jpg");
tile = loadTextureOpenGL("../../demo/common/img/tile.jpg");
if (!imagesLoaded) {
/* It would be more convenient if we could pass the type as a third parameter
* to nk_image() but that would be a breaking change and require a few more
* internal changes */
/* stretch as the default */
bunny_wide = load_nk_image("../../demo/common/img/bunny_wide.jpg");
bunny_portrait = load_nk_image("../../demo/common/img/bunny_portrait.jpg");
tile = load_nk_image("../../demo/common/img/tile.jpg");
bw_fill = bunny_wide; bw_fill.type = NK_IMAGE_FILL;
bw_fit = bunny_wide; bw_fit.type = NK_IMAGE_FIT;
bw_center = bunny_wide; bw_center.type = NK_IMAGE_CENTER;
bw_tile = bunny_wide; bw_tile.type = NK_IMAGE_TILE;
bp_fill = bunny_portrait; bp_fill.type = NK_IMAGE_FILL;
bp_fit = bunny_portrait; bp_fit.type = NK_IMAGE_FIT;
bp_center = bunny_portrait; bp_center.type = NK_IMAGE_CENTER;
bp_tile = bunny_portrait; bp_tile.type = NK_IMAGE_TILE;
tile_fill = tile; tile_fill.type = NK_IMAGE_FILL;
tile_fit = tile; tile_fit.type = NK_IMAGE_FIT;
tile_center = tile; tile_center.type = NK_IMAGE_CENTER;
tile_tile = tile; tile_tile.type = NK_IMAGE_TILE;
imagesLoaded = nk_true;
}
@@ -52,84 +83,102 @@ image_demo(struct nk_context *ctx)
however currently takes in the GPU texture ID and recreates the nk_image
in place, which is not resource friendly. */
if (nk_begin(ctx, "Image Wide", nk_rect(5, 25, 250, 700),
NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE | NK_WINDOW_NO_SCROLLBAR))
NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE | NK_WINDOW_NO_SCROLLBAR))
{
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_STRETCH", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(bunny_wide.gpuTextureID, bunny_wide.width, bunny_wide.height, NK_IMAGE_STRETCH));
nk_image(ctx, bunny_wide);
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_FILL", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(bunny_wide.gpuTextureID, bunny_wide.width, bunny_wide.height, NK_IMAGE_FILL));
nk_image(ctx, bw_fill);
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_FIT", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(bunny_wide.gpuTextureID, bunny_wide.width, bunny_wide.height, NK_IMAGE_FIT));
nk_image(ctx, bw_fit);
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_CENTER", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(bunny_wide.gpuTextureID, bunny_wide.width, bunny_wide.height, NK_IMAGE_CENTER));
nk_image(ctx, bw_center);
#ifndef NO_TILING
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_TILE", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(bunny_wide.gpuTextureID, bunny_wide.width, bunny_wide.height, NK_IMAGE_TILE));
nk_image(ctx, bw_tile);
#endif
}
nk_end(ctx);
if (nk_begin(ctx, "Image portrait", nk_rect(260, 25, 230, 700),
NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE | NK_WINDOW_NO_SCROLLBAR))
NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE | NK_WINDOW_NO_SCROLLBAR))
{
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_STRETCH", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(bunny_portrait.gpuTextureID, bunny_portrait.width, bunny_portrait.height, NK_IMAGE_STRETCH));
nk_image(ctx, bunny_portrait);
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_FILL", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(bunny_portrait.gpuTextureID, bunny_portrait.width, bunny_portrait.height, NK_IMAGE_FILL));
nk_image(ctx, bp_fill);
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_FIT", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(bunny_portrait.gpuTextureID, bunny_portrait.width, bunny_portrait.height, NK_IMAGE_FIT));
nk_image(ctx, bp_fit);
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_CENTER", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(bunny_portrait.gpuTextureID, bunny_portrait.width, bunny_portrait.height, NK_IMAGE_CENTER));
nk_image(ctx, bp_center);
#ifndef NO_TILING
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_TILE", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(bunny_portrait.gpuTextureID, bunny_portrait.width, bunny_portrait.height, NK_IMAGE_TILE));
nk_image(ctx, bp_tile);
#endif
}
nk_end(ctx);
if (nk_begin(ctx, "tile", nk_rect(500, 25, 230, 700),
NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE | NK_WINDOW_NO_SCROLLBAR))
NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE | NK_WINDOW_NO_SCROLLBAR))
{
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_STRETCH", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(tile.gpuTextureID, tile.width, tile.height, NK_IMAGE_STRETCH));
nk_image(ctx, tile);
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_FILL", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(tile.gpuTextureID, tile.width, tile.height, NK_IMAGE_FILL));
nk_image(ctx, tile_fill);
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_FIT", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(tile.gpuTextureID, tile.width, tile.height, NK_IMAGE_FIT));
nk_image(ctx, tile_fit);
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_CENTER", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(tile.gpuTextureID, tile.width, tile.height, NK_IMAGE_CENTER));
nk_image(ctx, tile_center);
#ifndef NO_TILING
nk_layout_row_dynamic(ctx, 15, 1);
nk_label(ctx, "NK_IMAGE_TILE", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 100, 1);
nk_image(ctx, nk_image_type_id(tile.gpuTextureID, tile.width, tile.height, NK_IMAGE_TILE));
nk_image(ctx, tile_tile);
#endif
}
nk_end(ctx);
}
}
+1
View File
@@ -73,6 +73,7 @@
#include "../../demo/common/style_configurator.c"
#endif
#ifdef INCLUDE_IMAGE
#define USING_OPENGL
#include "../../demo/common/image.c"
#endif
#ifdef INCLUDE_NODE_EDITOR
+9
View File
@@ -39,6 +39,7 @@
/*#define INCLUDE_CALCULATOR */
/*#define INCLUDE_CANVAS */
#define INCLUDE_OVERVIEW
#define INCLUDE_IMAGE
/*#define INCLUDE_CONFIGURATOR */
/*#define INCLUDE_NODE_EDITOR */
@@ -48,6 +49,7 @@
#define INCLUDE_CANVAS
#define INCLUDE_OVERVIEW
#define INCLUDE_CONFIGURATOR
#define INCLUDE_IMAGE
#define INCLUDE_NODE_EDITOR
#endif
@@ -66,6 +68,10 @@
#ifdef INCLUDE_CONFIGURATOR
#include "../../demo/common/style_configurator.c"
#endif
#ifdef INCLUDE_IMAGE
#define USING_OPENGL
#include "../../demo/common/image.c"
#endif
#ifdef INCLUDE_NODE_EDITOR
#include "../../demo/common/node_editor.c"
#endif
@@ -189,6 +195,9 @@ main(int argc, char *argv[])
#ifdef INCLUDE_NODE_EDITOR
node_editor(ctx);
#endif
#ifdef INCLUDE_IMAGE
image_demo(ctx);
#endif
/* ----------------------------------------- */
/* Draw */
+9
View File
@@ -43,6 +43,7 @@
/*#define INCLUDE_CALCULATOR */
/*#define INCLUDE_CANVAS */
#define INCLUDE_OVERVIEW
#define INCLUDE_IMAGE
/*#define INCLUDE_CONFIGURATOR */
/*#define INCLUDE_NODE_EDITOR */
@@ -52,6 +53,7 @@
#define INCLUDE_CANVAS
#define INCLUDE_OVERVIEW
#define INCLUDE_CONFIGURATOR
#define INCLUDE_IMAGE
#define INCLUDE_NODE_EDITOR
#endif
@@ -70,6 +72,10 @@
#ifdef INCLUDE_CONFIGURATOR
#include "../../demo/common/style_configurator.c"
#endif
#ifdef INCLUDE_IMAGE
#define USING_OPENGL
#include "../../demo/common/image.c"
#endif
#ifdef INCLUDE_NODE_EDITOR
#include "../../demo/common/node_editor.c"
#endif
@@ -213,6 +219,9 @@ int main(int argc, char *argv[])
#ifdef INCLUDE_NODE_EDITOR
node_editor(ctx);
#endif
#ifdef INCLUDE_IMAGE
image_demo(ctx);
#endif
/* ----------------------------------------- */
/* Draw */
+11 -2
View File
@@ -21,8 +21,8 @@
#include "../../nuklear.h"
#include "nuklear_sdl_gles2.h"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_WIDTH 1200
#define WINDOW_HEIGHT 800
#define MAX_VERTEX_MEMORY 512 * 1024
#define MAX_ELEMENT_MEMORY 128 * 1024
@@ -44,6 +44,7 @@
/*#define INCLUDE_CALCULATOR */
/*#define INCLUDE_CANVAS */
#define INCLUDE_OVERVIEW
#define INCLUDE_IMAGE
/*#define INCLUDE_CONFIGURATOR */
/*#define INCLUDE_NODE_EDITOR */
@@ -53,6 +54,7 @@
#define INCLUDE_CANVAS
#define INCLUDE_OVERVIEW
#define INCLUDE_CONFIGURATOR
#define INCLUDE_IMAGE
#define INCLUDE_NODE_EDITOR
#endif
@@ -71,6 +73,10 @@
#ifdef INCLUDE_CONFIGURATOR
#include "../../demo/common/style_configurator.c"
#endif
#ifdef INCLUDE_IMAGE
#define USING_OPENGL
#include "../../demo/common/image.c"
#endif
#ifdef INCLUDE_NODE_EDITOR
#include "../../demo/common/node_editor.c"
#endif
@@ -162,6 +168,9 @@ MainLoop(void* loopArg){
#ifdef INCLUDE_NODE_EDITOR
node_editor(ctx);
#endif
#ifdef INCLUDE_IMAGE
image_demo(ctx);
#endif
/* ----------------------------------------- */
/* Draw */
+42 -1
View File
@@ -38,6 +38,7 @@
/*#define INCLUDE_CALCULATOR */
/*#define INCLUDE_CANVAS */
#define INCLUDE_OVERVIEW
#define INCLUDE_IMAGE
/*#define INCLUDE_CONFIGURATOR */
/*#define INCLUDE_NODE_EDITOR */
@@ -47,9 +48,12 @@
#define INCLUDE_CANVAS
#define INCLUDE_OVERVIEW
#define INCLUDE_CONFIGURATOR
#define INCLUDE_IMAGE
#define INCLUDE_NODE_EDITOR
#endif
struct nk_image load_SDL_Texture(const char *filepath);
#ifdef INCLUDE_STYLE
#include "../../demo/common/style.c"
#endif
@@ -65,10 +69,17 @@
#ifdef INCLUDE_CONFIGURATOR
#include "../../demo/common/style_configurator.c"
#endif
#ifdef INCLUDE_IMAGE
#define load_nk_image load_SDL_Texture
#define NO_TILING
#include "../../demo/common/image.c"
#endif
#ifdef INCLUDE_NODE_EDITOR
#include "../../demo/common/node_editor.c"
#endif
SDL_Renderer *renderer;
/* ===============================================================
*
* DEMO
@@ -79,7 +90,6 @@ main(void)
{
/* Platform */
SDL_Window *win;
SDL_Renderer *renderer;
int running = 1;
int flags = 0;
float font_scale = 1;
@@ -228,6 +238,9 @@ main(void)
#ifdef INCLUDE_NODE_EDITOR
node_editor(ctx);
#endif
#ifdef INCLUDE_IMAGE
image_demo(ctx);
#endif
/* ----------------------------------------- */
SDL_SetRenderDrawColor(renderer, bg.r * 255, bg.g * 255, bg.b * 255, bg.a * 255);
@@ -245,3 +258,31 @@ cleanup:
SDL_Quit();
return 0;
}
struct nk_image load_SDL_Texture(const char *filepath)
{
int w, h, c;
SDL_Texture* tex = NULL;
unsigned char *data = stbi_load(filepath, &w, &h, &c, STBI_rgb_alpha);
if (!data) {
fprintf(stderr, "Failed to load image: %s\n", filepath);
return nk_image_type_ptr(0, 0, 0, 0);
}
tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, w, h);
if (!tex) {
fprintf(stderr, "Error creating texture: %s\n", SDL_GetError());
return nk_image_type_ptr(0, 0, 0, 0);
}
if (SDL_UpdateTexture(tex, NULL, data, w*4)) {
fprintf(stderr, "Error updating texture: %s\n", SDL_GetError());
return nk_image_type_ptr(0, 0, 0, 0);
}
stbi_image_free(data);
return nk_image_type_ptr(tex, w, h, NK_IMAGE_STRETCH);
}