2018-01-11 07:52:23 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "image/png"
|
|
|
|
|
|
|
|
"github.com/faiface/pixel"
|
|
|
|
"github.com/faiface/pixel/pixelgl"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
windowWidth = 600
|
|
|
|
windowHeight = 450
|
|
|
|
foregroundHeight = 149
|
|
|
|
// This is the scrolling speed (pixels per second)
|
|
|
|
backgroundSpeed = 60
|
|
|
|
foregroundSpeed = 120
|
|
|
|
)
|
|
|
|
|
|
|
|
func run() {
|
|
|
|
cfg := pixelgl.WindowConfig{
|
|
|
|
Title: "Scrolling background demo",
|
|
|
|
Bounds: pixel.R(0, 0, windowWidth, windowHeight),
|
|
|
|
VSync: true,
|
|
|
|
}
|
|
|
|
win, err := pixelgl.NewWindow(cfg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pic must have double the width of the window, as it will scroll to the left
|
|
|
|
picBackground, err := loadPicture("background.png")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
picForeground, err := loadPicture("foreground.png")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-01-11 09:55:20 -06:00
|
|
|
background := newScrollingBackground(picBackground, windowWidth, windowHeight, windowWidth)
|
|
|
|
foreground := newScrollingBackground(picForeground, windowWidth, foregroundHeight, windowWidth)
|
2018-01-11 07:52:23 -06:00
|
|
|
|
|
|
|
last := time.Now()
|
|
|
|
for !win.Closed() {
|
|
|
|
dt := time.Since(last).Seconds()
|
|
|
|
last = time.Now()
|
2018-01-11 09:55:20 -06:00
|
|
|
background.update(win, backgroundSpeed, dt)
|
|
|
|
foreground.update(win, foregroundSpeed, dt)
|
2018-01-11 07:52:23 -06:00
|
|
|
win.Update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
pixelgl.Run(run)
|
|
|
|
}
|