ebiten/graphics/opengl/context.go

125 lines
2.8 KiB
Go
Raw Normal View History

2013-12-09 14:40:54 +01:00
package opengl
2014-05-03 20:29:45 +02:00
import (
2014-12-06 07:47:48 +01:00
"github.com/go-gl/gl"
2014-12-05 14:16:58 +01:00
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/matrix"
2014-12-06 20:14:35 +01:00
"github.com/hajimehoshi/ebiten/ui"
2014-12-06 07:47:48 +01:00
"sync"
2014-05-03 20:29:45 +02:00
)
2014-05-03 17:40:53 +02:00
2014-12-07 16:07:36 +01:00
type ContextUpdater struct {
context *context
}
func NewContextUpdater(screenWidth, screenHeight, screenScale int) *ContextUpdater {
return &ContextUpdater{
context: newContext(screenWidth, screenHeight, screenScale),
}
}
func (u *ContextUpdater) Update(drawer ui.Drawer) error {
return u.context.update(drawer)
2014-05-03 17:40:53 +02:00
}
2014-12-06 07:47:48 +01:00
var onceInit sync.Once
2014-12-07 16:07:36 +01:00
type context struct {
2014-12-06 21:21:20 +01:00
screenId graphics.RenderTargetID
defaultId graphics.RenderTargetID
currentId graphics.RenderTargetID
2014-05-03 06:58:18 +02:00
screenWidth int
screenHeight int
screenScale int
2013-12-09 14:40:54 +01:00
}
2014-12-07 16:07:36 +01:00
func newContext(screenWidth, screenHeight, screenScale int) *context {
2014-12-06 07:47:48 +01:00
onceInit.Do(func() {
gl.Init()
gl.Enable(gl.TEXTURE_2D)
gl.Enable(gl.BLEND)
})
2014-12-07 16:07:36 +01:00
c := &context{
2014-05-03 06:58:18 +02:00
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
2013-12-09 14:40:54 +01:00
}
2014-12-06 07:47:48 +01:00
2014-12-07 16:07:36 +01:00
// The defualt framebuffer should be 0.
defaultRenderTarget := &renderTarget{
2014-10-12 07:07:44 +02:00
width: screenWidth * screenScale,
height: screenHeight * screenScale,
flipY: true,
2014-05-03 19:13:43 +02:00
}
2014-12-07 16:07:36 +01:00
c.defaultId = idsInstance.addRenderTarget(defaultRenderTarget)
2013-12-09 14:40:54 +01:00
var err error
2014-12-07 16:53:23 +01:00
c.screenId, err = idsInstance.newRenderTarget(screenWidth, screenHeight, graphics.FilterNearest)
2013-12-09 14:40:54 +01:00
if err != nil {
2014-12-07 16:53:23 +01:00
panic("opengl.newContext: initializing the offscreen failed: " + err.Error())
2013-12-09 14:40:54 +01:00
}
2014-05-03 06:58:18 +02:00
c.ResetOffscreen()
c.Clear()
2013-12-09 15:52:14 +01:00
2014-12-07 16:07:36 +01:00
return c
}
2014-01-11 03:18:55 +01:00
2014-12-07 16:07:36 +01:00
func (c *context) dispose() {
// NOTE: Now this method is not used anywhere.
idsInstance.deleteRenderTarget(c.screenId)
2013-12-09 15:52:14 +01:00
}
2014-12-07 16:07:36 +01:00
func (c *context) Clear() {
2014-05-01 15:45:48 +02:00
c.Fill(0, 0, 0)
2013-12-09 14:40:54 +01:00
}
2014-12-07 16:07:36 +01:00
func (c *context) Fill(r, g, b uint8) {
2014-10-12 07:07:44 +02:00
idsInstance.fillRenderTarget(c.currentId, r, g, b)
2013-12-09 14:40:54 +01:00
}
2014-12-07 16:07:36 +01:00
func (c *context) Texture(id graphics.TextureID) graphics.Drawer {
2014-05-03 08:25:41 +02:00
return &textureWithContext{id, c}
2013-12-09 14:40:54 +01:00
}
2014-12-07 16:07:36 +01:00
func (c *context) RenderTarget(id graphics.RenderTargetID) graphics.Drawer {
2014-10-12 07:07:44 +02:00
return &textureWithContext{idsInstance.toTexture(id), c}
2013-12-09 14:40:54 +01:00
}
2014-12-07 16:07:36 +01:00
func (c *context) ResetOffscreen() {
2014-05-03 06:58:18 +02:00
c.currentId = c.screenId
2013-12-09 14:40:54 +01:00
}
2014-12-07 16:07:36 +01:00
func (c *context) SetOffscreen(renderTargetId graphics.RenderTargetID) {
2014-05-03 06:58:18 +02:00
c.currentId = renderTargetId
2013-12-09 14:40:54 +01:00
}
2014-05-01 15:45:48 +02:00
2014-12-07 16:07:36 +01:00
func (c *context) update(drawer ui.Drawer) error {
c.ResetOffscreen()
c.Clear()
if err := drawer.Draw(c); err != nil {
return err
}
c.SetOffscreen(c.defaultId)
c.Clear()
scale := float64(c.screenScale)
geo := matrix.GeometryI()
geo.Scale(scale, scale)
graphics.DrawWhole(c.RenderTarget(c.screenId), c.screenWidth, c.screenHeight, geo, matrix.ColorI())
gl.Flush()
return nil
}
2014-05-03 08:25:41 +02:00
type textureWithContext struct {
2014-12-06 21:21:20 +01:00
id graphics.TextureID
2014-12-07 16:07:36 +01:00
context *context
2014-05-01 15:45:48 +02:00
}
2014-12-05 18:26:02 +01:00
func (t *textureWithContext) Draw(parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) {
2014-10-12 07:07:44 +02:00
idsInstance.drawTexture(t.context.currentId, t.id, parts, geo, color)
2014-05-01 15:45:48 +02:00
}