Skip to content

Commit

Permalink
Update the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Jan 4, 2024
1 parent c1d1657 commit 54731e2
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 136 deletions.
15 changes: 6 additions & 9 deletions cmd/blending/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"math/rand"
"os"
"runtime"
"time"

"github.com/veandco/go-sdl2/sdl"
"github.com/xyproto/pixelpusher"
pp "github.com/xyproto/pixelpusher"
)

const (
Expand Down Expand Up @@ -45,11 +44,11 @@ func DrawAll(pixels []uint32, cores int) {
var opaqueness uint8 = 1 // Almost completely transparent

// Draw a triangle, concurrently
pixelpusher.Triangle(cores, pixels, rw(), rh(), rw(), rh(), rw(), rh(), color.RGBA{rb(), rb(), rb(), opaqueness}, pitch)
pp.Triangle(cores, pixels, rw(), rh(), rw(), rh(), rw(), rh(), color.RGBA{rb(), rb(), rb(), opaqueness}, pitch)

// Draw a line and a red pixel, without caring about which order they appear in, or if they will complete before the next frame is drawn
go pixelpusher.Line(pixels, rw(), rh(), rw(), rh(), color.RGBA{rb(), rb(), rb(), opaqueness}, pitch)
go pixelpusher.Pixel(pixels, rw(), rh(), color.RGBA{0xff, 0, 0, opaqueness}, pitch)
go pp.Line(pixels, rw(), rh(), rw(), rh(), color.RGBA{rb(), rb(), rb(), opaqueness}, pitch)
go pp.Pixel(pixels, rw(), rh(), color.RGBA{0xff, 0, 0, opaqueness}, pitch)
}

func run() int {
Expand Down Expand Up @@ -89,8 +88,6 @@ func run() int {

texture.SetBlendMode(sdl.BLENDMODE_ADD)

rand.Seed(time.Now().UnixNano())

var (
pixels = make([]uint32, width*height)
cores = runtime.NumCPU()
Expand Down Expand Up @@ -136,7 +133,7 @@ func run() int {
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
pixelpusher.ToggleFullscreen(window)
pp.ToggleFullscreen(window)
// Clear pixels at fullscreen toggle, because of blend mode and no double buffering
renderer.Clear()
for i := range pixels {
Expand All @@ -155,7 +152,7 @@ func run() int {
fallthrough
case sdl.K_F12:
// screenshot
pixelpusher.Screenshot(renderer, "screenshot.png", true)
pp.Screenshot(renderer, "screenshot.png", true)
}
}
}
Expand Down
35 changes: 16 additions & 19 deletions cmd/butterfly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"math/rand"
"os"
"runtime"
"time"

"github.com/veandco/go-sdl2/sdl"
"github.com/xyproto/pixelpusher"
pp "github.com/xyproto/pixelpusher"
)

const (
Expand Down Expand Up @@ -90,16 +89,16 @@ func convolution(time float32, pixels []uint32, width, height, pitch int32, enr
above = pixels[(y-one2)*pitch+x*two1]
}

lr, lg, lb, _ := pixelpusher.ColorValueToRGBA(left)
rr, rg, rb, _ := pixelpusher.ColorValueToRGBA(right)
tr, tg, tb, _ := pixelpusher.ColorValueToRGBA(this)
ar, ag, ab, _ := pixelpusher.ColorValueToRGBA(above)
lr, lg, lb, _ := pp.ColorValueToRGBA(left)
rr, rg, rb, _ := pp.ColorValueToRGBA(right)
tr, tg, tb, _ := pp.ColorValueToRGBA(this)
ar, ag, ab, _ := pp.ColorValueToRGBA(above)

averageR := uint8(float32(lr+rr+tr+ar) / float32(4.55-stime))
averageG := uint8(float32(lg+rg+tg+ag) / float32(4.55-stime))
averageB := uint8(float32(lb+rb+tb+ab) / float32(4.55-stime))

pixels[y*pitch+x] = pixelpusher.RGBAToColorValue(averageR, averageG, averageB, 0xff)
pixels[y*pitch+x] = pp.RGBAToColorValue(averageR, averageG, averageB, 0xff)
}
}
// Top row
Expand Down Expand Up @@ -146,11 +145,11 @@ func clamp(v float32, max uint8) uint8 {
// Every pixel that is light, decrease the intensity
func darken(pixels []uint32) {
for i := range pixels {
r, g, b, _ := pixelpusher.ColorValueToRGBA(pixels[i])
r, g, b, _ := pp.ColorValueToRGBA(pixels[i])
if r < 20 && g < 20 && b < 20 {
continue
}
pixels[i] = pixelpusher.RGBAToColorValue(clamp(float32(r)*0.99, 255), clamp(float32(g)*0.99, 255), clamp(float32(b)*0.99, 255), 255)
pixels[i] = pp.RGBAToColorValue(clamp(float32(r)*0.99, 255), clamp(float32(g)*0.99, 255), clamp(float32(b)*0.99, 255), 255)
}
}

Expand All @@ -164,24 +163,24 @@ func TriangleDance(time float32, pixels []uint32, width, height, pitch int32, co

// The function is responsible for clearing the pixels,
// it might want to reuse the pixels from the last time (flame effect)
//pixelpusher.FastClear(pixels, bgColorValue)
//pp.FastClear(pixels, bgColorValue)

// Find a suitable placement and color
var x int32
if xdirection > 0 {
x = pixelpusher.Clamp(int32(float32(width)*time), size, width-size)
x = pp.Clamp(int32(float32(width)*time), size, width-size)
} else if xdirection == 0 {
x = int32(width / 2)
} else {
x = pixelpusher.Clamp(int32(float32(width)*(1.0-time)), size, width-size)
x = pp.Clamp(int32(float32(width)*(1.0-time)), size, width-size)
}
var y int32
if ydirection > 0 {
y = pixelpusher.Clamp(int32(float32(height)*time), size, height-size)
y = pp.Clamp(int32(float32(height)*time), size, height-size)
} else if ydirection == 0 {
y = int32(height / 2)
} else {
y = pixelpusher.Clamp(int32(float32(height)*(1.0-time)), size, height-size)
y = pp.Clamp(int32(float32(height)*(1.0-time)), size, height-size)
}

// Make the center triangle red
Expand All @@ -199,7 +198,7 @@ func TriangleDance(time float32, pixels []uint32, width, height, pitch int32, co
x3 := x + rand.Int31n(int32(size)) - int32(size/2)
y3 := y + rand.Int31n(int32(size)) - int32(size/2)

pixelpusher.Triangle(cores, pixels, x1, y1, x2, y2, x3, y3, c, pitch)
pp.Triangle(cores, pixels, x1, y1, x2, y2, x3, y3, c, pitch)

return 0.0
}
Expand Down Expand Up @@ -241,8 +240,6 @@ func run() int {

//texture.SetBlendMode(sdl.BLENDMODE_BLEND)

rand.Seed(time.Now().UnixNano())

var (
pixels = make([]uint32, width*height)
cores = runtime.NumCPU()
Expand Down Expand Up @@ -330,7 +327,7 @@ func run() int {
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
pixelpusher.ToggleFullscreen(window)
pp.ToggleFullscreen(window)
case sdl.K_p, sdl.K_SPACE:
// pause toggle
pause = !pause
Expand All @@ -344,7 +341,7 @@ func run() int {
fallthrough
case sdl.K_F12:
// screenshot
pixelpusher.Screenshot(renderer, "screenshot.png", true)
pp.Screenshot(renderer, "screenshot.png", true)
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions cmd/cube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"math/rand"
"os"
"runtime"
"time"

"github.com/fogleman/fauxgl"
"github.com/veandco/go-sdl2/sdl"
"github.com/xyproto/pixelpusher"
pp "github.com/xyproto/pixelpusher"
)

const (
Expand Down Expand Up @@ -46,7 +45,7 @@ var (
// by launching the same number of goroutines.
func DrawAll(pixels []uint32, cores int, mesh *fauxgl.Mesh, cameraAngle float32, meshHexColor string) {
// Draw a triangle, concurrently
pixelpusher.WireTriangle(cores, pixels, rw(), rh(), rw(), rh(), rw(), rh(), color.RGBA{rb(), rb(), rb(), opaque}, pitch)
pp.WireTriangle(cores, pixels, rw(), rh(), rw(), rh(), rw(), rh(), color.RGBA{rb(), rb(), rb(), opaque}, pitch)

// Draw a 3D object on top
DrawMesh(pixels, pitch, mesh, cameraAngle, meshHexColor)
Expand Down Expand Up @@ -84,8 +83,6 @@ func run() int {

//texture.SetBlendMode(sdl.BLENDMODE_BLEND) // sdl.BLENDMODE_ADD is also possible

rand.Seed(time.Now().UnixNano())

mesh, err := LoadMeshOBJ("bevelcube.obj")
if err != nil {
panic(err)
Expand Down Expand Up @@ -143,7 +140,7 @@ func run() int {
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
pixelpusher.ToggleFullscreen(window)
pp.ToggleFullscreen(window)
case sdl.K_SPACE, sdl.K_p:
pause = !pause
case sdl.K_s:
Expand All @@ -156,7 +153,7 @@ func run() int {
fallthrough
case sdl.K_F12:
// save the image
pixelpusher.SavePixelsToPNG(pixels, pitch, "screenshot.png", true)
pp.SavePixelsToPNG(pixels, pitch, "screenshot.png", true)
}
}
}
Expand Down
75 changes: 36 additions & 39 deletions cmd/glitchduck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"math/rand"
"os"
"runtime"
"time"

"github.com/veandco/go-sdl2/sdl"
"github.com/xyproto/pixelpusher"
pp "github.com/xyproto/pixelpusher"
)

const (
Expand Down Expand Up @@ -58,52 +57,52 @@ func Convolution(time float32, pixels []uint32, width, height, pitch int32, enr
switch enr {
case 0:
// "snow patterns"
left = pixelpusher.GetWrap(pixels, y*pitch+x-1, size)
right = pixelpusher.GetWrap(pixels, y*pitch+x+1, size)
this = pixelpusher.GetWrap(pixels, y*pitch+x, size)
above = pixelpusher.GetWrap(pixels, (y+1)*pitch+x, size)
left = pp.GetWrap(pixels, y*pitch+x-1, size)
right = pp.GetWrap(pixels, y*pitch+x+1, size)
this = pp.GetWrap(pixels, y*pitch+x, size)
above = pp.GetWrap(pixels, (y+1)*pitch+x, size)
case 1:
// "highway"
left = pixelpusher.GetWrap(pixels, (y-1)*pitch+x-1, size)
right = pixelpusher.GetWrap(pixels, (y-1)*pitch+x+1, size)
this = pixelpusher.GetWrap(pixels, y*pitch+x, size)
above = pixelpusher.GetWrap(pixels, (y-1)*pitch+x, size)
left = pp.GetWrap(pixels, (y-1)*pitch+x-1, size)
right = pp.GetWrap(pixels, (y-1)*pitch+x+1, size)
this = pp.GetWrap(pixels, y*pitch+x, size)
above = pp.GetWrap(pixels, (y-1)*pitch+x, size)
case 2:
// "dither highway"
left = pixelpusher.GetWrap(pixels, (y-1)*pitch+x-1, size)
right = pixelpusher.GetWrap(pixels, (y-1)*pitch+x+1, size)
this = pixelpusher.GetWrap(pixels, (y-1)*pitch+(x-1), size)
above = pixelpusher.GetWrap(pixels, (y+1)*pitch+(x+1), size)
left = pp.GetWrap(pixels, (y-1)*pitch+x-1, size)
right = pp.GetWrap(pixels, (y-1)*pitch+x+1, size)
this = pp.GetWrap(pixels, (y-1)*pitch+(x-1), size)
above = pp.GetWrap(pixels, (y+1)*pitch+(x+1), size)
case 3:
// "butterfly"
left = pixelpusher.GetWrap(pixels, y*pitch+(x-two1), size)
right = pixelpusher.GetWrap(pixels, y*pitch+(x+two1), size)
this = pixelpusher.GetWrap(pixels, y*pitch+x*two2, size)
above = pixelpusher.GetWrap(pixels, (y-two1)*pitch+x*two2, size)
left = pp.GetWrap(pixels, y*pitch+(x-two1), size)
right = pp.GetWrap(pixels, y*pitch+(x+two1), size)
this = pp.GetWrap(pixels, y*pitch+x*two2, size)
above = pp.GetWrap(pixels, (y-two1)*pitch+x*two2, size)
case 4:
// ?
left = pixelpusher.GetWrap(pixels, y*pitch+(x-two2), size)
right = pixelpusher.GetWrap(pixels, y*pitch+(x+two1), size)
this = pixelpusher.GetWrap(pixels, y*pitch+int32(float32(x)*stime), size)
above = pixelpusher.GetWrap(pixels, (y-two2)*pitch+int32(float32(x)*stime), size)
left = pp.GetWrap(pixels, y*pitch+(x-two2), size)
right = pp.GetWrap(pixels, y*pitch+(x+two1), size)
this = pp.GetWrap(pixels, y*pitch+int32(float32(x)*stime), size)
above = pp.GetWrap(pixels, (y-two2)*pitch+int32(float32(x)*stime), size)
case 5:
// "castle"
left = pixelpusher.GetWrap(pixels, y*pitch+(x-one1), size)
right = pixelpusher.GetWrap(pixels, y*pitch+(x+one1), size)
this = pixelpusher.GetWrap(pixels, y*pitch+x*two1, size)
above = pixelpusher.GetWrap(pixels, (y-one2)*pitch+x*two1, size)
left = pp.GetWrap(pixels, y*pitch+(x-one1), size)
right = pp.GetWrap(pixels, y*pitch+(x+one1), size)
this = pp.GetWrap(pixels, y*pitch+x*two1, size)
above = pp.GetWrap(pixels, (y-one2)*pitch+x*two1, size)
}

lr, lg, lb, _ := pixelpusher.ColorValueToRGBA(left)
rr, rg, rb, _ := pixelpusher.ColorValueToRGBA(right)
tr, tg, tb, _ := pixelpusher.ColorValueToRGBA(this)
ar, ag, ab, _ := pixelpusher.ColorValueToRGBA(above)
lr, lg, lb, _ := pp.ColorValueToRGBA(left)
rr, rg, rb, _ := pp.ColorValueToRGBA(right)
tr, tg, tb, _ := pp.ColorValueToRGBA(this)
ar, ag, ab, _ := pp.ColorValueToRGBA(above)

averageR := uint8(float32(lr+rr+tr+ar) / float32(4.8-stime))
averageG := uint8(float32(lg+rg+tg+ag) / float32(4.8-stime))
averageB := uint8(float32(lb+rb+tb+ab) / float32(4.8-stime))

pixelpusher.SetWrap(pixels, y*pitch+x, width*height, pixelpusher.RGBAToColorValue(averageR, averageG, averageB, 0xff))
pp.SetWrap(pixels, y*pitch+x, width*height, pp.RGBAToColorValue(averageR, averageG, averageB, 0xff))
}
}
}
Expand Down Expand Up @@ -140,8 +139,6 @@ func run() int {

//texture.SetBlendMode(sdl.BLENDMODE_BLEND) // sdl.BLENDMODE_ADD is also possible

rand.Seed(time.Now().UnixNano())

mesh, err := LoadMeshOBJ("duck.obj")
if err != nil {
panic(err)
Expand All @@ -165,12 +162,12 @@ func run() int {

if !pause {
// Clear the pixel buffer
pixelpusher.FastClear(pixels, 0xffffffff)
pp.FastClear(pixels, 0xffffffff)

// Draw a triangle, concurrently
if effect {
yellow := rb()
pixelpusher.Triangle(cores, pixels, rw(), rh(), rw(), rh(), rw(), rh(), color.RGBA{yellow, yellow, 0, 0x80}, pitch)
pp.Triangle(cores, pixels, rw(), rh(), rw(), rh(), rw(), rh(), color.RGBA{yellow, yellow, 0, 0x80}, pitch)
}

// Draw a 3D object
Expand All @@ -183,7 +180,7 @@ func run() int {
Convolution(0.9, pixelCopy, width, height, pitch, enr)

// Stretch contrast
pixelpusher.StretchContrast(cores, pixelCopy, pitch, 0.09)
pp.StretchContrast(cores, pixelCopy, pitch, 0.09)

// Draw pixel buffer to screen
texture.UpdateRGBA(nil, pixelCopy, width)
Expand All @@ -200,7 +197,7 @@ func run() int {
//}

// Clear the pixel buffer
//pixelpusher.FastClear(pixels, 0)
//pp.FastClear(pixels, 0)

renderer.Copy(texture, nil, nil)
renderer.Present()
Expand Down Expand Up @@ -231,7 +228,7 @@ func run() int {
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
pixelpusher.ToggleFullscreen(window)
pp.ToggleFullscreen(window)
case sdl.K_SPACE:
effect = !effect
case sdl.K_p:
Expand All @@ -246,7 +243,7 @@ func run() int {
fallthrough
case sdl.K_F12:
// save the image
pixelpusher.SavePixelsToPNG(pixels, pitch, "screenshot.png", true)
pp.SavePixelsToPNG(pixels, pitch, "screenshot.png", true)
}
}
}
Expand Down
Loading

0 comments on commit 54731e2

Please sign in to comment.