mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 02:42:02 +01:00
Reduce depndencies on go-gl/gl
This commit is contained in:
parent
3e576c97f3
commit
27fe3d9ab8
@ -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)
|
||||
}
|
||||
|
10
graphics.go
10
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)
|
||||
)
|
||||
|
@ -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
|
||||
}
|
||||
|
8
image.go
8
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
|
||||
}
|
||||
|
||||
|
45
internal/opengl/gl.go
Normal file
45
internal/opengl/gl.go
Normal file
@ -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()
|
||||
}
|
@ -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.")
|
||||
|
19
ui.go
19
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user