wip raylib

This commit is contained in:
2025-12-08 21:18:47 -05:00
commit 05dd26804a
3 changed files with 151 additions and 0 deletions

113
src/main.odin Normal file
View File

@@ -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
}
}