mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Add and use cocoa.Window
This commit is contained in:
parent
10104b95b4
commit
2e17c366ca
@ -40,13 +40,12 @@ func main() {
|
||||
ui.UI
|
||||
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()
|
||||
renderTargetCreated := u.RenderTargetCreated()
|
||||
inputStateUpdated := u.InputStateUpdated()
|
||||
screenSizeUpdated := u.ScreenSizeUpdated()
|
||||
|
||||
|
||||
for tag, path := range TexturePaths {
|
||||
tag := tag
|
||||
path := path
|
||||
@ -69,6 +68,8 @@ func main() {
|
||||
|
||||
drawing := make(chan *graphics.LazyCanvas)
|
||||
go func() {
|
||||
inputStateUpdated := window.InputStateUpdated()
|
||||
screenSizeUpdated := window.ScreenSizeUpdated()
|
||||
game := NewGame()
|
||||
frameTime := time.Duration(int64(time.Second) / int64(fps))
|
||||
tick := time.Tick(frameTime)
|
||||
@ -97,7 +98,7 @@ func main() {
|
||||
|
||||
for {
|
||||
u.PollEvents()
|
||||
u.Draw(func(actualCanvas graphics.Canvas) {
|
||||
window.Draw(func(actualCanvas graphics.Canvas) {
|
||||
drawing <- graphics.NewLazyCanvas()
|
||||
canvas := <-drawing
|
||||
canvas.Flush(actualCanvas)
|
||||
|
@ -7,10 +7,10 @@ import (
|
||||
)
|
||||
|
||||
type Device struct {
|
||||
ids *ids
|
||||
ids *ids
|
||||
}
|
||||
|
||||
func NewDevice(screenWidth, screenHeight, screenScale int) *Device {
|
||||
func NewDevice() *Device {
|
||||
device := &Device{
|
||||
ids: newIds(),
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include "ebiten_content_view.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 {
|
||||
}
|
||||
@ -17,7 +17,7 @@ void ebiten_InputUpdated(InputType inputType, int x, int y);
|
||||
fromView:nil];
|
||||
int x = location.x;
|
||||
int y = location.y;
|
||||
ebiten_InputUpdated(InputTypeMouseDown, x, y);
|
||||
ebiten_InputUpdated([self window], InputTypeMouseDown, x, y);
|
||||
}
|
||||
|
||||
- (void)mouseUp:(NSEvent*)theEvent {
|
||||
@ -26,7 +26,7 @@ void ebiten_InputUpdated(InputType inputType, int x, int y);
|
||||
fromView:nil];
|
||||
int x = location.x;
|
||||
int y = location.y;
|
||||
ebiten_InputUpdated(InputTypeMouseUp, x, y);
|
||||
ebiten_InputUpdated([self window], InputTypeMouseUp, x, y);
|
||||
}
|
||||
|
||||
- (void)mouseDragged:(NSEvent*)theEvent {
|
||||
@ -34,7 +34,7 @@ void ebiten_InputUpdated(InputType inputType, int x, int y);
|
||||
fromView:nil];
|
||||
int x = location.x;
|
||||
int y = location.y;
|
||||
ebiten_InputUpdated(InputTypeMouseDragged, x, y);
|
||||
ebiten_InputUpdated([self window], InputTypeMouseDragged, x, y);
|
||||
}
|
||||
|
||||
@end
|
||||
|
@ -3,8 +3,6 @@ package cocoa
|
||||
// #cgo CFLAGS: -x objective-c
|
||||
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL
|
||||
//
|
||||
// #include "input.h"
|
||||
//
|
||||
// void StartApplication(void);
|
||||
// void PollEvents(void);
|
||||
//
|
||||
@ -17,61 +15,36 @@ import (
|
||||
)
|
||||
|
||||
type UI struct {
|
||||
screenWidth int
|
||||
screenHeight int
|
||||
screenScale int
|
||||
window *Window
|
||||
initialEventSent bool
|
||||
textureFactory *textureFactory
|
||||
graphicsDevice *opengl.Device
|
||||
windowEvents
|
||||
textureFactory *textureFactory
|
||||
graphicsDevice *opengl.Device
|
||||
}
|
||||
|
||||
var currentUI *UI
|
||||
|
||||
func New(screenWidth, screenHeight, screenScale int, title string) *UI {
|
||||
func NewUI() *UI {
|
||||
if currentUI != nil {
|
||||
panic("UI can't be duplicated.")
|
||||
}
|
||||
u := &UI{
|
||||
screenWidth: screenWidth,
|
||||
screenHeight: screenHeight,
|
||||
screenScale: screenScale,
|
||||
initialEventSent: false,
|
||||
}
|
||||
u := &UI{}
|
||||
|
||||
C.StartApplication()
|
||||
|
||||
u.textureFactory = runTextureFactory()
|
||||
|
||||
u.textureFactory.useContext(func() {
|
||||
u.graphicsDevice = opengl.NewDevice(
|
||||
u.screenWidth,
|
||||
u.screenHeight,
|
||||
u.screenScale)
|
||||
u.graphicsDevice = opengl.NewDevice()
|
||||
})
|
||||
u.window = u.CreateWindow(
|
||||
u.screenWidth,
|
||||
u.screenHeight,
|
||||
u.screenScale,
|
||||
title)
|
||||
|
||||
currentUI = 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)
|
||||
}
|
||||
|
||||
func (u *UI) 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) {
|
||||
@ -113,41 +86,3 @@ func (u *UI) TextureCreated() <-chan graphics.TextureCreatedEvent {
|
||||
func (u *UI) RenderTargetCreated() <-chan graphics.RenderTargetCreatedEvent {
|
||||
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)
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package cocoa
|
||||
|
||||
// #include <stdlib.h>
|
||||
//
|
||||
// #include "input.h"
|
||||
//
|
||||
// void* CreateWindow(size_t width, size_t height, const char* title, void* glContext);
|
||||
// void* CreateGLContext(void* sharedGLContext);
|
||||
//
|
||||
@ -13,23 +15,33 @@ import "C"
|
||||
import (
|
||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl"
|
||||
"github.com/hajimehoshi/go-ebiten/ui"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Window struct {
|
||||
ui *UI
|
||||
native unsafe.Pointer
|
||||
canvas *opengl.Canvas
|
||||
funcs chan func()
|
||||
funcsDone chan struct{}
|
||||
ui *UI
|
||||
screenWidth int
|
||||
screenHeight int
|
||||
screenScale int
|
||||
native unsafe.Pointer
|
||||
canvas *opengl.Canvas
|
||||
funcs chan func()
|
||||
funcsDone chan struct{}
|
||||
windowEvents
|
||||
}
|
||||
|
||||
var windows = map[unsafe.Pointer]*Window{}
|
||||
|
||||
func runWindow(ui *UI, width, height, scale int, title string, sharedContext unsafe.Pointer) *Window {
|
||||
w := &Window{
|
||||
ui: ui,
|
||||
funcs: make(chan func()),
|
||||
funcsDone: make(chan struct{}),
|
||||
ui: ui,
|
||||
screenWidth: width,
|
||||
screenHeight: height,
|
||||
screenScale: scale,
|
||||
funcs: make(chan func()),
|
||||
funcsDone: make(chan struct{}),
|
||||
}
|
||||
|
||||
cTitle := C.CString(title)
|
||||
@ -43,6 +55,7 @@ func runWindow(ui *UI, width, height, scale int, title string, sharedContext uns
|
||||
C.size_t(height*scale),
|
||||
cTitle,
|
||||
glContext)
|
||||
windows[w.native] = w
|
||||
close(ch)
|
||||
w.loop()
|
||||
}()
|
||||
@ -76,3 +89,37 @@ func (w *Window) useContext(f func()) {
|
||||
w.funcs <- f
|
||||
<-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)
|
||||
}
|
||||
|
8
ui/ui.go
8
ui/ui.go
@ -14,15 +14,9 @@ type InputStateUpdatedEvent struct {
|
||||
Y int
|
||||
}
|
||||
|
||||
type UIEvents interface {
|
||||
ScreenSizeUpdated() <-chan ScreenSizeUpdatedEvent
|
||||
InputStateUpdated() <-chan InputStateUpdatedEvent
|
||||
}
|
||||
|
||||
type UI interface {
|
||||
PollEvents()
|
||||
Draw(func(graphics.Canvas))
|
||||
UIEvents
|
||||
CreateWindow(screenWidth, screenHeight, screenScale int, title string) Window
|
||||
}
|
||||
|
||||
type WindowEvents interface {
|
||||
|
Loading…
Reference in New Issue
Block a user