2018-01-15 21:41:59 -07:00
|
|
|
// main
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-01-15 23:20:34 -07:00
|
|
|
"image"
|
|
|
|
_ "image/png"
|
|
|
|
"os"
|
2018-01-16 20:27:09 -07:00
|
|
|
"time"
|
2018-01-15 23:20:34 -07:00
|
|
|
|
2018-01-15 22:42:11 -07:00
|
|
|
"github.com/faiface/pixel"
|
|
|
|
"github.com/faiface/pixel/pixelgl"
|
2018-01-15 22:50:18 -07:00
|
|
|
"golang.org/x/image/colornames"
|
2018-01-15 21:41:59 -07:00
|
|
|
)
|
|
|
|
|
2018-01-16 21:10:29 -07:00
|
|
|
//open file from the system
|
2018-01-15 23:20:34 -07:00
|
|
|
func loadPicture(path string) (pixel.Picture, error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-16 21:10:29 -07:00
|
|
|
//user 'defer' to close the file later on
|
2018-01-15 23:20:34 -07:00
|
|
|
defer file.Close()
|
2018-01-16 21:10:29 -07:00
|
|
|
|
|
|
|
//decode the file to find it's type
|
2018-01-15 23:20:34 -07:00
|
|
|
img, _, err := image.Decode(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pixel.PictureDataFromImage(img), nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 21:10:29 -07:00
|
|
|
//Pixel's main function (because GO forced them to do it this way)
|
2018-01-15 22:42:11 -07:00
|
|
|
func run() {
|
2018-01-16 21:10:29 -07:00
|
|
|
//set the Window parameters
|
2018-01-15 22:42:11 -07:00
|
|
|
cfg := pixelgl.WindowConfig{
|
|
|
|
Title: "Pixel Rocks!",
|
|
|
|
Bounds: pixel.R(0, 0, 1024, 768),
|
2018-01-15 22:50:18 -07:00
|
|
|
VSync: true,
|
2018-01-15 22:42:11 -07:00
|
|
|
}
|
|
|
|
|
2018-01-16 21:10:29 -07:00
|
|
|
//create a window and pass the parameters
|
2018-01-15 22:42:11 -07:00
|
|
|
win, err := pixelgl.NewWindow(cfg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-01-16 21:10:29 -07:00
|
|
|
//smooth the edges of the image (antialiasing)
|
2018-01-16 20:27:09 -07:00
|
|
|
win.SetSmooth(true)
|
2018-01-16 21:10:29 -07:00
|
|
|
|
|
|
|
//load a specific image and catch any errors
|
2018-01-15 23:20:34 -07:00
|
|
|
pic, err := loadPicture("hiking.png")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-01-16 21:10:29 -07:00
|
|
|
//create a sprite using the loaded image
|
2018-01-15 23:20:34 -07:00
|
|
|
sprite := pixel.NewSprite(pic, pic.Bounds())
|
|
|
|
|
2018-01-16 21:10:29 -07:00
|
|
|
//set the angle of the image
|
2018-01-16 20:27:09 -07:00
|
|
|
angle := 0.0
|
2018-01-16 21:10:29 -07:00
|
|
|
|
|
|
|
//set the time to compare later
|
2018-01-16 20:27:09 -07:00
|
|
|
last := time.Now()
|
2018-01-15 22:50:18 -07:00
|
|
|
|
2018-01-16 21:10:29 -07:00
|
|
|
//loop until window is closed by "X" button
|
2018-01-15 22:42:11 -07:00
|
|
|
for !win.Closed() {
|
2018-01-16 21:10:29 -07:00
|
|
|
//compare the last time to delta time
|
2018-01-16 20:27:09 -07:00
|
|
|
dt := time.Since(last).Seconds()
|
|
|
|
last = time.Now()
|
|
|
|
|
2018-01-16 21:10:29 -07:00
|
|
|
//set the angle according to delta time
|
2018-01-16 20:27:09 -07:00
|
|
|
angle += 3 * dt
|
|
|
|
|
2018-01-16 21:10:29 -07:00
|
|
|
//refresh screen and set background color
|
2018-01-16 20:27:09 -07:00
|
|
|
win.Clear(colornames.Firebrick)
|
|
|
|
|
2018-01-16 21:10:29 -07:00
|
|
|
//set the matrix and location of spirte in window
|
2018-01-16 20:27:09 -07:00
|
|
|
mat := pixel.IM
|
|
|
|
mat = mat.Rotated(pixel.ZV, angle)
|
|
|
|
mat = mat.Moved(win.Bounds().Center())
|
|
|
|
sprite.Draw(win, mat)
|
2018-01-16 21:10:29 -07:00
|
|
|
|
|
|
|
//update the window
|
2018-01-15 22:42:11 -07:00
|
|
|
win.Update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-15 21:41:59 -07:00
|
|
|
func main() {
|
2018-01-15 22:42:11 -07:00
|
|
|
pixelgl.Run(run)
|
2018-01-15 21:41:59 -07:00
|
|
|
}
|