GameView.mm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // GameView.m
  3. // danmaku
  4. //
  5. // Created by Sam Jaffe on 5/22/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #import "GameView.h"
  9. #include <memory>
  10. #include <OpenGL/gl3.h>
  11. #include "game/engine/events.hpp"
  12. #include "game/engine/game_dispatch.hpp"
  13. #include "game/graphics/renderer.hpp"
  14. namespace env { namespace detail {
  15. extern void resize_screen(math::vec2i const&);
  16. }}
  17. @implementation GameView {
  18. std::shared_ptr<graphics::direct_renderer> renderer;
  19. std::shared_ptr<engine::game_dispatch> game;
  20. };
  21. - (void)doInit {
  22. if (renderer) return;
  23. using namespace graphics;
  24. renderer = std::make_shared<direct_renderer>(driver::openGL);
  25. game = std::make_shared<engine::game_dispatch>(renderer);
  26. }
  27. - (id)initWithFrame:(NSRect)aRect {
  28. NSOpenGLPixelFormatAttribute attr[] = {
  29. NSOpenGLPFADoubleBuffer,
  30. 0
  31. };
  32. // create pixel format
  33. NSOpenGLPixelFormat *nsglFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
  34. // create the context...
  35. if (!(self = [super initWithFrame:aRect pixelFormat:nsglFormat])) {
  36. return nil;
  37. }
  38. // make the context current
  39. [[self openGLContext] makeCurrentContext];
  40. // rest of your init method ...
  41. [self doInit];
  42. return self;
  43. }
  44. - (void)prepareOpenGL {
  45. [self doInit];
  46. renderer->clear();
  47. // enable vertical sychronization to refresh rate
  48. const GLint vals = 0x01;
  49. [[self openGLContext] setValues:&vals forParameter:NSOpenGLCPSwapInterval];
  50. const GLint opaque = 0;
  51. [[self openGLContext] setValues:&opaque
  52. forParameter:NSOpenGLCPSurfaceOpacity];
  53. }
  54. - (void)drawRect:(NSRect)dirtyRect {
  55. // glClear(GL_COLOR_BUFFER_BIT);
  56. // glLoadIdentity();
  57. env::detail::resize_screen({{(int)dirtyRect.size.width,
  58. (int)dirtyRect.size.height}});
  59. game->render();
  60. // glFlush();
  61. // the correct way to do double buffering on Mac is this:
  62. [[self openGLContext] flushBuffer];
  63. int err;
  64. if ((err = glGetError()) != 0) {
  65. NSLog(@"glGetError(): %d", err);
  66. }
  67. }
  68. - (void)reshape {
  69. NSRect newFrame = {0, 0, [[self window] frame].size.width,
  70. [[self window] frame].size.height};
  71. [self setFrame:newFrame];
  72. // window resize; width and height are in pixel coordinates
  73. // but they are floats
  74. float screen_w = [self frame].size.width;
  75. float screen_h = [self frame].size.height;
  76. // glViewport(0,0, screen_w, screen_h);
  77. // here I cast floats to ints; most systems use integer coordinate systems
  78. env::detail::resize_screen({{(int)screen_w, (int)screen_h}});
  79. }
  80. - (BOOL)acceptsFirstResponder {
  81. return YES;
  82. }
  83. - (void) press_key:(int)key forEvent:(NSEvent *)evt ns_key:(int)local
  84. down:(BOOL)pressed {
  85. if ([evt keyCode] == local) {
  86. game->process_key_event(key, pressed);
  87. }
  88. }
  89. - (void) toggle_key:(int)key forEvent:(NSEvent *)evt ns_key:(int)local {
  90. NSUInteger flags = [evt modifierFlags] &
  91. NSEventModifierFlagDeviceIndependentFlagsMask;
  92. game->process_key_event(key, flags & local);
  93. }
  94. - (void)keyDown:(NSEvent *)theEvent {
  95. if ([theEvent isARepeat])
  96. return;
  97. NSString *str = [theEvent charactersIgnoringModifiers];
  98. unichar c = [str characterAtIndex:0];
  99. // only ASCII please
  100. if (c < ' ' || c > '~') {
  101. c = 0;
  102. }
  103. //(unsigned long)[theEvent modifierFlags]
  104. using namespace engine::keys;
  105. [self toggle_key:MOD_SHIFT forEvent:theEvent
  106. ns_key:NSEventModifierFlagShift];
  107. [self toggle_key:MOD_CONTROL forEvent:theEvent
  108. ns_key:NSEventModifierFlagControl];
  109. [self toggle_key:MOD_ALT forEvent:theEvent
  110. ns_key:NSEventModifierFlagOption];
  111. [self toggle_key:MOD_COMMAND forEvent:theEvent
  112. ns_key:NSEventModifierFlagCommand];
  113. [self press_key:UP_ARROW forEvent:theEvent ns_key:0x007e down:YES];
  114. [self press_key:DOWN_ARROW forEvent:theEvent ns_key:0x007d down:YES];
  115. [self press_key:LEFT_ARROW forEvent:theEvent ns_key:0x007b down:YES];
  116. [self press_key:RIGHT_ARROW forEvent:theEvent ns_key:0x007c down:YES];
  117. // NSLog(@"keyDown -> { 0x%04x, 0x%08lx, 0x%02x=%c }", [theEvent keyCode],
  118. // [theEvent modifierFlags], c, c);
  119. game->process_key_event(c, true);
  120. }
  121. - (void)keyUp:(NSEvent *)theEvent {
  122. // NSLog(@"keyUp -> { 0x%04x, 0x%08lx }", [theEvent keyCode], [theEvent modifierFlags]);
  123. NSString *str = [theEvent charactersIgnoringModifiers];
  124. unichar c = [str characterAtIndex:0];
  125. // only ASCII please
  126. if (c < ' ' || c > '~') {
  127. c = 0;
  128. }
  129. using namespace engine::keys;
  130. [self press_key:UP_ARROW forEvent:theEvent ns_key:0x007e down:NO];
  131. [self press_key:DOWN_ARROW forEvent:theEvent ns_key:0x007d down:NO];
  132. [self press_key:LEFT_ARROW forEvent:theEvent ns_key:0x007b down:NO];
  133. [self press_key:RIGHT_ARROW forEvent:theEvent ns_key:0x007c down:NO];
  134. game->process_key_event(c, false);
  135. }
  136. static NSTimer *timer = nil;
  137. static NSTimer *frameTimer = nil;
  138. static NSTimeInterval const FRAME_INTERVAL = 1.0 / 60.0;
  139. - (void)windowDidResignMain:(NSNotification *)notification {
  140. NSLog(@"window did resign main");
  141. [timer invalidate];
  142. [frameTimer invalidate];
  143. // env::pause_clock();
  144. [self setNeedsDisplay:YES];
  145. }
  146. - (void)windowDidBecomeMain:(NSNotification *)notification {
  147. NSLog(@"window did become main");
  148. // env::resume_clock();
  149. [self setNeedsDisplay:YES];
  150. // TODO (sjaffe): Become able to change framerate
  151. // TODO (sjaffe): Link this with env::fps::v60, etc.
  152. timer = [NSTimer timerWithTimeInterval:FRAME_INTERVAL
  153. target:self
  154. selector:@selector(timerEvent:)
  155. userInfo:nil
  156. repeats:YES];
  157. frameTimer = [NSTimer timerWithTimeInterval:FRAME_INTERVAL
  158. target:self
  159. selector:@selector(renderEvent:)
  160. userInfo:nil
  161. repeats:YES];
  162. [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
  163. [[NSRunLoop currentRunLoop] addTimer:frameTimer forMode:NSDefaultRunLoopMode];
  164. }
  165. - (void)renderEvent:(NSTimer *)t {
  166. game->render();
  167. [[self openGLContext] flushBuffer];
  168. }
  169. - (void)timerEvent:(NSTimer *)t {
  170. game->update();
  171. [self setNeedsDisplay:YES];
  172. }
  173. - (void) awakeFromNib
  174. {
  175. NSOpenGLPixelFormatAttribute attributes [] = {
  176. NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)24,
  177. NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
  178. (NSOpenGLPixelFormatAttribute)0
  179. };
  180. NSOpenGLPixelFormat *pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  181. [self setPixelFormat:pf];
  182. }
  183. @end