mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
Refactoring
This commit is contained in:
parent
bcb8c0d45a
commit
0a16107cd6
@ -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{}
|
||||
|
@ -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
|
||||
|
@ -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];
|
||||
}
|
||||
|
||||
|
@ -7,9 +7,6 @@
|
||||
|
||||
@interface EbitenController : NSObject<NSApplicationDelegate>
|
||||
|
||||
- (id)initWithWindow:(NSWindow*)window;
|
||||
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
|
@ -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
|
||||
|
@ -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];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user