Created
August 21, 2015 00:54
-
-
Save fogleman/568248e76b015efe801a to your computer and use it in GitHub Desktop.
Go 1.5 Crash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/go-gl/gl/v2.1/gl" | |
"github.com/go-gl/glfw/v3.1/glfw" | |
"github.com/ungerik/go-cairo" | |
) | |
func createTexture() uint32 { | |
var texture uint32 | |
gl.GenTextures(1, &texture) | |
gl.BindTexture(gl.TEXTURE_2D, texture) | |
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) | |
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) | |
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) | |
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) | |
return texture | |
} | |
func setTexture(surface *cairo.Surface) { | |
gl.TexImage2D( | |
gl.TEXTURE_2D, 0, gl.RGBA, | |
int32(surface.GetWidth()), | |
int32(surface.GetHeight()), | |
0, gl.RGBA, gl.UNSIGNED_BYTE, | |
gl.Ptr(surface.GetData())) | |
// or just: | |
// surface.GetData() // <-- just calling surface.GetData() is sufficient | |
} | |
func drawBuffer(window *glfw.Window) { | |
gl.Begin(gl.QUADS) | |
gl.TexCoord2f(0, 1) | |
gl.Vertex2f(-1, -1) | |
gl.TexCoord2f(1, 1) | |
gl.Vertex2f(1, -1) | |
gl.TexCoord2f(1, 0) | |
gl.Vertex2f(1, 1) | |
gl.TexCoord2f(0, 0) | |
gl.Vertex2f(-1, 1) | |
gl.End() | |
} | |
func main() { | |
// initialize glfw | |
if err := glfw.Init(); err != nil { | |
log.Fatalln(err) | |
} | |
defer glfw.Terminate() | |
// create window | |
glfw.WindowHint(glfw.ContextVersionMajor, 2) | |
glfw.WindowHint(glfw.ContextVersionMinor, 1) | |
window, err := glfw.CreateWindow(640, 480, "Go", nil, nil) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
window.MakeContextCurrent() | |
// initialize gl | |
if err := gl.Init(); err != nil { | |
log.Fatalln(err) | |
} | |
gl.Enable(gl.TEXTURE_2D) | |
createTexture() | |
for !window.ShouldClose() { | |
w, h := window.GetFramebufferSize() | |
fmt.Println(w, h) | |
x, y := float64(w), float64(h) | |
dc := cairo.NewSurface(cairo.FORMAT_ARGB32, w, h) | |
dc.SetSourceRGB(0, 0, 0) | |
dc.Paint() | |
dc.SetSourceRGB(1, 1, 1) | |
dc.MoveTo(0, 0) | |
dc.LineTo(x, y) | |
dc.MoveTo(x, 0) | |
dc.LineTo(0, y) | |
dc.SetLineWidth(8) | |
dc.Stroke() | |
setTexture(dc) | |
drawBuffer(window) | |
window.SwapBuffers() | |
glfw.PollEvents() | |
// runtime.GC() // <-- using this gets us further but still crashes | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment