mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Rename Context -> Canvas
This commit is contained in:
parent
2bd77e949a
commit
7e246015ff
121
graphics/opengl/canvas.go
Normal file
121
graphics/opengl/canvas.go
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
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"
|
||||||
|
"image"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Canvas struct {
|
||||||
|
screenId graphics.RenderTargetId
|
||||||
|
ids *ids
|
||||||
|
offscreen *offscreen.Offscreen
|
||||||
|
}
|
||||||
|
|
||||||
|
func newCanvas(screenWidth, screenHeight, screenScale int) *Canvas {
|
||||||
|
canvas := &Canvas{
|
||||||
|
ids: newIds(),
|
||||||
|
offscreen: offscreen.New(screenWidth, screenHeight, screenScale),
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
canvas.screenId, err = canvas.createRenderTarget(
|
||||||
|
screenWidth, screenHeight, texture.FilterNearest)
|
||||||
|
if err != nil {
|
||||||
|
panic("initializing the offscreen failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.Init()
|
||||||
|
|
||||||
|
return canvas
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init initializes the canvas. The initial state is saved for each GL canvas.
|
||||||
|
func (canvas *Canvas) Init() {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (canvas *Canvas) createRenderTarget(width, height int, filter texture.Filter) (
|
||||||
|
graphics.RenderTargetId, error) {
|
||||||
|
renderTargetId, err := canvas.ids.CreateRenderTarget(width, height, filter)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return renderTargetId, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (canvas *Canvas) CreateRenderTarget(width, height int) (
|
||||||
|
graphics.RenderTargetId, error) {
|
||||||
|
return canvas.createRenderTarget(width, height, texture.FilterLinear)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (canvas *Canvas) CreateTextureFromImage(img image.Image) (
|
||||||
|
graphics.TextureId, error) {
|
||||||
|
return canvas.ids.CreateTextureFromImage(img)
|
||||||
|
}
|
@ -1,121 +0,0 @@
|
|||||||
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"
|
|
||||||
"image"
|
|
||||||
"math"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Context struct {
|
|
||||||
screenId graphics.RenderTargetId
|
|
||||||
ids *ids
|
|
||||||
offscreen *offscreen.Offscreen
|
|
||||||
}
|
|
||||||
|
|
||||||
func newContext(screenWidth, screenHeight, screenScale int) *Context {
|
|
||||||
context := &Context{
|
|
||||||
ids: newIds(),
|
|
||||||
offscreen: offscreen.New(screenWidth, screenHeight, screenScale),
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
context.screenId, err = context.createRenderTarget(
|
|
||||||
screenWidth, screenHeight, texture.FilterNearest)
|
|
||||||
if err != nil {
|
|
||||||
panic("initializing the offscreen failed: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
context.Init()
|
|
||||||
|
|
||||||
return context
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) Clear() {
|
|
||||||
context.Fill(0, 0, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) 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 (context *Context) DrawTexture(
|
|
||||||
id graphics.TextureId,
|
|
||||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
|
||||||
tex := context.ids.TextureAt(id)
|
|
||||||
context.offscreen.DrawTexture(tex, geometryMatrix, colorMatrix)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) DrawRenderTarget(
|
|
||||||
id graphics.RenderTargetId,
|
|
||||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
|
||||||
context.DrawTexture(context.ids.ToTexture(id), geometryMatrix, colorMatrix)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) DrawTextureParts(
|
|
||||||
id graphics.TextureId, parts []graphics.TexturePart,
|
|
||||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
|
||||||
tex := context.ids.TextureAt(id)
|
|
||||||
context.offscreen.DrawTextureParts(tex, parts, geometryMatrix, colorMatrix)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init initializes the context. The initial state is saved for each GL context.
|
|
||||||
func (context *Context) Init() {
|
|
||||||
C.glEnable(C.GL_TEXTURE_2D)
|
|
||||||
C.glEnable(C.GL_BLEND)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) ResetOffscreen() {
|
|
||||||
context.SetOffscreen(context.screenId)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
|
|
||||||
renderTarget := context.ids.RenderTargetAt(renderTargetId)
|
|
||||||
context.offscreen.Set(renderTarget)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) setMainFramebufferOffscreen() {
|
|
||||||
context.offscreen.SetMainFramebuffer()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) flush() {
|
|
||||||
C.glFlush()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) createRenderTarget(width, height int, filter texture.Filter) (
|
|
||||||
graphics.RenderTargetId, error) {
|
|
||||||
renderTargetId, err := context.ids.CreateRenderTarget(width, height, filter)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return renderTargetId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) CreateRenderTarget(width, height int) (
|
|
||||||
graphics.RenderTargetId, error) {
|
|
||||||
return context.createRenderTarget(width, height, texture.FilterLinear)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) CreateTextureFromImage(img image.Image) (
|
|
||||||
graphics.TextureId, error) {
|
|
||||||
return context.ids.CreateTextureFromImage(img)
|
|
||||||
}
|
|
@ -7,42 +7,42 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
context *Context
|
canvas *Canvas
|
||||||
screenScale int
|
screenScale int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDevice(screenWidth, screenHeight, screenScale int) *Device {
|
func NewDevice(screenWidth, screenHeight, screenScale int) *Device {
|
||||||
context := newContext(screenWidth, screenHeight, screenScale)
|
canvas := newCanvas(screenWidth, screenHeight, screenScale)
|
||||||
return &Device{
|
return &Device{
|
||||||
context: context,
|
canvas: canvas,
|
||||||
screenScale: screenScale,
|
screenScale: screenScale,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Device) Update(draw func(graphics.Canvas)) {
|
func (d *Device) Update(draw func(graphics.Canvas)) {
|
||||||
context := d.context
|
canvas := d.canvas
|
||||||
context.Init()
|
canvas.Init()
|
||||||
context.ResetOffscreen()
|
canvas.ResetOffscreen()
|
||||||
context.Clear()
|
canvas.Clear()
|
||||||
|
|
||||||
draw(context)
|
draw(canvas)
|
||||||
|
|
||||||
context.flush()
|
canvas.flush()
|
||||||
context.setMainFramebufferOffscreen()
|
canvas.setMainFramebufferOffscreen()
|
||||||
context.Clear()
|
canvas.Clear()
|
||||||
|
|
||||||
scale := float64(d.screenScale)
|
scale := float64(d.screenScale)
|
||||||
geometryMatrix := matrix.IdentityGeometry()
|
geometryMatrix := matrix.IdentityGeometry()
|
||||||
geometryMatrix.Scale(scale, scale)
|
geometryMatrix.Scale(scale, scale)
|
||||||
context.DrawRenderTarget(context.screenId,
|
canvas.DrawRenderTarget(canvas.screenId,
|
||||||
geometryMatrix, matrix.IdentityColor())
|
geometryMatrix, matrix.IdentityColor())
|
||||||
context.flush()
|
canvas.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Device) CreateRenderTarget(width, height int) (graphics.RenderTargetId, error) {
|
func (d *Device) CreateRenderTarget(width, height int) (graphics.RenderTargetId, error) {
|
||||||
return d.context.CreateRenderTarget(width, height)
|
return d.canvas.CreateRenderTarget(width, height)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Device) CreateTexture(img image.Image) (graphics.TextureId, error) {
|
func (d *Device) CreateTexture(img image.Image) (graphics.TextureId, error) {
|
||||||
return d.context.CreateTextureFromImage(img)
|
return d.canvas.CreateTextureFromImage(img)
|
||||||
}
|
}
|
||||||
|
@ -44,14 +44,15 @@ func New(screenWidth, screenHeight, screenScale int, title string) *UI {
|
|||||||
|
|
||||||
u.textureFactory = runTextureFactory()
|
u.textureFactory = runTextureFactory()
|
||||||
|
|
||||||
u.textureFactory.UseContext(func() {
|
u.textureFactory.useContext(func() {
|
||||||
u.graphicsDevice = opengl.NewDevice(
|
u.graphicsDevice = opengl.NewDevice(
|
||||||
u.screenWidth,
|
u.screenWidth,
|
||||||
u.screenHeight,
|
u.screenHeight,
|
||||||
u.screenScale)
|
u.screenScale)
|
||||||
})
|
})
|
||||||
|
|
||||||
u.window = u.textureFactory.CreateWindow(
|
u.window = u.textureFactory.createWindow(
|
||||||
|
u,
|
||||||
u.screenWidth*u.screenScale,
|
u.screenWidth*u.screenScale,
|
||||||
u.screenHeight*u.screenScale,
|
u.screenHeight*u.screenScale,
|
||||||
title)
|
title)
|
||||||
@ -74,7 +75,7 @@ func (u *UI) CreateTexture(tag interface{}, img image.Image) {
|
|||||||
go func() {
|
go func() {
|
||||||
var id graphics.TextureId
|
var id graphics.TextureId
|
||||||
var err error
|
var err error
|
||||||
u.textureFactory.UseContext(func() {
|
u.textureFactory.useContext(func() {
|
||||||
id, err = u.graphicsDevice.CreateTexture(img)
|
id, err = u.graphicsDevice.CreateTexture(img)
|
||||||
})
|
})
|
||||||
e := graphics.TextureCreatedEvent{
|
e := graphics.TextureCreatedEvent{
|
||||||
@ -90,7 +91,7 @@ func (u *UI) CreateRenderTarget(tag interface{}, width, height int) {
|
|||||||
go func() {
|
go func() {
|
||||||
var id graphics.RenderTargetId
|
var id graphics.RenderTargetId
|
||||||
var err error
|
var err error
|
||||||
u.textureFactory.UseContext(func() {
|
u.textureFactory.useContext(func() {
|
||||||
id, err = u.graphicsDevice.CreateRenderTarget(width, height)
|
id, err = u.graphicsDevice.CreateRenderTarget(width, height)
|
||||||
})
|
})
|
||||||
e := graphics.RenderTargetCreatedEvent{
|
e := graphics.RenderTargetCreatedEvent{
|
||||||
@ -111,9 +112,7 @@ func (u *UI) RenderTargetCreated() <-chan graphics.RenderTargetCreatedEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *UI) Draw(f func(graphics.Canvas)) {
|
func (u *UI) Draw(f func(graphics.Canvas)) {
|
||||||
u.window.UseContext(func() {
|
u.window.Draw(f)
|
||||||
u.graphicsDevice.Update(f)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//export ebiten_ScreenSizeUpdated
|
//export ebiten_ScreenSizeUpdated
|
||||||
|
@ -45,11 +45,11 @@ func (t *textureFactory) loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *textureFactory) UseContext(f func()) {
|
func (t *textureFactory) useContext(f func()) {
|
||||||
t.funcs <- f
|
t.funcs <- f
|
||||||
<-t.funcsDone
|
<-t.funcsDone
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *textureFactory) CreateWindow(width, height int, title string) *window {
|
func (t *textureFactory) createWindow(ui *UI, width, height int, title string) *window {
|
||||||
return runWindow(width, height, title, t.sharedContext)
|
return runWindow(ui, width, height, title, t.sharedContext)
|
||||||
}
|
}
|
||||||
|
@ -11,18 +11,21 @@ package cocoa
|
|||||||
//
|
//
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||||
"runtime"
|
"runtime"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type window struct {
|
type window struct {
|
||||||
|
ui *UI
|
||||||
native unsafe.Pointer
|
native unsafe.Pointer
|
||||||
funcs chan func()
|
funcs chan func()
|
||||||
funcsDone chan struct{}
|
funcsDone chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runWindow(width, height int, title string, sharedContext unsafe.Pointer) *window {
|
func runWindow(ui *UI, width, height int, title string, sharedContext unsafe.Pointer) *window {
|
||||||
w := &window{
|
w := &window{
|
||||||
|
ui: ui,
|
||||||
funcs: make(chan func()),
|
funcs: make(chan func()),
|
||||||
funcsDone: make(chan struct{}),
|
funcsDone: make(chan struct{}),
|
||||||
}
|
}
|
||||||
@ -58,7 +61,13 @@ func (w *window) loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *window) UseContext(f func()) {
|
func (w *window) Draw(f func(graphics.Canvas)) {
|
||||||
|
w.useContext(func() {
|
||||||
|
w.ui.graphicsDevice.Update(f)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *window) useContext(f func()) {
|
||||||
w.funcs <- f
|
w.funcs <- f
|
||||||
<-w.funcsDone
|
<-w.funcsDone
|
||||||
}
|
}
|
||||||
|
10
ui/ui.go
10
ui/ui.go
@ -24,3 +24,13 @@ type UI interface {
|
|||||||
Draw(func(graphics.Canvas))
|
Draw(func(graphics.Canvas))
|
||||||
UIEvents
|
UIEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WindowEvents interface {
|
||||||
|
ScreenSizeUpdated() <-chan ScreenSizeUpdatedEvent
|
||||||
|
InputStateUpdated() <-chan InputStateUpdatedEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
type Window interface {
|
||||||
|
Draw(func(graphics.Canvas))
|
||||||
|
WindowEvents
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user