FLTK logo

[master] c2efb0d - macOS: support of FLTK widgets in OpenGL 3 windows - cont'd. This code is OK under macOS 10 9 and 13.

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] c2efb0d - macOS: support of FLTK widgets in OpenGL 3 windows - cont'd. This code is OK under macOS 10 9 and 13. "ManoloFLTK" Sep 28, 2022  
 
commit c2efb0d8494b101d9700afc79302fcf371995a6e
Author:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
AuthorDate: Wed Sep 28 17:06:00 2022 +0200
Commit:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
CommitDate: Wed Sep 28 17:06:00 2022 +0200

    macOS: support of FLTK widgets in OpenGL 3 windows - cont'd.
    This code is OK under macOS 10 9 and 13.

 examples/OpenGL3test.cxx                        |  6 +++--
 src/Fl_cocoa.mm                                 | 16 +++++++-----
 src/drivers/Cocoa/Fl_Cocoa_Gl_Window_Driver.H   |  3 ++-
 src/drivers/Cocoa/Fl_Cocoa_Gl_Window_Driver.cxx | 34 ++++++++++++++++++++-----
 src/drivers/Cocoa/Fl_Cocoa_Window_Driver.H      |  5 +++-
 5 files changed, 48 insertions(+), 16 deletions(-)

diff --git examples/OpenGL3test.cxx examples/OpenGL3test.cxx
index edcb56d..40d0bf7 100644
--- examples/OpenGL3test.cxx
+++ examples/OpenGL3test.cxx
@@ -191,8 +191,10 @@ void toggle_double(Fl_Widget *wid, void *data) {
   SimpleGL3Window *glwin = (SimpleGL3Window*)data;
   int flags = glwin->mode();
   if (doublebuff) flags |= FL_DOUBLE; else flags &= ~FL_DOUBLE;
-  glwin->mode(flags);
   glwin->reset();
+  glwin->hide();
+  glwin->mode(flags);
+  glwin->show();
 }
 
 
@@ -227,7 +229,7 @@ void button_cb(Fl_Widget *, void *) {
 }
 
 void add_widgets(Fl_Gl_Window *g) {
-  Fl::set_color(FL_FREE_COLOR, 255, 0, 0, 140); // partially transparent red
+  Fl::set_color(FL_FREE_COLOR, 255, 255, 255, 140); // partially transparent white
   g->begin();
   // Create here widgets to go above the GL3 scene
   Fl_Button* b = new Fl_Button( 0, 170, 60, 30, "button");
diff --git src/Fl_cocoa.mm src/Fl_cocoa.mm
index 9cb302d..ccb4ebe 100644
--- src/Fl_cocoa.mm
+++ src/Fl_cocoa.mm
@@ -2941,25 +2941,29 @@ NSOpenGLContext* Fl_Cocoa_Window_Driver::create_GLcontext_for_window(NSOpenGLPix
 }
 
 
-NSOpenGLContext *Fl_Cocoa_Window_Driver::gl1ctxt_create() {
+NSOpenGLContext *Fl_Cocoa_Window_Driver::gl1ctxt_create(NSView **gl1view) {
   FLView *view = (FLView*)[fl_xid(pWindow) contentView];
-  NSView *gl1view = [[NSView alloc] initWithFrame:[view frame]];
-  [view addSubview:gl1view];
-  [gl1view release];
+  *gl1view = [[NSView alloc] initWithFrame:[view frame]];
   NSOpenGLPixelFormat *gl1pixelformat =
       Fl_Cocoa_Window_Driver::mode_to_NSOpenGLPixelFormat(
                               FL_RGB8 | FL_ALPHA | FL_SINGLE, NULL);
   NSOpenGLContext *gl1ctxt = [[NSOpenGLContext alloc]
                               initWithFormat:gl1pixelformat shareContext:nil];
   [gl1pixelformat release];
-  remove_gl_context_opacity(gl1ctxt);
+  return gl1ctxt;
+}
+
+void Fl_Cocoa_Window_Driver::gl1ctxt_add(NSOpenGLContext *gl1ctxt, NSView *gl1view) {
+  FLView *flview = (FLView*)[fl_xid(pWindow) contentView];
+  [flview addSubview:gl1view];
+  [gl1view release];
 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_7
   if (fl_mac_os_version >= 100700 && Fl::use_high_res_GL()) {
     [gl1view setWantsBestResolutionOpenGLSurface:YES];
   }
 #endif
   [gl1ctxt setView:gl1view];
-  return gl1ctxt;
+  remove_gl_context_opacity(gl1ctxt);
 }
 
 
diff --git src/drivers/Cocoa/Fl_Cocoa_Gl_Window_Driver.H src/drivers/Cocoa/Fl_Cocoa_Gl_Window_Driver.H
index e1a8dab..f0456ee 100644
--- src/drivers/Cocoa/Fl_Cocoa_Gl_Window_Driver.H
+++ src/drivers/Cocoa/Fl_Cocoa_Gl_Window_Driver.H
@@ -28,9 +28,9 @@ class Fl_Gl_Choice;
 
 class Fl_Cocoa_Gl_Window_Driver : public Fl_Gl_Window_Driver {
   NSOpenGLContext *gl1ctxt; // GL1 context in addition to GL3 context
+  static void delayed_addgl1ctxt(void *data);
   friend Fl_Gl_Window_Driver* Fl_Gl_Window_Driver::newGlWindowDriver(Fl_Gl_Window *);
   Fl_Cocoa_Gl_Window_Driver(Fl_Gl_Window *win);
-  ~Fl_Cocoa_Gl_Window_Driver();
   virtual float pixels_per_unit();
   virtual void before_show(int& need_after);
   virtual void after_show();
@@ -53,6 +53,7 @@ class Fl_Cocoa_Gl_Window_Driver : public Fl_Gl_Window_Driver {
   void apply_scissor();
   virtual void switch_to_GL1();
   virtual void switch_back();
+  virtual void gl_hide_before(void *&);
 };
 
 
diff --git src/drivers/Cocoa/Fl_Cocoa_Gl_Window_Driver.cxx src/drivers/Cocoa/Fl_Cocoa_Gl_Window_Driver.cxx
index 7a20a75..bb4c53d 100644
--- src/drivers/Cocoa/Fl_Cocoa_Gl_Window_Driver.cxx
+++ src/drivers/Cocoa/Fl_Cocoa_Gl_Window_Driver.cxx
@@ -53,10 +53,6 @@ Fl_Cocoa_Gl_Window_Driver::Fl_Cocoa_Gl_Window_Driver(Fl_Gl_Window *win) :
   gl1ctxt = NULL;
 }
 
-Fl_Cocoa_Gl_Window_Driver::~Fl_Cocoa_Gl_Window_Driver() {
-  if (gl1ctxt) Fl_Cocoa_Window_Driver::GLcontext_release(gl1ctxt);
-}
-
 Fl_Gl_Choice *Fl_Cocoa_Gl_Window_Driver::find(int m, const int *alistp)
 {
   Fl::screen_driver()->open_display(); // useful when called through gl_start()
@@ -331,12 +327,31 @@ FL_EXPORT NSOpenGLContext *fl_mac_glcontext(GLContext rc) {
  view/GL context.
  */
 
+static struct win_view {
+  Fl_Gl_Window *win;
+  NSView *gl1view;
+  NSOpenGLContext *gl1ctxt;
+} win_view_struct;
+
+void Fl_Cocoa_Gl_Window_Driver::delayed_addgl1ctxt(void *d) {
+  struct win_view *data = (struct win_view *)d;
+  Fl_Cocoa_Window_Driver::driver(data->win)->gl1ctxt_add(data->gl1ctxt, data->gl1view);
+  data->win->redraw();
+}
+
 void Fl_Cocoa_Gl_Window_Driver::switch_to_GL1() {
   if (!gl1ctxt) {
-    gl1ctxt = Fl_Cocoa_Window_Driver::driver(pWindow)->gl1ctxt_create();
-    Fl::add_timeout(0.01, (Fl_Timeout_Handler)delayed_redraw, pWindow);
+    gl1ctxt = Fl_Cocoa_Window_Driver::driver(pWindow)->gl1ctxt_create(
+                &win_view_struct.gl1view);
+    win_view_struct.win = pWindow;
+    win_view_struct.gl1ctxt = gl1ctxt;
+    Fl::add_timeout(0.01,
+            Fl_Cocoa_Gl_Window_Driver::delayed_addgl1ctxt,
+            &win_view_struct);
   }
   Fl_Cocoa_Window_Driver::GLcontext_makecurrent(gl1ctxt);
+  glClearColor(0., 0., 0., 0.);
+  glClear(GL_COLOR_BUFFER_BIT);
 }
 
 void Fl_Cocoa_Gl_Window_Driver::switch_back() {
@@ -344,6 +359,13 @@ void Fl_Cocoa_Gl_Window_Driver::switch_back() {
   Fl_Cocoa_Window_Driver::GLcontext_makecurrent((NSOpenGLContext*)pWindow->context());
 }
 
+void Fl_Cocoa_Gl_Window_Driver::gl_hide_before(void *&) {
+  if (gl1ctxt) {
+  	Fl_Cocoa_Window_Driver::GLcontext_release(gl1ctxt);
+    gl1ctxt = 0;
+  }
+}
+
 
 class Fl_Gl_Cocoa_Plugin : public Fl_Cocoa_Plugin {
 public:
diff --git src/drivers/Cocoa/Fl_Cocoa_Window_Driver.H src/drivers/Cocoa/Fl_Cocoa_Window_Driver.H
index d6f7534..16cea4e 100644
--- src/drivers/Cocoa/Fl_Cocoa_Window_Driver.H
+++ src/drivers/Cocoa/Fl_Cocoa_Window_Driver.H
@@ -36,6 +36,7 @@ class Fl_Window;
 @class FLWindow;
 @class NSOpenGLContext;
 @class NSOpenGLPixelFormat;
+@class NSView;
 #else
 class CALayer;
 class NSCursor;
@@ -43,6 +44,7 @@ class NSImage;
 class FLWindow;
 class NSOpenGLContext;
 class NSOpenGLPixelFormat;
+class NSView;
 #endif // __OBJC__
 
 /**
@@ -155,7 +157,8 @@ public:
   static void GL_cleardrawable(void); // uses Objective-c
   static void gl_start(NSOpenGLContext*); // uses Objective-c
   static void remove_gl_context_opacity(NSOpenGLContext*); // uses Objective-c
-  NSOpenGLContext *gl1ctxt_create(); // uses Objective-c
+  NSOpenGLContext *gl1ctxt_create(NSView **); // uses Objective-c
+  void gl1ctxt_add(NSOpenGLContext*, NSView*); // uses Objective-c
   static void gl1ctxt_resize(NSOpenGLContext*); // uses Objective-c
 
   //icons
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.