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,240 +1,245 @@
package main package main
import ( import (
"runtime" "errors"
"time" "image"
"math" "image/png"
"log" "io"
"io" "log"
"errors" "math"
"os" "os"
"syscall" "runtime"
"image" "syscall"
"image/png" "time"
gl "github.com/go-gl/gl"
glfw "github.com/go-gl/glfw3" gl "github.com/go-gl/gl/v2.1/gl"
pa "code.google.com/p/portaudio-go/portaudio" glfw "github.com/go-gl/glfw/v3.0/glfw"
pa "github.com/gordonklaus/portaudio"
) )
var _ = pa.Initialize // TODO: remove later var _ = pa.Initialize // TODO: remove later
const ( const (
INPUT_UP = 0 INPUT_UP = 0
INPUT_DOWN = 1 INPUT_DOWN = 1
INPUT_LEFT = 2 INPUT_LEFT = 2
INPUT_RIGHT = 3 INPUT_RIGHT = 3
INPUT_JUMP = 4 INPUT_JUMP = 4
) )
type Texture uint32
// iterate faster // iterate faster
func rerun() (err error) { func rerun() (err error) {
log.Println("rerun") log.Println("rerun")
gopath := os.Getenv("GOPATH") gopath := os.Getenv("GOPATH")
env := []string{"GOPATH=" + gopath} env := []string{"GOPATH=" + gopath}
args := []string{"go", "run", "ld48-29.go"} args := []string{"go", "run", "ld48-29.go"}
err = syscall.Exec("/usr/local/bin/go", args, env) err = syscall.Exec("/usr/local/bin/go", args, env)
log.Fatal(err) log.Fatal(err)
return return
} }
func reexec() { func reexec() {
err := rerun() err := rerun()
if err != nil { panic(err) } if err != nil {
panic(err)
}
} }
// glfw callbacks // glfw callbacks
func onError(err glfw.ErrorCode, desc string) { func onError(err glfw.ErrorCode, desc string) {
log.Printf("%v: %v\n", err, desc) log.Printf("%v: %v\n", err, desc)
} }
func onKey(input chan int, window *glfw.Window, k glfw.Key, s int, action glfw.Action, mods glfw.ModifierKey) { func onKey(input chan int, window *glfw.Window, k glfw.Key, s int, action glfw.Action, mods glfw.ModifierKey) {
if action != glfw.Press && action != glfw.Release { if action != glfw.Press && action != glfw.Release {
return return
} }
release := 0 release := 0
if action == glfw.Release { if action == glfw.Release {
release = 8 release = 8
} }
switch glfw.Key(k) { switch glfw.Key(k) {
case glfw.KeyR: case glfw.KeyR:
if (mods & glfw.ModSuper != 0) && action == glfw.Press { if (mods&glfw.ModSuper != 0) && action == glfw.Press {
reexec() reexec()
} }
case glfw.KeyUp: case glfw.KeyUp:
input <- INPUT_UP + release input <- INPUT_UP + release
case glfw.KeyDown: case glfw.KeyDown:
input <- INPUT_DOWN + release input <- INPUT_DOWN + release
case glfw.KeyLeft: case glfw.KeyLeft:
input <- INPUT_LEFT + release input <- INPUT_LEFT + release
case glfw.KeyRight: case glfw.KeyRight:
input <- INPUT_RIGHT + release input <- INPUT_RIGHT + release
case glfw.KeySpace: case glfw.KeySpace:
input <- INPUT_JUMP + release input <- INPUT_JUMP + release
case glfw.KeyEscape: case glfw.KeyEscape:
if action == glfw.Press { if action == glfw.Press {
window.SetShouldClose(true) window.SetShouldClose(true)
} }
default: default:
return return
} }
} }
// utils // 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) img, err := png.Decode(r)
if err != nil { if err != nil {
return gl.Texture(0), err return Texture(0), err
} }
rgba, ok := img.(*image.NRGBA) rgba, ok := img.(*image.NRGBA)
if !ok { if !ok {
return gl.Texture(0), errors.New("not an NRGBA image") return Texture(0), errors.New("not an NRGBA image")
} }
texId = gl.GenTexture() tid := uint32(texId)
texId.Bind(gl.TEXTURE_2D) gl.GenTextures(1, &tid)
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.BindTexture(gl.TEXTURE_2D, uint32(texId))
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
w, h := rgba.Bounds().Dx(), rgba.Bounds().Dy() w, h := rgba.Bounds().Dx(), rgba.Bounds().Dy()
raw := make([]byte, w*h*4) raw := make([]byte, w*h*4)
raw_stride := w * 4 raw_stride := w * 4
if raw_stride != rgba.Stride { 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 dst := len(raw) - raw_stride
for src := 0; src < len(rgba.Pix); src += rgba.Stride { for src := 0; src < len(rgba.Pix); src += rgba.Stride {
copy(raw[dst:dst+raw_stride], rgba.Pix[src:src+rgba.Stride]) copy(raw[dst:dst+raw_stride], rgba.Pix[src:src+rgba.Stride])
dst -= raw_stride dst -= raw_stride
} }
lod := 0 lod := 0
border := 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 return
} }
func spriteQuad(x int, y int, w int, h int) { func spriteQuad(x int, y int, w int, h int) {
scaledSpriteQuad(x, y, w, h, 1.0) scaledSpriteQuad(x, y, w, h, 1.0)
} }
func scaledSpriteQuad(x int, y int, w int, h int, scale float32) { func scaledSpriteQuad(x int, y int, w int, h int, scale float32) {
// TODO: set elsewhere (spritesheet property) // TODO: set elsewhere (spritesheet property)
size := 256 // spritesheet size size := 256 // spritesheet size
unit := 16 // sprite unit size unit := 16 // sprite unit size
// sprite to absolute pixel coords // sprite to absolute pixel coords
// origin is as tex: bottom-left // origin is as tex: bottom-left
x1 := x * unit x1 := x * unit
y1 := y * unit y1 := y * unit
x2 := x1 + w * unit x2 := x1 + w*unit
y2 := y1 + h * unit y2 := y1 + h*unit
// abs pixel to relative tex coords // abs pixel to relative tex coords
rx1 := float32(x1) / float32(size) rx1 := float32(x1) / float32(size)
rx2 := float32(x2) / float32(size) rx2 := float32(x2) / float32(size)
ry1 := float32(y1) / float32(size) ry1 := float32(y1) / float32(size)
ry2 := float32(y2) / float32(size) ry2 := float32(y2) / float32(size)
// scale sprite // scale sprite
qsize := 1.0 * scale qsize := 1.0 * scale
// relative model coords // relative model coords
// sprite-centered origin, half each way // sprite-centered origin, half each way
qx1 := -qsize * float32(unit * w) / 2.0 qx1 := -qsize * float32(unit*w) / 2.0
qy1 := -qsize * float32(unit * h) / 2.0 qy1 := -qsize * float32(unit*h) / 2.0
qx2 := qsize * float32(unit * w) / 2.0 qx2 := qsize * float32(unit*w) / 2.0
qy2 := qsize * float32(unit * h) / 2.0 qy2 := qsize * float32(unit*h) / 2.0
// draw sprite quad // draw sprite quad
gl.MatrixMode(gl.MODELVIEW) gl.MatrixMode(gl.MODELVIEW)
gl.Begin(gl.QUADS) gl.Begin(gl.QUADS)
gl.Normal3f(0, 0, 1) gl.Normal3f(0, 0, 1)
gl.TexCoord2f(rx1, ry1) gl.TexCoord2f(rx1, ry1)
gl.Vertex3f(qx1, qy1, 1.0) gl.Vertex3f(qx1, qy1, 1.0)
gl.TexCoord2f(rx2, ry1) gl.TexCoord2f(rx2, ry1)
gl.Vertex3f(qx2, qy1, 1.0) gl.Vertex3f(qx2, qy1, 1.0)
gl.TexCoord2f(rx2, ry2) gl.TexCoord2f(rx2, ry2)
gl.Vertex3f(qx2, qy2, 1.0) gl.Vertex3f(qx2, qy2, 1.0)
gl.TexCoord2f(rx1, ry2) gl.TexCoord2f(rx1, ry2)
gl.Vertex3f(qx1, qy2, 1.0) gl.Vertex3f(qx1, qy2, 1.0)
gl.End() 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) deg := math.Mod(360*float64(a)/(2*math.Pi), 360.0)
gl.LoadIdentity() gl.LoadIdentity()
texture.Bind(gl.TEXTURE_2D) gl.BindTexture(gl.TEXTURE_2D, uint32(texture))
gl.Translatef(float32(x), float32(y), 0) 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.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) { func makeSprite(x int, y int, w int, h int) (quad uint) {
quad = gl.GenLists(1) quad = uint(gl.GenLists(1))
gl.NewList(quad, gl.COMPILE) gl.NewList(uint32(quad), gl.COMPILE)
spriteQuad(x, y, w, h) spriteQuad(x, y, w, h)
gl.EndList() gl.EndList()
return 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) sx := float64(16*x + 16/2)
sy := float64(16 * y + 16 / 2) sy := float64(16*y + 16/2)
drawSprite(texture, sx, sy, 0, 1, list) drawSprite(texture, sx, sy, 0, 1, list)
} }
func drawWaterTile(x int, y int, t float64) { func drawWaterTile(x int, y int, t float64) {
waveHeight := 0.0 waveHeight := 0.0
wavePhase := -1 + x % 2 * 2 wavePhase := -1 + x%2*2
if (t > 0) { if t > 0 {
waveHeight = float64(wavePhase) * math.Sin(t) waveHeight = float64(wavePhase) * math.Sin(t)
} }
qx1, qy1 := 16 * float32(x), 16 * float32(y) qx1, qy1 := 16*float32(x), 16*float32(y)
qx2, qy2 := qx1 + 16, qy1 + 16 qx2, qy2 := qx1+16, qy1+16
gl.Disable(gl.TEXTURE_2D) gl.Disable(gl.TEXTURE_2D)
gl.Disable(gl.LIGHTING) gl.Disable(gl.LIGHTING)
gl.MatrixMode(gl.MODELVIEW) gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity() gl.LoadIdentity()
gl.Color4f(0.0, 0.0, 0.5, 0.3) gl.Color4f(0.0, 0.0, 0.5, 0.3)
gl.Begin(gl.QUADS) gl.Begin(gl.QUADS)
gl.Normal3f(0.0, 0.0, 1.0) gl.Normal3f(0.0, 0.0, 1.0)
gl.Vertex3f(qx1, qy1, 1.0) gl.Vertex3f(qx1, qy1, 1.0)
gl.Vertex3f(qx2, qy1, 1.0) gl.Vertex3f(qx2, qy1, 1.0)
gl.Vertex3f(qx2, qy2 + float32(waveHeight), 1.0) gl.Vertex3f(qx2, qy2+float32(waveHeight), 1.0)
gl.Vertex3f(qx1, qy2 + float32(waveHeight), 1.0) gl.Vertex3f(qx1, qy2+float32(waveHeight), 1.0)
gl.End() gl.End()
gl.Enable(gl.LIGHTING) gl.Enable(gl.LIGHTING)
gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.TEXTURE_2D)
} }
// main // main
func main() { func main() {
done := make(chan int) done := make(chan int)
input := make(chan int, 64) input := make(chan int, 64)
tick := make(chan int) tick := make(chan int)
go renderer(done, input, tick) go renderer(done, input, tick)
go inputMapper(input) go inputMapper(input)
go stepper(tick) go stepper(tick)
<-done <-done
} }
// world // world
@ -242,84 +247,83 @@ func main() {
var inputState [5]bool var inputState [5]bool
func inputMapper(input chan int) { func inputMapper(input chan int) {
inputState = [5]bool{false,false,false,false,false} inputState = [5]bool{false, false, false, false, false}
for { for {
in := <- input in := <-input
pressed := in < 8 pressed := in < 8
if !pressed { if !pressed {
in = in - 8 in = in - 8
} }
inputState[in] = pressed inputState[in] = pressed
} }
} }
func stepper(tick chan int) { func stepper(tick chan int) {
pcx := 0.0 pcx := 0.0
pcy := 0.0 pcy := 0.0
ppx := pcx ppx := pcx
ppy := pcy ppy := pcy
pm := 10.0 pm := 10.0
ct := float64(time.Now().UnixNano()) / math.Pow(10, 9) ct := float64(time.Now().UnixNano()) / math.Pow(10, 9)
pt := ct pt := ct
dt := 0.0 dt := 0.0
for { for {
<-tick <-tick
pt, ct = ct, float64(time.Now().UnixNano()) / math.Pow(10, 9) pt, ct = ct, float64(time.Now().UnixNano())/math.Pow(10, 9)
dt = ct - pt dt = ct - pt
pvx, pvy := (pcx - ppx) / dt, (pcy - ppy) / dt pvx, pvy := (pcx-ppx)/dt, (pcy-ppy)/dt
ppx, ppy = pcx, pcy ppx, ppy = pcx, pcy
fx := 0.0 fx := 0.0
fy := 0.0 fy := 0.0
// gravity // gravity
fy += -10.0 * pm fy += -10.0 * pm
// movement // movement
if inputState[INPUT_LEFT] { if inputState[INPUT_LEFT] {
fx += -20 fx += -20
} }
if inputState[INPUT_RIGHT] { if inputState[INPUT_RIGHT] {
fx += 20 fx += 20
} }
if inputState[INPUT_JUMP] { if inputState[INPUT_JUMP] {
fy += 20 * pm fy += 20 * pm
} }
// friction // friction
fx += -57 * pvx fx += -57 * pvx
fy += -57 * pvy fy += -57 * pvy
// integrator // integrator
pax := fx / pm pax := fx / pm
pay := fy / pm pay := fy / pm
pvx = pax * dt + pvx pvx = pax*dt + pvx
pvy = pay * dt + pvy pvy = pay*dt + pvy
dpcx := pvx * dt dpcx := pvx * dt
dpcy := pvy * dt dpcy := pvy * dt
if math.Abs(dpcx) < 0.001 { if math.Abs(dpcx) < 0.001 {
dpcx = 0 dpcx = 0
} }
if math.Abs(dpcy) < 0.001 { if math.Abs(dpcy) < 0.001 {
dpcy = 0 dpcy = 0
} }
pcx += dpcx pcx += dpcx
pcy += dpcy pcy += dpcy
if pcy < 0 { if pcy < 0 {
pcy = 0 pcy = 0
} }
log.Printf("%.03f %.03f %.03f %.03f", pvx, pvy, pcx, pcy) log.Printf("%.03f %.03f %.03f %.03f", pvx, pvy, pcx, pcy)
} }
} }
// renderer // renderer
var mouseX float64 var mouseX float64
@ -331,108 +335,113 @@ var ww int
var wh int var wh int
func renderer(done chan int, input chan int, tick chan int) { func renderer(done chan int, input chan int, tick chan int) {
runtime.LockOSThread() runtime.LockOSThread()
glfw.SetErrorCallback(onError) glfw.SetErrorCallback(onError)
if !glfw.Init() { if !glfw.Init() {
panic("Can't init glfw!") panic("Can't init glfw!")
} }
defer glfw.Terminate() defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, 0) glfw.WindowHint(glfw.Resizable, 0)
ww = 640 ww = 640
wh = 480 wh = 480
window, err := glfw.CreateWindow(ww, wh, "LD48-29", nil, nil) window, err := glfw.CreateWindow(ww, wh, "LD48-29", nil, nil)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
window.SetInputMode(glfw.Cursor, glfw.CursorHidden) window.SetInputMode(glfw.Cursor, glfw.CursorHidden)
onKeyClosure := func (window *glfw.Window, k glfw.Key, s int, action glfw.Action, mods glfw.ModifierKey) { onKeyClosure := func(window *glfw.Window, k glfw.Key, s int, action glfw.Action, mods glfw.ModifierKey) {
onKey(input, window, k, s, action, mods) onKey(input, window, k, s, action, mods)
} }
onMouseClosure := func (window *glfw.Window, x float64, y float64) { onMouseClosure := func(window *glfw.Window, x float64, y float64) {
rx := float64(ww) / (vx2 - vx1) rx := float64(ww) / (vx2 - vx1)
ry := float64(wh) / (vy2 - vy1) ry := float64(wh) / (vy2 - vy1)
mouseX, mouseY = x/rx - vx1, vy2 - (y/ry - vy1) mouseX, mouseY = x/rx-vx1, vy2-(y/ry-vy1)
mouseVisible = mouseX < vx2 && mouseX >= 0 && mouseY < vy2 && mouseY >= 0 mouseVisible = mouseX < vx2 && mouseX >= 0 && mouseY < vy2 && mouseY >= 0
} }
onMouseButtonClosure := func (window *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) { onMouseButtonClosure := func(window *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
if button == 0 { if button == 0 {
mousePressed = action == 1 mousePressed = action == 1
} }
} }
window.SetKeyCallback(onKeyClosure) window.SetKeyCallback(onKeyClosure)
window.SetCursorPositionCallback(onMouseClosure) window.SetCursorPositionCallback(onMouseClosure)
window.SetMouseButtonCallback(onMouseButtonClosure) window.SetMouseButtonCallback(onMouseButtonClosure)
window.MakeContextCurrent() window.MakeContextCurrent()
textures, lists := setup() textures, lists := setup()
defer destroy(textures) defer destroy(textures)
for !window.ShouldClose() { for !window.ShouldClose() {
render(textures, lists) render(textures, lists)
window.SwapBuffers() window.SwapBuffers()
glfw.PollEvents() glfw.PollEvents()
tick <- 1 tick <- 1
} }
done <- 1 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.TEXTURE_2D)
gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.DEPTH_TEST)
gl.Enable(gl.LIGHTING) gl.Enable(gl.LIGHTING)
gl.Enable(gl.CULL_FACE) gl.Enable(gl.CULL_FACE)
gl.Enable(gl.BLEND) gl.Enable(gl.BLEND)
gl.ClearColor(0.4, 0.8, 0.95, 0) gl.ClearColor(0.4, 0.8, 0.95, 0)
gl.ClearDepth(1) gl.ClearDepth(1)
gl.DepthFunc(gl.LEQUAL) gl.DepthFunc(gl.LEQUAL)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
textures = map[string]gl.Texture{} textures = map[string]Texture{}
lists = map[string]uint{} lists = map[string]uint{}
// load spritesheet and make sprites // load spritesheet and make sprites
img, err := os.Open("spritesheet.png") img, err := os.Open("spritesheet.png")
if err != nil { log.Panic(err) } if err != nil {
defer img.Close() log.Panic(err)
}
defer img.Close()
spriteSheet, err := readTexture(img) spriteSheet, err := readTexture(img)
if err != nil { log.Panic(err) } if err != nil {
textures["sprites"] = spriteSheet log.Panic(err)
}
textures["sprites"] = spriteSheet
lists["test"] = makeSprite(0, 0, 2, 2) lists["test"] = makeSprite(0, 0, 2, 2)
lists["cursor"] = makeSprite(3, 2, 1, 1) lists["cursor"] = makeSprite(3, 2, 1, 1)
lists["cursorclick"] = makeSprite(3, 3, 1, 1) lists["cursorclick"] = makeSprite(3, 3, 1, 1)
lists["cloud1"] = makeSprite(0, 2, 3, 2) lists["cloud1"] = makeSprite(0, 2, 3, 2)
lists["cloud2"] = makeSprite(0, 4, 2, 2) lists["cloud2"] = makeSprite(0, 4, 2, 2)
lists["cloud3"] = makeSprite(2, 4, 2, 2) lists["cloud3"] = makeSprite(2, 4, 2, 2)
lists["stonewall"] = makeSprite(2, 0, 1, 1) lists["stonewall"] = makeSprite(2, 0, 1, 1)
lists["stonewallright"] = makeSprite(3, 0, 1, 1) lists["stonewallright"] = makeSprite(3, 0, 1, 1)
lists["stonewalltopright"] = makeSprite(3, 1, 1, 1) lists["stonewalltopright"] = makeSprite(3, 1, 1, 1)
lists["stonewalltop"] = makeSprite(2, 1, 1, 1) lists["stonewalltop"] = makeSprite(2, 1, 1, 1)
lists["stonewallleft"] = makeSprite(4, 0, 1, 1) lists["stonewallleft"] = makeSprite(4, 0, 1, 1)
lists["stonewalltopleft"] = makeSprite(4, 1, 1, 1) lists["stonewalltopleft"] = makeSprite(4, 1, 1, 1)
return return
} }
func destroy(textures map[string]gl.Texture) { func destroy(textures map[string]Texture) {
for _, texture := range textures { for _, texture := range textures {
texture.Delete() t := uint32(texture)
} gl.DeleteTextures(1, &t)
}
} }
var vx1 float64 var vx1 float64
@ -440,87 +449,87 @@ var vy1 float64
var vx2 float64 var vx2 float64
var vy2 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 // start afresh
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// set viewport // set viewport
width := float32(ww) width := float32(ww)
height := float32(wh) height := float32(wh)
density := 2 // times 2 because HiDPI 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 vw := 320
vh := 240 vh := 240
// set projection // set projection
gl.MatrixMode(gl.PROJECTION) gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity() gl.LoadIdentity()
vx1, vy1 = 0, 0 vx1, vy1 = 0, 0
vx2, vy2 = float64(vw), float64(vh) vx2, vy2 = float64(vw), float64(vh)
gl.Ortho(vx1, vx2, vy1, vy2, -1.0, 1.0) gl.Ortho(vx1, vx2, vy1, vy2, -1.0, 1.0)
gl.MatrixMode(gl.MODELVIEW); gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity() gl.LoadIdentity()
// lighten things // lighten things
ambient := []float32{1, 1, 1, 1} 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) gl.Enable(gl.LIGHT0)
// time source // time source
t := float64(time.Now().UnixNano()) / math.Pow(10, 9) t := float64(time.Now().UnixNano()) / math.Pow(10, 9)
// clouds // clouds
fy := 2 * math.Pi * t / 60 fy := 2 * math.Pi * t / 60
drawSprite(textures["sprites"], 200, 200+8*math.Sin(fy/1.3), 0, 2.0, lists["cloud1"]) drawSprite(textures["sprites"], 200, 200+8*math.Sin(fy/1.3), 0, 2.0, lists["cloud1"])
// wall tiles // wall tiles
for j := 0; j < 9; j++ { for j := 0; j < 9; j++ {
drawTile(textures["sprites"], 3, j, lists["stonewallright"]) drawTile(textures["sprites"], 3, j, lists["stonewallright"])
} }
drawTile(textures["sprites"], 3, 9, lists["stonewalltopright"]) drawTile(textures["sprites"], 3, 9, lists["stonewalltopright"])
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
for j:= 0; j < 9; j++ { for j := 0; j < 9; j++ {
drawTile(textures["sprites"], i, j, lists["stonewall"]) drawTile(textures["sprites"], i, j, lists["stonewall"])
} }
drawTile(textures["sprites"], i, 9, lists["stonewalltop"]) drawTile(textures["sprites"], i, 9, lists["stonewalltop"])
} }
for j := 0; j < 6; j++ { for j := 0; j < 6; j++ {
drawTile(textures["sprites"], 19 - 2, j, lists["stonewallleft"]) drawTile(textures["sprites"], 19-2, j, lists["stonewallleft"])
} }
drawTile(textures["sprites"], 19 - 2, 6, lists["stonewalltopleft"]) drawTile(textures["sprites"], 19-2, 6, lists["stonewalltopleft"])
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
for j:= 0; j < 6; j++ { for j := 0; j < 6; j++ {
drawTile(textures["sprites"], 19 - i, j, lists["stonewall"]) drawTile(textures["sprites"], 19-i, j, lists["stonewall"])
} }
drawTile(textures["sprites"], 19 - i, 6, lists["stonewalltop"]) drawTile(textures["sprites"], 19-i, 6, lists["stonewalltop"])
} }
// water // water
for i := 0; i < 320 / 16; i++ { for i := 0; i < 320/16; i++ {
for j := 0; j < 3; j++ { for j := 0; j < 3; j++ {
wt := 0.0 wt := 0.0
if j == 2 { if j == 2 {
wt = t wt = t
} }
drawWaterTile(i, j, 4*wt) drawWaterTile(i, j, 4*wt)
} }
} }
// mouse pointer // mouse pointer
if mouseVisible { if mouseVisible {
cursor := lists["cursor"] cursor := lists["cursor"]
if mousePressed { if mousePressed {
cursor = lists["cursorclick"] cursor = lists["cursorclick"]
} }
drawSprite(textures["sprites"], mouseX, mouseY, 0, 1.0, cursor) drawSprite(textures["sprites"], mouseX, mouseY, 0, 1.0, cursor)
} }
} }