texture changed

This commit is contained in:
vurtun 2015-03-20 18:01:27 +01:00
parent b4555c26dc
commit 6f82dbf69c
3 changed files with 18 additions and 105 deletions

77
gui.c
View File

@ -30,7 +30,7 @@
#define vec2_sub(r,a,b) do {(r).x=(a).x-(b).x; (r).y=(a).y-(b).y;} while(0) #define vec2_sub(r,a,b) do {(r).x=(a).x-(b).x; (r).y=(a).y-(b).y;} while(0)
#define vec2_muls(r, v, s) do {(r).x=(v).x*(s); (r).y=(v).y*(s);} while(0) #define vec2_muls(r, v, s) do {(r).x=(v).x*(s); (r).y=(v).y*(s);} while(0)
static const gui_texture null_tex = (void*)0; static const gui_texture null_tex;
static const struct gui_rect null_rect = {0, 0, 9999, 9999}; static const struct gui_rect null_rect = {0, 0, 9999, 9999};
static const gui_char utfbyte[GUI_UTF_SIZE+1] = {0x80, 0, 0xC0, 0xE0, 0xF0}; static const gui_char utfbyte[GUI_UTF_SIZE+1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
static const gui_char utfmask[GUI_UTF_SIZE+1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; static const gui_char utfmask[GUI_UTF_SIZE+1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
@ -1394,7 +1394,7 @@ gui_default_config(struct gui_config *config)
config->panel_min_size = gui_make_vec2(32.0f, 32.0f); config->panel_min_size = gui_make_vec2(32.0f, 32.0f);
config->item_spacing = gui_make_vec2(8.0f, 8.0f); config->item_spacing = gui_make_vec2(8.0f, 8.0f);
config->item_padding = gui_make_vec2(4.0f, 4.0f); config->item_padding = gui_make_vec2(4.0f, 4.0f);
config->colors[GUI_COLOR_TEXT] = gui_make_color(255, 255, 255, 255); config->colors[GUI_COLOR_TEXT] = gui_make_color(100, 100, 100, 255);
config->colors[GUI_COLOR_PANEL] = gui_make_color(45, 45, 45, 255); config->colors[GUI_COLOR_PANEL] = gui_make_color(45, 45, 45, 255);
config->colors[GUI_COLOR_BORDER] = gui_make_color(100, 100, 100, 255); config->colors[GUI_COLOR_BORDER] = gui_make_color(100, 100, 100, 255);
config->colors[GUI_COLOR_TITLEBAR] = gui_make_color(45, 45, 45, 255); config->colors[GUI_COLOR_TITLEBAR] = gui_make_color(45, 45, 45, 255);
@ -1679,35 +1679,6 @@ gui_panel_button_text(struct gui_panel *panel, const char *str, gui_size len,
return gui_button_text(panel->out, &button, str, len, panel->font, panel->in); return gui_button_text(panel->out, &button, str, len, panel->font, panel->in);
} }
gui_bool
gui_panel_button_invisible(struct gui_panel *panel, const char *str, gui_size len,
enum gui_button_behavior behavior)
{
struct gui_rect bounds;
struct gui_button button;
const struct gui_config *config;
const struct gui_color invis = {0,0,0,255};
if (!panel || !panel->config || !panel->in || !panel->out) return 0;
if (!panel->font || panel->minimized) return 0;
gui_panel_alloc_space(&bounds, panel);
config = panel->config;
button.x = bounds.x;
button.y = bounds.y;
button.w = bounds.w;
button.h = bounds.h;
button.behavior = behavior;
button.pad_x = config->item_padding.x;
button.pad_y = config->item_padding.y;
button.background = invis;
button.foreground = invis;
button.content = config->colors[GUI_COLOR_TEXT];
button.highlight = config->colors[GUI_COLOR_BUTTON_HOVER];
button.highlight_content = config->colors[GUI_COLOR_BUTTON_HOVER_FONT];
return gui_button_text(panel->out, &button, str, len, panel->font, panel->in);
}
gui_bool gui_panel_button_color(struct gui_panel *panel, const struct gui_color color, gui_bool gui_panel_button_color(struct gui_panel *panel, const struct gui_color color,
enum gui_button_behavior behavior) enum gui_button_behavior behavior)
{ {
@ -1731,7 +1702,7 @@ gui_bool gui_panel_button_color(struct gui_panel *panel, const struct gui_color
button.foreground = color; button.foreground = color;
button.highlight = color; button.highlight = color;
button.highlight_content = config->colors[GUI_COLOR_BUTTON_HOVER_FONT]; button.highlight_content = config->colors[GUI_COLOR_BUTTON_HOVER_FONT];
return gui_button_text(panel->out, &button, NULL, 0, panel->font, panel->in); return gui_button(panel->out, &button, panel->in);
} }
gui_bool gui_bool
@ -2237,48 +2208,6 @@ gui_panel_list(struct gui_panel *panel, gui_bool *selection,
return offset; return offset;
} }
void
gui_panel_text_box(struct gui_panel *panel, const gui_char *content, gui_size *len,
gui_float *offset)
{
gui_size i;
struct gui_panel box;
struct gui_config config;
struct gui_text text;
struct gui_rect bounds;
const struct gui_config *temp;
if (!panel || !content || !len || !offset) return;
if (panel->minimized) return;
temp = panel->config;
memcopy(&config, panel->config, sizeof(struct gui_config));
config.panel_padding.y = 0.0f;
panel->config = &config;
gui_panel_frame_begin(panel, &box, NULL);
box.offset = *offset;
config.panel_padding.x = 0.0f;
gui_panel_layout(&box, box.height, 1);
gui_panel_alloc_space(&bounds, &box);
text.x = bounds.x;
text.y = bounds.y;
text.w = bounds.w;
text.h = bounds.h;
text.pad_x = box.config->item_padding.x;
text.pad_y = box.config->item_padding.y;
text.text = (const char*)content;
text.length = *len;
text.font = temp->colors[GUI_COLOR_TEXT];
text.background = temp->colors[GUI_COLOR_PANEL];
gui_text_wrap(panel->out, &text, panel->font);
gui_panel_frame_end(&box);
panel->config = temp;
*offset = box.offset;
return;
}
void void
gui_panel_frame_begin(struct gui_panel *panel, struct gui_panel *frame, const char *title) gui_panel_frame_begin(struct gui_panel *panel, struct gui_panel *frame, const char *title)
{ {

13
gui.h
View File

@ -18,11 +18,11 @@ typedef unsigned int gui_bool;
typedef unsigned int gui_flags; typedef unsigned int gui_flags;
typedef unsigned char gui_char; typedef unsigned char gui_char;
typedef float gui_float; typedef float gui_float;
typedef void* gui_texture;
typedef unsigned char gui_byte; typedef unsigned char gui_byte;
typedef unsigned int gui_flag; typedef unsigned int gui_flag;
typedef unsigned long gui_size; typedef unsigned long gui_size;
typedef gui_char gui_glyph[GUI_UTF_SIZE]; typedef gui_char gui_glyph[GUI_UTF_SIZE];
typedef union {void* dx; gui_uint gl;} gui_texture;
enum {gui_false, gui_true}; enum {gui_false, gui_true};
enum gui_heading {GUI_UP, GUI_RIGHT, GUI_DOWN, GUI_LEFT}; enum gui_heading {GUI_UP, GUI_RIGHT, GUI_DOWN, GUI_LEFT};
@ -130,8 +130,8 @@ struct gui_font {
gui_float scale; gui_float scale;
gui_texture texture; gui_texture texture;
struct gui_vec2 tex_size; struct gui_vec2 tex_size;
struct gui_font_glyph *glyphes;
gui_long glyph_count; gui_long glyph_count;
struct gui_font_glyph *glyphes;
const struct gui_font_glyph *fallback; const struct gui_font_glyph *fallback;
}; };
@ -323,10 +323,7 @@ enum gui_panel_flags {
GUI_PANEL_MINIMIZABLE = 0x8, GUI_PANEL_MINIMIZABLE = 0x8,
GUI_PANEL_CLOSEABLE = 0x10, GUI_PANEL_CLOSEABLE = 0x10,
GUI_PANEL_SCROLLBAR = 0x20, GUI_PANEL_SCROLLBAR = 0x20,
GUI_PANEL_HIDDEN = 0x40, GUI_PANEL_HIDDEN = 0x40
GUI_PANEL_MOVEABLE = 0x80,
GUI_PANEL_SCALEABLE = 0x100,
GUI_PANEL_OVERLAP = 0x100,
}; };
struct gui_panel { struct gui_panel {
@ -407,8 +404,6 @@ gui_bool gui_panel_check(struct gui_panel *p, const char *s, gui_size l, gui_boo
gui_bool gui_panel_option(struct gui_panel *p, const char *s, gui_size l, gui_bool a); gui_bool gui_panel_option(struct gui_panel *p, const char *s, gui_size l, gui_bool a);
gui_bool gui_panel_button_text(struct gui_panel *panel, const char *str, gui_size len, gui_bool gui_panel_button_text(struct gui_panel *panel, const char *str, gui_size len,
enum gui_button_behavior behavior); enum gui_button_behavior behavior);
gui_bool gui_panel_button_invisible(struct gui_panel *panel, const char *str, gui_size len,
enum gui_button_behavior behavior);
gui_bool gui_panel_button_color(struct gui_panel *panel, const struct gui_color color, gui_bool gui_panel_button_color(struct gui_panel *panel, const struct gui_color color,
enum gui_button_behavior behavior); enum gui_button_behavior behavior);
gui_bool gui_panel_button_triangle(struct gui_panel *panel, enum gui_heading heading, gui_bool gui_panel_button_triangle(struct gui_panel *panel, enum gui_heading heading,
@ -434,8 +429,6 @@ gui_int gui_panel_plot(struct gui_panel *panel, const gui_float *values,
gui_size value_count); gui_size value_count);
gui_int gui_panel_histo(struct gui_panel *panel, const gui_float *values, gui_int gui_panel_histo(struct gui_panel *panel, const gui_float *values,
gui_size value_count); gui_size value_count);
void gui_panel_text_box(struct gui_panel *panel, const gui_char *text, gui_size *len,
gui_float *offset);
gui_float gui_panel_list(struct gui_panel *panel, gui_bool *selected, const char *items[], gui_float gui_panel_list(struct gui_panel *panel, gui_bool *selected, const char *items[],
gui_size item_count, gui_float offset, gui_float item_height); gui_size item_count, gui_float offset, gui_float item_height);
void gui_panel_frame_begin(struct gui_panel *panel, struct gui_panel *tab, const char *title); void gui_panel_frame_begin(struct gui_panel *panel, struct gui_panel *tab, const char *title);

View File

@ -27,6 +27,11 @@
#include "gui.h" #include "gui.h"
/* ---- TODO ------
* - Font loading
* - X11 -> SDL
*/
/* macros */ /* macros */
#define WIN_WIDTH 800 #define WIN_WIDTH 800
#define WIN_HEIGHT 600 #define WIN_HEIGHT 600
@ -68,12 +73,8 @@ struct GUI {
/* State */ /* State */
gui_char input_text[INPUT_MAX]; gui_char input_text[INPUT_MAX];
gui_char cmd_input[INPUT_MAX]; gui_char cmd_input[INPUT_MAX];
gui_char box_input[INPUT_MAX];
gui_size input_len; gui_size input_len;
gui_size cmd_len; gui_size cmd_len;
gui_size box_len;
gui_float box_off;
gui_bool box_act;
gui_bool typing; gui_bool typing;
gui_float slider; gui_float slider;
gui_size prog; gui_size prog;
@ -101,10 +102,7 @@ static void bpress(struct GUI*, XEvent*);
static void brelease(struct GUI*, XEvent*); static void brelease(struct GUI*, XEvent*);
static void bmotion(struct GUI*, XEvent*); static void bmotion(struct GUI*, XEvent*);
static void resize(struct GUI*, XEvent*); static void resize(struct GUI*, XEvent*);
static GLuint ldbmp(gui_byte*, uint32_t*, uint32_t*); static GLuint ldbmp(gui_byte*, uint32_t*, uint32_t*);
static struct gui_font *ldfont(const char*, unsigned char);
static void delfont(struct gui_font*);
/* gobals */ /* gobals */
static void static void
@ -304,10 +302,7 @@ ldbmp(gui_byte *data, uint32_t *width, uint32_t *height)
static struct gui_font* static struct gui_font*
ldfont(const char *name, unsigned char height) ldfont(const char *name, unsigned char height)
{ {
union conversion { gui_texture tex;
gui_texture handle;
uintptr_t ptr;
} convert;
size_t size; size_t size;
struct gui_font *font; struct gui_font *font;
uint32_t img_width, img_height; uint32_t img_width, img_height;
@ -322,14 +317,12 @@ ldfont(const char *name, unsigned char height)
uint16_t tex_width; uint16_t tex_width;
uint16_t tex_height; uint16_t tex_height;
/* header */
gui_byte *buffer = (gui_byte*)ldfile(name, O_RDONLY, &size); gui_byte *buffer = (gui_byte*)ldfile(name, O_RDONLY, &size);
memcpy(&num, buffer, sizeof(uint16_t)); memcpy(&num, buffer, sizeof(uint16_t));
memcpy(&indexes, &buffer[0x02], sizeof(uint16_t)); memcpy(&indexes, &buffer[0x02], sizeof(uint16_t));
memcpy(&tex_width, &buffer[0x04], sizeof(uint16_t)); memcpy(&tex_width, &buffer[0x04], sizeof(uint16_t));
memcpy(&tex_height, &buffer[0x06], sizeof(uint16_t)); memcpy(&tex_height, &buffer[0x06], sizeof(uint16_t));
/* glyphes */
iter = &buffer[0x08]; iter = &buffer[0x08];
mem = sizeof(struct gui_font_glyph) * ((size_t)indexes + 1); mem = sizeof(struct gui_font_glyph) * ((size_t)indexes + 1);
glyphes = xcalloc(mem, 1); glyphes = xcalloc(mem, 1);
@ -361,15 +354,10 @@ ldfont(const char *name, unsigned char height)
iter += 22; iter += 22;
} }
/* texture */
convert.ptr = ldbmp(iter, &img_width, &img_height);
assert(img_width == tex_width && img_height == tex_height);
/* font */
font = xcalloc(sizeof(struct gui_font), 1); font = xcalloc(sizeof(struct gui_font), 1);
font->height = height; font->height = height;
font->scale = (float)height/(float)max_height; font->scale = (float)height/(float)max_height;
font->texture = convert.handle; font->texture.gl = ldbmp(iter, &img_width, &img_height);
font->tex_size.x = tex_width; font->tex_size.x = tex_width;
font->tex_size.y = tex_height; font->tex_size.y = tex_height;
font->fallback = &glyphes['?']; font->fallback = &glyphes['?'];
@ -435,7 +423,7 @@ draw(int width, int height, const struct gui_draw_call_list **list, gui_size cou
w = (int)cmd->clip_rect.w; w = (int)cmd->clip_rect.w;
h = (int)cmd->clip_rect.h; h = (int)cmd->clip_rect.h;
glScissor(x, y, w, h); glScissor(x, y, w, h);
glBindTexture(GL_TEXTURE_2D, (GLuint)(unsigned long)cmd->texture); glBindTexture(GL_TEXTURE_2D, (GLuint)(unsigned long)cmd->texture.gl);
glDrawArrays(GL_TRIANGLES, offset, (GLsizei)cmd->vertex_count); glDrawArrays(GL_TRIANGLES, offset, (GLsizei)cmd->vertex_count);
offset += (GLint)cmd->vertex_count; offset += (GLint)cmd->vertex_count;
} }
@ -510,6 +498,10 @@ main(int argc, char *argv[])
gui.main.vertex_size = MAX_BUFFER - gui.main.clip_size - gui.main.command_size; gui.main.vertex_size = MAX_BUFFER - gui.main.clip_size - gui.main.command_size;
gui_default_config(&gui.config); gui_default_config(&gui.config);
gui_panel_init(&gui.panel, &gui.config, gui.font, &gui.in); gui_panel_init(&gui.panel, &gui.config, gui.font, &gui.in);
gui.config.colors[GUI_COLOR_TEXT].r = 255;
gui.config.colors[GUI_COLOR_TEXT].g = 255;
gui.config.colors[GUI_COLOR_TEXT].b = 255;
gui.config.colors[GUI_COLOR_TEXT].a = 255;
xw.running = 1; xw.running = 1;
while (xw.running) { while (xw.running) {
@ -554,7 +546,6 @@ main(int argc, char *argv[])
gui.seloff = gui_panel_list(&gui.panel, gui.selection, sel, LEN(sel), gui.seloff, 30); gui.seloff = gui_panel_list(&gui.panel, gui.selection, sel, LEN(sel), gui.seloff, 30);
gui_panel_histo(&gui.panel, values, LEN(values)); gui_panel_histo(&gui.panel, values, LEN(values));
gui_panel_plot(&gui.panel, values, LEN(values)); gui_panel_plot(&gui.panel, values, LEN(values));
gui_panel_text_box(&gui.panel, gui.box_input, &gui.box_len, &gui.box_off);
gui_panel_end(&gui.panel); gui_panel_end(&gui.panel);
gui_end(&gui.out, &gui.draw_list, &gui.status); gui_end(&gui.out, &gui.draw_list, &gui.status);
/* ---------------------------------------------------------*/ /* ---------------------------------------------------------*/