ebiten/graphics/opengl/context.go

113 lines
2.9 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"
"math"
)
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 {
context := &Context{
2013-12-09 15:52:14 +01:00
ids: ids,
offscreen: offscreen.New(screenWidth, screenHeight, screenScale),
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())
}
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() {
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) {
2013-12-09 14:40:54 +01:00
const max = float64(math.MaxUint8)
C.glClearColor(
C.GLclampf(float64(r)/max),
C.GLclampf(float64(g)/max),
C.GLclampf(float64(b)/max),
1)
C.glClear(C.GL_COLOR_BUFFER_BIT)
}
2013-12-13 22:10:24 +01:00
func (context *Context) DrawTexture(
2013-12-09 14:40:54 +01:00
id graphics.TextureId,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
2013-12-13 22:10:24 +01:00
tex := context.ids.TextureAt(id)
context.offscreen.DrawTexture(tex, geometryMatrix, colorMatrix)
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,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
2013-12-13 22:10:24 +01:00
context.DrawTexture(context.ids.ToTexture(id), geometryMatrix, colorMatrix)
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) {
2013-12-13 22:10:24 +01:00
tex := context.ids.TextureAt(id)
context.offscreen.DrawTextureParts(tex, 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) {
2013-12-13 22:10:24 +01:00
context.DrawTextureParts(context.ids.ToTexture(id), 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) {
renderTarget := context.ids.RenderTargetAt(renderTargetId)
context.offscreen.Set(renderTarget)
2013-12-09 14:40:54 +01:00
}