UnuseGLContext

This commit is contained in:
Hajime Hoshi 2013-12-08 01:35:24 +09:00
parent e55060c239
commit 4489557c84
7 changed files with 94 additions and 16 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/ui"
"github.com/hajimehoshi/go-ebiten/ui/cocoa"
"image"
"os"
"runtime"
"time"
@ -22,6 +23,20 @@ type Game interface {
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() {
runtime.GOMAXPROCS(runtime.NumCPU())
runtime.LockOSThread()
@ -59,20 +74,35 @@ func main() {
type UI interface {
ui.UI
//graphics.TextureFactory
graphics.TextureFactory2
}
var u UI = cocoa.New(screenWidth, screenHeight, screenScale, title)
// TODO: Get a map or something
// TODO: Remove this
u.LoadResources(game.InitTextures)
textureCreated := u.TextureCreated()
inputStateUpdated := u.InputStateUpdated()
screenSizeUpdated := u.ScreenSizeUpdated()
img, err := loadImage("images/ebiten.png")
if err != nil {
panic(err)
}
u.CreateTexture("ebiten", img)
drawing := make(chan *graphics.LazyCanvas)
go func() {
frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime)
for {
select {
case e, ok := <-textureCreated:
if ok {
print(e.Error)
} else {
textureCreated = nil
}
case e, ok := <-inputStateUpdated:
// TODO: Use Adaptor?
if ok {

View File

@ -110,12 +110,12 @@ func (context *Context) createRenderTarget(width, height int, filter texture.Fil
return renderTargetId, nil
}
func (context *Context) CreateRenderTarget(tag string, width, height int) (
func (context *Context) CreateRenderTarget(width, height int) (
graphics.RenderTargetId, error) {
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) {
return context.ids.CreateTextureFromImage(img)
}

View File

@ -39,10 +39,11 @@ func (d *Device) Update(draw func(graphics.Canvas)) {
context.flush()
}
// TODO: Remove 'tag'
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) {
return d.context.CreateTextureFromImage(tag, img)
return d.context.CreateTextureFromImage(img)
}

View File

@ -11,9 +11,9 @@ type TextureCreatedEvent struct {
}
type RenderTargetCreatedEvent struct {
Tag string
RenderTargetId RenderTargetId
Error error
Tag string
Id RenderTargetId
Error error
}
type TextureFactoryEvents interface {
@ -28,6 +28,7 @@ type TextureFactory2 interface {
TextureFactoryEvents
}
// TODO: Deprecated
type TextureFactory interface {
CreateRenderTarget(tag string, width, height int) (RenderTargetId, error)
CreateTextureFromImage(tag string, img image.Image) (TextureId, error)

View File

@ -23,10 +23,10 @@ type UI struct {
screenWidth int
screenHeight int
screenScale int
graphicsDevice *opengl.Device
window unsafe.Pointer
initialEventSent bool
textureFactory *textureFactory
graphicsDevice *opengl.Device
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)) {
@ -85,6 +119,7 @@ func (u *UI) LoadResources(f func(graphics.TextureFactory)) {
}
func (u *UI) Draw(f func(graphics.Canvas)) {
// TODO: Use UseContext instead
C.BeginDrawing(u.window)
u.graphicsDevice.Update(f)
C.EndDrawing(u.window)

View File

@ -62,9 +62,19 @@ void PollEvents(void) {
}
}
void UseGLContext(void* glContext) {
// TODO: CGLLock
[(NSOpenGLContext*)glContext makeCurrentContext];
void UseGLContext(void* glContextPtr) {
NSOpenGLContext* glContext = (NSOpenGLContext*)glContextPtr;
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) {

View File

@ -5,6 +5,7 @@ package cocoa
// void* CreateGLContext(void* sharedGLContext);
// void* CreateWindow(size_t width, size_t height, const char* title, void* sharedGLContext);
// void UseGLContext(void* glContext);
// void UnuseGLContext(void);
//
import "C"
import (
@ -40,8 +41,8 @@ func (t *textureFactory) loop() {
case f := <-t.funcs:
C.UseGLContext(t.sharedContext)
f()
C.UnuseGLContext()
t.funcsDone <- struct{}{}
// TODO: Unuse
}
}
}