|
|
@@ -11,9 +11,15 @@
|
|
|
#include <OpenGL/gl3.h>
|
|
|
|
|
|
#include "game/engine/events.hpp"
|
|
|
-#include "GameAdaptor.h"
|
|
|
+#include "game/engine/game_dispatch.hpp"
|
|
|
|
|
|
-@implementation GameScene
|
|
|
+namespace env { namespace detail {
|
|
|
+ extern void resize_screen(math::vec2i const&);
|
|
|
+}}
|
|
|
+
|
|
|
+@implementation GameScene {
|
|
|
+ engine::game_dispatch game;
|
|
|
+};
|
|
|
|
|
|
- (id)initWithFrame:(NSRect)aRect {
|
|
|
NSOpenGLPixelFormatAttribute attr[] = {
|
|
|
@@ -37,7 +43,7 @@
|
|
|
}
|
|
|
|
|
|
- (void)prepareOpenGL {
|
|
|
- init_gl();
|
|
|
+ // Renderer::GetInstance().Clear();
|
|
|
// enable vertical sychronization to refresh rate
|
|
|
const GLint vals = 0x01;
|
|
|
[[self openGLContext] setValues:&vals forParameter:NSOpenGLCPSwapInterval];
|
|
|
@@ -50,19 +56,22 @@
|
|
|
// glClear(GL_COLOR_BUFFER_BIT);
|
|
|
// glLoadIdentity();
|
|
|
|
|
|
- screen_resize((int)dirtyRect.size.width, (int)dirtyRect.size.height);
|
|
|
- draw_screen();
|
|
|
+ env::detail::resize_screen({{(int)dirtyRect.size.width,
|
|
|
+ (int)dirtyRect.size.height}});
|
|
|
+ game.render();
|
|
|
// glFlush();
|
|
|
// the correct way to do double buffering on Mac is this:
|
|
|
[[self openGLContext] flushBuffer];
|
|
|
|
|
|
int err;
|
|
|
- if ((err = glGetError()) != 0)
|
|
|
+ if ((err = glGetError()) != 0) {
|
|
|
NSLog(@"glGetError(): %d", err);
|
|
|
- }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- (void)reshape {
|
|
|
- NSRect newFrame = {0, 0, [[self window] frame].size.width, [[self window] frame].size.height};
|
|
|
+ NSRect newFrame = {0, 0, [[self window] frame].size.width,
|
|
|
+ [[self window] frame].size.height};
|
|
|
[self setFrame:newFrame];
|
|
|
|
|
|
// window resize; width and height are in pixel coordinates
|
|
|
@@ -73,29 +82,24 @@
|
|
|
// glViewport(0,0, screen_w, screen_h);
|
|
|
|
|
|
// here I cast floats to ints; most systems use integer coordinate systems
|
|
|
- screen_resize((int)screen_w, (int)screen_h);
|
|
|
+ env::detail::resize_screen({{(int)screen_w, (int)screen_h}});
|
|
|
}
|
|
|
|
|
|
- (BOOL)acceptsFirstResponder {
|
|
|
return YES;
|
|
|
}
|
|
|
|
|
|
-void bind_key_up(NSEvent * evt, int ns_key, int key) {
|
|
|
- if ([evt keyCode] == ns_key) { key_up(key); }
|
|
|
-}
|
|
|
-
|
|
|
-void bind_key_down(NSEvent * evt, int ns_key, int key) {
|
|
|
- if ([evt keyCode] == ns_key) { key_down(key); }
|
|
|
+- (void) press_key:(int)key forEvent:(NSEvent *)evt ns_key:(int)local
|
|
|
+ down:(BOOL)pressed {
|
|
|
+ if ([evt keyCode] == local) {
|
|
|
+ game.process_key_event(key, pressed);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-void bind_key_toggle(NSEvent * evt, int ns_key, int key) {
|
|
|
+- (void) toggle_key:(int)key forEvent:(NSEvent *)evt ns_key:(int)local {
|
|
|
NSUInteger flags = [evt modifierFlags] &
|
|
|
NSEventModifierFlagDeviceIndependentFlagsMask;
|
|
|
- if (flags & ns_key) {
|
|
|
- key_down(key);
|
|
|
- } else {
|
|
|
- key_up(key);
|
|
|
- }
|
|
|
+ game.process_key_event(key, flags & local);
|
|
|
}
|
|
|
|
|
|
- (void)keyDown:(NSEvent *)theEvent {
|
|
|
@@ -112,17 +116,21 @@ void bind_key_toggle(NSEvent * evt, int ns_key, int key) {
|
|
|
|
|
|
//(unsigned long)[theEvent modifierFlags]
|
|
|
using namespace engine::keys;
|
|
|
- bind_key_toggle(theEvent, NSEventModifierFlagShift, MOD_SHIFT);
|
|
|
- bind_key_toggle(theEvent, NSEventModifierFlagControl, MOD_CONTROL);
|
|
|
- bind_key_toggle(theEvent, NSEventModifierFlagOption, MOD_ALT);
|
|
|
- bind_key_toggle(theEvent, NSEventModifierFlagCommand, MOD_COMMAND);
|
|
|
- bind_key_down(theEvent, 0x007e, UP_ARROW);
|
|
|
- bind_key_down(theEvent, 0x007d, DOWN_ARROW);
|
|
|
- bind_key_down(theEvent, 0x007b, LEFT_ARROW);
|
|
|
- bind_key_down(theEvent, 0x007c, RIGHT_ARROW);
|
|
|
-
|
|
|
+ [self toggle_key:MOD_SHIFT forEvent:theEvent
|
|
|
+ ns_key:NSEventModifierFlagShift];
|
|
|
+ [self toggle_key:MOD_CONTROL forEvent:theEvent
|
|
|
+ ns_key:NSEventModifierFlagControl];
|
|
|
+ [self toggle_key:MOD_ALT forEvent:theEvent
|
|
|
+ ns_key:NSEventModifierFlagOption];
|
|
|
+ [self toggle_key:MOD_COMMAND forEvent:theEvent
|
|
|
+ ns_key:NSEventModifierFlagCommand];
|
|
|
+ [self press_key:UP_ARROW forEvent:theEvent ns_key:0x007e down:YES];
|
|
|
+ [self press_key:DOWN_ARROW forEvent:theEvent ns_key:0x007d down:YES];
|
|
|
+ [self press_key:LEFT_ARROW forEvent:theEvent ns_key:0x007b down:YES];
|
|
|
+ [self press_key:RIGHT_ARROW forEvent:theEvent ns_key:0x007c down:YES];
|
|
|
+
|
|
|
// NSLog(@"keyDown -> { 0x%04x, 0x%08lx, 0x%02x=%c }", [theEvent keyCode], [theEvent modifierFlags], c, c);
|
|
|
- key_down(c);
|
|
|
+ game.process_key_event(c, true);
|
|
|
}
|
|
|
|
|
|
- (void)keyUp:(NSEvent *)theEvent {
|
|
|
@@ -136,12 +144,12 @@ void bind_key_toggle(NSEvent * evt, int ns_key, int key) {
|
|
|
}
|
|
|
|
|
|
using namespace engine::keys;
|
|
|
- bind_key_up(theEvent, 0x007e, UP_ARROW);
|
|
|
- bind_key_up(theEvent, 0x007d, DOWN_ARROW);
|
|
|
- bind_key_up(theEvent, 0x007b, LEFT_ARROW);
|
|
|
- bind_key_up(theEvent, 0x007c, RIGHT_ARROW);
|
|
|
+ [self press_key:UP_ARROW forEvent:theEvent ns_key:0x007e down:NO];
|
|
|
+ [self press_key:DOWN_ARROW forEvent:theEvent ns_key:0x007d down:NO];
|
|
|
+ [self press_key:LEFT_ARROW forEvent:theEvent ns_key:0x007b down:NO];
|
|
|
+ [self press_key:RIGHT_ARROW forEvent:theEvent ns_key:0x007c down:NO];
|
|
|
|
|
|
- key_up(c);
|
|
|
+ game.process_key_event(c, false);
|
|
|
}
|
|
|
|
|
|
static NSTimer *timer = nil;
|
|
|
@@ -153,16 +161,18 @@ static NSTimeInterval const FRAME_INTERVAL = 1.0 / 60.0;
|
|
|
[timer invalidate];
|
|
|
[frameTimer invalidate];
|
|
|
|
|
|
- game_deactivate(); // freeze, pause
|
|
|
+ // env::pause_clock();
|
|
|
[self setNeedsDisplay:YES];
|
|
|
}
|
|
|
|
|
|
- (void)windowDidBecomeMain:(NSNotification *)notification {
|
|
|
NSLog(@"window did become main");
|
|
|
|
|
|
- game_activate();
|
|
|
+ // env::resume_clock();
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
|
|
+ // TODO (sjaffe): Become able to change framerate
|
|
|
+ // TODO (sjaffe): Link this with env::fps::v60, etc.
|
|
|
timer = [NSTimer timerWithTimeInterval:FRAME_INTERVAL
|
|
|
target:self
|
|
|
selector:@selector(timerEvent:)
|
|
|
@@ -181,12 +191,12 @@ static NSTimeInterval const FRAME_INTERVAL = 1.0 / 60.0;
|
|
|
}
|
|
|
|
|
|
- (void)renderEvent:(NSTimer *)t {
|
|
|
- draw_screen(); // FIXME
|
|
|
+ game.render();
|
|
|
[[self openGLContext] flushBuffer];
|
|
|
}
|
|
|
|
|
|
- (void)timerEvent:(NSTimer *)t {
|
|
|
- run_game();
|
|
|
+ game.update();
|
|
|
[self setNeedsDisplay:YES];
|
|
|
}
|
|
|
|