mirror of
https://github.com/wisplite/a-star-go.git
synced 2026-06-27 15:37:07 -05:00
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)
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"image/color"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
rg "github.com/gen2brain/raylib-go/raygui"
|
rg "github.com/gen2brain/raylib-go/raygui"
|
||||||
rl "github.com/gen2brain/raylib-go/raylib"
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
@@ -80,6 +82,7 @@ func main() {
|
|||||||
toolOptionsText := strings.Join(toolOptions, ";")
|
toolOptionsText := strings.Join(toolOptions, ";")
|
||||||
activeTool := int32(0)
|
activeTool := int32(0)
|
||||||
toolDropdownOpen := false
|
toolDropdownOpen := false
|
||||||
|
textureNeedsUpdate := true
|
||||||
|
|
||||||
cellSize := float32(25)
|
cellSize := float32(25)
|
||||||
|
|
||||||
@@ -147,9 +150,7 @@ func main() {
|
|||||||
prevX := int(lastMousePos.X / cellSize)
|
prevX := int(lastMousePos.X / cellSize)
|
||||||
prevY := int(lastMousePos.Y / cellSize)
|
prevY := int(lastMousePos.Y / cellSize)
|
||||||
rl.ImageDrawLine(mapImage, int32(prevX), int32(prevY), int32(x), int32(y), rl.NewColor(0, 0, 0, 255))
|
rl.ImageDrawLine(mapImage, int32(prevX), int32(prevY), int32(x), int32(y), rl.NewColor(0, 0, 0, 255))
|
||||||
newTex := rl.LoadTextureFromImage(mapImage)
|
textureNeedsUpdate = true
|
||||||
rl.UnloadTexture(mapTexture)
|
|
||||||
mapTexture = newTex
|
|
||||||
}
|
}
|
||||||
case 1: // Start — must run on first paint frame (lastMousePos may be -1 after release)
|
case 1: // Start — must run on first paint frame (lastMousePos may be -1 after release)
|
||||||
if int(startPos.X) != x || int(startPos.Y) != y {
|
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))
|
rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(0, 255, 0, 255))
|
||||||
startPos = rl.NewVector2(float32(x), float32(y))
|
startPos = rl.NewVector2(float32(x), float32(y))
|
||||||
newTex := rl.LoadTextureFromImage(mapImage)
|
textureNeedsUpdate = true
|
||||||
rl.UnloadTexture(mapTexture)
|
|
||||||
mapTexture = newTex
|
|
||||||
}
|
}
|
||||||
case 2: // End
|
case 2: // End
|
||||||
if int(endPos.X) != x || int(endPos.Y) != y {
|
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))
|
rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(255, 0, 0, 255))
|
||||||
endPos = rl.NewVector2(float32(x), float32(y))
|
endPos = rl.NewVector2(float32(x), float32(y))
|
||||||
newTex := rl.LoadTextureFromImage(mapImage)
|
textureNeedsUpdate = true
|
||||||
rl.UnloadTexture(mapTexture)
|
|
||||||
mapTexture = newTex
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lastMousePos = worldPos
|
lastMousePos = worldPos
|
||||||
@@ -190,6 +187,12 @@ func main() {
|
|||||||
// 1. Draw Canvas
|
// 1. Draw Canvas
|
||||||
rl.BeginScissorMode(0, 0, int32(canvasWidth), int32(screenHeight))
|
rl.BeginScissorMode(0, 0, int32(canvasWidth), int32(screenHeight))
|
||||||
rl.BeginMode2D(camera)
|
rl.BeginMode2D(camera)
|
||||||
|
if textureNeedsUpdate {
|
||||||
|
ptr := (*color.RGBA)(mapImage.Data)
|
||||||
|
pixels := unsafe.Slice(ptr, width*height)
|
||||||
|
rl.UpdateTexture(mapTexture, pixels)
|
||||||
|
textureNeedsUpdate = false
|
||||||
|
}
|
||||||
rl.DrawTextureEx(
|
rl.DrawTextureEx(
|
||||||
mapTexture,
|
mapTexture,
|
||||||
rl.NewVector2(0, 0), // Position
|
rl.NewVector2(0, 0), // Position
|
||||||
|
|||||||
Reference in New Issue
Block a user