extract sprite

This commit is contained in:
Loic Nageleisen 2014-04-26 15:25:47 +02:00
parent 7691c1520d
commit 656ced6742
4 changed files with 68 additions and 12 deletions

View file

@ -6,6 +6,7 @@ import (
"io"
"errors"
"os"
"syscall"
"image"
"image/png"
gl "github.com/go-gl/gl"
@ -16,6 +17,24 @@ import (
var _ = pa.Initialize // TODO: remove later
// iterate faster
func rerun() (err error) {
log.Println("rerun")
gopath := os.Getenv("GOPATH")
env := []string{"GOPATH=" + gopath}
args := []string{"go", "run", "ld48-29.go"}
err = syscall.Exec("/usr/local/bin/go", args, env)
log.Fatal(err)
return
}
func reexec() {
err := rerun()
if err != nil { panic(err) }
}
// glfw callbacks
func onError(err glfw.ErrorCode, desc string) {
@ -28,6 +47,10 @@ func onKey(window *glfw.Window, k glfw.Key, s int, action glfw.Action, mods glfw
}
switch glfw.Key(k) {
case glfw.KeyR:
if mods & glfw.ModSuper != 0 {
reexec()
}
case glfw.KeyEscape:
window.SetShouldClose(true)
default:
@ -74,6 +97,33 @@ func readTexture(r io.Reader) (texId gl.Texture, err error) {
return
}
func spriteQuad(x int, y int, w int, h int) {
size := 256
unit := 16
x1 := x * unit
y1 := y * unit
x2 := x * unit + w * unit
y2 := y * unit + h * unit
rx1 := float32(x1) / float32(size)
rx2 := float32(x2) / float32(size)
ry1 := float32(y1) / float32(size)
ry2 := float32(y2) / float32(size)
gl.MatrixMode(gl.MODELVIEW)
gl.Begin(gl.QUADS)
gl.Normal3f(0, 0, 1)
gl.TexCoord2f(rx1, ry1)
gl.Vertex3f(-1.0, -1.0, 1.0)
gl.TexCoord2f(rx2, ry1)
gl.Vertex3f(1.0, -1.0, 1.0)
gl.TexCoord2f(rx2, ry2)
gl.Vertex3f(1.0, 1.0, 1.0)
gl.TexCoord2f(rx1, ry2)
gl.Vertex3f(-1.0, 1.0, 1.0)
gl.End()
}
// main
@ -132,17 +182,7 @@ func setup() (textures map[string]gl.Texture, lists map[string]uint) {
quad := gl.GenLists(1)
gl.NewList(quad, gl.COMPILE)
gl.Begin(gl.QUADS)
gl.Normal3f(0, 0, 1)
gl.TexCoord2f(0, 0)
gl.Vertex3f(-1.0, -1.0, 1.0)
gl.TexCoord2f(0.5, 0)
gl.Vertex3f(1.0, -1.0, 1.0)
gl.TexCoord2f(0.5, 0.5)
gl.Vertex3f(1.0, 1.0, 1.0)
gl.TexCoord2f(0, 0.5)
gl.Vertex3f(-1.0, 1.0, 1.0)
gl.End()
spriteQuad(0, 0, 4, 4)
gl.EndList()
lists["test"] = quad

View file

@ -48,8 +48,24 @@ All time is CEST
- 11:57
And we have a spritesheet! ![](screenshots/1.png)
And we have a spritesheet!
![](screenshots/1.png)
- 12:12
Taking a light break to stuff self.body with calories
- 12:45
Short on supplies. Have to get out or risk falling down in 2 hours due to lack or carbohydrates in bloodstream.
- 14:12
Got sidetracked in reading Doom engine trivia
- 15:21
Getting to terms with OpenGL. Sprite system coming along nicely
![](screenshots/2.png)

BIN
screenshots/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

Before After
Before After