mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
Reduce panics (#16)
This commit is contained in:
parent
d8ffa6388a
commit
22035e79fe
@ -64,15 +64,16 @@ func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c col
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) {
|
// DebugPrint prints the given text str on the given image r.
|
||||||
|
func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) error {
|
||||||
if d.textImage == nil {
|
if d.textImage == nil {
|
||||||
img, err := assets.TextImage()
|
img, err := assets.TextImage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
d.textImage, err = ebiten.NewImageFromImage(img, ebiten.FilterNearest)
|
d.textImage, err = ebiten.NewImageFromImage(img, ebiten.FilterNearest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if d.debugPrintRenderTarget == nil {
|
if d.debugPrintRenderTarget == nil {
|
||||||
@ -80,9 +81,10 @@ func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) {
|
|||||||
var err error
|
var err error
|
||||||
d.debugPrintRenderTarget, err = ebiten.NewImage(width, height, ebiten.FilterNearest)
|
d.debugPrintRenderTarget, err = ebiten.NewImage(width, height, ebiten.FilterNearest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
d.drawText(r, str, 1, d.y+1, color.NRGBA{0x00, 0x00, 0x00, 0x80})
|
d.drawText(r, str, 1, d.y+1, color.NRGBA{0x00, 0x00, 0x00, 0x80})
|
||||||
d.drawText(r, str, 0, d.y, color.NRGBA{0xff, 0xff, 0xff, 0xff})
|
d.drawText(r, str, 0, d.y, color.NRGBA{0xff, 0xff, 0xff, 0xff})
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
5
image.go
5
image.go
@ -75,8 +75,7 @@ func (i *innerImage) drawImage(img *innerImage, options *DrawImageOptions) error
|
|||||||
w, h := img.texture.Size()
|
w, h := img.texture.Size()
|
||||||
quads := textureQuads(parts, w, h)
|
quads := textureQuads(parts, w, h)
|
||||||
projectionMatrix := i.framebuffer.ProjectionMatrix()
|
projectionMatrix := i.framebuffer.ProjectionMatrix()
|
||||||
shader.DrawTexture(img.texture.Native(), projectionMatrix, quads, geo, clr)
|
return shader.DrawTexture(img.texture.Native(), projectionMatrix, quads, geo, clr)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func u(x float64, width int) float32 {
|
func u(x float64, width int) float32 {
|
||||||
@ -200,7 +199,7 @@ type ImagePart struct {
|
|||||||
Src image.Rectangle
|
Src image.Rectangle
|
||||||
}
|
}
|
||||||
|
|
||||||
// A DrawImageOptions presents options to render an image on an image.
|
// A DrawImageOptions represents options to render an image on an image.
|
||||||
type DrawImageOptions struct {
|
type DrawImageOptions struct {
|
||||||
Parts []ImagePart
|
Parts []ImagePart
|
||||||
GeoM GeoM
|
GeoM GeoM
|
||||||
|
@ -40,11 +40,13 @@ const size = 10000
|
|||||||
const uint16Size = 2
|
const uint16Size = 2
|
||||||
const short32Size = 4
|
const short32Size = 4
|
||||||
|
|
||||||
func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads []TextureQuad, geo Matrix, color Matrix) {
|
func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads []TextureQuad, geo Matrix, color Matrix) error {
|
||||||
// TODO: Check len(quads) and gl.MAX_ELEMENTS_INDICES?
|
// TODO: Check len(quads) and gl.MAX_ELEMENTS_INDICES?
|
||||||
const stride = 4 * 4
|
const stride = 4 * 4
|
||||||
if !initialized {
|
if !initialized {
|
||||||
initialize()
|
if err := initialize(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
vertexBuffer := gl.GenBuffer()
|
vertexBuffer := gl.GenBuffer()
|
||||||
vertexBuffer.Bind(gl.ARRAY_BUFFER)
|
vertexBuffer.Bind(gl.ARRAY_BUFFER)
|
||||||
@ -68,7 +70,7 @@ func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads []Text
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(quads) == 0 {
|
if len(quads) == 0 {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
// TODO: Check performance
|
// TODO: Check performance
|
||||||
program := useProgramColorMatrix(glMatrix(projectionMatrix), geo, color)
|
program := useProgramColorMatrix(glMatrix(projectionMatrix), geo, color)
|
||||||
@ -110,4 +112,5 @@ func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads []Text
|
|||||||
gl.DrawElements(gl.TRIANGLES, 6*len(quads), gl.UNSIGNED_SHORT, uintptr(0))
|
gl.DrawElements(gl.TRIANGLES, 6*len(quads), gl.UNSIGNED_SHORT, uintptr(0))
|
||||||
|
|
||||||
gl.Flush()
|
gl.Flush()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package shader
|
package shader
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/go-gl/gl"
|
"github.com/go-gl/gl"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -27,10 +28,10 @@ var programColorMatrix = program{
|
|||||||
shaderIds: []shaderId{shaderVertex, shaderColorMatrix},
|
shaderIds: []shaderId{shaderVertex, shaderColorMatrix},
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *program) create() {
|
func (p *program) create() error {
|
||||||
p.native = gl.CreateProgram()
|
p.native = gl.CreateProgram()
|
||||||
if p.native == 0 {
|
if p.native == 0 {
|
||||||
panic("glCreateProgram failed")
|
return errors.New("glCreateProgram failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, shaderId := range p.shaderIds {
|
for _, shaderId := range p.shaderIds {
|
||||||
@ -38,13 +39,16 @@ func (p *program) create() {
|
|||||||
}
|
}
|
||||||
p.native.Link()
|
p.native.Link()
|
||||||
if p.native.Get(gl.LINK_STATUS) == gl.FALSE {
|
if p.native.Get(gl.LINK_STATUS) == gl.FALSE {
|
||||||
panic("program error")
|
return errors.New("program error")
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialize() {
|
func initialize() error {
|
||||||
for _, shader := range shaders {
|
for _, shader := range shaders {
|
||||||
shader.compile()
|
if err := shader.compile(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
for _, shader := range shaders {
|
for _, shader := range shaders {
|
||||||
@ -52,7 +56,7 @@ func initialize() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
programColorMatrix.create()
|
return programColorMatrix.create()
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAttributeLocation(program gl.Program, name string) gl.AttribLocation {
|
func getAttributeLocation(program gl.Program, name string) gl.AttribLocation {
|
||||||
|
@ -15,8 +15,9 @@
|
|||||||
package shader
|
package shader
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/go-gl/gl"
|
"github.com/go-gl/gl"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type shader struct {
|
type shader struct {
|
||||||
@ -75,26 +76,26 @@ void main(void) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *shader) compile() {
|
func (s *shader) compile() error {
|
||||||
s.native = gl.CreateShader(s.shaderType)
|
s.native = gl.CreateShader(s.shaderType)
|
||||||
if s.native == 0 {
|
if s.native == 0 {
|
||||||
panic("glCreateShader failed")
|
return errors.New("glCreateShader failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
s.native.Source(s.source)
|
s.native.Source(s.source)
|
||||||
s.native.Compile()
|
s.native.Compile()
|
||||||
|
|
||||||
if s.native.Get(gl.COMPILE_STATUS) == gl.FALSE {
|
if s.native.Get(gl.COMPILE_STATUS) == gl.FALSE {
|
||||||
s.showShaderLog()
|
return errors.New(fmt.Sprintf("shader compile failed: %s", s.shaderLog()))
|
||||||
panic("shader compile failed")
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *shader) showShaderLog() {
|
func (s *shader) shaderLog() string {
|
||||||
if s.native.Get(gl.INFO_LOG_LENGTH) == 0 {
|
if s.native.Get(gl.INFO_LOG_LENGTH) == 0 {
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
log.Fatalf("shader error: %s\n", s.native.GetInfoLog())
|
return s.native.GetInfoLog()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *shader) delete() {
|
func (s *shader) delete() {
|
||||||
|
@ -59,10 +59,10 @@ func (t *Texture) Size() (width, height int) {
|
|||||||
return t.width, t.height
|
return t.width, t.height
|
||||||
}
|
}
|
||||||
|
|
||||||
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter Filter) gl.Texture {
|
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter Filter) (gl.Texture, error) {
|
||||||
nativeTexture := gl.GenTexture()
|
nativeTexture := gl.GenTexture()
|
||||||
if nativeTexture < 0 {
|
if nativeTexture < 0 {
|
||||||
panic("glGenTexture failed")
|
return 0, errors.New("glGenTexture failed")
|
||||||
}
|
}
|
||||||
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
||||||
nativeTexture.Bind(gl.TEXTURE_2D)
|
nativeTexture.Bind(gl.TEXTURE_2D)
|
||||||
@ -73,7 +73,7 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter
|
|||||||
|
|
||||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
|
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
|
||||||
|
|
||||||
return nativeTexture
|
return nativeTexture, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTexture(width, height int, filter Filter) (*Texture, error) {
|
func NewTexture(width, height int, filter Filter) (*Texture, error) {
|
||||||
@ -85,7 +85,10 @@ func NewTexture(width, height int, filter Filter) (*Texture, error) {
|
|||||||
if h < 4 {
|
if h < 4 {
|
||||||
return nil, errors.New("height must be equal or more than 4.")
|
return nil, errors.New("height must be equal or more than 4.")
|
||||||
}
|
}
|
||||||
native := createNativeTexture(w, h, nil, filter)
|
native, err := createNativeTexture(w, h, nil, filter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &Texture{native, width, height}, nil
|
return &Texture{native, width, height}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +102,10 @@ func NewTextureFromImage(img image.Image, filter Filter) (*Texture, error) {
|
|||||||
}
|
}
|
||||||
adjustedImage := adjustImageForTexture(img)
|
adjustedImage := adjustImageForTexture(img)
|
||||||
size := adjustedImage.Bounds().Size()
|
size := adjustedImage.Bounds().Size()
|
||||||
native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter)
|
native, err := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &Texture{native, origSize.X, origSize.Y}, nil
|
return &Texture{native, origSize.X, origSize.Y}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user