unicornpaint

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

FakeUnicorn.go (1221B)


      1 // +build linux,386 linux,amd64
      2 
      3 package unicorn
      4 
      5 import (
      6 	"github.com/veandco/go-sdl2/sdl"
      7 )
      8 
      9 type FakeUnicorn struct {
     10 	BaseUnicorn
     11 	*BaseFakeUnicorn
     12 }
     13 
     14 // NewUnicorn ...
     15 // Constructs a new fake unicorn out of paint and glue
     16 func NewUnicorn() (*FakeUnicorn, error) {
     17 	width := uint8(16)
     18 	height := uint8(16)
     19 
     20 	baseFake, err := NewBaseFakeUnicorn(300, 300)
     21 	if err != nil {
     22 		return nil, err
     23 	}
     24 
     25 	unicorn := &FakeUnicorn{
     26 		BaseUnicorn{
     27 			pixels: makePixels(width, height),
     28 		},
     29 		baseFake,
     30 	}
     31 
     32 	return unicorn, nil
     33 }
     34 
     35 func (f *FakeUnicorn) Show() {
     36 	width, height := f.GetWidth(), f.GetHeight()
     37 	for x := uint8(0); x < width; x++ {
     38 		for y := uint8(0); y < height; y++ {
     39 			r, g, b := Rgb(f.pixels[x][y])
     40 			if err := f.renderer.SetDrawColor(r, g, b, uint8(255)); err != nil {
     41 				panic(err)
     42 			}
     43 			cellWidth := f.displayWidth / int32(width)
     44 			cellHeight := f.displayHeight / int32(height)
     45 			if err := f.renderer.FillRect(&sdl.Rect{
     46 				X: cellWidth * int32(x),
     47 				Y: f.displayHeight - (cellHeight * int32(y)) - cellHeight, // SDL Y coordinate is from the top
     48 				W: cellWidth,
     49 				H: cellHeight,
     50 			}); err != nil {
     51 				panic(err)
     52 			}
     53 		}
     54 	}
     55 	f.renderer.Present()
     56 }
     57 
     58 func (f *FakeUnicorn) Off() {
     59 	f.Close()
     60 }