ebiten: convert view-obj

This commit is contained in:
2019-12-29 15:38:49 +00:00
parent 0320743b30
commit 6f605aa502
7 changed files with 179 additions and 228 deletions

View File

@@ -1,23 +1,11 @@
package conv
import (
"fmt"
"image/color"
"log"
"github.com/faiface/pixel"
"github.com/hajimehoshi/ebiten"
"ur.gs/ordoor/internal/data"
)
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:
//
// * Width & height now stored using int
@@ -26,8 +14,7 @@ type Sprite struct {
Width int
Height int
Pic *pixel.PictureData
Spr *pixel.Sprite
Image *ebiten.Image
}
type Object struct {
@@ -45,140 +32,38 @@ func MapByName(objects []*Object) map[string]*Object {
return out
}
func ConvertObjects(objects []*data.Object) ([]*Object, *pixel.PictureData) {
// 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
yOffset := 0
rowMaxY := 0
out := make([]*Object, 0, len(objects))
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 = append(out, &Object{Name: rawObj.Name, Sprites: sprites})
}
return out, spritesheet
}
func ConvertObjectWithSpritesheet(rawObj *data.Object, name string, pic *pixel.PictureData, xOffset int) *Object {
// We store the sprites vertically in the provided pic
yOffset := 0
func ConvertObject(rawObj *data.Object, name string) (*Object, error) {
out := &Object{
Name: name,
Sprites: make([]*Sprite, len(rawObj.Sprites)),
}
for i, rawSpr := range rawObj.Sprites {
spr := spriteToPic(name, i, rawSpr, pic, xOffset, yOffset)
out.Sprites[i] = &Sprite{
Width: int(rawSpr.Width),
Height: int(rawSpr.Height),
Pic: pic,
Spr: spr,
}
w := int(rawSpr.Width)
h := int(rawSpr.Height)
data := spriteToData(name, i, rawSpr)
yOffset = yOffset + int(rawSpr.Height)
image, err := ebiten.NewImage(w, h, ebiten.FilterDefault)
if err != nil {
return nil, err
}
_ = image.ReplacePixels(data)
out.Sprites[i] = &Sprite{Width: w, Height: h, Image: image}
}
return out
return out, nil
}
func ConvertObject(rawObj *data.Object, name string) *Object {
out := &Object{
Name: name,
Sprites: make([]*Sprite, len(rawObj.Sprites)),
}
for i, rawSpr := range rawObj.Sprites {
pic := pixel.MakePictureData(
pixel.R(
float64(0),
float64(0),
float64(rawSpr.Width),
float64(rawSpr.Height),
),
)
spr := spriteToPic(name, i, rawSpr, pic, 0, 0)
out.Sprites[i] = &Sprite{
Width: int(rawSpr.Width),
Height: int(rawSpr.Height),
Pic: pic,
Spr: spr,
}
}
return out
}
func spriteToPic(name string, idx int, sprite *data.Sprite, pic *pixel.PictureData, xOffset, yOffset int) *pixel.Sprite {
func spriteToData(name string, idx int, sprite *data.Sprite) []byte {
width := int(sprite.Width)
height := int(sprite.Height)
out := make([]byte, 0, 4*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)
}
}
for _, palette := range sprite.Data {
color := data.ColorPalette[int(palette)]
out = append(out, color.R, color.G, color.B, color.A)
}
bounds := pixel.R(
float64(xOffset),
float64(yOffset),
float64(xOffset+width),
float64(yOffset+height),
)
return pixel.NewSprite(pic, bounds)
}
func setPaletteColor(pic *pixel.PictureData, x int, y int, colorIdx byte) error {
vec := pixel.V(float64(x), float64(y))
idx := pic.Index(vec)
if idx > len(pic.Pix)-1 {
return fmt.Errorf("Got index %v which exceeds bounds", idx)
}
r, g, b, a := data.ColorPalette[int(colorIdx)].RGBA()
color := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
pic.Pix[idx] = color
return nil
return out
}

View File

@@ -5,7 +5,7 @@ import "image/color"
var (
Transparent = color.RGBA{R: 0, G: 0, B: 0, A: 0}
ColorPalette = color.Palette{
ColorPalette = []color.RGBA{
Transparent,
color.RGBA{R: 128, G: 0, B: 0, A: 255},
color.RGBA{R: 0, G: 128, B: 0, A: 255},

View File

@@ -3,8 +3,7 @@ package ui
import (
"flag"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/hajimehoshi/ebiten"
)
var (
@@ -13,35 +12,50 @@ var (
)
type Window struct {
PixelWindow *pixelgl.Window
ButtonEventHandlers map[pixelgl.Button][]func()
Title string
KeyEventHandlers map[ebiten.Key][]func()
// User-provided update actions
updateFn func() error
drawFn func(*ebiten.Image) error
}
// WARE! 0,0 is the *bottom left* of the window
// 0,0 is the *top left* of the window
//
// To invert things so 0,0 is the *top left*, apply the InvertY` matrix
// ebiten assumes a single window, so only call this once...
func NewWindow(title string) (*Window, error) {
cfg := pixelgl.WindowConfig{
Title: title,
Bounds: pixel.R(0, 0, float64(*winX), float64(*winY)),
VSync: true,
Resizable: true,
}
pixelWindow, err := pixelgl.NewWindow(cfg)
if err != nil {
return nil, err
}
return &Window{
PixelWindow: pixelWindow,
Title: title,
KeyEventHandlers: make(map[ebiten.Key][]func()),
}, nil
}
// TODO: a stop or other cancellation mechanism
func (w *Window) Run(f func()) {
for !w.PixelWindow.Closed() {
f()
w.PixelWindow.Update()
}
func (w *Window) OnKeyUp(key ebiten.Key, f func()) {
}
func (w *Window) run(screen *ebiten.Image) error {
if err := w.updateFn(); err != nil {
return err
}
// Process keys
if !ebiten.IsDrawingSkipped() {
if err := w.drawFn(screen); err != nil {
return err
}
}
return nil
}
// TODO: a stop or other cancellation mechanism
//
// Note that this must be called on the main OS thread
func (w *Window) Run(updateFn func() error, drawFn func(*ebiten.Image) error) error {
w.updateFn = updateFn
w.drawFn = drawFn
return ebiten.Run(w.run, *winX, *winY, 1, w.Title)
}