mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Add cocoa.window
This commit is contained in:
parent
e10bac5a8a
commit
c36d8c5512
@ -7,8 +7,6 @@ package cocoa
|
||||
//
|
||||
// void StartApplication(void);
|
||||
// void PollEvents(void);
|
||||
// void BeginDrawing(void* window);
|
||||
// void EndDrawing(void* window);
|
||||
//
|
||||
import "C"
|
||||
import (
|
||||
@ -16,14 +14,13 @@ import (
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl"
|
||||
"github.com/hajimehoshi/go-ebiten/ui"
|
||||
"image"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type UI struct {
|
||||
screenWidth int
|
||||
screenHeight int
|
||||
screenScale int
|
||||
window unsafe.Pointer
|
||||
window *window
|
||||
initialEventSent bool
|
||||
textureFactory *textureFactory
|
||||
graphicsDevice *opengl.Device
|
||||
@ -114,10 +111,9 @@ func (u *UI) RenderTargetCreated() <-chan graphics.RenderTargetCreatedEvent {
|
||||
}
|
||||
|
||||
func (u *UI) Draw(f func(graphics.Canvas)) {
|
||||
// TODO: Use UseContext instead
|
||||
C.BeginDrawing(u.window)
|
||||
u.graphicsDevice.Update(f)
|
||||
C.EndDrawing(u.window)
|
||||
u.window.UseContext(func() {
|
||||
u.graphicsDevice.Update(f)
|
||||
})
|
||||
}
|
||||
|
||||
//export ebiten_ScreenSizeUpdated
|
||||
|
@ -50,9 +50,8 @@ void* CreateGLContext(void* sharedGLContext) {
|
||||
return glContext;
|
||||
}
|
||||
|
||||
void* CreateWindow(size_t width, size_t height, const char* title, void* sharedGLContext) {
|
||||
NSOpenGLContext* glContext = CreateGLContext(sharedGLContext);
|
||||
[glContext makeCurrentContext];
|
||||
void* CreateWindow(size_t width, size_t height, const char* title, void* glContext_) {
|
||||
NSOpenGLContext* glContext = (NSOpenGLContext*)glContext_;
|
||||
|
||||
NSSize size = NSMakeSize(width, height);
|
||||
EbitenWindow* window = [[EbitenWindow alloc]
|
||||
@ -95,6 +94,10 @@ void UnuseGLContext(void) {
|
||||
CGLUnlockContext(cglContext);
|
||||
}
|
||||
|
||||
void* GetGLContext(void* window) {
|
||||
return [(EbitenWindow*)window glContext];
|
||||
}
|
||||
|
||||
void BeginDrawing(void* window) {
|
||||
// TODO: CGLLock
|
||||
[[(EbitenWindow*)window glContext] makeCurrentContext];
|
||||
|
@ -1,15 +1,12 @@
|
||||
package cocoa
|
||||
|
||||
// #include <stdlib.h>
|
||||
//
|
||||
// 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 (
|
||||
//"github.com/hajimehoshi/go-ebiten/graphics"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@ -22,11 +19,12 @@ type textureFactory struct {
|
||||
|
||||
func runTextureFactory() *textureFactory {
|
||||
t := &textureFactory{
|
||||
funcs: make(chan func()),
|
||||
funcs: make(chan func()),
|
||||
funcsDone: make(chan struct{}),
|
||||
}
|
||||
ch := make(chan struct{})
|
||||
go func() {
|
||||
runtime.LockOSThread()
|
||||
t.sharedContext = C.CreateGLContext(unsafe.Pointer(nil))
|
||||
close(ch)
|
||||
t.loop()
|
||||
@ -52,12 +50,6 @@ func (t *textureFactory) UseContext(f func()) {
|
||||
<-t.funcsDone
|
||||
}
|
||||
|
||||
func (t *textureFactory) CreateWindow(width, height int, title string) unsafe.Pointer {
|
||||
cTitle := C.CString(title)
|
||||
defer C.free(unsafe.Pointer(cTitle))
|
||||
|
||||
return C.CreateWindow(C.size_t(width),
|
||||
C.size_t(height),
|
||||
cTitle,
|
||||
t.sharedContext)
|
||||
func (t *textureFactory) CreateWindow(width, height int, title string) *window {
|
||||
return runWindow(width, height, title, t.sharedContext)
|
||||
}
|
||||
|
65
ui/cocoa/window.go
Normal file
65
ui/cocoa/window.go
Normal file
@ -0,0 +1,65 @@
|
||||
package cocoa
|
||||
|
||||
// #include <stdlib.h>
|
||||
//
|
||||
// void* CreateWindow(size_t width, size_t height, const char* title, void* glContext);
|
||||
// void* CreateGLContext(void* sharedGLContext);
|
||||
//
|
||||
// void* GetGLContext(void* window);
|
||||
// void UseGLContext(void* glContext);
|
||||
// void UnuseGLContext(void);
|
||||
//
|
||||
import "C"
|
||||
import (
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type window struct {
|
||||
native unsafe.Pointer
|
||||
funcs chan func()
|
||||
funcsDone chan struct{}
|
||||
}
|
||||
|
||||
func runWindow(width, height int, title string, sharedContext unsafe.Pointer) *window {
|
||||
w := &window{
|
||||
funcs: make(chan func()),
|
||||
funcsDone: make(chan struct{}),
|
||||
}
|
||||
|
||||
cTitle := C.CString(title)
|
||||
defer C.free(unsafe.Pointer(cTitle))
|
||||
|
||||
ch := make(chan struct{})
|
||||
go func() {
|
||||
runtime.LockOSThread()
|
||||
glContext := C.CreateGLContext(sharedContext)
|
||||
w.native = C.CreateWindow(C.size_t(width),
|
||||
C.size_t(height),
|
||||
cTitle,
|
||||
glContext)
|
||||
close(ch)
|
||||
w.loop()
|
||||
}()
|
||||
<-ch
|
||||
// TODO: Activate here?
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *window) loop() {
|
||||
for {
|
||||
select {
|
||||
case f := <-w.funcs:
|
||||
glContext := C.GetGLContext(w.native)
|
||||
C.UseGLContext(glContext)
|
||||
f()
|
||||
C.UnuseGLContext()
|
||||
w.funcsDone <- struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *window) UseContext(f func()) {
|
||||
w.funcs <- f
|
||||
<-w.funcsDone
|
||||
}
|
Loading…
Reference in New Issue
Block a user