opengl: Error prefix

This commit is contained in:
Hajime Hoshi 2016-02-24 00:31:25 +09:00
parent ffb8a78e37
commit e8a3e04535
3 changed files with 23 additions and 21 deletions

View File

@ -114,7 +114,7 @@ func (c *Context) Check() {
func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (Texture, error) { func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (Texture, error) {
t := gl.CreateTexture() t := gl.CreateTexture()
if t.Value < 0 { if t.Value < 0 {
return Texture{}, errors.New("graphics: glGenTexture failed") return Texture{}, errors.New("opengl: glGenTexture failed")
} }
gl.PixelStorei(mgl.UNPACK_ALIGNMENT, 4) gl.PixelStorei(mgl.UNPACK_ALIGNMENT, 4)
gl.BindTexture(mgl.TEXTURE_2D, t) gl.BindTexture(mgl.TEXTURE_2D, t)
@ -139,7 +139,7 @@ func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8,
pixels := make([]uint8, 4*width*height) pixels := make([]uint8, 4*width*height)
gl.ReadPixels(pixels, 0, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE) gl.ReadPixels(pixels, 0, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE)
if e := gl.GetError(); e != mgl.NO_ERROR { if e := gl.GetError(); e != mgl.NO_ERROR {
return nil, errors.New(fmt.Sprintf("graphics: glReadPixels: %d", e)) return nil, errors.New(fmt.Sprintf("opengl: glReadPixels: %d", e))
} }
return pixels, nil return pixels, nil
} }
@ -168,12 +168,12 @@ func (c *Context) NewFramebuffer(texture Texture) (Framebuffer, error) {
s := gl.CheckFramebufferStatus(mgl.FRAMEBUFFER) s := gl.CheckFramebufferStatus(mgl.FRAMEBUFFER)
if s != mgl.FRAMEBUFFER_COMPLETE { if s != mgl.FRAMEBUFFER_COMPLETE {
if s != 0 { if s != 0 {
return Framebuffer{}, errors.New(fmt.Sprintf("graphics: creating framebuffer failed: %v", s)) return Framebuffer{}, errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %v", s))
} }
if e := gl.GetError(); e != mgl.NO_ERROR { if e := gl.GetError(); e != mgl.NO_ERROR {
return Framebuffer{}, errors.New(fmt.Sprintf("graphics: creating framebuffer failed: (glGetError) %d", e)) return Framebuffer{}, errors.New(fmt.Sprintf("opengl: creating framebuffer failed: (glGetError) %d", e))
} }
return Framebuffer{}, errors.New(fmt.Sprintf("graphics: creating framebuffer failed: unknown error")) return Framebuffer{}, errors.New(fmt.Sprintf("opengl: creating framebuffer failed: unknown error"))
} }
return Framebuffer(f), nil return Framebuffer(f), nil
@ -184,9 +184,9 @@ func (c *Context) SetViewport(f Framebuffer, width, height int) error {
gl.BindFramebuffer(mgl.FRAMEBUFFER, mgl.Framebuffer(f)) gl.BindFramebuffer(mgl.FRAMEBUFFER, mgl.Framebuffer(f))
if err := gl.CheckFramebufferStatus(mgl.FRAMEBUFFER); err != mgl.FRAMEBUFFER_COMPLETE { if err := gl.CheckFramebufferStatus(mgl.FRAMEBUFFER); err != mgl.FRAMEBUFFER_COMPLETE {
if e := gl.GetError(); e != 0 { if e := gl.GetError(); e != 0 {
return errors.New(fmt.Sprintf("graphics: glBindFramebuffer failed: %d", e)) return errors.New(fmt.Sprintf("opengl: glBindFramebuffer failed: %d", e))
} }
return errors.New("graphics: glBindFramebuffer failed: the context is different?") return errors.New("opengl: glBindFramebuffer failed: the context is different?")
} }
gl.Viewport(0, 0, width, height) gl.Viewport(0, 0, width, height)
return nil return nil
@ -205,7 +205,7 @@ func (c *Context) DeleteFramebuffer(f Framebuffer) {
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) { func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
s := gl.CreateShader(mgl.Enum(shaderType)) s := gl.CreateShader(mgl.Enum(shaderType))
if s.Value == 0 { if s.Value == 0 {
return Shader{}, errors.New("graphics: glCreateShader failed") return Shader{}, errors.New("opengl: glCreateShader failed")
} }
gl.ShaderSource(s, source) gl.ShaderSource(s, source)
gl.CompileShader(s) gl.CompileShader(s)
@ -213,7 +213,7 @@ func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error
v := gl.GetShaderi(s, mgl.COMPILE_STATUS) v := gl.GetShaderi(s, mgl.COMPILE_STATUS)
if v == mgl.FALSE { if v == mgl.FALSE {
log := gl.GetShaderInfoLog(s) log := gl.GetShaderInfoLog(s)
return Shader{}, errors.New(fmt.Sprintf("graphics: shader compile failed: %s", log)) return Shader{}, errors.New(fmt.Sprintf("opengl: shader compile failed: %s", log))
} }
return Shader(s), nil return Shader(s), nil
} }
@ -229,7 +229,7 @@ func (c *Context) GlslHighpSupported() bool {
func (c *Context) NewProgram(shaders []Shader) (Program, error) { func (c *Context) NewProgram(shaders []Shader) (Program, error) {
p := gl.CreateProgram() p := gl.CreateProgram()
if p.Value == 0 { if p.Value == 0 {
return Program{}, errors.New("graphics: glCreateProgram failed") return Program{}, errors.New("opengl: glCreateProgram failed")
} }
for _, shader := range shaders { for _, shader := range shaders {
@ -238,7 +238,7 @@ func (c *Context) NewProgram(shaders []Shader) (Program, error) {
gl.LinkProgram(p) gl.LinkProgram(p)
v := gl.GetProgrami(p, mgl.LINK_STATUS) v := gl.GetProgrami(p, mgl.LINK_STATUS)
if v == mgl.FALSE { if v == mgl.FALSE {
return Program{}, errors.New("graphics: program error") return Program{}, errors.New("opengl: program error")
} }
return Program(p), nil return Program(p), nil
} }
@ -297,6 +297,7 @@ func (c *Context) DisableVertexAttribArray(p Program, location string) {
func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer { func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer {
b := gl.CreateBuffer() b := gl.CreateBuffer()
gl.BindBuffer(mgl.Enum(bufferType), b) gl.BindBuffer(mgl.Enum(bufferType), b)
//var bb []byte
switch v := v.(type) { switch v := v.(type) {
case int: case int:
gl.BufferInit(mgl.Enum(bufferType), v, mgl.Enum(bufferUsage)) gl.BufferInit(mgl.Enum(bufferType), v, mgl.Enum(bufferUsage))
@ -308,7 +309,7 @@ func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage Bu
} }
bt := &bytes.Buffer{} bt := &bytes.Buffer{}
if err := binary.Write(bt, binary.LittleEndian, v); err != nil { if err := binary.Write(bt, binary.LittleEndian, v); err != nil {
panic(fmt.Sprintf("graphics: Binary error: %v", err)) panic(fmt.Sprintf("opengl: Binary error: %v", err))
} }
gl.BufferData(mgl.Enum(bufferType), bt.Bytes(), mgl.Enum(bufferUsage)) gl.BufferData(mgl.Enum(bufferType), bt.Bytes(), mgl.Enum(bufferUsage))
return Buffer(b) return Buffer(b)
@ -321,7 +322,7 @@ func (c *Context) BindElementArrayBuffer(b Buffer) {
func (c *Context) BufferSubData(bufferType BufferType, data []int16) { func (c *Context) BufferSubData(bufferType BufferType, data []int16) {
bt := &bytes.Buffer{} bt := &bytes.Buffer{}
if err := binary.Write(bt, binary.LittleEndian, data); err != nil { if err := binary.Write(bt, binary.LittleEndian, data); err != nil {
panic(fmt.Sprintf("graphics: Binary error: %v", err)) panic(fmt.Sprintf("opengl: Binary error: %v", err))
} }
gl.BufferSubData(mgl.Enum(bufferType), 0, bt.Bytes()) gl.BufferSubData(mgl.Enum(bufferType), 0, bt.Bytes())
} }

View File

@ -119,7 +119,7 @@ func (c *Context) init() {
func (c *Context) Check() { func (c *Context) Check() {
gl := c.gl gl := c.gl
if e := gl.GetError(); e != gl.NO_ERROR { if e := gl.GetError(); e != gl.NO_ERROR {
panic(fmt.Sprintf("check failed: %d", e)) panic(fmt.Sprintf("opengl: check failed: %d", e))
} }
} }
@ -127,7 +127,7 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (
gl := c.gl gl := c.gl
t := gl.CreateTexture() t := gl.CreateTexture()
if t == nil { if t == nil {
return Texture{nil}, errors.New("glGenTexture failed") return Texture{nil}, errors.New("opengl: glGenTexture failed")
} }
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
gl.BindTexture(gl.TEXTURE_2D, t) gl.BindTexture(gl.TEXTURE_2D, t)
@ -167,7 +167,7 @@ func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8,
pixels := js.Global.Get("Uint8Array").New(4 * width * height) pixels := js.Global.Get("Uint8Array").New(4 * width * height)
gl.ReadPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels) gl.ReadPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
if e := gl.GetError(); e != gl.NO_ERROR { if e := gl.GetError(); e != gl.NO_ERROR {
return nil, errors.New(fmt.Sprintf("gl error: %d", e)) return nil, errors.New(fmt.Sprintf("opengl: error: %d", e))
} }
return pixels.Interface().([]uint8), nil return pixels.Interface().([]uint8), nil
} }
@ -198,7 +198,7 @@ func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) {
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, t.Object, 0) gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, t.Object, 0)
s := gl.CheckFramebufferStatus(gl.FRAMEBUFFER) s := gl.CheckFramebufferStatus(gl.FRAMEBUFFER)
if s != gl.FRAMEBUFFER_COMPLETE { if s != gl.FRAMEBUFFER_COMPLETE {
return Framebuffer{nil}, errors.New(fmt.Sprintf("creating framebuffer failed: %d", s)) return Framebuffer{nil}, errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s))
} }
return Framebuffer{f}, nil return Framebuffer{f}, nil
@ -237,7 +237,7 @@ func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error
gl := c.gl gl := c.gl
s := gl.CreateShader(int(shaderType)) s := gl.CreateShader(int(shaderType))
if s == nil { if s == nil {
return Shader{nil}, errors.New("glCreateShader failed") return Shader{nil}, errors.New("opengl: glCreateShader failed")
} }
gl.ShaderSource(s, source) gl.ShaderSource(s, source)
@ -245,7 +245,7 @@ func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error
if !gl.GetShaderParameterb(s, gl.COMPILE_STATUS) { if !gl.GetShaderParameterb(s, gl.COMPILE_STATUS) {
log := gl.GetShaderInfoLog(s) log := gl.GetShaderInfoLog(s)
return Shader{nil}, errors.New(fmt.Sprintf("shader compile failed: %s", log)) return Shader{nil}, errors.New(fmt.Sprintf("opengl: shader compile failed: %s", log))
} }
return Shader{s}, nil return Shader{s}, nil
} }
@ -270,7 +270,7 @@ func (c *Context) NewProgram(shaders []Shader) (Program, error) {
gl := c.gl gl := c.gl
p := gl.CreateProgram() p := gl.CreateProgram()
if p == nil { if p == nil {
return Program{nil}, errors.New("glCreateProgram failed") return Program{nil}, errors.New("opengl: glCreateProgram failed")
} }
p.Set("__ebiten_programId", lastProgramID) p.Set("__ebiten_programId", lastProgramID)
lastProgramID++ lastProgramID++
@ -280,7 +280,7 @@ func (c *Context) NewProgram(shaders []Shader) (Program, error) {
} }
gl.LinkProgram(p) gl.LinkProgram(p)
if !gl.GetProgramParameterb(p, gl.LINK_STATUS) { if !gl.GetProgramParameterb(p, gl.LINK_STATUS) {
return Program{nil}, errors.New("program error") return Program{nil}, errors.New("opengl: program error")
} }
return Program{p}, nil return Program{p}, nil
} }

1
run.go
View File

@ -64,6 +64,7 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
gameTime := ui.Now() gameTime := ui.Now()
before := ui.Now() before := ui.Now()
for { for {
// TODO: setSize should be called after swapping buffers?
if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight || 0 < runContext.newScreenScale { if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight || 0 < runContext.newScreenScale {
changed := false changed := false
actualScale := 0 actualScale := 0