GameScene.mm 5.5 KB

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