Advertisement
Guest User

Untitled

a guest
Sep 30th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. **
  2. * This software is in the public domain. Where that dedication is not recognized,
  3. * you are granted a perpetual, irrevokable license to copy and modify this file
  4. * as you see fit.
  5. *
  6. * Requires SDL 2.0.4.
  7. * Devices that do not support Metal are not handled currently.
  8. **/
  9.  
  10. #import <Cocoa/Cocoa.h>
  11. #import <Metal/Metal.h>
  12. #import <QuartzCore/CAMetalLayer.h>
  13.  
  14. #include <SDL2/SDL.h>
  15. #include <SDL2/SDL_syswm.h>
  16.  
  17. @interface MetalView : NSView
  18.  
  19. @property (nonatomic) CAMetalLayer *metalLayer;
  20.  
  21. @end
  22.  
  23. @implementation MetalView
  24.  
  25. + (Class)layerClass
  26. {
  27. return [CAMetalLayer class];
  28. }
  29.  
  30. - (instancetype)initWithFrame:(CGRect)frame
  31. {
  32. if ((self = [super initWithFrame:frame])) {
  33. /* Resize properly when rotated. */
  34. //self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  35.  
  36. /* Use the screen's native scale (retina resolution, when available.) */
  37. //self.contentScaleFactor = [UIScreen mainScreen].nativeScale;
  38.  
  39. _metalLayer = (CAMetalLayer *) self.layer;
  40. _metalLayer.opaque = YES;
  41. _metalLayer.device = MTLCreateSystemDefaultDevice();
  42.  
  43. [self updateDrawableSize];
  44. }
  45.  
  46. return self;
  47. }
  48.  
  49. /* Set the size of the metal drawables when the view is resized. */
  50. - (void)layoutSubviews
  51. {
  52. //[super layoutSubviews];
  53. [self updateDrawableSize];
  54. }
  55.  
  56. - (void)updateDrawableSize
  57. {
  58. CGSize size = self.bounds.size;
  59. //size.width *= self.contentScaleFactor;
  60. //size.height *= self.contentScaleFactor;
  61.  
  62. _metalLayer.drawableSize = size;
  63. }
  64.  
  65. @end
  66.  
  67. int main(int argc, char *argv[])
  68. {
  69. SDL_InitSubSystem(SDL_INIT_VIDEO);
  70.  
  71. SDL_Window *window = SDL_CreateWindow("", 0, 0, 480, 320, SDL_WINDOW_RESIZABLE);
  72.  
  73. SDL_SysWMinfo info;
  74. SDL_VERSION(&info.version);
  75. SDL_GetWindowWMInfo(window, &info);
  76.  
  77. /**
  78. * As of SDL 2.0.4, a view and view controller is associated with a
  79. * window as soon as the window is created. In previous SDL versions
  80. * that didn't happen until SDL_GL_CreateContext(window) was called.
  81. **/
  82. NSView *sdlview = info.info.cocoa.window.contentView; //info.info.uikit.window.rootViewController.view;
  83.  
  84. MetalView *metalview = [[MetalView alloc] initWithFrame:sdlview.frame];
  85. [sdlview addSubview:metalview];
  86.  
  87. MTLRenderPassDescriptor *renderdesc = [MTLRenderPassDescriptor renderPassDescriptor];
  88. MTLRenderPassColorAttachmentDescriptor *colorattachment = renderdesc.colorAttachments[0];
  89.  
  90. /* Clear to a red-orange color when beginning the render pass. */
  91. colorattachment.clearColor = MTLClearColorMake(1.0, 0.3, 0.0, 1.0);
  92. colorattachment.loadAction = MTLLoadActionClear;
  93. colorattachment.storeAction = MTLStoreActionStore;
  94.  
  95. id <MTLCommandQueue> queue = [metalview.metalLayer.device newCommandQueue];
  96.  
  97. while (1) {
  98. SDL_Event e;
  99. while (SDL_PollEvent(&e)) {
  100. /* Handle SDL events. */
  101. }
  102.  
  103. @autoreleasepool {
  104. id <MTLCommandBuffer> cmdbuf = [queue commandBuffer];
  105.  
  106. id <CAMetalDrawable> drawable = [metalview.metalLayer nextDrawable];
  107. colorattachment.texture = drawable.texture;
  108.  
  109. /* The drawable's texture is cleared to the specified color here. */
  110. id <MTLRenderCommandEncoder> encoder = [cmdbuf renderCommandEncoderWithDescriptor:renderdesc];
  111. [encoder endEncoding];
  112.  
  113. [cmdbuf presentDrawable:drawable];
  114. [cmdbuf commit];
  115. }
  116. }
  117.  
  118. [metalview removeFromSuperview];
  119.  
  120. SDL_DestroyWindow(window);
  121. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  122.  
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement