ebiten/graphics/opengl/context.go

108 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"
"github.com/hajimehoshi/go-ebiten/graphics/opengl/offscreen"
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
ids *ids
offscreen *offscreen.Offscreen
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,
2014-01-10 13:28:50 +01:00
offscreen: offscreen.New(mainFramebuffer),
2013-12-09 15:52:14 +01:00
screenScale: screenScale,
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
2013-12-11 17:46:32 +01:00
C.glFlush()
2013-12-13 22:10:24 +01:00
context.offscreen.SetMainFramebuffer()
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:20:05 +01:00
context.offscreen.Fill(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) {
context.ids.DrawTexture(id, context.offscreen, 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) {
context.ids.DrawRenderTarget(id, context.offscreen, 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-09 16:42:42 +01:00
context.ids.DrawTextureParts(id, context.offscreen, 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-09 16:42:42 +01:00
context.ids.DrawRenderTargetParts(id, context.offscreen, parts, geometryMatrix, colorMatrix)
2013-12-09 14:40:54 +01:00
}
2013-12-13 22:10:24 +01:00
func (context *Context) ResetOffscreen() {
context.SetOffscreen(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-09 16:42:42 +01:00
context.ids.SetRenderTargetAsOffscreen(renderTargetId, context.offscreen)
2013-12-09 14:40:54 +01:00
}