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