Remove void*

This commit is contained in:
Hajime Hoshi 2013-12-31 03:17:39 +09:00
parent f82241b56f
commit cc14882cb2
7 changed files with 40 additions and 38 deletions

View File

@ -28,7 +28,7 @@ func main() {
u := cocoa.UI() u := cocoa.UI()
textureFactory := cocoa.TextureFactory() textureFactory := cocoa.TextureFactory()
window := u.CreateWindow(screenWidth, screenHeight, screenScale, title) window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title)
textureFactoryEvents := textureFactory.Events() textureFactoryEvents := textureFactory.Events()

View File

@ -45,6 +45,11 @@ void ebiten_WindowClosed(void* nativeWindow);
return self; return self;
} }
- (void)dealloc {
[self->glContext_ release];
[super dealloc];
}
- (NSOpenGLContext*)glContext { - (NSOpenGLContext*)glContext {
return self->glContext_; return self->glContext_;
} }
@ -73,7 +78,6 @@ void ebiten_WindowClosed(void* nativeWindow);
(void)alert; (void)alert;
(void)contextInfo; (void)contextInfo;
if (returnCode == NSAlertDefaultReturn) { if (returnCode == NSAlertDefaultReturn) {
[self->glContext_ release];
[self close]; [self close];
ebiten_WindowClosed(self); ebiten_WindowClosed(self);
} }

View File

@ -34,7 +34,7 @@ void StartApplication(void) {
[app finishLaunching]; [app finishLaunching];
} }
void* CreateGLContext(void* sharedGLContext) { NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext) {
NSOpenGLPixelFormatAttribute attributes[] = { NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAWindow, NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer, NSOpenGLPFADoubleBuffer,
@ -46,13 +46,12 @@ void* CreateGLContext(void* sharedGLContext) {
initWithAttributes:attributes]; initWithAttributes:attributes];
NSOpenGLContext* glContext = NSOpenGLContext* glContext =
[[NSOpenGLContext alloc] initWithFormat:format [[NSOpenGLContext alloc] initWithFormat:format
shareContext:(NSOpenGLContext*)sharedGLContext]; shareContext:sharedGLContext];
[format release]; [format release];
return glContext; return glContext;
} }
void* CreateWindow(size_t width, size_t height, const char* title, void* glContext_) { NSWindow* CreateWindow(size_t width, size_t height, const char* title, NSOpenGLContext* glContext) {
NSOpenGLContext* glContext = (NSOpenGLContext*)glContext_;
NSSize size = NSMakeSize(width, height); NSSize size = NSMakeSize(width, height);
EbitenWindow* window = [[EbitenWindow alloc] EbitenWindow* window = [[EbitenWindow alloc]
initWithSize:size initWithSize:size
@ -87,8 +86,7 @@ void PollEvents(void) {
} }
} }
void UseGLContext(void* glContextPtr) { void UseGLContext(NSOpenGLContext* glContext) {
NSOpenGLContext* glContext = (NSOpenGLContext*)glContextPtr;
CGLContextObj cglContext = [glContext CGLContextObj]; CGLContextObj cglContext = [glContext CGLContextObj];
CGLLockContext(cglContext); CGLLockContext(cglContext);
[glContext makeCurrentContext]; [glContext makeCurrentContext];
@ -102,16 +100,6 @@ void UnuseGLContext(void) {
CGLUnlockContext(cglContext); CGLUnlockContext(cglContext);
} }
void* GetGLContext(void* window) { NSOpenGLContext* GetGLContext(NSWindow* window) {
return [(EbitenWindow*)window glContext]; return [(EbitenWindow*)window glContext];
} }
void BeginDrawing(void* window) {
// TODO: CGLLock
[[(EbitenWindow*)window glContext] makeCurrentContext];
glClear(GL_COLOR_BUFFER_BIT);
}
void EndDrawing(void* window) {
[[(EbitenWindow*)window glContext] flushBuffer];
}

View File

@ -1,17 +1,18 @@
package cocoa package cocoa
// void* CreateGLContext(void* sharedGLContext); // @class NSOpenGLContext;
// void UseGLContext(void* glContext); //
// NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext);
// void UseGLContext(NSOpenGLContext* glContext);
// void UnuseGLContext(void); // void UnuseGLContext(void);
// //
import "C" import "C"
import ( import (
"runtime" "runtime"
"unsafe"
) )
type textureFactory struct { type textureFactory struct {
sharedContext unsafe.Pointer sharedContext *C.NSOpenGLContext
funcs chan func() funcs chan func()
funcsDone chan struct{} funcsDone chan struct{}
} }
@ -24,7 +25,7 @@ func runTextureFactory() *textureFactory {
ch := make(chan struct{}) ch := make(chan struct{})
go func() { go func() {
runtime.LockOSThread() runtime.LockOSThread()
t.sharedContext = C.CreateGLContext(unsafe.Pointer(nil)) t.sharedContext = C.CreateGLContext(nil)
close(ch) close(ch)
t.loop() t.loop()
}() }()

View File

@ -47,7 +47,7 @@ func TextureFactory() graphics.TextureFactory {
return getCurrentUI() return getCurrentUI()
} }
func (u *cocoaUI) CreateWindow(width, height, scale int, title string) ui.Window { func (u *cocoaUI) CreateGameWindow(width, height, scale int, title string) ui.GameWindow {
return u.textureFactory.createWindow(u, width, height, scale, title) return u.textureFactory.createWindow(u, width, height, scale, title)
} }

View File

@ -4,11 +4,16 @@ package cocoa
// //
// #include "input.h" // #include "input.h"
// //
// void* CreateWindow(size_t width, size_t height, const char* title, void* glContext); // @class NSWindow;
// void* CreateGLContext(void* sharedGLContext); // @class NSOpenGLContext;
// //
// void* GetGLContext(void* window); // typedef NSWindow* NSWindowPtr;
// void UseGLContext(void* glContext); //
// NSWindow* CreateWindow(size_t width, size_t height, const char* title, NSOpenGLContext* glContext);
// NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext);
//
// NSOpenGLContext* GetGLContext(NSWindow* window);
// void UseGLContext(NSOpenGLContext* glContext);
// void UnuseGLContext(void); // void UnuseGLContext(void);
// //
import "C" import "C"
@ -26,7 +31,7 @@ type Window struct {
screenHeight int screenHeight int
screenScale int screenScale int
closed bool closed bool
native unsafe.Pointer native *C.NSWindow
pressedKeys map[ui.Key]struct{} pressedKeys map[ui.Key]struct{}
context *opengl.Context context *opengl.Context
funcs chan func() funcs chan func()
@ -34,9 +39,9 @@ type Window struct {
events chan interface{} events chan interface{}
} }
var windows = map[unsafe.Pointer]*Window{} var windows = map[*C.NSWindow]*Window{}
func runWindow(cocoaUI *cocoaUI, width, height, scale int, title string, sharedContext unsafe.Pointer) *Window { func runWindow(cocoaUI *cocoaUI, width, height, scale int, title string, sharedContext *C.NSOpenGLContext) *Window {
w := &Window{ w := &Window{
ui: cocoaUI, ui: cocoaUI,
screenWidth: width, screenWidth: width,
@ -116,7 +121,7 @@ func (w *Window) notify(e interface{}) {
// Now this function is not used anywhere. // Now this function is not used anywhere.
//export ebiten_WindowSizeUpdated //export ebiten_WindowSizeUpdated
func ebiten_WindowSizeUpdated(nativeWindow unsafe.Pointer, width, height int) { func ebiten_WindowSizeUpdated(nativeWindow C.NSWindowPtr, width, height int) {
w := windows[nativeWindow] w := windows[nativeWindow]
e := ui.WindowSizeUpdatedEvent{width, height} e := ui.WindowSizeUpdatedEvent{width, height}
w.notify(e) w.notify(e)
@ -141,7 +146,7 @@ var cocoaKeyCodeToKey = map[int]ui.Key{
} }
//export ebiten_KeyDown //export ebiten_KeyDown
func ebiten_KeyDown(nativeWindow unsafe.Pointer, keyCode int) { func ebiten_KeyDown(nativeWindow C.NSWindowPtr, keyCode int) {
key, ok := cocoaKeyCodeToKey[keyCode] key, ok := cocoaKeyCodeToKey[keyCode]
if !ok { if !ok {
return return
@ -152,7 +157,7 @@ func ebiten_KeyDown(nativeWindow unsafe.Pointer, keyCode int) {
} }
//export ebiten_KeyUp //export ebiten_KeyUp
func ebiten_KeyUp(nativeWindow unsafe.Pointer, keyCode int) { func ebiten_KeyUp(nativeWindow C.NSWindowPtr, keyCode int) {
key, ok := cocoaKeyCodeToKey[keyCode] key, ok := cocoaKeyCodeToKey[keyCode]
if !ok { if !ok {
return return
@ -163,7 +168,7 @@ func ebiten_KeyUp(nativeWindow unsafe.Pointer, keyCode int) {
} }
//export ebiten_MouseStateUpdated //export ebiten_MouseStateUpdated
func ebiten_MouseStateUpdated(nativeWindow unsafe.Pointer, inputType C.InputType, cx, cy C.int) { func ebiten_MouseStateUpdated(nativeWindow C.NSWindowPtr, inputType C.InputType, cx, cy C.int) {
w := windows[nativeWindow] w := windows[nativeWindow]
if inputType == C.InputTypeMouseUp { if inputType == C.InputTypeMouseUp {
@ -190,7 +195,7 @@ func ebiten_MouseStateUpdated(nativeWindow unsafe.Pointer, inputType C.InputType
} }
//export ebiten_WindowClosed //export ebiten_WindowClosed
func ebiten_WindowClosed(nativeWindow unsafe.Pointer) { func ebiten_WindowClosed(nativeWindow C.NSWindowPtr) {
w := windows[nativeWindow] w := windows[nativeWindow]
w.closed = true w.closed = true
w.notify(ui.WindowClosedEvent{}) w.notify(ui.WindowClosedEvent{})

View File

@ -34,10 +34,14 @@ type WindowClosedEvent struct {
type UI interface { type UI interface {
PollEvents() PollEvents()
CreateWindow(screenWidth, screenHeight, screenScale int, title string) Window CreateGameWindow(screenWidth, screenHeight, screenScale int, title string) GameWindow
} }
type Window interface { type Window interface {
Events() <-chan interface{}
}
type GameWindow interface {
Draw(func(graphics.Context)) Draw(func(graphics.Context))
Events() <-chan interface{} Events() <-chan interface{}
} }