Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Jan 4, 2024
1 parent 408dc41 commit 8d30ad8
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 167 deletions.
5 changes: 2 additions & 3 deletions cmd/blending/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

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

const (
Expand Down Expand Up @@ -137,7 +136,7 @@ func run() int {
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
sdl2utils.ToggleFullscreen(window)
pixelpusher.ToggleFullscreen(window)
// Clear pixels at fullscreen toggle, because of blend mode and no double buffering
renderer.Clear()
for i := range pixels {
Expand All @@ -156,7 +155,7 @@ func run() int {
fallthrough
case sdl.K_F12:
// screenshot
sdl2utils.Screenshot(renderer, "screenshot.png", true)
pixelpusher.Screenshot(renderer, "screenshot.png", true)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/butterfly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

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

const (
Expand Down Expand Up @@ -331,7 +330,7 @@ func run() int {
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
sdl2utils.ToggleFullscreen(window)
pixelpusher.ToggleFullscreen(window)
case sdl.K_p, sdl.K_SPACE:
// pause toggle
pause = !pause
Expand All @@ -345,7 +344,7 @@ func run() int {
fallthrough
case sdl.K_F12:
// screenshot
sdl2utils.Screenshot(renderer, "screenshot.png", true)
pixelpusher.Screenshot(renderer, "screenshot.png", true)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/cube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/fogleman/fauxgl"
"github.com/veandco/go-sdl2/sdl"
"github.com/xyproto/pixelpusher"
"github.com/xyproto/sdl2utils"
)

const (
Expand Down Expand Up @@ -144,7 +143,7 @@ func run() int {
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
sdl2utils.ToggleFullscreen(window)
pixelpusher.ToggleFullscreen(window)
case sdl.K_SPACE, sdl.K_p:
pause = !pause
case sdl.K_s:
Expand Down
3 changes: 1 addition & 2 deletions cmd/glitchduck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

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

const (
Expand Down Expand Up @@ -232,7 +231,7 @@ func run() int {
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
sdl2utils.ToggleFullscreen(window)
pixelpusher.ToggleFullscreen(window)
case sdl.K_SPACE:
effect = !effect
case sdl.K_p:
Expand Down
5 changes: 2 additions & 3 deletions cmd/simple/main.go → cmd/multicore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

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

const (
Expand Down Expand Up @@ -137,7 +136,7 @@ func run() int {
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
sdl2utils.ToggleFullscreen(window)
pixelpusher.ToggleFullscreen(window)
case sdl.K_SPACE, sdl.K_p:
pause = !pause
case sdl.K_s:
Expand All @@ -150,7 +149,7 @@ func run() int {
fallthrough
case sdl.K_F12:
// screenshot
sdl2utils.Screenshot(renderer, "screenshot.png", true)
pixelpusher.Screenshot(renderer, "screenshot.png", true)
}
}
}
Expand Down
154 changes: 5 additions & 149 deletions cmd/redpixel/main.go
Original file line number Diff line number Diff line change
@@ -1,159 +1,15 @@
package main

import (
"fmt"
"image/color"
"math/rand"
"os"
"time"

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

const (
// Size of "worldspace pixels", measured in "screenspace pixels"
pixelscale = 4

// The resolution (worldspace)
width = 320
height = 200

// The width of the pixel buffer, used when calculating where to place pixels (y*pitch+x)
pitch = width

// Target framerate
frameRate = 60

// Alpha value for opaque colors
opaque = 255
)

func run() int {

sdl.Init(uint32(sdl.INIT_VIDEO))
defer sdl.Quit()

var (
window *sdl.Window
renderer *sdl.Renderer
err error
)

window, err = sdl.CreateWindow("Red Pixel", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, int32(width*pixelscale), int32(height*pixelscale), sdl.WINDOW_SHOWN)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
return 1
}
defer window.Destroy()

renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
return 1
}
defer renderer.Destroy()

// Fill the render buffer with black
renderer.SetDrawColor(0, 0, 0, opaque)
renderer.Clear()

texture, err := renderer.CreateTexture(sdl.PIXELFORMAT_ARGB8888, sdl.TEXTUREACCESS_STREAMING, width, height)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err)
return 1
}

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

var (
pixels = make([]uint32, width*height)
event sdl.Event

pause, recording, quit bool

loopCounter uint64
frameCounter uint64
)

// Innerloop
for !quit {

if !pause {

// Draw a red pixel at 0,0
pixelpusher.Pixel(pixels, 0, 0, color.RGBA{255, 0, 0, 255}, pitch)

// Draw a red pixel at 0,0
//pixels[0] = 0xffff0000

texture.UpdateRGBA(nil, pixels, pitch)

renderer.Copy(texture, nil, nil)
renderer.Present()

if recording {
filename := fmt.Sprintf("frame%05d.png", frameCounter)
pixelpusher.SavePixelsToPNG(pixels, pitch, filename, true)
frameCounter++
}

}

// Check for events
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
quit = true
case *sdl.KeyboardEvent:
ke := event.(*sdl.KeyboardEvent)
if ke.Type == sdl.KEYDOWN {
ks := ke.Keysym
switch ks.Sym {
case sdl.K_ESCAPE:
quit = true
case sdl.K_q:
quit = true
case sdl.K_RETURN:
altHeldDown := ks.Mod == sdl.KMOD_LALT || ks.Mod == sdl.KMOD_RALT
if !altHeldDown {
// alt+enter is not pressed
break
}
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
sdl2utils.ToggleFullscreen(window)
case sdl.K_p, sdl.K_SPACE:
// pause toggle
pause = !pause
case sdl.K_s:
ctrlHeldDown := ks.Mod == sdl.KMOD_LCTRL || ks.Mod == sdl.KMOD_RCTRL
if !ctrlHeldDown {
// ctrl+s is not pressed
break
}
// ctrl+s is pressed
fallthrough
case sdl.K_F12:
// screenshot
sdl2utils.Screenshot(renderer, "screenshot.png", true)
case sdl.K_r:
// recording
recording = !recording
frameCounter = 0
}
}
}
}
sdl.Delay(1000 / frameRate)
loopCounter++
}
return 0
func onDraw(gfx *pixelpusher.Config) error {
// x, y, r, g, b
return pixelpusher.Plot(gfx, 0, 0, 255, 0, 0)
}

func main() {
// This is to allow the deferred functions in run() to kick in at exit
os.Exit(run())
gfx := pixelpusher.New("Red Pixel")
gfx.Run(onDraw, nil, nil, nil)
}
Binary file removed cmd/simple/simple
Binary file not shown.
34 changes: 34 additions & 0 deletions cmd/simpledraw/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"errors"

"github.com/xyproto/pixelpusher"
)

var x, y = 160, 100

func onDraw(gfx *pixelpusher.Config) error {
return pixelpusher.Plot(gfx, x, y, 255, 0, 0)
}

func onPress(left, right, up, down, space, enter, esc bool) error {
if up {
y--
} else if down {
y++
}
if left {
x--
} else if right {
x++
}
if esc {
return errors.New("quit")
}
return nil
}

func main() {
pixelpusher.New("Simple Draw").Run(onDraw, onPress, nil, nil)
}
5 changes: 2 additions & 3 deletions cmd/strobe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/veandco/go-sdl2/sdl"
"github.com/xyproto/pf"
"github.com/xyproto/pixelpusher"
"github.com/xyproto/sdl2utils"
)

const (
Expand Down Expand Up @@ -335,7 +334,7 @@ func run() int {
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
sdl2utils.ToggleFullscreen(window)
pixelpusher.ToggleFullscreen(window)
case sdl.K_p:
// pause toggle
pause = !pause
Expand All @@ -352,7 +351,7 @@ func run() int {
glitch = !glitch
case sdl.K_F12:
// screenshot
sdl2utils.Screenshot(renderer, "screenshot.png", true)
pixelpusher.Screenshot(renderer, "screenshot.png", true)
case sdl.K_r:
// recording
recording = !recording
Expand Down
3 changes: 1 addition & 2 deletions cmd/wiretri/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

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

const (
Expand Down Expand Up @@ -125,7 +124,7 @@ func run() int {
// alt+enter is pressed
fallthrough
case sdl.K_f, sdl.K_F11:
sdl2utils.ToggleFullscreen(window)
pixelpusher.ToggleFullscreen(window)
case sdl.K_SPACE:
nodelay = !nodelay
case sdl.K_p:
Expand Down

0 comments on commit 8d30ad8

Please sign in to comment.