Skip to content

Instantly share code, notes, and snippets.

@vaiorabbit
Created November 26, 2016 15:27
Show Gist options
  • Save vaiorabbit/e36c7c04f7f3dddadf14653f31eb3faf to your computer and use it in GitHub Desktop.
Save vaiorabbit/e36c7c04f7f3dddadf14653f31eb3faf to your computer and use it in GitHub Desktop.
Nuklear : Unicode character test ( based on https://github.com/vurtun/nuklear/blob/master/demo/glfw_opengl3/main.c )
/* -*- encoding: utf-8; -*- */
/* nuklear - v1.17 - public domain */
/* Unicode character test based on:
* https://github.com/vurtun/nuklear/blob/master/demo/glfw_opengl3/main.c */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <math.h>
#include <limits.h>
#include <time.h>
#include <GL/glew.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_GL3_IMPLEMENTATION
#include "../../nuklear.h"
#include "nuklear_glfw_gl3.h"
#define WINDOW_WIDTH 1200
#define WINDOW_HEIGHT 800
#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024
#define UNUSED(a) (void)a
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) < (b) ? (b) : (a))
#define LEN(a) (sizeof(a)/sizeof(a)[0])
/* (Not complete) Glyph range table for Japanese.
* [NOTE] Tables for some Chinese, Cyrillic and Korean are already embedded as 'nk_font_chinese_glyph_ranges', etc.
*/
const nk_rune nk_font_japanese_glyph_ranges[] = {
0x0020, 0x00FF,
0x2200, 0x22FF, // Mathematical Operators
0x3000, 0x303F, // CJK Symbols and Punctuation
0x3040, 0x309F, // Hiragana
0x30A0, 0x30FF, // Katakana
0x0370, 0x03FF, // Greek and Coptic
0xFF00, 0xFFEF, // Halfwidth and Fullwidth Forms
0x4E00, 0x9FFF, // CJK Unified Ideographs
0
};
/* ===============================================================
*
* EXAMPLE
*
* ===============================================================*/
/* This are some code examples to provide a small overview of what can be
* done with this library. To try out an example uncomment the include
* and the corresponding function. */
/*#include "../style.c"*/
/*#include "../calculator.c"*/
/*#include "../overview.c"*/
/*#include "../node_editor.c"*/
/* ===============================================================
*
* DEMO
*
* ===============================================================*/
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;
struct nk_context *ctx;
struct nk_color background;
/* GLFW */
glfwSetErrorCallback(error_callback);
if (!glfwInit()) {
fprintf(stdout, "[GFLW] failed to init!\n");
exit(1);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
glfwMakeContextCurrent(win);
glfwGetWindowSize(win, &width, &height);
/* OpenGL */
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glewExperimental = 1;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to setup GLEW\n");
exit(1);
}
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);
/* >>> Use your favorite font here. */
/* ex.) GenShinGothic : http://jikasei.me/font/genshin/ */
struct nk_font_config conf = nk_font_config(0);
conf.range = &nk_font_japanese_glyph_ranges[0];
struct nk_font *genshin = nk_font_atlas_add_from_file(atlas, "../../extra_font/GenShinGothic-Normal.ttf", 22, &conf);
nk_glfw3_font_stash_end();
nk_style_set_font(ctx, &genshin->handle);}
/* <<< Use your favorite font here. */
/* style.c */
/*set_style(ctx, THEME_WHITE);*/
/*set_style(ctx, THEME_RED);*/
/*set_style(ctx, THEME_BLUE);*/
/*set_style(ctx, THEME_DARK);*/
background = nk_rgb(28,48,62);
while (!glfwWindowShouldClose(win))
{
/* Input */
glfwPollEvents();
nk_glfw3_new_frame();
/* GUI */
if (nk_begin(ctx, "日本語", nk_rect(50, 50, 230, 250),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
{
enum {EASY, HARD};
static int op = EASY;
static int property = 20;
nk_layout_row_static(ctx, 30, 80, 1);
if (nk_button_label(ctx, "ボタン"))
fprintf(stdout, "押されました\n");
nk_layout_row_dynamic(ctx, 30, 2);
if (nk_option_label(ctx, "かんたん", op == EASY)) op = EASY;
if (nk_option_label(ctx, "難しい", op == HARD)) op = HARD;
nk_layout_row_dynamic(ctx, 25, 1);
nk_property_int(ctx, "圧縮率:", 0, &property, 100, 10, 1);
nk_layout_row_dynamic(ctx, 20, 1);
nk_label(ctx, "背景色:", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 25, 1);
if (nk_combo_begin_color(ctx, background, nk_vec2(nk_widget_width(ctx),400))) {
nk_layout_row_dynamic(ctx, 120, 1);
background = nk_color_picker(ctx, background, NK_RGBA);
nk_layout_row_dynamic(ctx, 25, 1);
background.r = (nk_byte)nk_propertyi(ctx, "#赤:", 0, background.r, 255, 1,1);
background.g = (nk_byte)nk_propertyi(ctx, "#緑:", 0, background.g, 255, 1,1);
background.b = (nk_byte)nk_propertyi(ctx, "#青:", 0, background.b, 255, 1,1);
background.a = (nk_byte)nk_propertyi(ctx, "#α:", 0, background.a, 255, 1,1);
nk_combo_end(ctx);
}
}
nk_end(ctx);
/* -------------- EXAMPLES ---------------- */
/*calculator(ctx);*/
/*overview(ctx);*/
/*node_editor(ctx);*/
/* ----------------------------------------- */
/* Draw */
{float bg[4];
nk_color_fv(bg, background);
glfwGetWindowSize(win, &width, &height);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(bg[0], bg[1], bg[2], bg[3]);
/* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state
* with blending, scissor, face culling, depth test and viewport and
* defaults everything back into a default state.
* Make sure to either a.) save and restore or b.) reset your own state after
* rendering the UI. */
nk_glfw3_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
glfwSwapBuffers(win);}
}
nk_glfw3_shutdown();
glfwTerminate();
return 0;
}
@Farsthary
Copy link

Farsthary commented Mar 2, 2018

It does not work for me, I keep getting "?" chars as an output.
Should I set any flag on VisualStudio or include any additional library?
https://ibb.co/fBWjiH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment