Skip to content

Commit 9b6b98d

Browse files
committed
Add demo for glfw + opengl4 without the use of gl extensions
1 parent 1dcce46 commit 9b6b98d

File tree

7 files changed

+913
-4
lines changed

7 files changed

+913
-4
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ INTRO = HEADER.md
2121
PUB = nuklear.h
2222
OUTPUT = nuklear.h
2323

24-
PRIV1 = nuklear_internal.h nuklear_math.c nuklear_util.c nuklear_color.c nuklear_utf8.c nuklear_buffer.c nuklear_string.c nuklear_draw.c nuklear_vertex.c
24+
PRIV1 = nuklear_internal.h nuklear_math.c nuklear_util.c nuklear_color.c nuklear_utf8.c nuklear_buffer.c nuklear_string.c nuklear_draw.c nuklear_vertex.c
2525

26-
EXTERN = stb_rect_pack.h stb_truetype.h
26+
EXTERN = stb_rect_pack.h stb_truetype.h
2727

2828
PRIV2 = nuklear_font.c nuklear_input.c nuklear_style.c nuklear_context.c nuklear_pool.c nuklear_page_element.c nuklear_table.c nuklear_panel.c nuklear_window.c nuklear_popup.c nuklear_contextual.c nuklear_menu.c nuklear_layout.c nuklear_tree.c nuklear_group.c nuklear_list_view.c nuklear_widget.c nuklear_text.c nuklear_image.c nuklear_9slice.c nuklear_button.c nuklear_toggle.c nuklear_selectable.c nuklear_slider.c nuklear_knob.c nuklear_progress.c nuklear_scrollbar.c nuklear_text_editor.c nuklear_edit.c nuklear_property.c nuklear_chart.c nuklear_color_picker.c nuklear_combo.c nuklear_tooltip.c
2929

@@ -45,7 +45,7 @@ usage:
4545
echo "make demos to build all of the demos"
4646
echo "make all to re-pack the header and create documentation"
4747

48-
all: docs nuke demos
48+
all: docs nuke demos
4949
demos: $(DEMO_LIST)
5050

5151

@@ -62,7 +62,7 @@ nuke: $(addprefix $(SRC_PATH)/, $(SRC))
6262
########################################################################################
6363
## Docs
6464

65-
docs: $(DOCS_PATH)/html/index.html
65+
docs: $(DOCS_PATH)/html/index.html
6666

6767
$(DOCS_PATH)/html/index.html: $(DOCS_PATH)/doxygen-awesome-css/doxygen-awesome.css $(DOXYFILE)
6868
doxygen $(DOXYFILE)
File renamed without changes.

demo/glfw_opengl4_core/main.c

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
/* nuklear - 1.32.0 - public domain */
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <stdint.h>
5+
#include <stdarg.h>
6+
#include <string.h>
7+
#include <math.h>
8+
#include <assert.h>
9+
#include <limits.h>
10+
#include <time.h>
11+
12+
#include <GL/glew.h>
13+
#include <GLFW/glfw3.h>
14+
15+
#define NK_INCLUDE_FIXED_TYPES
16+
#define NK_INCLUDE_STANDARD_IO
17+
#define NK_INCLUDE_STANDARD_VARARGS
18+
#define NK_INCLUDE_DEFAULT_ALLOCATOR
19+
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
20+
#define NK_INCLUDE_FONT_BAKING
21+
#define NK_INCLUDE_DEFAULT_FONT
22+
#define NK_IMPLEMENTATION
23+
#define NK_GLFW_GL4_IMPLEMENTATION
24+
#include "../../nuklear.h"
25+
#include "nuklear_glfw_gl4.h"
26+
27+
#define WINDOW_WIDTH 1200
28+
#define WINDOW_HEIGHT 800
29+
30+
#define MAX_VERTEX_BUFFER 512 * 1024
31+
#define MAX_ELEMENT_BUFFER 128 * 1024
32+
33+
/* ===============================================================
34+
*
35+
* EXAMPLE
36+
*
37+
* ===============================================================*/
38+
/* This are some code examples to provide a small overview of what can be
39+
* done with this library. To try out an example uncomment the defines */
40+
/*#define INCLUDE_ALL */
41+
/*#define INCLUDE_STYLE */
42+
/*#define INCLUDE_CALCULATOR */
43+
/*#define INCLUDE_CANVAS */
44+
#define INCLUDE_OVERVIEW
45+
/*#define INCLUDE_CONFIGURATOR */
46+
/*#define INCLUDE_NODE_EDITOR */
47+
48+
#ifdef INCLUDE_ALL
49+
#define INCLUDE_STYLE
50+
#define INCLUDE_CALCULATOR
51+
#define INCLUDE_CANVAS
52+
#define INCLUDE_OVERVIEW
53+
#define INCLUDE_CONFIGURATOR
54+
#define INCLUDE_NODE_EDITOR
55+
#endif
56+
57+
#ifdef INCLUDE_STYLE
58+
#include "../../demo/common/style.c"
59+
#endif
60+
#ifdef INCLUDE_CALCULATOR
61+
#include "../../demo/common/calculator.c"
62+
#endif
63+
#ifdef INCLUDE_CANVAS
64+
#include "../../demo/common/canvas.c"
65+
#endif
66+
#ifdef INCLUDE_OVERVIEW
67+
#include "../../demo/common/overview.c"
68+
#endif
69+
#ifdef INCLUDE_CONFIGURATOR
70+
#include "../../demo/common/style_configurator.c"
71+
#endif
72+
#ifdef INCLUDE_NODE_EDITOR
73+
#include "../../demo/common/node_editor.c"
74+
#endif
75+
76+
/* ===============================================================
77+
*
78+
* DEMO
79+
*
80+
* ===============================================================*/
81+
static void error_callback(int e, const char *d)
82+
{printf("Error %d: %s\n", e, d);}
83+
84+
int main(void)
85+
{
86+
/* Platform */
87+
static GLFWwindow *win;
88+
int width = 0, height = 0;
89+
struct nk_context *ctx;
90+
struct nk_colorf bg;
91+
struct nk_image img;
92+
93+
#ifdef INCLUDE_CONFIGURATOR
94+
static struct nk_color color_table[NK_COLOR_COUNT];
95+
memcpy(color_table, nk_default_color_style, sizeof(color_table));
96+
#endif
97+
98+
/* GLFW */
99+
glfwSetErrorCallback(error_callback);
100+
if (!glfwInit()) {
101+
fprintf(stdout, "[GFLW] failed to init!\n");
102+
exit(1);
103+
}
104+
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
105+
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
106+
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
107+
#ifdef __APPLE__
108+
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
109+
#endif
110+
win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
111+
glfwMakeContextCurrent(win);
112+
glfwGetWindowSize(win, &width, &height);
113+
114+
/* OpenGL */
115+
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
116+
glewExperimental = 1;
117+
if (glewInit() != GLEW_OK) {
118+
fprintf(stderr, "Failed to setup GLEW\n");
119+
exit(1);
120+
}
121+
ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
122+
/* Load Fonts: if none of these are loaded a default font will be used */
123+
/* Load Cursor: if you uncomment cursor loading please hide the cursor */
124+
{struct nk_font_atlas *atlas;
125+
nk_glfw3_font_stash_begin(&atlas);
126+
/*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
127+
/*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
128+
/*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
129+
/*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
130+
/*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
131+
/*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
132+
nk_glfw3_font_stash_end();
133+
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/
134+
/*nk_style_set_font(ctx, &droid->handle);*/}
135+
136+
{int tex_index = 0;
137+
enum {tex_width = 256, tex_height = 256};
138+
char pixels[tex_width * tex_height * 4];
139+
memset(pixels, 128, sizeof(pixels));
140+
tex_index = nk_glfw3_create_texture(pixels, tex_width, tex_height);
141+
img = nk_image_id(tex_index);}
142+
143+
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
144+
while (!glfwWindowShouldClose(win))
145+
{
146+
/* Input */
147+
glfwPollEvents();
148+
nk_glfw3_new_frame();
149+
150+
/* GUI */
151+
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
152+
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
153+
NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
154+
{
155+
enum {EASY, HARD};
156+
static int op = EASY;
157+
static int property = 20;
158+
nk_layout_row_static(ctx, 30, 80, 1);
159+
if (nk_button_label(ctx, "button"))
160+
fprintf(stdout, "button pressed\n");
161+
162+
nk_layout_row_dynamic(ctx, 30, 2);
163+
if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
164+
if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
165+
166+
nk_layout_row_dynamic(ctx, 25, 1);
167+
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
168+
169+
nk_layout_row_dynamic(ctx, 20, 1);
170+
nk_label(ctx, "background:", NK_TEXT_LEFT);
171+
nk_layout_row_dynamic(ctx, 25, 1);
172+
if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
173+
nk_layout_row_dynamic(ctx, 120, 1);
174+
bg = nk_color_picker(ctx, bg, NK_RGBA);
175+
nk_layout_row_dynamic(ctx, 25, 1);
176+
bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
177+
bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
178+
bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
179+
bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
180+
nk_combo_end(ctx);
181+
}
182+
}
183+
nk_end(ctx);
184+
185+
/* Bindless Texture */
186+
if (nk_begin(ctx, "Texture", nk_rect(250, 150, 230, 250),
187+
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
188+
NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
189+
{
190+
struct nk_command_buffer *canvas = nk_window_get_canvas(ctx);
191+
struct nk_rect total_space = nk_window_get_content_region(ctx);
192+
nk_draw_image(canvas, total_space, &img, nk_white);
193+
}
194+
nk_end(ctx);
195+
196+
/* -------------- EXAMPLES ---------------- */
197+
#ifdef INCLUDE_CALCULATOR
198+
calculator(ctx);
199+
#endif
200+
#ifdef INCLUDE_CANVAS
201+
canvas(ctx);
202+
#endif
203+
#ifdef INCLUDE_OVERVIEW
204+
overview(ctx);
205+
#endif
206+
#ifdef INCLUDE_CONFIGURATOR
207+
style_configurator(ctx, color_table);
208+
#endif
209+
#ifdef INCLUDE_NODE_EDITOR
210+
node_editor(ctx);
211+
#endif
212+
/* ----------------------------------------- */
213+
214+
/* Draw */
215+
glfwGetWindowSize(win, &width, &height);
216+
glViewport(0, 0, width, height);
217+
glClear(GL_COLOR_BUFFER_BIT);
218+
glClearColor(bg.r, bg.g, bg.b, bg.a);
219+
/* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state
220+
* with blending, scissor, face culling, depth test and viewport and
221+
* defaults everything back into a default state.
222+
* Make sure to either a.) save and restore or b.) reset your own state after
223+
* rendering the UI. */
224+
nk_glfw3_render(NK_ANTI_ALIASING_ON);
225+
glfwSwapBuffers(win);
226+
}
227+
nk_glfw3_shutdown();
228+
glfwTerminate();
229+
return 0;
230+
}
231+

0 commit comments

Comments
 (0)