瀏覽代碼

fixed panel spacing

vurtun 10 年之前
父節點
當前提交
523a604258
共有 4 個文件被更改,包括 26 次插入36 次删除
  1. 10 9
      Makefile
  2. 7 17
      gui.c
  3. 2 2
      gui.h
  4. 7 8
      opengl.c

+ 10 - 9
Makefile

@@ -6,29 +6,30 @@ CC = gcc
 DCC = clang
 
 # Flags
-CFLAGS = -std=c89 -pedantic-errors -Wdeprecated-declarations -D_POSIX_C_SOURCE=200809L
-CFLAGS = -g -Wextra -Werror -Wformat -Wunreachable-code
+CFLAGS = -std=c89 -pedantic-errors -Wdeprecated-declarations
+CFLAGS = -g -Wall -Wextra -Wformat-security -Wunreachable-code
 CFLAGS += -fstack-protector-strong -Winline -Wshadow -Wwrite-strings -fstrict-aliasing
-CFLAGS += -Wstrict-prototypes -Wold-style-definition -Wconversion
+CFLAGS += -Wstrict-prototypes -Wold-style-definition -Wconversion -Wfloat-equal
 CFLAGS += -Wredundant-decls -Wnested-externs -Wmissing-include-dirs
 CFLAGS += -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wmissing-prototypes -Wconversion
 CFLAGS += -Wswitch-default -Wundef -Wno-unused -Wstrict-overflow=5 -Wsign-conversion
-CFLAGS += -Winit-self -Wstrict-aliasing -fsanitize=address -fsanitize=undefined
+CFLAGS += -Winit-self -Wstrict-aliasing -fsanitize=address -fsanitize=undefined -ftrapv
+CFLAGS += -Wswitch-enum -Winvalid-pch
 
 SRC = gui.c opengl.c
 OBJ = $(SRC:.c=.o)
 
 # Modes
-.PHONY: gcc
-gcc: CC = gcc
-gcc: $(BIN)
-
 .PHONY: clang
 clang: CC = clang
 clang: $(BIN)
 
+.PHONY: gcc
+gcc: CC = gcc
+gcc: $(BIN)
+
 $(BIN):
 	@mkdir -p bin
 	rm -f bin/$(BIN) $(OBJS)
-	$(CC) $(SRC) $(CFLAGS) -o bin/$(BIN) -lX11 -lGL -lGLU
+	$(CC) $(SRC) -D_POSIX_C_SOURCE=200809L $(CFLAGS) -o bin/$(BIN) -lX11 -lGL -lGLU
 

+ 7 - 17
gui.c

@@ -7,8 +7,6 @@
 
 #define NULL (void*)0
 #define UTF_INVALID 0xFFFD
-#define PI 3.141592654f
-
 #define PASTE(a,b) a##b
 #define MIN(a,b) ((a) < (b) ? (a) : (b))
 #define MAX(a,b) ((a) < (b) ? (b) : (a))
@@ -16,8 +14,6 @@
 #define LEN(a) (sizeof(a)/sizeof(a)[0])
 #define ABS(a) (((a) < 0) ? -(a) : (a))
 #define UNUSED(a) ((void)(a))
-#define DEG2RAD(a) ((a) * (PI / 180.0f))
-#define RAD2DEG(a) ((a) * (180.0f / PI))
 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
 #define INBOX(x, y, x0, y0, x1, y1) (BETWEEN(x, x0, x1) && BETWEEN(y, y0, y1))
 
@@ -29,9 +25,7 @@
 #define vec2_mov(to,from) (to).x = (from).x, (to).y = (from).y
 #define vec2_len(v) ((float)fsqrt((v).x*(v).x+(v).y*(v).y))
 #define vec2_sub(r,a,b) do {(r).x=(a).x-(b).x; (r).y=(a).y-(b).y;} while(0)
-#define vec2_add(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_norm(r, v) do {float _ = vec2_len(v); if (_) vec2_muls(r, v, 1.0f/_);} while(0)
 
 static const gui_texture null_tex = (void*)0;
 static const struct gui_rect null_rect = {-9999, -9999, 9999 * 2, 9999 * 2};
@@ -50,9 +44,7 @@ fsqrt(float x)
     val.f = x;
     val.i = 0x5f375a86 - (val.i>>1);
     val.f = val.f*(1.5f-xhalf*val.f*val.f);
-    if (val.f != 0.0f)
-        return 1.0f/val.f;
-    return val.f;
+    return 1.0f/val.f;
 }
 
 static void*
@@ -159,7 +151,7 @@ gui_default_config(struct gui_config *config)
     config->header_height = 50.0f;
     config->scrollbar_width = 16;
     config->scroll_factor = 2;
-    config->panel_padding = gui_make_vec2(8.0f, 8.0f);
+    config->panel_padding = gui_make_vec2(10.0f, 10.0f);
     config->panel_min_size = gui_make_vec2(32.0f, 32.0f);
     config->item_spacing = gui_make_vec2(8.0f, 4.0f);
     config->item_padding = gui_make_vec2(4.0f, 4.0f);
@@ -373,8 +365,7 @@ gui_vertex_line(struct gui_draw_buffer* buffer, gui_float x0, gui_float y0,
     vec2_load(b, x1, y1);
     vec2_sub(d, b, a);
 
-    len = vec2_len(d);
-    if (len) len = 0.5f / vec2_len(d);
+    len = 0.5f / vec2_len(d);
     vec2_muls(hn, d, len);
     vec2_load(hp0, +hn.y, -hn.x);
     vec2_load(hp1, -hn.y, +hn.x);
@@ -602,7 +593,6 @@ gui_slider(struct gui_draw_buffer *buffer, const struct gui_slider *slider,
     gui_float cursor_w;
 
     if (!buffer || !slider || !in) return 0;
-    if (slider->step == 0.0f) return slider->value;
     x = slider->x; y = slider->y;
     w = MAX(slider->w, 2 * slider->pad_x);
     h = MAX(slider->h, 2 * slider->pad_y);
@@ -825,8 +815,8 @@ gui_histo(struct gui_draw_buffer *buffer, const struct gui_histo *histo,
     gui_rectf(buffer, x, y, w, h, histo->background);
     canvas_x = x + histo->pad_x;
     canvas_y = y + histo->pad_y;
-    canvas_w = w - 2 * histo->pad_x;
-    canvas_h = h - 2 * histo->pad_y;
+    canvas_w = MAX(0, w - 2 * histo->pad_x);
+    canvas_h = MAX(0, h - 2 * histo->pad_y);
     if (histo->value_count) {
         gui_float padding = (gui_float)(histo->value_count-1) * histo->pad_x;
         item_w = (canvas_w - padding) / (gui_float)(histo->value_count);
@@ -1024,10 +1014,10 @@ gui_panel_alloc(struct gui_rect *bounds, struct gui_panel *panel)
     space  = panel->width - padding - spacing;
 
     item_width = space / (gui_float)panel->row_columns;
-    item_offset = config->item_padding.x + (gui_float)panel->index * item_width;
+    item_offset = (gui_float)panel->index * item_width;
     item_spacing = (gui_float)panel->index * config->item_spacing.x;
 
-    bounds->x = panel->x + item_offset + item_spacing;
+    bounds->x = panel->x + item_offset + item_spacing + config->panel_padding.x;
     bounds->y = panel->at_y;
     bounds->w = item_width;
     bounds->h = panel->row_height - config->item_spacing.y;

+ 2 - 2
gui.h

@@ -102,7 +102,7 @@ struct gui_font_glyph {
 };
 
 struct gui_font {
-    gui_short height;
+    gui_float height;
     gui_float scale;
     gui_texture texture;
     struct gui_vec2 tex_size;
@@ -128,8 +128,8 @@ struct gui_toggle {
     gui_float w, h;
     gui_float pad_x, pad_y;
     gui_int active;
-    const char *text;
     gui_size length;
+    const char *text;
     struct gui_color font;
     struct gui_color background;
     struct gui_color foreground;

+ 7 - 8
opengl.c

@@ -34,16 +34,13 @@
 #define MAX_BUFFER (32 * 1024)
 #define INPUT_MAX 64
 
-#define MIN(a,b)((a) < (b) ? (a) : (b))
-#define MAX(a,b)((a) < (b) ? (b) : (a))
-#define CLAMP(i,v,x) (MAX(MIN(v,x), i))
 #define LEN(a)(sizeof(a)/sizeof(a)[0])
 #define UNUSED(a)((void)(a))
 
 /* types  */
 struct XWindow {
-    Display *dpy;
     Window root;
+    Display *dpy;
     XVisualInfo *vi;
     Colormap cmap;
     XSetWindowAttributes swa;
@@ -139,6 +136,7 @@ kpress(struct GUI *gui, XEvent* e)
     else if ((*keysym >= 'a' && *keysym <= 'z') ||
             (*keysym >= '0' && *keysym <= '9'))
         gui_input_char(&gui->in, (unsigned char*)keysym);
+    XFree(keysym);
 }
 
 static void
@@ -157,6 +155,7 @@ krelease(struct GUI *gui, XEvent* e)
         gui_input_key(&gui->in, GUI_KEY_ENTER, gui_false);
     else if (*keysym == XK_BackSpace)
         gui_input_key(&gui->in, GUI_KEY_BACKSPACE, gui_false);
+    XFree(keysym);
 }
 
 static void
@@ -437,12 +436,10 @@ main(int argc, char *argv[])
     memset(&xw, 0, sizeof xw);
     memset(&gui, 0, sizeof gui);
     xw.dpy = XOpenDisplay(NULL);
-    if (!xw.dpy)
-        die("XOpenDisplay failed\n");
+    if (!xw.dpy) die("XOpenDisplay failed\n");
     xw.root = DefaultRootWindow(xw.dpy);
     xw.vi = glXChooseVisual(xw.dpy, 0, att);
-    if (!xw.vi)
-        die("Failed to find appropriate visual\n");
+    if (!xw.vi) die("Failed to find appropriate visual\n");
     xw.cmap = XCreateColormap(xw.dpy,xw.root,xw.vi->visual,AllocNone);
     xw.swa.colormap = xw.cmap;
     xw.swa.event_mask =
@@ -515,7 +512,9 @@ main(int argc, char *argv[])
     }
 
     /* Cleanup */
+    free(buffer);
     delfont(gui.font);
+    XFree(xw.vi);
     glXMakeCurrent(xw.dpy, None, NULL);
     glXDestroyContext(xw.dpy, xw.glc);
     XDestroyWindow(xw.dpy, xw.win);