From e758c46d705f87c0021d816cae30d1cd72dfd3d7 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Fri, 27 Dec 2019 17:35:42 -0800 Subject: [PATCH] Change public API to use int instead of int32 everywhere --- README.md | 18 ++++------------ canvas/text.go | 4 ++-- canvas/texture.go | 2 +- examples/hello-wasm/main_wasm.go | 36 ++++++++++++++++---------------- examples/hello-world/main.go | 36 ++++++++++++++++---------------- functions.go | 10 ++++++++- interface.go | 16 +++++++------- point.go | 14 ++++++------- rect_test.go | 10 ++++----- sdl/canvas.go | 20 +++++++++--------- sdl/text.go | 8 +++---- sdl/texture.go | 2 +- sdl/utils.go | 8 +++---- shapes.go | 11 ++++------ 14 files changed, 95 insertions(+), 100 deletions(-) diff --git a/README.md b/README.md index f24b18e..6a50c16 100644 --- a/README.md +++ b/README.md @@ -64,8 +64,8 @@ func main() { // Draw the text centered in the window. mw.DrawText(text, render.NewPoint( - (int32(w)/2)-(rect.W/2), - (int32(h)/2)-(rect.H/2), + (w/2) - (rect.W/2), + (h/2) - (rect.H/2), )) mw.Present() @@ -82,13 +82,6 @@ This module was written as part of my drawing-based maze game, code named [Project: Doodle](https://www.kirsle.net/doodle). It is currently in **alpha status** and its API may change and be cleaned up in the future. -There are a few API cleanup tasks on my to-do list for this project, but they -will come later when I have a chance to update the Project: Doodle game -accordingly: - -* I want to replace all `int32` types with normal `int` -- int32 was used - initially due to SDL2 but for the Go API I want to abstract this away. - ## Drawing Methods (Engine) This package provides some _basic_ primitive drawing methods which are @@ -111,13 +104,10 @@ render.Engine interface. The drawing methods supported are: This package defines a handful of types useful for drawing operations. See the godoc for full details. -**Note:** all int32 values are to become normal ints at some point in the -future, pending refactor in my Project: Doodle game. - * Color: an RGBA color holding uint8 values for each channel. * NewRGBA(red, green, blue, alpha uint8) to construct a new color. -* Point: holds an X,Y pair of coordinates (int32, to become int at some point) -* Rect: holds an X,Y and a W,H value (int32). +* Point: holds an X,Y pair of coordinates. +* Rect: holds an X,Y and a W,H value. * Text: holds text and configuration for rendering (color, stroke, shadow, size, etc.) diff --git a/canvas/text.go b/canvas/text.go index 43c2288..5d5709d 100644 --- a/canvas/text.go +++ b/canvas/text.go @@ -81,8 +81,8 @@ func (e *Engine) ComputeTextRect(text render.Text) (render.Rect, error) { rect := render.Rect{ // TODO: the only TextMetrics widely supported in browsers is // the width. For height, use the text size for now. - W: int32(measure.Get("width").Int()), - H: int32(text.Size), + W: measure.Get("width").Int(), + H: text.Size, } return rect, nil } diff --git a/canvas/texture.go b/canvas/texture.go index 2428e8d..50d60e6 100644 --- a/canvas/texture.go +++ b/canvas/texture.go @@ -69,7 +69,7 @@ func (e *Engine) StoreTexture(name string, img image.Image) (render.Texturer, er // Size returns the dimensions of the texture. func (t *Texture) Size() render.Rect { - return render.NewRect(int32(t.width), int32(t.height)) + return render.NewRect(t.width, t.height) } // LoadTexture recalls a cached texture image. diff --git a/examples/hello-wasm/main_wasm.go b/examples/hello-wasm/main_wasm.go index 9220f1e..dac9a84 100644 --- a/examples/hello-wasm/main_wasm.go +++ b/examples/hello-wasm/main_wasm.go @@ -21,13 +21,13 @@ var ( // Gopher sprite variables gopher render.Texturer texSize render.Rect - speed int32 = 4 - position = render.NewPoint(0, 0) - velocity = render.NewPoint(speed, speed) + speed = 4 + position = render.NewPoint(0, 0) + velocity = render.NewPoint(speed, speed) // Decorative border variables - borderColor = render.Red - borderSize int32 = 12 + borderColor = render.Red + borderSize = 12 // Background color of the window. bgColor = render.RGBA(255, 255, 128, 255) @@ -97,13 +97,13 @@ func update(e render.Engine) { // Bounce off the walls. w, h := e.WindowSize() - if velocity.X > 0 && position.X+texSize.W >= int32(w)-borderSize { + if velocity.X > 0 && position.X+texSize.W >= w-borderSize { velocity.X *= -1 } else if velocity.X < 0 && position.X <= borderSize { velocity.X *= -1 } - if velocity.Y > 0 && position.Y+texSize.H >= int32(h)-borderSize { + if velocity.Y > 0 && position.Y+texSize.H >= h-borderSize { velocity.Y *= -1 } else if velocity.Y < 0 && position.Y <= borderSize { velocity.Y *= -1 @@ -125,7 +125,7 @@ func draw(e render.Engine) { } rect, _ := e.ComputeTextRect(text) e.DrawText(text, render.NewPoint( - (int32(w)/2)-(rect.W/2), + (w/2)-(rect.W/2), 25, )) @@ -144,34 +144,34 @@ func drawBorder(e render.Engine, w, h int) { e.DrawBox(borderColor.Lighten(40), render.Rect{ X: 0, Y: 0, - W: int32(w), - H: int32(h), + W: w, + H: h, }) e.DrawBox(borderColor.Darken(40), render.Rect{ X: borderSize / 2, Y: borderSize / 2, - W: int32(w) - (borderSize / 2), - H: int32(h) - (borderSize / 2), + W: w - (borderSize / 2), + H: h - (borderSize / 2), }) // Now inset a bit and draw the light/dark edges of the bottom/right. e.DrawBox(borderColor.Darken(40), render.Rect{ X: borderSize, Y: borderSize, - W: int32(w), - H: int32(h), + W: w, + H: h, }) e.DrawBox(borderColor.Lighten(40), render.Rect{ X: borderSize, Y: borderSize, - W: int32(w) - borderSize - (borderSize / 2), - H: int32(h) - borderSize - (borderSize / 2), + W: w - borderSize - (borderSize / 2), + H: h - borderSize - (borderSize / 2), }) e.DrawBox(bgColor, render.Rect{ X: borderSize, Y: borderSize, - W: int32(w) - (borderSize * 2), - H: int32(h) - (borderSize * 2), + W: w - (borderSize * 2), + H: h - (borderSize * 2), }) } diff --git a/examples/hello-world/main.go b/examples/hello-world/main.go index aaa1e5b..063b48e 100644 --- a/examples/hello-world/main.go +++ b/examples/hello-world/main.go @@ -17,13 +17,13 @@ var ( // Gopher sprite variables gopher render.Texturer texSize render.Rect - speed int32 = 4 - position = render.NewPoint(0, 0) - velocity = render.NewPoint(speed, speed) + speed = 4 + position = render.NewPoint(0, 0) + velocity = render.NewPoint(speed, speed) // Decorative border variables - borderColor = render.Red - borderSize int32 = 12 + borderColor = render.Red + borderSize = 12 // Background color of the window. bgColor = render.RGBA(255, 255, 128, 255) @@ -84,13 +84,13 @@ func update(e render.Engine) { // Bounce off the walls. w, h := e.WindowSize() - if velocity.X > 0 && position.X+texSize.W >= int32(w)-borderSize { + if velocity.X > 0 && position.X+texSize.W >= w-borderSize { velocity.X *= -1 } else if velocity.X < 0 && position.X <= borderSize { velocity.X *= -1 } - if velocity.Y > 0 && position.Y+texSize.H >= int32(h)-borderSize { + if velocity.Y > 0 && position.Y+texSize.H >= h-borderSize { velocity.Y *= -1 } else if velocity.Y < 0 && position.Y <= borderSize { velocity.Y *= -1 @@ -112,7 +112,7 @@ func draw(e render.Engine) { } rect, _ := e.ComputeTextRect(text) e.DrawText(text, render.NewPoint( - (int32(w)/2)-(rect.W/2), + (w/2)-(rect.W/2), 25, )) @@ -131,34 +131,34 @@ func drawBorder(e render.Engine, w, h int) { e.DrawBox(borderColor.Lighten(40), render.Rect{ X: 0, Y: 0, - W: int32(w), - H: int32(h), + W: w, + H: h, }) e.DrawBox(borderColor.Darken(40), render.Rect{ X: borderSize / 2, Y: borderSize / 2, - W: int32(w) - (borderSize / 2), - H: int32(h) - (borderSize / 2), + W: w - (borderSize / 2), + H: h - (borderSize / 2), }) // Now inset a bit and draw the light/dark edges of the bottom/right. e.DrawBox(borderColor.Darken(40), render.Rect{ X: borderSize, Y: borderSize, - W: int32(w), - H: int32(h), + W: w, + H: h, }) e.DrawBox(borderColor.Lighten(40), render.Rect{ X: borderSize, Y: borderSize, - W: int32(w) - borderSize - (borderSize / 2), - H: int32(h) - borderSize - (borderSize / 2), + W: w - borderSize - (borderSize / 2), + H: h - borderSize - (borderSize / 2), }) e.DrawBox(bgColor, render.Rect{ X: borderSize, Y: borderSize, - W: int32(w) - (borderSize * 2), - H: int32(h) - (borderSize * 2), + W: w - (borderSize * 2), + H: h - (borderSize * 2), }) } diff --git a/functions.go b/functions.go index 7b719a3..a9de42a 100644 --- a/functions.go +++ b/functions.go @@ -33,7 +33,7 @@ func ParseResolution(resi string) (int, int, error) { // TrimBox helps with Engine.Copy() to trim a destination box so that it // won't overflow with the parent container. -func TrimBox(src, dst *Rect, p Point, S Rect, thickness int32) { +func TrimBox(src, dst *Rect, p Point, S Rect, thickness int) { // Constrain source width to not bigger than Canvas width. if src.W > S.W { src.W = S.W @@ -99,6 +99,14 @@ func TrimBox(src, dst *Rect, p Point, S Rect, thickness int32) { } } +// AbsInt returns the absolute value of an integer. +func AbsInt(v int) int { + if v < 0 { + return -v + } + return v +} + // AbsInt32 returns the absolute value of an int32. func AbsInt32(v int32) int32 { if v < 0 { diff --git a/interface.go b/interface.go index cfd6a8c..ce7af8d 100644 --- a/interface.go +++ b/interface.go @@ -53,15 +53,15 @@ type Texturer interface { // Rect has a coordinate and a width and height. type Rect struct { - X int32 - Y int32 - W int32 - H int32 + X int + Y int + W int + H int } // NewRect creates a rectangle of size `width` and `height`. The X,Y values // are initialized to zero. -func NewRect(width, height int32) Rect { +func NewRect(width, height int) Rect { return Rect{ W: width, H: height, @@ -155,9 +155,9 @@ type Text struct { Text string Size int Color Color - Padding int32 - PadX int32 - PadY int32 + Padding int + PadX int + PadY int Stroke Color // Stroke color (if not zero) Shadow Color // Drop shadow color (if not zero) FontFilename string // Path to *.ttf file on disk diff --git a/point.go b/point.go index 6ef757d..182ecf0 100644 --- a/point.go +++ b/point.go @@ -8,8 +8,8 @@ import ( // Point holds an X,Y coordinate value. type Point struct { - X int32 - Y int32 + X int + Y int } // Common points. @@ -18,7 +18,7 @@ var ( ) // NewPoint makes a new Point at an X,Y coordinate. -func NewPoint(x, y int32) Point { +func NewPoint(x, y int) Point { return Point{ X: x, Y: y, @@ -44,8 +44,8 @@ func ParsePoint(v string) (Point, error) { ) } return Point{ - X: int32(x), - Y: int32(y), + X: int(x), + Y: int(y), }, nil } @@ -103,7 +103,7 @@ func (p *Point) UnmarshalText(b []byte) error { ) } - p.X = int32(x) - p.Y = int32(y) + p.X = int(x) + p.Y = int(y) return nil } diff --git a/rect_test.go b/rect_test.go index 25e64af..44a8f08 100644 --- a/rect_test.go +++ b/rect_test.go @@ -10,10 +10,10 @@ import ( func TestIntersection(t *testing.T) { newRect := func(x, y, w, h int) render.Rect { return render.Rect{ - X: int32(x), - Y: int32(y), - W: int32(w), - H: int32(h), + X: x, + Y: y, + W: w, + H: h, } } @@ -62,7 +62,7 @@ func TestIntersection(t *testing.T) { { A: newRect(0, 30, 11, 62), B: newRect(7, 4, 17, 28), - Expect: false, + Expect: true, }, } diff --git a/sdl/canvas.go b/sdl/canvas.go index bc26818..5665622 100644 --- a/sdl/canvas.go +++ b/sdl/canvas.go @@ -19,7 +19,7 @@ func (r *Renderer) DrawPoint(color render.Color, point render.Point) { if color != r.lastColor { r.renderer.SetDrawColor(color.Red, color.Green, color.Blue, color.Alpha) } - r.renderer.DrawPoint(point.X, point.Y) + r.renderer.DrawPoint(int32(point.X), int32(point.Y)) } // DrawLine draws a line between two points. @@ -27,7 +27,7 @@ func (r *Renderer) DrawLine(color render.Color, a, b render.Point) { if color != r.lastColor { r.renderer.SetDrawColor(color.Red, color.Green, color.Blue, color.Alpha) } - r.renderer.DrawLine(a.X, a.Y, b.X, b.Y) + r.renderer.DrawLine(int32(a.X), int32(a.Y), int32(b.X), int32(b.Y)) } // DrawRect draws a rectangle. @@ -36,10 +36,10 @@ func (r *Renderer) DrawRect(color render.Color, rect render.Rect) { r.renderer.SetDrawColor(color.Red, color.Green, color.Blue, color.Alpha) } r.renderer.DrawRect(&sdl.Rect{ - X: rect.X, - Y: rect.Y, - W: rect.W, - H: rect.H, + X: int32(rect.X), + Y: int32(rect.Y), + W: int32(rect.W), + H: int32(rect.H), }) } @@ -49,9 +49,9 @@ func (r *Renderer) DrawBox(color render.Color, rect render.Rect) { r.renderer.SetDrawColor(color.Red, color.Green, color.Blue, color.Alpha) } r.renderer.FillRect(&sdl.Rect{ - X: rect.X, - Y: rect.Y, - W: rect.W, - H: rect.H, + X: int32(rect.X), + Y: int32(rect.Y), + W: int32(rect.W), + H: int32(rect.H), }) } diff --git a/sdl/text.go b/sdl/text.go index 54182b3..66e2102 100644 --- a/sdl/text.go +++ b/sdl/text.go @@ -95,8 +95,8 @@ func (r *Renderer) ComputeTextRect(text render.Text) (render.Rect, error) { } defer surface.Free() - rect.W = surface.W - rect.H = surface.H + rect.W = int(surface.W) + rect.H = int(surface.H) return rect, err } @@ -125,8 +125,8 @@ func (r *Renderer) DrawText(text render.Text, point render.Point) error { defer tex.Destroy() tmp := &sdl.Rect{ - X: point.X + dx, - Y: point.Y + dy, + X: int32(point.X) + dx, + Y: int32(point.Y) + dy, W: surface.W, H: surface.H, } diff --git a/sdl/texture.go b/sdl/texture.go index 8c52e6b..09966ab 100644 --- a/sdl/texture.go +++ b/sdl/texture.go @@ -72,7 +72,7 @@ func (r *Renderer) StoreTexture(name string, img image.Image) (render.Texturer, // Size returns the dimensions of the texture. func (t *Texture) Size() render.Rect { - return render.NewRect(t.width, t.height) + return render.NewRect(int(t.width), int(t.height)) } // LoadTexture initializes a texture from a bitmap image. diff --git a/sdl/utils.go b/sdl/utils.go index ee72a9b..2ca5629 100644 --- a/sdl/utils.go +++ b/sdl/utils.go @@ -18,9 +18,9 @@ func ColorToSDL(c render.Color) sdl.Color { // RectToSDL converts Doodle's Rect type to an sdl.Rect. func RectToSDL(r render.Rect) sdl.Rect { return sdl.Rect{ - X: r.X, - Y: r.Y, - W: r.W, - H: r.H, + X: int32(r.X), + Y: int32(r.Y), + W: int32(r.W), + H: int32(r.H), } } diff --git a/shapes.go b/shapes.go index fb873cb..ae214f6 100644 --- a/shapes.go +++ b/shapes.go @@ -32,10 +32,7 @@ func IterLine(p1 Point, p2 Point) chan Point { x := float64(x1) y := float64(y1) for i := 0; i <= int(step); i++ { - generator <- Point{ - X: int32(x), - Y: int32(y), - } + generator <- NewPoint(int(x), int(y)) x += dx y += dy } @@ -95,10 +92,10 @@ func IterRect(p1, p2 Point) chan Point { // bottom-right corners of a rectangle that encompasses the ellipse. func IterEllipse(A, B Point) chan Point { var ( - width = AbsInt32(B.X - A.X) - height = AbsInt32(B.Y - A.Y) + width = AbsInt(B.X - A.X) + height = AbsInt(B.Y - A.Y) radius = NewPoint(width/2, height/2) - center = NewPoint(AbsInt32(B.X-radius.X), AbsInt32(B.Y-radius.Y)) + center = NewPoint(AbsInt(B.X-radius.X), AbsInt(B.Y-radius.Y)) ) return MidpointEllipse(center, radius)