Added OpenGL3 for SFML and also working on "pure" SFML renderer.
This commit is contained in:
parent
2babe51576
commit
73b0b945b4
8
demo/sfml/Makefile
Normal file
8
demo/sfml/Makefile
Normal file
@ -0,0 +1,8 @@
|
||||
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
|
2
demo/sfml/build.bat
Normal file
2
demo/sfml/build.bat
Normal file
@ -0,0 +1,2 @@
|
||||
mingw32-make -f Makefile
|
||||
pause
|
178
demo/sfml/main.cpp
Normal file
178
demo/sfml/main.cpp
Normal file
@ -0,0 +1,178 @@
|
||||
/* 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;
|
||||
}
|
||||
|
582
demo/sfml/nuklear_sfml.h
Normal file
582
demo/sfml/nuklear_sfml.h
Normal file
@ -0,0 +1,582 @@
|
||||
/*
|
||||
* 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
|
10
demo/sfml_opengl3/Makefile
Normal file
10
demo/sfml_opengl3/Makefile
Normal file
@ -0,0 +1,10 @@
|
||||
GLAD_INC = -I C:/Users/Ricky/MinGW-Libs/GLAD/include
|
||||
|
||||
SFML_INC = -I C:/Users/Ricky/MinGW-Libs/SFML/include
|
||||
SFML_LIB = -L C:/Users/Ricky/MinGW-Libs/SFML/lib
|
||||
|
||||
LIBS = -DGLAD_HEADER_ONLY -DSFML_STATIC -lmingw32 -lsfml-window-s -lsfml-system-s -lopengl32 -lwinmm -lgdi32
|
||||
|
||||
|
||||
build:
|
||||
g++ main.cpp $(GLAD_INC) $(SFML_INC) $(SFML_LIB) $(LIBS) -o demo.exe
|
2
demo/sfml_opengl3/build.bat
Normal file
2
demo/sfml_opengl3/build.bat
Normal file
@ -0,0 +1,2 @@
|
||||
mingw32-make -f Makefile
|
||||
pause
|
183
demo/sfml_opengl3/main.cpp
Normal file
183
demo/sfml_opengl3/main.cpp
Normal file
@ -0,0 +1,183 @@
|
||||
/* 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>
|
||||
|
||||
#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_GL3_IMPLEMENTATION
|
||||
#include "../../nuklear.h"
|
||||
#include "nuklear_sfml_gl3.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, 3, 3);
|
||||
sf::Window window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Demo", sf::Style::Default, settings);
|
||||
|
||||
window.setVerticalSyncEnabled(true);
|
||||
window.setActive(true);
|
||||
|
||||
if(!gladLoadGL())
|
||||
{
|
||||
printf("Failed to load OpenGL extensions!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
540
demo/sfml_opengl3/nuklear_sfml_gl3.h
Normal file
540
demo/sfml_opengl3/nuklear_sfml_gl3.h
Normal file
@ -0,0 +1,540 @@
|
||||
/*
|
||||
* 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_GL3_H_
|
||||
#define NK_SFML_GL3_H_
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <SFML/Window.hpp>
|
||||
|
||||
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_end(void);
|
||||
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);
|
||||
|
||||
NK_API void nk_sfml_device_create(void);
|
||||
NK_API void nk_sfml_device_destroy(void);
|
||||
|
||||
#endif
|
||||
/*
|
||||
* ==============================================================
|
||||
*
|
||||
* IMPLEMENTATION
|
||||
*
|
||||
* ===============================================================
|
||||
*/
|
||||
#ifdef NK_SFML_GL3_IMPLEMENTATION
|
||||
|
||||
#include <string>
|
||||
|
||||
struct nk_sfml_device
|
||||
{
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
GLuint vbo, vao, ebo;
|
||||
GLuint prog;
|
||||
GLuint vert_shdr;
|
||||
GLuint frag_shdr;
|
||||
GLint attrib_pos;
|
||||
GLint attrib_uv;
|
||||
GLint attrib_col;
|
||||
GLint uniform_tex;
|
||||
GLint uniform_proj;
|
||||
GLuint font_tex;
|
||||
};
|
||||
|
||||
struct nk_sfml_vertex
|
||||
{
|
||||
float position[2];
|
||||
float uv[2];
|
||||
nk_byte col[4];
|
||||
};
|
||||
|
||||
static struct nk_sfml
|
||||
{
|
||||
sf::Window* window;
|
||||
struct nk_sfml_device ogl;
|
||||
struct nk_context ctx;
|
||||
struct nk_font_atlas atlas;
|
||||
} sfml;
|
||||
|
||||
#ifdef __APPLE__
|
||||
#define NK_SHADER_VERSION "#version 150\n"
|
||||
#else
|
||||
#define NK_SHADER_VERSION "#version 300 es\n"
|
||||
#endif
|
||||
|
||||
NK_API void
|
||||
nk_sfml_device_create(void)
|
||||
{
|
||||
GLint status;
|
||||
static const GLchar* vertex_shader =
|
||||
NK_SHADER_VERSION
|
||||
"uniform mat4 ProjMtx;\n"
|
||||
"in vec2 Position;\n"
|
||||
"in vec2 TexCoord;\n"
|
||||
"in vec4 Color;\n"
|
||||
"out vec2 Frag_UV;\n"
|
||||
"out vec4 Frag_Color;\n"
|
||||
"void main() {\n"
|
||||
" Frag_UV = TexCoord;\n"
|
||||
" Frag_Color = Color;\n"
|
||||
" gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n"
|
||||
"}\n";
|
||||
static const GLchar *fragment_shader =
|
||||
NK_SHADER_VERSION
|
||||
"precision mediump float;\n"
|
||||
"uniform sampler2D Texture;\n"
|
||||
"in vec2 Frag_UV;\n"
|
||||
"in vec4 Frag_Color;\n"
|
||||
"out vec4 Out_Color;\n"
|
||||
"void main(){\n"
|
||||
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
|
||||
"}\n";
|
||||
|
||||
struct nk_sfml_device* dev = &sfml.ogl;
|
||||
nk_buffer_init_default(&dev->cmds);
|
||||
|
||||
dev->prog = glCreateProgram();
|
||||
dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER);
|
||||
dev->frag_shdr = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
||||
glShaderSource(dev->vert_shdr, 1, &vertex_shader, 0);
|
||||
glShaderSource(dev->frag_shdr, 1, &fragment_shader, 0);
|
||||
glCompileShader(dev->vert_shdr);
|
||||
glCompileShader(dev->frag_shdr);
|
||||
|
||||
glGetShaderiv(dev->vert_shdr, GL_COMPILE_STATUS, &status);
|
||||
assert(status == GL_TRUE);
|
||||
|
||||
glGetShaderiv(dev->frag_shdr, GL_COMPILE_STATUS, &status);
|
||||
assert(status == GL_TRUE);
|
||||
|
||||
glAttachShader(dev->prog, dev->vert_shdr);
|
||||
glAttachShader(dev->prog, dev->frag_shdr);
|
||||
glLinkProgram(dev->prog);
|
||||
|
||||
glGetProgramiv(dev->prog, GL_LINK_STATUS, &status);
|
||||
assert(status == GL_TRUE);
|
||||
|
||||
dev->uniform_tex = glGetUniformLocation(dev->prog, "Texture");
|
||||
dev->uniform_proj = glGetUniformLocation(dev->prog, "ProjMtx");
|
||||
dev->attrib_pos = glGetAttribLocation(dev->prog, "Position");
|
||||
dev->attrib_uv = glGetAttribLocation(dev->prog, "TexCoord");
|
||||
dev->attrib_col = glGetAttribLocation(dev->prog, "Color");
|
||||
|
||||
{
|
||||
/* buffer setup */
|
||||
GLsizei vs = sizeof(struct nk_sfml_vertex);
|
||||
size_t vp = offsetof(struct nk_sfml_vertex, position);
|
||||
size_t vt = offsetof(struct nk_sfml_vertex, uv);
|
||||
size_t vc = offsetof(struct nk_sfml_vertex, col);
|
||||
|
||||
glGenBuffers(1, &dev->vbo);
|
||||
glGenBuffers(1, &dev->ebo);
|
||||
glGenVertexArrays(1, &dev->vao);
|
||||
|
||||
glBindVertexArray(dev->vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
|
||||
|
||||
glEnableVertexAttribArray((GLuint)dev->attrib_pos);
|
||||
glEnableVertexAttribArray((GLuint)dev->attrib_uv);
|
||||
glEnableVertexAttribArray((GLuint)dev->attrib_col);
|
||||
|
||||
glVertexAttribPointer((GLuint)dev->attrib_pos, 2, GL_FLOAT, GL_FALSE, vs, (void*)vp);
|
||||
glVertexAttribPointer((GLuint)dev->attrib_uv, 2, GL_FLOAT, GL_FALSE, vs, (void*)vt);
|
||||
glVertexAttribPointer((GLuint)dev->attrib_col, 4, GL_UNSIGNED_BYTE, GL_TRUE, vs, (void*)vc);
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
NK_API void
|
||||
nk_sfml_device_destroy(void)
|
||||
{
|
||||
struct nk_sfml_device* dev = &sfml.ogl;
|
||||
|
||||
glDetachShader(dev->prog, dev->vert_shdr);
|
||||
glDetachShader(dev->prog, dev->frag_shdr);
|
||||
glDeleteShader(dev->vert_shdr);
|
||||
glDeleteShader(dev->vert_shdr);
|
||||
glDeleteProgram(dev->prog);
|
||||
glDeleteTextures(1, &dev->font_tex);
|
||||
glDeleteBuffers(1, &dev->vbo);
|
||||
glDeleteBuffers(1, &dev->ebo);
|
||||
nk_buffer_free(&dev->cmds);
|
||||
}
|
||||
|
||||
NK_INTERN void
|
||||
nk_sfml_device_upload_atlas(const void* image, int width, int height)
|
||||
{
|
||||
struct nk_sfml_device* dev = &sfml.ogl;
|
||||
glGenTextures(1, &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_MAG_FILTER, GL_LINEAR);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, image);
|
||||
}
|
||||
|
||||
NK_API void
|
||||
nk_sfml_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
|
||||
{
|
||||
/* setup global state */
|
||||
struct nk_sfml_device* dev = &sfml.ogl;
|
||||
|
||||
int window_width = sfml.window->getSize().x;
|
||||
int window_height = sfml.window->getSize().y;
|
||||
|
||||
GLfloat ortho[4][4] =
|
||||
{
|
||||
{2.0f, 0.0f, 0.0f, 0.0f},
|
||||
{0.0f,-2.0f, 0.0f, 0.0f},
|
||||
{0.0f, 0.0f,-1.0f, 0.0f},
|
||||
{-1.0f,1.0f, 0.0f, 1.0f},
|
||||
};
|
||||
|
||||
ortho[0][0] /= (GLfloat)window_width;
|
||||
ortho[1][1] /= (GLfloat)window_height;
|
||||
|
||||
glViewport(0, 0, window_width, window_height);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendEquation(GL_FUNC_ADD);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
/* setup program */
|
||||
glUseProgram(dev->prog);
|
||||
glUniform1i(dev->uniform_tex, 0);
|
||||
glUniformMatrix4fv(dev->uniform_proj, 1, GL_FALSE, &ortho[0][0]);
|
||||
{
|
||||
/* convert from command queue into draw list and draw to screen */
|
||||
const struct nk_draw_command *cmd;
|
||||
void *vertices, *elements;
|
||||
const nk_draw_index *offset = NULL;
|
||||
|
||||
/* allocate vertex and element buffer */
|
||||
glBindVertexArray(dev->vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, max_vertex_buffer, NULL, GL_STREAM_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, max_element_buffer, NULL, GL_STREAM_DRAW);
|
||||
|
||||
/* load vertices/elements directly into vertex/element buffer */
|
||||
vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
||||
elements = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
|
||||
|
||||
{
|
||||
/* fill convert configuration */
|
||||
struct nk_convert_config config;
|
||||
|
||||
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_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_LAYOUT_END}
|
||||
};
|
||||
|
||||
NK_MEMSET(&config, 0, sizeof(config));
|
||||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_sfml_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_sfml_vertex);
|
||||
config.null = dev->null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
config.global_alpha = 1.0f;
|
||||
config.shape_AA = AA;
|
||||
config.line_AA = AA;
|
||||
|
||||
/* setup buffers to load vertices and elements */
|
||||
{
|
||||
struct nk_buffer vbuf, ebuf;
|
||||
nk_buffer_init_fixed(&vbuf, vertices, (nk_size)max_vertex_buffer);
|
||||
nk_buffer_init_fixed(&ebuf, elements, (nk_size)max_element_buffer);
|
||||
nk_convert(&sfml.ctx, &dev->cmds, &vbuf, &ebuf, &config);
|
||||
}
|
||||
}
|
||||
|
||||
glUnmapBuffer(GL_ARRAY_BUFFER);
|
||||
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
|
||||
|
||||
/* iterate over and execute each draw command */
|
||||
nk_draw_foreach(cmd, &sfml.ctx, &dev->cmds)
|
||||
{
|
||||
if (!cmd->elem_count) continue;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
|
||||
glScissor(
|
||||
(GLint)(cmd->clip_rect.x),
|
||||
(GLint)((window_height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h))),
|
||||
(GLint)(cmd->clip_rect.w),
|
||||
(GLint)(cmd->clip_rect.h));
|
||||
glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
|
||||
offset += cmd->elem_count;
|
||||
}
|
||||
nk_clear(&sfml.ctx);
|
||||
}
|
||||
|
||||
glUseProgram(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
glDisable(GL_BLEND);
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
}
|
||||
|
||||
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(sf::Window* window)
|
||||
{
|
||||
sfml.window = window;
|
||||
nk_init_default(&sfml.ctx, 0);
|
||||
sfml.ctx.clip.copy = nk_sfml_clipboard_copy;
|
||||
sfml.ctx.clip.paste = nk_sfml_clipboard_paste;
|
||||
sfml.ctx.clip.userdata = nk_handle_ptr(0);
|
||||
//nk_buffer_init_default(&sfml.ogl.cmds);
|
||||
nk_sfml_device_create();
|
||||
return &sfml.ctx;
|
||||
}
|
||||
|
||||
NK_API void
|
||||
nk_sfml_font_stash_begin(struct nk_font_atlas** atlas)
|
||||
{
|
||||
nk_font_atlas_init_default(&sfml.atlas);
|
||||
nk_font_atlas_begin(&sfml.atlas);
|
||||
*atlas = &sfml.atlas;
|
||||
}
|
||||
|
||||
NK_API void
|
||||
nk_sfml_font_stash_end(void)
|
||||
{
|
||||
const void* image;
|
||||
int w, h;
|
||||
image = nk_font_atlas_bake(&sfml.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
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);
|
||||
if(sfml.atlas.default_font)
|
||||
nk_style_set_font(&sfml.ctx, &sfml.atlas.default_font->handle);
|
||||
}
|
||||
|
||||
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_font_atlas_clear(&sfml.atlas);
|
||||
nk_free(&sfml.ctx);
|
||||
nk_sfml_device_destroy();
|
||||
memset(&sfml, 0, sizeof(sfml));
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user