Add `nuklear` example

This commit is contained in:
Alexander Popov 2024-01-27 23:54:37 +03:00
parent 64c7319fce
commit 2b3606c758
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
3 changed files with 152 additions and 0 deletions

6
nuklear/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
test
*.ttf
nuklear_glfw_gl2.h
stb_image.h
canvas.c

16
nuklear/Makefile Normal file
View File

@ -0,0 +1,16 @@
PROGRAM = test
CC = gcc
CFLAGS = -std=c99 -Wall -O2
CFLAGS += -I/home/user/Git/Nuklear
LDFLAGS =
LIBS = -lglfw -lGL -lm -lGLU
.PHONY: $(PROGRAM)
all: $(PROGRAM)
$(PROGRAM):
$(CC) $(CFLAGS) -o $@ main.c $(LIBS)
clean:
rm -f $(PROGRAM)

130
nuklear/main.c Normal file
View File

@ -0,0 +1,130 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <limits.h>
#include <time.h>
#include <GLFW/glfw3.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_IMPLEMENTATION
#define NK_GLFW_GL2_IMPLEMENTATION
#define NK_KEYSTATE_BASED_INPUT
#include <nuklear.h>
#include "nuklear_glfw_gl2.h"
#define WINDOW_WIDTH 230
#define WINDOW_HEIGHT 250
static void error_callback(int e, const char *d) {
printf("Error %d: %s\n", e, d);
}
int main(void) {
/* Platform */
static GLFWwindow *win;
int width = 0, height = 0;
/* GUI */
struct nk_context *ctx;
struct nk_colorf bg;
/* GLFW */
glfwSetErrorCallback(error_callback);
if (!glfwInit()) {
fprintf(stdout, "[GFLW] failed to init!\n");
exit(1);
}
win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
glfwMakeContextCurrent(win);
glfwGetWindowSize(win, &width, &height);
/* GUI */
ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS);
/* Load Fonts: if none of these are loaded a default font will be used */
/* Load Cursor: if you uncomment cursor loading please hide the cursor */
{
struct nk_font_atlas *atlas;
nk_glfw3_font_stash_begin(&atlas);
/*struct nk_font *droid = nk_font_atlas_add_from_file(atlas,
* "../../../extra_font/DroidSans.ttf", 14, 0);*/
/*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas,
* "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
/*struct nk_font *future = nk_font_atlas_add_from_file(atlas,
* "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
/*struct nk_font *clean = nk_font_atlas_add_from_file(atlas,
* "../../../extra_font/ProggyClean.ttf", 12, 0);*/
/*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas,
* "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
nk_rune ranges_latin[] = {
0x0020, 0x007E, /* Ascii */
0x00A1, 0x00FF, /* Symbols + Umlaute */
0x0410, 0x044F, /* Main Cyrillic */
0
};
struct nk_font_config cfg_japan = nk_font_config(0);
cfg_japan.range = ranges_latin;
struct nk_font *compassgold_font = nk_font_atlas_add_from_file(atlas, "Ubuntu-R.ttf", 18, 0, &cfg_japanm );
nk_style_set_font(ctx, &compassgold_font->handle);
nk_glfw3_font_stash_end();
}
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
while (!glfwWindowShouldClose(win)) {
/* Input */
glfwPollEvents();
nk_glfw3_new_frame();
/* GUI */
if (nk_begin(ctx, "Demo", nk_rect(0, 0, 230, 250), NK_WINDOW_BORDER)) {
enum { EASY, HARD };
static int op = EASY;
static char text[1][64];
static int text_len[3];
nk_layout_row_static(ctx, 30, 80, 1);
nk_label(ctx, "Введите URL", NK_TEXT_CENTERED);
nk_edit_string(ctx, NK_EDIT_FIELD, text[0], &text_len[2], 64, nk_filter_default);
if (nk_button_label(ctx, "button"))
fprintf(stdout, "button pressed\n");
nk_layout_row_dynamic(ctx, 30, 2);
if (nk_option_label(ctx, "easy", op == EASY))
op = EASY;
if (nk_option_label(ctx, "hard", op == HARD))
op = HARD;
}
nk_end(ctx);
/* Draw */
glfwGetWindowSize(win, &width, &height);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(bg.r, bg.g, bg.b, bg.a);
/* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state
* with blending, scissor, face culling and depth test and defaults
* everything back into a default state. Make sure to either save and
* restore or reset your own state after drawing rendering the UI. */
nk_glfw3_render(NK_ANTI_ALIASING_ON);
glfwSwapBuffers(win);
}
nk_glfw3_shutdown();
glfwTerminate();
return 0;
}