-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
50 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters