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 07:47:48 +01:00
|
|
|
"sync"
|
2014-05-03 20:29:45 +02:00
|
|
|
)
|
2014-05-03 17:40:53 +02:00
|
|
|
|
|
|
|
func flush() {
|
2014-12-06 07:47:48 +01:00
|
|
|
gl.Flush()
|
2014-05-03 17:40:53 +02:00
|
|
|
}
|
|
|
|
|
2014-12-06 07:47:48 +01:00
|
|
|
var onceInit sync.Once
|
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
type Context struct {
|
2014-05-03 06:58:18 +02:00
|
|
|
screenId graphics.RenderTargetId
|
2014-05-03 19:13:43 +02:00
|
|
|
defaultId graphics.RenderTargetId
|
2014-05-03 06:58:18 +02:00
|
|
|
currentId graphics.RenderTargetId
|
|
|
|
screenWidth int
|
|
|
|
screenHeight int
|
|
|
|
screenScale int
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2014-10-12 07:07:44 +02: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)
|
|
|
|
})
|
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
context := &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-05-03 19:13:43 +02:00
|
|
|
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-10-12 07:07:44 +02:00
|
|
|
context.defaultId = idsInstance.addRenderTarget(defaultRenderTarget)
|
2013-12-09 14:40:54 +01:00
|
|
|
|
|
|
|
var err error
|
2014-12-05 18:26:02 +01:00
|
|
|
context.screenId, err = idsInstance.createRenderTarget(screenWidth, screenHeight, graphics.FilterNearest)
|
2013-12-09 14:40:54 +01:00
|
|
|
if err != nil {
|
|
|
|
panic("initializing the offscreen failed: " + err.Error())
|
|
|
|
}
|
2014-01-11 00:20:05 +01:00
|
|
|
context.ResetOffscreen()
|
2013-12-18 10:05:28 +01:00
|
|
|
context.Clear()
|
2013-12-09 14:40:54 +01:00
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
return context
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2014-05-03 06:58:18 +02:00
|
|
|
func (c *Context) Dispose() {
|
2014-05-03 19:13:43 +02:00
|
|
|
// TODO: remove the default framebuffer?
|
2014-10-12 07:07:44 +02:00
|
|
|
idsInstance.deleteRenderTarget(c.screenId)
|
2014-01-07 13:58:46 +01:00
|
|
|
}
|
|
|
|
|
2014-12-05 18:26:02 +01:00
|
|
|
// TODO: This interface is confusing: Can we change this?
|
2014-05-03 06:58:18 +02:00
|
|
|
func (c *Context) Update(draw func(graphics.Context)) {
|
|
|
|
c.ResetOffscreen()
|
|
|
|
c.Clear()
|
2013-12-09 15:52:14 +01:00
|
|
|
|
2014-05-03 06:58:18 +02:00
|
|
|
draw(c)
|
2013-12-09 15:52:14 +01:00
|
|
|
|
2014-05-03 19:13:43 +02:00
|
|
|
c.SetOffscreen(c.defaultId)
|
2014-05-03 06:58:18 +02:00
|
|
|
c.Clear()
|
2013-12-09 15:52:14 +01:00
|
|
|
|
2014-05-03 06:58:18 +02:00
|
|
|
scale := float64(c.screenScale)
|
2014-12-06 15:03:17 +01:00
|
|
|
geo := matrix.GeometryI()
|
2014-05-03 06:58:18 +02:00
|
|
|
geo.Scale(scale, scale)
|
2014-12-06 15:03:17 +01:00
|
|
|
graphics.DrawWhole(c.RenderTarget(c.screenId), c.screenWidth, c.screenHeight, geo, matrix.ColorI())
|
2014-01-11 03:18:55 +01:00
|
|
|
|
|
|
|
flush()
|
2013-12-09 15:52:14 +01:00
|
|
|
}
|
|
|
|
|
2014-05-01 15:45:48 +02:00
|
|
|
func (c *Context) Clear() {
|
|
|
|
c.Fill(0, 0, 0)
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2014-05-01 15:45:48 +02: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-05-01 15:45:48 +02: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-05-01 15:45:48 +02: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-05-03 06:58:18 +02:00
|
|
|
func (c *Context) ResetOffscreen() {
|
|
|
|
c.currentId = c.screenId
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2014-05-03 06:58:18 +02:00
|
|
|
func (c *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
|
|
|
|
c.currentId = renderTargetId
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
2014-05-01 15:45:48 +02:00
|
|
|
|
2014-05-03 08:25:41 +02:00
|
|
|
type textureWithContext struct {
|
2014-05-01 15:45:48 +02:00
|
|
|
id graphics.TextureId
|
|
|
|
context *Context
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|