2019-06-27 01:36:54 +00:00
|
|
|
package canvas
|
|
|
|
|
|
|
|
import (
|
2019-06-27 18:57:26 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
2019-06-27 05:44:08 +00:00
|
|
|
"errors"
|
2019-06-27 18:57:26 +00:00
|
|
|
"image"
|
|
|
|
"image/png"
|
2019-06-27 05:44:08 +00:00
|
|
|
"syscall/js"
|
2019-06-27 01:36:54 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.kirsle.net/apps/doodle/lib/events"
|
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Engine implements a rendering engine targeting an HTML canvas for
|
|
|
|
// WebAssembly targets.
|
|
|
|
type Engine struct {
|
|
|
|
canvas Canvas
|
|
|
|
startTime time.Time
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
ticks uint32
|
|
|
|
|
|
|
|
// Private fields.
|
2019-06-27 18:57:26 +00:00
|
|
|
events *events.State
|
|
|
|
running bool
|
|
|
|
textures map[string]*Texture // cached texture PNG images
|
2019-06-27 03:33:24 +00:00
|
|
|
|
|
|
|
// Event channel. WASM subscribes to events asynchronously using the
|
|
|
|
// JavaScript APIs, whereas SDL2 polls the event queue which orders them
|
|
|
|
// all up for processing. This channel will order and queue the events.
|
|
|
|
queue chan Event
|
2019-06-27 01:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates the Canvas Engine.
|
|
|
|
func New(canvasID string) (*Engine, error) {
|
|
|
|
canvas := GetCanvas(canvasID)
|
|
|
|
|
|
|
|
engine := &Engine{
|
|
|
|
canvas: canvas,
|
|
|
|
startTime: time.Now(),
|
|
|
|
events: events.New(),
|
|
|
|
width: canvas.ClientW(),
|
|
|
|
height: canvas.ClientH(),
|
2019-06-27 03:33:24 +00:00
|
|
|
queue: make(chan Event, 1024),
|
2019-06-27 18:57:26 +00:00
|
|
|
textures: map[string]*Texture{},
|
2019-06-27 01:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return engine, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WindowSize returns the size of the canvas window.
|
|
|
|
func (e *Engine) WindowSize() (w, h int) {
|
2019-06-27 18:57:26 +00:00
|
|
|
// Good time to recompute it first?
|
|
|
|
var (
|
|
|
|
window = js.Global().Get("window")
|
|
|
|
width = window.Get("innerWidth").Int()
|
|
|
|
height = window.Get("innerHeight").Int()
|
|
|
|
)
|
|
|
|
e.canvas.Value.Set("width", width)
|
|
|
|
e.canvas.Value.Set("height", height)
|
2019-06-27 01:36:54 +00:00
|
|
|
return e.canvas.ClientW(), e.canvas.ClientH()
|
|
|
|
}
|
|
|
|
|
2019-06-27 03:33:24 +00:00
|
|
|
// GetTicks returns the number of milliseconds since the engine started.
|
2019-06-27 01:36:54 +00:00
|
|
|
func (e *Engine) GetTicks() uint32 {
|
2019-06-27 03:33:24 +00:00
|
|
|
ms := time.Since(e.startTime) * time.Millisecond
|
|
|
|
return uint32(ms)
|
2019-06-27 01:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TO BE IMPLEMENTED...
|
|
|
|
|
|
|
|
func (e *Engine) Setup() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) Present() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 05:44:08 +00:00
|
|
|
// Texture can hold on to cached image textures.
|
|
|
|
type Texture struct {
|
|
|
|
data string // data:image/png URI
|
|
|
|
image js.Value // DOM image element
|
2019-06-27 18:57:26 +00:00
|
|
|
canvas js.Value // Warmed up canvas element
|
|
|
|
ctx2d js.Value // 2D drawing context for the canvas.
|
2019-06-27 05:44:08 +00:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:57:26 +00:00
|
|
|
// NewTexture caches a texture from a bitmap.
|
|
|
|
func (e *Engine) NewTexture(filename string, img image.Image) (render.Texturer, error) {
|
|
|
|
var (
|
|
|
|
fh = bytes.NewBuffer([]byte{})
|
|
|
|
imageSize = img.Bounds().Size()
|
|
|
|
width = imageSize.X
|
|
|
|
height = imageSize.Y
|
|
|
|
)
|
|
|
|
|
|
|
|
// Encode to PNG format.
|
|
|
|
if err := png.Encode(fh, img); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var dataURI = "data:image/png;base64," + base64.StdEncoding.EncodeToString(fh.Bytes())
|
|
|
|
|
|
|
|
tex := &Texture{
|
|
|
|
data: dataURI,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preheat a cached Canvas object.
|
|
|
|
canvas := js.Global().Get("document").Call("createElement", "canvas")
|
|
|
|
canvas.Set("width", width)
|
|
|
|
canvas.Set("height", height)
|
|
|
|
tex.canvas = canvas
|
|
|
|
|
|
|
|
ctx2d := canvas.Call("getContext", "2d")
|
|
|
|
tex.ctx2d = ctx2d
|
|
|
|
|
|
|
|
// Load as a JS Image object.
|
|
|
|
image := js.Global().Call("eval", "new Image()")
|
|
|
|
image.Call("addEventListener", "load", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
|
|
ctx2d.Call("drawImage", image, 0, 0)
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
image.Set("src", tex.data)
|
|
|
|
tex.image = image
|
|
|
|
|
|
|
|
// Cache the texture in memory.
|
|
|
|
e.textures[filename] = tex
|
|
|
|
|
|
|
|
return tex, nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 05:44:08 +00:00
|
|
|
// Size returns the dimensions of the texture.
|
|
|
|
func (t *Texture) Size() render.Rect {
|
|
|
|
return render.NewRect(int32(t.width), int32(t.height))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBitmap initializes a texture from a bitmap image. The image is stored
|
|
|
|
// in HTML5 Session Storage.
|
2019-06-27 01:36:54 +00:00
|
|
|
func (e *Engine) NewBitmap(filename string) (render.Texturer, error) {
|
2019-06-27 18:57:26 +00:00
|
|
|
if tex, ok := e.textures[filename]; ok {
|
|
|
|
return tex, nil
|
2019-06-27 05:44:08 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 18:57:26 +00:00
|
|
|
panic("no bitmap for " + filename)
|
2019-06-27 05:44:08 +00:00
|
|
|
return nil, errors.New("no bitmap data stored for " + filename)
|
2019-06-27 01:36:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 05:44:08 +00:00
|
|
|
// Copy a texturer bitmap onto the canvas.
|
|
|
|
func (e *Engine) Copy(t render.Texturer, src, dist render.Rect) {
|
|
|
|
tex := t.(*Texture)
|
|
|
|
|
2019-06-27 18:57:26 +00:00
|
|
|
// e.canvas.ctx2d.Call("drawImage", tex.image, dist.X, dist.Y)
|
|
|
|
e.canvas.ctx2d.Call("drawImage", tex.canvas, dist.X, dist.Y)
|
2019-06-27 05:44:08 +00:00
|
|
|
|
2019-06-27 01:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delay for a moment.
|
|
|
|
func (e *Engine) Delay(delay uint32) {
|
|
|
|
time.Sleep(time.Duration(delay) * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Teardown tasks.
|
|
|
|
func (e *Engine) Teardown() {}
|
|
|
|
|
|
|
|
func (e *Engine) Loop() error {
|
|
|
|
return nil
|
|
|
|
}
|