Browse Source

measure_text_ex etc

Karl Zylinski 2 months ago
parent
commit
a4f40ac5fc
2 changed files with 19 additions and 3 deletions
  1. 9 1
      examples/fonts/fonts.odin
  2. 10 2
      karl2d.odin

+ 9 - 1
examples/fonts/fonts.odin

@@ -2,6 +2,7 @@ package karl2d_fonts_example
 
 import k2 "../.."
 import "core:log"
+import "core:fmt"
 
 main :: proc() {
 	context.logger = log.create_console_logger()
@@ -33,7 +34,14 @@ step :: proc() -> bool {
 		font = cat_and_onion_font
 	}
 
-	k2.draw_text_ex(font, "Hellöpe! Hold K to swap font", {20, 20}, 64, k2.WHITE)
+	msg := "Hellöpe! Hold K to swap font"
+	k2.draw_text_ex(font, msg, {20, 20}, 64, k2.WHITE)
+
+	size := k2.measure_text_ex(font, msg, 64)
+	size_msg := fmt.tprintf("The text above takes %.1f x %.1f pixels of space", size.x, size.y)
+
+	k2.draw_text(size_msg, {20, 100}, 32)
+
 	k2.present()
 	free_all(context.temp_allocator)
 	return !k2.shutdown_wanted()

+ 10 - 2
karl2d.odin

@@ -841,11 +841,19 @@ measure_text :: proc(text: string, font_size: f32) -> Vec2 {
 	return {b[2] - b[0], b[3] - b[1]}
 }
 
-draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color) {
+measure_text_ex :: proc(font_handle: Font_Handle, text: string, font_size: f32) -> Vec2 {
+	_set_font(font_handle)
+	fs.SetSize(&s.fs, font_size)
+	b: [4]f32
+	fs.TextBounds(&s.fs, text, bounds = &b)
+	return {b[2] - b[0], b[3] - b[1]}
+}
+
+draw_text :: proc(text: string, pos: Vec2, font_size: f32, color := BLACK) {
 	draw_text_ex(s.default_font, text, pos, font_size, color)
 }
 
-draw_text_ex :: proc(font_handle: Font_Handle, text: string, pos: Vec2, font_size: f32, color: Color) {
+draw_text_ex :: proc(font_handle: Font_Handle, text: string, pos: Vec2, font_size: f32, color := BLACK) {
 	if int(font_handle) >= len(s.fonts) {
 		return
 	}