From 05dd26804a5ad324fbf34ca21c1792c2c3a7bd0a Mon Sep 17 00:00:00 2001 From: Grant Horner Date: Mon, 8 Dec 2025 21:18:47 -0500 Subject: [PATCH] wip raylib --- .gitignore | 1 + src/main.odin | 113 +++++++++++++++++++++++++++++++++++++++++++ type_conversions.txt | 37 ++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 .gitignore create mode 100644 src/main.odin create mode 100644 type_conversions.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba077a4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin diff --git a/src/main.odin b/src/main.odin new file mode 100644 index 0000000..7d69a07 --- /dev/null +++ b/src/main.odin @@ -0,0 +1,113 @@ +package main + +import "core:fmt" +import "core:mem" +import "core:os" +import "core:strings" +import utf8 "core:unicode/utf8" +import r "vendor:raylib" + +read_file :: proc(path: string) -> (string, bool) { + content: []u8 + success: bool + content, success = os.read_entire_file(path) + return string(content), success +} + +Cursor :: struct { + line: i32, + char: i32, +} + +Line :: struct { + start: i32, + end: i32 +} + +font_size: f32 = 20.0 +text_height: f32 +cursor: Cursor +content: [dynamic]u8 +lines: [dynamic]Line + +render_cursor :: proc(cursor: ^Cursor) { + x := cursor.char * i32(font_size / 2) + y := i32(font_size) * i32(cursor.line) + r.DrawLine(x, y, x, y + i32(text_height), r.WHITE) + r.DrawLine(x + 1, y, x + 1, y + i32(text_height), r.WHITE) +} + +main :: proc() { + r.InitWindow(800, 600, "odit") + r.SetTargetFPS(60) + font := r.LoadFont("/Users/grant/Library/Fonts/BerkeleyMono-Regular.otf") + r.GuiSetFont(font) + two_lines_size := r.MeasureTextEx(font, "foo\nbar", font_size, 0.0) + text_height = two_lines_size[1] / 2 + + content, _success := read_file("src/main.odin") + + + + text: string + dirty := true + for !r.WindowShouldClose() { + for c := r.GetCharPressed(); c != rune(0); c = r.GetCharPressed() { + chars, _ := utf8.encode_rune(c) + char := chars[0] + inject_at(&lines[cursor.line], cursor.char, char) + cursor.char += 1 + } + + if r.IsKeyPressed(r.KeyboardKey.RIGHT) { + cursor.char += 1 + } + if r.IsKeyPressed(r.KeyboardKey.LEFT) { + cursor.char -= 1 + } + if r.IsKeyPressed(r.KeyboardKey.UP) { + cursor.line -= 1 + } + if r.IsKeyPressed(r.KeyboardKey.DOWN) { + cursor.line += 1 + } + if r.IsKeyPressed(r.KeyboardKey.BACKSPACE) { + if cursor.line == 0 && cursor.char == 0 do continue + if cursor.char == 0 { + append(&lines[cursor.line - 1], string(lines[cursor.line][:])) + ordered_remove(&lines, cursor.line) + cursor.line -= 1 + cursor.char = i32(len(lines[cursor.line])) + } else { + ordered_remove(&lines[cursor.line], cursor.char - 1) + cursor.char -= 1 + } + } + + r.BeginDrawing() + r.ClearBackground(r.BLACK) + + for line, line_index in lines { + line := line + if len(line) == cap(line) { + reserve(&line, len(line) * 2) + } + fmt.println("Here") + raw_data(line)[len(line)] = 0 + fmt.println("Here2") + + cstr := strings.unsafe_string_to_cstring(string(line[:])) + pos := {0, font_size * f32(line_index)} + r.DrawTextEx(font, cstr, pos, font_size, 0, r.WHITE) + } + + render_cursor(&cursor) + + r.DrawFPS(10, 10) + + r.EndDrawing() + + free_all(context.temp_allocator) + dirty = false + } +} diff --git a/type_conversions.txt b/type_conversions.txt new file mode 100644 index 0000000..fe18ca6 --- /dev/null +++ b/type_conversions.txt @@ -0,0 +1,37 @@ +From string to X # +To Action Code +[]u8 alias transmute([]u8)st +string copy strings.clone(st) +cstring copy strings.clone_to_cstring(st) +cstring alias strings.unsafe_string_to_cstring(st) +[]rune stream for rune in st { ... } +[]rune copy utf8.string_to_runes(st) +[^]u8 alias raw_data(st) + +From cstring to X # +To Action Code +string alias string(st) +[^]u8 alias transmute([^]u8)st + +From a string literal to X # +To Action Code +string alias string(st) or newstr: string = st +cstring alias cstring(st) or newstr: cstring = st + +From []u8 to X # +To Action Code +string alias transmute(string)st +string alias string(st) unless a slice literal +[^]u8 alias raw_data(st) + +From []rune to string # +Action Code +copy utf8.runes_to_string(st) + +From [^]u8 to cstring # +Action Code +alias cstring(st) + +From [^]u8 and length int to string # +Action Code +alias strings.string_from_ptr(ptr, length)