add comments

This commit is contained in:
mollusk 2018-01-16 21:10:29 -07:00
parent 21406a1bfb
commit 9136672799

21
main.go
View File

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