DiogoMaMartins

Golang

Oct 14th, 2020 (edited)
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.04 KB | None | 0 0
  1. package main
  2.  
  3. import(
  4.   "image/png"
  5.   "image/draw"
  6.   "image"
  7.   "io"
  8.   "os"
  9.   "fmt"
  10.   "bufio"
  11.   "log"
  12.   "encoding/base64"
  13. )
  14.  
  15.  
  16.  
  17. func ReadFile(file_name string) []string{
  18.     var list_images []string
  19.     file, err := os.Open(file_name)
  20.     if err != nil {
  21.         log.Fatal(err)
  22.     }
  23.     defer file.Close()
  24.  
  25.     scanner := bufio.NewScanner(file)
  26.     for scanner.Scan() {
  27.    
  28.         list_images = append(list_images, scanner.Text())
  29.  
  30.     }
  31.  
  32.     if err := scanner.Err(); err != nil {
  33.         log.Fatal(err)
  34.     }
  35.     return list_images
  36. }
  37.  
  38.  
  39. func isError(err error) bool {
  40.     if err != nil {
  41.         fmt.Println(err.Error())
  42.     }
  43.     return (err != nil)
  44. }
  45.  
  46.  
  47. func convert_png_to_tmx(image_name string){
  48.   tileSize := 16;
  49.   inputFile,_ := os.Open(image_name)
  50.   defer inputFile.Close()
  51.  
  52.   reader := io.Reader(inputFile)
  53.   img,_ := png.Decode(reader)
  54.   rgba := image.NewRGBA(img.Bounds())
  55.   draw.Draw(rgba, rgba.Bounds(),  img, rgba.Bounds().Min, draw.Over)
  56.   width, height := rgba.Bounds().Max.X, rgba.Bounds().Max.Y
  57.   mapwidth := width / tileSize
  58.   mapheight := height / tileSize
  59.   var tileset []image.Image
  60.   var tilemap []uint32
  61.  
  62.   for y := 0; y + tileSize <= height; y += tileSize {
  63.     for x := 0; x + tileSize <= width; x += tileSize {
  64.       tile := rgba.SubImage(image.Rect(x, y, x+tileSize, y+tileSize))
  65.       matchIndex := matchAny(tileset, tile)
  66.       if  matchIndex < 0 {
  67.         tilemap = append(tilemap, uint32(len(tileset)))
  68.         tileset = append(tileset, tile)
  69.         fmt.Print(".")
  70.       } else {
  71.         tilemap = append(tilemap, uint32(matchIndex))
  72.       }
  73.     }
  74.   }
  75.  
  76.  
  77.   fmt.Println("")
  78.   fmt.Printf("%d tiles found\n", len(tileset))
  79.   fmt.Println("Writing TMX file...")
  80.   writeTMX(image_name,tileset, tilemap, mapwidth, mapheight)
  81. }
  82.  
  83. func matchAny(tileset []image.Image, img image.Image) int {
  84.   for i := range tileset {
  85.     if match(tileset[i], img) {
  86.       return i
  87.     }
  88.   }
  89.   return -1
  90. }
  91.  
  92. func match(img1, img2 image.Image) bool {
  93.   for x := 0; x < 16; x++ {
  94.     for y := 0; y < 16; y++ {
  95.       r1, g1, b1, a1 := img1.At(img1.Bounds().Min.X + x, img1.Bounds().Min.Y + y).RGBA()
  96.       r2, g2, b2, a2 := img2.At(img2.Bounds().Min.X + x, img2.Bounds().Min.Y + y).RGBA()
  97.       if r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2 {
  98.         return false
  99.       }
  100.     }
  101.   }
  102.   return true
  103. }
  104.  
  105. func writeTMX(image_name string,tileset []image.Image, tilemap []uint32, mapwidth, mapheight int) error {
  106.  
  107.   tilesize := 20 // temporary
  108.  
  109.   outputFile,err := os.Create("Maps/" + image_name + ".tmx")
  110.  
  111.   if err != nil {
  112.     return err
  113.   }
  114.   defer outputFile.Close()
  115.   writer := io.Writer(outputFile)
  116.   var encoder io.WriteCloser
  117.   writer.Write(([]byte)(fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
  118.   <map version="1.0" orientation="orthogonal" width="%d" height="%d" tilewidth="%d" tileheight="%d">
  119.    <tileset firstgid="1" name="tileset" tilewidth="%d" tileheight="%d">`,
  120.   mapwidth, mapheight, tilesize, tilesize, tilesize, tilesize)))
  121.  
  122.   for index, tile := range tileset {
  123.     writer.Write(([]byte)("\n"))
  124.     writer.Write(([]byte)(fmt.Sprintf(`<tile id="%d">
  125.     <image width="%d" height="%d" format="png">
  126.     <data encoding="base64">`, index, tilesize, tilesize)))
  127.     encoder = base64.NewEncoder(base64.StdEncoding, writer)
  128.     png.Encode(encoder, tile)
  129.     encoder.Close()
  130.     writer.Write(([]byte)(fmt.Sprintf(`</data></image></tile>`)))
  131.   }
  132.   writer.Write(([]byte)(fmt.Sprintf(`
  133.   </tileset>
  134.    <layer name="Tile Layer 1" width="%d" height="%d">
  135.     <data encoding="csv">`, mapwidth, mapheight)))
  136.   for i, index := range tilemap {
  137.     if i > 0 {
  138.       writer.Write(([]byte)(","))
  139.     }
  140.     if i % mapwidth == 0 {
  141.       writer.Write(([]byte)("\n"))
  142.     }
  143.     writer.Write(([]byte)(fmt.Sprintf("%d",index+1)))
  144.  
  145.  
  146.   }
  147.   writer.Write(([]byte)(`
  148.     </data>
  149.    </layer>
  150.   </map>`))
  151.  
  152.   return nil
  153.  
  154. }
  155.  
  156. func main() {
  157.  
  158.  
  159.   list_images := ReadFile("PicNames.txt")
  160.  
  161.   for _, element := range list_images {
  162.     convert_png_to_tmx(element)
  163.  
  164.   }
  165.  
  166. }
  167.  
Add Comment
Please, Sign In to add comment