Dumped the pure SFML folder. Got it working enough to realize that it just ran too slow to continue wasting time on it. SFML creates a OpenGL context anyway so users can drop in the nk_sfml_gl2.h and not need anything else. I'll have to add this in the readme. Tried to make my code as similar to other demos for sake of consistency.
This commit is contained in:
parent
73b0b945b4
commit
5007519ae7
@ -1,8 +0,0 @@
|
|||||||
SFML_INC = -I C:/Users/Ricky/MinGW-Libs/SFML/include
|
|
||||||
SFML_LIB = -L C:/Users/Ricky/MinGW-Libs/SFML/lib
|
|
||||||
|
|
||||||
LIBS = -DSFML_STATIC -lmingw32 -lsfml-window-s -lsfml-system-s -lopengl32 -lwinmm -lgdi32
|
|
||||||
|
|
||||||
|
|
||||||
build:
|
|
||||||
g++ main.cpp $(SFML_INC) $(SFML_LIB) $(LIBS) -o demo.exe
|
|
@ -1,2 +0,0 @@
|
|||||||
mingw32-make -f Makefile
|
|
||||||
pause
|
|
@ -1,178 +0,0 @@
|
|||||||
/* nuklear - v1.32.0 - public domain */
|
|
||||||
#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 <SFML/Window.hpp>
|
|
||||||
#include <SFML/OpenGL.hpp>
|
|
||||||
|
|
||||||
#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_SFML_GL2_IMPLEMENTATION
|
|
||||||
#include "../../nuklear.h"
|
|
||||||
#include "nuklear_sfml_gl2.h"
|
|
||||||
|
|
||||||
#define WINDOW_WIDTH 800
|
|
||||||
#define WINDOW_HEIGHT 600
|
|
||||||
|
|
||||||
#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])
|
|
||||||
|
|
||||||
/* ===============================================================
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* ===============================================================*/
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
/* Platform */
|
|
||||||
sf::ContextSettings settings(24, 8, 4, 2, 2);
|
|
||||||
sf::Window window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Demo", sf::Style::Default, settings);
|
|
||||||
|
|
||||||
window.setVerticalSyncEnabled(true);
|
|
||||||
window.setActive(true);
|
|
||||||
|
|
||||||
glViewport(0, 0, window.getSize().x, window.getSize().y);
|
|
||||||
struct nk_context *ctx;
|
|
||||||
struct nk_color background;
|
|
||||||
|
|
||||||
/* GUI */
|
|
||||||
ctx = nk_sfml_init(&window);
|
|
||||||
/* 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_sfml_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);*/
|
|
||||||
/*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
|
|
||||||
nk_sfml_font_stash_end();
|
|
||||||
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/
|
|
||||||
/*nk_style_set_font(ctx, &droid->handle);*/
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 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 (window.isOpen())
|
|
||||||
{
|
|
||||||
/* Input */
|
|
||||||
sf::Event event;
|
|
||||||
|
|
||||||
nk_input_begin(ctx);
|
|
||||||
while(window.pollEvent(event))
|
|
||||||
{
|
|
||||||
if(event.type == sf::Event::Closed)
|
|
||||||
window.close();
|
|
||||||
else if(event.type == sf::Event::Resized)
|
|
||||||
{
|
|
||||||
// adjust the viewport when the window is resized
|
|
||||||
glViewport(0, 0, event.size.width, event.size.height);
|
|
||||||
}
|
|
||||||
|
|
||||||
nk_sfml_handle_event(&event);
|
|
||||||
}
|
|
||||||
nk_input_end(ctx);
|
|
||||||
|
|
||||||
/* GUI */
|
|
||||||
if (nk_begin(ctx, "Demo", 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, "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_layout_row_dynamic(ctx, 25, 1);
|
|
||||||
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
|
|
||||||
|
|
||||||
nk_layout_row_dynamic(ctx, 20, 1);
|
|
||||||
nk_label(ctx, "background:", 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, "#R:", 0, background.r, 255, 1,1);
|
|
||||||
background.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, background.g, 255, 1,1);
|
|
||||||
background.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, background.b, 255, 1,1);
|
|
||||||
background.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, background.a, 255, 1,1);
|
|
||||||
nk_combo_end(ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nk_end(ctx);
|
|
||||||
|
|
||||||
/* -------------- EXAMPLES ---------------- */
|
|
||||||
//calculator(ctx);
|
|
||||||
overview(ctx);
|
|
||||||
//node_editor(ctx);
|
|
||||||
/* ----------------------------------------- */
|
|
||||||
|
|
||||||
/* Draw */
|
|
||||||
{
|
|
||||||
window.setActive(true);
|
|
||||||
|
|
||||||
float bg[4];
|
|
||||||
nk_color_fv(bg, background);
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
|
||||||
glClearColor(bg[0], bg[1], bg[2], bg[3]);
|
|
||||||
/* IMPORTANT: `nk_sfml_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_sfml_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
|
|
||||||
|
|
||||||
window.display();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
nk_sfml_shutdown();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
@ -1,582 +0,0 @@
|
|||||||
/*
|
|
||||||
* Nuklear - 1.32.0 - public domain
|
|
||||||
* no warrenty implied; use at your own risk.
|
|
||||||
* authored from 2015-2016 by Micha Mettke
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* ==============================================================
|
|
||||||
*
|
|
||||||
* API
|
|
||||||
*
|
|
||||||
* ===============================================================
|
|
||||||
*/
|
|
||||||
#ifndef NK_SFML_H_
|
|
||||||
#define NK_SFML_H_
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <SFML/Graphics.hpp>
|
|
||||||
|
|
||||||
NK_API struct nk_context* nk_sfml_init(NkSFMLFont* sfmlfont, sf::RenderWindow* window, sf::View view);
|
|
||||||
NK_API int nk_sfml_handle_event(sf::Event* event);
|
|
||||||
NK_API void nk_sfml_render(enum nk_anti_aliasing, int max_vertex_buffer, int max_element_buffer);
|
|
||||||
NK_API void nk_sfml_shutdown(void);
|
|
||||||
|
|
||||||
typedef struct NkSFMLFont;
|
|
||||||
NK_API NkSFMLFont* nk_sfml_font_create(const std::string& file_name, int font_size, int flags);
|
|
||||||
NK_API void nk_sfml_font_delete(NkSFMLFont);
|
|
||||||
NK_API void nk_sfml_font_set(NkSFMLFont);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
/*
|
|
||||||
* ==============================================================
|
|
||||||
*
|
|
||||||
* IMPLEMENTATION
|
|
||||||
*
|
|
||||||
* ===============================================================
|
|
||||||
*/
|
|
||||||
#ifdef NK_SFML_IMPLEMENTATION
|
|
||||||
|
|
||||||
sf::Shape& RoundedRectangle(float rectWidth, float rectHeight, float radius)
|
|
||||||
{
|
|
||||||
sf::Shape round_rect;
|
|
||||||
rect->SetOutlineWidth(Outline);
|
|
||||||
|
|
||||||
float a = 0.0f;
|
|
||||||
float b = 0.0f;
|
|
||||||
|
|
||||||
for(int i=0; i<POINTS; i++)
|
|
||||||
{
|
|
||||||
X+=radius/POINTS;
|
|
||||||
Y=sqrt(radius*radius-X*X);
|
|
||||||
rrect->AddPoint(X+x+rectWidth-radius,y-Y+radius,Col,OutlineCol);
|
|
||||||
}
|
|
||||||
Y=0;
|
|
||||||
for(int i=0; i<POINTS; i++)
|
|
||||||
{
|
|
||||||
Y+=radius/POINTS;
|
|
||||||
X=sqrt(radius*radius-Y*Y);
|
|
||||||
rrect->AddPoint(x+rectWidth+X-radius,y+rectHeight-radius+Y,Col,OutlineCol);
|
|
||||||
}
|
|
||||||
X=0;
|
|
||||||
for(int i=0; i<POINTS; i++)
|
|
||||||
{
|
|
||||||
X+=radius/POINTS;
|
|
||||||
Y=sqrt(radius*radius-X*X);
|
|
||||||
rrect->AddPoint(x+radius-X,y+rectHeight-radius+Y,Col,OutlineCol);
|
|
||||||
}
|
|
||||||
Y=0;
|
|
||||||
for(int i=0; i<POINTS; i++)
|
|
||||||
{
|
|
||||||
Y+=radius/POINTS;
|
|
||||||
X=sqrt(radius*radius-Y*Y);
|
|
||||||
rrect->AddPoint(x-X+radius,y+radius-Y,Col,OutlineCol);
|
|
||||||
}
|
|
||||||
return *rrect;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
struct NkSFMLFont
|
|
||||||
{
|
|
||||||
struct nk_user_font nk;
|
|
||||||
int height;
|
|
||||||
sf::Font font;
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct nk_sfml
|
|
||||||
{
|
|
||||||
sf::RenderWindow* window;
|
|
||||||
sf::View view;
|
|
||||||
struct nk_context ctx;
|
|
||||||
struct nk_buffer cmds;
|
|
||||||
} sfml;
|
|
||||||
|
|
||||||
NK_API NkSFMLFont*
|
|
||||||
nk_sfml_font_create(const std::string& file_name)
|
|
||||||
{
|
|
||||||
NkSFMLFont* font = (NkSFMLFont*)calloc(1, sizeof(NkSFMLFont));
|
|
||||||
|
|
||||||
if(!font->font.loadFromFile(file_name))
|
|
||||||
{
|
|
||||||
fprintf(stdout, "Unable to load font file: %s\n", file_name);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// I really need to think of a better way of doing this
|
|
||||||
sf::Text text("abjdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", font->font);
|
|
||||||
font->height = text.getGlobalBounds().height;
|
|
||||||
|
|
||||||
return font;
|
|
||||||
}
|
|
||||||
|
|
||||||
static float
|
|
||||||
nk_sfml_font_get_text_width(nk_handle handle, float height, const char* string, int len)
|
|
||||||
{
|
|
||||||
NkSFMLFont* font = (NkSFMLFont*)handle.ptr;
|
|
||||||
|
|
||||||
if(!font || !string)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
sf::Text sfmltext(font->font, string);
|
|
||||||
|
|
||||||
//char strcpy[len + 1];
|
|
||||||
//strncpy((char*)&strcpy, text, len);
|
|
||||||
//strcpy[len] = '\0';
|
|
||||||
|
|
||||||
return sfmltext.getGlobalBounds().width;
|
|
||||||
}
|
|
||||||
|
|
||||||
NK_API void
|
|
||||||
nk_sfml_font_set(NkSFMLFont* sfmlfont)
|
|
||||||
{
|
|
||||||
struct nk_user_font* font = &sfmlfont->nk;
|
|
||||||
font->userdata = nk_handle_ptr(sfmlfont);
|
|
||||||
font->height = (float)sfmlfont->height;
|
|
||||||
font->width = nk_sfml_font_get_text_width;
|
|
||||||
nk_style_set_font(&sfml.ctx, font);
|
|
||||||
}
|
|
||||||
|
|
||||||
NK_API void
|
|
||||||
nk_sfml_font_del(NkSFMLFont* font)
|
|
||||||
{
|
|
||||||
if(!font)
|
|
||||||
return;
|
|
||||||
|
|
||||||
free(font);
|
|
||||||
}
|
|
||||||
|
|
||||||
static sf::Color
|
|
||||||
nk_color_to_sfml(struct nk_color color)
|
|
||||||
{
|
|
||||||
return sf::Color(color.r, color.g, color.b, color.a);
|
|
||||||
}
|
|
||||||
|
|
||||||
NK_API void
|
|
||||||
nk_sfml_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
|
|
||||||
{
|
|
||||||
const struct nk_command *cmd;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
nk_foreach(cmd, &sfml.ctx)
|
|
||||||
{
|
|
||||||
sf::Color color;
|
|
||||||
|
|
||||||
switch (cmd->type)
|
|
||||||
{
|
|
||||||
case NK_COMMAND_NOP: break;
|
|
||||||
case NK_COMMAND_SCISSOR:
|
|
||||||
{
|
|
||||||
const struct nk_command_scissor *s = (const struct nk_command_scissor*)cmd;
|
|
||||||
|
|
||||||
|
|
||||||
//sf::RenderTexture clip_tex((unsigned int)s->x, (unsigned int)s->y);
|
|
||||||
//sf::Sprite clip;
|
|
||||||
sf::View view;
|
|
||||||
view.setCenter(s->x, s->y);
|
|
||||||
view.setSize(s->w, s->h);
|
|
||||||
sfml.window->setView(view);
|
|
||||||
//al_set_clipping_rectangle((int)s->x, (int)s->y, (int)s->w, (int)s->h);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_LINE:
|
|
||||||
{
|
|
||||||
const struct nk_command_line *l = (const struct nk_command_line *)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(l->color);
|
|
||||||
|
|
||||||
sf::Vertex line[] =
|
|
||||||
{
|
|
||||||
sf::Vertex(sf::Vector2f((float)l->begin.x - l->line_thickness, (float)l->begin.y + l->line_thickness), color),
|
|
||||||
sf::Vertex(sf::Vector2f((float)l->begin.x + l->line_thickness, (float)l->begin.y - l->line_thickness), color),
|
|
||||||
sf::Vertex(sf::Vector2f((float)l->end.x + l->line_thickness, (float)l->end.y - l->line_thickness), color),
|
|
||||||
sf::Vertex(sf::Vector2f((float)l->end.x - l->line_thickness, (float)l->end.y + l->line_thickness), color)
|
|
||||||
};
|
|
||||||
|
|
||||||
sfml.window->draw(line, sf::Quads);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_RECT:
|
|
||||||
{
|
|
||||||
const struct nk_command_rect *r = (const struct nk_command_rect *)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(l->color);
|
|
||||||
|
|
||||||
sf::RectangleShape rect;
|
|
||||||
rect.setSize(sf::Vector2f(()))
|
|
||||||
|
|
||||||
sfml.window->draw(line, sf::Quads);
|
|
||||||
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(r->color);
|
|
||||||
al_draw_rounded_rectangle(
|
|
||||||
(float)r->x, (float)r->y, (float)(r->x + r->w),
|
|
||||||
(float)(r->y + r->h), (float)r->rounding, (float)r->rounding, color,
|
|
||||||
(float)r->line_thickness);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_RECT_FILLED:
|
|
||||||
{
|
|
||||||
const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(r->color);
|
|
||||||
al_draw_filled_rounded_rectangle(
|
|
||||||
(float)r->x, (float)r->y,
|
|
||||||
(float)(r->x + r->w), (float)(r->y + r->h), (float)r->rounding,
|
|
||||||
(float)r->rounding, color);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_CIRCLE:
|
|
||||||
{
|
|
||||||
const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(c->color);
|
|
||||||
float xr, yr;
|
|
||||||
xr = (float)c->w/2;
|
|
||||||
yr = (float)c->h/2;
|
|
||||||
al_draw_ellipse(
|
|
||||||
((float)(c->x)) + xr, ((float)c->y) + yr,
|
|
||||||
xr, yr, color, (float)c->line_thickness);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_CIRCLE_FILLED:
|
|
||||||
{
|
|
||||||
const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(c->color);
|
|
||||||
float xr, yr;
|
|
||||||
xr = (float)c->w/2;
|
|
||||||
yr = (float)c->h/2;
|
|
||||||
al_draw_filled_ellipse(
|
|
||||||
((float)(c->x)) + xr, ((float)c->y) + yr,
|
|
||||||
xr, yr, color);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_TRIANGLE:
|
|
||||||
{
|
|
||||||
const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(t->color);
|
|
||||||
al_draw_triangle(
|
|
||||||
(float)t->a.x, (float)t->a.y, (float)t->b.x, (float)t->b.y,
|
|
||||||
(float)t->c.x, (float)t->c.y, color, (float)t->line_thickness);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_TRIANGLE_FILLED:
|
|
||||||
{
|
|
||||||
const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(t->color);
|
|
||||||
al_draw_filled_triangle(
|
|
||||||
(float)t->a.x, (float)t->a.y, (float)t->b.x,
|
|
||||||
(float)t->b.y, (float)t->c.x, (float)t->c.y, color);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_POLYGON:
|
|
||||||
{
|
|
||||||
const struct nk_command_polygon *p = (const struct nk_command_polygon*)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(p->color);
|
|
||||||
int i;
|
|
||||||
float vertices[p->point_count * 2];
|
|
||||||
|
|
||||||
for (i = 0; i < p->point_count; i++)
|
|
||||||
{
|
|
||||||
vertices[i*2] = p->points[i].x;
|
|
||||||
vertices[(i*2) + 1] = p->points[i].y;
|
|
||||||
}
|
|
||||||
|
|
||||||
al_draw_polyline(
|
|
||||||
(const float*)&vertices, (2 * sizeof(float)),
|
|
||||||
(int)p->point_count, ALLEGRO_LINE_JOIN_ROUND, ALLEGRO_LINE_CAP_CLOSED,
|
|
||||||
color, (float)p->line_thickness, 0.0);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_POLYGON_FILLED:
|
|
||||||
{
|
|
||||||
const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(p->color);
|
|
||||||
int i;
|
|
||||||
float vertices[p->point_count * 2];
|
|
||||||
|
|
||||||
for (i = 0; i < p->point_count; i++)
|
|
||||||
{
|
|
||||||
vertices[i*2] = p->points[i].x;
|
|
||||||
vertices[(i*2) + 1] = p->points[i].y;
|
|
||||||
}
|
|
||||||
|
|
||||||
al_draw_filled_polygon((const float*)&vertices, (int)p->point_count, color);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_POLYLINE:
|
|
||||||
{
|
|
||||||
const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(p->color);
|
|
||||||
int i;
|
|
||||||
float vertices[p->point_count * 2];
|
|
||||||
|
|
||||||
for (i = 0; i < p->point_count; i++)
|
|
||||||
{
|
|
||||||
vertices[i*2] = p->points[i].x;
|
|
||||||
vertices[(i*2) + 1] = p->points[i].y;
|
|
||||||
}
|
|
||||||
|
|
||||||
al_draw_polyline(
|
|
||||||
(const float*)&vertices, (2 * sizeof(float)),
|
|
||||||
(int)p->point_count, ALLEGRO_LINE_JOIN_ROUND, ALLEGRO_LINE_CAP_ROUND,
|
|
||||||
color, (float)p->line_thickness, 0.0);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_TEXT:
|
|
||||||
{
|
|
||||||
const struct nk_command_text *t = (const struct nk_command_text*)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(t->foreground);
|
|
||||||
NkAllegro5Font *font = (NkAllegro5Font*)t->font->userdata.ptr;
|
|
||||||
|
|
||||||
al_draw_text(
|
|
||||||
font->font,
|
|
||||||
color, (float)t->x, (float)t->y, 0,
|
|
||||||
(const char*)t->string);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_CURVE:
|
|
||||||
{
|
|
||||||
const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(q->color);
|
|
||||||
float points[8];
|
|
||||||
points[0] = (float)q->begin.x;
|
|
||||||
points[1] = (float)q->begin.y;
|
|
||||||
points[2] = (float)q->ctrl[0].x;
|
|
||||||
points[3] = (float)q->ctrl[0].y;
|
|
||||||
points[4] = (float)q->ctrl[1].x;
|
|
||||||
points[5] = (float)q->ctrl[1].y;
|
|
||||||
points[6] = (float)q->end.x;
|
|
||||||
points[7] = (float)q->end.y;
|
|
||||||
al_draw_spline(points, color, (float)q->line_thickness);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_ARC:
|
|
||||||
{
|
|
||||||
const struct nk_command_arc *a = (const struct nk_command_arc *)cmd;
|
|
||||||
|
|
||||||
color = nk_color_to_sfml(a->color);
|
|
||||||
al_draw_arc(
|
|
||||||
(float)a->cx, (float)a->cy, (float)a->r, a->a[0],
|
|
||||||
a->a[1], color, (float)a->line_thickness);
|
|
||||||
} break;
|
|
||||||
case NK_COMMAND_RECT_MULTI_COLOR:
|
|
||||||
case NK_COMMAND_IMAGE:
|
|
||||||
case NK_COMMAND_ARC_FILLED:
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nk_clear(&allegro5.ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
nk_sfml_clipboard_paste(nk_handle usr, struct nk_text_edit* edit)
|
|
||||||
{
|
|
||||||
/* Not Implemented in SFML
|
|
||||||
sf::Clipboard clipboard(sfml.window);
|
|
||||||
const char* text = clipboard.getText();
|
|
||||||
|
|
||||||
if(text)
|
|
||||||
nk_textedit_paste(edit, text, nk_strlen(text));
|
|
||||||
(void)usr;
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
nk_sfml_clipboard_copy(nk_handle usr, const char* text, int len)
|
|
||||||
{
|
|
||||||
char* str = 0;
|
|
||||||
(void)usr;
|
|
||||||
if(!len)
|
|
||||||
return;
|
|
||||||
str = (char*)malloc((size_t)len+1);
|
|
||||||
if(!str)
|
|
||||||
return;
|
|
||||||
memcpy(str, text, (size_t)len);
|
|
||||||
str[len] = '\0';
|
|
||||||
|
|
||||||
/* Not Implemented in SFML
|
|
||||||
sf::Clipboard clipboard(sfml.window);
|
|
||||||
clipboard.setText(str);
|
|
||||||
*/
|
|
||||||
|
|
||||||
free(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
NK_API struct nk_context*
|
|
||||||
nk_sfml_init(NkSFMLFont* sfmlfont, sf::RenderWindow* window, sf::View view)
|
|
||||||
{
|
|
||||||
struct nk_user_font* font = &sfmlfont->nk;
|
|
||||||
font->userdata = nk_handle_ptr(sfmlfont);
|
|
||||||
font->height = (float)sfmlfont->height;
|
|
||||||
font->width = nk_sfml_get_text_width;
|
|
||||||
|
|
||||||
sfml.window = window;
|
|
||||||
sfml.view = view;
|
|
||||||
|
|
||||||
nk_init_default(&sfml.ctx, font);
|
|
||||||
sfml.ctx.clip.copy = nk_sfml_clipboard_copy;
|
|
||||||
sfml.ctx.clip.paste = nk_sfml_clipboard_paste;
|
|
||||||
sfml.ctx.clip.userdata = nk_handle_ptr(0);
|
|
||||||
return &sfml.ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
NK_API int
|
|
||||||
nk_sfml_handle_event(sf::Event* event)
|
|
||||||
{
|
|
||||||
struct nk_context* ctx = &sfml.ctx;
|
|
||||||
|
|
||||||
/* optional grabbing behavior */
|
|
||||||
if(ctx->input.mouse.grab)
|
|
||||||
{
|
|
||||||
sfml.window->setMouseCursorGrabbed(true);
|
|
||||||
ctx->input.mouse.grab = 0;
|
|
||||||
}
|
|
||||||
else if(ctx->input.mouse.ungrab)
|
|
||||||
{
|
|
||||||
int x = (int)ctx->input.mouse.prev.x;
|
|
||||||
int y = (int)ctx->input.mouse.prev.y;
|
|
||||||
sfml.window->setMouseCursorGrabbed(false);
|
|
||||||
sf::Mouse::setPosition(sf::Vector2i(x, y));
|
|
||||||
ctx->input.mouse.ungrab = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(event->type == sf::Event::KeyReleased || event->type == sf::Event::KeyPressed)
|
|
||||||
{
|
|
||||||
int down = event->type == sf::Event::KeyPressed;
|
|
||||||
sf::Keyboard::Key key = event->key.code;
|
|
||||||
|
|
||||||
if(key == sf::Keyboard::RShift || key == sf::Keyboard::LShift)
|
|
||||||
nk_input_key(ctx, NK_KEY_SHIFT, down);
|
|
||||||
else if(key == sf::Keyboard::Delete)
|
|
||||||
nk_input_key(ctx, NK_KEY_DEL, down);
|
|
||||||
else if(key == sf::Keyboard::Return)
|
|
||||||
nk_input_key(ctx, NK_KEY_ENTER, down);
|
|
||||||
else if(key == sf::Keyboard::Tab)
|
|
||||||
nk_input_key(ctx, NK_KEY_TAB, down);
|
|
||||||
else if(key == sf::Keyboard::BackSpace)
|
|
||||||
nk_input_key(ctx, NK_KEY_BACKSPACE, down);
|
|
||||||
else if(key == sf::Keyboard::Home)
|
|
||||||
{
|
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_START, down);
|
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_START, down);
|
|
||||||
}
|
|
||||||
else if(key == sf::Keyboard::End)
|
|
||||||
{
|
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_END, down);
|
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_END, down);
|
|
||||||
}
|
|
||||||
else if(key == sf::Keyboard::PageDown)
|
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
|
|
||||||
else if(key == sf::Keyboard::PageUp)
|
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
|
|
||||||
else if(key == sf::Keyboard::Z)
|
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
|
||||||
else if(key == sf::Keyboard::R)
|
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_REDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
|
||||||
else if(key == sf::Keyboard::C)
|
|
||||||
nk_input_key(ctx, NK_KEY_COPY, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
|
||||||
else if(key == sf::Keyboard::V)
|
|
||||||
nk_input_key(ctx, NK_KEY_PASTE, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
|
||||||
else if(key == sf::Keyboard::X)
|
|
||||||
nk_input_key(ctx, NK_KEY_CUT, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
|
||||||
else if(key == sf::Keyboard::B)
|
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
|
||||||
else if(key == sf::Keyboard::E)
|
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
|
||||||
else if(key == sf::Keyboard::Up)
|
|
||||||
nk_input_key(ctx, NK_KEY_UP, down);
|
|
||||||
else if(key == sf::Keyboard::Down)
|
|
||||||
nk_input_key(ctx, NK_KEY_DOWN, down);
|
|
||||||
else if(key == sf::Keyboard::Left)
|
|
||||||
{
|
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
|
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
|
|
||||||
else
|
|
||||||
nk_input_key(ctx, NK_KEY_LEFT, down);
|
|
||||||
}
|
|
||||||
else if(key == sf::Keyboard::Right)
|
|
||||||
{
|
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
|
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
|
|
||||||
else
|
|
||||||
nk_input_key(ctx, NK_KEY_RIGHT, down);
|
|
||||||
}
|
|
||||||
else return 0;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if(event->type == sf::Event::MouseButtonPressed || event->type == sf::Event::MouseButtonReleased)
|
|
||||||
{
|
|
||||||
int down = event->type == sf::Event::MouseButtonPressed;
|
|
||||||
const int x = event->mouseButton.x;
|
|
||||||
const int y = event->mouseButton.y;
|
|
||||||
|
|
||||||
if(event->mouseButton.button == sf::Mouse::Left)
|
|
||||||
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
|
|
||||||
if(event->mouseButton.button == sf::Mouse::Middle)
|
|
||||||
nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
|
|
||||||
if(event->mouseButton.button == sf::Mouse::Right)
|
|
||||||
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if(event->type == sf::Event::MouseMoved)
|
|
||||||
{
|
|
||||||
if(ctx->input.mouse.grabbed)
|
|
||||||
{
|
|
||||||
int x = (int)ctx->input.mouse.prev.x + event->mouseMove.x;
|
|
||||||
int y = (int)ctx->input.mouse.prev.y + event->mouseMove.y;
|
|
||||||
|
|
||||||
nk_input_motion(ctx, x, y);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
nk_input_motion(ctx, event->mouseMove.x, event->mouseMove.y);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
/* For Android*/
|
|
||||||
else if(event->type == sf::Event::TouchBegan || event->type == sf::Event::TouchEnded)
|
|
||||||
{
|
|
||||||
int down = event->type == sf::Event::TouchBegan;
|
|
||||||
const int x = event->touch.x;
|
|
||||||
const int y = event->touch.y;
|
|
||||||
|
|
||||||
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if(event->type == sf::Event::TouchMoved)
|
|
||||||
{
|
|
||||||
if(ctx->input.mouse.grabbed)
|
|
||||||
{
|
|
||||||
int x = (int)ctx->input.mouse.prev.x;
|
|
||||||
int y = (int)ctx->input.mouse.prev.y;
|
|
||||||
|
|
||||||
nk_input_motion(ctx, x + event->touch.x, y + event->touch.y);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
nk_input_motion(ctx, event->touch.x, event->touch.y);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if(event->type == sf::Event::TextEntered)
|
|
||||||
{
|
|
||||||
nk_input_unicode(ctx, event->text.unicode);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if(event->type == sf::Event::MouseWheelScrolled)
|
|
||||||
{
|
|
||||||
nk_input_scroll(ctx, event->mouseWheelScroll.delta);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
NK_API
|
|
||||||
void nk_sfml_shutdown(void)
|
|
||||||
{
|
|
||||||
nk_free(&sfml.ctx);
|
|
||||||
memset(&sfml, 0, sizeof(sfml));
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
@ -11,7 +11,6 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
#include <SFML/OpenGL.hpp>
|
|
||||||
|
|
||||||
#define NK_INCLUDE_FIXED_TYPES
|
#define NK_INCLUDE_FIXED_TYPES
|
||||||
#define NK_INCLUDE_STANDARD_IO
|
#define NK_INCLUDE_STANDARD_IO
|
||||||
@ -25,8 +24,8 @@
|
|||||||
#include "../../nuklear.h"
|
#include "../../nuklear.h"
|
||||||
#include "nuklear_sfml_gl2.h"
|
#include "nuklear_sfml_gl2.h"
|
||||||
|
|
||||||
#define WINDOW_WIDTH 800
|
#define WINDOW_WIDTH 1200
|
||||||
#define WINDOW_HEIGHT 600
|
#define WINDOW_HEIGHT 800
|
||||||
|
|
||||||
#define MAX_VERTEX_BUFFER 512 * 1024
|
#define MAX_VERTEX_BUFFER 512 * 1024
|
||||||
#define MAX_ELEMENT_BUFFER 128 * 1024
|
#define MAX_ELEMENT_BUFFER 128 * 1024
|
||||||
@ -44,10 +43,10 @@
|
|||||||
/* This are some code examples to provide a small overview of what can be
|
/* 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
|
* done with this library. To try out an example uncomment the include
|
||||||
* and the corresponding function. */
|
* and the corresponding function. */
|
||||||
//#include "../style.c"
|
/*#include "../style.c"*/
|
||||||
//#include "../calculator.c"
|
/*#include "../calculator.c"*/
|
||||||
#include "../overview.c"
|
/*#include "../overview.c"*/
|
||||||
//#include "../node_editor.c"
|
/*#include "../node_editor.c"*/
|
||||||
|
|
||||||
/* ===============================================================
|
/* ===============================================================
|
||||||
*
|
*
|
||||||
@ -104,10 +103,7 @@ int main(void)
|
|||||||
if(event.type == sf::Event::Closed)
|
if(event.type == sf::Event::Closed)
|
||||||
window.close();
|
window.close();
|
||||||
else if(event.type == sf::Event::Resized)
|
else if(event.type == sf::Event::Resized)
|
||||||
{
|
|
||||||
// adjust the viewport when the window is resized
|
|
||||||
glViewport(0, 0, event.size.width, event.size.height);
|
glViewport(0, 0, event.size.width, event.size.height);
|
||||||
}
|
|
||||||
|
|
||||||
nk_sfml_handle_event(&event);
|
nk_sfml_handle_event(&event);
|
||||||
}
|
}
|
||||||
@ -149,9 +145,9 @@ int main(void)
|
|||||||
nk_end(ctx);
|
nk_end(ctx);
|
||||||
|
|
||||||
/* -------------- EXAMPLES ---------------- */
|
/* -------------- EXAMPLES ---------------- */
|
||||||
//calculator(ctx);
|
/*calculator(ctx);*/
|
||||||
overview(ctx);
|
/*overview(ctx);*/
|
||||||
//node_editor(ctx);
|
/*node_editor(ctx);*/
|
||||||
/* ----------------------------------------- */
|
/* ----------------------------------------- */
|
||||||
|
|
||||||
/* Draw */
|
/* Draw */
|
||||||
@ -162,6 +158,7 @@ int main(void)
|
|||||||
nk_color_fv(bg, background);
|
nk_color_fv(bg, background);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glClearColor(bg[0], bg[1], bg[2], bg[3]);
|
glClearColor(bg[0], bg[1], bg[2], bg[3]);
|
||||||
|
|
||||||
/* IMPORTANT: `nk_sfml_render` modifies some global OpenGL state
|
/* IMPORTANT: `nk_sfml_render` modifies some global OpenGL state
|
||||||
* with blending, scissor, face culling and depth test and defaults everything
|
* with blending, scissor, face culling and depth test and defaults everything
|
||||||
* back into a default state. Make sure to either save and restore or
|
* back into a default state. Make sure to either save and restore or
|
||||||
|
@ -15,12 +15,12 @@
|
|||||||
|
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
|
|
||||||
NK_API struct nk_context* nk_sfml_init(sf::Window* window);
|
NK_API struct nk_context* nk_sfml_init(sf::Window* window);
|
||||||
NK_API void nk_sfml_font_stash_begin(struct nk_font_atlas** atlas);
|
NK_API void nk_sfml_font_stash_begin(struct nk_font_atlas** atlas);
|
||||||
NK_API void nk_sfml_font_stash_end(void);
|
NK_API void nk_sfml_font_stash_end(void);
|
||||||
NK_API int nk_sfml_handle_event(sf::Event* event);
|
NK_API int nk_sfml_handle_event(sf::Event* event);
|
||||||
NK_API void nk_sfml_render(enum nk_anti_aliasing, int max_vertex_buffer, int max_element_buffer);
|
NK_API void nk_sfml_render(enum nk_anti_aliasing, int max_vertex_buffer, int max_element_buffer);
|
||||||
NK_API void nk_sfml_shutdown(void);
|
NK_API void nk_sfml_shutdown(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
/*
|
/*
|
||||||
@ -34,383 +34,383 @@ NK_API void nk_sfml_shutdown(void);
|
|||||||
|
|
||||||
struct nk_sfml_device
|
struct nk_sfml_device
|
||||||
{
|
{
|
||||||
struct nk_buffer cmds;
|
struct nk_buffer cmds;
|
||||||
struct nk_draw_null_texture null;
|
struct nk_draw_null_texture null;
|
||||||
GLuint font_tex;
|
GLuint font_tex;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct nk_sfml_vertex
|
struct nk_sfml_vertex
|
||||||
{
|
{
|
||||||
float position[2];
|
float position[2];
|
||||||
float uv[2];
|
float uv[2];
|
||||||
nk_byte col[4];
|
nk_byte col[4];
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct nk_sfml
|
static struct nk_sfml
|
||||||
{
|
{
|
||||||
sf::Window* window;
|
sf::Window* window;
|
||||||
struct nk_sfml_device ogl;
|
struct nk_sfml_device ogl;
|
||||||
struct nk_context ctx;
|
struct nk_context ctx;
|
||||||
struct nk_font_atlas atlas;
|
struct nk_font_atlas atlas;
|
||||||
} sfml;
|
} sfml;
|
||||||
|
|
||||||
NK_INTERN void
|
NK_INTERN void
|
||||||
nk_sfml_device_upload_atlas(const void* image, int width, int height)
|
nk_sfml_device_upload_atlas(const void* image, int width, int height)
|
||||||
{
|
{
|
||||||
struct nk_sfml_device* dev = &sfml.ogl;
|
struct nk_sfml_device* dev = &sfml.ogl;
|
||||||
glGenTextures(1, &dev->font_tex);
|
glGenTextures(1, &dev->font_tex);
|
||||||
glBindTexture(GL_TEXTURE_2D, dev->font_tex);
|
glBindTexture(GL_TEXTURE_2D, dev->font_tex);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
|
||||||
GL_RGBA, GL_UNSIGNED_BYTE, image);
|
GL_RGBA, GL_UNSIGNED_BYTE, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API void
|
NK_API void
|
||||||
nk_sfml_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
|
nk_sfml_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
|
||||||
{
|
{
|
||||||
/* setup global state */
|
/* setup global state */
|
||||||
struct nk_sfml_device* dev = &sfml.ogl;
|
struct nk_sfml_device* dev = &sfml.ogl;
|
||||||
|
|
||||||
int window_width = sfml.window->getSize().x;
|
int window_width = sfml.window->getSize().x;
|
||||||
int window_height = sfml.window->getSize().y;
|
int window_height = sfml.window->getSize().y;
|
||||||
|
|
||||||
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
|
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
|
||||||
glDisable(GL_CULL_FACE);
|
glDisable(GL_CULL_FACE);
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
glEnable(GL_SCISSOR_TEST);
|
glEnable(GL_SCISSOR_TEST);
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
|
||||||
glViewport(0, 0, (GLsizei)window_width, (GLsizei)window_height);
|
glViewport(0, 0, (GLsizei)window_width, (GLsizei)window_height);
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glOrtho(0.0f, window_width, window_height, 0.0f, -1.0f, 1.0f);
|
glOrtho(0.0f, window_width, window_height, 0.0f, -1.0f, 1.0f);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
glEnableClientState(GL_COLOR_ARRAY);
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
{
|
{
|
||||||
GLsizei vs = sizeof(struct nk_sfml_vertex);
|
GLsizei vs = sizeof(struct nk_sfml_vertex);
|
||||||
size_t vp = offsetof(struct nk_sfml_vertex, position);
|
size_t vp = offsetof(struct nk_sfml_vertex, position);
|
||||||
size_t vt = offsetof(struct nk_sfml_vertex, uv);
|
size_t vt = offsetof(struct nk_sfml_vertex, uv);
|
||||||
size_t vc = offsetof(struct nk_sfml_vertex, col);
|
size_t vc = offsetof(struct nk_sfml_vertex, col);
|
||||||
|
|
||||||
/* convert from command queue into draw list and draw to screen */
|
/* convert from command queue into draw list and draw to screen */
|
||||||
const struct nk_draw_command* cmd;
|
const struct nk_draw_command* cmd;
|
||||||
const nk_draw_index* offset = NULL;
|
const nk_draw_index* offset = NULL;
|
||||||
struct nk_buffer vbuf, ebuf;
|
struct nk_buffer vbuf, ebuf;
|
||||||
|
|
||||||
/* fill converting configuration */
|
/* fill converting configuration */
|
||||||
struct nk_convert_config config;
|
struct nk_convert_config config;
|
||||||
static const struct nk_draw_vertex_layout_element vertex_layout[] =
|
static const struct nk_draw_vertex_layout_element vertex_layout[] =
|
||||||
{
|
{
|
||||||
{NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sfml_vertex, position)},
|
{NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sfml_vertex, position)},
|
||||||
{NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sfml_vertex, uv)},
|
{NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sfml_vertex, uv)},
|
||||||
{NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_sfml_vertex, col)},
|
{NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_sfml_vertex, col)},
|
||||||
{NK_VERTEX_LAYOUT_END}
|
{NK_VERTEX_LAYOUT_END}
|
||||||
};
|
};
|
||||||
NK_MEMSET(&config, 0, sizeof(config));
|
NK_MEMSET(&config, 0, sizeof(config));
|
||||||
config.vertex_layout = vertex_layout;
|
config.vertex_layout = vertex_layout;
|
||||||
config.vertex_size = sizeof(struct nk_sfml_vertex);
|
config.vertex_size = sizeof(struct nk_sfml_vertex);
|
||||||
config.vertex_alignment = NK_ALIGNOF(struct nk_sfml_vertex);
|
config.vertex_alignment = NK_ALIGNOF(struct nk_sfml_vertex);
|
||||||
config.null = dev->null;
|
config.null = dev->null;
|
||||||
config.circle_segment_count = 22;
|
config.circle_segment_count = 22;
|
||||||
config.curve_segment_count = 22;
|
config.curve_segment_count = 22;
|
||||||
config.arc_segment_count = 22;
|
config.arc_segment_count = 22;
|
||||||
config.global_alpha = 1.0f;
|
config.global_alpha = 1.0f;
|
||||||
config.shape_AA = AA;
|
config.shape_AA = AA;
|
||||||
config.line_AA = AA;
|
config.line_AA = AA;
|
||||||
|
|
||||||
/* convert shapes into vertices */
|
/* convert shapes into vertices */
|
||||||
nk_buffer_init_default(&vbuf);
|
nk_buffer_init_default(&vbuf);
|
||||||
nk_buffer_init_default(&ebuf);
|
nk_buffer_init_default(&ebuf);
|
||||||
nk_convert(&sfml.ctx, &dev->cmds, &vbuf, &ebuf, &config);
|
nk_convert(&sfml.ctx, &dev->cmds, &vbuf, &ebuf, &config);
|
||||||
|
|
||||||
/* setup vertex buffer pointer */
|
/* setup vertex buffer pointer */
|
||||||
{
|
{
|
||||||
const void* vertices = nk_buffer_memory_const(&vbuf);
|
const void* vertices = nk_buffer_memory_const(&vbuf);
|
||||||
glVertexPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vp));
|
glVertexPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vp));
|
||||||
glTexCoordPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vt));
|
glTexCoordPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vt));
|
||||||
glColorPointer(4, GL_UNSIGNED_BYTE, vs, (const void*)((const nk_byte*)vertices + vc));
|
glColorPointer(4, GL_UNSIGNED_BYTE, vs, (const void*)((const nk_byte*)vertices + vc));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* iterate over and execute each draw command */
|
/* iterate over and execute each draw command */
|
||||||
offset = (const nk_draw_index*)nk_buffer_memory_const(&ebuf);
|
offset = (const nk_draw_index*)nk_buffer_memory_const(&ebuf);
|
||||||
nk_draw_foreach(cmd, &sfml.ctx, &dev->cmds)
|
nk_draw_foreach(cmd, &sfml.ctx, &dev->cmds)
|
||||||
{
|
{
|
||||||
if(!cmd->elem_count) continue;
|
if(!cmd->elem_count) continue;
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
|
glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
|
||||||
glScissor(
|
glScissor(
|
||||||
(GLint)(cmd->clip_rect.x),
|
(GLint)(cmd->clip_rect.x),
|
||||||
(GLint)((window_height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h))),
|
(GLint)((window_height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h))),
|
||||||
(GLint)(cmd->clip_rect.w),
|
(GLint)(cmd->clip_rect.w),
|
||||||
(GLint)(cmd->clip_rect.h));
|
(GLint)(cmd->clip_rect.h));
|
||||||
glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
|
glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
|
||||||
offset += cmd->elem_count;
|
offset += cmd->elem_count;
|
||||||
}
|
}
|
||||||
nk_clear(&sfml.ctx);
|
nk_clear(&sfml.ctx);
|
||||||
nk_buffer_free(&vbuf);
|
nk_buffer_free(&vbuf);
|
||||||
nk_buffer_free(&ebuf);
|
nk_buffer_free(&ebuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* default OpenGL state */
|
/* default OpenGL state */
|
||||||
glDisableClientState(GL_VERTEX_ARRAY);
|
glDisableClientState(GL_VERTEX_ARRAY);
|
||||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
glDisableClientState(GL_COLOR_ARRAY);
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
|
|
||||||
glDisable(GL_CULL_FACE);
|
glDisable(GL_CULL_FACE);
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
glDisable(GL_SCISSOR_TEST);
|
glDisable(GL_SCISSOR_TEST);
|
||||||
glDisable(GL_BLEND);
|
glDisable(GL_BLEND);
|
||||||
glDisable(GL_TEXTURE_2D);
|
glDisable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
glPopAttrib();
|
glPopAttrib();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
nk_sfml_clipboard_paste(nk_handle usr, struct nk_text_edit* edit)
|
nk_sfml_clipboard_paste(nk_handle usr, struct nk_text_edit* edit)
|
||||||
{
|
{
|
||||||
/* Not Implemented in SFML
|
/* Not Implemented in SFML
|
||||||
sf::Clipboard clipboard(sfml.window);
|
sf::Clipboard clipboard(sfml.window);
|
||||||
const char* text = clipboard.getText();
|
const char* text = clipboard.getText();
|
||||||
|
|
||||||
if(text)
|
if(text)
|
||||||
nk_textedit_paste(edit, text, nk_strlen(text));
|
nk_textedit_paste(edit, text, nk_strlen(text));
|
||||||
(void)usr;
|
(void)usr;
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
nk_sfml_clipboard_copy(nk_handle usr, const char* text, int len)
|
nk_sfml_clipboard_copy(nk_handle usr, const char* text, int len)
|
||||||
{
|
{
|
||||||
char* str = 0;
|
char* str = 0;
|
||||||
(void)usr;
|
(void)usr;
|
||||||
if(!len)
|
if(!len)
|
||||||
return;
|
return;
|
||||||
str = (char*)malloc((size_t)len+1);
|
str = (char*)malloc((size_t)len+1);
|
||||||
if(!str)
|
if(!str)
|
||||||
return;
|
return;
|
||||||
memcpy(str, text, (size_t)len);
|
memcpy(str, text, (size_t)len);
|
||||||
str[len] = '\0';
|
str[len] = '\0';
|
||||||
|
|
||||||
/* Not Implemented in SFML
|
/* Not Implemented in SFML
|
||||||
sf::Clipboard clipboard(sfml.window);
|
sf::Clipboard clipboard(sfml.window);
|
||||||
clipboard.setText(str);
|
clipboard.setText(str);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
free(str);
|
free(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API struct nk_context*
|
NK_API struct nk_context*
|
||||||
nk_sfml_init(sf::Window* window)
|
nk_sfml_init(sf::Window* window)
|
||||||
{
|
{
|
||||||
sfml.window = window;
|
sfml.window = window;
|
||||||
nk_init_default(&sfml.ctx, 0);
|
nk_init_default(&sfml.ctx, 0);
|
||||||
sfml.ctx.clip.copy = nk_sfml_clipboard_copy;
|
sfml.ctx.clip.copy = nk_sfml_clipboard_copy;
|
||||||
sfml.ctx.clip.paste = nk_sfml_clipboard_paste;
|
sfml.ctx.clip.paste = nk_sfml_clipboard_paste;
|
||||||
sfml.ctx.clip.userdata = nk_handle_ptr(0);
|
sfml.ctx.clip.userdata = nk_handle_ptr(0);
|
||||||
nk_buffer_init_default(&sfml.ogl.cmds);
|
nk_buffer_init_default(&sfml.ogl.cmds);
|
||||||
return &sfml.ctx;
|
return &sfml.ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API void
|
NK_API void
|
||||||
nk_sfml_font_stash_begin(struct nk_font_atlas** atlas)
|
nk_sfml_font_stash_begin(struct nk_font_atlas** atlas)
|
||||||
{
|
{
|
||||||
nk_font_atlas_init_default(&sfml.atlas);
|
nk_font_atlas_init_default(&sfml.atlas);
|
||||||
nk_font_atlas_begin(&sfml.atlas);
|
nk_font_atlas_begin(&sfml.atlas);
|
||||||
*atlas = &sfml.atlas;
|
*atlas = &sfml.atlas;
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API void
|
NK_API void
|
||||||
nk_sfml_font_stash_end(void)
|
nk_sfml_font_stash_end(void)
|
||||||
{
|
{
|
||||||
const void* image;
|
const void* image;
|
||||||
int w, h;
|
int w, h;
|
||||||
image = nk_font_atlas_bake(&sfml.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
image = nk_font_atlas_bake(&sfml.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||||
nk_sfml_device_upload_atlas(image, w, h);
|
nk_sfml_device_upload_atlas(image, w, h);
|
||||||
nk_font_atlas_end(&sfml.atlas, nk_handle_id((int)sfml.ogl.font_tex), &sfml.ogl.null);
|
nk_font_atlas_end(&sfml.atlas, nk_handle_id((int)sfml.ogl.font_tex), &sfml.ogl.null);
|
||||||
if(sfml.atlas.default_font)
|
if(sfml.atlas.default_font)
|
||||||
nk_style_set_font(&sfml.ctx, &sfml.atlas.default_font->handle);
|
nk_style_set_font(&sfml.ctx, &sfml.atlas.default_font->handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API int
|
NK_API int
|
||||||
nk_sfml_handle_event(sf::Event* event)
|
nk_sfml_handle_event(sf::Event* event)
|
||||||
{
|
{
|
||||||
struct nk_context* ctx = &sfml.ctx;
|
struct nk_context* ctx = &sfml.ctx;
|
||||||
|
|
||||||
/* optional grabbing behavior */
|
/* optional grabbing behavior */
|
||||||
if(ctx->input.mouse.grab)
|
if(ctx->input.mouse.grab)
|
||||||
{
|
{
|
||||||
sfml.window->setMouseCursorGrabbed(true);
|
sfml.window->setMouseCursorGrabbed(true);
|
||||||
ctx->input.mouse.grab = 0;
|
ctx->input.mouse.grab = 0;
|
||||||
}
|
}
|
||||||
else if(ctx->input.mouse.ungrab)
|
else if(ctx->input.mouse.ungrab)
|
||||||
{
|
{
|
||||||
int x = (int)ctx->input.mouse.prev.x;
|
int x = (int)ctx->input.mouse.prev.x;
|
||||||
int y = (int)ctx->input.mouse.prev.y;
|
int y = (int)ctx->input.mouse.prev.y;
|
||||||
sfml.window->setMouseCursorGrabbed(false);
|
sfml.window->setMouseCursorGrabbed(false);
|
||||||
sf::Mouse::setPosition(sf::Vector2i(x, y));
|
sf::Mouse::setPosition(sf::Vector2i(x, y));
|
||||||
ctx->input.mouse.ungrab = 0;
|
ctx->input.mouse.ungrab = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(event->type == sf::Event::KeyReleased || event->type == sf::Event::KeyPressed)
|
if(event->type == sf::Event::KeyReleased || event->type == sf::Event::KeyPressed)
|
||||||
{
|
{
|
||||||
int down = event->type == sf::Event::KeyPressed;
|
int down = event->type == sf::Event::KeyPressed;
|
||||||
sf::Keyboard::Key key = event->key.code;
|
sf::Keyboard::Key key = event->key.code;
|
||||||
|
|
||||||
if(key == sf::Keyboard::RShift || key == sf::Keyboard::LShift)
|
if(key == sf::Keyboard::RShift || key == sf::Keyboard::LShift)
|
||||||
nk_input_key(ctx, NK_KEY_SHIFT, down);
|
nk_input_key(ctx, NK_KEY_SHIFT, down);
|
||||||
else if(key == sf::Keyboard::Delete)
|
else if(key == sf::Keyboard::Delete)
|
||||||
nk_input_key(ctx, NK_KEY_DEL, down);
|
nk_input_key(ctx, NK_KEY_DEL, down);
|
||||||
else if(key == sf::Keyboard::Return)
|
else if(key == sf::Keyboard::Return)
|
||||||
nk_input_key(ctx, NK_KEY_ENTER, down);
|
nk_input_key(ctx, NK_KEY_ENTER, down);
|
||||||
else if(key == sf::Keyboard::Tab)
|
else if(key == sf::Keyboard::Tab)
|
||||||
nk_input_key(ctx, NK_KEY_TAB, down);
|
nk_input_key(ctx, NK_KEY_TAB, down);
|
||||||
else if(key == sf::Keyboard::BackSpace)
|
else if(key == sf::Keyboard::BackSpace)
|
||||||
nk_input_key(ctx, NK_KEY_BACKSPACE, down);
|
nk_input_key(ctx, NK_KEY_BACKSPACE, down);
|
||||||
else if(key == sf::Keyboard::Home)
|
else if(key == sf::Keyboard::Home)
|
||||||
{
|
{
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_START, down);
|
nk_input_key(ctx, NK_KEY_TEXT_START, down);
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_START, down);
|
nk_input_key(ctx, NK_KEY_SCROLL_START, down);
|
||||||
}
|
}
|
||||||
else if(key == sf::Keyboard::End)
|
else if(key == sf::Keyboard::End)
|
||||||
{
|
{
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_END, down);
|
nk_input_key(ctx, NK_KEY_TEXT_END, down);
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_END, down);
|
nk_input_key(ctx, NK_KEY_SCROLL_END, down);
|
||||||
}
|
}
|
||||||
else if(key == sf::Keyboard::PageDown)
|
else if(key == sf::Keyboard::PageDown)
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
|
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
|
||||||
else if(key == sf::Keyboard::PageUp)
|
else if(key == sf::Keyboard::PageUp)
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
|
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
|
||||||
else if(key == sf::Keyboard::Z)
|
else if(key == sf::Keyboard::Z)
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::R)
|
else if(key == sf::Keyboard::R)
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_REDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_TEXT_REDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::C)
|
else if(key == sf::Keyboard::C)
|
||||||
nk_input_key(ctx, NK_KEY_COPY, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_COPY, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::V)
|
else if(key == sf::Keyboard::V)
|
||||||
nk_input_key(ctx, NK_KEY_PASTE, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_PASTE, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::X)
|
else if(key == sf::Keyboard::X)
|
||||||
nk_input_key(ctx, NK_KEY_CUT, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_CUT, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::B)
|
else if(key == sf::Keyboard::B)
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::E)
|
else if(key == sf::Keyboard::E)
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::Up)
|
else if(key == sf::Keyboard::Up)
|
||||||
nk_input_key(ctx, NK_KEY_UP, down);
|
nk_input_key(ctx, NK_KEY_UP, down);
|
||||||
else if(key == sf::Keyboard::Down)
|
else if(key == sf::Keyboard::Down)
|
||||||
nk_input_key(ctx, NK_KEY_DOWN, down);
|
nk_input_key(ctx, NK_KEY_DOWN, down);
|
||||||
else if(key == sf::Keyboard::Left)
|
else if(key == sf::Keyboard::Left)
|
||||||
{
|
{
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
|
nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
|
||||||
else
|
else
|
||||||
nk_input_key(ctx, NK_KEY_LEFT, down);
|
nk_input_key(ctx, NK_KEY_LEFT, down);
|
||||||
}
|
}
|
||||||
else if(key == sf::Keyboard::Right)
|
else if(key == sf::Keyboard::Right)
|
||||||
{
|
{
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
|
nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
|
||||||
else
|
else
|
||||||
nk_input_key(ctx, NK_KEY_RIGHT, down);
|
nk_input_key(ctx, NK_KEY_RIGHT, down);
|
||||||
}
|
}
|
||||||
else return 0;
|
else return 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if(event->type == sf::Event::MouseButtonPressed || event->type == sf::Event::MouseButtonReleased)
|
else if(event->type == sf::Event::MouseButtonPressed || event->type == sf::Event::MouseButtonReleased)
|
||||||
{
|
{
|
||||||
int down = event->type == sf::Event::MouseButtonPressed;
|
int down = event->type == sf::Event::MouseButtonPressed;
|
||||||
const int x = event->mouseButton.x;
|
const int x = event->mouseButton.x;
|
||||||
const int y = event->mouseButton.y;
|
const int y = event->mouseButton.y;
|
||||||
|
|
||||||
if(event->mouseButton.button == sf::Mouse::Left)
|
if(event->mouseButton.button == sf::Mouse::Left)
|
||||||
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
|
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
|
||||||
if(event->mouseButton.button == sf::Mouse::Middle)
|
if(event->mouseButton.button == sf::Mouse::Middle)
|
||||||
nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
|
nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
|
||||||
if(event->mouseButton.button == sf::Mouse::Right)
|
if(event->mouseButton.button == sf::Mouse::Right)
|
||||||
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
|
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if(event->type == sf::Event::MouseMoved)
|
else if(event->type == sf::Event::MouseMoved)
|
||||||
{
|
{
|
||||||
if(ctx->input.mouse.grabbed)
|
if(ctx->input.mouse.grabbed)
|
||||||
{
|
{
|
||||||
int x = (int)ctx->input.mouse.prev.x + event->mouseMove.x;
|
int x = (int)ctx->input.mouse.prev.x + event->mouseMove.x;
|
||||||
int y = (int)ctx->input.mouse.prev.y + event->mouseMove.y;
|
int y = (int)ctx->input.mouse.prev.y + event->mouseMove.y;
|
||||||
|
|
||||||
nk_input_motion(ctx, x, y);
|
nk_input_motion(ctx, x, y);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
nk_input_motion(ctx, event->mouseMove.x, event->mouseMove.y);
|
nk_input_motion(ctx, event->mouseMove.x, event->mouseMove.y);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
/* For Android*/
|
/* For Android*/
|
||||||
else if(event->type == sf::Event::TouchBegan || event->type == sf::Event::TouchEnded)
|
else if(event->type == sf::Event::TouchBegan || event->type == sf::Event::TouchEnded)
|
||||||
{
|
{
|
||||||
int down = event->type == sf::Event::TouchBegan;
|
int down = event->type == sf::Event::TouchBegan;
|
||||||
const int x = event->touch.x;
|
const int x = event->touch.x;
|
||||||
const int y = event->touch.y;
|
const int y = event->touch.y;
|
||||||
|
|
||||||
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
|
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if(event->type == sf::Event::TouchMoved)
|
else if(event->type == sf::Event::TouchMoved)
|
||||||
{
|
{
|
||||||
if(ctx->input.mouse.grabbed)
|
if(ctx->input.mouse.grabbed)
|
||||||
{
|
{
|
||||||
int x = (int)ctx->input.mouse.prev.x;
|
int x = (int)ctx->input.mouse.prev.x;
|
||||||
int y = (int)ctx->input.mouse.prev.y;
|
int y = (int)ctx->input.mouse.prev.y;
|
||||||
|
|
||||||
nk_input_motion(ctx, x + event->touch.x, y + event->touch.y);
|
nk_input_motion(ctx, x + event->touch.x, y + event->touch.y);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
nk_input_motion(ctx, event->touch.x, event->touch.y);
|
nk_input_motion(ctx, event->touch.x, event->touch.y);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if(event->type == sf::Event::TextEntered)
|
else if(event->type == sf::Event::TextEntered)
|
||||||
{
|
{
|
||||||
nk_input_unicode(ctx, event->text.unicode);
|
nk_input_unicode(ctx, event->text.unicode);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if(event->type == sf::Event::MouseWheelScrolled)
|
else if(event->type == sf::Event::MouseWheelScrolled)
|
||||||
{
|
{
|
||||||
nk_input_scroll(ctx, event->mouseWheelScroll.delta);
|
nk_input_scroll(ctx, event->mouseWheelScroll.delta);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API
|
NK_API
|
||||||
void nk_sfml_shutdown(void)
|
void nk_sfml_shutdown(void)
|
||||||
{
|
{
|
||||||
struct nk_sfml_device* dev = &sfml.ogl;
|
struct nk_sfml_device* dev = &sfml.ogl;
|
||||||
nk_font_atlas_clear(&sfml.atlas);
|
nk_font_atlas_clear(&sfml.atlas);
|
||||||
nk_free(&sfml.ctx);
|
nk_free(&sfml.ctx);
|
||||||
glDeleteTextures(1, &dev->font_tex);
|
glDeleteTextures(1, &dev->font_tex);
|
||||||
nk_buffer_free(&dev->cmds);
|
nk_buffer_free(&dev->cmds);
|
||||||
memset(&sfml, 0, sizeof(sfml));
|
memset(&sfml, 0, sizeof(sfml));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -24,8 +24,8 @@
|
|||||||
#include "../../nuklear.h"
|
#include "../../nuklear.h"
|
||||||
#include "nuklear_sfml_gl3.h"
|
#include "nuklear_sfml_gl3.h"
|
||||||
|
|
||||||
#define WINDOW_WIDTH 800
|
#define WINDOW_WIDTH 1200
|
||||||
#define WINDOW_HEIGHT 600
|
#define WINDOW_HEIGHT 800
|
||||||
|
|
||||||
#define MAX_VERTEX_BUFFER 512 * 1024
|
#define MAX_VERTEX_BUFFER 512 * 1024
|
||||||
#define MAX_ELEMENT_BUFFER 128 * 1024
|
#define MAX_ELEMENT_BUFFER 128 * 1024
|
||||||
@ -43,10 +43,10 @@
|
|||||||
/* This are some code examples to provide a small overview of what can be
|
/* 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
|
* done with this library. To try out an example uncomment the include
|
||||||
* and the corresponding function. */
|
* and the corresponding function. */
|
||||||
//#include "../style.c"
|
/*#include "../style.c"*/
|
||||||
//#include "../calculator.c"
|
/*#include "../calculator.c"*/
|
||||||
#include "../overview.c"
|
/*#include "../overview.c"*/
|
||||||
//#include "../node_editor.c"
|
/*#include "../node_editor.c"*/
|
||||||
|
|
||||||
/* ===============================================================
|
/* ===============================================================
|
||||||
*
|
*
|
||||||
@ -62,6 +62,7 @@ int main(void)
|
|||||||
window.setVerticalSyncEnabled(true);
|
window.setVerticalSyncEnabled(true);
|
||||||
window.setActive(true);
|
window.setActive(true);
|
||||||
|
|
||||||
|
/* Load OpenGL extensions */
|
||||||
if(!gladLoadGL())
|
if(!gladLoadGL())
|
||||||
{
|
{
|
||||||
printf("Failed to load OpenGL extensions!\n");
|
printf("Failed to load OpenGL extensions!\n");
|
||||||
@ -109,10 +110,7 @@ int main(void)
|
|||||||
if(event.type == sf::Event::Closed)
|
if(event.type == sf::Event::Closed)
|
||||||
window.close();
|
window.close();
|
||||||
else if(event.type == sf::Event::Resized)
|
else if(event.type == sf::Event::Resized)
|
||||||
{
|
|
||||||
// adjust the viewport when the window is resized
|
|
||||||
glViewport(0, 0, event.size.width, event.size.height);
|
glViewport(0, 0, event.size.width, event.size.height);
|
||||||
}
|
|
||||||
|
|
||||||
nk_sfml_handle_event(&event);
|
nk_sfml_handle_event(&event);
|
||||||
}
|
}
|
||||||
@ -154,9 +152,9 @@ int main(void)
|
|||||||
nk_end(ctx);
|
nk_end(ctx);
|
||||||
|
|
||||||
/* -------------- EXAMPLES ---------------- */
|
/* -------------- EXAMPLES ---------------- */
|
||||||
//calculator(ctx);
|
/*calculator(ctx);*/
|
||||||
overview(ctx);
|
/*overview(ctx);*/
|
||||||
//node_editor(ctx);
|
/*node_editor(ctx);*/
|
||||||
/* ----------------------------------------- */
|
/* ----------------------------------------- */
|
||||||
|
|
||||||
/* Draw */
|
/* Draw */
|
||||||
@ -167,6 +165,7 @@ int main(void)
|
|||||||
nk_color_fv(bg, background);
|
nk_color_fv(bg, background);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glClearColor(bg[0], bg[1], bg[2], bg[3]);
|
glClearColor(bg[0], bg[1], bg[2], bg[3]);
|
||||||
|
|
||||||
/* IMPORTANT: `nk_sfml_render` modifies some global OpenGL state
|
/* IMPORTANT: `nk_sfml_render` modifies some global OpenGL state
|
||||||
* with blending, scissor, face culling and depth test and defaults everything
|
* with blending, scissor, face culling and depth test and defaults everything
|
||||||
* back into a default state. Make sure to either save and restore or
|
* back into a default state. Make sure to either save and restore or
|
||||||
|
@ -13,18 +13,21 @@
|
|||||||
#ifndef NK_SFML_GL3_H_
|
#ifndef NK_SFML_GL3_H_
|
||||||
#define NK_SFML_GL3_H_
|
#define NK_SFML_GL3_H_
|
||||||
|
|
||||||
|
/* Feel free to edit here and include your own extension wrangler */
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
/* I use GLAD but you can use GLEW or what you like */
|
||||||
|
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
|
|
||||||
NK_API struct nk_context* nk_sfml_init(sf::Window* window);
|
NK_API struct nk_context* nk_sfml_init(sf::Window* window);
|
||||||
NK_API void nk_sfml_font_stash_begin(struct nk_font_atlas** atlas);
|
NK_API void nk_sfml_font_stash_begin(struct nk_font_atlas** atlas);
|
||||||
NK_API void nk_sfml_font_stash_end(void);
|
NK_API void nk_sfml_font_stash_end(void);
|
||||||
NK_API int nk_sfml_handle_event(sf::Event* event);
|
NK_API int nk_sfml_handle_event(sf::Event* event);
|
||||||
NK_API void nk_sfml_render(enum nk_anti_aliasing, int max_vertex_buffer, int max_element_buffer);
|
NK_API void nk_sfml_render(enum nk_anti_aliasing, int max_vertex_buffer, int max_element_buffer);
|
||||||
NK_API void nk_sfml_shutdown(void);
|
NK_API void nk_sfml_shutdown(void);
|
||||||
|
|
||||||
NK_API void nk_sfml_device_create(void);
|
NK_API void nk_sfml_device_create(void);
|
||||||
NK_API void nk_sfml_device_destroy(void);
|
NK_API void nk_sfml_device_destroy(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
/*
|
/*
|
||||||
@ -40,9 +43,9 @@ NK_API void nk_sfml_device_destroy(void);
|
|||||||
|
|
||||||
struct nk_sfml_device
|
struct nk_sfml_device
|
||||||
{
|
{
|
||||||
struct nk_buffer cmds;
|
struct nk_buffer cmds;
|
||||||
struct nk_draw_null_texture null;
|
struct nk_draw_null_texture null;
|
||||||
GLuint vbo, vao, ebo;
|
GLuint vbo, vao, ebo;
|
||||||
GLuint prog;
|
GLuint prog;
|
||||||
GLuint vert_shdr;
|
GLuint vert_shdr;
|
||||||
GLuint frag_shdr;
|
GLuint frag_shdr;
|
||||||
@ -51,22 +54,22 @@ struct nk_sfml_device
|
|||||||
GLint attrib_col;
|
GLint attrib_col;
|
||||||
GLint uniform_tex;
|
GLint uniform_tex;
|
||||||
GLint uniform_proj;
|
GLint uniform_proj;
|
||||||
GLuint font_tex;
|
GLuint font_tex;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct nk_sfml_vertex
|
struct nk_sfml_vertex
|
||||||
{
|
{
|
||||||
float position[2];
|
float position[2];
|
||||||
float uv[2];
|
float uv[2];
|
||||||
nk_byte col[4];
|
nk_byte col[4];
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct nk_sfml
|
static struct nk_sfml
|
||||||
{
|
{
|
||||||
sf::Window* window;
|
sf::Window* window;
|
||||||
struct nk_sfml_device ogl;
|
struct nk_sfml_device ogl;
|
||||||
struct nk_context ctx;
|
struct nk_context ctx;
|
||||||
struct nk_font_atlas atlas;
|
struct nk_font_atlas atlas;
|
||||||
} sfml;
|
} sfml;
|
||||||
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
@ -78,9 +81,9 @@ static struct nk_sfml
|
|||||||
NK_API void
|
NK_API void
|
||||||
nk_sfml_device_create(void)
|
nk_sfml_device_create(void)
|
||||||
{
|
{
|
||||||
GLint status;
|
GLint status;
|
||||||
static const GLchar* vertex_shader =
|
static const GLchar* vertex_shader =
|
||||||
NK_SHADER_VERSION
|
NK_SHADER_VERSION
|
||||||
"uniform mat4 ProjMtx;\n"
|
"uniform mat4 ProjMtx;\n"
|
||||||
"in vec2 Position;\n"
|
"in vec2 Position;\n"
|
||||||
"in vec2 TexCoord;\n"
|
"in vec2 TexCoord;\n"
|
||||||
@ -167,42 +170,42 @@ nk_sfml_device_create(void)
|
|||||||
NK_API void
|
NK_API void
|
||||||
nk_sfml_device_destroy(void)
|
nk_sfml_device_destroy(void)
|
||||||
{
|
{
|
||||||
struct nk_sfml_device* dev = &sfml.ogl;
|
struct nk_sfml_device* dev = &sfml.ogl;
|
||||||
|
|
||||||
glDetachShader(dev->prog, dev->vert_shdr);
|
glDetachShader(dev->prog, dev->vert_shdr);
|
||||||
glDetachShader(dev->prog, dev->frag_shdr);
|
glDetachShader(dev->prog, dev->frag_shdr);
|
||||||
glDeleteShader(dev->vert_shdr);
|
glDeleteShader(dev->vert_shdr);
|
||||||
glDeleteShader(dev->vert_shdr);
|
glDeleteShader(dev->vert_shdr);
|
||||||
glDeleteProgram(dev->prog);
|
glDeleteProgram(dev->prog);
|
||||||
glDeleteTextures(1, &dev->font_tex);
|
glDeleteTextures(1, &dev->font_tex);
|
||||||
glDeleteBuffers(1, &dev->vbo);
|
glDeleteBuffers(1, &dev->vbo);
|
||||||
glDeleteBuffers(1, &dev->ebo);
|
glDeleteBuffers(1, &dev->ebo);
|
||||||
nk_buffer_free(&dev->cmds);
|
nk_buffer_free(&dev->cmds);
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_INTERN void
|
NK_INTERN void
|
||||||
nk_sfml_device_upload_atlas(const void* image, int width, int height)
|
nk_sfml_device_upload_atlas(const void* image, int width, int height)
|
||||||
{
|
{
|
||||||
struct nk_sfml_device* dev = &sfml.ogl;
|
struct nk_sfml_device* dev = &sfml.ogl;
|
||||||
glGenTextures(1, &dev->font_tex);
|
glGenTextures(1, &dev->font_tex);
|
||||||
glBindTexture(GL_TEXTURE_2D, dev->font_tex);
|
glBindTexture(GL_TEXTURE_2D, dev->font_tex);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
|
||||||
GL_RGBA, GL_UNSIGNED_BYTE, image);
|
GL_RGBA, GL_UNSIGNED_BYTE, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API void
|
NK_API void
|
||||||
nk_sfml_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
|
nk_sfml_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
|
||||||
{
|
{
|
||||||
/* setup global state */
|
/* setup global state */
|
||||||
struct nk_sfml_device* dev = &sfml.ogl;
|
struct nk_sfml_device* dev = &sfml.ogl;
|
||||||
|
|
||||||
int window_width = sfml.window->getSize().x;
|
int window_width = sfml.window->getSize().x;
|
||||||
int window_height = sfml.window->getSize().y;
|
int window_height = sfml.window->getSize().y;
|
||||||
|
|
||||||
GLfloat ortho[4][4] =
|
GLfloat ortho[4][4] =
|
||||||
{
|
{
|
||||||
{2.0f, 0.0f, 0.0f, 0.0f},
|
{2.0f, 0.0f, 0.0f, 0.0f},
|
||||||
{0.0f,-2.0f, 0.0f, 0.0f},
|
{0.0f,-2.0f, 0.0f, 0.0f},
|
||||||
{0.0f, 0.0f,-1.0f, 0.0f},
|
{0.0f, 0.0f,-1.0f, 0.0f},
|
||||||
@ -269,10 +272,10 @@ nk_sfml_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_
|
|||||||
|
|
||||||
/* setup buffers to load vertices and elements */
|
/* setup buffers to load vertices and elements */
|
||||||
{
|
{
|
||||||
struct nk_buffer vbuf, ebuf;
|
struct nk_buffer vbuf, ebuf;
|
||||||
nk_buffer_init_fixed(&vbuf, vertices, (nk_size)max_vertex_buffer);
|
nk_buffer_init_fixed(&vbuf, vertices, (nk_size)max_vertex_buffer);
|
||||||
nk_buffer_init_fixed(&ebuf, elements, (nk_size)max_element_buffer);
|
nk_buffer_init_fixed(&ebuf, elements, (nk_size)max_element_buffer);
|
||||||
nk_convert(&sfml.ctx, &dev->cmds, &vbuf, &ebuf, &config);
|
nk_convert(&sfml.ctx, &dev->cmds, &vbuf, &ebuf, &config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,7 +289,7 @@ nk_sfml_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_
|
|||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
|
glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
|
||||||
glScissor(
|
glScissor(
|
||||||
(GLint)(cmd->clip_rect.x),
|
(GLint)(cmd->clip_rect.x),
|
||||||
(GLint)((window_height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h))),
|
(GLint)((window_height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h))),
|
||||||
(GLint)(cmd->clip_rect.w),
|
(GLint)(cmd->clip_rect.w),
|
||||||
(GLint)(cmd->clip_rect.h));
|
(GLint)(cmd->clip_rect.h));
|
||||||
@ -307,234 +310,234 @@ nk_sfml_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_
|
|||||||
static void
|
static void
|
||||||
nk_sfml_clipboard_paste(nk_handle usr, struct nk_text_edit* edit)
|
nk_sfml_clipboard_paste(nk_handle usr, struct nk_text_edit* edit)
|
||||||
{
|
{
|
||||||
/* Not Implemented in SFML
|
/* Not Implemented in SFML
|
||||||
sf::Clipboard clipboard(sfml.window);
|
sf::Clipboard clipboard(sfml.window);
|
||||||
const char* text = clipboard.getText();
|
const char* text = clipboard.getText();
|
||||||
|
|
||||||
if(text)
|
if(text)
|
||||||
nk_textedit_paste(edit, text, nk_strlen(text));
|
nk_textedit_paste(edit, text, nk_strlen(text));
|
||||||
(void)usr;
|
(void)usr;
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
nk_sfml_clipboard_copy(nk_handle usr, const char* text, int len)
|
nk_sfml_clipboard_copy(nk_handle usr, const char* text, int len)
|
||||||
{
|
{
|
||||||
char* str = 0;
|
char* str = 0;
|
||||||
(void)usr;
|
(void)usr;
|
||||||
if(!len)
|
if(!len)
|
||||||
return;
|
return;
|
||||||
str = (char*)malloc((size_t)len+1);
|
str = (char*)malloc((size_t)len+1);
|
||||||
if(!str)
|
if(!str)
|
||||||
return;
|
return;
|
||||||
memcpy(str, text, (size_t)len);
|
memcpy(str, text, (size_t)len);
|
||||||
str[len] = '\0';
|
str[len] = '\0';
|
||||||
|
|
||||||
/* Not Implemented in SFML
|
/* Not Implemented in SFML
|
||||||
sf::Clipboard clipboard(sfml.window);
|
sf::Clipboard clipboard(sfml.window);
|
||||||
clipboard.setText(str);
|
clipboard.setText(str);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
free(str);
|
free(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API struct nk_context*
|
NK_API struct nk_context*
|
||||||
nk_sfml_init(sf::Window* window)
|
nk_sfml_init(sf::Window* window)
|
||||||
{
|
{
|
||||||
sfml.window = window;
|
sfml.window = window;
|
||||||
nk_init_default(&sfml.ctx, 0);
|
nk_init_default(&sfml.ctx, 0);
|
||||||
sfml.ctx.clip.copy = nk_sfml_clipboard_copy;
|
sfml.ctx.clip.copy = nk_sfml_clipboard_copy;
|
||||||
sfml.ctx.clip.paste = nk_sfml_clipboard_paste;
|
sfml.ctx.clip.paste = nk_sfml_clipboard_paste;
|
||||||
sfml.ctx.clip.userdata = nk_handle_ptr(0);
|
sfml.ctx.clip.userdata = nk_handle_ptr(0);
|
||||||
//nk_buffer_init_default(&sfml.ogl.cmds);
|
//nk_buffer_init_default(&sfml.ogl.cmds);
|
||||||
nk_sfml_device_create();
|
nk_sfml_device_create();
|
||||||
return &sfml.ctx;
|
return &sfml.ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API void
|
NK_API void
|
||||||
nk_sfml_font_stash_begin(struct nk_font_atlas** atlas)
|
nk_sfml_font_stash_begin(struct nk_font_atlas** atlas)
|
||||||
{
|
{
|
||||||
nk_font_atlas_init_default(&sfml.atlas);
|
nk_font_atlas_init_default(&sfml.atlas);
|
||||||
nk_font_atlas_begin(&sfml.atlas);
|
nk_font_atlas_begin(&sfml.atlas);
|
||||||
*atlas = &sfml.atlas;
|
*atlas = &sfml.atlas;
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API void
|
NK_API void
|
||||||
nk_sfml_font_stash_end(void)
|
nk_sfml_font_stash_end(void)
|
||||||
{
|
{
|
||||||
const void* image;
|
const void* image;
|
||||||
int w, h;
|
int w, h;
|
||||||
image = nk_font_atlas_bake(&sfml.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
image = nk_font_atlas_bake(&sfml.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||||
nk_sfml_device_upload_atlas(image, w, h);
|
nk_sfml_device_upload_atlas(image, w, h);
|
||||||
nk_font_atlas_end(&sfml.atlas, nk_handle_id((int)sfml.ogl.font_tex), &sfml.ogl.null);
|
nk_font_atlas_end(&sfml.atlas, nk_handle_id((int)sfml.ogl.font_tex), &sfml.ogl.null);
|
||||||
if(sfml.atlas.default_font)
|
if(sfml.atlas.default_font)
|
||||||
nk_style_set_font(&sfml.ctx, &sfml.atlas.default_font->handle);
|
nk_style_set_font(&sfml.ctx, &sfml.atlas.default_font->handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API int
|
NK_API int
|
||||||
nk_sfml_handle_event(sf::Event* event)
|
nk_sfml_handle_event(sf::Event* event)
|
||||||
{
|
{
|
||||||
struct nk_context* ctx = &sfml.ctx;
|
struct nk_context* ctx = &sfml.ctx;
|
||||||
|
|
||||||
/* optional grabbing behavior */
|
/* optional grabbing behavior */
|
||||||
if(ctx->input.mouse.grab)
|
if(ctx->input.mouse.grab)
|
||||||
{
|
{
|
||||||
sfml.window->setMouseCursorGrabbed(true);
|
sfml.window->setMouseCursorGrabbed(true);
|
||||||
ctx->input.mouse.grab = 0;
|
ctx->input.mouse.grab = 0;
|
||||||
}
|
}
|
||||||
else if(ctx->input.mouse.ungrab)
|
else if(ctx->input.mouse.ungrab)
|
||||||
{
|
{
|
||||||
int x = (int)ctx->input.mouse.prev.x;
|
int x = (int)ctx->input.mouse.prev.x;
|
||||||
int y = (int)ctx->input.mouse.prev.y;
|
int y = (int)ctx->input.mouse.prev.y;
|
||||||
sfml.window->setMouseCursorGrabbed(false);
|
sfml.window->setMouseCursorGrabbed(false);
|
||||||
sf::Mouse::setPosition(sf::Vector2i(x, y));
|
sf::Mouse::setPosition(sf::Vector2i(x, y));
|
||||||
ctx->input.mouse.ungrab = 0;
|
ctx->input.mouse.ungrab = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(event->type == sf::Event::KeyReleased || event->type == sf::Event::KeyPressed)
|
if(event->type == sf::Event::KeyReleased || event->type == sf::Event::KeyPressed)
|
||||||
{
|
{
|
||||||
int down = event->type == sf::Event::KeyPressed;
|
int down = event->type == sf::Event::KeyPressed;
|
||||||
sf::Keyboard::Key key = event->key.code;
|
sf::Keyboard::Key key = event->key.code;
|
||||||
|
|
||||||
if(key == sf::Keyboard::RShift || key == sf::Keyboard::LShift)
|
if(key == sf::Keyboard::RShift || key == sf::Keyboard::LShift)
|
||||||
nk_input_key(ctx, NK_KEY_SHIFT, down);
|
nk_input_key(ctx, NK_KEY_SHIFT, down);
|
||||||
else if(key == sf::Keyboard::Delete)
|
else if(key == sf::Keyboard::Delete)
|
||||||
nk_input_key(ctx, NK_KEY_DEL, down);
|
nk_input_key(ctx, NK_KEY_DEL, down);
|
||||||
else if(key == sf::Keyboard::Return)
|
else if(key == sf::Keyboard::Return)
|
||||||
nk_input_key(ctx, NK_KEY_ENTER, down);
|
nk_input_key(ctx, NK_KEY_ENTER, down);
|
||||||
else if(key == sf::Keyboard::Tab)
|
else if(key == sf::Keyboard::Tab)
|
||||||
nk_input_key(ctx, NK_KEY_TAB, down);
|
nk_input_key(ctx, NK_KEY_TAB, down);
|
||||||
else if(key == sf::Keyboard::BackSpace)
|
else if(key == sf::Keyboard::BackSpace)
|
||||||
nk_input_key(ctx, NK_KEY_BACKSPACE, down);
|
nk_input_key(ctx, NK_KEY_BACKSPACE, down);
|
||||||
else if(key == sf::Keyboard::Home)
|
else if(key == sf::Keyboard::Home)
|
||||||
{
|
{
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_START, down);
|
nk_input_key(ctx, NK_KEY_TEXT_START, down);
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_START, down);
|
nk_input_key(ctx, NK_KEY_SCROLL_START, down);
|
||||||
}
|
}
|
||||||
else if(key == sf::Keyboard::End)
|
else if(key == sf::Keyboard::End)
|
||||||
{
|
{
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_END, down);
|
nk_input_key(ctx, NK_KEY_TEXT_END, down);
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_END, down);
|
nk_input_key(ctx, NK_KEY_SCROLL_END, down);
|
||||||
}
|
}
|
||||||
else if(key == sf::Keyboard::PageDown)
|
else if(key == sf::Keyboard::PageDown)
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
|
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
|
||||||
else if(key == sf::Keyboard::PageUp)
|
else if(key == sf::Keyboard::PageUp)
|
||||||
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
|
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
|
||||||
else if(key == sf::Keyboard::Z)
|
else if(key == sf::Keyboard::Z)
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::R)
|
else if(key == sf::Keyboard::R)
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_REDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_TEXT_REDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::C)
|
else if(key == sf::Keyboard::C)
|
||||||
nk_input_key(ctx, NK_KEY_COPY, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_COPY, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::V)
|
else if(key == sf::Keyboard::V)
|
||||||
nk_input_key(ctx, NK_KEY_PASTE, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_PASTE, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::X)
|
else if(key == sf::Keyboard::X)
|
||||||
nk_input_key(ctx, NK_KEY_CUT, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_CUT, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::B)
|
else if(key == sf::Keyboard::B)
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::E)
|
else if(key == sf::Keyboard::E)
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
|
||||||
else if(key == sf::Keyboard::Up)
|
else if(key == sf::Keyboard::Up)
|
||||||
nk_input_key(ctx, NK_KEY_UP, down);
|
nk_input_key(ctx, NK_KEY_UP, down);
|
||||||
else if(key == sf::Keyboard::Down)
|
else if(key == sf::Keyboard::Down)
|
||||||
nk_input_key(ctx, NK_KEY_DOWN, down);
|
nk_input_key(ctx, NK_KEY_DOWN, down);
|
||||||
else if(key == sf::Keyboard::Left)
|
else if(key == sf::Keyboard::Left)
|
||||||
{
|
{
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
|
nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
|
||||||
else
|
else
|
||||||
nk_input_key(ctx, NK_KEY_LEFT, down);
|
nk_input_key(ctx, NK_KEY_LEFT, down);
|
||||||
}
|
}
|
||||||
else if(key == sf::Keyboard::Right)
|
else if(key == sf::Keyboard::Right)
|
||||||
{
|
{
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
|
||||||
nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
|
nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
|
||||||
else
|
else
|
||||||
nk_input_key(ctx, NK_KEY_RIGHT, down);
|
nk_input_key(ctx, NK_KEY_RIGHT, down);
|
||||||
}
|
}
|
||||||
else return 0;
|
else return 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if(event->type == sf::Event::MouseButtonPressed || event->type == sf::Event::MouseButtonReleased)
|
else if(event->type == sf::Event::MouseButtonPressed || event->type == sf::Event::MouseButtonReleased)
|
||||||
{
|
{
|
||||||
int down = event->type == sf::Event::MouseButtonPressed;
|
int down = event->type == sf::Event::MouseButtonPressed;
|
||||||
const int x = event->mouseButton.x;
|
const int x = event->mouseButton.x;
|
||||||
const int y = event->mouseButton.y;
|
const int y = event->mouseButton.y;
|
||||||
|
|
||||||
if(event->mouseButton.button == sf::Mouse::Left)
|
if(event->mouseButton.button == sf::Mouse::Left)
|
||||||
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
|
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
|
||||||
if(event->mouseButton.button == sf::Mouse::Middle)
|
if(event->mouseButton.button == sf::Mouse::Middle)
|
||||||
nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
|
nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
|
||||||
if(event->mouseButton.button == sf::Mouse::Right)
|
if(event->mouseButton.button == sf::Mouse::Right)
|
||||||
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
|
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if(event->type == sf::Event::MouseMoved)
|
else if(event->type == sf::Event::MouseMoved)
|
||||||
{
|
{
|
||||||
if(ctx->input.mouse.grabbed)
|
if(ctx->input.mouse.grabbed)
|
||||||
{
|
{
|
||||||
int x = (int)ctx->input.mouse.prev.x + event->mouseMove.x;
|
int x = (int)ctx->input.mouse.prev.x + event->mouseMove.x;
|
||||||
int y = (int)ctx->input.mouse.prev.y + event->mouseMove.y;
|
int y = (int)ctx->input.mouse.prev.y + event->mouseMove.y;
|
||||||
|
|
||||||
nk_input_motion(ctx, x, y);
|
nk_input_motion(ctx, x, y);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
nk_input_motion(ctx, event->mouseMove.x, event->mouseMove.y);
|
nk_input_motion(ctx, event->mouseMove.x, event->mouseMove.y);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
/* For Android*/
|
/* For Android*/
|
||||||
else if(event->type == sf::Event::TouchBegan || event->type == sf::Event::TouchEnded)
|
else if(event->type == sf::Event::TouchBegan || event->type == sf::Event::TouchEnded)
|
||||||
{
|
{
|
||||||
int down = event->type == sf::Event::TouchBegan;
|
int down = event->type == sf::Event::TouchBegan;
|
||||||
const int x = event->touch.x;
|
const int x = event->touch.x;
|
||||||
const int y = event->touch.y;
|
const int y = event->touch.y;
|
||||||
|
|
||||||
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
|
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if(event->type == sf::Event::TouchMoved)
|
else if(event->type == sf::Event::TouchMoved)
|
||||||
{
|
{
|
||||||
if(ctx->input.mouse.grabbed)
|
if(ctx->input.mouse.grabbed)
|
||||||
{
|
{
|
||||||
int x = (int)ctx->input.mouse.prev.x;
|
int x = (int)ctx->input.mouse.prev.x;
|
||||||
int y = (int)ctx->input.mouse.prev.y;
|
int y = (int)ctx->input.mouse.prev.y;
|
||||||
|
|
||||||
nk_input_motion(ctx, x + event->touch.x, y + event->touch.y);
|
nk_input_motion(ctx, x + event->touch.x, y + event->touch.y);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
nk_input_motion(ctx, event->touch.x, event->touch.y);
|
nk_input_motion(ctx, event->touch.x, event->touch.y);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if(event->type == sf::Event::TextEntered)
|
else if(event->type == sf::Event::TextEntered)
|
||||||
{
|
{
|
||||||
nk_input_unicode(ctx, event->text.unicode);
|
nk_input_unicode(ctx, event->text.unicode);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if(event->type == sf::Event::MouseWheelScrolled)
|
else if(event->type == sf::Event::MouseWheelScrolled)
|
||||||
{
|
{
|
||||||
nk_input_scroll(ctx, event->mouseWheelScroll.delta);
|
nk_input_scroll(ctx, event->mouseWheelScroll.delta);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
NK_API
|
NK_API
|
||||||
void nk_sfml_shutdown(void)
|
void nk_sfml_shutdown(void)
|
||||||
{
|
{
|
||||||
nk_font_atlas_clear(&sfml.atlas);
|
nk_font_atlas_clear(&sfml.atlas);
|
||||||
nk_free(&sfml.ctx);
|
nk_free(&sfml.ctx);
|
||||||
nk_sfml_device_destroy();
|
nk_sfml_device_destroy();
|
||||||
memset(&sfml, 0, sizeof(sfml));
|
memset(&sfml, 0, sizeof(sfml));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user