From 29bce85e829672420821e8da8f40014395686ca6 Mon Sep 17 00:00:00 2001 From: wisplite Date: Thu, 14 May 2026 07:06:38 -0500 Subject: [PATCH] slight rework of draw code to address a small memory leak (changes are pushed straight to vram rather than recreating an image from scratch every time) --- main.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index fa48a53..05a84a1 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,10 @@ package main import ( + "image/color" "strconv" "strings" + "unsafe" rg "github.com/gen2brain/raylib-go/raygui" rl "github.com/gen2brain/raylib-go/raylib" @@ -80,6 +82,7 @@ func main() { toolOptionsText := strings.Join(toolOptions, ";") activeTool := int32(0) toolDropdownOpen := false + textureNeedsUpdate := true cellSize := float32(25) @@ -147,9 +150,7 @@ func main() { prevX := int(lastMousePos.X / cellSize) prevY := int(lastMousePos.Y / cellSize) rl.ImageDrawLine(mapImage, int32(prevX), int32(prevY), int32(x), int32(y), rl.NewColor(0, 0, 0, 255)) - newTex := rl.LoadTextureFromImage(mapImage) - rl.UnloadTexture(mapTexture) - mapTexture = newTex + textureNeedsUpdate = true } case 1: // Start — must run on first paint frame (lastMousePos may be -1 after release) if int(startPos.X) != x || int(startPos.Y) != y { @@ -158,9 +159,7 @@ func main() { } rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(0, 255, 0, 255)) startPos = rl.NewVector2(float32(x), float32(y)) - newTex := rl.LoadTextureFromImage(mapImage) - rl.UnloadTexture(mapTexture) - mapTexture = newTex + textureNeedsUpdate = true } case 2: // End if int(endPos.X) != x || int(endPos.Y) != y { @@ -169,9 +168,7 @@ func main() { } rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(255, 0, 0, 255)) endPos = rl.NewVector2(float32(x), float32(y)) - newTex := rl.LoadTextureFromImage(mapImage) - rl.UnloadTexture(mapTexture) - mapTexture = newTex + textureNeedsUpdate = true } } lastMousePos = worldPos @@ -190,6 +187,12 @@ func main() { // 1. Draw Canvas rl.BeginScissorMode(0, 0, int32(canvasWidth), int32(screenHeight)) rl.BeginMode2D(camera) + if textureNeedsUpdate { + ptr := (*color.RGBA)(mapImage.Data) + pixels := unsafe.Slice(ptr, width*height) + rl.UpdateTexture(mapTexture, pixels) + textureNeedsUpdate = false + } rl.DrawTextureEx( mapTexture, rl.NewVector2(0, 0), // Position