From: Brendan Hansen Date: Sun, 6 Jun 2021 04:42:01 +0000 (-0500) Subject: moved ui module to onyx core modules X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=bb22075943d4b5326264109d6f9074a3b7a4080b;p=tower.git moved ui module to onyx core modules --- diff --git a/src/build.onyx b/src/build.onyx index 85329ac..9a96cbd 100644 --- a/src/build.onyx +++ b/src/build.onyx @@ -7,8 +7,9 @@ #load "modules/webgl2/module" #load "modules/js_events/module" #load "modules/immediate_mode/module" +#load "modules/ui/module" -#load "src/ui/ui" +// #load "src/ui/ui" #load "src/config" #load "src/font/bitmap_font" #load "src/tower" diff --git a/src/config.onyx b/src/config.onyx index 7dab26d..50e515c 100644 --- a/src/config.onyx +++ b/src/config.onyx @@ -1,2 +1,2 @@ -DEBUG :: true +DEBUG :: false diff --git a/src/font/bitmap_font.onyx b/src/font/bitmap_font.onyx index 5cb0afa..7feba42 100644 --- a/src/font/bitmap_font.onyx +++ b/src/font/bitmap_font.onyx @@ -22,8 +22,7 @@ The separator color must be present on the entire left and top sides of the glyp - - +@Cleanup // this should be part of the standard set of modules package bitmap_font use package core @@ -58,6 +57,22 @@ Bitmap_Font :: struct { return width; } + + get_height :: (use bmp: ^Bitmap_Font, text: str, size: f32) -> f32 { + tallest_height: f32 = 0; + for char: text { + glyph := map.get_ptr(^glyphs, ~~char); + + if glyph == null { + glyph = map.get_ptr(^glyphs, 255); + assert(glyph != null, "NO NULL GLYPH"); + } + + tallest_height = math.max(tallest_height, glyph.h * size * em); + } + + return tallest_height; + } } Bitmap_Font_Texture :: struct { diff --git a/src/res/font.data b/src/res/font.data deleted file mode 100644 index 615eafb..0000000 Binary files a/src/res/font.data and /dev/null differ diff --git a/src/res/font_2.data b/src/res/font_2.data deleted file mode 100644 index 85287d4..0000000 Binary files a/src/res/font_2.data and /dev/null differ diff --git a/src/tower.onyx b/src/tower.onyx index a59d2df..f52a927 100644 --- a/src/tower.onyx +++ b/src/tower.onyx @@ -8,7 +8,6 @@ use package core #private_file bitmap_font :: package bitmap_font #private_file ui :: package ui - main :: (args: [] cstr) { gl.init("game"); events.init(); @@ -41,26 +40,24 @@ last_time := 0; window_width := 0 window_height := 0 poll_events :: () { - for event: events.consume() do switch event.kind { - case .MouseDown do println("Mouse was down!"); - - case .Resize { - printf("Window was resized to: %i %i\n", event.resize.width, event.resize.height); - - window_width = event.resize.width; - window_height = event.resize.height; - - gl.setSize(event.resize.width, event.resize.height); - gl.viewport(0, 0, event.resize.width, event.resize.height); - gfx.use_ortho_projection(0, ~~window_width, 0, ~~window_height); + for event: events.consume() { + switch event.kind { + case .MouseDown do println("Mouse was down!"); + + case .Resize { + printf("Window was resized to: %i %i\n", event.resize.width, event.resize.height); + + window_width = event.resize.width; + window_height = event.resize.height; + + gl.setSize(event.resize.width, event.resize.height); + gl.viewport(0, 0, event.resize.width, event.resize.height); + gfx.use_ortho_projection(0, ~~window_width, 0, ~~window_height); + } } - } -} -#if DEBUG { - fps := 0 - fps_timer := 1.0f - frames := 0 + #if DEBUG { updates_this_frame += 1; } + } } update :: (dt: f32) { @@ -80,33 +77,33 @@ draw :: () { gl.clearColor(0.1, 0.1, 0.1, 1); gl.clear(gl.COLOR_BUFFER_BIT); - #if false { - ui.draw_text("Hello World", 100, 100, 128); - ui.draw_text("something else...", 100, 230, 32, .{1,0,0}); - - ui.draw_text("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 100, 300, 16); - ui.draw_text("abcdefghijklmnopqrstuvwxyz", 100, 340, 16); - - gfx.set_texture(); - gfx.rect(.{100, 400}, .{200, 200}, .{0, 0, 1}); - } - master_rectangle := ui.Rectangle.{ x1 = ~~window_width, y1 = ~~window_height }; - left, right := ui.Flow.split_vertical(master_rectangle, left_percent=0.4); - right_top, right_bottom := ui.Flow.split_horizontal(right, top_height=200); + left, right := ui.Flow.split_vertical(master_rectangle, left_width=400); + right_top, right_bottom := ui.Flow.split_horizontal(right, top_percent=.6); - ui.draw_rect(left, .{ 0.5, 0, 0.5 }); + ui.draw_rect(left, .{ 0.2, 0.15, 0.15 }); ui.draw_rect(right, .{ 0, 0.25, 0.5 }); ui.draw_rect(right_top, .{ 1, 0, 0 }); ui.draw_text("Something here", right_bottom.x0, right_bottom.y0, 48, .{ 1, 1, 1 }); - #if DEBUG { - fps_buffer : [16] u8; - fps_str := conv.str_format("FPS: %i", ~~ fps_buffer, fps); - ui.draw_text(fps_str, 0, 0, 32, .{0,1,0}); + { + button_rect, _ := ui.Flow.split_horizontal(left, top_height=75); + ui.draw_button(button_rect, "Test Button"); } + #if DEBUG { + Debug_Info_Font_Size :: 32; + + fps_buffer : [24] u8; + fps_str := conv.str_format("%i %i", ~~ fps_buffer, fps, updates_this_frame); + + gfx.set_texture(); + gfx.rect(.{ 0, 0 }, .{ 160, 36 }, .{0,0,0,.8}); + ui.draw_text(fps_str, 0, 0, ~~Debug_Info_Font_Size, .{0,1,0}); + + updates_this_frame = 0; + } gfx.flush(); } @@ -114,3 +111,13 @@ draw :: () { + +// Debug variables +#if DEBUG { + fps := 0 + fps_timer := 1.0f + frames := 0 + + updates_this_frame := 0 +} + diff --git a/src/ui/ui.onyx b/src/ui/ui.onyx deleted file mode 100644 index ccb6b2f..0000000 --- a/src/ui/ui.onyx +++ /dev/null @@ -1,168 +0,0 @@ -package ui - -#private_file gfx :: package immediate_mode -#private_file bitmap_font :: package bitmap_font -#private_file gl :: package gl -#private_file math :: package core.math - -#private font : bitmap_font.Bitmap_Font; -#private font_texture : gl.GLTexture; - -@Temporary -DEFAULT_TEXT_SIZE :: 32.0f - - -init_ui :: () { - init_font(); -} - -#private init_font :: () { - font_data := #file_contents "./../res/font_2.data"; - - bft := bitmap_font.Bitmap_Font_Texture.{ - data = font_data, - width = 256, - height = 256, - }; - - font = bitmap_font.bitmap_font_create(bft, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 \xff:"); - - font_texture = gl.createTexture(); - gl.bindTexture(gl.TEXTURE_2D, font_texture); - gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, font_data); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); - gl.bindTexture(gl.TEXTURE_2D, -1); -} - -get_text_width :: (text: str, size: f32 = DEFAULT_TEXT_SIZE) -> f32 { - return font->get_width(text, size); -} - -// 'x' and 'y' are the top-left coordinates of the text. 'y' is NOT the baseline. -draw_text :: (text: str, x: f32, y: f32, size := DEFAULT_TEXT_SIZE, color := gfx.Color4.{1,1,1}) { - gl.bindTexture(gl.TEXTURE_2D, font_texture); - gfx.set_texture(0); - - for char: text { - glyph := font->get_glyph(char); - - if glyph == null { - glyph = font->get_glyph(255); - assert(glyph != null, "NO NULL GLYPH"); - } - - gfx.textured_rect( - .{ x, y }, - .{ glyph.w * size * font.em, glyph.h * size * font.em }, - .{ glyph.x0, glyph.y0 }, - .{ glyph.x1 - glyph.x0, glyph.y1 - glyph.y0 }, - color = color); - - x += glyph.w * size * font.em; - } - - gfx.flush(); - gl.bindTexture(gl.TEXTURE_2D, -1); -} - -draw_rect :: proc { - (use r: Rectangle, color := gfx.Color4.{1,1,1}) { - gfx.set_texture(); - - width, height := Rectangle.dimensions(r); - gfx.rect(.{ x0, y0 }, .{ width, height }, color); - }, - - (x: f32, y: f32, w: f32, h: f32, color := gfx.Color4.{1,1,1}) { - gfx.set_texture(); - gfx.rect(.{ x, y }, .{ w, h }, color); - } -} - - - -Rectangle :: struct { - // - // x0,y0 ------------+ - // | | - // | | - // +------------ x1, y1 - // - - x0: f32 = 0; - y0: f32 = 0; - x1: f32 = 0; - y1: f32 = 0; - - width :: (use r: Rectangle) -> f32 do return math.abs(x1 - x0); - height :: (use r: Rectangle) -> f32 do return math.abs(y1 - y0); - - dimensions :: (use r: Rectangle) -> (width: f32, height: f32) { - return math.abs(x1 - x0), math.abs(y1 - y0); - } - - top_left :: (use r: Rectangle) -> (x: f32, y: f32) do return math.min(x0, x1), math.min(y0, y1); - bottom_right :: (use r: Rectangle) -> (x: f32, y: f32) do return math.max(x0, x1), math.max(y0, y1); -} - - - -// UI Flow - -Flow :: struct { - split_vertical :: proc { - (r: Rectangle, left_percent: f32) -> (left: Rectangle, right: Rectangle) { - return split_vertical(r, left_width=left_percent * Rectangle.width(r)); - }, - - (r: Rectangle, right_percent: f32) -> (left: Rectangle, right: Rectangle) { - return split_vertical(r, right_width=right_percent * Rectangle.width(r)); - }, - - (r: Rectangle, left_width: f32) -> (left: Rectangle, right: Rectangle) { - x0, y0 := Rectangle.top_left(r); - x1, y1 := Rectangle.bottom_right(r); - - return .{ x0=x0, x1=x0+left_width, y0=y0, y1=y1 }, - .{ x0=x0+left_width, x1=x1, y0=y0, y1=y1 }; - }, - - (r: Rectangle, right_width: f32) -> (left: Rectangle, right: Rectangle) { - x0, y0 := Rectangle.top_left(r); - x1, y1 := Rectangle.bottom_right(r); - - return .{ x0=x0, x1=x1-right_width, y0=y0, y1=y1 }, - .{ x0=x1-right_width, x1=x1, y0=y0, y1=y1 }; - } - } - - - split_horizontal :: proc { - (r: Rectangle, top_percent: f32) -> (top: Rectangle, bottom: Rectangle) { - return split_horizontal(r, top_height=top_percent * Rectangle.height(r)); - }, - - (r: Rectangle, bottom_percent: f32) -> (top: Rectangle, bottom: Rectangle) { - return split_horizontal(r, bottom_height=bottom_percent * Rectangle.height(r)); - }, - - (r: Rectangle, top_height: f32) -> (top: Rectangle, bottom: Rectangle) { - x0, y0 := Rectangle.top_left(r); - x1, y1 := Rectangle.bottom_right(r); - - return .{ x0=x0, x1=x1, y0=y0, y1=y0+top_height }, - .{ x0=x0, x1=x1, y0=y0+top_height, y1=y1 }; - }, - - (r: Rectangle, bottom_height: f32) -> (top: Rectangle, bottom: Rectangle) { - x0, y0 := Rectangle.top_left(r); - x1, y1 := Rectangle.bottom_right(r); - - return .{ x0=x0, x1=x1, y0=y0, y1=y1-bottom_height }, - .{ x0=x0, x1=x1, y0=y1-bottom_height, y1=y1 }; - } - } -} \ No newline at end of file