Browse Source

Change public API to use int instead of int32 everywhere

master
Noah Petherbridge 1 year ago
parent
commit
e758c46d70
14 changed files with 95 additions and 100 deletions
  1. +4
    -14
      README.md
  2. +2
    -2
      canvas/text.go
  3. +1
    -1
      canvas/texture.go
  4. +18
    -18
      examples/hello-wasm/main_wasm.go
  5. +18
    -18
      examples/hello-world/main.go
  6. +9
    -1
      functions.go
  7. +8
    -8
      interface.go
  8. +7
    -7
      point.go
  9. +5
    -5
      rect_test.go
  10. +10
    -10
      sdl/canvas.go
  11. +4
    -4
      sdl/text.go
  12. +1
    -1
      sdl/texture.go
  13. +4
    -4
      sdl/utils.go
  14. +4
    -7
      shapes.go

+ 4
- 14
README.md View File

@@ -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.)



+ 2
- 2
canvas/text.go View File

@@ -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
}

+ 1
- 1
canvas/texture.go 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.
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.


+ 18
- 18
examples/hello-wasm/main_wasm.go View File

@@ -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),
})
}

+ 18
- 18
examples/hello-world/main.go View File

@@ -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),
})
}

+ 9
- 1
functions.go 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
// 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 {


+ 8
- 8
interface.go View File

@@ -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


+ 7
- 7
point.go View File

@@ -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
}

+ 5
- 5
rect_test.go View File

@@ -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,
},
}



+ 10
- 10
sdl/canvas.go View File

@@ -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),
})
}

+ 4
- 4
sdl/text.go View File

@@ -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,
}


+ 1
- 1
sdl/texture.go View File

@@ -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.


+ 4
- 4
sdl/utils.go View File

@@ -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),
}
}

+ 4
- 7
shapes.go View File

@@ -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)


Loading…
Cancel
Save