mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Refactoring
This commit is contained in:
parent
a28443b2d9
commit
790a840389
@ -56,8 +56,6 @@ func (context *Context) Init() {
|
||||
panic("creating main framebuffer failed: " + err.Error())
|
||||
}
|
||||
|
||||
shader.Init()
|
||||
|
||||
context.screenId, err = context.NewRenderTarget(
|
||||
context.screenWidth, context.screenHeight)
|
||||
if err != nil {
|
||||
@ -83,36 +81,30 @@ func (context *Context) Fill(r, g, b uint8) {
|
||||
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
||||
}
|
||||
|
||||
type TextureDrawing struct {
|
||||
projectionMatrix [16]float32
|
||||
geometryMatrix matrix.Geometry
|
||||
colorMatrix matrix.Color
|
||||
}
|
||||
|
||||
func (t *TextureDrawing) Draw(native interface{}, quads []texture.Quad) {
|
||||
shader.DrawTexture(uint(native.(C.GLuint)), t.projectionMatrix, quads, t.geometryMatrix, t.colorMatrix)
|
||||
}
|
||||
|
||||
func (context *Context) DrawTexture(
|
||||
textureId graphics.TextureId,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||
texture, ok := context.textures[textureId]
|
||||
tex, ok := context.textures[textureId]
|
||||
if !ok {
|
||||
panic("invalid texture ID")
|
||||
}
|
||||
drawing := &TextureDrawing{context.projectionMatrix, geometryMatrix, colorMatrix}
|
||||
texture.Draw(drawing.Draw)
|
||||
tex.Draw(func(native interface{}, quads []texture.Quad) {
|
||||
shader.DrawTexture(uint(native.(C.GLuint)), context.projectionMatrix, quads,
|
||||
geometryMatrix, colorMatrix)
|
||||
})
|
||||
}
|
||||
|
||||
func (context *Context) DrawTextureParts(
|
||||
textureId graphics.TextureId, parts []graphics.TexturePart,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||
texture, ok := context.textures[textureId]
|
||||
tex, ok := context.textures[textureId]
|
||||
if !ok {
|
||||
panic("invalid texture ID")
|
||||
}
|
||||
drawing := &TextureDrawing{context.projectionMatrix, geometryMatrix, colorMatrix}
|
||||
texture.DrawParts(parts, drawing.Draw)
|
||||
tex.DrawParts(parts, func(native interface{}, quads []texture.Quad) {
|
||||
shader.DrawTexture(uint(native.(C.GLuint)), context.projectionMatrix, quads,
|
||||
geometryMatrix, colorMatrix)
|
||||
})
|
||||
}
|
||||
|
||||
func (context *Context) ResetOffscreen() {
|
||||
@ -170,8 +162,6 @@ func (v *viewportSetter) Set(x, y, width, height int) {
|
||||
v.context.projectionMatrix[i+j*4] = float32(matrix[i][j])
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: call 'setShaderProgram' here?
|
||||
}
|
||||
|
||||
func (context *Context) setMainFramebufferOffscreen() {
|
||||
@ -204,7 +194,7 @@ func (context *Context) NewRenderTarget(width, height int) (
|
||||
|
||||
func (context *Context) NewTextureFromImage(img image.Image) (
|
||||
graphics.TextureId, error) {
|
||||
texture, err := texture.NewFromImage(img, &NativeTextureCreator{})
|
||||
texture, err := texture.NewFromImage(img, createFromImage)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
@ -114,7 +114,11 @@ func createProgram(shaders ...*shader) C.GLuint {
|
||||
return program
|
||||
}
|
||||
|
||||
func Init() {
|
||||
var (
|
||||
initialized = false
|
||||
)
|
||||
|
||||
func initialize() {
|
||||
// TODO: when should this function be called?
|
||||
vertexShader.id = C.glCreateShader(C.GL_VERTEX_SHADER)
|
||||
if vertexShader.id == 0 {
|
||||
@ -139,6 +143,8 @@ func Init() {
|
||||
C.glDeleteShader(vertexShader.id)
|
||||
C.glDeleteShader(fragmentShader.id)
|
||||
C.glDeleteShader(colorMatrixShader.id)
|
||||
|
||||
initialized = true
|
||||
}
|
||||
|
||||
const (
|
||||
@ -246,6 +252,10 @@ func use(projectionMatrix [16]float32, geometryMatrix matrix.Geometry, colorMatr
|
||||
|
||||
func DrawTexture(native uint, projectionMatrix [16]float32, quads []texture.Quad,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||
if !initialized {
|
||||
initialize()
|
||||
}
|
||||
|
||||
if len(quads) == 0 {
|
||||
return
|
||||
}
|
||||
|
@ -36,19 +36,17 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8) C.GLui
|
||||
return nativeTexture
|
||||
}
|
||||
|
||||
type NativeTextureCreator struct{}
|
||||
|
||||
func (creator *NativeTextureCreator) Create(textureWidth, textureHeight int) (interface{}, error) {
|
||||
func create(textureWidth, textureHeight int) (interface{}, error) {
|
||||
return createNativeTexture(textureWidth, textureHeight, nil), nil
|
||||
}
|
||||
|
||||
func (creator *NativeTextureCreator) CreateFromImage(img *image.NRGBA) (interface{}, error) {
|
||||
func createFromImage(img *image.NRGBA) (interface{}, error) {
|
||||
size := img.Bounds().Size()
|
||||
return createNativeTexture(size.X, size.Y, img.Pix), nil
|
||||
}
|
||||
|
||||
func newRenderTarget(width, height int) (*rendertarget.RenderTarget, error) {
|
||||
texture, err := texture.New(width, height, &NativeTextureCreator{})
|
||||
texture, err := texture.New(width, height, create)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -57,7 +55,7 @@ func newRenderTarget(width, height int) (*rendertarget.RenderTarget, error) {
|
||||
}
|
||||
|
||||
func newRenderTargetWithFramebuffer(width, height int, framebuffer C.GLuint) (*rendertarget.RenderTarget, error) {
|
||||
texture, err := texture.New(width, height, &NativeTextureCreator{})
|
||||
texture, err := texture.New(width, height, create)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -23,24 +23,20 @@ type Texture struct {
|
||||
height int
|
||||
}
|
||||
|
||||
func New(width, height int, creator interface {
|
||||
Create(textureWidth, textureHeight int) (interface{}, error)
|
||||
}) (*Texture, error) {
|
||||
func New(width, height int, create func(textureWidth, textureHeight int) (interface{}, error)) (*Texture, error) {
|
||||
texture := &Texture{
|
||||
width: width,
|
||||
height: height,
|
||||
}
|
||||
var err error
|
||||
texture.native, err = creator.Create(texture.textureWidth(), texture.textureHeight())
|
||||
texture.native, err = create(texture.textureWidth(), texture.textureHeight())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return texture, nil
|
||||
}
|
||||
|
||||
func NewFromImage(img image.Image, creator interface {
|
||||
CreateFromImage(img *image.NRGBA) (interface{}, error)
|
||||
}) (*Texture, error) {
|
||||
func NewFromImage(img image.Image, create func(img *image.NRGBA) (interface{}, error)) (*Texture, error) {
|
||||
size := img.Bounds().Size()
|
||||
width, height := size.X, size.Y
|
||||
texture := &Texture{
|
||||
@ -58,7 +54,7 @@ func NewFromImage(img image.Image, creator interface {
|
||||
}
|
||||
draw.Draw(adjustedImage, dstBound, img, image.ZP, draw.Src)
|
||||
var err error
|
||||
texture.native, err = creator.CreateFromImage(adjustedImage)
|
||||
texture.native, err = create(adjustedImage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user