unicornpaint

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

BaseFakeUnicorn.go (1674B)


      1 // +build linux,386 linux,amd64
      2 
      3 package unicorn
      4 
      5 import "github.com/veandco/go-sdl2/sdl"
      6 
      7 // BaseFakeUnicorn ...
      8 // The base for FakeUnicorn & FakeUnicorn2
      9 // Share the SDL code.
     10 type BaseFakeUnicorn struct {
     11 	displayWidth  int32
     12 	displayHeight int32
     13 	window        *sdl.Window
     14 	renderer      *sdl.Renderer
     15 }
     16 
     17 func NewBaseFakeUnicorn(width, height int32) (*BaseFakeUnicorn, error) {
     18 	if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
     19 		return nil, err
     20 	}
     21 
     22 	unicorn := &BaseFakeUnicorn{
     23 		displayWidth:  width,
     24 		displayHeight: height,
     25 		window:        nil,
     26 		renderer:      nil,
     27 	}
     28 	if err := unicorn.createWindow(); err != nil {
     29 		unicorn.Close()
     30 		return nil, err
     31 	}
     32 	if err := unicorn.createRenderer(); err != nil {
     33 		unicorn.Close()
     34 		return nil, err
     35 	}
     36 	return unicorn, nil
     37 }
     38 
     39 func (f *BaseFakeUnicorn) createWindow() error {
     40 	window, err := sdl.CreateWindow("Fake Unicorn",
     41 		sdl.WINDOWPOS_UNDEFINED,
     42 		sdl.WINDOWPOS_UNDEFINED,
     43 		f.displayWidth,
     44 		f.displayHeight,
     45 		sdl.WINDOW_SHOWN)
     46 	f.window = window
     47 	return err
     48 }
     49 
     50 func (f *BaseFakeUnicorn) createRenderer() error {
     51 	renderer, err := sdl.CreateRenderer(f.window, -1, sdl.RENDERER_ACCELERATED)
     52 	f.renderer = renderer
     53 	return err
     54 }
     55 
     56 func (f *BaseFakeUnicorn) Close() error {
     57 	if f.window != nil {
     58 		f.window.Destroy()
     59 	}
     60 	if f.renderer != nil {
     61 		f.renderer.Destroy()
     62 	}
     63 	return nil
     64 }
     65 
     66 // MainLoop ...
     67 // Handle UI events so OS doesn't think we're frozen
     68 func (f *BaseFakeUnicorn) MainLoop() {
     69 	running := true
     70 	for running {
     71 		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
     72 			switch event.(type) {
     73 			case *sdl.QuitEvent:
     74 				println("Quit")
     75 				running = false
     76 				break
     77 			}
     78 		}
     79 	}
     80 }