Refactoring

This commit is contained in:
Hajime Hoshi 2013-11-24 01:17:22 +09:00
parent bcb8c0d45a
commit 0a16107cd6
6 changed files with 44 additions and 60 deletions

View File

@ -51,7 +51,6 @@ func main() {
const fps = 60 const fps = 60
const title = "Ebiten Demo" const title = "Ebiten Demo"
ui := cocoa.New(screenWidth, screenHeight, screenScale, title) ui := cocoa.New(screenWidth, screenHeight, screenScale, title)
ui.Start()
ui.InitTextures(game.InitTextures) ui.InitTextures(game.InitTextures)
lock := sync.Mutex{} lock := sync.Mutex{}

View File

@ -6,10 +6,11 @@ package cocoa
// #include <stdlib.h> // #include <stdlib.h>
// #include "input.h" // #include "input.h"
// //
// void Start(size_t width, size_t height, size_t scale, const char* title); // void StartApplication(void);
// void* CreateWindow(size_t width, size_t height, const char* title);
// void PollEvents(void); // void PollEvents(void);
// void BeginDrawing(void); // void BeginDrawing(void* window);
// void EndDrawing(void); // void EndDrawing(void* window);
// //
import "C" import "C"
import ( import (
@ -47,10 +48,10 @@ type UI struct {
screenWidth int screenWidth int
screenHeight int screenHeight int
screenScale int screenScale int
title string
graphicsDevice *opengl.Device graphicsDevice *opengl.Device
gameContext *GameContext gameContext *GameContext
gameContextLock sync.Mutex gameContextLock sync.Mutex
window unsafe.Pointer
} }
var currentUI *UI var currentUI *UI
@ -63,7 +64,6 @@ func New(screenWidth, screenHeight, screenScale int, title string) *UI {
screenWidth: screenWidth, screenWidth: screenWidth,
screenHeight: screenHeight, screenHeight: screenHeight,
screenScale: screenScale, screenScale: screenScale,
title: title,
gameContext: &GameContext{ gameContext: &GameContext{
screenWidth: screenWidth, screenWidth: screenWidth,
screenHeight: screenHeight, screenHeight: screenHeight,
@ -71,22 +71,22 @@ func New(screenWidth, screenHeight, screenScale int, title string) *UI {
}, },
gameContextLock: sync.Mutex{}, gameContextLock: sync.Mutex{},
} }
currentUI = ui
return ui
}
func (ui *UI) Start() { cTitle := C.CString(title)
cTitle := C.CString(ui.title)
defer C.free(unsafe.Pointer(cTitle)) defer C.free(unsafe.Pointer(cTitle))
C.Start(C.size_t(ui.screenWidth), C.StartApplication()
C.size_t(ui.screenHeight),
C.size_t(ui.screenScale), ui.window = C.CreateWindow(C.size_t(ui.screenWidth * ui.screenScale),
C.size_t(ui.screenHeight * ui.screenScale),
cTitle) cTitle)
ui.graphicsDevice = opengl.NewDevice( ui.graphicsDevice = opengl.NewDevice(
ui.screenWidth, ui.screenWidth,
ui.screenHeight, ui.screenHeight,
ui.screenScale) ui.screenScale)
currentUI = ui
return ui
} }
func (ui *UI) PollEvents() { func (ui *UI) PollEvents() {
@ -94,9 +94,9 @@ func (ui *UI) PollEvents() {
} }
func (ui *UI) InitTextures(f func(graphics.TextureFactory)) { func (ui *UI) InitTextures(f func(graphics.TextureFactory)) {
C.BeginDrawing() C.BeginDrawing(ui.window)
f(ui.graphicsDevice.TextureFactory()) f(ui.graphicsDevice.TextureFactory())
C.EndDrawing() C.EndDrawing(ui.window)
} }
func (ui *UI) Update(f func(ebiten.GameContext)) { func (ui *UI) Update(f func(ebiten.GameContext)) {
@ -106,9 +106,9 @@ func (ui *UI) Update(f func(ebiten.GameContext)) {
} }
func (ui *UI) Draw(f func(graphics.Context)) { func (ui *UI) Draw(f func(graphics.Context)) {
C.BeginDrawing() C.BeginDrawing(ui.window)
ui.graphicsDevice.Update(f) ui.graphicsDevice.Update(f)
C.EndDrawing() C.EndDrawing(ui.window)
} }
//export ebiten_InputUpdated //export ebiten_InputUpdated

View File

@ -3,15 +3,6 @@
#import "ebiten_controller.h" #import "ebiten_controller.h"
@implementation EbitenController { @implementation EbitenController {
@private
NSWindow* window_;
}
- (id)initWithWindow:(NSWindow*)window {
if (self = [super init]) {
self->window_ = window;
}
return self;
} }
- (void)initMenu { - (void)initMenu {
@ -32,9 +23,6 @@
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification { - (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
(void)aNotification; (void)aNotification;
NSWindow* window = self->window_;
assert(window);
[window makeKeyAndOrderFront:nil];
[self initMenu]; [self initMenu];
} }

View File

@ -7,9 +7,6 @@
@interface EbitenController : NSObject<NSApplicationDelegate> @interface EbitenController : NSObject<NSApplicationDelegate>
- (id)initWithWindow:(NSWindow*)window;
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification;
@end @end
#endif #endif

View File

@ -53,7 +53,6 @@
initWithAttributes:attributes]; initWithAttributes:attributes];
self->glContext_ = [[NSOpenGLContext alloc] initWithFormat:format self->glContext_ = [[NSOpenGLContext alloc] initWithFormat:format
shareContext:nil]; shareContext:nil];
[self->glContext_ setView:[self contentView]];
[self->glContext_ makeCurrentContext]; [self->glContext_ makeCurrentContext];
[format release]; [format release];
@ -88,7 +87,7 @@
} }
- (void)beginDrawing { - (void)beginDrawing {
[self->glContext_ makeCurrentContext]; [self->glContext_ setView:[self contentView]];
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
} }
@ -96,4 +95,8 @@
[self->glContext_ flushBuffer]; [self->glContext_ flushBuffer];
} }
- (BOOL)canBecomeMainWindow {
return YES;
}
@end @end

View File

@ -5,7 +5,24 @@
#import "ebiten_controller.h" #import "ebiten_controller.h"
#import "ebiten_window.h" #import "ebiten_window.h"
static EbitenWindow* currentWindow = 0; void StartApplication() {
EbitenController* controller = [[EbitenController alloc] init];
NSApplication* app = [NSApplication sharedApplication];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
[app setDelegate:controller];
[app finishLaunching];
[app activateIgnoringOtherApps:YES];
}
void* CreateWindow(size_t width, size_t height, const char* title) {
NSSize size = NSMakeSize(width, height);
EbitenWindow* window = [[EbitenWindow alloc]
initWithSize:size];
[window setTitle: [[NSString alloc] initWithUTF8String:title]];
[window makeKeyAndOrderFront:nil];
[window initializeGLContext];
return window;
}
void PollEvents(void) { void PollEvents(void) {
for (;;) { for (;;) {
@ -20,30 +37,10 @@ void PollEvents(void) {
} }
} }
void Start(size_t width, size_t height, size_t scale, const char* title) { void BeginDrawing(void* window) {
NSSize size = NSMakeSize(width * scale, height * scale); [(EbitenWindow*)window beginDrawing];
EbitenWindow* window = [[EbitenWindow alloc]
initWithSize:size];
[window setTitle: [[NSString alloc] initWithUTF8String:title]];
EbitenController* controller = [[EbitenController alloc]
initWithWindow:window];
NSApplication* app = [NSApplication sharedApplication];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
[app setDelegate:controller];
[app finishLaunching];
[app activateIgnoringOtherApps:YES];
currentWindow = window;
PollEvents();
[window initializeGLContext];
} }
void BeginDrawing(void) { void EndDrawing(void* window) {
[currentWindow beginDrawing]; [(EbitenWindow*)window endDrawing];
}
void EndDrawing(void) {
[currentWindow endDrawing];
} }