diff options
Diffstat (limited to 'numbers')
| -rw-r--r-- | numbers/numbers.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/numbers/numbers.go b/numbers/numbers.go new file mode 100644 index 0000000..21e5cb5 --- /dev/null +++ b/numbers/numbers.go @@ -0,0 +1,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 +} |
