update to latest

This commit is contained in:
Loic Nageleisen 2016-03-10 15:01:12 +01:00
parent 58bc762dc1
commit ff8dd84a8f
2 changed files with 380 additions and 371 deletions

View file

View file

@ -1,19 +1,20 @@
package main
import (
"runtime"
"time"
"math"
"log"
"io"
"errors"
"os"
"syscall"
"image"
"image/png"
gl "github.com/go-gl/gl"
glfw "github.com/go-gl/glfw3"
pa "code.google.com/p/portaudio-go/portaudio"
"io"
"log"
"math"
"os"
"runtime"
"syscall"
"time"
gl "github.com/go-gl/gl/v2.1/gl"
glfw "github.com/go-gl/glfw/v3.0/glfw"
pa "github.com/gordonklaus/portaudio"
)
var _ = pa.Initialize // TODO: remove later
@ -26,6 +27,8 @@ const (
INPUT_JUMP = 4
)
type Texture uint32
// iterate faster
func rerun() (err error) {
@ -41,7 +44,9 @@ func rerun() (err error) {
func reexec() {
err := rerun()
if err != nil { panic(err) }
if err != nil {
panic(err)
}
}
// glfw callbacks
@ -84,22 +89,22 @@ func onKey(input chan int, window *glfw.Window, k glfw.Key, s int, action glfw.A
}
}
// utils
func readTexture(r io.Reader) (texId gl.Texture, err error) {
func readTexture(r io.Reader) (texId Texture, err error) {
img, err := png.Decode(r)
if err != nil {
return gl.Texture(0), err
return Texture(0), err
}
rgba, ok := img.(*image.NRGBA)
if !ok {
return gl.Texture(0), errors.New("not an NRGBA image")
return Texture(0), errors.New("not an NRGBA image")
}
texId = gl.GenTexture()
texId.Bind(gl.TEXTURE_2D)
tid := uint32(texId)
gl.GenTextures(1, &tid)
gl.BindTexture(gl.TEXTURE_2D, uint32(texId))
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
@ -107,7 +112,7 @@ func readTexture(r io.Reader) (texId gl.Texture, err error) {
raw := make([]byte, w*h*4)
raw_stride := w * 4
if raw_stride != rgba.Stride {
return gl.Texture(0), errors.New("incompatible stride")
return Texture(0), errors.New("incompatible stride")
}
dst := len(raw) - raw_stride
@ -118,7 +123,7 @@ func readTexture(r io.Reader) (texId gl.Texture, err error) {
lod := 0
border := 0
gl.TexImage2D(gl.TEXTURE_2D, lod, gl.RGBA, w, h, border, gl.RGBA, gl.UNSIGNED_BYTE, raw)
gl.TexImage2D(gl.TEXTURE_2D, int32(lod), gl.RGBA, int32(w), int32(h), int32(border), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(raw))
return
}
@ -170,26 +175,26 @@ func scaledSpriteQuad(x int, y int, w int, h int, scale float32) {
gl.End()
}
func drawSprite(texture gl.Texture, x float64, y float64, a float64, s float64, list uint) {
func drawSprite(texture Texture, x float64, y float64, a float64, s float64, list uint) {
deg := math.Mod(360*float64(a)/(2*math.Pi), 360.0)
gl.LoadIdentity()
texture.Bind(gl.TEXTURE_2D)
gl.BindTexture(gl.TEXTURE_2D, uint32(texture))
gl.Translatef(float32(x), float32(y), 0)
gl.Rotatef(float32(deg), 0.0, 0.0, 1.0);
gl.Rotatef(float32(deg), 0.0, 0.0, 1.0)
gl.Scalef(float32(s), float32(s), 1.0)
gl.CallList(list)
gl.CallList(uint32(list))
}
func makeSprite(x int, y int, w int, h int) (quad uint) {
quad = gl.GenLists(1)
gl.NewList(quad, gl.COMPILE)
quad = uint(gl.GenLists(1))
gl.NewList(uint32(quad), gl.COMPILE)
spriteQuad(x, y, w, h)
gl.EndList()
return
}
func drawTile(texture gl.Texture, x int, y int, list uint) {
func drawTile(texture Texture, x int, y int, list uint) {
sx := float64(16*x + 16/2)
sy := float64(16*y + 16/2)
drawSprite(texture, sx, sy, 0, 1, list)
@ -199,7 +204,7 @@ func drawWaterTile(x int, y int, t float64) {
waveHeight := 0.0
wavePhase := -1 + x%2*2
if (t > 0) {
if t > 0 {
waveHeight = float64(wavePhase) * math.Sin(t)
}
@ -319,7 +324,6 @@ func stepper(tick chan int) {
}
// renderer
var mouseX float64
@ -388,7 +392,7 @@ func renderer(done chan int, input chan int, tick chan int) {
done <- 1
}
func setup() (textures map[string]gl.Texture, lists map[string]uint) {
func setup() (textures map[string]Texture, lists map[string]uint) {
gl.Enable(gl.TEXTURE_2D)
gl.Enable(gl.DEPTH_TEST)
gl.Enable(gl.LIGHTING)
@ -400,17 +404,21 @@ func setup() (textures map[string]gl.Texture, lists map[string]uint) {
gl.DepthFunc(gl.LEQUAL)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
textures = map[string]gl.Texture{}
textures = map[string]Texture{}
lists = map[string]uint{}
// load spritesheet and make sprites
img, err := os.Open("spritesheet.png")
if err != nil { log.Panic(err) }
if err != nil {
log.Panic(err)
}
defer img.Close()
spriteSheet, err := readTexture(img)
if err != nil { log.Panic(err) }
if err != nil {
log.Panic(err)
}
textures["sprites"] = spriteSheet
lists["test"] = makeSprite(0, 0, 2, 2)
@ -429,9 +437,10 @@ func setup() (textures map[string]gl.Texture, lists map[string]uint) {
return
}
func destroy(textures map[string]gl.Texture) {
func destroy(textures map[string]Texture) {
for _, texture := range textures {
texture.Delete()
t := uint32(texture)
gl.DeleteTextures(1, &t)
}
}
@ -440,7 +449,7 @@ var vy1 float64
var vx2 float64
var vy2 float64
func render(textures map[string]gl.Texture, lists map[string]uint) {
func render(textures map[string]Texture, lists map[string]uint) {
// start afresh
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
@ -448,7 +457,7 @@ func render(textures map[string]gl.Texture, lists map[string]uint) {
width := float32(ww)
height := float32(wh)
density := 2 // times 2 because HiDPI
gl.Viewport(0, 0, int(width)*density, int(height)*density)
gl.Viewport(0, 0, int32(width)*int32(density), int32(height)*int32(density))
vw := 320
vh := 240
@ -460,12 +469,12 @@ func render(textures map[string]gl.Texture, lists map[string]uint) {
vx2, vy2 = float64(vw), float64(vh)
gl.Ortho(vx1, vx2, vy1, vy2, -1.0, 1.0)
gl.MatrixMode(gl.MODELVIEW);
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
// lighten things
ambient := []float32{1, 1, 1, 1}
gl.Lightfv(gl.LIGHT0, gl.AMBIENT, ambient)
gl.Lightfv(gl.LIGHT0, gl.AMBIENT, &ambient[0])
gl.Enable(gl.LIGHT0)
// time source