diff --git a/buttonbar.go b/buttonbar.go index f494d34..3c2d081 100644 --- a/buttonbar.go +++ b/buttonbar.go @@ -30,7 +30,7 @@ func (b *ButtonBar) Arrange(ctx *Context, bounds Rectangle) { } offset := bounds.X for i := range b.Buttons { - b.Buttons[i].Arrange(ctx, RectSize(offset, bounds.Y, length, bounds.H)) + b.Buttons[i].Arrange(ctx, Rect(offset, bounds.Y, length, bounds.H)) offset += length } default: @@ -39,7 +39,7 @@ func (b *ButtonBar) Arrange(ctx *Context, bounds Rectangle) { } offset := bounds.Y for i := range b.Buttons { - b.Buttons[i].Arrange(ctx, RectSize(bounds.X, offset, bounds.W, length)) + b.Buttons[i].Arrange(ctx, Rect(bounds.X, offset, bounds.W, length)) offset += length } } diff --git a/buyflowerbutton.go b/buyflowerbutton.go index 7aecec2..dc5b905 100644 --- a/buyflowerbutton.go +++ b/buyflowerbutton.go @@ -88,7 +88,7 @@ func (b *BuyFlowerButton) Render(ctx *Context) { iconTexture := b.activeTexture(ctx) pos := Pt(b.Bounds.X, b.Bounds.Y) - iconTexture.CopyResize(ctx.Renderer, RectSize(pos.X, pos.Y-60, b.Bounds.W, 120)) + iconTexture.CopyResize(ctx.Renderer, Rect(pos.X, pos.Y-60, b.Bounds.W, 120)) if (b.IsMouseOver && !b.IsDisabled) || b.IsActive { SetDrawColor(ctx.Renderer, TransparentWhite) ctx.Renderer.FillRect(b.Bounds.SDLPtr()) @@ -102,7 +102,7 @@ func (b *BuyFlowerButton) Render(ctx *Context) { left := b.Bounds.W - 8 - b.hoverOffset top := pos.Y + b.Bounds.H - 20 if left < 0 { - part := Rect(-left, 0, b.hoverTexture.Size().X, b.hoverTexture.Size().Y) + part := RectAbs(-left, 0, b.hoverTexture.Size().X, b.hoverTexture.Size().Y) b.hoverTexture.CopyPart(ctx.Renderer, part, Pt(pos.X, top)) } else { b.hoverTexture.Copy(ctx.Renderer, Pt(pos.X+left, top)) diff --git a/cmd/tins2020/tins2020.go b/cmd/tins2020/tins2020.go index a989243..333b7d3 100644 --- a/cmd/tins2020/tins2020.go +++ b/cmd/tins2020/tins2020.go @@ -118,7 +118,7 @@ func run() error { dialogs.ShowIntro(ctx) w, h := window.GetSize() - app.Arrange(ctx, tins2020.Rect(0, 0, w, h)) + app.Arrange(ctx, tins2020.RectAbs(0, 0, w, h)) for { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { @@ -133,7 +133,7 @@ func run() error { ctx.Settings.Window.Location = tins2020.PtPtr(x, y) case sdl.WINDOWEVENT_SIZE_CHANGED: w, h := window.GetSize() - app.Arrange(ctx, tins2020.Rect(0, 0, w, h)) + app.Arrange(ctx, tins2020.RectAbs(0, 0, w, h)) ctx.Settings.Window.Size = tins2020.PtPtr(w, h) } } diff --git a/conversion_test.go b/conversion_test.go new file mode 100644 index 0000000..a5cf9e1 --- /dev/null +++ b/conversion_test.go @@ -0,0 +1,42 @@ +package tins2020_test + +import ( + "math/rand" + "testing" +) + +type native struct{ a, b, c, d int } + +type similar struct{ a, b, c, d int } + +func (s similar) toNative() native { return native{s.a, s.b, s.c, s.d} } + +type wrapper struct{ native } + +func (w wrapper) toNative() native { return w.native } + +func nativeFunction(n native) int { return n.a + n.b + n.c + n.d } + +func BenchmarkNative(b *testing.B) { + var sum int + for i := 0; i < b.N; i++ { + n := native{rand.Int(), rand.Int(), rand.Int(), rand.Int()} + sum += nativeFunction(n) + } +} + +func BenchmarkSimilar(b *testing.B) { + var sum int + for i := 0; i < b.N; i++ { + s := similar{rand.Int(), rand.Int(), rand.Int(), rand.Int()} + sum += nativeFunction(s.toNative()) + } +} + +func BenchmarkWrapper(b *testing.B) { + var sum int + for i := 0; i < b.N; i++ { + w := wrapper{native{rand.Int(), rand.Int(), rand.Int(), rand.Int()}} + sum += nativeFunction(w.toNative()) + } +} diff --git a/gamecontrols.go b/gamecontrols.go index 4294e0b..9d95285 100644 --- a/gamecontrols.go +++ b/gamecontrols.go @@ -77,10 +77,10 @@ func (c *GameControls) updateFlowerControls(ctx *Context) { func (c *GameControls) Arrange(ctx *Context, bounds Rectangle) { c.Bounds = bounds - c.menu.Arrange(ctx, RectSize(bounds.X, bounds.Y, buttonBarWidth, bounds.H)) - c.top.Arrange(ctx, Rect(bounds.X+bounds.W/2+8, bounds.Y, bounds.Right(), bounds.Y+64)) - c.flowers.Arrange(ctx, RectSize(bounds.Right()-buttonBarWidth, bounds.Y, buttonBarWidth, bounds.H)) - c.otherTools.Arrange(ctx, RectSize(bounds.Right()-buttonBarWidth, bounds.Bottom()-2*buttonBarWidth, buttonBarWidth, 2*buttonBarWidth)) + c.menu.Arrange(ctx, Rect(bounds.X, bounds.Y, buttonBarWidth, bounds.H)) + c.top.Arrange(ctx, RectAbs(bounds.X+bounds.W/2+8, bounds.Y, bounds.Right(), bounds.Y+64)) + c.flowers.Arrange(ctx, Rect(bounds.Right()-buttonBarWidth, bounds.Y, buttonBarWidth, bounds.H)) + c.otherTools.Arrange(ctx, Rect(bounds.Right()-buttonBarWidth, bounds.Bottom()-2*buttonBarWidth, buttonBarWidth, 2*buttonBarWidth)) } func (c *GameControls) Init(ctx *Context) error { @@ -185,7 +185,7 @@ func (c *GameControls) Handle(ctx *Context, event sdl.Event) bool { func (c *GameControls) Render(ctx *Context) { topBar := MustHexColor("#0000007f") SetDrawColor(ctx.Renderer, topBar) - ctx.Renderer.FillRect(Rect(c.menu.Bounds.Right(), 0, c.flowers.Bounds.X, 64).SDLPtr()) + ctx.Renderer.FillRect(RectAbs(c.menu.Bounds.Right(), 0, c.flowers.Bounds.X, 64).SDLPtr()) ctx.Fonts.Font("balance").RenderCopyAlign(ctx.Renderer, FmtMoney(c.game.Balance), Pt(c.top.Bounds.X-8, 58), MustHexColor("#4AC69A"), TextAlignmentRight) c.Container.Render(ctx) diff --git a/iconbutton.go b/iconbutton.go index 6682ea3..162d906 100644 --- a/iconbutton.go +++ b/iconbutton.go @@ -72,7 +72,7 @@ func (b *IconButton) Render(ctx *Context) { } else if b.IconHeight != 0 { size = Pt(b.IconHeight*size.X/size.Y, b.IconHeight) } - iconTexture.CopyResize(ctx.Renderer, RectSize(b.Bounds.X+(b.Bounds.W-size.X)/2, b.Bounds.Y+(b.Bounds.H-size.Y)/2, size.X, size.Y)) + iconTexture.CopyResize(ctx.Renderer, Rect(b.Bounds.X+(b.Bounds.W-size.X)/2, b.Bounds.Y+(b.Bounds.H-size.Y)/2, size.X, size.Y)) } else { iconTexture.CopyResize(ctx.Renderer, b.Bounds) } diff --git a/largedialog.go b/largedialog.go index 4339405..e16c2c1 100644 --- a/largedialog.go +++ b/largedialog.go @@ -56,9 +56,9 @@ type LargeDialog struct { func (d *LargeDialog) Arrange(ctx *Context, bounds Rectangle) { const titleHeight = 64 d.ControlBase.Arrange(ctx, bounds) - d.title.Arrange(ctx, RectSize(bounds.X, bounds.Y, bounds.W, titleHeight)) - d.close.Arrange(ctx, RectSize(bounds.W-64, 0, 64, 64)) - d.content.Arrange(ctx, RectSize(bounds.X+titleHeight, 96, bounds.W-2*titleHeight, bounds.H-titleHeight)) + d.title.Arrange(ctx, Rect(bounds.X, bounds.Y, bounds.W, titleHeight)) + d.close.Arrange(ctx, Rect(bounds.W-64, 0, 64, 64)) + d.content.Arrange(ctx, Rect(bounds.X+titleHeight, 96, bounds.W-2*titleHeight, bounds.H-titleHeight)) } func (d *LargeDialog) Init(ctx *Context) error { diff --git a/projection.go b/projection.go index de31e7b..f31b493 100644 --- a/projection.go +++ b/projection.go @@ -55,11 +55,11 @@ func (p *projection) screenToMapRel(x, y int32) PointF { } func (p *projection) screenToTileFitRect(pos Point) Rectangle { - return RectSize(pos.X-p.tileFitScreenSize.X, pos.Y-p.tileFitScreenSize.Y, 2*p.tileFitScreenSize.X, 2*p.tileFitScreenSize.Y) + return Rect(pos.X-p.tileFitScreenSize.X, pos.Y-p.tileFitScreenSize.Y, 2*p.tileFitScreenSize.X, 2*p.tileFitScreenSize.Y) } func (p *projection) screenToTileRect(pos Point) Rectangle { - return RectSize(pos.X-p.tileScreenOffset.X, pos.Y-p.tileScreenOffset.Y, p.tileScreenSize.X, p.tileScreenSize.Y) + return Rect(pos.X-p.tileScreenOffset.X, pos.Y-p.tileScreenOffset.Y, p.tileScreenSize.X, p.tileScreenSize.Y) } func (p *projection) update(renderer *sdl.Renderer) { @@ -74,8 +74,8 @@ func (p *projection) update(renderer *sdl.Renderer) { log.Fatal(err) } p.windowCenter = Pt(windowW/2, windowH/2) - p.windowInteractRect = Rect(buttonBarWidth, 64, windowW-buttonBarWidth, windowH) - p.windowVisibleRect = Rect(buttonBarWidth, 0, windowW-buttonBarWidth, windowH+p.tileScreenSize.Y) // Adding a tile height to the bottom for trees that stick out from the cells below. + p.windowInteractRect = RectAbs(buttonBarWidth, 64, windowW-buttonBarWidth, windowH) + p.windowVisibleRect = RectAbs(buttonBarWidth, 0, windowW-buttonBarWidth, windowH+p.tileScreenSize.Y) // Adding a tile height to the bottom for trees that stick out from the cells below. } func (p *projection) visibleTiles(action func(int32, int32, Point)) { diff --git a/rect.go b/rect.go index f345692..27bd0d4 100644 --- a/rect.go +++ b/rect.go @@ -6,7 +6,7 @@ type Rectangle struct { sdl.Rect } -func Rect(x1, y1, x2, y2 int32) Rectangle { +func RectAbs(x1, y1, x2, y2 int32) Rectangle { if x1 > x2 { x1, x2 = x2, x1 } @@ -16,7 +16,7 @@ func Rect(x1, y1, x2, y2 int32) Rectangle { return Rectangle{sdl.Rect{X: x1, Y: y1, W: x2 - x1, H: y2 - y1}} } -func RectSize(x, y, w, h int32) Rectangle { return Rectangle{sdl.Rect{X: x, Y: y, W: w, H: h}} } +func Rect(x, y, w, h int32) Rectangle { return Rectangle{sdl.Rect{X: x, Y: y, W: w, H: h}} } func (r Rectangle) Bottom() int32 { return r.Y + r.H } diff --git a/research.go b/research.go index ed0fce2..a4d4312 100644 --- a/research.go +++ b/research.go @@ -92,8 +92,8 @@ func (r *Research) Init(ctx *Context) error { func (r *Research) Arrange(ctx *Context, bounds Rectangle) { r.Container.Arrange(ctx, bounds) - r.specialists.Arrange(ctx, RectSize(r.Bounds.X, r.Bounds.Y+40, r.Bounds.W, r.Bounds.H-40)) - r.input.Arrange(ctx, RectSize(r.Bounds.X, r.Bounds.X+r.Bounds.H-48, r.Bounds.W, 24)) + r.specialists.Arrange(ctx, Rect(r.Bounds.X, r.Bounds.Y+40, r.Bounds.W, r.Bounds.H-40)) + r.input.Arrange(ctx, Rect(r.Bounds.X, r.Bounds.X+r.Bounds.H-48, r.Bounds.W, 24)) r.input.Alignment = TextAlignmentCenter center := Pt(r.Bounds.X+r.Bounds.W/2, r.Bounds.Y+r.Bounds.H/2) @@ -103,7 +103,7 @@ func (r *Research) Arrange(ctx *Context, bounds Rectangle) { angle := (float64((10-i)%10)*0.16 + .2) * math.Pi pos := Pt(int32(distance*math.Cos(angle)), int32(.8*distance*math.Sin(angle))) digitCenter := center.Add(pos) - r.digits[i].Arrange(ctx, RectSize(digitCenter.X-24, digitCenter.Y-24, 48, 48)) + r.digits[i].Arrange(ctx, Rect(digitCenter.X-24, digitCenter.Y-24, 48, 48)) } } diff --git a/textures.go b/textures.go index 4d0eb7b..83bf6f9 100644 --- a/textures.go +++ b/textures.go @@ -29,11 +29,11 @@ func (t *Texture) Size() Point { return t.size } // func (t *Texture) SDLRectPtr() *sdl.Rect { return t.rect.SDLPtr() } func (t *Texture) Copy(renderer *sdl.Renderer, dst Point) { - t.CopyResize(renderer, RectSize(dst.X, dst.Y, t.size.X, t.size.Y)) + t.CopyResize(renderer, Rect(dst.X, dst.Y, t.size.X, t.size.Y)) } func (t *Texture) CopyPart(renderer *sdl.Renderer, src Rectangle, dst Point) { - t.CopyPartResize(renderer, src, RectSize(dst.X, dst.Y, src.W, src.H)) + t.CopyPartResize(renderer, src, Rect(dst.X, dst.Y, src.W, src.H)) } func (t *Texture) CopyPartResize(renderer *sdl.Renderer, src Rectangle, dst Rectangle) { @@ -41,7 +41,7 @@ func (t *Texture) CopyPartResize(renderer *sdl.Renderer, src Rectangle, dst Rect } func (t *Texture) CopyResize(renderer *sdl.Renderer, dst Rectangle) { - t.CopyPartResize(renderer, Rect(0, 0, t.size.X, t.size.Y), dst) + t.CopyPartResize(renderer, RectAbs(0, 0, t.size.X, t.size.Y), dst) } func (t *Texture) SetColor(color sdl.Color) {