diff options
Diffstat (limited to 'numbers')
| -rw-r--r-- | numbers/numbers.go | 94 |
1 files changed, 37 insertions, 57 deletions
diff --git a/numbers/numbers.go b/numbers/numbers.go index 21e5cb5..7a2194a 100644 --- a/numbers/numbers.go +++ b/numbers/numbers.go @@ -6,80 +6,60 @@ import ( const width = 60 -func drawDigit(surface *sdl.Surface, d int, x, y int32) error { +var digitSegments = [][]int{ + {0, 1, 2, 4, 5, 6}, + {2, 5}, + {0, 2, 3, 4, 6}, + {0, 2, 3, 5, 6}, + {1, 2, 3, 5}, + {0, 1, 3, 5, 6}, + {0, 1, 3, 4, 5, 6}, + {0, 2, 5}, + {0, 1, 2, 3, 4, 5, 6}, + {0, 1, 2, 3, 5, 6}, +} + +func drawDigit(renderer *sdl.Renderer, 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") + for _, segment := range digitSegments[d] { + switch segment { + case 0: + renderer.FillRect(&sdl.Rect{X: x, Y: y, W: width, H: thickness}) + case 1: + renderer.FillRect(&sdl.Rect{X: x, Y: y, W: thickness, H: height / 2}) + case 2: + renderer.FillRect(&sdl.Rect{X: x + width - thickness, Y: y, W: thickness, H: height / 2}) + case 3: + renderer.FillRect(&sdl.Rect{X: x, Y: y + height/2 - thickness/2, W: width, H: thickness}) + case 4: + renderer.FillRect(&sdl.Rect{X: x, Y: y + height/2, W: thickness, H: height / 2}) + case 5: + renderer.FillRect(&sdl.Rect{X: x + width - thickness, Y: y + height/2, W: thickness, H: height / 2}) + case 6: + renderer.FillRect(&sdl.Rect{X: x, Y: y + height - thickness, W: width, H: thickness}) + } } return nil } -func Draw(surface *sdl.Surface, n int, x, y int32) error { +func Draw(renderer *sdl.Renderer, n int, x, y int32) error { const padding = 20 + if err := renderer.SetDrawColor(245, 245, 245, 255); err != nil { + return err + } if n == 0 { - return drawDigit(surface, 0, x, y) + return drawDigit(renderer, 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 { + if err := drawDigit(renderer, digits[i], x, y); err != nil { return err } x += width + padding |
