diff --git a/gamecontext.go b/gamecontext.go index 4f904b79d..943d88b1e 100644 --- a/gamecontext.go +++ b/gamecontext.go @@ -17,7 +17,6 @@ limitations under the License. package ebiten import ( - "github.com/go-gl/gl" "image" ) @@ -36,23 +35,12 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool { return currentUI.input.isMouseButtonPressed(mouseButton) } -func glFilter(f Filter) int { - switch f { - case FilterNearest: - return gl.NEAREST - case FilterLinear: - return gl.LINEAR - default: - panic("not reached") - } -} - // NewImage returns an empty image. func NewImage(width, height int, filter Filter) (*Image, error) { - return currentUI.newImage(width, height, glFilter(filter)) + return currentUI.newImage(width, height, filter) } // NewImageFromImage creates a new image with the given image (img). func NewImageFromImage(img image.Image, filter Filter) (*Image, error) { - return currentUI.newImageFromImage(img, glFilter(filter)) + return currentUI.newImageFromImage(img, filter) } diff --git a/graphics.go b/graphics.go index b924a6311..e3ab0f63d 100644 --- a/graphics.go +++ b/graphics.go @@ -16,6 +16,10 @@ limitations under the License. package ebiten +import ( + "github.com/hajimehoshi/ebiten/internal/opengl" +) + // A Rect represents a rectangle. type Rect struct { X float64 @@ -40,10 +44,10 @@ func DrawWholeImage(target *Image, image *Image, geo GeometryMatrix, color Color } // Filter represents the type of filter to be used when an image is maginified or minified. -type Filter int +type Filter opengl.Filter // Filters const ( - FilterNearest Filter = iota - FilterLinear + FilterNearest = Filter(opengl.FilterNearest) + FilterLinear = Filter(opengl.FilterLinear) ) diff --git a/graphicscontext.go b/graphicscontext.go index 208fac797..353a2e267 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -17,7 +17,6 @@ limitations under the License. package ebiten import ( - "github.com/go-gl/gl" "github.com/hajimehoshi/ebiten/internal/opengl" ) @@ -27,11 +26,11 @@ func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsCo return nil, err } - texture, err := opengl.NewTexture(screenWidth, screenHeight, gl.NEAREST) + texture, err := opengl.NewTexture(screenWidth, screenHeight, opengl.Filter(FilterNearest)) if err != nil { return nil, err } - screen, err := newInnerImage(texture, gl.NEAREST) + screen, err := newInnerImage(texture) if err != nil { return nil, err } @@ -79,6 +78,6 @@ func (c *graphicsContext) postUpdate() error { return err } - gl.Flush() + opengl.Flush() return nil } diff --git a/image.go b/image.go index 56cb1cbd5..4453ff0c1 100644 --- a/image.go +++ b/image.go @@ -17,7 +17,6 @@ limitations under the License. package ebiten import ( - "github.com/go-gl/gl" "github.com/hajimehoshi/ebiten/internal" "github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl/internal/shader" @@ -30,7 +29,7 @@ type innerImage struct { texture *opengl.Texture } -func newInnerImage(texture *opengl.Texture, filter int) (*innerImage, error) { +func newInnerImage(texture *opengl.Texture) (*innerImage, error) { framebuffer, err := opengl.NewFramebufferFromTexture(texture) if err != nil { return nil, err @@ -50,9 +49,8 @@ func (i *innerImage) Fill(clr color.Color) error { if err := i.framebuffer.SetAsViewport(); err != nil { return err } - rf, gf, bf, af := internal.RGBA(clr) - gl.ClearColor(gl.GLclampf(rf), gl.GLclampf(gf), gl.GLclampf(bf), gl.GLclampf(af)) - gl.Clear(gl.COLOR_BUFFER_BIT) + r, g, b, a := internal.RGBA(clr) + opengl.Clear(r, g, b, a) return nil } diff --git a/internal/opengl/gl.go b/internal/opengl/gl.go new file mode 100644 index 000000000..f8f0c4d3a --- /dev/null +++ b/internal/opengl/gl.go @@ -0,0 +1,45 @@ +/* +Copyright 2014 Hajime Hoshi + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package opengl + +import ( + "github.com/go-gl/gl" +) + +type Filter int + +const ( + FilterNearest Filter = gl.NEAREST + FilterLinear = gl.LINEAR +) + +func Init() { + gl.Init() + gl.Enable(gl.TEXTURE_2D) + // Textures' pixel formats are alpha premultiplied. + gl.Enable(gl.BLEND) + gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) +} + +func Clear(r, g, b, a float64) { + gl.ClearColor(gl.GLclampf(r), gl.GLclampf(g), gl.GLclampf(b), gl.GLclampf(a)) + gl.Clear(gl.COLOR_BUFFER_BIT) +} + +func Flush() { + gl.Flush() +} diff --git a/internal/opengl/texture.go b/internal/opengl/texture.go index 1890fbf4f..433d7c1e9 100644 --- a/internal/opengl/texture.go +++ b/internal/opengl/texture.go @@ -61,7 +61,7 @@ func (t *Texture) Size() (width, height int) { return t.width, t.height } -func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter int) gl.Texture { +func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter Filter) gl.Texture { nativeTexture := gl.GenTexture() if nativeTexture < 0 { panic("glGenTexture failed") @@ -70,15 +70,15 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter nativeTexture.Bind(gl.TEXTURE_2D) defer gl.Texture(0).Bind(gl.TEXTURE_2D) - gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter) - gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter) + gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int(filter)) + gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int(filter)) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels) return nativeTexture } -func NewTexture(width, height int, filter int) (*Texture, error) { +func NewTexture(width, height int, filter Filter) (*Texture, error) { w := internal.NextPowerOf2Int(width) h := internal.NextPowerOf2Int(height) if w < 4 { @@ -91,7 +91,7 @@ func NewTexture(width, height int, filter int) (*Texture, error) { return &Texture{native, width, height}, nil } -func NewTextureFromImage(img image.Image, filter int) (*Texture, error) { +func NewTextureFromImage(img image.Image, filter Filter) (*Texture, error) { origSize := img.Bounds().Size() if origSize.X < 4 { return nil, errors.New("width must be equal or more than 4.") diff --git a/ui.go b/ui.go index 43ab86fb9..6e575c24f 100644 --- a/ui.go +++ b/ui.go @@ -18,7 +18,6 @@ package ebiten import ( "fmt" - "github.com/go-gl/gl" glfw "github.com/go-gl/glfw3" "github.com/hajimehoshi/ebiten/internal/opengl" "image" @@ -50,11 +49,7 @@ func init() { } currentUI.run() currentUI.use(func() { - gl.Init() - gl.Enable(gl.TEXTURE_2D) - // Textures' pixel formats are alpha premultiplied. - gl.Enable(gl.BLEND) - gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) + opengl.Init() }) } @@ -134,16 +129,16 @@ func (u *ui) draw(f func(*Image) error) (err error) { return } -func (u *ui) newImageFromImage(img image.Image, filter int) (*Image, error) { +func (u *ui) newImageFromImage(img image.Image, filter Filter) (*Image, error) { var innerImage *innerImage var err error u.use(func() { var texture *opengl.Texture - texture, err = opengl.NewTextureFromImage(img, filter) + texture, err = opengl.NewTextureFromImage(img, opengl.Filter(filter)) if err != nil { return } - innerImage, err = newInnerImage(texture, filter) + innerImage, err = newInnerImage(texture) }) if err != nil { return nil, err @@ -151,16 +146,16 @@ func (u *ui) newImageFromImage(img image.Image, filter int) (*Image, error) { return &Image{syncer: u, inner: innerImage}, nil } -func (u *ui) newImage(width, height int, filter int) (*Image, error) { +func (u *ui) newImage(width, height int, filter Filter) (*Image, error) { var innerImage *innerImage var err error u.use(func() { var texture *opengl.Texture - texture, err = opengl.NewTexture(width, height, filter) + texture, err = opengl.NewTexture(width, height, opengl.Filter(filter)) if err != nil { return } - innerImage, err = newInnerImage(texture, filter) + innerImage, err = newInnerImage(texture) innerImage.Clear() }) if err != nil {