forked from JoelOtter/termloop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
55 lines (46 loc) · 972 Bytes
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
// A very simple image viewer, which uses Termloop's pixel mode
import (
"fmt"
"os"
tl "github.com/LtLi0n/termloop"
tb "github.com/gdamore/tcell/v2/termbox"
)
type Image struct {
e *tl.Entity
}
func NewImage(c *tl.Canvas) *Image {
i := Image{e: tl.NewEntity(0, 0, len(*c), len((*c)[0]))}
i.e.ApplyCanvas(c)
return &i
}
func (i *Image) Draw(s *tl.Screen) { i.e.Draw(s) }
func (i *Image) Tick(ev tb.Event) {
// Enable arrow key movement
if ev.Type == tb.EventKey {
x, y := i.e.Position()
switch ev.Key {
case tb.KeyArrowRight:
x -= 1
case tb.KeyArrowLeft:
x += 1
case tb.KeyArrowUp:
y += 1
case tb.KeyArrowDown:
y -= 1
}
i.e.SetPosition(x, y)
}
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Please provide a filepath to open")
return
}
g := tl.NewGame()
g.Screen().SetFps(30)
g.Screen().EnablePixelMode()
c := tl.BackgroundCanvasFromFile(os.Args[1])
g.Screen().AddEntity(NewImage(c))
g.Start()
}