From 827f639912529589e651da5c35e28881c8e08c1f Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sun, 25 Aug 2024 12:05:01 -0700 Subject: Port to SDL. --- numbers/numbers.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 numbers/numbers.go (limited to 'numbers/numbers.go') 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 +} -- cgit v1.3.1