Skip to content

Commit

Permalink
Add examples for file loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Ray committed Aug 30, 2024
1 parent 688fdc8 commit 214000d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
53 changes: 53 additions & 0 deletions core/loadingpictures/explicitimport/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"image/jpeg"
"image/png"

"github.com/gopxl/pixel/v2"
"github.com/gopxl/pixel/v2/backends/opengl"
"golang.org/x/image/colornames"
)

func main() {
opengl.Run(run)
}

func run() {
cfg := opengl.WindowConfig{
Title: "Image Loading",
Bounds: pixel.R(0, 0, 1024, 768),
}

win, err := opengl.NewWindow(cfg)
if err != nil {
panic(err)
}
defer win.Destroy()

p1, err := pixel.PictureDataFromFile("../hiking.png", png.Decode)
if err != nil {
panic(err)
}

p2, err := pixel.PictureDataFromFile("../hiking.jpeg", jpeg.Decode)
if err != nil {
panic(err)
}

spr1 := pixel.NewSprite(p1, p1.Bounds())
spr2 := pixel.NewSprite(p2, p2.Bounds())

for !win.Closed() {
if win.JustReleased(pixel.KeyEscape) {
win.SetClosed(true)
}

win.Clear(colornames.Skyblue)

spr1.Draw(win, pixel.IM.Moved(win.Bounds().Center().Add(pixel.V(0, 100))))
spr2.Draw(win, pixel.IM.Moved(win.Bounds().Center().Add(pixel.V(0, -100))))

win.Update()
}
}
Binary file added core/loadingpictures/hiking.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added core/loadingpictures/hiking.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions core/loadingpictures/implicitimport/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
// Since we're using a nil decoder, we need to import these to register the formats.
_ "image/jpeg"
_ "image/png"

"github.com/gopxl/pixel/v2"
"github.com/gopxl/pixel/v2/backends/opengl"
"golang.org/x/image/colornames"
)

func main() {
opengl.Run(run)
}

func run() {
cfg := opengl.WindowConfig{
Title: "Image Loading",
Bounds: pixel.R(0, 0, 1024, 768),
}

win, err := opengl.NewWindow(cfg)
if err != nil {
panic(err)
}
defer win.Destroy()

p1, err := pixel.PictureDataFromFile("../hiking.png", nil)
if err != nil {
panic(err)
}

p2, err := pixel.PictureDataFromFile("../hiking.jpeg", nil)
if err != nil {
panic(err)
}

spr1 := pixel.NewSprite(p1, p1.Bounds())
spr2 := pixel.NewSprite(p2, p2.Bounds())

for !win.Closed() {
if win.JustReleased(pixel.KeyEscape) {
win.SetClosed(true)
}

win.Clear(colornames.Skyblue)

spr1.Draw(win, pixel.IM.Moved(win.Bounds().Center().Add(pixel.V(0, 100))))
spr2.Draw(win, pixel.IM.Moved(win.Bounds().Center().Add(pixel.V(0, -100))))

win.Update()
}
}

0 comments on commit 214000d

Please sign in to comment.