Skip to content

Commit 32e319a

Browse files
committed
Remove x,y,w,h from PixMap.Copy, Merge and Foreach
1 parent 8c481dc commit 32e319a

File tree

4 files changed

+46
-28
lines changed

4 files changed

+46
-28
lines changed

devtools/internal/inspector/spr.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (m *sprSelectionMode) update(spr *Spr) {
6565
}
6666

6767
func (m *sprSelectionMode) draw() {
68-
pi.SprSheet().Copy(0, 0, pi.SprSheet().Width(), pi.SprSheet().Height(), pi.Scr(), m.camera.X, m.camera.Y)
68+
pi.SprSheet().Copy(pi.Scr(), m.camera.X, m.camera.Y)
6969

7070
mouseX, mouseY := pi.MousePos.X, pi.MousePos.Y
7171

examples/pixmap/main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@ func main() {
1616
pi.Load(resources)
1717

1818
// copy from sprite-sheet to sprite-sheet:
19-
pi.SprSheet().Copy(10, 0, 100, 100, pi.SprSheet(), 0, 0)
19+
src := pi.SprSheet().WithClip(10, 0, 100, 100)
20+
src.Copy(pi.SprSheet(), 0, 0)
2021

2122
// draw a filled rectangle directly to sprite-sheet:
2223
pi.SprSheet().RectFill(60, 30, 70, 40, 7)
2324

2425
// merge from sprite-sheet to screen using custom merge function, which merges two lines
25-
pi.SprSheet().Merge(-1, -1, 103, 70, pi.Scr(), -1, -1, func(dst, src []byte) {
26+
src = pi.SprSheet().WithClip(-1, -1, 103, 70)
27+
src.Merge(pi.Scr(), -1, -1, func(dst, src []byte) {
2628
for x := 0; x < len(dst); x++ {
2729
dst[x] += pi.Pal[src[x]] + 1
2830
}
2931
})
3032

3133
// update each line in a loop:
32-
pi.Scr().Foreach(10, 10, 16, 16, func(x, y int, line []byte) {
34+
src = pi.Scr().WithClip(10, 10, 16, 16)
35+
src.Foreach(func(x, y int, line []byte) {
3336
for i := 0; i < len(line); i++ {
3437
line[i] = byte(i)
3538
}

pixmap.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ type Pointer struct {
226226
RemainingLines int
227227
}
228228

229-
// Copy copies the region specified by x, y, w, h into dst PixMap at dstX,dstY position.
230-
func (p PixMap) Copy(x, y, w, h int, dst PixMap, dstX, dstY int) {
231-
dstPtr, srcPtr := p.pointersForCopy(x, y, w, h, dst, dstX, dstY)
229+
// Copy copies the clipped pixmap into dst PixMap at dstX,dstY position.
230+
func (p PixMap) Copy(dst PixMap, dstX, dstY int) {
231+
dstPtr, srcPtr := p.pointersForCopy(dst, dstX, dstY)
232232

233233
remainingLines := MinInt(dstPtr.RemainingLines, srcPtr.RemainingLines)
234234

@@ -247,8 +247,8 @@ func (p PixMap) Copy(x, y, w, h int, dst PixMap, dstX, dstY int) {
247247
}
248248

249249
// Merge merges destination with source by running merge operation for each destination line.
250-
func (p PixMap) Merge(x, y, w, h int, dst PixMap, dstX, dstY int, merge func(dst, src []byte)) {
251-
dstPtr, srcPtr := p.pointersForCopy(x, y, w, h, dst, dstX, dstY)
250+
func (p PixMap) Merge(dst PixMap, dstX, dstY int, merge func(dst, src []byte)) {
251+
dstPtr, srcPtr := p.pointersForCopy(dst, dstX, dstY)
252252

253253
remainingLines := MinInt(dstPtr.RemainingLines, srcPtr.RemainingLines)
254254

@@ -266,14 +266,19 @@ func (p PixMap) Merge(x, y, w, h int, dst PixMap, dstX, dstY int, merge func(dst
266266
}
267267
}
268268

269-
// Foreach runs the update function on PixMap fragment specified by x, y, w and h.
269+
// Foreach runs the update function on clipped PixMap.
270270
//
271271
// The update function accepts entire line to increase the performance.
272-
func (p PixMap) Foreach(x, y, w, h int, update func(x, y int, dst []byte)) {
272+
func (p PixMap) Foreach(update func(x, y int, dst []byte)) {
273273
if update == nil {
274274
return
275275
}
276276

277+
x := p.clip.X
278+
y := p.clip.Y
279+
w := p.clip.W
280+
h := p.clip.H
281+
277282
ptr, ok := p.Pointer(x, y, w, h)
278283
if !ok {
279284
return
@@ -334,9 +339,9 @@ func (p PixMap) lineOfColor(col byte, length int) []byte {
334339
return line
335340
}
336341

337-
func (p PixMap) pointersForCopy(srcX int, srcY int, w int, h int, dst PixMap, dstX int, dstY int) (Pointer, Pointer) {
338-
dstPtr, _ := dst.Pointer(dstX, dstY, w, h)
339-
srcPtr, _ := p.Pointer(srcX, srcY, w, h)
342+
func (p PixMap) pointersForCopy(dst PixMap, dstX int, dstY int) (Pointer, Pointer) {
343+
dstPtr, _ := dst.Pointer(dstX, dstY, p.clip.W, p.clip.H)
344+
srcPtr, _ := p.Pointer(p.clip.X, p.clip.Y, p.clip.W, p.clip.H)
340345

341346
// both maps must be moved by the same DeltaX and DeltaY
342347
if srcPtr.DeltaX > dstPtr.DeltaX {

pixmap_test.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,15 @@ func TestPixMap_Pointer(t *testing.T) {
351351
func TestPixMap_Foreach(t *testing.T) {
352352
t.Run("should not do anything when update function is nil", func(t *testing.T) {
353353
pixMap := pi.NewPixMap(2, 2)
354-
pixMap.Foreach(0, 0, 2, 2, nil)
354+
pixMap.Foreach(nil)
355355
})
356356

357357
t.Run("should run update line number of times", func(t *testing.T) {
358358
t.Run("inside clipping range", func(t *testing.T) {
359359
pixMap := pi.NewPixMap(1, 3)
360360
timesRun := 0
361-
pixMap.Foreach(0, 0, 1, 2, func(x, y int, dst []byte) {
361+
src := pixMap.WithClip(0, 0, 1, 2)
362+
src.Foreach(func(x, y int, dst []byte) {
362363
timesRun++
363364
})
364365
assert.Equal(t, 2, timesRun)
@@ -367,7 +368,8 @@ func TestPixMap_Foreach(t *testing.T) {
367368
t.Run("outside clipping range", func(t *testing.T) {
368369
pixMap := pi.NewPixMap(1, 3)
369370
timesRun := 0
370-
pixMap.Foreach(0, 0, 1, 4, func(x, y int, dst []byte) {
371+
src := pixMap.WithClip(0, 0, 1, 4)
372+
src.Foreach(func(x, y int, dst []byte) {
371373
timesRun++
372374
})
373375
assert.Equal(t, 3, timesRun)
@@ -380,15 +382,17 @@ func TestPixMap_Foreach(t *testing.T) {
380382

381383
t.Run("x=0", func(t *testing.T) {
382384
var lines [][]byte
383-
pixMap.Foreach(0, 0, 2, 3, func(x, y int, dst []byte) {
385+
src := pixMap.WithClip(0, 0, 2, 3)
386+
src.Foreach(func(x, y int, dst []byte) {
384387
lines = append(lines, dst)
385388
})
386389
assert.Equal(t, [][]byte{{0, 1}, {3, 4}, {6, 7}}, lines)
387390
})
388391

389392
t.Run("x=1", func(t *testing.T) {
390393
var lines [][]byte
391-
pixMap.Foreach(1, 0, 2, 3, func(x, y int, dst []byte) {
394+
src := pixMap.WithClip(1, 0, 2, 3)
395+
src.Foreach(func(x, y int, dst []byte) {
392396
lines = append(lines, dst)
393397
})
394398
assert.Equal(t, [][]byte{{1, 2}, {4, 5}, {7, 8}}, lines)
@@ -401,15 +405,17 @@ func TestPixMap_Foreach(t *testing.T) {
401405

402406
t.Run("inside clipping range", func(t *testing.T) {
403407
var coords []pi.Position
404-
pixMap.Foreach(1, 1, 2, 2, func(x, y int, dst []byte) {
408+
src := pixMap.WithClip(1, 1, 2, 2)
409+
src.Foreach(func(x, y int, dst []byte) {
405410
coords = append(coords, pi.Position{X: x, Y: y})
406411
})
407412
assert.Equal(t, []pi.Position{{X: 1, Y: 1}, {X: 1, Y: 2}}, coords)
408413
})
409414

410415
t.Run("outside clipping range", func(t *testing.T) {
411416
var coords []pi.Position
412-
pixMap.Foreach(-1, -1, 3, 3, func(x, y int, dst []byte) {
417+
src := pixMap.WithClip(-1, -1, 3, 3)
418+
src.Foreach(func(x, y int, dst []byte) {
413419
coords = append(coords, pi.Position{X: x, Y: y})
414420
})
415421
assert.Equal(t, []pi.Position{{X: 0, Y: 0}, {X: 0, Y: 1}}, coords)
@@ -422,14 +428,16 @@ func TestPixMap_Foreach(t *testing.T) {
422428
update := func(x, y int, dst []byte) {
423429
executed = true
424430
}
425-
pixMap.Foreach(0, 0, 0, 1, update) // width = 0
431+
src := pixMap.WithClip(0, 0, 0, 1) // width = 0
432+
src.Foreach(update)
426433
assert.False(t, executed)
427434
})
428435

429436
t.Run("should update pixels", func(t *testing.T) {
430437
pixMap := pi.NewPixMap(2, 3)
431438
i := byte(1)
432-
pixMap.Foreach(0, 0, 2, 3, func(x, y int, dst []byte) {
439+
src := pixMap.WithClip(0, 0, 2, 3)
440+
src.Foreach(func(x, y int, dst []byte) {
433441
dst[0] = i
434442
dst[1] = i + 1
435443
i += 2
@@ -442,7 +450,7 @@ func TestPixMap_Copy(t *testing.T) {
442450
testPixMapCopy(t, pi.PixMap.Copy)
443451
}
444452

445-
func testPixMapCopy(t *testing.T, merge func(pi.PixMap, int, int, int, int, pi.PixMap, int, int)) {
453+
func testPixMapCopy(t *testing.T, merge func(pi.PixMap, pi.PixMap, int, int)) {
446454
t.Run("src bigger than dst", func(t *testing.T) {
447455
src := pi.NewPixMapWithPix([]byte{
448456
1, 2, 3,
@@ -488,7 +496,8 @@ func testPixMapCopy(t *testing.T, merge func(pi.PixMap, int, int, int, int, pi.P
488496
for name, test := range tests {
489497
t.Run(name, func(t *testing.T) {
490498
dst := pi.NewPixMap(dstWidth, dstHeight)
491-
merge(src, test.x, test.y, test.w, test.h, dst, test.dstX, test.dstY)
499+
source := src.WithClip(test.x, test.y, test.w, test.h)
500+
merge(source, dst, test.dstX, test.dstY)
492501
assert.Equal(t, test.expected, dst.Pix())
493502
})
494503
}
@@ -517,16 +526,17 @@ func testPixMapCopy(t *testing.T, merge func(pi.PixMap, int, int, int, int, pi.P
517526
for name, test := range tests {
518527
t.Run(name, func(t *testing.T) {
519528
dst := pi.NewPixMap(dstWidth, dstHeight)
520-
merge(src, test.x, test.y, test.w, test.h, dst, test.dstX, test.dstY)
529+
source := src.WithClip(test.x, test.y, test.w, test.h)
530+
merge(source, dst, test.dstX, test.dstY)
521531
assert.Equal(t, test.expected, dst.Pix())
522532
})
523533
}
524534
})
525535
}
526536

527537
func TestPixMap_Merge(t *testing.T) {
528-
testPixMapCopy(t, func(src pi.PixMap, x int, y int, w int, h int, dst pi.PixMap, dstX int, dstY int) {
529-
src.Merge(x, y, w, h, dst, dstX, dstY, func(dst, src []byte) {
538+
testPixMapCopy(t, func(src pi.PixMap, dst pi.PixMap, dstX int, dstY int) {
539+
src.Merge(dst, dstX, dstY, func(dst, src []byte) {
530540
copy(dst, src)
531541
})
532542
})

0 commit comments

Comments
 (0)