Limit texture size to 8192x8192 pixels

This commit is contained in:
2018-10-13 01:37:44 +01:00
parent 1fa61c72c2
commit 0dd44d9de7
3 changed files with 77 additions and 96 deletions

View File

@@ -3,16 +3,20 @@ package conv
import (
"fmt"
"image/color"
"image/png"
"log"
"os"
"github.com/faiface/pixel"
"ur.gs/ordoor/internal/data"
)
var transparent = color.RGBA{0, 0, 0, 0}
const (
// Textures can be this size at most. So putting every sprite onto the same
// sheet is a fraught business. With effective packing, it may be fine.
// If not, I'll have to come up with another idea
maxTextureX = 8192
maxTextureY = 8192
)
// Important conversions:
//
@@ -28,86 +32,73 @@ type Sprite struct {
type Object struct {
Name string
Sprites []Sprite
}
func maxWidth(obj *data.Object) int {
out := 0
for _, spr := range obj.Sprites {
width := int(spr.Width)
if width > out {
out = width
}
}
return out
}
func fullHeight(obj *data.Object) int {
out := 0
for _, spr := range obj.Sprites {
out = out + int(spr.Height)
}
return out
Sprites []*Sprite
}
func ConvertObjects(objects []*data.Object) (map[string]*Object, *pixel.PictureData) {
// This needs to be the maxWidth of all the objects, added together
maxX := 0
for _, obj := range objects {
maxX = maxX + maxWidth(obj)
}
// This needs to be the largest fullHeight of all the objects
maxY := 0
for _, obj := range objects {
height := fullHeight(obj)
if height > maxY {
maxY = height
}
}
// FIXME: this is rather inefficient. It would be better to determine the
// maximum size we need for the objects at hand.
spritesheet := pixel.MakePictureData(
pixel.R(0.0, 0.0, float64(maxTextureX), float64(maxTextureY)),
)
// These are updated each time a sprite is added to the sheet
xOffset := 0
spritesheet := pixel.MakePictureData(pixel.R(0.0, 0.0, float64(maxX), float64(maxY)))
yOffset := 0
rowMaxY := 0
out := make(map[string]*Object)
for _, rawObj := range objects {
log.Println("xOffset:", xOffset)
cObj := ConvertObjectWithSpritesheet(rawObj, rawObj.Name, spritesheet, xOffset)
xOffset = xOffset + maxWidth(rawObj)
out[cObj.Name] = cObj
}
f, _ := os.Create("spritesheet.png")
img := spritesheet.Image()
png.Encode(f, img)
f.Close()
for _, rawObj := range objects {
sprites := make([]*Sprite, 0, len(rawObj.Sprites))
for i, rawSpr := range rawObj.Sprites {
width := int(rawSpr.Width)
height := int(rawSpr.Height)
// CR+LF if needed
if xOffset+width > maxTextureX {
xOffset = 0
yOffset += rowMaxY
}
if xOffset+width > maxTextureX || yOffset+height > maxTextureY {
panic("Sprite does not fit in spritesheet")
}
spr := spriteToPic(rawObj.Name, i, rawSpr, spritesheet, xOffset, yOffset)
sprites = append(sprites, &Sprite{width, height, spritesheet, spr})
xOffset = xOffset + width
if height > rowMaxY {
rowMaxY = height
}
}
out[rawObj.Name] = &Object{Name: rawObj.Name, Sprites: sprites}
}
return out, spritesheet
}
func ConvertObjectWithSpritesheet(rawObj *data.Object, name string, pic *pixel.PictureData, xOffset int) *Object {
out := &Object{
Name: name,
Sprites: make([]Sprite, len(rawObj.Sprites)),
}
// We store the sprites vertically in the provided pic
yOffset := 0
log.Printf("Converting %v: xOffset = %v", name, xOffset)
out := &Object{
Name: name,
Sprites: make([]*Sprite, len(rawObj.Sprites)),
}
for i, rawSpr := range rawObj.Sprites {
spr := spriteToPic(name, i, rawSpr, pic, xOffset, yOffset)
log.Printf(" %#v", spr.Frame())
out.Sprites[i] = Sprite{
out.Sprites[i] = &Sprite{
Width: int(rawSpr.Width),
Height: int(rawSpr.Height),
Pic: pic,
Spr: spr,
}
yOffset = yOffset + int(rawSpr.Height)
}
@@ -117,7 +108,7 @@ func ConvertObjectWithSpritesheet(rawObj *data.Object, name string, pic *pixel.P
func ConvertObject(rawObj *data.Object, name string) *Object {
out := &Object{
Name: name,
Sprites: make([]Sprite, len(rawObj.Sprites)),
Sprites: make([]*Sprite, len(rawObj.Sprites)),
}
for i, rawSpr := range rawObj.Sprites {
@@ -131,7 +122,7 @@ func ConvertObject(rawObj *data.Object, name string) *Object {
)
spr := spriteToPic(name, i, rawSpr, pic, 0, 0)
out.Sprites[i] = Sprite{
out.Sprites[i] = &Sprite{
Width: int(rawSpr.Width),
Height: int(rawSpr.Height),
Pic: pic,
@@ -146,20 +137,17 @@ func spriteToPic(name string, idx int, sprite *data.Sprite, pic *pixel.PictureDa
width := int(sprite.Width)
height := int(sprite.Height)
//log.Printf("%v %v: width=%v height=%v", name, idx, width, height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
b := sprite.Data[y*width+x]
// Update the picture
if err := setPaletteColor(pic, x+xOffset, y+yOffset, b); err != nil {
//log.Printf("%s %d: %d,%d: %v", name, idx, x, y, err)
log.Printf("%s %d: %d,%d: %v", name, idx, x, y, err)
}
}
}
// FIXME: I really don't get this fetish for starting at the bottom-left
bounds := pixel.R(
float64(xOffset),
float64(yOffset),
@@ -167,9 +155,6 @@ func spriteToPic(name string, idx int, sprite *data.Sprite, pic *pixel.PictureDa
float64(yOffset+height),
)
//bounds.Min = bounds.Min.ScaledXY(pixel.Vec{1.0, -1.0})
//bounds.Max = bounds.Max.ScaledXY(pixel.Vec{1.0, -1.0})
return pixel.NewSprite(pic, bounds)
}