mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-04 15:04:28 +01:00
UnuseGLContext
This commit is contained in:
parent
e55060c239
commit
4489557c84
@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||||
"github.com/hajimehoshi/go-ebiten/ui"
|
"github.com/hajimehoshi/go-ebiten/ui"
|
||||||
"github.com/hajimehoshi/go-ebiten/ui/cocoa"
|
"github.com/hajimehoshi/go-ebiten/ui/cocoa"
|
||||||
|
"image"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
@ -22,6 +23,20 @@ type Game interface {
|
|||||||
Draw(canvas graphics.Canvas)
|
Draw(canvas graphics.Canvas)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadImage(path string) (image.Image, error) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
img, _, err := image.Decode(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return img, nil
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
@ -59,20 +74,35 @@ func main() {
|
|||||||
|
|
||||||
type UI interface {
|
type UI interface {
|
||||||
ui.UI
|
ui.UI
|
||||||
//graphics.TextureFactory
|
graphics.TextureFactory2
|
||||||
}
|
}
|
||||||
var u UI = cocoa.New(screenWidth, screenHeight, screenScale, title)
|
var u UI = cocoa.New(screenWidth, screenHeight, screenScale, title)
|
||||||
// TODO: Get a map or something
|
|
||||||
|
// TODO: Remove this
|
||||||
u.LoadResources(game.InitTextures)
|
u.LoadResources(game.InitTextures)
|
||||||
|
|
||||||
|
textureCreated := u.TextureCreated()
|
||||||
inputStateUpdated := u.InputStateUpdated()
|
inputStateUpdated := u.InputStateUpdated()
|
||||||
screenSizeUpdated := u.ScreenSizeUpdated()
|
screenSizeUpdated := u.ScreenSizeUpdated()
|
||||||
|
|
||||||
|
img, err := loadImage("images/ebiten.png")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
u.CreateTexture("ebiten", img)
|
||||||
|
|
||||||
drawing := make(chan *graphics.LazyCanvas)
|
drawing := make(chan *graphics.LazyCanvas)
|
||||||
go func() {
|
go func() {
|
||||||
frameTime := time.Duration(int64(time.Second) / int64(fps))
|
frameTime := time.Duration(int64(time.Second) / int64(fps))
|
||||||
tick := time.Tick(frameTime)
|
tick := time.Tick(frameTime)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
case e, ok := <-textureCreated:
|
||||||
|
if ok {
|
||||||
|
print(e.Error)
|
||||||
|
} else {
|
||||||
|
textureCreated = nil
|
||||||
|
}
|
||||||
case e, ok := <-inputStateUpdated:
|
case e, ok := <-inputStateUpdated:
|
||||||
// TODO: Use Adaptor?
|
// TODO: Use Adaptor?
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -110,12 +110,12 @@ func (context *Context) createRenderTarget(width, height int, filter texture.Fil
|
|||||||
return renderTargetId, nil
|
return renderTargetId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *Context) CreateRenderTarget(tag string, width, height int) (
|
func (context *Context) CreateRenderTarget(width, height int) (
|
||||||
graphics.RenderTargetId, error) {
|
graphics.RenderTargetId, error) {
|
||||||
return context.createRenderTarget(width, height, texture.FilterLinear)
|
return context.createRenderTarget(width, height, texture.FilterLinear)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *Context) CreateTextureFromImage(tag string, img image.Image) (
|
func (context *Context) CreateTextureFromImage(img image.Image) (
|
||||||
graphics.TextureId, error) {
|
graphics.TextureId, error) {
|
||||||
return context.ids.CreateTextureFromImage(img)
|
return context.ids.CreateTextureFromImage(img)
|
||||||
}
|
}
|
||||||
|
@ -39,10 +39,11 @@ func (d *Device) Update(draw func(graphics.Canvas)) {
|
|||||||
context.flush()
|
context.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Remove 'tag'
|
||||||
func (d *Device) CreateRenderTarget(tag string, width, height int) (graphics.RenderTargetId, error) {
|
func (d *Device) CreateRenderTarget(tag string, width, height int) (graphics.RenderTargetId, error) {
|
||||||
return d.context.CreateRenderTarget(tag, width, height)
|
return d.context.CreateRenderTarget(width, height)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Device) CreateTextureFromImage(tag string, img image.Image) (graphics.TextureId, error) {
|
func (d *Device) CreateTextureFromImage(tag string, img image.Image) (graphics.TextureId, error) {
|
||||||
return d.context.CreateTextureFromImage(tag, img)
|
return d.context.CreateTextureFromImage(img)
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ type TextureCreatedEvent struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type RenderTargetCreatedEvent struct {
|
type RenderTargetCreatedEvent struct {
|
||||||
Tag string
|
Tag string
|
||||||
RenderTargetId RenderTargetId
|
Id RenderTargetId
|
||||||
Error error
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
type TextureFactoryEvents interface {
|
type TextureFactoryEvents interface {
|
||||||
@ -28,6 +28,7 @@ type TextureFactory2 interface {
|
|||||||
TextureFactoryEvents
|
TextureFactoryEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Deprecated
|
||||||
type TextureFactory interface {
|
type TextureFactory interface {
|
||||||
CreateRenderTarget(tag string, width, height int) (RenderTargetId, error)
|
CreateRenderTarget(tag string, width, height int) (RenderTargetId, error)
|
||||||
CreateTextureFromImage(tag string, img image.Image) (TextureId, error)
|
CreateTextureFromImage(tag string, img image.Image) (TextureId, error)
|
||||||
|
@ -23,10 +23,10 @@ type UI struct {
|
|||||||
screenWidth int
|
screenWidth int
|
||||||
screenHeight int
|
screenHeight int
|
||||||
screenScale int
|
screenScale int
|
||||||
graphicsDevice *opengl.Device
|
|
||||||
window unsafe.Pointer
|
window unsafe.Pointer
|
||||||
initialEventSent bool
|
initialEventSent bool
|
||||||
textureFactory *textureFactory
|
textureFactory *textureFactory
|
||||||
|
graphicsDevice *opengl.Device
|
||||||
uiEvents
|
uiEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,10 +73,44 @@ func (u *UI) PollEvents() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UI) CreateRenderTarget(tag string, width, height int) {
|
func (u *UI) CreateTexture(tag string, img image.Image) {
|
||||||
|
go func() {
|
||||||
|
var id graphics.TextureId
|
||||||
|
var err error
|
||||||
|
u.textureFactory.UseContext(func() {
|
||||||
|
id, err = u.graphicsDevice.CreateTextureFromImage("", img)
|
||||||
|
})
|
||||||
|
e := graphics.TextureCreatedEvent{
|
||||||
|
Tag: tag,
|
||||||
|
Id: id,
|
||||||
|
Error: err,
|
||||||
|
}
|
||||||
|
u.textureFactory.notifyTextureCreated(e)
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UI) CreateTexture(tag string, img image.Image) {
|
func (u *UI) CreateRenderTarget(tag string, width, height int) {
|
||||||
|
go func() {
|
||||||
|
var id graphics.RenderTargetId
|
||||||
|
var err error
|
||||||
|
u.textureFactory.UseContext(func() {
|
||||||
|
id, err = u.graphicsDevice.CreateRenderTarget("", width, height)
|
||||||
|
})
|
||||||
|
e := graphics.RenderTargetCreatedEvent{
|
||||||
|
Tag: tag,
|
||||||
|
Id: id,
|
||||||
|
Error: err,
|
||||||
|
}
|
||||||
|
u.textureFactory.notifyRenderTargetCreated(e)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UI) TextureCreated() <-chan graphics.TextureCreatedEvent {
|
||||||
|
return u.textureFactory.TextureCreated()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UI) RenderTargetCreated() <-chan graphics.RenderTargetCreatedEvent {
|
||||||
|
return u.textureFactory.RenderTargetCreated()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UI) LoadResources(f func(graphics.TextureFactory)) {
|
func (u *UI) LoadResources(f func(graphics.TextureFactory)) {
|
||||||
@ -85,6 +119,7 @@ func (u *UI) LoadResources(f func(graphics.TextureFactory)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *UI) Draw(f func(graphics.Canvas)) {
|
func (u *UI) Draw(f func(graphics.Canvas)) {
|
||||||
|
// TODO: Use UseContext instead
|
||||||
C.BeginDrawing(u.window)
|
C.BeginDrawing(u.window)
|
||||||
u.graphicsDevice.Update(f)
|
u.graphicsDevice.Update(f)
|
||||||
C.EndDrawing(u.window)
|
C.EndDrawing(u.window)
|
||||||
|
@ -62,9 +62,19 @@ void PollEvents(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UseGLContext(void* glContext) {
|
void UseGLContext(void* glContextPtr) {
|
||||||
// TODO: CGLLock
|
NSOpenGLContext* glContext = (NSOpenGLContext*)glContextPtr;
|
||||||
[(NSOpenGLContext*)glContext makeCurrentContext];
|
CGLContextObj cglContext = [glContext CGLContextObj];
|
||||||
|
CGLLockContext(cglContext);
|
||||||
|
[glContext makeCurrentContext];
|
||||||
|
}
|
||||||
|
|
||||||
|
void UnuseGLContext(void) {
|
||||||
|
NSOpenGLContext* glContext = [NSOpenGLContext currentContext];
|
||||||
|
[glContext flushBuffer];
|
||||||
|
[NSOpenGLContext clearCurrentContext];
|
||||||
|
CGLContextObj cglContext = [glContext CGLContextObj];
|
||||||
|
CGLUnlockContext(cglContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BeginDrawing(void* window) {
|
void BeginDrawing(void* window) {
|
||||||
|
@ -5,6 +5,7 @@ package cocoa
|
|||||||
// void* CreateGLContext(void* sharedGLContext);
|
// void* CreateGLContext(void* sharedGLContext);
|
||||||
// void* CreateWindow(size_t width, size_t height, const char* title, void* sharedGLContext);
|
// void* CreateWindow(size_t width, size_t height, const char* title, void* sharedGLContext);
|
||||||
// void UseGLContext(void* glContext);
|
// void UseGLContext(void* glContext);
|
||||||
|
// void UnuseGLContext(void);
|
||||||
//
|
//
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
@ -40,8 +41,8 @@ func (t *textureFactory) loop() {
|
|||||||
case f := <-t.funcs:
|
case f := <-t.funcs:
|
||||||
C.UseGLContext(t.sharedContext)
|
C.UseGLContext(t.sharedContext)
|
||||||
f()
|
f()
|
||||||
|
C.UnuseGLContext()
|
||||||
t.funcsDone <- struct{}{}
|
t.funcsDone <- struct{}{}
|
||||||
// TODO: Unuse
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user