Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
319 changes: 175 additions & 144 deletions demo/common/nuklear_console_demo.c

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions demo/glfw/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ int main(void)
struct nk_glfw glfw = {0};
static GLFWwindow *win;
int width = 0, height = 0;
struct nk_context *ctx;
float font_scale = 3;

/* GLFW */
Expand Down Expand Up @@ -103,7 +102,13 @@ int main(void)
/*nk_style_set_font(ctx, &droid->handle);*/
}

nk_console* console = nuklear_console_demo_init(ctx, NULL, nk_image_id(0));
console = nk_console_init(ctx);

// Initialize console state
struct demo_console_state state = demo_console_state_defaults();

// Intialize the console demo widgets
nuklear_console_demo_init(console, &state, NULL, nk_image_id(0));

while (!glfwWindowShouldClose(win))
{
Expand Down
17 changes: 11 additions & 6 deletions demo/pntr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

typedef struct AppData {
pntr_font* font;
struct nk_context* ctx;
pntr_image* image;
struct nk_console* console;
} AppData;

bool Init(pntr_app* app) {
Expand All @@ -27,18 +25,25 @@ bool Init(pntr_app* app) {

// Load the default font
appData->font = pntr_load_font_default();
appData->ctx = pntr_load_nuklear(appData->font);

ctx = pntr_load_nuklear(appData->font);
appData->image = pntr_load_image("resources/image.png");

// Initialize the Gamepads
appData->console = nuklear_console_demo_init(appData->ctx, app, pntr_image_nk(appData->image));
// Init top console
console = nk_console_init(ctx);

// Initialize console state
struct demo_console_state state = demo_console_state_defaults();

// Intialize the console demo widgets
nuklear_console_demo_init(console, &state, NULL, pntr_image_nk(appData->image));

return true;
}

bool Update(pntr_app* app, pntr_image* screen) {
AppData* appData = (AppData*)pntr_app_userdata(app);
struct nk_context* ctx = appData->ctx;

// Update the pntr input state.
pntr_nuklear_update(ctx, app);
Expand Down Expand Up @@ -74,7 +79,7 @@ void Close(pntr_app* app) {
// Unload the font
pntr_unload_font(appData->font);
pntr_unload_image(appData->image);
pntr_unload_nuklear(appData->ctx);
pntr_unload_nuklear(ctx);

pntr_unload_memory(appData);
}
Expand Down
6 changes: 4 additions & 2 deletions demo/raylib/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

void UpdateDrawFrame(void);

struct nk_context *ctx;
nk_bool closeWindow = nk_false;

int main() {
Expand All @@ -28,7 +27,10 @@ int main() {
ctx = InitNuklearEx(font, fontSize);
Texture texture = LoadTexture("resources/image.png");

console = nuklear_console_demo_init(ctx, NULL, TextureToNuklear(texture));
console = nk_console_init(ctx);

struct demo_console_state state = demo_console_state_defaults();
nuklear_console_demo_init(console, &state, NULL, TextureToNuklear(texture));

#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
Expand Down
32 changes: 23 additions & 9 deletions demo/sdl_renderer/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Install
BIN = nuklear_console_demo_sdl
BIN_MAIN = nuklear_console_demo_sdl
BIN_MULTIPLE = nuklear_console_demo_sdl_multiple_windows

# Flags

Expand All @@ -10,8 +11,11 @@ CXXFLAGS += -pedantic -O0 -fpermissive -std=c++17
CFLAGS += `sdl2-config --cflags`
CXXFLAGS += `sdl2-config --cflags`

SRC = main.c
OBJ = $(SRC:.c=.o)
SRC_MAIN = main.c
SRC_MULTIPLE = main_multiple_windows.c

OBJ_MAIN = $(SRC_MAIN:.c=.o)
OBJ_MULTIPLE = $(SRC_MULTIPLE:.c=.o)

ifeq ($(OS),Windows_NT)
#TODO
Expand All @@ -20,17 +24,27 @@ ifeq ($(OS),Windows_NT)
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
#TODO LIBS = -lSDL2 -framework OpenGL -lm
LIBS += -lm -ldl `sdl2-config --libs`
else
LIBS += -lm -ldl `sdl2-config --libs`
endif
endif

$(BIN): clean
$(CC) $(SRC) $(CFLAGS) -o $(BIN) $(LIBS)
# Default target: build both binaries
all: $(BIN_MAIN) $(BIN_MULTIPLE)

# Rule for nuklear_console_demo_sdl (main.c)
$(BIN_MAIN): $(SRC_MAIN)
$(CC) $(SRC_MAIN) $(CFLAGS) -o $(BIN_MAIN) $(LIBS)

# Rule for nuklear_console_demo_sdl_multiple (main_multiple.c)
$(BIN_MULTIPLE): $(SRC_MULTIPLE)
$(CC) $(SRC_MULTIPLE) $(CFLAGS) -o $(BIN_MULTIPLE) $(LIBS)

# Clean up
clean:
rm -rf $(BIN) $(OBJS)
rm -rf $(BIN_MAIN) $(BIN_MULTIPLE) $(OBJ_MAIN) $(OBJ_MULTIPLE)

test: $(BIN)
./$(BIN)
# Test the main binary
test: $(BIN_MAIN)
./$(BIN_MAIN)
9 changes: 5 additions & 4 deletions demo/sdl_renderer/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ int main(int argc, char *argv[]) {
int flags = 0;
float font_scale = 3;

/* GUI */
struct nk_context *ctx;

/* SDL setup */
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMECONTROLLER);
Expand Down Expand Up @@ -103,7 +100,11 @@ int main(int argc, char *argv[]) {
SDL_FreeSurface(surface);
}

nk_console* console = nuklear_console_demo_init(ctx, NULL, img);
console = nk_console_init(ctx);

// Initialize console state
struct demo_console_state state = demo_console_state_defaults();
nuklear_console_demo_init(console, &state, NULL, img);

while (running) {
/* Input */
Expand Down
59 changes: 59 additions & 0 deletions demo/sdl_renderer/main_multiple_windows.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "setup.h"

int main(int argc, char* argv[]) {
NK_UNUSED(argc);
NK_UNUSED(argv);

// Init demo context
struct demo_ctx demo = init_demo_context(1280, 720);

// Init console
console = nk_console_init(ctx);

const int WINDOW_COUNT = 3;

int window_flags = NK_WINDOW_SCROLL_AUTO_HIDE | NK_WINDOW_TITLE;

// Register console windows
struct demo_console_state states[WINDOW_COUNT];

for (int i = 0; i < WINDOW_COUNT; i++) {
// Initialize console window state
states[i] = demo_console_state_defaults();

// Set window title
snprintf(states[i].title, sizeof(states[i].title), "nuklear_console_%d", i + 1);

// Register console window
nk_console* window = nk_console_window(console, states[i].title, nk_rect(i * demo.window_width / WINDOW_COUNT, 0, demo.window_width / WINDOW_COUNT, demo.window_height), window_flags);

// Register demo widgets to console window
nuklear_console_demo_init(window, &states[i], NULL, demo.img);
}

int running = 1;

while (running) {
/* Input */
SDL_Event evt;
nk_input_begin(ctx);
nk_gamepad_update(nk_console_get_gamepads(console));
while (SDL_PollEvent(&evt)) {
if (evt.type == SDL_QUIT) return cleanup(&demo);
if (evt.type == SDL_KEYUP && evt.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = 0;

nk_sdl_handle_event(&evt);
nk_gamepad_sdl_handle_event(nk_console_get_gamepads(console), &evt);
}
nk_input_end(ctx);

/* GUI */
if (nuklear_console_demo_render()) {
running = 0;
}

render(demo.renderer);
}

return 0;
}
114 changes: 114 additions & 0 deletions demo/sdl_renderer/setup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <string.h>
#include <time.h>

#include <SDL2/SDL.h>

#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_COMMAND_USERDATA
#define NK_IMPLEMENTATION
#define NK_SDL_RENDERER_IMPLEMENTATION
#include "../../vendor/Nuklear/nuklear.h"
#include "../../vendor/Nuklear/demo/sdl_renderer/nuklear_sdl_renderer.h"

#include "../common/nuklear_console_demo.c"

struct demo_ctx {
SDL_Renderer* renderer;
struct nk_image img;
SDL_Texture* _texture;
SDL_Window* _window;
int window_width;
int window_height;
};

static int configure(struct demo_ctx* demo) {
/* Platform */
int flags = 0;
float font_scale = 3;

/* SDL setup */
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMECONTROLLER);

demo->_window = SDL_CreateWindow("nuklear_console_demo_multiple_window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, demo->window_width, demo->window_height, SDL_WINDOW_SHOWN);

if (demo->_window == NULL) {
SDL_Log("Error SDL_CreateWindow %s", SDL_GetError());
return 1;
}

demo->renderer = SDL_CreateRenderer(demo->_window, -1, flags);

if (demo->renderer == NULL) {
SDL_Log("Error SDL_CreateRenderer %s", SDL_GetError());
return 1;
}

/* GUI */
ctx = nk_sdl_init(demo->_window, demo->renderer);
{
struct nk_font_atlas* atlas;
struct nk_font_config config = nk_font_config(0);
struct nk_font* font;

nk_sdl_font_stash_begin(&atlas);
font = nk_font_atlas_add_default(atlas, 13 * font_scale, &config);
nk_sdl_font_stash_end();
nk_style_set_font(ctx, &font->handle);
}

// Attempt to load the sample image.
demo->img = nk_image_id(0);
SDL_Surface* surface = SDL_LoadBMP("image.bmp");
if (surface != NULL) {
demo->_texture = SDL_CreateTextureFromSurface(demo->renderer, surface);
if (demo->_texture != NULL) {
demo->img = nk_image_ptr(demo->_texture);
demo->img.w = surface->w;
demo->img.h = surface->h;
demo->img.region[0] = 0;
demo->img.region[1] = 0;
demo->img.region[2] = surface->w;
demo->img.region[3] = surface->h;
}
SDL_FreeSurface(surface);
}

return 0;
}

static struct demo_ctx init_demo_context(int window_width, int window_height) {
struct demo_ctx demo;
demo.window_width = window_width;
demo.window_height = window_height;
configure(&demo);
return demo;
}

static int cleanup(struct demo_ctx* demo) {
if (demo->_texture != NULL) {
SDL_DestroyTexture(demo->_texture);
}
nuklear_console_demo_free();
nk_sdl_shutdown();
SDL_DestroyRenderer(demo->renderer);
SDL_DestroyWindow(demo->_window);
SDL_Quit();

return 0;
}

static void render(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);

nk_sdl_render(NK_ANTI_ALIASING_ON);

SDL_RenderPresent(renderer);
}
Loading