g4tools  5.4.0
sg_viewer.hm
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_metal_sg_viewer_hm
5 #define tools_metal_sg_viewer_hm
6 
7 #include "session.hm"
8 
9 #if MAC_OS_X_VERSION_MAX_ALLOWED <= 1050
10 @interface tools_metal_sg_viewer_win_delegate : NSObject {
11 #else
12 @interface tools_metal_sg_viewer_win_delegate : NSObject<NSWindowDelegate> {
13 #endif
14  tools::metal::session* m_session;
15 }
16 - (id)init:(tools::metal::session*)a_session;
17 - (void)dealloc;
18 - (void)windowWillClose:(NSNotification*)a_not;
19 @end
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;
24  return self;
25 }
26 - (void)dealloc {[super dealloc];}
27 - (void)windowWillClose:(NSNotification*)a_not {
28  m_session->set_to_exit();
29  (void)a_not;
30 }
31 @end
32 
33 #include "view.hm"
34 
35 #include "render.hm"
36 
37 namespace tools {
38 namespace metal {
39 
40 class sg_viewer : public ui_viewer {
41  typedef ui_viewer parent;
42 public:
43  virtual void win_render() { //ui_viewer::win_render()
44  if(!m_ww) return;
45  if(!m_wh) return;
46  if(!m_win) return;
47  if(m_mgr_gra.begin_render(0,0,m_ww,m_wh,
48  m_clear_color.r(),
49  m_clear_color.g(),
50  m_clear_color.b(),
51  m_clear_color.a())) {
52  render action(m_mgr_gra,m_out,m_ww,m_wh);
53  action.state().m_use_gsto = m_use_gsto;
54  m_sg.render(action);
55  if(!action.end()) { //check that matrices stack are ok.
56  m_out << "exib::sg::viewer : bad gl_action end." << std::endl;
57  }
58  m_mgr_gra.end_render();
59  }
60  }
61 public:
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)
67  ,m_session(a_session)
68  ,m_mgr_gra(a_session.out())
69  ,m_win(0)
70  ,m_view(0)
71  {
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
77 
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];
80 
81  NSRect rect;
82  rect = [m_win frame];
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);}
88 
89  m_view = [[tools_metal_view alloc] init:rect device:m_mgr_gra.device() viewer:this];
90 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
91  m_view.paused = true;
92  m_view.enableSetNeedsDisplay = true;
93 #endif
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;
98 
99 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
100 #else
101  tools_metal_view_controller* view_controller = [[tools_metal_view_controller alloc] init];
102  m_view.delegate = view_controller;
103 #endif
104 
105  m_mgr_gra.set_view(m_view);
106  [m_win setContentView:m_view];
107  [m_view release];
108  }
109  virtual ~sg_viewer() {
110  if(m_win) {
111  m_session.delete_window(m_win);
112  }
113  }
114 protected:
115  sg_viewer(const sg_viewer& a_from)
116  :parent(a_from)
117  ,m_session(a_from.m_session)
118  ,m_mgr_gra(a_from.m_mgr_gra)
119  ,m_win(a_from.m_win)
120  {}
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;
125  return *this;
126  }
127 public:
128  bool has_window() const {return m_win?true:false;} //for SWIG
129 
130  bool show() {
131  if(!m_win) return false;
132  m_session.show_window(m_win);
133  return true;
134  }
135 protected:
136  session& m_session;
137  metal::manager m_mgr_gra;
138  NSWindow* m_win;
139  tools_metal_view* m_view;
140 };
141 
142 }}
143 
144 
145 
146 #endif
147