Initial draw package
This commit is contained in:
BIN
draw/examples/fonts/JetBrainsMono-Bold.ttf
Normal file
BIN
draw/examples/fonts/JetBrainsMono-Bold.ttf
Normal file
Binary file not shown.
BIN
draw/examples/fonts/JetBrainsMono-Regular.ttf
Normal file
BIN
draw/examples/fonts/JetBrainsMono-Regular.ttf
Normal file
Binary file not shown.
147
draw/examples/hellope.odin
Normal file
147
draw/examples/hellope.odin
Normal file
@@ -0,0 +1,147 @@
|
||||
package examples
|
||||
|
||||
import "../../draw"
|
||||
import "../../vendor/clay"
|
||||
import "core:c"
|
||||
import "core:os"
|
||||
import sdl "vendor:sdl3"
|
||||
import sdl_ttf "vendor:sdl3/ttf"
|
||||
|
||||
JETBRAINS_MONO_REGULAR_RAW :: #load("fonts/JetBrainsMono-Regular.ttf")
|
||||
JETBRAINS_MONO_REGULAR: draw.Font_Id = max(draw.Font_Id) // Max so we crash if registration is forgotten
|
||||
|
||||
hellope_shapes :: proc() {
|
||||
if !sdl.Init({.VIDEO}) do os.exit(1)
|
||||
window := sdl.CreateWindow("Hellope!", 500, 500, {.HIGH_PIXEL_DENSITY})
|
||||
gpu := sdl.CreateGPUDevice({.MSL}, true, nil)
|
||||
if !sdl.ClaimWindowForGPUDevice(gpu, window) do os.exit(1)
|
||||
if !draw.init(gpu, window) do os.exit(1)
|
||||
|
||||
for {
|
||||
defer free_all(context.temp_allocator)
|
||||
ev: sdl.Event
|
||||
for sdl.PollEvent(&ev) {
|
||||
if ev.type == .QUIT do return
|
||||
}
|
||||
base_layer := draw.begin({w = 500, h = 500})
|
||||
|
||||
// Background
|
||||
draw.rectangle(base_layer, {0, 0, 500, 500}, {40, 40, 40, 255})
|
||||
|
||||
// Shapes demo
|
||||
draw.rectangle(base_layer, {20, 20, 200, 120}, {80, 120, 200, 255})
|
||||
draw.rectangle_lines(base_layer, {20, 20, 200, 120}, draw.WHITE, thick = 2)
|
||||
draw.rectangle_rounded(base_layer, {240, 20, 240, 120}, 0.3, {200, 80, 80, 255})
|
||||
draw.rectangle_gradient(
|
||||
base_layer,
|
||||
{20, 160, 460, 60},
|
||||
{255, 0, 0, 255},
|
||||
{0, 255, 0, 255},
|
||||
{0, 0, 255, 255},
|
||||
{255, 255, 0, 255},
|
||||
)
|
||||
|
||||
draw.circle(base_layer, {120, 320}, 60, {100, 200, 100, 255})
|
||||
draw.circle_lines(base_layer, {120, 320}, 60, draw.WHITE, thick = 2)
|
||||
draw.circle_gradient(base_layer, {300, 320}, 60, {255, 200, 50, 255}, {200, 50, 50, 255})
|
||||
draw.ring(base_layer, {430, 320}, 30, 55, 0, 270, {100, 100, 220, 255})
|
||||
|
||||
draw.triangle(base_layer, {60, 420}, {180, 480}, {20, 480}, {220, 180, 60, 255})
|
||||
draw.line(base_layer, {220, 420}, {460, 480}, {255, 255, 100, 255}, thick = 3)
|
||||
draw.poly(base_layer, {350, 450}, 6, 40, {180, 100, 220, 255}, rotation = 30)
|
||||
draw.poly_lines(base_layer, {350, 450}, 6, 40, draw.WHITE, rotation = 30, thick = 2)
|
||||
|
||||
draw.end(gpu, window)
|
||||
}
|
||||
}
|
||||
|
||||
hellope_text :: proc() {
|
||||
if !sdl.Init({.VIDEO}) do os.exit(1)
|
||||
window := sdl.CreateWindow("Hellope!", 500, 500, {.HIGH_PIXEL_DENSITY})
|
||||
gpu := sdl.CreateGPUDevice({.MSL}, true, nil)
|
||||
if !sdl.ClaimWindowForGPUDevice(gpu, window) do os.exit(1)
|
||||
if !draw.init(gpu, window) do os.exit(1)
|
||||
JETBRAINS_MONO_REGULAR = draw.register_font(JETBRAINS_MONO_REGULAR_RAW)
|
||||
|
||||
FONT_SIZE :: u16(24)
|
||||
TEXT_ID :: u32(1)
|
||||
|
||||
font := draw.get_font(JETBRAINS_MONO_REGULAR, FONT_SIZE)
|
||||
dpi := sdl.GetWindowDisplayScale(window)
|
||||
|
||||
for {
|
||||
defer free_all(context.temp_allocator)
|
||||
ev: sdl.Event
|
||||
for sdl.PollEvent(&ev) {
|
||||
if ev.type == .QUIT do return
|
||||
}
|
||||
base_layer := draw.begin({w = 500, h = 500})
|
||||
|
||||
// Grey background
|
||||
draw.rectangle(base_layer, {0, 0, 500, 500}, {127, 127, 127, 255})
|
||||
|
||||
// Measure and center text
|
||||
tw, th: c.int
|
||||
sdl_ttf.GetStringSize(font, "Hellope!", 0, &tw, &th)
|
||||
text_w := f32(tw) / dpi
|
||||
text_h := f32(th) / dpi
|
||||
pos_x := (500.0 - text_w) / 2.0
|
||||
pos_y := (500.0 - text_h) / 2.0
|
||||
|
||||
txt := draw.text(
|
||||
TEXT_ID,
|
||||
"Hellope!",
|
||||
{pos_x, pos_y},
|
||||
color = draw.WHITE,
|
||||
font_id = JETBRAINS_MONO_REGULAR,
|
||||
font_size = FONT_SIZE,
|
||||
)
|
||||
draw.prepare_text(base_layer, txt)
|
||||
|
||||
draw.end(gpu, window)
|
||||
}
|
||||
}
|
||||
|
||||
hellope_clay :: proc() {
|
||||
if !sdl.Init({.VIDEO}) do os.exit(1)
|
||||
window := sdl.CreateWindow("Hellope!", 500, 500, {.HIGH_PIXEL_DENSITY})
|
||||
gpu := sdl.CreateGPUDevice({.MSL}, true, nil)
|
||||
if !sdl.ClaimWindowForGPUDevice(gpu, window) do os.exit(1)
|
||||
if !draw.init(gpu, window) do os.exit(1)
|
||||
JETBRAINS_MONO_REGULAR = draw.register_font(JETBRAINS_MONO_REGULAR_RAW)
|
||||
|
||||
text_config := clay.TextElementConfig {
|
||||
fontId = JETBRAINS_MONO_REGULAR,
|
||||
fontSize = 24,
|
||||
textColor = {255, 255, 255, 255},
|
||||
}
|
||||
|
||||
for {
|
||||
defer free_all(context.temp_allocator)
|
||||
ev: sdl.Event
|
||||
for sdl.PollEvent(&ev) {
|
||||
if ev.type == .QUIT do return
|
||||
}
|
||||
base_layer := draw.begin({w = 500, h = 500})
|
||||
clay.SetLayoutDimensions({width = base_layer.bounds.w, height = base_layer.bounds.h})
|
||||
clay.BeginLayout()
|
||||
if clay.UI()(
|
||||
{
|
||||
id = clay.ID("outer"),
|
||||
layout = {
|
||||
sizing = {clay.SizingGrow({}), clay.SizingGrow({})},
|
||||
childAlignment = {x = .Center, y = .Center},
|
||||
},
|
||||
backgroundColor = {127, 127, 127, 255},
|
||||
},
|
||||
) {
|
||||
clay.Text("Hellope!", &text_config)
|
||||
}
|
||||
clay_batch := draw.ClayBatch {
|
||||
bounds = base_layer.bounds,
|
||||
cmds = clay.EndLayout(),
|
||||
}
|
||||
draw.prepare_clay_batch(base_layer, &clay_batch, {0, 0})
|
||||
draw.end(gpu, window)
|
||||
}
|
||||
}
|
||||
73
draw/examples/main.odin
Normal file
73
draw/examples/main.odin
Normal file
@@ -0,0 +1,73 @@
|
||||
package examples
|
||||
|
||||
import "core:fmt"
|
||||
import "core:mem"
|
||||
import "core:os"
|
||||
|
||||
main :: proc() {
|
||||
//----- Tracking allocator ----------------------------------
|
||||
{
|
||||
tracking_temp_allocator := false
|
||||
// Temp
|
||||
track_temp: mem.Tracking_Allocator
|
||||
if tracking_temp_allocator {
|
||||
mem.tracking_allocator_init(&track_temp, context.temp_allocator)
|
||||
context.temp_allocator = mem.tracking_allocator(&track_temp)
|
||||
}
|
||||
// Default
|
||||
track: mem.Tracking_Allocator
|
||||
mem.tracking_allocator_init(&track, context.allocator)
|
||||
context.allocator = mem.tracking_allocator(&track)
|
||||
// Log a warning about any memory that was not freed by the end of the program.
|
||||
// This could be fine for some global state or it could be a memory leak.
|
||||
defer {
|
||||
// Temp allocator
|
||||
if tracking_temp_allocator {
|
||||
if len(track_temp.allocation_map) > 0 {
|
||||
fmt.eprintf("=== %v allocations not freed - temp allocator: ===\n", len(track_temp.allocation_map))
|
||||
for _, entry in track_temp.allocation_map {
|
||||
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
|
||||
}
|
||||
}
|
||||
if len(track_temp.bad_free_array) > 0 {
|
||||
fmt.eprintf("=== %v incorrect frees - temp allocator: ===\n", len(track_temp.bad_free_array))
|
||||
for entry in track_temp.bad_free_array {
|
||||
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
|
||||
}
|
||||
}
|
||||
mem.tracking_allocator_destroy(&track_temp)
|
||||
}
|
||||
// Default allocator
|
||||
if len(track.allocation_map) > 0 {
|
||||
fmt.eprintf("=== %v allocations not freed - main allocator: ===\n", len(track.allocation_map))
|
||||
for _, entry in track.allocation_map {
|
||||
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
|
||||
}
|
||||
}
|
||||
if len(track.bad_free_array) > 0 {
|
||||
fmt.eprintf("=== %v incorrect frees - main allocator: ===\n", len(track.bad_free_array))
|
||||
for entry in track.bad_free_array {
|
||||
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
|
||||
}
|
||||
}
|
||||
mem.tracking_allocator_destroy(&track)
|
||||
}
|
||||
}
|
||||
|
||||
args := os.args
|
||||
if len(args) < 2 {
|
||||
fmt.eprintln("Usage: examples <example_name>")
|
||||
fmt.eprintln("Available examples: hellope-shapes, hellope-text, hellope-clay")
|
||||
os.exit(1)
|
||||
}
|
||||
|
||||
switch args[1] {
|
||||
case "hellope-clay": hellope_clay()
|
||||
case "hellope-shapes": hellope_shapes()
|
||||
case "hellope-text": hellope_text()
|
||||
case:
|
||||
fmt.eprintf("Unknown example: %v\n", args[1])
|
||||
fmt.eprintln("Available examples: hellope-shapes, hellope-text, hellope-clay")
|
||||
os.exit(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user