ebiten/graphics/opengl/context.go

122 lines
3.3 KiB
Go
Raw Normal View History

2013-06-19 01:49:54 +02:00
package opengl
2013-06-15 10:07:14 +02:00
// #cgo LDFLAGS: -framework OpenGL
//
// #include <stdlib.h>
2013-06-17 16:10:55 +02:00
// #include <OpenGL/gl.h>
2013-06-15 10:07:14 +02:00
import "C"
import (
2013-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
2013-11-26 17:46:07 +01:00
"github.com/hajimehoshi/go-ebiten/graphics/opengl/offscreen"
2013-10-27 11:54:28 +01:00
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
2013-06-19 16:08:24 +02:00
"image"
2013-06-22 08:56:25 +02:00
"math"
2013-06-15 10:07:14 +02:00
)
2013-06-27 17:33:03 +02:00
type Context struct {
2013-11-27 03:56:44 +01:00
screenId graphics.RenderTargetId
ids *ids
offscreen *offscreen.Offscreen
2013-06-15 10:07:14 +02:00
}
2013-06-27 17:33:03 +02:00
func newContext(screenWidth, screenHeight, screenScale int) *Context {
2013-11-20 16:05:57 +01:00
context := &Context{
2013-11-27 03:56:44 +01:00
ids: newIds(),
offscreen: offscreen.New(screenWidth, screenHeight, screenScale),
2013-06-15 10:07:14 +02:00
}
2013-10-05 18:47:19 +02:00
2013-10-25 02:30:39 +02:00
var err error
2013-10-27 14:58:56 +01:00
context.screenId, err = context.createRenderTarget(
2013-11-27 03:56:44 +01:00
screenWidth, screenHeight, texture.FilterNearest)
2013-10-20 18:13:09 +02:00
if err != nil {
panic("initializing the offscreen failed: " + err.Error())
}
2013-11-20 16:05:57 +01:00
2013-11-25 14:27:59 +01:00
context.Init()
2013-11-20 16:05:57 +01:00
return context
2013-10-16 17:14:32 +02:00
}
2013-06-27 17:33:03 +02:00
func (context *Context) Clear() {
2013-10-11 20:20:13 +02:00
context.Fill(0, 0, 0)
2013-06-15 10:07:14 +02:00
}
2013-10-11 20:20:13 +02:00
func (context *Context) Fill(r, g, b uint8) {
const max = float64(math.MaxUint8)
2013-06-17 16:10:55 +02:00
C.glClearColor(
2013-06-19 16:51:41 +02:00
C.GLclampf(float64(r)/max),
C.GLclampf(float64(g)/max),
C.GLclampf(float64(b)/max),
2013-10-11 20:20:13 +02:00
1)
2013-06-17 16:10:55 +02:00
C.glClear(C.GL_COLOR_BUFFER_BIT)
2013-06-15 10:07:14 +02:00
}
2013-10-25 21:15:27 +02:00
func (context *Context) DrawTexture(
2013-11-29 14:45:18 +01:00
id graphics.TextureId,
2013-10-25 21:15:27 +02:00
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
2013-11-29 14:45:18 +01:00
tex := context.ids.TextureAt(id)
2013-11-26 17:46:07 +01:00
context.offscreen.DrawTexture(tex, geometryMatrix, colorMatrix)
2013-10-25 21:15:27 +02:00
}
2013-11-29 14:45:18 +01:00
func (context *Context) DrawRenderTarget(
id graphics.RenderTargetId,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
context.DrawTexture(context.ids.ToTexture(id), geometryMatrix, colorMatrix)
}
2013-10-25 21:15:27 +02:00
func (context *Context) DrawTextureParts(
2013-11-29 14:45:18 +01:00
id graphics.TextureId, parts []graphics.TexturePart,
2013-10-25 21:15:27 +02:00
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
2013-11-29 14:45:18 +01:00
tex := context.ids.TextureAt(id)
2013-11-26 17:46:07 +01:00
context.offscreen.DrawTextureParts(tex, parts, geometryMatrix, colorMatrix)
2013-10-25 21:15:27 +02:00
}
2013-11-29 14:45:18 +01:00
func (context *Context) DrawRenderTargetParts(
id graphics.RenderTargetId, parts []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
context.DrawTextureParts(context.ids.ToTexture(id), parts, geometryMatrix, colorMatrix)
}
2013-11-28 19:21:53 +01:00
// Init initializes the context. The initial state is saved for each GL context.
2013-11-25 14:27:59 +01:00
func (context *Context) Init() {
C.glEnable(C.GL_TEXTURE_2D)
C.glEnable(C.GL_BLEND)
}
2013-10-16 16:06:35 +02:00
func (context *Context) ResetOffscreen() {
2013-10-20 08:51:26 +02:00
context.SetOffscreen(context.screenId)
2013-10-16 16:06:35 +02:00
}
2013-10-21 15:18:10 +02:00
func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
2013-10-27 14:58:56 +01:00
renderTarget := context.ids.RenderTargetAt(renderTargetId)
2013-11-27 03:56:44 +01:00
context.offscreen.Set(renderTarget)
2013-10-22 18:08:01 +02:00
}
func (context *Context) setMainFramebufferOffscreen() {
2013-11-26 17:46:07 +01:00
context.offscreen.SetMainFramebuffer()
2013-10-22 18:08:01 +02:00
}
func (context *Context) flush() {
C.glFlush()
2013-06-15 10:07:14 +02:00
}
2013-10-27 14:58:56 +01:00
func (context *Context) createRenderTarget(width, height int, filter texture.Filter) (
2013-10-21 15:18:10 +02:00
graphics.RenderTargetId, error) {
2013-10-27 14:58:56 +01:00
renderTargetId, err := context.ids.CreateRenderTarget(width, height, filter)
2013-10-20 18:13:09 +02:00
if err != nil {
2013-10-27 14:58:56 +01:00
return 0, err
2013-10-20 18:13:09 +02:00
}
return renderTargetId, nil
2013-06-19 16:08:24 +02:00
}
func (context *Context) CreateRenderTarget(tag string, width, height int) (
2013-10-27 13:27:16 +01:00
graphics.RenderTargetId, error) {
2013-10-27 14:58:56 +01:00
return context.createRenderTarget(width, height, texture.FilterLinear)
2013-10-27 13:27:16 +01:00
}
func (context *Context) CreateTextureFromImage(tag string, img image.Image) (
2013-10-21 15:18:10 +02:00
graphics.TextureId, error) {
2013-10-27 14:58:56 +01:00
return context.ids.CreateTextureFromImage(img)
2013-06-15 10:07:14 +02:00
}