mirror of
https://github.com/wisplite/a-star-go.git
synced 2026-06-27 15:37:07 -05:00
massively improved performance by using a texture instead of drawing boxes
This commit is contained in:
@@ -11,26 +11,39 @@ func canvasMouse() rl.Vector2 {
|
|||||||
return rl.GetMousePosition()
|
return rl.GetMousePosition()
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawGrid(grid [][]int, lineThickness float32) {
|
func drawInfiniteGridLines(camera rl.Camera2D, canvasW float32, canvasH float32, cellSize float32, width int, height int) {
|
||||||
if lineThickness > 5 {
|
// 1. Level of Detail (LOD) Check
|
||||||
lineThickness = 0
|
// If we are zoomed out too far, don't draw the gridlines at all
|
||||||
|
if camera.Zoom < 0.4 {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
for i := 0; i < len(grid); i++ {
|
|
||||||
for j := 0; j < len(grid[i]); j++ {
|
|
||||||
rl.DrawRectangle(int32(i*25), int32(j*25), 25, 25, rl.NewColor(240, 240, 240, 255))
|
|
||||||
if lineThickness > 0 {
|
|
||||||
rl.DrawRectangleLinesEx(rl.NewRectangle(float32(i*25), float32(j*25), 25, 25), lineThickness, rl.Black)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateGrid(width int, height int) [][]int {
|
lineThickness := 1.0 / camera.Zoom
|
||||||
grid := make([][]int, width)
|
lineColor := rl.NewColor(200, 200, 200, 255) // Light gray so it's not distracting
|
||||||
for i := range grid {
|
|
||||||
grid[i] = make([]int, height)
|
// 2. Draw Vertical Lines
|
||||||
|
// Start at the left edge of the screen, draw a line from top to bottom,
|
||||||
|
// step right by cellSize, repeat until off the right edge.
|
||||||
|
for x := float32(0.0); x <= cellSize*float32(width); x += cellSize {
|
||||||
|
rl.DrawLineEx(
|
||||||
|
rl.NewVector2(x, 0),
|
||||||
|
rl.NewVector2(x, cellSize*float32(width)),
|
||||||
|
lineThickness,
|
||||||
|
lineColor,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Draw Horizontal Lines
|
||||||
|
// Start at the top edge of the screen, draw a line from left to right,
|
||||||
|
// step down by cellSize, repeat until off the bottom edge.
|
||||||
|
for y := float32(0.0); y <= cellSize*float32(height); y += cellSize {
|
||||||
|
rl.DrawLineEx(
|
||||||
|
rl.NewVector2(0, y),
|
||||||
|
rl.NewVector2(cellSize*float32(height), y),
|
||||||
|
lineThickness,
|
||||||
|
lineColor,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
return grid
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -61,7 +74,12 @@ func main() {
|
|||||||
editModeWidth := false
|
editModeWidth := false
|
||||||
editModeHeight := false
|
editModeHeight := false
|
||||||
|
|
||||||
grid := generateGrid(width, height)
|
cellSize := float32(25)
|
||||||
|
|
||||||
|
mapImage := rl.GenImageColor(width, height, rl.NewColor(240, 240, 240, 255))
|
||||||
|
mapTexture := rl.LoadTextureFromImage(mapImage)
|
||||||
|
defer rl.UnloadTexture(mapTexture)
|
||||||
|
defer rl.UnloadImage(mapImage)
|
||||||
|
|
||||||
for !rl.WindowShouldClose() {
|
for !rl.WindowShouldClose() {
|
||||||
screenWidth := float32(rl.GetScreenWidth())
|
screenWidth := float32(rl.GetScreenWidth())
|
||||||
@@ -84,8 +102,8 @@ func main() {
|
|||||||
|
|
||||||
// 2. Apply the zoom
|
// 2. Apply the zoom
|
||||||
camera.Zoom += float32(wheel) * 0.1
|
camera.Zoom += float32(wheel) * 0.1
|
||||||
if camera.Zoom < 0.1 {
|
if camera.Zoom < 0.01 {
|
||||||
camera.Zoom = 0.1
|
camera.Zoom = 0.01
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Where does that same screen pixel point to AFTER zooming?
|
// 3. Where does that same screen pixel point to AFTER zooming?
|
||||||
@@ -110,7 +128,14 @@ 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)
|
||||||
drawGrid(grid, 1/camera.Zoom)
|
rl.DrawTextureEx(
|
||||||
|
mapTexture,
|
||||||
|
rl.NewVector2(0, 0), // Position
|
||||||
|
0.0, // Rotation
|
||||||
|
cellSize, // Scale factor
|
||||||
|
rl.White, // Tint (White means no tint)
|
||||||
|
)
|
||||||
|
drawInfiniteGridLines(camera, canvasWidth, screenHeight, cellSize, width, height)
|
||||||
rl.EndMode2D()
|
rl.EndMode2D()
|
||||||
rl.EndScissorMode()
|
rl.EndScissorMode()
|
||||||
|
|
||||||
@@ -136,7 +161,10 @@ func main() {
|
|||||||
if rg.Button(rl.NewRectangle(sidebarX+(10*scale), (40*scale), (180*scale), (30*scale)), "Generate Grid") {
|
if rg.Button(rl.NewRectangle(sidebarX+(10*scale), (40*scale), (180*scale), (30*scale)), "Generate Grid") {
|
||||||
width, _ = strconv.Atoi(widthInputValue)
|
width, _ = strconv.Atoi(widthInputValue)
|
||||||
height, _ = strconv.Atoi(heightInputValue)
|
height, _ = strconv.Atoi(heightInputValue)
|
||||||
grid = generateGrid(width, height)
|
mapImage = rl.GenImageColor(width, height, rl.NewColor(240, 240, 240, 255))
|
||||||
|
mapTexture = rl.LoadTextureFromImage(mapImage)
|
||||||
|
defer rl.UnloadTexture(mapTexture)
|
||||||
|
defer rl.UnloadImage(mapImage)
|
||||||
}
|
}
|
||||||
|
|
||||||
rl.EndDrawing()
|
rl.EndDrawing()
|
||||||
|
|||||||
Reference in New Issue
Block a user