Noah Petherbridge
af67b20d9b
* Initial WebAssembly build target for Doodle in the wasm/ folder. * Add a new render.Engine implementation, lib/render/canvas that uses the HTML 5 Canvas API instead of SDL2 for the WebAssembly target. * Ported the basic DrawLine(), DrawBox() etc. functions from SDL2 to Canvas context2d API. * Fonts are handled with CSS embedded fonts named after the font filename and defined in wasm/index.html * `make wasm` builds the WASM program, and `make wasm-serve` runs a dev Go server that hosts the WASM file for development. The server also watches the dev tree for *.go files and rebuilds the WASM binary automatically on change. * This build "basically" runs the game. UI and fonts all work and mouse movements and clicks are detected. No wallpaper support yet or texture caching (which will crash the game as soon as you click and draw a pixel in your map!)
85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
package wallpaper
|
|
|
|
// The methods that deal in cached Textures for Doodle.
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"image"
|
|
"os"
|
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
|
"git.kirsle.net/apps/doodle/pkg/userdir"
|
|
"golang.org/x/image/bmp"
|
|
)
|
|
|
|
// CornerTexture returns the Texture.
|
|
func (wp *Wallpaper) CornerTexture(e render.Engine) (render.Texturer, error) {
|
|
if !wp.ready {
|
|
return nil, errors.New("wallpaper not ready")
|
|
}
|
|
|
|
if wp.tex.corner == nil {
|
|
tex, err := texture(e, wp.corner, wp.Name+"c")
|
|
wp.tex.corner = tex
|
|
return tex, err
|
|
}
|
|
return wp.tex.corner, nil
|
|
}
|
|
|
|
// TopTexture returns the Texture.
|
|
func (wp *Wallpaper) TopTexture(e render.Engine) (render.Texturer, error) {
|
|
if wp.tex.top == nil {
|
|
tex, err := texture(e, wp.top, wp.Name+"t")
|
|
wp.tex.top = tex
|
|
return tex, err
|
|
}
|
|
return wp.tex.top, nil
|
|
}
|
|
|
|
// LeftTexture returns the Texture.
|
|
func (wp *Wallpaper) LeftTexture(e render.Engine) (render.Texturer, error) {
|
|
if wp.tex.left == nil {
|
|
tex, err := texture(e, wp.left, wp.Name+"l")
|
|
wp.tex.left = tex
|
|
return tex, err
|
|
}
|
|
return wp.tex.left, nil
|
|
}
|
|
|
|
// RepeatTexture returns the Texture.
|
|
func (wp *Wallpaper) RepeatTexture(e render.Engine) (render.Texturer, error) {
|
|
if wp.tex.repeat == nil {
|
|
tex, err := texture(e, wp.repeat, wp.Name+"x")
|
|
wp.tex.repeat = tex
|
|
return tex, err
|
|
}
|
|
return wp.tex.repeat, nil
|
|
}
|
|
|
|
func texture(e render.Engine, img *image.RGBA, name string) (render.Texturer, error) {
|
|
filename := userdir.CacheFilename("wallpaper", name+".bmp")
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
fh, err := os.Create(filename)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("texture(%s): %s", name, err.Error())
|
|
}
|
|
|
|
err = bmp.Encode(fh, img)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("texture(%s): bmp.Encode: %s", name, err.Error())
|
|
}
|
|
|
|
err = fh.Close()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("texture(%s): fh.Close: %s", name, err.Error())
|
|
}
|
|
}
|
|
|
|
texture, err := e.NewBitmap(filename)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("CornerTexture: NewBitmap(%s): %s", filename, err.Error())
|
|
}
|
|
return texture, nil
|
|
}
|