go:pixel stuff
This commit is contained in:
parent
505a0e7118
commit
1a4517a49f
68
go/pixel/draw-sprite/main.go
Normal file
68
go/pixel/draw-sprite/main.go
Normal file
@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
_ "image/png"
|
||||
"os"
|
||||
|
||||
"github.com/faiface/pixel"
|
||||
"github.com/faiface/pixel/pixelgl"
|
||||
"golang.org/x/image/colornames"
|
||||
)
|
||||
|
||||
// Picture Loader helper
|
||||
// If we wanted to decode more types, we'd have to import them
|
||||
// Such as "image/jpeg"
|
||||
|
||||
func loadPicture(path string) (pixel.Picture, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
img, _, err := image.Decode(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pixel.PictureDataFromImage(img), nil
|
||||
}
|
||||
|
||||
func run() {
|
||||
cfg := pixelgl.WindowConfig{
|
||||
Title: "Pixel Rocks!",
|
||||
Bounds: pixel.R(0, 0, 1024, 768),
|
||||
VSync: true,
|
||||
}
|
||||
|
||||
win, err := pixelgl.NewWindow(cfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
pic, err := loadPicture("vim-go.png")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//We choose pic.Bounds() because we want
|
||||
//The whole thing
|
||||
sprite := pixel.NewSprite(pic, pic.Bounds())
|
||||
|
||||
win.Clear(colornames.Black)
|
||||
// At this point just know that pixel.IM
|
||||
// Means "no spacial transformations"
|
||||
//sprite.Draw(win, pixel.IM)
|
||||
|
||||
//We'll give it some changes this time
|
||||
sprite.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
|
||||
|
||||
for !win.Closed() {
|
||||
win.Update()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
pixelgl.Run(run)
|
||||
}
|
BIN
go/pixel/draw-sprite/vim-go.png
Normal file
BIN
go/pixel/draw-sprite/vim-go.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
36
go/pixel/window/main.go
Normal file
36
go/pixel/window/main.go
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/faiface/pixel"
|
||||
"github.com/faiface/pixel/pixelgl"
|
||||
"golang.org/x/image/colornames"
|
||||
)
|
||||
|
||||
func run() {
|
||||
// All code goes here!
|
||||
// Note: There are more options, but they have defaults
|
||||
cfg := pixelgl.WindowConfig{
|
||||
Title: "Pixel Rocks!",
|
||||
//rectangle, lower left xy, upper right xy
|
||||
Bounds: pixel.R(0, 0, 1024, 768),
|
||||
VSync: true,
|
||||
}
|
||||
|
||||
//Takes a config and returns a window or err
|
||||
win, err := pixelgl.NewWindow(cfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
win.Clear(colornames.Skyblue)
|
||||
|
||||
// Main loop
|
||||
for !win.Closed() {
|
||||
win.Update()
|
||||
}
|
||||
}
|
||||
|
||||
/* Nothing else happens in main*/
|
||||
func main() {
|
||||
pixelgl.Run(run)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user