OpenGLGadget context

Just starting out? Need help? Post your questions and find answers here.
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

OpenGLGadget context

Post by wombats »

Hi,

I'm not sure if I completely understand the OpenGLGadget, but is it possible to two or more of them to share the same context, so as to have access to the same data? Imagine a game where each OpenGLGadget is a level and each needs access to the sprite images, etc.

Thanks.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: OpenGLGadget context

Post by Samuel »

Multiple contexts usually isn't the most efficient way to go. I'd recommend creating one large context and then use glviewport_() to specify which regions to render to.
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: OpenGLGadget context

Post by wombats »

Thanks for the reply.

My application is an editor. The OpenGLGadget is used as the editing area - the CanvasGadget was too slow. It has a tabbed interface, so I was using one OpenGLGadget per document. I tried using just one and switching it between the tabs by replacing gadgets in a SplitterGadget with it, but that caused all kinds of weirdness - sometimes the gadget would be destroyed for no reason.

So what I want is for each OpenGLGadget to be able to show the same things without duplicating them in memory for each tab.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: OpenGLGadget context

Post by Samuel »

If you're putting the OpenGL gadget within the tabs of the panel gadget. Then I believe you have no choice but to have multiple contexts. There is such a thing as sharing data between contexts, but I'm not very familiar with it.

If the OpenGL gadget is the only thing within the panel gadget. Then you could try making a custom tab system within the OpenGLGadget itself. That way there would be no need for other OpenGL gadgets.
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: OpenGLGadget context

Post by wombats »

There are a couple of other types of tabs that use standard GUI controls, which makes it a bit tricky.

I have a feeling that shared contexts would need to be added in by the PB team since I think each OpenGLGadget creates its own. I'm not that familiar with OpenGL, though.
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: OpenGLGadget context

Post by J. Baker »

Create your own button/tabs and you can do whatever you want. I created my own buttons for my SpriteMonkey v2 app and they work great. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: OpenGLGadget context

Post by wombats »

J. Baker wrote:Create your own button/tabs and you can do whatever you want. I created my own buttons for my SpriteMonkey v2 app and they work great. ;)
I used the TabBarGadget by STARGÅTE, and it works alright except for a very annoying flash when the tab is changed to an OpenGL one. I can't work out how to stop it doing that.

Code: Select all

XIncludeFile("TabBarGadget.pbi")

Enumeration
  #Window
  #Menu
  #MenuAddStringTab
  #MenuAddOpenGLTab
  #TabGadget
  #OpenGLGadget
  #TabTypeString
  #TabTypeGL
  #OGLTimer
EndEnumeration

#GL_TEXTURE_RECTANGLE_NV = $84F5

Structure TabData
  type.i
  *container
  color.i
EndStructure

Global *OpenGLContainer

Procedure AddTab(type)
  Protected *tabData.TabData, text.s, position
  
  *tabData = AllocateStructure(TabData)
  *tabData\color = RGB(Random(255), Random(255), Random(255))
  
  If type = 1
    *tabData\type = #TabTypeString
    *tabData\container = ContainerGadget(#PB_Any, 10, GadgetY(#TabGadget) + GadgetHeight(#TabGadget), GadgetWidth(#TabGadget),
                                         WindowHeight(#Window) - (MenuHeight() + GadgetY(#TabGadget) + GadgetHeight(#TabGadget) + 10), #PB_Container_Single)
    Define gadget = StringGadget(#PB_Any, 0, 0, GadgetWidth(*tabData\container), GadgetHeight(*tabData\container), "String...")
    SetGadgetColor(gadget, #PB_Gadget_BackColor, *tabData\color)
    CloseGadgetList()
    text = "String Tab"
  ElseIf type = 2
    *tabData\type = #TabTypeGL
    *tabData\container = *OpenGLContainer
    text = "OpenGL Tab"
  EndIf
  
  position = AddTabBarGadgetItem(#TabGadget, #PB_Default, text, #Null, *tabData)
  SetTabBarGadgetState(#TabGadget, position)
  PostEvent(#PB_Event_Gadget, #Window, #TabGadget, #TabBarGadget_EventType_Change, position)
EndProcedure

Procedure RenderGL()
  Protected item, *tabData.TabData, r, g, b
  item = GetTabBarGadgetState(#TabGadget)
  If item > -1
    *tabData = GetTabBarGadgetItemData(#TabGadget, item)
    r = Red(*tabData\color) : g = Green(*tabData\color) : b = Blue(*tabData\color)
    If *tabData\type = #TabTypeGL
      SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_SetContext, #True)
      glClearColor_(r/255.0, g/255.0, b/255.0, 1.0)
      glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
      glDisable_(#GL_TEXTURE_RECTANGLE_NV)
      glBegin_(#GL_QUADS)
      Define x0, y0, x1, y1
      glColor4ub_(0, 0, 0, 1)
      x0 = 32
      y0 = 32
      x1 = 200
      y1 = 200
      glVertex2f_(x0, y0)
      glVertex2f_(x1, y0)
      glVertex2f_(x1, y1)
      glVertex2f_(x0, y1)
      glEnd_()
      SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
    EndIf
  EndIf
EndProcedure

If OpenWindow(#Window, 0, 0, 640, 480, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  If CreateMenu(#Menu, WindowID(#Window))
    MenuTitle("Tab")
    MenuItem(#MenuAddStringTab, "Add String Tab")
    MenuItem(#MenuAddOpenGLTab, "Add OpenGL Tab")
  EndIf
  
  TabBarGadget(#TabGadget, 10, 10, WindowWidth(#Window) - 20, 32, #TabBarGadget_CloseButton, #Window)
  
  *OpenGLContainer = ContainerGadget(#PB_Any, 10, GadgetY(#TabGadget) + GadgetHeight(#TabGadget), GadgetWidth(#TabGadget),
                                     WindowHeight(#Window) - (MenuHeight() + GadgetY(#TabGadget) + GadgetHeight(#TabGadget) + 10), #PB_Container_Single)
  OpenGLGadget(#OpenGLGadget, 0, 0, GadgetWidth(*OpenGLContainer), GadgetHeight(*OpenGLContainer))
  CloseGadgetList()
  
  HideGadget(*OpenGLContainer, 1)
  
  glViewport_(0, 0, GadgetWidth(#OpenGLGadget), GadgetHeight(#OpenGLGadget))
  glMatrixMode_(#GL_PROJECTION)
  glLoadIdentity_()
  glOrtho_(0, GadgetWidth(#OpenGLGadget), GadgetHeight(#OpenGLGadget), 0, -1, 1)
  glMatrixMode_(#GL_MODELVIEW)
  glLoadIdentity_()
  glTranslatef_(-0.375, -0.375, 0.0)
  
  AddWindowTimer(#Window, #OGLTimer, 16)
  
  Repeat
    
    Define Event
    
    Event = WaitWindowEvent()
    
    Select Event
        
      Case #PB_Event_Timer
        RenderGL()
        
      Case #PB_Event_Menu
        
        Select EventMenu()
          Case #MenuAddStringTab : AddTab(1)
          Case #MenuAddOpenGLTab : AddTab(2)
        EndSelect
        
      Case #PB_Event_Gadget
        
        Select EventType()
            
          Case #TabBarGadget_EventType_Change
            
            Define item, *tabData.TabData, i, *otherTabData.TabData
            item = EventData();GetTabBarGadgetItemPosition(#TabGadget, #TabBarGadgetItem_Event)
            If item > -1
              *tabData = GetTabBarGadgetItemData(#TabGadget, item)
              If *tabData
                HideGadget(*tabData\container, 0)
                For i = 0 To CountTabBarGadgetItems(#TabGadget) - 1
                  *otherTabData = GetTabBarGadgetItemData(#TabGadget, i)
                  If *otherTabData
                    If *otherTabData <> *tabData
                      If *otherTabData\type <> #TabTypeGL
                        HideGadget(*otherTabData\container, 1)
                      Else
                        If *tabData\type <> #TabTypeGL
                          HideGadget(*OpenGLContainer, 1)
                        EndIf
                      EndIf
                    EndIf
                  EndIf
                Next
              EndIf
            EndIf
            
        EndSelect
        
    EndSelect
    
  Until Event = #PB_Event_CloseWindow
  
EndIf
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: OpenGLGadget context

Post by wombats »

Sorry to bring up an old topic, but I'm still struggling to get rid of the flash that occurs when changing between a String tab and an OpenGL tab. Does anyone have any ideas?

The OpenGLGadget is giving me such a headache, unfortunately: http://www.purebasic.fr/english/viewtop ... 23&t=68980
Post Reply