Skip to content

Use Go 1.21 #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
go: [ "1.20" ]
go: [ 1.21 ]
env:
DISPLAY: ':99.0'
steps:
Expand Down Expand Up @@ -57,10 +57,10 @@ jobs:
run: GOOS=js GOARCH=wasm go test -v ./...

- name: Lint
uses: golangci/golangci-lint-action@v3.4.0
uses: golangci/golangci-lint-action@v3.6.0
with:
args: "-v"
version: v1.52.1
version: v1.54.1

- name: Generate coverage report
run: go test -v -coverprofile=coverage.txt -covermode=atomic ./...
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Pi is under development. Only limited functionality is provided. API is not stab
## How to get started?

1. Install dependencies
* [Go 1.20+](https://go.dev/dl/)
* [Go 1.21+](https://go.dev/dl/)
* If not on Windows, please install additional dependencies for [Linux](docs/install-linux.md) or [macOS](docs/install-macos.md).
2. Try examples from [examples](examples) directory.
3. Create a new game using provided [Github template](https://github.com/elgopher/pi-template).
Expand Down
14 changes: 1 addition & 13 deletions devtools/internal/lib/github_com-elgopher-pi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions docs/ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
* [ ] map API
* [ ] math API
* [x] Cos, Sin, Atan2
* [x] Min, Max, Mid for integers
* [x] Mid for float64
* [x] Generic Mid for float64 and integers
* [x] Game controller support: gamepad and keyboard
* [x] Mouse support
* [ ] Add mouse wheel support
Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func drawMousePointer() {
func radius(x0, y0, x1, y1 int) int {
dx := math.Abs(float64(x0 - x1))
dy := math.Abs(float64(y0 - y1))
return int(math.Max(dx, dy))
return int(max(dx, dy))
}

func printCmd(command string) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/elgopher/pi

go 1.20
go 1.21

require (
github.com/hajimehoshi/ebiten/v2 v2.5.6
Expand Down
40 changes: 6 additions & 34 deletions internal/bench/math_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,18 @@ import (
"github.com/elgopher/pi"
)

func BenchmarkMinInt(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
for j := 0; j < 60; j++ {
pi.MinInt(j, j+1)
}
for j := 0; j < 60; j++ {
pi.MinInt(j+1, j)
}
}
}

func BenchmarkMaxInt(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
for j := 0; j < 60; j++ {
pi.MaxInt(j, j+1)
}
for j := 0; j < 60; j++ {
pi.MaxInt(j+1, j)
}
}
}

func BenchmarkMidInt(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
for j := 0; j < 20; j++ {
pi.MidInt(j, j+1, j+2) // y
pi.MidInt(j+2, j+1, j) // y
pi.MidInt(j+1, j, j+2) // x
pi.MidInt(j+1, j+2, j) // x
pi.MidInt(j, j+2, j+1) // z
pi.MidInt(j+2, j, j+1) // z
pi.Mid(j, j+1, j+2) // y
pi.Mid(j+2, j+1, j) // y
pi.Mid(j+1, j, j+2) // x
pi.Mid(j+1, j+2, j) // x
pi.Mid(j, j+2, j+1) // z
pi.Mid(j+2, j, j+1) // z
}
}
}
Expand Down
14 changes: 1 addition & 13 deletions internal/fuzz/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,9 @@ import (
"github.com/elgopher/pi"
)

func FuzzMinInt(f *testing.F) {
f.Fuzz(func(t *testing.T, x, y int) {
pi.MinInt(x, y)
})
}

func FuzzMaxInt(f *testing.F) {
f.Fuzz(func(t *testing.T, x, y int) {
pi.MaxInt(x, y)
})
}

func FuzzMidInt(f *testing.F) {
f.Fuzz(func(t *testing.T, x, y, z int) {
pi.MidInt(x, y, z)
pi.Mid(x, y, z)
})
}

Expand Down
64 changes: 9 additions & 55 deletions math.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package pi

import "math"
import (
"cmp"
"math"
)

// Sin returns the sine of angle which is in the range of 0.0-1.0 measured clockwise.
//
Expand Down Expand Up @@ -36,60 +39,11 @@ func Atan2(dx, dy float64) float64 {
return math.Mod(0.75+v/(math.Pi*2), 1)
}

// Int is a generic type for all integer types
type Int interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

// MinInt returns minimum of two integer numbers.
func MinInt[T Int](x, y T) T {
if x < y {
return x
}

return y
}

// MaxInt returns maximum of two integer numbers.
func MaxInt[T Int](x, y T) T {
if x > y {
return x
}

return y
}

// MidInt returns the middle of three integer numbers. Very useful for clamping.
func MidInt[T Int](x, y, z T) T {
if x > y {
x, y = y, x
}

if y > z {
y = z
}

if x > y {
y = x
}

return y
}

// Mid returns the middle of three float64 numbers. Very useful for clamping.
// NaNs are always put at the beginning (are the smallest ones).
func Mid(x, y, z float64) float64 {
if x > y || math.IsNaN(y) {
x, y = y, x
}

if y > z || math.IsNaN(z) {
y = z
}

if x > y || math.IsNaN(y) {
y = x
}
// Mid returns the middle of three ordered values (numbers or strings). Very useful for clamping.
func Mid[T cmp.Ordered](x, y, z T) T {
x, y = min(x, y), max(x, y)
y = min(y, z)
y = max(x, y)

return y
}
58 changes: 19 additions & 39 deletions math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,51 +114,31 @@ func TestAtan2(t *testing.T) {
}
}

func TestMinInt(t *testing.T) {
assert.Equal(t, 0, pi.MinInt(0, 0))
assert.Equal(t, 1, pi.MinInt(1, 2))
assert.Equal(t, 1, pi.MinInt(1, 1))
assert.Equal(t, 1, pi.MinInt(2, 1))
assert.Equal(t, -2, pi.MinInt(-1, -2))
assert.Equal(t, -2, pi.MinInt(-2, 2))
}

func TestMaxInt(t *testing.T) {
assert.Equal(t, 0, pi.MaxInt(0, 0))
assert.Equal(t, 2, pi.MaxInt(2, 1))
assert.Equal(t, 1, pi.MaxInt(1, 1))
assert.Equal(t, 2, pi.MaxInt(1, 2))
assert.Equal(t, -1, pi.MaxInt(-1, -2))
assert.Equal(t, 2, pi.MaxInt(-2, 2))
}

func TestMidInt(t *testing.T) {
assert.Equal(t, 0, pi.MidInt(0, 0, 0))
assert.Equal(t, 1, pi.MidInt(0, 1, 2))
assert.Equal(t, 1, pi.MidInt(2, 1, 0))
assert.Equal(t, 1, pi.MidInt(1, 0, 2))
assert.Equal(t, 1, pi.MidInt(1, 2, 0))
assert.Equal(t, 1, pi.MidInt(2, 0, 1))
assert.Equal(t, 1, pi.MidInt(0, 2, 1))
assert.Equal(t, -1, pi.MidInt(0, -1, -2))
}

func TestMid(t *testing.T) {
assert.Equal(t, 0.0, pi.Mid(0, 0, 0))
assert.Equal(t, 1.0, pi.Mid(0, 1, 2))
assert.Equal(t, 1.0, pi.Mid(2, 1, 0))
assert.Equal(t, 1.0, pi.Mid(1, 0, 2))
assert.Equal(t, 1.0, pi.Mid(1, 2, 0))
assert.Equal(t, 1.0, pi.Mid(2, 0, 1))
assert.Equal(t, 1.0, pi.Mid(0, 2, 1))
assert.Equal(t, -1.0, pi.Mid(0, -1, -2))
assert.Equal(t, 0, pi.Mid(0, 0, 0))
assert.Equal(t, 1, pi.Mid(0, 1, 2))
assert.Equal(t, 1, pi.Mid(2, 1, 0))
assert.Equal(t, 1, pi.Mid(1, 0, 2))
assert.Equal(t, 1, pi.Mid(1, 2, 0))
assert.Equal(t, 1, pi.Mid(2, 0, 1))
assert.Equal(t, 1, pi.Mid(0, 2, 1))
assert.Equal(t, -1, pi.Mid(0, -1, -2))

assert.Equal(t, 0.0, pi.Mid(0.0, 0.0, 0.0))
assert.Equal(t, 1.0, pi.Mid(0.0, 1.0, 2.0))
assert.Equal(t, 1.0, pi.Mid(2.0, 1.0, 0.0))
assert.Equal(t, 1.0, pi.Mid(1.0, 0.0, 2.0))
assert.Equal(t, 1.0, pi.Mid(1.0, 2.0, 0.0))
assert.Equal(t, 1.0, pi.Mid(2.0, 0.0, 1.0))
assert.Equal(t, 1.0, pi.Mid(0.0, 2.0, 1.0))
assert.Equal(t, -1.0, pi.Mid(0.0, -1.0, -2.0))

assertNaN(t, pi.Mid(math.NaN(), math.NaN(), math.NaN()))
assertInf(t, pi.Mid(math.Inf(1), math.Inf(1), math.Inf(1)), 1)
assert.Equal(t, 1.0, pi.Mid(1.0, math.NaN(), 2.0)) // NaNs always go to the beginning
assertNaN(t, pi.Mid(1.0, math.NaN(), 2.0))
assertNaN(t, pi.Mid(math.NaN(), math.NaN(), 1.0))
assertNaN(t, pi.Mid(1.0, math.NaN(), math.NaN()))
assert.Equal(t, 1.0, pi.Mid(1.0, 2.0, math.NaN()))
assertNaN(t, pi.Mid(1.0, 2.0, math.NaN()))
assert.Equal(t, 1.0, pi.Mid(math.Inf(1), math.Inf(-1), 1.0))
}

Expand Down
17 changes: 7 additions & 10 deletions pixmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type PixMap struct {
height int
clip Region

zeroPix []byte
wholeLinePix []byte
}

Expand All @@ -44,7 +43,6 @@ func NewPixMap(width, height int) PixMap {
width: width,
height: height,
clip: Region{W: width, H: height},
zeroPix: make([]byte, len(pixels)),
wholeLinePix: make([]byte, width),
}
}
Expand Down Expand Up @@ -84,7 +82,6 @@ func NewPixMapWithPixels(pixels []byte, lineWidth int) PixMap {
width: lineWidth,
height: height,
clip: Region{W: lineWidth, H: height},
zeroPix: make([]byte, len(pixels)),
wholeLinePix: make([]byte, lineWidth),
}
}
Expand Down Expand Up @@ -147,7 +144,7 @@ func (p PixMap) WithClip(x, y, w, h int) PixMap {

// Clear clears the entire PixMap with color 0. It does not take into account the clipping region.
func (p PixMap) Clear() {
copy(p.pix, p.zeroPix)
clear(p.pix)
}

// ClearCol clears the entire PixMap with specified color. It does not take into account the clipping region.
Expand Down Expand Up @@ -212,8 +209,8 @@ func (p PixMap) Pointer(x, y, w, h int) (ptr Pointer, ok bool) {
DeltaX: dx,
DeltaY: dy,
Pix: pix,
RemainingPixels: MinInt(w, clip.X+clip.W-x),
RemainingLines: MinInt(h, clip.Y+clip.H-y),
RemainingPixels: min(w, clip.X+clip.W-x),
RemainingLines: min(h, clip.Y+clip.H-y),
}, true
}

Expand All @@ -230,13 +227,13 @@ type Pointer struct {
func (p PixMap) Copy(x, y, w, h int, dst PixMap, dstX, dstY int) {
dstPtr, srcPtr := p.pointersForCopy(x, y, w, h, dst, dstX, dstY)

remainingLines := MinInt(dstPtr.RemainingLines, srcPtr.RemainingLines)
remainingLines := min(dstPtr.RemainingLines, srcPtr.RemainingLines)

if remainingLines == 0 {
return
}

remainingPixels := MinInt(dstPtr.RemainingPixels, srcPtr.RemainingPixels)
remainingPixels := min(dstPtr.RemainingPixels, srcPtr.RemainingPixels)

copy(dstPtr.Pix[:remainingPixels], srcPtr.Pix)
for i := 1; i < remainingLines; i++ {
Expand All @@ -250,13 +247,13 @@ func (p PixMap) Copy(x, y, w, h int, dst PixMap, dstX, dstY int) {
func (p PixMap) Merge(x, y, w, h int, dst PixMap, dstX, dstY int, merge func(dst, src []byte)) {
dstPtr, srcPtr := p.pointersForCopy(x, y, w, h, dst, dstX, dstY)

remainingLines := MinInt(dstPtr.RemainingLines, srcPtr.RemainingLines)
remainingLines := min(dstPtr.RemainingLines, srcPtr.RemainingLines)

if remainingLines == 0 {
return
}

remainingPixels := MinInt(dstPtr.RemainingPixels, srcPtr.RemainingPixels)
remainingPixels := min(dstPtr.RemainingPixels, srcPtr.RemainingPixels)

merge(dstPtr.Pix[:remainingPixels], srcPtr.Pix)
for i := 1; i < remainingLines; i++ {
Expand Down