Add and use cocoa.Window

This commit is contained in:
Hajime Hoshi 2013-12-11 00:18:08 +09:00
parent 10104b95b4
commit 2e17c366ca
6 changed files with 74 additions and 97 deletions

View File

@ -40,12 +40,11 @@ func main() {
ui.UI ui.UI
graphics.TextureFactory graphics.TextureFactory
} }
var u UI = cocoa.New(screenWidth, screenHeight, screenScale, title) var u UI = cocoa.NewUI()
window := u.CreateWindow(screenWidth, screenHeight, screenScale, title)
textureCreated := u.TextureCreated() textureCreated := u.TextureCreated()
renderTargetCreated := u.RenderTargetCreated() renderTargetCreated := u.RenderTargetCreated()
inputStateUpdated := u.InputStateUpdated()
screenSizeUpdated := u.ScreenSizeUpdated()
for tag, path := range TexturePaths { for tag, path := range TexturePaths {
tag := tag tag := tag
@ -69,6 +68,8 @@ func main() {
drawing := make(chan *graphics.LazyCanvas) drawing := make(chan *graphics.LazyCanvas)
go func() { go func() {
inputStateUpdated := window.InputStateUpdated()
screenSizeUpdated := window.ScreenSizeUpdated()
game := NewGame() game := NewGame()
frameTime := time.Duration(int64(time.Second) / int64(fps)) frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime) tick := time.Tick(frameTime)
@ -97,7 +98,7 @@ func main() {
for { for {
u.PollEvents() u.PollEvents()
u.Draw(func(actualCanvas graphics.Canvas) { window.Draw(func(actualCanvas graphics.Canvas) {
drawing <- graphics.NewLazyCanvas() drawing <- graphics.NewLazyCanvas()
canvas := <-drawing canvas := <-drawing
canvas.Flush(actualCanvas) canvas.Flush(actualCanvas)

View File

@ -10,7 +10,7 @@ type Device struct {
ids *ids ids *ids
} }
func NewDevice(screenWidth, screenHeight, screenScale int) *Device { func NewDevice() *Device {
device := &Device{ device := &Device{
ids: newIds(), ids: newIds(),
} }

View File

@ -3,7 +3,7 @@
#include "ebiten_content_view.h" #include "ebiten_content_view.h"
#include "input.h" #include "input.h"
void ebiten_InputUpdated(InputType inputType, int x, int y); void ebiten_InputUpdated(void* nativeWindow, InputType inputType, int x, int y);
@implementation EbitenContentView { @implementation EbitenContentView {
} }
@ -17,7 +17,7 @@ void ebiten_InputUpdated(InputType inputType, int x, int y);
fromView:nil]; fromView:nil];
int x = location.x; int x = location.x;
int y = location.y; int y = location.y;
ebiten_InputUpdated(InputTypeMouseDown, x, y); ebiten_InputUpdated([self window], InputTypeMouseDown, x, y);
} }
- (void)mouseUp:(NSEvent*)theEvent { - (void)mouseUp:(NSEvent*)theEvent {
@ -26,7 +26,7 @@ void ebiten_InputUpdated(InputType inputType, int x, int y);
fromView:nil]; fromView:nil];
int x = location.x; int x = location.x;
int y = location.y; int y = location.y;
ebiten_InputUpdated(InputTypeMouseUp, x, y); ebiten_InputUpdated([self window], InputTypeMouseUp, x, y);
} }
- (void)mouseDragged:(NSEvent*)theEvent { - (void)mouseDragged:(NSEvent*)theEvent {
@ -34,7 +34,7 @@ void ebiten_InputUpdated(InputType inputType, int x, int y);
fromView:nil]; fromView:nil];
int x = location.x; int x = location.x;
int y = location.y; int y = location.y;
ebiten_InputUpdated(InputTypeMouseDragged, x, y); ebiten_InputUpdated([self window], InputTypeMouseDragged, x, y);
} }
@end @end

View File

@ -3,8 +3,6 @@ package cocoa
// #cgo CFLAGS: -x objective-c // #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL // #cgo LDFLAGS: -framework Cocoa -framework OpenGL
// //
// #include "input.h"
//
// void StartApplication(void); // void StartApplication(void);
// void PollEvents(void); // void PollEvents(void);
// //
@ -17,61 +15,36 @@ import (
) )
type UI struct { type UI struct {
screenWidth int
screenHeight int
screenScale int
window *Window
initialEventSent bool
textureFactory *textureFactory textureFactory *textureFactory
graphicsDevice *opengl.Device graphicsDevice *opengl.Device
windowEvents
} }
var currentUI *UI var currentUI *UI
func New(screenWidth, screenHeight, screenScale int, title string) *UI { func NewUI() *UI {
if currentUI != nil { if currentUI != nil {
panic("UI can't be duplicated.") panic("UI can't be duplicated.")
} }
u := &UI{ u := &UI{}
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
initialEventSent: false,
}
C.StartApplication() C.StartApplication()
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.screenHeight,
u.screenScale)
}) })
u.window = u.CreateWindow(
u.screenWidth,
u.screenHeight,
u.screenScale,
title)
currentUI = u currentUI = u
return u return u
} }
func (u *UI) CreateWindow(width, height, scale int, title string) *Window { func (u *UI) CreateWindow(width, height, scale int, title string) ui.Window {
return u.textureFactory.createWindow(u, width, height, scale, title) return u.textureFactory.createWindow(u, width, height, scale, title)
} }
func (u *UI) PollEvents() { func (u *UI) PollEvents() {
C.PollEvents() C.PollEvents()
if !u.initialEventSent {
e := ui.ScreenSizeUpdatedEvent{u.screenWidth, u.screenHeight}
u.windowEvents.notifyScreenSizeUpdated(e)
u.initialEventSent = true
}
} }
func (u *UI) CreateTexture(tag interface{}, img image.Image) { func (u *UI) CreateTexture(tag interface{}, img image.Image) {
@ -113,41 +86,3 @@ func (u *UI) TextureCreated() <-chan graphics.TextureCreatedEvent {
func (u *UI) RenderTargetCreated() <-chan graphics.RenderTargetCreatedEvent { func (u *UI) RenderTargetCreated() <-chan graphics.RenderTargetCreatedEvent {
return u.textureFactory.RenderTargetCreated() return u.textureFactory.RenderTargetCreated()
} }
func (u *UI) Draw(f func(graphics.Canvas)) {
u.window.Draw(f)
}
//export ebiten_ScreenSizeUpdated
func ebiten_ScreenSizeUpdated(width, height int) {
u := currentUI
e := ui.ScreenSizeUpdatedEvent{width, height}
u.windowEvents.notifyScreenSizeUpdated(e)
}
//export ebiten_InputUpdated
func ebiten_InputUpdated(inputType C.InputType, cx, cy C.int) {
u := currentUI
if inputType == C.InputTypeMouseUp {
e := ui.InputStateUpdatedEvent{-1, -1}
u.windowEvents.notifyInputStateUpdated(e)
return
}
x, y := int(cx), int(cy)
x /= u.screenScale
y /= u.screenScale
if x < 0 {
x = 0
} else if u.screenWidth <= x {
x = u.screenWidth - 1
}
if y < 0 {
y = 0
} else if u.screenHeight <= y {
y = u.screenHeight - 1
}
e := ui.InputStateUpdatedEvent{x, y}
u.windowEvents.notifyInputStateUpdated(e)
}

View File

@ -2,6 +2,8 @@ package cocoa
// #include <stdlib.h> // #include <stdlib.h>
// //
// #include "input.h"
//
// void* CreateWindow(size_t width, size_t height, const char* title, void* glContext); // void* CreateWindow(size_t width, size_t height, const char* title, void* glContext);
// void* CreateGLContext(void* sharedGLContext); // void* CreateGLContext(void* sharedGLContext);
// //
@ -13,21 +15,31 @@ import "C"
import ( import (
"github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/opengl" "github.com/hajimehoshi/go-ebiten/graphics/opengl"
"github.com/hajimehoshi/go-ebiten/ui"
"runtime" "runtime"
"unsafe" "unsafe"
) )
type Window struct { type Window struct {
ui *UI ui *UI
screenWidth int
screenHeight int
screenScale int
native unsafe.Pointer native unsafe.Pointer
canvas *opengl.Canvas canvas *opengl.Canvas
funcs chan func() funcs chan func()
funcsDone chan struct{} funcsDone chan struct{}
windowEvents
} }
var windows = map[unsafe.Pointer]*Window{}
func runWindow(ui *UI, width, height, scale int, title string, sharedContext unsafe.Pointer) *Window { func runWindow(ui *UI, width, height, scale int, title string, sharedContext unsafe.Pointer) *Window {
w := &Window{ w := &Window{
ui: ui, ui: ui,
screenWidth: width,
screenHeight: height,
screenScale: scale,
funcs: make(chan func()), funcs: make(chan func()),
funcsDone: make(chan struct{}), funcsDone: make(chan struct{}),
} }
@ -43,6 +55,7 @@ func runWindow(ui *UI, width, height, scale int, title string, sharedContext uns
C.size_t(height*scale), C.size_t(height*scale),
cTitle, cTitle,
glContext) glContext)
windows[w.native] = w
close(ch) close(ch)
w.loop() w.loop()
}() }()
@ -76,3 +89,37 @@ func (w *Window) useContext(f func()) {
w.funcs <- f w.funcs <- f
<-w.funcsDone <-w.funcsDone
} }
/*//export ebiten_ScreenSizeUpdated
func ebiten_ScreenSizeUpdated(nativeWindow unsafe.Pointer, width, height int) {
u := currentUI
e := ui.ScreenSizeUpdatedEvent{width, height}
u.windowEvents.notifyScreenSizeUpdated(e)
}*/
//export ebiten_InputUpdated
func ebiten_InputUpdated(nativeWindow unsafe.Pointer, inputType C.InputType, cx, cy C.int) {
w := windows[nativeWindow]
if inputType == C.InputTypeMouseUp {
e := ui.InputStateUpdatedEvent{-1, -1}
w.notifyInputStateUpdated(e)
return
}
x, y := int(cx), int(cy)
x /= w.screenScale
y /= w.screenScale
if x < 0 {
x = 0
} else if w.screenWidth <= x {
x = w.screenWidth - 1
}
if y < 0 {
y = 0
} else if w.screenHeight <= y {
y = w.screenHeight - 1
}
e := ui.InputStateUpdatedEvent{x, y}
w.notifyInputStateUpdated(e)
}

View File

@ -14,15 +14,9 @@ type InputStateUpdatedEvent struct {
Y int Y int
} }
type UIEvents interface {
ScreenSizeUpdated() <-chan ScreenSizeUpdatedEvent
InputStateUpdated() <-chan InputStateUpdatedEvent
}
type UI interface { type UI interface {
PollEvents() PollEvents()
Draw(func(graphics.Canvas)) CreateWindow(screenWidth, screenHeight, screenScale int, title string) Window
UIEvents
} }
type WindowEvents interface { type WindowEvents interface {