wip raylib
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
bin
|
||||
113
src/main.odin
Normal file
113
src/main.odin
Normal 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
|
||||
}
|
||||
}
|
||||
37
type_conversions.txt
Normal file
37
type_conversions.txt
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user