createAtlasFromXML unmarshals and unpacks the xml data into a TextureAtlas it also adds the main image and subtextures to the imageLoader if the subtexture doesn't have an extension in it's Name field, it will append the main image's extension to it
(r io.Reader, url string)
| 104 | // if the subtexture doesn't have an extension in it's Name field, |
| 105 | // it will append the main image's extension to it |
| 106 | func createAtlasFromXML(r io.Reader, url string) (*TextureAtlasResource, error) { |
| 107 | var atlas *TextureAtlas |
| 108 | err := xml.NewDecoder(r).Decode(&atlas) |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | |
| 113 | imgURL := path.Join(path.Dir(url), atlas.ImagePath) |
| 114 | if err := engo.Files.Load(imgURL); err != nil { |
| 115 | return nil, fmt.Errorf("failed load texture atlas image: %v", err) |
| 116 | } |
| 117 | |
| 118 | res, err := engo.Files.Resource(imgURL) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | |
| 123 | img, ok := res.(TextureResource) |
| 124 | if !ok { |
| 125 | return nil, fmt.Errorf("resource not of type `TextureResource`: %v", url) |
| 126 | } |
| 127 | |
| 128 | ext := path.Ext(atlas.ImagePath) |
| 129 | for i, subTexture := range atlas.SubTextures { |
| 130 | texture := &Texture{ |
| 131 | id: img.Texture, |
| 132 | width: subTexture.Width, |
| 133 | height: subTexture.Height, |
| 134 | } |
| 135 | |
| 136 | viewport := engo.AABB{ |
| 137 | Min: engo.Point{ |
| 138 | X: subTexture.X / img.Width, |
| 139 | Y: subTexture.Y / img.Height, |
| 140 | }, |
| 141 | Max: engo.Point{ |
| 142 | X: (subTexture.X + subTexture.Width) / img.Width, |
| 143 | Y: (subTexture.Y + subTexture.Height) / img.Height, |
| 144 | }, |
| 145 | } |
| 146 | |
| 147 | subtextureURL := subTexture.Name |
| 148 | if path.Ext(subTexture.Name) == "" { |
| 149 | subtextureURL += ext |
| 150 | atlas.SubTextures[i].Name = subtextureURL |
| 151 | } |
| 152 | |
| 153 | imgLoader.images[subtextureURL] = TextureResource{Texture: texture.id, Width: texture.width, Height: texture.height, Viewport: &viewport} |
| 154 | } |
| 155 | |
| 156 | return &TextureAtlasResource{ |
| 157 | Atlas: atlas, |
| 158 | url: url, |
| 159 | texture: img.Texture, |
| 160 | }, nil |
| 161 | |
| 162 | } |
| 163 |