finished font loading

This commit is contained in:
vurtun 2015-03-05 18:14:47 +01:00
parent a0dcd2c090
commit 03e1e72c62
4 changed files with 38 additions and 34 deletions

14
gui.c
View File

@ -143,10 +143,6 @@ gui_input_key(struct gui_input *in, enum gui_keys key, gui_int down)
{ {
if (!in) return; if (!in) return;
in->keys[key] = down; in->keys[key] = down;
if (key == GUI_KEY_SHIFT)
in->shift = gui_true;
if (key == GUI_KEY_CTRL)
in->ctrl = gui_true;
} }
void void
@ -313,6 +309,13 @@ gui_rectf(struct gui_draw_list *list, gui_float x, gui_float y,
gui_vertex(cmd, x, y + h, col); gui_vertex(cmd, x, y + h, col);
} }
static void
gui_text(const struct gui_draw_list *list, gui_float x, gui_float y,
gui_float w, gui_float h, const gui_char *txt, gui_size len)
{
}
void void
gui_begin(struct gui_draw_list *list, gui_byte *memory, gui_size size) gui_begin(struct gui_draw_list *list, gui_byte *memory, gui_size size)
{ {
@ -524,8 +527,7 @@ gui_scroll(struct gui_draw_list *list, const struct gui_input *in,
cursor_py = cursor_y + cursor_h; cursor_py = cursor_y + cursor_h;
inscroll = INBOX(mouse_x, mouse_y, x, y, x + w, y + h); inscroll = INBOX(mouse_x, mouse_y, x, y, x + w, y + h);
incursor = INBOX(prev_x, prev_y, cursor_x, cursor_y, cursor_px, cursor_py); incursor = INBOX(prev_x, prev_y, cursor_x, cursor_y, cursor_px, cursor_py);
if (in->mouse_down && inscroll && incursor) if (in->mouse_down && inscroll && incursor) {
{
const gui_float pixel = in->mouse_delta.y; const gui_float pixel = in->mouse_delta.y;
const gui_float delta = (pixel/(gui_float)bar_h) * (gui_float)dst; const gui_float delta = (pixel/(gui_float)bar_h) * (gui_float)dst;
offset = CLAMP(0, offset + delta, dst - bar_h); offset = CLAMP(0, offset + delta, dst - bar_h);

2
gui.h
View File

@ -65,8 +65,6 @@ struct gui_input {
gui_bool keys[GUI_KEY_MAX]; gui_bool keys[GUI_KEY_MAX];
gui_glyph text[GUI_INPUT_MAX]; gui_glyph text[GUI_INPUT_MAX];
gui_size glyph_count; gui_size glyph_count;
gui_bool shift;
gui_bool ctrl;
struct gui_vec2 mouse_pos; struct gui_vec2 mouse_pos;
struct gui_vec2 mouse_prev; struct gui_vec2 mouse_prev;
struct gui_vec2 mouse_delta; struct gui_vec2 mouse_delta;

BIN
mono.font

Binary file not shown.

56
x11.c
View File

@ -38,6 +38,7 @@
#define CLAMP(i,v,x) (MAX(MIN(v,x), i)) #define CLAMP(i,v,x) (MAX(MIN(v,x), i))
#define LEN(a)(sizeof(a)/sizeof(a)[0]) #define LEN(a)(sizeof(a)/sizeof(a)[0])
#define UNUSED(a)((void)(a)) #define UNUSED(a)((void)(a))
#define glerror() glerror_(__FILE__, __LINE__)
/* types */ /* types */
struct XWindow { struct XWindow {
@ -207,6 +208,24 @@ ldfile(const char* path, int flags, size_t* siz)
return buf; return buf;
} }
static void
glerror_(const char *file, int line)
{
const GLenum code = glGetError();
if (code == GL_INVALID_ENUM)
fprintf(stdout, "[GL] Error: (%s:%d) invalid value!\n", file, line);
else if (code == GL_INVALID_OPERATION)
fprintf(stdout, "[GL] Error: (%s:%d) invalid operation!\n", file, line);
else if (code == GL_INVALID_FRAMEBUFFER_OPERATION)
fprintf(stdout, "[GL] Error: (%s:%d) invalid frame op!\n", file, line);
else if (code == GL_OUT_OF_MEMORY)
fprintf(stdout, "[GL] Error: (%s:%d) out of memory!\n", file, line);
else if (code == GL_STACK_UNDERFLOW)
fprintf(stdout, "[GL] Error: (%s:%d) stack underflow!\n", file, line);
else if (code == GL_STACK_OVERFLOW)
fprintf(stdout, "[GL] Error: (%s:%d) stack overflow!\n", file, line);
}
static struct gui_font* static struct gui_font*
ldfont(const char *name, unsigned char height) ldfont(const char *name, unsigned char height)
{ {
@ -220,9 +239,7 @@ ldfont(const char *name, unsigned char height)
short i = 0; short i = 0;
uint32_t bpp; uint32_t bpp;
short max_height = 0; short max_height = 0;
uint32_t ioff;
uint32_t iheight;
uint32_t iwidth;
/* header */ /* header */
unsigned char *buffer = (unsigned char*)ldfile(name, O_RDONLY, &size); unsigned char *buffer = (unsigned char*)ldfile(name, O_RDONLY, &size);
@ -262,17 +279,19 @@ ldfont(const char *name, unsigned char height)
header = iter; header = iter;
assert(header[0] == 'B'); assert(header[0] == 'B');
assert(header[1] == 'M'); assert(header[1] == 'M');
ioff = *(uint32_t*)(&header[0x0A]);
data = header + 54; data = iter + ioff;
glGenTextures(1, &texture); glGenTextures(1, &texture);
convert.ptr = texture; convert.ptr = texture;
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, data); GL_BGRA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glerror();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
/* font */ /* font */
font = xcalloc(sizeof(struct gui_font), 1); font = xcalloc(sizeof(struct gui_font), 1);
@ -326,20 +345,6 @@ draw(struct GUI *con, int width, int height, const struct gui_draw_list *list)
glPushMatrix(); glPushMatrix();
glLoadIdentity(); glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, (unsigned long)con->font->texture);
glColor4ub(255,255,255,255);
glBegin(GL_QUADS);
glTexCoord3d(1, 1, 0);
glVertex2f(300, 300);
glTexCoord2d(0, 1);
glVertex2f(50, 300);
glTexCoord2d(0, 0);
glVertex2f(50, 50);
glTexCoord2d(1, 0);
glVertex2f(300, 50);
glEnd();
#if 0
cmd = list->begin; cmd = list->begin;
while (cmd) { while (cmd) {
const int x = (int)cmd->clip_rect.x; const int x = (int)cmd->clip_rect.x;
@ -355,7 +360,6 @@ draw(struct GUI *con, int width, int height, const struct gui_draw_list *list)
glDrawArrays(GL_TRIANGLES, 0, cmd->vertex_count); glDrawArrays(GL_TRIANGLES, 0, cmd->vertex_count);
cmd = gui_next(list, cmd); cmd = gui_next(list, cmd);
} }
#endif
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY);
@ -411,8 +415,6 @@ main(int argc, char *argv[])
XMapWindow(xw.dpy, xw.win); XMapWindow(xw.dpy, xw.win);
XFlush(xw.dpy); XFlush(xw.dpy);
XSync(xw.dpy, False); XSync(xw.dpy, False);
gui.win = &xw;
gui.font = ldfont("mono.font", 16);
/* OpenGL */ /* OpenGL */
xw.glc = glXCreateContext(xw.dpy, xw.vi, NULL, GL_TRUE); xw.glc = glXCreateContext(xw.dpy, xw.vi, NULL, GL_TRUE);
@ -420,6 +422,8 @@ main(int argc, char *argv[])
buffer = xcalloc(MAX_VERTEX_BUFFER, 1); buffer = xcalloc(MAX_VERTEX_BUFFER, 1);
xw.running = 1; xw.running = 1;
gui.win = &xw;
gui.font = ldfont("mono.font", 16);
while (xw.running) { while (xw.running) {
XEvent ev; XEvent ev;
started = timestamp(); started = timestamp();