Skip to content

Commit 5cef81a

Browse files
committed
Add PixMap struct
PixMap is a generic data structure for manipulating any kind of pixel data - screen, sprite-sheet etc. PixMap uses a single byte (8 bits) for storing single color/pixel. This means that max 256 colors can be used. PixMap can also be used for maps which not necessary contain pixel colors, such as world map (as long as only 256 different tiles are used). This commit replaces pi.Screen and pi.SpriteSheet with pi.PixMap. PixMap provides also new methods: * Copy - copy PixMap fragment to another PixMap at specific location * Merge - customizable variant of Copy. User can provide his own merge function which will be used instead of directly copying pixels. * Foreach - run code line by line on selected PixMap fragment * Pointer - low-level method for high-performance processing
1 parent 0cc8f5d commit 5cef81a

27 files changed

+1478
-629
lines changed

devtools/internal/inspector/draw.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Draw() {
3737
func cursorOutOfWindow() bool {
3838
x, y := pi.MousePos()
3939
screen := pi.Scr()
40-
return x < 0 || x >= screen.W || y < 0 || y >= screen.H
40+
return x < 0 || x >= screen.Width() || y < 0 || y >= screen.Height()
4141
}
4242

4343
func handleScreenshot() {

devtools/internal/inspector/inspector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func moveBarIfNeeded() {
1414
switch {
1515
case isBarOnTop && mouseY <= 12:
1616
isBarOnTop = false
17-
case !isBarOnTop && mouseY >= pi.Scr().H-12:
17+
case !isBarOnTop && mouseY >= pi.Scr().Height()-12:
1818
isBarOnTop = true
1919
}
2020
}

devtools/internal/inspector/measure.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ func (m *Measure) drawBar() {
2929
mouseX, mouseY := pi.MousePos()
3030
var barY int
3131
if !isBarOnTop {
32-
barY = screen.H - 7
32+
barY = screen.Height() - 7
3333
}
3434

35-
pi.RectFill(0, barY, screen.W, barY+6, BgColor)
35+
pi.RectFill(0, barY, screen.Width(), barY+6, BgColor)
3636

3737
textX := 1
3838
textY := barY + 1

devtools/internal/inspector/toolbar.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func (t *Toolbar) toggle() {
2828
t.pos.X = 0
2929
}
3030
scr := pi.Scr()
31-
if t.pos.X+toolbarWidth > scr.W {
32-
t.pos.X = scr.W - toolbarWidth - 1
31+
if t.pos.X+toolbarWidth > scr.Width() {
32+
t.pos.X = scr.Width() - toolbarWidth - 1
3333
}
3434
t.pos.Y -= toolbarHeight + 2
3535
if t.pos.Y < 0 {

devtools/internal/snapshot/snapshot.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ func Take() {
2323
}
2424

2525
screen := pi.Scr()
26+
pix := screen.Pix()
2627
if snapshot == nil {
27-
snapshot = make([]byte, len(screen.Pix))
28+
snapshot = make([]byte, len(pix))
2829
}
29-
copy(snapshot, screen.Pix)
30+
copy(snapshot, pix)
3031
}
3132

3233
func Draw() {
33-
copy(pi.Scr().Pix, snapshot)
34+
copy(pi.Scr().Pix(), snapshot)
3435
}
3536

3637
func Undo() {

ebitengine/ebitengine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func Run() error {
3030
ebiten.SetScreenClearedEveryFrame(false)
3131
ebiten.SetRunnableOnUnfocused(true)
3232
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
33-
ebiten.SetWindowSize(screen.W*scale(), screen.H*scale())
34-
ebiten.SetWindowSizeLimits(screen.W, screen.H, -1, -1)
33+
ebiten.SetWindowSize(screen.Width()*scale(), screen.Height()*scale())
34+
ebiten.SetWindowSizeLimits(screen.Width(), screen.Height(), -1, -1)
3535
ebiten.SetCursorMode(ebiten.CursorModeHidden)
3636
ebiten.SetWindowTitle("Pi Game")
3737

ebitengine/game.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (e *game) Draw(screen *ebiten.Image) {
7676
}
7777

7878
func (e *game) writeScreenPixels(screen *ebiten.Image) {
79-
pix := pi.Scr().Pix
79+
pix := pi.Scr().Pix()
8080
if e.screenDataRGBA == nil || len(e.screenDataRGBA)/4 != len(pix) {
8181
e.screenDataRGBA = make([]byte, len(pix)*4)
8282
}
@@ -96,5 +96,5 @@ func (e *game) writeScreenPixels(screen *ebiten.Image) {
9696

9797
func (e *game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
9898
scr := pi.Scr()
99-
return scr.W, scr.H
99+
return scr.Width(), scr.Height()
100100
}

examples/memory/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func main() {
1313
pi.Draw = func() {
14-
pixels := pi.Scr().Pix
14+
pixels := pi.Scr().Pix()
1515
for i := 0; i < len(pixels); i++ {
1616
randomColor := byte(rand.Intn(16))
1717
pixels[i] = randomColor // put a random color to each pixel

examples/pixmap/main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Example showing how to use PixMap struct, which is used to store screen
2+
// and sprite-sheet pixels.
3+
package main
4+
5+
import (
6+
"embed"
7+
8+
"github.com/elgopher/pi"
9+
"github.com/elgopher/pi/ebitengine"
10+
)
11+
12+
//go:embed sprite-sheet.png
13+
var resources embed.FS
14+
15+
func main() {
16+
pi.Load(resources)
17+
18+
// copy from sprite-sheet to sprite-sheet:
19+
pi.SprSheet().Copy(10, 0, 100, 100, pi.SprSheet(), 0, 0)
20+
21+
// draw a filled rectangle directly to sprite-sheet:
22+
pi.SprSheet().RectFill(60, 30, 70, 40, 7)
23+
24+
// 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+
for x := 0; x < len(dst); x++ {
27+
dst[x] += pi.DrawPalette[src[x]] + 1
28+
}
29+
})
30+
31+
// update each line in a loop:
32+
pi.Scr().Foreach(10, 10, 16, 16, func(x, y int, line []byte) {
33+
for i := 0; i < len(line); i++ {
34+
line[i] = byte(i)
35+
}
36+
})
37+
38+
ebitengine.MustRun()
39+
}

examples/pixmap/sprite-sheet.png

268 Bytes
Loading

0 commit comments

Comments
 (0)