1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
4 #ifndef tools_metal_sg_viewer_hm
5 #define tools_metal_sg_viewer_hm
9 #if MAC_OS_X_VERSION_MAX_ALLOWED <= 1050
10 @interface tools_metal_sg_viewer_win_delegate : NSObject {
12 @interface tools_metal_sg_viewer_win_delegate : NSObject<NSWindowDelegate> {
14 tools::metal::session* m_session;
16 - (id)init:(tools::metal::session*)a_session;
18 - (void)windowWillClose:(NSNotification*)a_not;
20 @implementation tools_metal_sg_viewer_win_delegate
21 - (id)init:(tools::metal::session*)a_session {
22 if(self = [super init]) {}
23 m_session = a_session;
26 - (void)dealloc {[super dealloc];}
27 - (void)windowWillClose:(NSNotification*)a_not {
28 m_session->set_to_exit();
40 class sg_viewer : public ui_viewer {
41 typedef ui_viewer parent;
43 virtual void win_render() { //ui_viewer::win_render()
47 if(m_mgr_gra.begin_render(0,0,m_ww,m_wh,
52 render action(m_mgr_gra,m_out,m_ww,m_wh);
53 action.state().m_use_gsto = m_use_gsto;
55 if(!action.end()) { //check that matrices stack are ok.
56 m_out << "exib::sg::viewer : bad gl_action end." << std::endl;
58 m_mgr_gra.end_render();
62 sg_viewer(session& a_session,
63 int a_x = 0,int a_y = 0,
64 unsigned int a_width = 500,unsigned int a_height = 500,
65 const std::string& a_win_title = "")
66 :parent(a_session.out(),a_width,a_height)
68 ,m_mgr_gra(a_session.out())
72 //////////////////////////////////////////////////////////
73 /// windowing : //////////////////////////////////////////
74 //////////////////////////////////////////////////////////
75 m_win = m_session.create_window(a_win_title.c_str(),a_x,a_y,a_width,a_height);
76 if(!m_win) return; //throw
78 tools_metal_sg_viewer_win_delegate* _win_delegate = [[tools_metal_sg_viewer_win_delegate alloc] init:&a_session];
79 [m_win setDelegate:_win_delegate];
83 rect = [m_win contentRectForFrameRect:rect]; //window content true size.
84 {int w = rect.size.width;
85 int h = rect.size.height;
86 //::printf("debug : sg_viewer::cstor %d %d\n",w,h);
87 parent::set_size(w,h);}
89 m_view = [[tools_metal_view alloc] init:rect device:m_mgr_gra.device() viewer:this];
90 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
92 m_view.enableSetNeedsDisplay = true;
94 //m_view.presentsWithTransaction = true; //it does not help.
95 m_view.depthStencilPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
96 //m_view.clearDepth = -100000;
97 //m_view.clearStencil = 0;
99 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
101 tools_metal_view_controller* view_controller = [[tools_metal_view_controller alloc] init];
102 m_view.delegate = view_controller;
105 m_mgr_gra.set_view(m_view);
106 [m_win setContentView:m_view];
109 virtual ~sg_viewer() {
111 m_session.delete_window(m_win);
115 sg_viewer(const sg_viewer& a_from)
117 ,m_session(a_from.m_session)
118 ,m_mgr_gra(a_from.m_mgr_gra)
121 sg_viewer& operator=(const sg_viewer& a_from){
122 parent::operator=(a_from);
123 m_mgr_gra = a_from.m_mgr_gra;
124 m_win = a_from.m_win;
128 bool has_window() const {return m_win?true:false;} //for SWIG
131 if(!m_win) return false;
132 m_session.show_window(m_win);
137 metal::manager m_mgr_gra;
139 tools_metal_view* m_view;