diff --git a/main.odin b/main.odin index 533cfd4..13c37fe 100644 --- a/main.odin +++ b/main.odin @@ -545,11 +545,43 @@ render_text :: proc(text: string, x, y: i32, color: sdl.Color) { sdl.RenderCopy(app.renderer, texture, nil, &dst_rect) } -render_wrapped_line :: proc(line: string, x, y: i32, max_width: i32) { - if len(line) == 0 do return +render_wrapped_line :: proc(line: string, x, y: i32, max_width: i32) -> i32 { + wrap_text :: proc(text: string, max_line_length: int) -> (lines: [dynamic]string, ok: bool) { + if len(text) < max_line_length { + line := strings.clone(text) + append(&lines, line) + + return lines, true + } + + number_of_lines := math.ceil(f64(len(text)) / f64(max_line_length)) + + builder := strings.builder_make() + for i := 0; i < int(number_of_lines); i += 1 { + start_of_line := max_line_length * i + end_of_line := max_line_length + (max_line_length * i) + + if end_of_line > len(text) do end_of_line = len(text) + + for char in text[start_of_line:end_of_line] { + strings.write_rune(&builder, char) + } + temp := strings.to_string(builder) + strings.builder_reset(&builder) + line := strings.clone(temp) + append(&lines, line) + } + strings.builder_destroy(&builder) + + ok = true + return + } + + + if len(line) == 0 do return 0 chars_per_line := int(max_width / app.char_width) - if chars_per_line <= 0 do return + if chars_per_line <= 0 do return 0 // Simple word wrapping - break at word boundaries when possible words := strings.split(line, " ") @@ -559,6 +591,19 @@ render_wrapped_line :: proc(line: string, x, y: i32, max_width: i32) { line_y := y for word in words { + // current_line = word + + // new method + if len(word) > chars_per_line { + lines, ok := wrap_text(word, chars_per_line) + for line in lines { + render_text(line, x, line_y, app.config.colours.text) + line_y += app.char_height + } + continue + } + + // old method test_line := current_line if len(current_line) > 0 { test_line = strings.concatenate({current_line, " ", word}) @@ -581,6 +626,10 @@ render_wrapped_line :: proc(line: string, x, y: i32, max_width: i32) { // Render final line if len(current_line) > 0 { render_text(current_line, x, line_y, app.config.colours.text) + line_y += app.char_height } + + + return line_y }