summaryrefslogtreecommitdiffstats
path: root/numbers/numbers.go
blob: 21e5cb5ac66a9a2ef9ed1610a91751b423bf1db3 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package numbers

import (
	"github.com/veandco/go-sdl2/sdl"
)

const width = 60

func drawDigit(surface *sdl.Surface, d int, x, y int32) error {
	const (
		height    = 100
		thickness = 10
	)
	white := sdl.MapRGB(surface.Format, 245, 245, 245)
	switch d {
	case 0:
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x + width - thickness, Y: y, W: thickness, H: height}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: thickness, H: height}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height - thickness, W: width, H: thickness}, white)
	case 1:
		surface.FillRect(&sdl.Rect{X: x + width - thickness, Y: y, W: thickness, H: height}, white)
	case 2:
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x + width - thickness, Y: y, W: thickness, H: height / 2}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height/2 - thickness/2, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height/2, W: thickness, H: height / 2}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height - thickness, W: width, H: thickness}, white)
	case 3:
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x + width - thickness, Y: y, W: thickness, H: height}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height/2 - thickness/2, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height - thickness, W: width, H: thickness}, white)
	case 4:
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: thickness, H: height / 2}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height/2 - thickness/2, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x + width - thickness, Y: y, W: thickness, H: height}, white)
	case 5:
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: thickness, H: height / 2}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height/2 - thickness/2, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x + width - thickness, Y: y + height/2, W: thickness, H: height / 2}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height - thickness, W: width, H: thickness}, white)
	case 6:
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: thickness, H: height}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height/2 - thickness/2, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x + width - thickness, Y: y + height/2, W: thickness, H: height / 2}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height - thickness, W: width, H: thickness}, white)
	case 7:
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x + width - thickness, Y: y, W: thickness, H: height}, white)
	case 8:
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: thickness, H: height}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x + width - thickness, Y: y, W: thickness, H: height}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height/2 - thickness/2, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height - thickness, W: width, H: thickness}, white)
	case 9:
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: thickness, H: height / 2}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x + width - thickness, Y: y, W: thickness, H: height}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height/2 - thickness/2, W: width, H: thickness}, white)
		surface.FillRect(&sdl.Rect{X: x, Y: y + height - thickness, W: width, H: thickness}, white)
	default:
		panic("invalid digit")
	}
	return nil
}

func Draw(surface *sdl.Surface, n int, x, y int32) error {
	const padding = 20

	if n == 0 {
		return drawDigit(surface, 0, x, y)
	}
	digits := make([]int, 0, 19)
	for ; n > 0; n /= 10 {
		digits = append(digits, n%10)
	}
	for i := len(digits) - 1; i >= 0; i-- {
		if err := drawDigit(surface, digits[i], x, y); err != nil {
			return err
		}
		x += width + padding
	}
	return nil
}