ebiten/graphics/opengl/context.go

102 lines
2.4 KiB
Go
Raw Normal View History

2013-12-09 14:40:54 +01:00
package opengl
import (
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
)
2013-12-13 22:10:24 +01:00
type Context struct {
2013-12-09 15:52:14 +01:00
screenId graphics.RenderTargetId
2014-01-11 00:59:38 +01:00
mainId graphics.RenderTargetId
currentId graphics.RenderTargetId
2013-12-09 15:52:14 +01:00
ids *ids
screenScale int
2013-12-09 14:40:54 +01:00
}
2013-12-13 22:10:24 +01:00
func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context {
context := &Context{
2013-12-09 15:52:14 +01:00
ids: ids,
screenScale: screenScale,
2013-12-09 14:40:54 +01:00
}
2014-05-01 14:42:57 +02:00
mainRenderTarget := newRTWithCurrentFramebuffer(
2014-01-11 07:18:04 +01:00
screenWidth*screenScale,
screenHeight*screenScale)
2014-05-02 17:06:20 +02:00
context.mainId = context.ids.addRenderTarget(mainRenderTarget)
2013-12-09 14:40:54 +01:00
var err error
2014-05-02 17:06:20 +02:00
context.screenId, err = ids.createRenderTarget(
2013-12-18 10:05:28 +01:00
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
2014-01-11 03:18:55 +01:00
enableAlphaBlending()
2013-12-13 22:10:24 +01:00
return context
2013-12-09 14:40:54 +01:00
}
func (context *Context) Dispose() {
2014-01-11 00:20:05 +01:00
// TODO: remove main framebuffer?
2014-05-02 17:06:20 +02:00
context.ids.deleteRenderTarget(context.screenId)
}
2014-01-11 03:42:23 +01:00
func (context *Context) Update(draw func(graphics.Context)) {
2013-12-13 22:10:24 +01:00
context.ResetOffscreen()
context.Clear()
2013-12-09 15:52:14 +01:00
2013-12-13 22:10:24 +01:00
draw(context)
2013-12-09 15:52:14 +01:00
2014-01-11 00:59:38 +01:00
context.SetOffscreen(context.mainId)
2013-12-13 22:10:24 +01:00
context.Clear()
2013-12-09 15:52:14 +01:00
2013-12-13 22:10:24 +01:00
scale := float64(context.screenScale)
2013-12-09 15:52:14 +01:00
geometryMatrix := matrix.IdentityGeometry()
geometryMatrix.Scale(scale, scale)
2014-05-02 17:06:20 +02:00
context.RenderTarget(context.screenId).Draw(
geometryMatrix, matrix.IdentityColor())
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-05-02 17:06:20 +02:00
c.ids.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 {
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-05-02 17:06:20 +02:00
return &TextureWithContext{c.ids.toTexture(id), c}
2013-12-09 14:40:54 +01:00
}
2013-12-13 22:10:24 +01:00
func (context *Context) ResetOffscreen() {
2014-01-11 00:59:38 +01:00
context.currentId = context.screenId
2013-12-09 14:40:54 +01:00
}
2013-12-13 22:10:24 +01:00
func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
2014-01-11 00:59:38 +01:00
context.currentId = renderTargetId
2013-12-09 14:40:54 +01:00
}
2014-05-01 15:45:48 +02:00
type TextureWithContext struct {
id graphics.TextureId
context *Context
}
func (t *TextureWithContext) Draw(geo matrix.Geometry, color matrix.Color) {
2014-05-02 17:06:20 +02:00
t.context.ids.drawTexture(t.context.currentId, t.id, geo, color)
2014-05-01 15:45:48 +02:00
}
2014-05-02 17:06:20 +02:00
func (t *TextureWithContext) DrawParts(
parts []graphics.TexturePart,
geo matrix.Geometry,
color matrix.Color) {
t.context.ids.drawTextureParts(t.context.currentId, t.id, parts, geo, color)
2014-05-01 15:45:48 +02:00
}