added text height to text command

This commit is contained in:
vurtun 2015-11-20 00:20:03 +01:00
parent b1f35976ca
commit 44156326b9
5 changed files with 64 additions and 58 deletions

View File

@ -464,12 +464,12 @@ show_test_window(struct zr_window *window, struct zr_style *config, enum theme *
zr_tooltip(&layout, "This is a tooltip");
zr_layout_row(&layout, ZR_STATIC, 30, 2, ratio);
zr_labelf(&layout, ZR_TEXT_LEFT, "Slider int: %d", int_slider);
zr_labelf(&layout, ZR_TEXT_LEFT, "Slider int");
zr_slider_int(&layout, 0, &int_slider, 10, 1);
zr_labelf(&layout, ZR_TEXT_LEFT, "Slider float: %.2f", float_slider);
zr_label(&layout, "Slider float", ZR_TEXT_LEFT);
zr_slider_float(&layout, 0, &float_slider, 5.0, 0.5f);
zr_labelf(&layout, ZR_TEXT_LEFT, "Progressbar: %lu:" , prog_value);
zr_labelf(&layout, ZR_TEXT_LEFT, "Progressbar" , prog_value);
zr_progress(&layout, &prog_value, 100, ZR_MODIFYABLE);
zr_layout_row(&layout, ZR_STATIC, 30, 2, ratio);
@ -1124,7 +1124,7 @@ run_demo(struct demo *gui)
if (zr_layout_push(&layout, ZR_LAYOUT_NODE, "Primitives", &prim_state))
{
zr_layout_row_dynamic(&layout, 20, 2);
zr_layout_row_dynamic(&layout, 25, 2);
zr_label(&layout,"Scissor:", ZR_TEXT_LEFT);
zr_labelf(&layout, ZR_TEXT_LEFT, "%u", stats->scissors);
zr_label(&layout,"Lines:", ZR_TEXT_LEFT);

View File

@ -33,7 +33,7 @@
#include <GL/glu.h>
#include <SDL2/SDL.h>
#define NANOVG_GLES2_IMPLEMENTATION
#define NANOVG_GLES3_IMPLEMENTATION
#include "dep/nanovg.h"
#include "dep/nanovg_gl.h"
#include "dep/nanovg_gl_utils.h"
@ -46,7 +46,7 @@ static void
clipboard_set(const char *text)
{SDL_SetClipboardText(text);}
static zr_bool
static int
clipboard_is_filled(void)
{return SDL_HasClipboardText();}
@ -67,14 +67,14 @@ die(const char *fmt, ...)
exit(EXIT_FAILURE);
}
static zr_size
font_get_width(zr_handle handle, const zr_char *text, zr_size len)
static size_t
font_get_width(zr_handle handle, const char *text, size_t len)
{
zr_size width;
size_t width;
float bounds[4];
NVGcontext *ctx = (NVGcontext*)handle.ptr;
nvgTextBounds(ctx, 0, 0, text, &text[len], bounds);
width = (zr_size)(bounds[2] - bounds[0]);
width = (size_t)(bounds[2] - bounds[0]);
return width;
}
@ -149,6 +149,7 @@ draw(NVGcontext *nvg, struct zr_command_queue *queue, int width, int height)
nvgBeginPath(nvg);
nvgFillColor(nvg, nvgRGBA(t->foreground.r, t->foreground.g,
t->foreground.b, t->foreground.a));
nvgFontSize(nvg, (float)t->height);
nvgTextAlign(nvg, NVG_ALIGN_MIDDLE);
nvgText(nvg, t->x, t->y + t->h * 0.5f, t->string, &t->string[t->length]);
nvgFill(nvg);
@ -174,7 +175,7 @@ draw(NVGcontext *nvg, struct zr_command_queue *queue, int width, int height)
}
static void
key(struct zr_input *in, SDL_Event *evt, zr_bool down)
key(struct zr_input *in, SDL_Event *evt, int down)
{
const Uint8* state = SDL_GetKeyboardState(NULL);
SDL_Keycode sym = evt->key.keysym.sym;
@ -201,16 +202,16 @@ key(struct zr_input *in, SDL_Event *evt, zr_bool down)
static void
motion(struct zr_input *in, SDL_Event *evt)
{
const zr_int x = evt->motion.x;
const zr_int y = evt->motion.y;
const int x = evt->motion.x;
const int y = evt->motion.y;
zr_input_motion(in, x, y);
}
static void
btn(struct zr_input *in, SDL_Event *evt, zr_bool down)
btn(struct zr_input *in, SDL_Event *evt, int down)
{
const zr_int x = evt->button.x;
const zr_int y = evt->button.y;
const int x = evt->button.x;
const int y = evt->button.y;
if (evt->button.button == SDL_BUTTON_LEFT)
zr_input_button(in, ZR_BUTTON_LEFT, x, y, down);
else if (evt->button.button == SDL_BUTTON_LEFT)
@ -238,7 +239,7 @@ main(int argc, char *argv[])
/* Platform */
int width, height;
const char *font_path;
zr_size font_height;
int font_height;
SDL_Window *win;
SDL_GLContext glContext;
NVGcontext *vg = NULL;
@ -246,11 +247,11 @@ main(int argc, char *argv[])
/* GUI */
struct demo gui;
if (argc < 2) {
fprintf(stdout,"Missing TTF Font file argument: gui <path>\n");
fprintf(stdout,"Missing TTF Font file argument: binary <font-path>\n");
exit(EXIT_FAILURE);
}
font_path = argv[1];
font_height = 10;
font_height = 14;
/* SDL */
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS);
@ -268,7 +269,7 @@ main(int argc, char *argv[])
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
/* nanovg */
vg = nvgCreateGLES2(NVG_ANTIALIAS|NVG_DEBUG);
vg = nvgCreateGLES3(NVG_ANTIALIAS);
if (!vg) die("[NVG]: failed to init\n");
nvgCreateFont(vg, "fixed", font_path);
nvgFontFace(vg, "fixed");
@ -286,6 +287,7 @@ main(int argc, char *argv[])
while (gui.running) {
/* Input */
SDL_Event evt;
uint64_t dt, started = SDL_GetTicks();
zr_input_begin(&gui.input);
while (SDL_PollEvent(&evt)) {
if (evt.type == SDL_WINDOWEVENT) resize(&evt);
@ -309,13 +311,15 @@ main(int argc, char *argv[])
glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
draw(vg, &gui.queue, width, height);
dt = SDL_GetTicks() - started;
/*fprintf(stdout, "%lu\n", dt);*/
SDL_GL_SwapWindow(win);
}
cleanup:
/* Cleanup */
free(zr_buffer_memory(&gui.queue.buffer));
nvgDeleteGLES2(vg);
nvgDeleteGLES3(vg);
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(win);
SDL_Quit();

View File

@ -35,12 +35,12 @@
#define DTIME 33
#define MAX_DRAW_COMMAND_MEMORY (4 * 1024)
#define MAX_VERTEX_MEMORY 128 * 1024
#define MAX_ELEMENT_MEMORY 32 * 1024
#define MAX_ELEMENT_MEMORY 64 * 1024
#include "../../zahnrad.h"
static void clipboard_set(const char *text){SDL_SetClipboardText(text);}
static zr_bool clipboard_is_filled(void) {return SDL_HasClipboardText();}
static int clipboard_is_filled(void) {return SDL_HasClipboardText();}
static const char* clipboard_get(void){return SDL_GetClipboardText();}
#include "../demo.c"
@ -174,10 +174,10 @@ device_init(struct device *dev)
static struct zr_user_font
font_bake_and_upload(struct device *dev, struct zr_font *font,
const char *path, unsigned int font_height, const zr_long *range)
const char *path, unsigned int font_height, const zr_rune *range)
{
zr_size glyph_count;
zr_size img_width, img_height;
int glyph_count;
int img_width, img_height;
struct zr_font_glyph *glyphes;
struct zr_baked_font baked_font;
struct zr_user_font user_font;
@ -191,7 +191,7 @@ font_bake_and_upload(struct device *dev, struct zr_font *font,
/* bake and upload font texture */
void *img, *tmp;
size_t ttf_size;
zr_size tmp_size, img_size;
size_t tmp_size, img_size;
const char *custom_data = "....";
struct zr_font_config config;
char *ttf_blob = file_load(path, &ttf_size);
@ -206,14 +206,14 @@ font_bake_and_upload(struct device *dev, struct zr_font *font,
config.coord_type = ZR_COORD_UV;
config.range = range;
config.pixel_snap = zr_false;
config.size = (zr_float)font_height;
config.size = (float)font_height;
config.spacing = zr_vec2(0,0);
config.oversample_h = 1;
config.oversample_v = 1;
/* query needed amount of memory for the font baking process */
zr_font_bake_memory(&tmp_size, &glyph_count, &config, 1);
glyphes = (struct zr_font_glyph*)calloc(sizeof(struct zr_font_glyph), glyph_count);
glyphes = (struct zr_font_glyph*)calloc(sizeof(struct zr_font_glyph), (size_t)glyph_count);
tmp = calloc(1, tmp_size);
/* pack all glyphes and return needed image width height and memory size*/
@ -227,8 +227,8 @@ font_bake_and_upload(struct device *dev, struct zr_font *font,
zr_font_bake_custom_data(img, img_width, img_height, custom, custom_data, 2, 2, '.', 'X');
{
/* convert alpha8 image into rgba8 image */
void *img_rgba = calloc(4, img_height * img_width);
zr_font_bake_convert(img_rgba, (zr_ushort)img_width, (zr_ushort)img_height, img);
void *img_rgba = calloc(4, (size_t)(img_height * img_width));
zr_font_bake_convert(img_rgba, img_width, img_height, img);
free(img);
img = img_rgba;
}
@ -247,15 +247,15 @@ font_bake_and_upload(struct device *dev, struct zr_font *font,
}
/* default white pixel in a texture which is needed to draw primitives */
dev->null.texture.id = (zr_int)dev->font_tex;
dev->null.uv = zr_vec2((custom.x + 0.5f)/(zr_float)img_width,
(custom.y + 0.5f)/(zr_float)img_height);
dev->null.texture.id = (int)dev->font_tex;
dev->null.uv = zr_vec2((custom.x + 0.5f)/(float)img_width,
(custom.y + 0.5f)/(float)img_height);
/* setup font with glyphes. IMPORTANT: the font only references the glyphes
this was done to have the possibility to have multible fonts with one
total glyph array. Not quite sure if it is a good thing since the
glyphes have to be freed as well. */
zr_font_init(font, (zr_float)font_height, '?', glyphes, &baked_font, dev->null.texture);
zr_font_init(font, (float)font_height, '?', glyphes, &baked_font, dev->null.texture);
user_font = zr_font_ref(font);
return user_font;
}
@ -274,8 +274,8 @@ device_shutdown(struct device *dev)
}
/* this is stupid but needed for C89 since sinf and cosf do not exist */
static zr_float fsin(zr_float f) {return (zr_float)sin(f);}
static zr_float fcos(zr_float f) {return (zr_float)cos(f);}
static float fsin(float f) {return (float)sin(f);}
static float fcos(float f) {return (float)cos(f);}
static void
device_draw(struct device *dev, struct zr_command_queue *queue, int width, int height,
@ -346,6 +346,7 @@ device_draw(struct device *dev, struct zr_command_queue *queue, int width, int h
/* iterate over and execute each draw command */
zr_foreach_draw_command(cmd, &draw_list) {
if (!cmd->elem_count) continue;
glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
glScissor((GLint)cmd->clip_rect.x,
height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h),
@ -368,7 +369,7 @@ device_draw(struct device *dev, struct zr_command_queue *queue, int width, int h
}
static void
input_key(struct zr_input *in, SDL_Event *evt, zr_bool down)
input_key(struct zr_input *in, SDL_Event *evt, int down)
{
const Uint8* state = SDL_GetKeyboardState(NULL);
SDL_Keycode sym = evt->key.keysym.sym;
@ -395,16 +396,16 @@ input_key(struct zr_input *in, SDL_Event *evt, zr_bool down)
static void
input_motion(struct zr_input *in, SDL_Event *evt)
{
const zr_int x = evt->motion.x;
const zr_int y = evt->motion.y;
const int x = evt->motion.x;
const int y = evt->motion.y;
zr_input_motion(in, x, y);
}
static void
input_button(struct zr_input *in, SDL_Event *evt, zr_bool down)
input_button(struct zr_input *in, SDL_Event *evt, int down)
{
const zr_int x = evt->button.x;
const zr_int y = evt->button.y;
const int x = evt->button.x;
const int y = evt->button.y;
if (evt->button.button == SDL_BUTTON_LEFT)
zr_input_button(in, ZR_BUTTON_LEFT, x, y, down);
if (evt->button.button == SDL_BUTTON_RIGHT)
@ -426,7 +427,7 @@ resize(SDL_Event *evt)
glViewport(0, 0, evt->window.data1, evt->window.data2);
}
static void* mem_alloc(zr_handle unused, zr_size size)
static void* mem_alloc(zr_handle unused, size_t size)
{UNUSED(unused); return calloc(1, size);}
static void mem_free(zr_handle unused, void *ptr)
{UNUSED(unused); free(ptr);}
@ -505,8 +506,8 @@ main(int argc, char *argv[])
/* GUI */
SDL_GetWindowSize(win, &width, &height);
gui.w = (zr_size)width;
gui.h = (zr_size)height;
gui.w = (size_t)width;
gui.h = (size_t)height;
run_demo(&gui);
/* Draw */

View File

@ -1057,13 +1057,11 @@ zr_user_font_glyphes_fitting_in_space(const struct zr_user_font *font, const cha
*text_width = last_width;
return offset;
}
/*
* ==============================================================
/* ==============================================================
*
* Input
*
* ===============================================================
*/
* ===============================================================*/
void
zr_input_begin(struct zr_input *in)
{
@ -1817,6 +1815,7 @@ zr_command_buffer_push_text(struct zr_command_buffer *b, struct zr_rect r,
cmd->foreground = fg;
cmd->font = font;
cmd->length = length;
cmd->height = font->height;
zr_memcopy(cmd->string, string, length);
cmd->string[length] = '\0';
b->stats.text++;
@ -2232,7 +2231,7 @@ zr_draw_list_load(struct zr_draw_list *list, struct zr_command_queue *queue,
case ZR_COMMAND_TEXT: {
const struct zr_command_text *t = zr_command(text, cmd);
zr_draw_list_add_text(list, t->font, zr_rect(t->x, t->y, t->w, t->h),
t->string, t->length, t->background, t->foreground);
t->string, t->length, t->height, t->background, t->foreground);
} break;
case ZR_COMMAND_IMAGE: {
const struct zr_command_image *i = zr_command(image, cmd);
@ -2943,14 +2942,15 @@ zr_draw_list_add_image(struct zr_draw_list *list, struct zr_image texture,
void
zr_draw_list_add_text(struct zr_draw_list *list, const struct zr_user_font *font,
struct zr_rect rect, const char *text, zr_size len,
struct zr_rect rect, const char *text, zr_size len, float font_height,
struct zr_color bg, struct zr_color fg)
{
float x;
float x, scale;
zr_size text_len;
zr_rune unicode, next;
zr_size glyph_len, next_glyph_len;
struct zr_user_font_glyph g;
scale = font_height / font->height;
ZR_ASSERT(list);
if (!list || !len || !text) return;
@ -2977,10 +2977,10 @@ zr_draw_list_add_text(struct zr_draw_list *list, const struct zr_user_font *font
font->query(font->userdata, &g, unicode, (next == ZR_UTF_INVALID) ? '\0' : next);
/* calculate and draw glyph drawing rectangle and image */
gx = x + g.offset.x;
gy = rect.y + (rect.h/2) - (font->height/2) + g.offset.y;
gw = g.width; gh = g.height;
char_width = g.xadvance;
gx = x + g.offset.x * scale;
gy = rect.y + (rect.h/2) - (font->height/2) + g.offset.y * scale;
gw = g.width * scale; gh = g.height * scale;
char_width = g.xadvance * scale;
zr_draw_list_push_rect_uv(list, zr_vec2(gx,gy), zr_vec2(gx + gw, gy+ gh),
g.uv[0], g.uv[1], fg);

View File

@ -670,6 +670,7 @@ struct zr_command_text {
struct zr_color foreground;
short x, y;
unsigned short w, h;
float height;
zr_size length;
char string[1];
};
@ -1269,7 +1270,7 @@ void zr_draw_list_add_curve(struct zr_draw_list*, struct zr_vec2 p0,
*/
void zr_draw_list_add_text(struct zr_draw_list*, const struct zr_user_font*,
struct zr_rect, const char*, zr_size length,
struct zr_color bg, struct zr_color fg);
float font_height, struct zr_color bg, struct zr_color fg);
/* this function renders text
Input:
- user font to draw the text with