package main import ( "fmt" "image" "math" "os" "time" _ "image/png" "gitbutter.pw/zolfite/project-rpg/gameobjects" "github.com/faiface/pixel" "github.com/faiface/pixel/pixelgl" "golang.org/x/image/colornames" ) 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: "Project RPG", Bounds: pixel.R(0, 0, 1024, 768), VSync: true, } win, err := pixelgl.NewWindow(cfg) if err != nil { panic(err) } spritesheet, err := loadPicture("sprites.png") if err != nil { panic(err) } //batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet) background, err := loadPicture("background.png") if err != nil { panic(err) } bg := pixel.NewSprite(background, background.Bounds()) //batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet) var ( camPos = pixel.ZV camZoom = 0.3 camZoomSpeed = 1.2 ) var ( frames = 0 second = time.Tick(time.Second) ) player := gameobjects.NewPlayer(win, spritesheet) var fps int64 = 60 timePerFrame := 1000000000 / fps var lastTime int64 = time.Now().UnixNano() var now int64 var dt int64 // Game Loop for !win.Closed() { now = time.Now().UnixNano() dt += (now - lastTime) lastTime = time.Now().UnixNano() //Execute a frame. if dt >= timePerFrame { // *** Update begins *** // cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos)) win.SetMatrix(cam) player.Update(win, dt) camPos.X = player.PlayerVec.X camPos.Y = player.PlayerVec.Y camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y) // *** Update Ends *** // // *** Render begin *** // win.Clear(colornames.Forestgreen) bg.Draw(win, pixel.IM.Moved(win.Bounds().Center())) player.Render(win) //batch.Draw(win) win.Update() // *** Render End *** // dt = 0 frames++ select { case <-second: win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames)) frames = 0 default: } } } } func main() { pixelgl.Run(run) }