ebiten/graphics/opengl/canvas.go

121 lines
3.1 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"
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
"math"
)
type Canvas 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-09 15:52:14 +01:00
func newCanvas(ids *ids, screenWidth, screenHeight, screenScale int) *Canvas {
2013-12-09 14:40:54 +01:00
canvas := &Canvas{
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-09 15:52:14 +01:00
canvas.screenId, err = ids.CreateRenderTarget(
2013-12-09 14:40:54 +01:00
screenWidth, screenHeight, texture.FilterNearest)
if err != nil {
panic("initializing the offscreen failed: " + err.Error())
}
return canvas
}
2013-12-09 15:52:14 +01:00
func (canvas *Canvas) update(draw func(graphics.Canvas)) {
canvas.init()
canvas.ResetOffscreen()
canvas.Clear()
draw(canvas)
canvas.flush()
canvas.setMainFramebufferOffscreen()
canvas.Clear()
scale := float64(canvas.screenScale)
geometryMatrix := matrix.IdentityGeometry()
geometryMatrix.Scale(scale, scale)
canvas.DrawRenderTarget(canvas.screenId,
geometryMatrix, matrix.IdentityColor())
canvas.flush()
}
2013-12-09 14:40:54 +01:00
func (canvas *Canvas) Clear() {
canvas.Fill(0, 0, 0)
}
func (canvas *Canvas) Fill(r, g, b uint8) {
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)
}
func (canvas *Canvas) DrawTexture(
id graphics.TextureId,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
tex := canvas.ids.TextureAt(id)
canvas.offscreen.DrawTexture(tex, geometryMatrix, colorMatrix)
}
func (canvas *Canvas) DrawRenderTarget(
id graphics.RenderTargetId,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
canvas.DrawTexture(canvas.ids.ToTexture(id), geometryMatrix, colorMatrix)
}
func (canvas *Canvas) DrawTextureParts(
id graphics.TextureId, parts []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
tex := canvas.ids.TextureAt(id)
canvas.offscreen.DrawTextureParts(tex, parts, geometryMatrix, colorMatrix)
}
func (canvas *Canvas) DrawRenderTargetParts(
id graphics.RenderTargetId, parts []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
canvas.DrawTextureParts(canvas.ids.ToTexture(id), parts, geometryMatrix, colorMatrix)
}
2013-12-09 15:52:14 +01:00
// init initializes the canvas. The initial state is saved for each GL canvas.
func (canvas *Canvas) init() {
2013-12-09 14:40:54 +01:00
C.glEnable(C.GL_TEXTURE_2D)
C.glEnable(C.GL_BLEND)
}
func (canvas *Canvas) ResetOffscreen() {
canvas.SetOffscreen(canvas.screenId)
}
func (canvas *Canvas) SetOffscreen(renderTargetId graphics.RenderTargetId) {
renderTarget := canvas.ids.RenderTargetAt(renderTargetId)
canvas.offscreen.Set(renderTarget)
}
func (canvas *Canvas) setMainFramebufferOffscreen() {
canvas.offscreen.SetMainFramebuffer()
}
func (canvas *Canvas) flush() {
C.glFlush()
}