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 title = "Ebiten Demo"
ui := cocoa.New(screenWidth, screenHeight, screenScale, title)
ui.Start()
ui.InitTextures(game.InitTextures)
lock := sync.Mutex{}

View File

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

View File

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

View File

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

View File

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

View File

@ -5,7 +5,24 @@
#import "ebiten_controller.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) {
for (;;) {
@ -20,30 +37,10 @@ void PollEvents(void) {
}
}
void Start(size_t width, size_t height, size_t scale, const char* title) {
NSSize size = NSMakeSize(width * scale, height * scale);
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* window) {
[(EbitenWindow*)window beginDrawing];
}
void BeginDrawing(void) {
[currentWindow beginDrawing];
}
void EndDrawing(void) {
[currentWindow endDrawing];
void EndDrawing(void* window) {
[(EbitenWindow*)window endDrawing];
}