Fix memory leak in odin website example (#12)

This commit is contained in:
Dudejoe870 2024-08-31 17:23:06 -05:00 committed by Nic Barker
parent 29ebed2010
commit 4d1b65071f
2 changed files with 7 additions and 6 deletions

View File

@ -538,6 +538,8 @@ main :: proc() {
checkImage5 = raylib.LoadTextureFromImage(raylib.LoadImage("resources/check_5.png")) checkImage5 = raylib.LoadTextureFromImage(raylib.LoadImage("resources/check_5.png"))
for !raylib.WindowShouldClose() { for !raylib.WindowShouldClose() {
defer free_all(context.temp_allocator)
animationLerpValue += raylib.GetFrameTime() animationLerpValue += raylib.GetFrameTime()
if animationLerpValue > 1 { if animationLerpValue > 1 {
animationLerpValue = animationLerpValue - 2 animationLerpValue = animationLerpValue - 2

View File

@ -2,6 +2,7 @@ package main
import clay "../../clay-odin" import clay "../../clay-odin"
import "core:math" import "core:math"
import "core:strings"
import "vendor:raylib" import "vendor:raylib"
RaylibFont :: struct { RaylibFont :: struct {
@ -47,7 +48,7 @@ measureText :: proc "c" (text: ^clay.String, config: ^clay.TextElementConfig) ->
return textSize return textSize
} }
clayRaylibRender :: proc(renderCommands: ^clay.ClayArray(clay.RenderCommand)) { clayRaylibRender :: proc(renderCommands: ^clay.ClayArray(clay.RenderCommand), allocator := context.temp_allocator) {
for i := 0; i < cast(int)renderCommands.length; i += 1 { for i := 0; i < cast(int)renderCommands.length; i += 1 {
renderCommand := clay.RenderCommandArray_Get(renderCommands, cast(i32)i) renderCommand := clay.RenderCommandArray_Get(renderCommands, cast(i32)i)
boundingBox := renderCommand.boundingBox boundingBox := renderCommand.boundingBox
@ -58,14 +59,12 @@ clayRaylibRender :: proc(renderCommands: ^clay.ClayArray(clay.RenderCommand)) {
case clay.RenderCommandType.Text: case clay.RenderCommandType.Text:
{ {
// Raylib uses standard C strings so isn't compatible with cheap slices, we need to clone the string to append null terminator // Raylib uses standard C strings so isn't compatible with cheap slices, we need to clone the string to append null terminator
text := renderCommand.text text := string(renderCommand.text.chars[:renderCommand.text.length])
cloned := make([]u8, text.length + 1, context.temp_allocator) cloned := strings.clone_to_cstring(text, allocator)
copy(cloned[0:text.length], text.chars[0:text.length])
cloned[text.length] = 0
fontToUse: raylib.Font = raylibFonts[renderCommand.config.textElementConfig.fontId].font fontToUse: raylib.Font = raylibFonts[renderCommand.config.textElementConfig.fontId].font
raylib.DrawTextEx( raylib.DrawTextEx(
fontToUse, fontToUse,
cstring(raw_data(cloned)), cloned,
raylib.Vector2{boundingBox.x, boundingBox.y}, raylib.Vector2{boundingBox.x, boundingBox.y},
cast(f32)renderCommand.config.textElementConfig.fontSize, cast(f32)renderCommand.config.textElementConfig.fontSize,
cast(f32)renderCommand.config.textElementConfig.letterSpacing, cast(f32)renderCommand.config.textElementConfig.letterSpacing,