2025-03-21 03:58:35 +03:00

114 lines
3.5 KiB
Go

package worldMap
import (
"bytes"
"fmt"
"image"
"image/png"
"os"
"github.com/veandco/go-sdl2/sdl"
)
const (
mapWidth = 24
mapHeight = 24
texWidth = 64
texHeight = 64
)
var textures [8][]uint32
var level = [mapWidth][mapHeight]int{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 3, 0, 3, 0, 3, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 2, 2, 0, 2, 2, 0, 0, 0, 0, 3, 0, 3, 0, 3, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 0, 4, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 0, 4, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 0, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
}
func loadImage(out *[]uint32, w uint32, h uint32, filename string) error {
// Read the file content
file, err := os.ReadFile(filename)
if err != nil {
fmt.Println("Error reading file:", err)
return err
}
// Decode the PNG file
img, err := png.Decode(bytes.NewReader(file))
fmt.Println(filename)
if err != nil {
fmt.Println("Error decoding PNG:", err)
return err
}
// Get the width and height
w = uint32(img.Bounds().Dx())
h = uint32(img.Bounds().Dy())
// Resize the output slice to hold the pixel data
*out = make([]uint32, w*h)
// Convert the image to RGBA and extract pixel data
rgbaImg, ok := img.(*image.RGBA)
if !ok {
fmt.Println("Image is not in RGBA format")
return err
}
// Process each pixel and store it in the output slice
for i := range *out {
r := rgbaImg.Pix[i*4+0]
g := rgbaImg.Pix[i*4+1]
b := rgbaImg.Pix[i*4+2]
a := rgbaImg.Pix[i*4+3]
// Pack the ARGB values into a uint32
(*out)[i] = uint32(a)<<24 | uint32(r)<<16 | uint32(g)<<8 | uint32(b)
}
return nil
}
func GetMap() [24][24]int {
return level
}
func LoadTextures(renderer *sdl.Renderer) ([][]uint32, error) {
textureFiles := []string{
"assets/eagle.png", "assets/redbrick.png", "assets/purplestone.png", "assets/greystone.png",
"assets/bluestone.png", "assets/mossy.png", "assets/wood.png", "assets/colorstone.png",
}
textures := make([][]uint32, 8)
for i, file := range textureFiles {
var out []uint32
if err := loadImage(&out, uint32(64), 64, file); err != nil {
panic(err)
}
textures[i] = out
}
return textures, nil
}