ebiten/graphics/opengl/context.go

107 lines
2.8 KiB
Go
Raw Normal View History

2013-12-09 14:40:54 +01:00
package opengl
// #cgo LDFLAGS: -framework OpenGL
//
// #include <stdlib.h>
// #include <OpenGL/gl.h>
import "C"
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 {
2014-01-10 13:28:50 +01:00
mainFramebuffer := rendertarget.NewWithCurrentFramebuffer(
screenWidth*screenScale,
screenHeight*screenScale)
2013-12-13 22:10:24 +01:00
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 00:59:38 +01:00
context.mainId = context.ids.AddRenderTarget(mainFramebuffer)
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
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?
context.ids.DeleteRenderTarget(context.screenId)
}
2013-12-13 22:10:24 +01:00
func (context *Context) update(draw func(graphics.Context)) {
2013-12-11 17:46:32 +01:00
C.glEnable(C.GL_TEXTURE_2D)
C.glEnable(C.GL_BLEND)
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())
2013-12-11 17:46:32 +01:00
C.glFlush()
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
}