GameView.mm 6.5 KB

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