2013-12-09 14:40:54 +01:00
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
2014-01-10 13:28:50 +01:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget"
|
2013-12-09 14:40:54 +01:00
|
|
|
)
|
|
|
|
|
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-01-11 07:18:04 +01:00
|
|
|
mainRenderTarget := rendertarget.NewWithCurrentFramebuffer(
|
|
|
|
screenWidth*screenScale,
|
|
|
|
screenHeight*screenScale)
|
|
|
|
context.mainId = context.ids.AddRenderTarget(mainRenderTarget)
|
2013-12-09 14:40:54 +01:00
|
|
|
|
|
|
|
var err error
|
2013-12-13 22:10:24 +01: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
|
|
|
}
|
|
|
|
|
2014-01-07 13:58:46 +01:00
|
|
|
func (context *Context) Dispose() {
|
2014-01-11 00:20:05 +01:00
|
|
|
// TODO: remove main framebuffer?
|
2014-01-07 13:58:46 +01: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)
|
2013-12-13 22:10:24 +01:00
|
|
|
context.DrawRenderTarget(context.screenId,
|
2013-12-09 15:52:14 +01:00
|
|
|
geometryMatrix, matrix.IdentityColor())
|
2014-01-11 03:18:55 +01:00
|
|
|
|
|
|
|
flush()
|
2013-12-09 15:52:14 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
func (context *Context) Clear() {
|
|
|
|
context.Fill(0, 0, 0)
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
func (context *Context) Fill(r, g, b uint8) {
|
2014-01-11 00:59:38 +01:00
|
|
|
context.ids.FillRenderTarget(context.currentId, r, g, b)
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
func (context *Context) DrawTexture(
|
2014-01-09 16:42:42 +01:00
|
|
|
id graphics.TextureId, geo matrix.Geometry, color matrix.Color) {
|
2014-01-11 00:59:38 +01:00
|
|
|
context.ids.DrawTexture(context.currentId, id, geo, color)
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
func (context *Context) DrawRenderTarget(
|
2013-12-09 14:40:54 +01:00
|
|
|
id graphics.RenderTargetId,
|
2014-01-09 16:42:42 +01:00
|
|
|
geo matrix.Geometry, color matrix.Color) {
|
2014-01-11 00:59:38 +01:00
|
|
|
context.ids.DrawRenderTarget(context.currentId, id, geo, color)
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
func (context *Context) DrawTextureParts(
|
2013-12-09 14:40:54 +01:00
|
|
|
id graphics.TextureId, parts []graphics.TexturePart,
|
|
|
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
2014-01-11 00:59:38 +01:00
|
|
|
context.ids.DrawTextureParts(context.currentId, id, parts, geometryMatrix, colorMatrix)
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
func (context *Context) DrawRenderTargetParts(
|
2013-12-09 14:40:54 +01:00
|
|
|
id graphics.RenderTargetId, parts []graphics.TexturePart,
|
|
|
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
2014-01-11 00:59:38 +01:00
|
|
|
context.ids.DrawRenderTargetParts(context.currentId, id, parts, geometryMatrix, colorMatrix)
|
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
|
|
|
}
|