243 lines
7.1 KiB
Odin
243 lines
7.1 KiB
Odin
package main
|
|
|
|
import rl "libraries/raylib"
|
|
import "base:intrinsics"
|
|
import "core:strings"
|
|
import "core:fmt"
|
|
import "core:math/linalg"
|
|
import "core:mem"
|
|
|
|
// Wraps os.read_entire_file and os.write_entire_file, but they also work with emscripten.
|
|
@(require_results)
|
|
read_entire_file :: proc(name: string, allocator := context.allocator, loc := #caller_location) -> (data: []byte, success: bool) {
|
|
return _read_entire_file(name, allocator, loc)
|
|
}
|
|
|
|
write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
|
|
return _write_entire_file(name, data, truncate)
|
|
}
|
|
|
|
/// ### RAYLIB SHORTHANDS ### ///
|
|
measure_text :: proc(text: cstring, style: FontStyle) -> v2 {
|
|
return rl.MeasureTextEx(style.font, text, style.size, style.spacing)
|
|
}
|
|
|
|
draw_text :: proc(text: cstring, position: v2, style: FontStyle, color: rl.Color) {
|
|
rl.DrawTextEx(style.font, text, position, style.size, style.spacing, color)
|
|
}
|
|
|
|
TextHalign :: enum {
|
|
Left,
|
|
Middle,
|
|
Right,
|
|
}
|
|
|
|
TextValign :: enum {
|
|
Top,
|
|
Center,
|
|
Bottom,
|
|
}
|
|
|
|
draw_text_aligned :: proc(text: cstring, position: v2, style: FontStyle, halign: TextHalign, valign: TextValign, color: rl.Color) {
|
|
size := measure_text(text, style)
|
|
position := position
|
|
switch halign {
|
|
case .Left:
|
|
case .Middle: position.x -= size.x / 2.0
|
|
case .Right: position.x -= size.x
|
|
}
|
|
switch valign {
|
|
case .Top:
|
|
case .Center: position.y -= size.y / 2.0
|
|
case .Bottom: position.y -= size.y
|
|
}
|
|
draw_text(text, position, style, color)
|
|
}
|
|
|
|
/// ### TYPES ### ///
|
|
v1 :: [1]f32
|
|
v2 :: [2]f32
|
|
v3 :: [3]f32
|
|
v4 :: [4]f32
|
|
iv2 :: [2]i32
|
|
iv3 :: [3]i32
|
|
iv4 :: [4]i32
|
|
mat2 :: matrix[2, 2]f32
|
|
mat3 :: matrix[3, 3]f32
|
|
mat4 :: matrix[4, 4]f32
|
|
rect2 :: rl.Rectangle
|
|
irect2 :: struct {x, y, width, height: i32}
|
|
tri2 :: [3]v2
|
|
Bezier :: [3]v2
|
|
|
|
V2_ONE :: v2{1.0, 1.0}
|
|
V2_LEFT :: v2{-1.0, 0.0}
|
|
V2_RIGHT :: v2{1.0, 0.0}
|
|
V2_UP :: v2{0.0, -1.0}
|
|
V2_DOWN :: v2{0.0, 1.0}
|
|
|
|
/// ### MISCELANIOUS ### ///
|
|
enum_to_raygui_options_string :: proc($T: typeid, allocator: mem.Allocator) -> cstring where intrinsics.type_is_enum(T) {
|
|
b := strings.builder_make(allocator)
|
|
for element, i in T {
|
|
option := fmt.aprint(element, allocator = allocator)
|
|
strings.write_string(&b, strings.to_ada_case(option, allocator))
|
|
strings.write_rune(&b, ';')
|
|
}
|
|
strings.builder_replace_all(&b, "_", " ")
|
|
b.buf[len(b.buf) - 1] = 0
|
|
return cstring(raw_data(b.buf[:len(b.buf)]))
|
|
}
|
|
|
|
|
|
/// ### RECTANGLES ### ///
|
|
rect2_to_irect2 :: proc "contextless" (rect: rect2) -> irect2 {
|
|
return {i32(rect.x), i32(rect.y), i32(rect.width), i32(rect.height)}
|
|
}
|
|
|
|
irect2_to_rect2 :: proc "contextless" (rect: irect2) -> rect2 {
|
|
return {f32(rect.x), f32(rect.y), f32(rect.width), f32(rect.height)}
|
|
}
|
|
|
|
|
|
rect_has_point :: proc {rect2_has_point, irect2_has_point}
|
|
|
|
@(private="file") rect2_has_point :: proc "contextless" (rect: rect2, p: v2) -> bool {
|
|
return p.x >= rect.x && p.x < rect.x + rect.width && p.y >= rect.y && p.y < rect.y + rect.height
|
|
}
|
|
|
|
@(private="file") irect2_has_point :: proc "contextless" (rect: irect2, p: iv2) -> bool {
|
|
return p.x >= rect.x && p.x < rect.x + rect.width && p.y >= rect.y && p.y < rect.y + rect.height
|
|
}
|
|
|
|
|
|
rect_translate :: proc {rect2_translate, irect2_translate}
|
|
|
|
@(private="file") rect2_translate :: proc "contextless" (rect: rect2, translation: v2) -> rect2 {
|
|
return {rect.x + translation.x, rect.y + translation.y, rect.width, rect.height}
|
|
}
|
|
|
|
@(private="file") irect2_translate :: proc "contextless" (rect: irect2, translation: iv2) -> irect2 {
|
|
return {rect.x + translation.x, rect.y + translation.y, rect.width, rect.height}
|
|
}
|
|
|
|
|
|
rect_from :: proc {rect2_from_v2, irect2_from_iv2}
|
|
|
|
@(private="file") rect2_from_v2 :: proc "contextless" (position, size: v2) -> rect2 {
|
|
return {position.x, position.y, size.x, size.y}
|
|
}
|
|
|
|
@(private="file") irect2_from_iv2 :: proc "contextless" (position, size: iv2) -> irect2 {
|
|
return {position.x, position.y, size.x, size.y}
|
|
}
|
|
|
|
|
|
rect_scale :: proc{rect2_scale, irect2_scale}
|
|
|
|
@(private="file") rect2_scale :: proc "contextless" (rect: rect2, scalar: f32) -> rect2 {
|
|
return {rect.x * scalar, rect.y * scalar, rect.width * scalar, rect.height * scalar}
|
|
}
|
|
|
|
@(private="file") irect2_scale :: proc "contextless" (rect: irect2, scalar: i32) -> irect2 {
|
|
return {rect.x * scalar, rect.y * scalar, rect.width * scalar, rect.height * scalar}
|
|
}
|
|
|
|
|
|
rect_grow :: proc {rect2_grow, rect2_grow_v2, irect2_grow, rect2_grow_iv2}
|
|
|
|
@(private="file") rect2_grow :: proc "contextless" (rect: rect2, grow: f32) -> rect2 {
|
|
return {rect.x - grow, rect.y - grow, rect.width + grow * 2.0, rect.height + grow * 2.0}
|
|
}
|
|
|
|
@(private="file") rect2_grow_v2 :: proc "contextless" (rect: rect2, grow: v2) -> rect2 {
|
|
return {rect.x - grow.x, rect.y - grow.y, rect.width + grow.x * 2.0, rect.height + grow.y * 2.0}
|
|
}
|
|
|
|
@(private="file") irect2_grow :: proc "contextless" (rect: irect2, grow: i32) -> irect2 {
|
|
return {rect.x - grow, rect.y - grow, rect.width + grow * 2, rect.height + grow * 2}
|
|
}
|
|
|
|
@(private="file") rect2_grow_iv2 :: proc "contextless" (rect: irect2, grow: iv2) -> irect2 {
|
|
return {rect.x - grow.x, rect.y - grow.y, rect.width + grow.x * 2.0, rect.height + grow.y * 2.0}
|
|
}
|
|
|
|
|
|
rect_from_area :: proc {rect_from_area_v2, rect_from_area_iv2}
|
|
|
|
@(private="file") rect_from_area_v2 :: proc "contextless" (start, end: v2) -> rect2 {
|
|
tl := linalg.min(start, end)
|
|
return {**tl, **(linalg.max(start, end) - tl)}
|
|
}
|
|
|
|
@(private="file") rect_from_area_iv2 :: proc "contextless" (start, end: iv2) -> irect2 {
|
|
tl := linalg.min(start, end)
|
|
return {**tl, **(linalg.max(start, end) - tl)}
|
|
}
|
|
|
|
|
|
rect_from_corners :: proc {rect2_from_corners, irect2_from_corners}
|
|
|
|
@(private="file") rect2_from_corners :: proc "contextless" (tl, tr, br, bl: v2) -> rect2 {
|
|
top_left := linalg.min(linalg.min(tl, tr, br), bl)
|
|
bottom_right := linalg.max(linalg.max(tl, tr, br), bl)
|
|
return {**top_left, **(bottom_right - top_left)}
|
|
}
|
|
|
|
@(private="file") irect2_from_corners :: proc "contextless" (tl, tr, br, bl: iv2) -> irect2 {
|
|
top_left := linalg.min(linalg.min(tl, tr, br), bl)
|
|
bottom_right := linalg.max(linalg.max(tl, tr, br), bl)
|
|
return {**top_left, **(bottom_right - top_left)}
|
|
}
|
|
|
|
|
|
rect_get_tl :: proc {rect2_get_tl, irect2_get_tl}
|
|
|
|
@(private="file") rect2_get_tl :: proc(rect: rect2) -> v2 {
|
|
return {rect.x, rect.y}
|
|
}
|
|
|
|
@(private="file") irect2_get_tl :: proc(rect: irect2) -> iv2 {
|
|
return {rect.x, rect.y}
|
|
}
|
|
|
|
|
|
rect_get_br :: proc {rect2_get_br, irect2_get_br}
|
|
|
|
@(private="file") rect2_get_br :: proc(rect: rect2) -> v2 {
|
|
return {rect.x + rect.width, rect.y + rect.height}
|
|
}
|
|
|
|
@(private="file") irect2_get_br :: proc(rect: irect2) -> iv2 {
|
|
return {rect.x + rect.width, rect.y + rect.height}
|
|
}
|
|
|
|
|
|
rect_get_size :: proc {rect2_get_size, irect2_get_size}
|
|
|
|
@(private="file") rect2_get_size :: proc(rect: rect2) -> v2 {
|
|
return {rect.width, rect.height}
|
|
}
|
|
|
|
@(private="file") irect2_get_size :: proc(rect: irect2) -> iv2 {
|
|
return {rect.width, rect.height}
|
|
}
|
|
|
|
/// ## LINEAR ALGEBRA ## ///
|
|
matrix3_translate2 :: proc "contextless" (translation: v2) -> mat3 {
|
|
return {
|
|
1.0, 0.0, translation.x,
|
|
0.0, 1.0, translation.y,
|
|
0.0, 0.0, 1.0,
|
|
}
|
|
}
|
|
|
|
matrix3_transform_v2 :: proc(m: mat3, v: v2) -> v2 {
|
|
return (m * v3{**v, 1.0}).xy
|
|
}
|
|
|
|
matrix3_transform_rect2 :: proc(m: mat3, r: rect2) -> rect2 {
|
|
tl := m * v3{r.x, r.y, 1.0}
|
|
br := m * v3{r.x + r.width, r.y + r.height, 1.0}
|
|
return {**tl.xy, **(br - tl).xy}
|
|
}
|