Forráskód Böngészése

Don't use a bogus singleton pattern here...

Sam Jaffe 6 éve
szülő
commit
8fb92439cb

+ 0 - 6
danmaku.xcodeproj/project.pbxproj

@@ -13,7 +13,6 @@
 		CD7E87972295FCED00D877FE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CD7E87952295FCED00D877FE /* Main.storyboard */; };
 		CD7E879A2295FCED00D877FE /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = CD7E87992295FCED00D877FE /* main.m */; };
 		CD7E87A52295FCED00D877FE /* danmakuUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = CD7E87A42295FCED00D877FE /* danmakuUITests.m */; };
-		CD7E87B12295FE5A00D877FE /* GameAdaptor.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD7E87B02295FE5A00D877FE /* GameAdaptor.cxx */; };
 		CD7E881522960D8A00D877FE /* libengine.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD7E881222960D8200D877FE /* libengine.dylib */; };
 		CD7E882422960DA000D877FE /* libgameutils.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD7E882322960D9C00D877FE /* libgameutils.dylib */; };
 /* End PBXBuildFile section */
@@ -98,8 +97,6 @@
 		CD7E87A02295FCED00D877FE /* danmakuUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = danmakuUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
 		CD7E87A42295FCED00D877FE /* danmakuUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = danmakuUITests.m; sourceTree = "<group>"; };
 		CD7E87A62295FCED00D877FE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
-		CD7E87AF2295FE5A00D877FE /* GameAdaptor.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GameAdaptor.h; sourceTree = "<group>"; };
-		CD7E87B02295FE5A00D877FE /* GameAdaptor.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = GameAdaptor.cxx; sourceTree = "<group>"; };
 		CD7E880B22960D8200D877FE /* engine.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = engine.xcodeproj; path = ../game/engine/engine.xcodeproj; sourceTree = "<group>"; };
 		CD7E881D22960D9C00D877FE /* gameutils.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = gameutils.xcodeproj; path = ../game/util/gameutils.xcodeproj; sourceTree = "<group>"; };
 		CD7E882922960DBB00D877FE /* math.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = math.xcodeproj; path = ../game/math/math.xcodeproj; sourceTree = "<group>"; };
@@ -156,8 +153,6 @@
 				CD7E87872295FCEA00D877FE /* AppDelegate.m */,
 				CD7E878D2295FCEA00D877FE /* GameScene.h */,
 				CD7E878E2295FCEA00D877FE /* GameScene.mm */,
-				CD7E87AF2295FE5A00D877FE /* GameAdaptor.h */,
-				CD7E87B02295FE5A00D877FE /* GameAdaptor.cxx */,
 				CD7E87932295FCEC00D877FE /* Assets.xcassets */,
 				CD7E87952295FCED00D877FE /* Main.storyboard */,
 				CD7E87982295FCED00D877FE /* Info.plist */,
@@ -382,7 +377,6 @@
 			buildActionMask = 2147483647;
 			files = (
 				CD7E878F2295FCEA00D877FE /* GameScene.mm in Sources */,
-				CD7E87B12295FE5A00D877FE /* GameAdaptor.cxx in Sources */,
 				CD7E879A2295FCED00D877FE /* main.m in Sources */,
 				CD7E87882295FCEA00D877FE /* AppDelegate.m in Sources */,
 			);

+ 0 - 81
danmaku/GameAdaptor.cxx

@@ -1,81 +0,0 @@
-//
-//  GameAdaptor.cxx
-//  danmaku
-//
-//  Created by Sam Jaffe on 5/22/19.
-//  Copyright © 2019 Sam Jaffe. All rights reserved.
-//
-
-#include "GameAdaptor.h"
-
-//#include "game/graphics/renderer.hpp"
-
-#include "game/util/time.hpp"
-#include "game/util/env.hpp"
-#include "game/math/math_fwd.hpp"
-#include "vector/vector.hpp"
-//#include "Engine/Utilities/Logger.hpp"
-//#include "Game/TheGame.hpp"
-
-#include "GameAdaptor.h"
-
-void init_gl() {
-  //  Renderer::GetInstance().Clear();
-}
-
-void draw_screen() {
-  static engine::timestamp lastTime = engine::clock::now();
-  engine::timestamp now = engine::clock::now();
-  std::chrono::duration<double> delta(now - lastTime);
-  //  Logger::Instance().debugf("RTick: %f", now - lastTime);
-  //  TheGame::GetInstance().Render();
-  lastTime = now;
-}
-
-namespace env {
-  namespace detail {
-    extern void resize_screen(math::vec2i const&);
-  }
-}
-
-void screen_resize(int width, int height) {
-  env::detail::resize_screen(make_vector(width, height));
-}
-
-// flags:
-// alnum: 0x00000100
-// arrow: 0x00a00000
-// ?ANY:  0x00000100
-
-// CTRL:  0x00040001
-// LSHIFT:0x00020002
-// RSHIFT:0x00020004
-// LCMD:  0x00100008
-// RCMD:  0x00100010
-// LALT:  0x00080020
-// RALT:  0x00080040
-// FN:    0x00800000
-void key_down(unsigned short key) {
-//  TheGame::GetInstance().KeyPressEvent(key);
-}
-
-void key_up(unsigned short key) {
-//  TheGame::GetInstance().KeyReleaseEvent(key);
-}
-
-void game_deactivate() {
-//  Env::StopClock();
-}
-
-void game_activate() {
-//  Env::StartClock();
-}
-
-void run_game() {
-  static engine::timestamp lastTime = engine::clock::now();
-  engine::timestamp now = engine::clock::now();
-  std::chrono::duration<double> delta(now - lastTime);
-//  Logger::Instance().debugf("UTick: %f", now - lastTime);
-//  TheGame::GetInstance().Update(now - lastTime);
-  lastTime = now;
-}

+ 0 - 20
danmaku/GameAdaptor.h

@@ -1,20 +0,0 @@
-//
-//  GameAdaptor.h
-//  danmaku
-//
-//  Created by Sam Jaffe on 5/22/19.
-//  Copyright © 2019 Sam Jaffe. All rights reserved.
-//
-
-#pragma once
-
-void init_gl();
-void draw_screen();
-void screen_resize(int width, int height);
-
-void key_down(unsigned short key);
-void key_up(unsigned short key);
-
-void game_deactivate();
-void game_activate();
-void run_game();

+ 3 - 1
danmaku/GameScene.h

@@ -9,5 +9,7 @@
 #import <Cocoa/Cocoa.h>
 
 @interface GameScene : NSOpenGLView<NSWindowDelegate>
-
+- (void) press_key:(int)key forEvent:(NSEvent *)evt ns_key:(int)local
+              down:(BOOL)pressed;
+- (void) toggle_key:(int)key forEvent:(NSEvent *)evt ns_key:(int)local;
 @end

+ 50 - 40
danmaku/GameScene.mm

@@ -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];
 }