unicornpaint

A web-based painting app for raspberry PI and pimoroni Unicorn Hat HD
Log | Files | Refs | README

FakeUnicorn2.go (1240B)


      1 // +build linux,386 linux,amd64
      2 
      3 package unicorn
      4 
      5 import (
      6 	"image"
      7 
      8 	"github.com/veandco/go-sdl2/sdl"
      9 )
     10 
     11 type FakeUnicorn2 struct {
     12 	*BaseUnicorn2
     13 	*BaseFakeUnicorn
     14 }
     15 
     16 func (u *FakeUnicorn2) renderImage(im image.Image) {
     17 	b := im.Bounds()
     18 	width, height := b.Dx(), b.Dy()
     19 	for x := 0; x < width; x++ {
     20 		for y := 0; y < height; y++ {
     21 			col := im.At(x, y)
     22 			r, g, b, _ := col.RGBA()
     23 			// Ignore alpha for now, not worked out how it should work on real unicorn
     24 			if err := u.renderer.SetDrawColor(uint8(r), uint8(g), uint8(b), uint8(255)); err != nil {
     25 				panic(err)
     26 			}
     27 			cellWidth := u.displayWidth / int32(width)
     28 			cellHeight := u.displayHeight / int32(height)
     29 			if err := u.renderer.FillRect(&sdl.Rect{
     30 				X: cellWidth * int32(x),
     31 				Y: u.displayHeight - (cellHeight * int32(y)) - cellHeight, // SDL Y coordinate is from the top
     32 				W: cellWidth,
     33 				H: cellHeight,
     34 			}); err != nil {
     35 				panic(err)
     36 			}
     37 		}
     38 	}
     39 	u.renderer.Present()
     40 }
     41 
     42 func (u *FakeUnicorn2) StartRender() chan bool {
     43 	return u.StartRenderBase(u.renderImage)
     44 }
     45 
     46 func NewUnicorn2() (*FakeUnicorn2, error) {
     47 	baseFake, err := NewBaseFakeUnicorn(300, 300)
     48 	if err != nil {
     49 		return nil, err
     50 	}
     51 	return &FakeUnicorn2{
     52 		NewBaseUnicorn2(),
     53 		baseFake,
     54 	}, nil
     55 }