Change public API to use int instead of int32 everywhere

master
Noah 2019-12-27 17:35:42 -08:00
parent 930bb3ac72
commit e758c46d70
14 changed files with 95 additions and 100 deletions

View File

@ -64,8 +64,8 @@ func main() {
// Draw the text centered in the window. // Draw the text centered in the window.
mw.DrawText(text, render.NewPoint( mw.DrawText(text, render.NewPoint(
(int32(w)/2)-(rect.W/2), (w/2) - (rect.W/2),
(int32(h)/2)-(rect.H/2), (h/2) - (rect.H/2),
)) ))
mw.Present() 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 [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. **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) ## Drawing Methods (Engine)
This package provides some _basic_ primitive drawing methods which are 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. This package defines a handful of types useful for drawing operations.
See the godoc for full details. 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. * Color: an RGBA color holding uint8 values for each channel.
* NewRGBA(red, green, blue, alpha uint8) to construct a new color. * 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) * Point: holds an X,Y pair of coordinates.
* Rect: holds an X,Y and a W,H value (int32). * Rect: holds an X,Y and a W,H value.
* Text: holds text and configuration for rendering (color, stroke, shadow, * Text: holds text and configuration for rendering (color, stroke, shadow,
size, etc.) size, etc.)

View File

@ -81,8 +81,8 @@ func (e *Engine) ComputeTextRect(text render.Text) (render.Rect, error) {
rect := render.Rect{ rect := render.Rect{
// TODO: the only TextMetrics widely supported in browsers is // TODO: the only TextMetrics widely supported in browsers is
// the width. For height, use the text size for now. // the width. For height, use the text size for now.
W: int32(measure.Get("width").Int()), W: measure.Get("width").Int(),
H: int32(text.Size), H: text.Size,
} }
return rect, nil return rect, nil
} }

View File

@ -69,7 +69,7 @@ func (e *Engine) StoreTexture(name string, img image.Image) (render.Texturer, er
// Size returns the dimensions of the texture. // Size returns the dimensions of the texture.
func (t *Texture) Size() render.Rect { 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. // LoadTexture recalls a cached texture image.

View File

@ -21,13 +21,13 @@ var (
// Gopher sprite variables // Gopher sprite variables
gopher render.Texturer gopher render.Texturer
texSize render.Rect texSize render.Rect
speed int32 = 4 speed = 4
position = render.NewPoint(0, 0) position = render.NewPoint(0, 0)
velocity = render.NewPoint(speed, speed) velocity = render.NewPoint(speed, speed)
// Decorative border variables // Decorative border variables
borderColor = render.Red borderColor = render.Red
borderSize int32 = 12 borderSize = 12
// Background color of the window. // Background color of the window.
bgColor = render.RGBA(255, 255, 128, 255) bgColor = render.RGBA(255, 255, 128, 255)
@ -97,13 +97,13 @@ func update(e render.Engine) {
// Bounce off the walls. // Bounce off the walls.
w, h := e.WindowSize() 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 velocity.X *= -1
} else if velocity.X < 0 && position.X <= borderSize { } else if velocity.X < 0 && position.X <= borderSize {
velocity.X *= -1 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 velocity.Y *= -1
} else if velocity.Y < 0 && position.Y <= borderSize { } else if velocity.Y < 0 && position.Y <= borderSize {
velocity.Y *= -1 velocity.Y *= -1
@ -125,7 +125,7 @@ func draw(e render.Engine) {
} }
rect, _ := e.ComputeTextRect(text) rect, _ := e.ComputeTextRect(text)
e.DrawText(text, render.NewPoint( e.DrawText(text, render.NewPoint(
(int32(w)/2)-(rect.W/2), (w/2)-(rect.W/2),
25, 25,
)) ))
@ -144,34 +144,34 @@ func drawBorder(e render.Engine, w, h int) {
e.DrawBox(borderColor.Lighten(40), render.Rect{ e.DrawBox(borderColor.Lighten(40), render.Rect{
X: 0, X: 0,
Y: 0, Y: 0,
W: int32(w), W: w,
H: int32(h), H: h,
}) })
e.DrawBox(borderColor.Darken(40), render.Rect{ e.DrawBox(borderColor.Darken(40), render.Rect{
X: borderSize / 2, X: borderSize / 2,
Y: borderSize / 2, Y: borderSize / 2,
W: int32(w) - (borderSize / 2), W: w - (borderSize / 2),
H: int32(h) - (borderSize / 2), H: h - (borderSize / 2),
}) })
// Now inset a bit and draw the light/dark edges of the bottom/right. // Now inset a bit and draw the light/dark edges of the bottom/right.
e.DrawBox(borderColor.Darken(40), render.Rect{ e.DrawBox(borderColor.Darken(40), render.Rect{
X: borderSize, X: borderSize,
Y: borderSize, Y: borderSize,
W: int32(w), W: w,
H: int32(h), H: h,
}) })
e.DrawBox(borderColor.Lighten(40), render.Rect{ e.DrawBox(borderColor.Lighten(40), render.Rect{
X: borderSize, X: borderSize,
Y: borderSize, Y: borderSize,
W: int32(w) - borderSize - (borderSize / 2), W: w - borderSize - (borderSize / 2),
H: int32(h) - borderSize - (borderSize / 2), H: h - borderSize - (borderSize / 2),
}) })
e.DrawBox(bgColor, render.Rect{ e.DrawBox(bgColor, render.Rect{
X: borderSize, X: borderSize,
Y: borderSize, Y: borderSize,
W: int32(w) - (borderSize * 2), W: w - (borderSize * 2),
H: int32(h) - (borderSize * 2), H: h - (borderSize * 2),
}) })
} }

View File

@ -17,13 +17,13 @@ var (
// Gopher sprite variables // Gopher sprite variables
gopher render.Texturer gopher render.Texturer
texSize render.Rect texSize render.Rect
speed int32 = 4 speed = 4
position = render.NewPoint(0, 0) position = render.NewPoint(0, 0)
velocity = render.NewPoint(speed, speed) velocity = render.NewPoint(speed, speed)
// Decorative border variables // Decorative border variables
borderColor = render.Red borderColor = render.Red
borderSize int32 = 12 borderSize = 12
// Background color of the window. // Background color of the window.
bgColor = render.RGBA(255, 255, 128, 255) bgColor = render.RGBA(255, 255, 128, 255)
@ -84,13 +84,13 @@ func update(e render.Engine) {
// Bounce off the walls. // Bounce off the walls.
w, h := e.WindowSize() 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 velocity.X *= -1
} else if velocity.X < 0 && position.X <= borderSize { } else if velocity.X < 0 && position.X <= borderSize {
velocity.X *= -1 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 velocity.Y *= -1
} else if velocity.Y < 0 && position.Y <= borderSize { } else if velocity.Y < 0 && position.Y <= borderSize {
velocity.Y *= -1 velocity.Y *= -1
@ -112,7 +112,7 @@ func draw(e render.Engine) {
} }
rect, _ := e.ComputeTextRect(text) rect, _ := e.ComputeTextRect(text)
e.DrawText(text, render.NewPoint( e.DrawText(text, render.NewPoint(
(int32(w)/2)-(rect.W/2), (w/2)-(rect.W/2),
25, 25,
)) ))
@ -131,34 +131,34 @@ func drawBorder(e render.Engine, w, h int) {
e.DrawBox(borderColor.Lighten(40), render.Rect{ e.DrawBox(borderColor.Lighten(40), render.Rect{
X: 0, X: 0,
Y: 0, Y: 0,
W: int32(w), W: w,
H: int32(h), H: h,
}) })
e.DrawBox(borderColor.Darken(40), render.Rect{ e.DrawBox(borderColor.Darken(40), render.Rect{
X: borderSize / 2, X: borderSize / 2,
Y: borderSize / 2, Y: borderSize / 2,
W: int32(w) - (borderSize / 2), W: w - (borderSize / 2),
H: int32(h) - (borderSize / 2), H: h - (borderSize / 2),
}) })
// Now inset a bit and draw the light/dark edges of the bottom/right. // Now inset a bit and draw the light/dark edges of the bottom/right.
e.DrawBox(borderColor.Darken(40), render.Rect{ e.DrawBox(borderColor.Darken(40), render.Rect{
X: borderSize, X: borderSize,
Y: borderSize, Y: borderSize,
W: int32(w), W: w,
H: int32(h), H: h,
}) })
e.DrawBox(borderColor.Lighten(40), render.Rect{ e.DrawBox(borderColor.Lighten(40), render.Rect{
X: borderSize, X: borderSize,
Y: borderSize, Y: borderSize,
W: int32(w) - borderSize - (borderSize / 2), W: w - borderSize - (borderSize / 2),
H: int32(h) - borderSize - (borderSize / 2), H: h - borderSize - (borderSize / 2),
}) })
e.DrawBox(bgColor, render.Rect{ e.DrawBox(bgColor, render.Rect{
X: borderSize, X: borderSize,
Y: borderSize, Y: borderSize,
W: int32(w) - (borderSize * 2), W: w - (borderSize * 2),
H: int32(h) - (borderSize * 2), H: h - (borderSize * 2),
}) })
} }

View File

@ -33,7 +33,7 @@ func ParseResolution(resi string) (int, int, error) {
// TrimBox helps with Engine.Copy() to trim a destination box so that it // TrimBox helps with Engine.Copy() to trim a destination box so that it
// won't overflow with the parent container. // 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. // Constrain source width to not bigger than Canvas width.
if src.W > S.W { if src.W > S.W {
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. // AbsInt32 returns the absolute value of an int32.
func AbsInt32(v int32) int32 { func AbsInt32(v int32) int32 {
if v < 0 { if v < 0 {

View File

@ -53,15 +53,15 @@ type Texturer interface {
// Rect has a coordinate and a width and height. // Rect has a coordinate and a width and height.
type Rect struct { type Rect struct {
X int32 X int
Y int32 Y int
W int32 W int
H int32 H int
} }
// NewRect creates a rectangle of size `width` and `height`. The X,Y values // NewRect creates a rectangle of size `width` and `height`. The X,Y values
// are initialized to zero. // are initialized to zero.
func NewRect(width, height int32) Rect { func NewRect(width, height int) Rect {
return Rect{ return Rect{
W: width, W: width,
H: height, H: height,
@ -155,9 +155,9 @@ type Text struct {
Text string Text string
Size int Size int
Color Color Color Color
Padding int32 Padding int
PadX int32 PadX int
PadY int32 PadY int
Stroke Color // Stroke color (if not zero) Stroke Color // Stroke color (if not zero)
Shadow Color // Drop shadow color (if not zero) Shadow Color // Drop shadow color (if not zero)
FontFilename string // Path to *.ttf file on disk FontFilename string // Path to *.ttf file on disk

View File

@ -8,8 +8,8 @@ import (
// Point holds an X,Y coordinate value. // Point holds an X,Y coordinate value.
type Point struct { type Point struct {
X int32 X int
Y int32 Y int
} }
// Common points. // Common points.
@ -18,7 +18,7 @@ var (
) )
// NewPoint makes a new Point at an X,Y coordinate. // NewPoint makes a new Point at an X,Y coordinate.
func NewPoint(x, y int32) Point { func NewPoint(x, y int) Point {
return Point{ return Point{
X: x, X: x,
Y: y, Y: y,
@ -44,8 +44,8 @@ func ParsePoint(v string) (Point, error) {
) )
} }
return Point{ return Point{
X: int32(x), X: int(x),
Y: int32(y), Y: int(y),
}, nil }, nil
} }
@ -103,7 +103,7 @@ func (p *Point) UnmarshalText(b []byte) error {
) )
} }
p.X = int32(x) p.X = int(x)
p.Y = int32(y) p.Y = int(y)
return nil return nil
} }

View File

@ -10,10 +10,10 @@ import (
func TestIntersection(t *testing.T) { func TestIntersection(t *testing.T) {
newRect := func(x, y, w, h int) render.Rect { newRect := func(x, y, w, h int) render.Rect {
return render.Rect{ return render.Rect{
X: int32(x), X: x,
Y: int32(y), Y: y,
W: int32(w), W: w,
H: int32(h), H: h,
} }
} }
@ -62,7 +62,7 @@ func TestIntersection(t *testing.T) {
{ {
A: newRect(0, 30, 11, 62), A: newRect(0, 30, 11, 62),
B: newRect(7, 4, 17, 28), B: newRect(7, 4, 17, 28),
Expect: false, Expect: true,
}, },
} }

View File

@ -19,7 +19,7 @@ func (r *Renderer) DrawPoint(color render.Color, point render.Point) {
if color != r.lastColor { if color != r.lastColor {
r.renderer.SetDrawColor(color.Red, color.Green, color.Blue, color.Alpha) 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. // 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 { if color != r.lastColor {
r.renderer.SetDrawColor(color.Red, color.Green, color.Blue, color.Alpha) 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. // 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.SetDrawColor(color.Red, color.Green, color.Blue, color.Alpha)
} }
r.renderer.DrawRect(&sdl.Rect{ r.renderer.DrawRect(&sdl.Rect{
X: rect.X, X: int32(rect.X),
Y: rect.Y, Y: int32(rect.Y),
W: rect.W, W: int32(rect.W),
H: rect.H, 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.SetDrawColor(color.Red, color.Green, color.Blue, color.Alpha)
} }
r.renderer.FillRect(&sdl.Rect{ r.renderer.FillRect(&sdl.Rect{
X: rect.X, X: int32(rect.X),
Y: rect.Y, Y: int32(rect.Y),
W: rect.W, W: int32(rect.W),
H: rect.H, H: int32(rect.H),
}) })
} }

View File

@ -95,8 +95,8 @@ func (r *Renderer) ComputeTextRect(text render.Text) (render.Rect, error) {
} }
defer surface.Free() defer surface.Free()
rect.W = surface.W rect.W = int(surface.W)
rect.H = surface.H rect.H = int(surface.H)
return rect, err return rect, err
} }
@ -125,8 +125,8 @@ func (r *Renderer) DrawText(text render.Text, point render.Point) error {
defer tex.Destroy() defer tex.Destroy()
tmp := &sdl.Rect{ tmp := &sdl.Rect{
X: point.X + dx, X: int32(point.X) + dx,
Y: point.Y + dy, Y: int32(point.Y) + dy,
W: surface.W, W: surface.W,
H: surface.H, H: surface.H,
} }

View File

@ -72,7 +72,7 @@ func (r *Renderer) StoreTexture(name string, img image.Image) (render.Texturer,
// Size returns the dimensions of the texture. // Size returns the dimensions of the texture.
func (t *Texture) Size() render.Rect { 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. // LoadTexture initializes a texture from a bitmap image.

View File

@ -18,9 +18,9 @@ func ColorToSDL(c render.Color) sdl.Color {
// RectToSDL converts Doodle's Rect type to an sdl.Rect. // RectToSDL converts Doodle's Rect type to an sdl.Rect.
func RectToSDL(r render.Rect) sdl.Rect { func RectToSDL(r render.Rect) sdl.Rect {
return sdl.Rect{ return sdl.Rect{
X: r.X, X: int32(r.X),
Y: r.Y, Y: int32(r.Y),
W: r.W, W: int32(r.W),
H: r.H, H: int32(r.H),
} }
} }

View File

@ -32,10 +32,7 @@ func IterLine(p1 Point, p2 Point) chan Point {
x := float64(x1) x := float64(x1)
y := float64(y1) y := float64(y1)
for i := 0; i <= int(step); i++ { for i := 0; i <= int(step); i++ {
generator <- Point{ generator <- NewPoint(int(x), int(y))
X: int32(x),
Y: int32(y),
}
x += dx x += dx
y += dy y += dy
} }
@ -95,10 +92,10 @@ func IterRect(p1, p2 Point) chan Point {
// bottom-right corners of a rectangle that encompasses the ellipse. // bottom-right corners of a rectangle that encompasses the ellipse.
func IterEllipse(A, B Point) chan Point { func IterEllipse(A, B Point) chan Point {
var ( var (
width = AbsInt32(B.X - A.X) width = AbsInt(B.X - A.X)
height = AbsInt32(B.Y - A.Y) height = AbsInt(B.Y - A.Y)
radius = NewPoint(width/2, height/2) 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) return MidpointEllipse(center, radius)