PageRenderTime 11ms CodeModel.GetById 9ms app.highlight 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Core_WindowEvents/MacOSXCocoa/RenderContextPimpl_MACOSX.mm

http://github.com/Akranar/daguerreo
Objective C++ | 133 lines | 113 code | 20 blank | 0 comment | 8 complexity | b6232699f7c365fd601b8f40efe3cb9e MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, LGPL-3.0, GPL-2.0
  1#include "RenderContextPimpl_MACOSX.h"
  2#include "Cocoa_WindowData.h"
  3#include "../WindowPixelFormat.h"
  4#include "../RenderContext.h"
  5#include <iostream>
  6
  7
  8RenderContextPimpl::RenderContextPimpl(WindowData * window_data, const WindowPixelFormat * pixel_format)
  9{
 10	bool use_multisample = pixel_format->GetSampleBuffers() > 0 && pixel_format->GetSamples() > 1;
 11	
 12	if (use_multisample)
 13	{
 14		SetAdvancedMultisamplingPixelFormat(pixel_format);
 15		
 16		if(!ns_opengl_pixelformat)
 17		{
 18			std::cout << "Multisampling Pixel Format Creation Failed." << std::endl;
 19			SetAdvancedPixelFormat(pixel_format);
 20		}
 21	}
 22	else 
 23	{
 24		SetAdvancedPixelFormat(pixel_format);
 25	}
 26	
 27	if (!ns_opengl_pixelformat)
 28	{
 29		std::cout << "Advanced Pixel Format Creation Failed." << std::endl;
 30		SetBasicPixelFormat(pixel_format);
 31	}
 32
 33	
 34	if (!ns_opengl_pixelformat)
 35	{
 36		std::cout << "Basic Pixel Format Creation Failed" << std::endl;
 37		throw 1;
 38	}
 39	
 40	ns_opengl_context = [[NSOpenGLContext alloc] initWithFormat:ns_opengl_pixelformat shareContext:nil];
 41	[ns_opengl_context makeCurrentContext];
 42	
 43	if (!ns_opengl_context)
 44	{
 45		std::cout << "Context Creation Failed." << std::endl;
 46		throw 1;
 47	}
 48}
 49
 50RenderContextPimpl::RenderContextPimpl(WindowData * window_data, const RenderContext * render_context)
 51{
 52	ns_opengl_pixelformat = [[NSOpenGLPixelFormat alloc] initWithCGLPixelFormatObj:[render_context->GetPimpl()->GetPixelFormatObject() CGLPixelFormatObj]];
 53	ns_opengl_context = [[NSOpenGLContext alloc] initWithFormat:ns_opengl_pixelformat shareContext:render_context->GetPimpl()->GetContextObject()];
 54	[ns_opengl_context makeCurrentContext];
 55	
 56	if (!ns_opengl_context)
 57	{
 58		std::cout << "Context Creation Failed." << std::endl;
 59		throw 1;
 60	}
 61}
 62
 63RenderContextPimpl::~RenderContextPimpl()
 64{
 65	[ns_opengl_context release];
 66	[ns_opengl_pixelformat release];
 67
 68}
 69
 70void RenderContextPimpl::SetAdvancedMultisamplingPixelFormat(const WindowPixelFormat * pixel_format)
 71{
 72	NSOpenGLPixelFormatAttribute attributes_multisampling[] =
 73	{
 74		NSOpenGLPFAWindow,
 75		NSOpenGLPFAAccelerated,
 76		NSOpenGLPFADoubleBuffer,
 77		NSOpenGLPFANoRecovery,
 78		NSOpenGLPFAMinimumPolicy,
 79		NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID()),
 80		NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetTotalColorBits(),
 81		NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetAlphaBits(),
 82		NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetDepthBits(),
 83		NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetStencilBits(),
 84		NSOpenGLPFAMultisample,
 85		NSOpenGLPFASampleBuffers, pixel_format->GetSampleBuffers(),
 86		NSOpenGLPFASamples, pixel_format->GetSamples(),
 87		NSOpenGLPFASampleAlpha,
 88		(NSOpenGLPixelFormatAttribute)nil
 89	};
 90	
 91	ns_opengl_pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes_multisampling];
 92}
 93
 94void RenderContextPimpl::SetAdvancedPixelFormat(const WindowPixelFormat * pixel_format)
 95{
 96	NSOpenGLPixelFormatAttribute attributes_advanced[] =
 97	{
 98		NSOpenGLPFAWindow,
 99		NSOpenGLPFAAccelerated,
100		NSOpenGLPFADoubleBuffer,
101		NSOpenGLPFANoRecovery,
102		NSOpenGLPFAMinimumPolicy,
103		NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID()),
104		NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetTotalColorBits(),
105		NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetAlphaBits(),
106		NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetDepthBits(),
107		NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetStencilBits(),
108		(NSOpenGLPixelFormatAttribute)nil
109	};
110	
111	ns_opengl_pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes_advanced];
112}
113void RenderContextPimpl::SetBasicPixelFormat(const WindowPixelFormat * pixel_format)
114{
115	NSOpenGLPixelFormatAttribute attributes_basic [] =
116	{
117		NSOpenGLPFAWindow,
118		NSOpenGLPFADoubleBuffer,
119		NSOpenGLPFAColorSize,
120		(NSOpenGLPixelFormatAttribute)32,
121		NSOpenGLPFADepthSize,
122		(NSOpenGLPixelFormatAttribute)16,
123		NSOpenGLPFAStencilSize,
124		(NSOpenGLPixelFormatAttribute)8,
125		NSOpenGLPFAScreenMask,
126		CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID()),
127		(NSOpenGLPixelFormatAttribute)nil
128        
129	};
130	ns_opengl_pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes_basic];
131	
132}
133