GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynamic)

Share your advanced PureBasic knowledge/code with the community.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: GLFW 304 wrapper 1.01 + Binaries (broken on OSX)

Post by luis »

Wow, thank you to you Danilo, I really appreciate your help on this, I was becoming crazy.

To be honest, while looking at the Objective-C code I had a hunch could have been something fishy with NSApp, but I missed the further step you took, primarly because I didnt' know how to debug the thing (the PB program mixed with the library).
No excuse anyway !

Can you tell me how did you thought the problem was localized there ? Did you tried to comment it out without knowing it in advance for sure (a hunch) or did you test the NSApp value in some way ?

Probably the same it's happening inside the SDL code.

Now I'll try what you described here both for glfw and sdl2.1 and report back.

Again, thank you for your help, I don't know when (if ever) I would have sorted this out by myself. :mrgreen:

Running to try all this ...


EDIT: tried on glfw, all ok!
I've updated the first post with the new libraries and changed a little the test program to accommodate the window resizing options now available on OSX, and some other little things useful for testing.

EDIT 2: and I confirm even sdl2 works now, it was practically the same problem !
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: GLFW 304 wrapper 1.01 + Binaries (broken on OSX)

Post by Danilo »

luis wrote:Can you tell me how did you thought the problem was localized there ?
I just read some of the program flow (init function, window creation -> I just followed your example source).
Added createMenuBar() at the end of the "create window" stuff as a first guess, but didn't work.
So I wanted to start debugging at initializeAppKit(). Commented out the first 2 lines, because
it was clear that the following code is not executed if 'NSApp' is true. This already worked, so
I guess I were just lucky.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by luis »

I took a hit to the head and so I decided to make available the static libraries too.
Now you can decide if use static or dynamic linking !

Updated first post.

Now it's time for me to put this stuff to good use :)
"Have you tried turning it off and on again ?"
A little PureBasic review
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Might be a bug (not sure about it)

Post by Poshu »

So I've been testing with GLFW and I'm somehow stuck with the joysticks:
glfwJoystickPresent(0) always return true if the joystick was connected on startup and false if it wasn't. There is pretty much no update when I plug/unplug my pad.
Using glfwGetJoystickButtons(0,0) instantly make the application crash with an IMA.
Is there a command I need to add on each loop? (like the update function from SFML)

Tested on OSX maverick x64

Edit: Just tried on Windows: glfwJoystickPresent(0) works perfectly fine with the same code, still IMA on glfwGetJoystickButtons(0,0
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by luis »

Don't know if I can test it under osx, no idea if a pc usb gamepad works there.
But in any case, can you post a sample code ?
Did you try the same scenario using the PB joystick lib ?
Did you use the same pad on both oses ?
Does have osx a control-panel thingy to try the joystick like the one available in windows ?
"Have you tried turning it off and on again ?"
A little PureBasic review
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by Poshu »

luis wrote:Don't know if I can test it under osx, no idea if a pc usb gamepad works there.
I'm using a dualshock 4, osx recognizes it natively, so I guess any standard (so no x360 controller) should work as well.
luis wrote:But in any case, can you post a sample code ?
Update problem (osx only)

Code: Select all

; *****************************************************************
; Complete wrapper for glfw 3.0.4 
; for Win/Lin/OSX, 32/64 bits, Dynamic/Static
;
; Jan 2014 by Luis, release 1.03
; http://luis.no-ip.net
;
; Many thanks to Danilo for solving the "Application Menu" problem 
; under OSX
; *****************************************************************


EnableExplicit

; CONFIGURE THE WRAPPER

#GLFW_LINKING_STATIC    = 0 ; (0 = dynamic linking, 1 = static linking)
#GLFW_WRAPPER_PATH$     = "." 
#GLFW_LIBS_PATH$        = "../glfw-libs"

; INCLUDE THE WRAPPER

IncludeFile #GLFW_WRAPPER_PATH$ + "/" + "glfw3.declares.pbi"
IncludeFile #GLFW_WRAPPER_PATH$ + "/" + "glfw3.imports.pbi"

; IF LINKING IS DYNAMIC, THEN DO THE LATE BINDING

CompilerIf #GLFW_LINKING_STATIC = 0
 If glfwBindLibrary(#GLFW_LIBS_PATH$) = 0
    MessageRequester("glfw", "Cannot open the dynamic library, aborting.")
    End
 EndIf 
CompilerEndIf


; Stuff needed for this demo

#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_MODELVIEW = $1700
#GL_PROJECTION = $1701
#GL_TRIANGLES = 4

CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)

CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
Import "Opengl32.lib" 
 glClear_(a.i) As "_glClear@4"
 glViewport_(a.i,b.i,c.i,d.i) As "_glViewport@16"
 glMatrixMode_(a.i) As "_glMatrixMode@4"
 glLoadIdentity_() As "_glLoadIdentity@0"
 glTranslatef_(a.f,b.f,c.f) As "_glTranslatef@12"
 glBegin_(a.i) As "_glBegin@4"
 glColor3f_(a.f,b.f,c.f) As "_glColor3f@12"
 glVertex3f_(a.f,b.f,c.f) As "_glVertex3f@12"
 glEnd_() As "_glEnd@0"
EndImport

Import "Glu32.lib"
 gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective@32"
EndImport

CompilerElse   
 
Import "Opengl32.lib" 
 glClear_(a.i) As "glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
 glMatrixMode_(a.i) As "glMatrixMode"
 glLoadIdentity_() As "glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
 glBegin_(a.i) As "glBegin"
 glColor3f_(a.f,b.f,c.f) As "glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
 glEnd_() As "glEnd"
EndImport

Import "Glu32.lib"
 gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport

CompilerEndIf

CompilerEndIf

CompilerIf (#PB_Compiler_OS = #PB_OS_Linux)

ImportC "-lGL" 
 glClear_(a.i) As "glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
 glMatrixMode_(a.i) As "glMatrixMode"
 glLoadIdentity_() As "glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
 glBegin_(a.i) As "glBegin"
 glColor3f_(a.f,b.f,c.f) As "glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
 glEnd_() As "glEnd"
EndImport

ImportC "-lGLU"
 gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport

CompilerEndIf
    
CompilerIf (#PB_Compiler_OS = #PB_OS_MacOS)

ImportC "/System/Library/Frameworks/OpenGL.framework/OpenGL"
 glClear_(a.i) As "_glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "_glViewport"
 glMatrixMode_(a.i) As "_glMatrixMode"
 glLoadIdentity_() As "_glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "_glTranslatef"
 glBegin_(a.i) As "_glBegin"
 glColor3f_(a.f,b.f,c.f) As "_glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "_glVertex3f"
 glEnd_() As "_glEnd"
 gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective"
EndImport

CompilerEndIf

    
Procedure Render(window)
 Protected w, h
 
 glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
 
 glfwGetFramebufferSize(window, @w, @h)
 glViewport_(0, 0, w, h)
 glMatrixMode_(#GL_PROJECTION)
 glLoadIdentity_()
 gluPerspective_(30.0, Abs(w/h), 0.1, 100.0)
 glMatrixMode_(#GL_MODELVIEW)
 glLoadIdentity_()

 glTranslatef_(0.0, 0.0, -8.0)
 
 glBegin_(#GL_TRIANGLES)      
  glColor3f_ ( 1.0,  0.0, 0.0) 
  glVertex3f_( 0.0,  1.0, 0.0) 
  glColor3f_ ( 0.0,  1.0, 0.0) 
  glVertex3f_(-1.0, -1.0, 0.0) 
  glColor3f_ ( 0.0,  0.0, 1.0) 
  glVertex3f_( 1.0, -1.0, 0.0) 
 glEnd_() 
EndProcedure


ProcedureC key_callback(window, key, scancode, action, mods)
 Debug "key = " + key + ", scancode = " + scancode + ", action = " + action + ", mode = " + mods
 If (key = #GLFW_KEY_ESCAPE And action = #GLFW_PRESS)
    glfwSetWindowShouldClose(window, 1)
 EndIf
EndProcedure

ProcedureC error_callback (err, *desc)
 Debug "Error: " + err + ", " + PeekS(*desc, -1, #PB_UTF8)
EndProcedure


Procedure Main()
 Protected window
     
 Debug PeekS(glfwGetVersionString(), -1, #PB_UTF8)

 glfwSetErrorCallback(@error_callback())    
   
 If glfwInit()       
    window = glfwCreateWindow(640, 480, "Hello World", #Null, #Null)
    
    If window   
        glfwMakeContextCurrent(window)
        glfwSetKeyCallback(window, @key_callback())
        Debug glfwJoystickPresent(0)
        While (glfwWindowShouldClose(window) = 0)
            Render(window)
            glfwSwapBuffers(window)
            glfwPollEvents()
            Debug glfwJoystickPresent(0) ; <- this part is not updated when I plug/unplug the controller
        Wend            
    EndIf
         
    glfwTerminate()
 EndIf
EndProcedure

Main()
Button problem (both osx and windows... might be my fault :p)

Code: Select all

; *****************************************************************
; Complete wrapper for glfw 3.0.4 
; for Win/Lin/OSX, 32/64 bits, Dynamic/Static
;
; Jan 2014 by Luis, release 1.03
; http://luis.no-ip.net
;
; Many thanks to Danilo for solving the "Application Menu" problem 
; under OSX
; *****************************************************************


EnableExplicit

; CONFIGURE THE WRAPPER

#GLFW_LINKING_STATIC    = 0 ; (0 = dynamic linking, 1 = static linking)
#GLFW_WRAPPER_PATH$     = "." 
#GLFW_LIBS_PATH$        = "../glfw-libs"

; INCLUDE THE WRAPPER

IncludeFile #GLFW_WRAPPER_PATH$ + "/" + "glfw3.declares.pbi"
IncludeFile #GLFW_WRAPPER_PATH$ + "/" + "glfw3.imports.pbi"

; IF LINKING IS DYNAMIC, THEN DO THE LATE BINDING

CompilerIf #GLFW_LINKING_STATIC = 0
 If glfwBindLibrary(#GLFW_LIBS_PATH$) = 0
    MessageRequester("glfw", "Cannot open the dynamic library, aborting.")
    End
 EndIf 
CompilerEndIf


; Stuff needed for this demo

#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_MODELVIEW = $1700
#GL_PROJECTION = $1701
#GL_TRIANGLES = 4

CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)

CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
Import "Opengl32.lib" 
 glClear_(a.i) As "_glClear@4"
 glViewport_(a.i,b.i,c.i,d.i) As "_glViewport@16"
 glMatrixMode_(a.i) As "_glMatrixMode@4"
 glLoadIdentity_() As "_glLoadIdentity@0"
 glTranslatef_(a.f,b.f,c.f) As "_glTranslatef@12"
 glBegin_(a.i) As "_glBegin@4"
 glColor3f_(a.f,b.f,c.f) As "_glColor3f@12"
 glVertex3f_(a.f,b.f,c.f) As "_glVertex3f@12"
 glEnd_() As "_glEnd@0"
EndImport

Import "Glu32.lib"
 gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective@32"
EndImport

CompilerElse   
 
Import "Opengl32.lib" 
 glClear_(a.i) As "glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
 glMatrixMode_(a.i) As "glMatrixMode"
 glLoadIdentity_() As "glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
 glBegin_(a.i) As "glBegin"
 glColor3f_(a.f,b.f,c.f) As "glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
 glEnd_() As "glEnd"
EndImport

Import "Glu32.lib"
 gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport

CompilerEndIf

CompilerEndIf

CompilerIf (#PB_Compiler_OS = #PB_OS_Linux)

ImportC "-lGL" 
 glClear_(a.i) As "glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
 glMatrixMode_(a.i) As "glMatrixMode"
 glLoadIdentity_() As "glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
 glBegin_(a.i) As "glBegin"
 glColor3f_(a.f,b.f,c.f) As "glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
 glEnd_() As "glEnd"
EndImport

ImportC "-lGLU"
 gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport

CompilerEndIf
    
CompilerIf (#PB_Compiler_OS = #PB_OS_MacOS)

ImportC "/System/Library/Frameworks/OpenGL.framework/OpenGL"
 glClear_(a.i) As "_glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "_glViewport"
 glMatrixMode_(a.i) As "_glMatrixMode"
 glLoadIdentity_() As "_glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "_glTranslatef"
 glBegin_(a.i) As "_glBegin"
 glColor3f_(a.f,b.f,c.f) As "_glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "_glVertex3f"
 glEnd_() As "_glEnd"
 gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective"
EndImport

CompilerEndIf

    
Procedure Render(window)
 Protected w, h
 
 glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
 
 glfwGetFramebufferSize(window, @w, @h)
 glViewport_(0, 0, w, h)
 glMatrixMode_(#GL_PROJECTION)
 glLoadIdentity_()
 gluPerspective_(30.0, Abs(w/h), 0.1, 100.0)
 glMatrixMode_(#GL_MODELVIEW)
 glLoadIdentity_()

 glTranslatef_(0.0, 0.0, -8.0)
 
 glBegin_(#GL_TRIANGLES)      
  glColor3f_ ( 1.0,  0.0, 0.0) 
  glVertex3f_( 0.0,  1.0, 0.0) 
  glColor3f_ ( 0.0,  1.0, 0.0) 
  glVertex3f_(-1.0, -1.0, 0.0) 
  glColor3f_ ( 0.0,  0.0, 1.0) 
  glVertex3f_( 1.0, -1.0, 0.0) 
 glEnd_() 
EndProcedure


ProcedureC key_callback(window, key, scancode, action, mods)
 Debug "key = " + key + ", scancode = " + scancode + ", action = " + action + ", mode = " + mods
 If (key = #GLFW_KEY_ESCAPE And action = #GLFW_PRESS)
    glfwSetWindowShouldClose(window, 1)
 EndIf
EndProcedure

ProcedureC error_callback (err, *desc)
 Debug "Error: " + err + ", " + PeekS(*desc, -1, #PB_UTF8)
EndProcedure


Procedure Main()
 Protected window
     
 Debug PeekS(glfwGetVersionString(), -1, #PB_UTF8)

 glfwSetErrorCallback(@error_callback())    
   
 If glfwInit()       
    window = glfwCreateWindow(640, 480, "Hello World", #Null, #Null)
    
    If window   
        glfwMakeContextCurrent(window)
        glfwSetKeyCallback(window, @key_callback())
        Debug glfwJoystickPresent(0)
        While (glfwWindowShouldClose(window) = 0)
            Render(window)
            glfwSwapBuffers(window)
            glfwPollEvents()
            If glfwJoystickPresent(0)
            	Debug glfwGetJoystickButtons(0,1) ;<- IMA
            EndIf
        Wend            
    EndIf
         
    glfwTerminate()
 EndIf
EndProcedure

Main()
luis wrote:Did you try the same scenario using the PB joystick lib ?
Yup, on windows only but no ima to be seen.
luis wrote:Did you use the same pad on both oses ?
Yup, dualshock 4, on the same hardware (late 2013 macbook pro retina)
luis wrote:Does have osx a control-panel thingy to try the joystick like the one available in windows ?
I'm kinda new to osx, but I couldn't find one (though I now for a fact that the controller works: I'm playing games with it)
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by applePi »

once luis have issued the glfw wrapper i have found that it is very useful for who wants to learn Opengl, since we don't focus on the internals which are very complex but on the exercises. but i have always ambiguity regarding refreshing the screen. it is easy to refresh the screen if we draw a rotating rectangle by only calling the drawing code again and again and the computer will not freeze, but what if the drawing routine are time consuming such as a fractal, it will not work and will freeze the program. unless we store whats drawn before in a memory
i have tried the glGenLists then calling the generated list with glCallList, it display the stored fractal , and refreshed when we resize the window or cover it, but still have some slow refreshing performance on my machine.
the following code rotating the triangle by executing the same draw code. there is no difficulty in refreshing the screen here.
and after that the fractal draw, i have added run = 1 in the Render procedure so the second run in the main procedure will not execute the lengthy plotting code but the generated list in the memory glCallList_(glist)
i have included OpenGL.pbi because it contains the definitions for #GL_COMPILE_AND_EXECUTE) , #GL_COMPILE , #GL_POINTS
i will try to learn the introductory OpenGL again from time to time.
rotating triangle:

Code: Select all

; *****************************************************************
; Complete wrapper for glfw 3.0.4 
; for Win/Lin/OSX, 32/64 bits, Dynamic/Static
;
; Jan 2014 by Luis, release 1.03
; http://luis.no-ip.net
;
; Many thanks to Danilo for solving the "Application Menu" problem 
; under OSX
; *****************************************************************

EnableExplicit

#GLFW_LINKING_STATIC    = 0 ; (0 = dynamic linking, 1 = static linking)
#GLFW_WRAPPER_PATH$     = "." 
#GLFW_LIBS_PATH$        = "../glfw-libs"

#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_MODELVIEW = $1700
#GL_PROJECTION = $1701
#GL_TRIANGLES = 4

IncludeFile "glfw3.declares.pbi"
IncludeFile "glfw3.imports.pbi"
Global rot.f

CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)

CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
Import "Opengl32.lib" 
 glClear_(a.i) As "_glClear@4"
 glViewport_(a.i,b.i,c.i,d.i) As "_glViewport@16"
 glMatrixMode_(a.i) As "_glMatrixMode@4"
 glLoadIdentity_() As "_glLoadIdentity@0"
 glTranslatef_(a.f,b.f,c.f) As "_glTranslatef@12"
 glBegin_(a.i) As "_glBegin@4"
 glColor3f_(a.f,b.f,c.f) As "_glColor3f@12"
 glVertex3f_(a.f,b.f,c.f) As "_glVertex3f@12"
 glEnd_() As "_glEnd@0"
EndImport

Import "Glu32.lib"
 gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective@32"
EndImport

CompilerElse   
 
Import "Opengl32.lib" 
 glClear_(a.i) As "glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
 glMatrixMode_(a.i) As "glMatrixMode"
 glLoadIdentity_() As "glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
 glBegin_(a.i) As "glBegin"
 glColor3f_(a.f,b.f,c.f) As "glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
 glEnd_() As "glEnd"
EndImport

Import "Glu32.lib"
 gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport
CompilerEndIf

CompilerEndIf

CompilerIf (#PB_Compiler_OS = #PB_OS_Linux)
ImportC "-lGL" 
 glClear_(a.i) As "glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
 glMatrixMode_(a.i) As "glMatrixMode"
 glLoadIdentity_() As "glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
 glBegin_(a.i) As "glBegin"
 glColor3f_(a.f,b.f,c.f) As "glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
 glEnd_() As "glEnd"
EndImport

ImportC "-lGLU"
 gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport
CompilerEndIf
    
CompilerIf (#PB_Compiler_OS = #PB_OS_MacOS)
ImportC "/System/Library/Frameworks/OpenGL.framework/OpenGL"
 glClear_(a.i) As "_glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "_glViewport"
 glMatrixMode_(a.i) As "_glMatrixMode"
 glLoadIdentity_() As "_glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "_glTranslatef"
 glBegin_(a.i) As "_glBegin"
 glColor3f_(a.f,b.f,c.f) As "_glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "_glVertex3f"
 glEnd_() As "_glEnd"
 gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective"
EndImport
CompilerEndIf
    
Procedure Render()
  Protected.f width = 450, height = 450, ratio = width / height
  Protected.f rot
  rot + 0.5

        glViewport_(0, 0, width, height);
        glClear_(#GL_COLOR_BUFFER_BIT);
  glMatrixMode_(#GL_PROJECTION);
        ;glLoadIdentity_();
        
        glOrtho_(-ratio, ratio, -1, 1, 1, -1);
        glMatrixMode_(#GL_MODELVIEW);

        glLoadIdentity_();
        glRotatef_(glfwGetTime() * 50, 0, 0, 1);
        ;glRotatef_(rot, 0, 0, 1);
        glBegin_(#GL_TRIANGLES);
        glColor3f_(1, 0, 0);
        glVertex3f_(-0.6, -0.4, 0);
        glColor3f_(0, 1, 0);
        glVertex3f_(0.6, -0.4, 0);
        glColor3f_(0, 0, 1);
        glVertex3f_(0, 0.6, 0);
        glEnd_();

        ;glfwSwapBuffers(window);
        ;glfwPollEvents();
  ;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww 
 ;glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)

 ;glViewport_(0, 0, 640, 480)
 ;glMatrixMode_(#GL_PROJECTION)
 ;glLoadIdentity_()
 ;gluPerspective_(30.0, Abs(640.0/480.0), 0.1, 100.0)
 ;glMatrixMode_(#GL_MODELVIEW)
 ;glLoadIdentity_()

 ;glTranslatef_(0.0, 0.0, -8.0)
 
 ;glBegin_(#GL_TRIANGLES)      
  ;glColor3f_ ( 1.0,  0.0, 0.0) 
  ;glVertex3f_( 0.0,  1.0, 0.0) 
  ;glColor3f_ ( 0.0,  1.0, 0.0) 
  ;glVertex3f_(-1.0, -1.0, 0.0) 
  ;glColor3f_ ( 0.0,  0.0, 1.0) 
  ;glVertex3f_( 1.0, -1.0, 0.0) 
 ;glEnd_() 
EndProcedure

Procedure main()
 Protected window
  
 If glfwBindLibrary ("../glfw-libs") 
 
    ;Debug PeekS(glfwGetVersionString(), -1, #PB_UTF8)
     
    If glfwInit()    
        window = glfwCreateWindow(450, 450, "Hello World", #Null, #Null)
                
        If window   
            glfwMakeContextCurrent(window)
            
            While (glfwWindowShouldClose(window) = 0)
                Render()
                glfwSwapBuffers(window)
                glfwPollEvents()
            Wend
        
        EndIf
        
        glfwTerminate()
    EndIf
 Else
    MessageRequester("glfw", "Cannot open the dynamic library.")
 EndIf
EndProcedure

Main()
some Fractal:

Code: Select all

; *****************************************************************
; Complete wrapper for glfw 3.0.4 
; for Win/Lin/OSX, 32/64 bits, Dynamic/Static
;
; Jan 2014 by Luis, release 1.03
; http://luis.no-ip.net
;
; Many thanks to Danilo for solving the "Application Menu" problem 
; under OSX
; *****************************************************************


EnableExplicit
Global run.i = 0
Global axrng.f = 10.0
Global x.f=0: Global y.f=0: Global r.f=0: Global count.f=0
Global glist.l

; CONFIGURE THE WRAPPER

#GLFW_LINKING_STATIC    = 0 ; (0 = dynamic linking, 1 = static linking)
#GLFW_WRAPPER_PATH$     = "." 
#GLFW_LIBS_PATH$        = "../glfw-libs"

#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_MODELVIEW = $1700
#GL_PROJECTION = $1701
#GL_TRIANGLES = 4

;#GL_COMPILE   ; can be found in OpenGL.pbi                     = $1300
;#GL_COMPILE_AND_EXECUTE            = $1301
;#GL_POINTS                         = $0000

; INCLUDE THE WRAPPER

IncludeFile #GLFW_WRAPPER_PATH$ + "/" + "glfw3.declares.pbi"
IncludeFile #GLFW_WRAPPER_PATH$ + "/" + "glfw3.imports.pbi"
IncludeFile #PB_Compiler_Home + "Examples\Sources - Advanced\OpenGL Cube\OpenGL.pbi"


; IF LINKING IS DYNAMIC, THEN DO THE LATE BINDING

CompilerIf #GLFW_LINKING_STATIC = 0
 If glfwBindLibrary(#GLFW_LIBS_PATH$) = 0
    MessageRequester("glfw", "Cannot open the dynamic library, aborting.")
    End
 EndIf 
CompilerEndIf


; Stuff needed for this demo

#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_MODELVIEW = $1700
#GL_PROJECTION = $1701
#GL_TRIANGLES = 4

CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)

CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
Import "Opengl32.lib" 
 glClear_(a.i) As "_glClear@4"
 glViewport_(a.i,b.i,c.i,d.i) As "_glViewport@16"
 glMatrixMode_(a.i) As "_glMatrixMode@4"
 glLoadIdentity_() As "_glLoadIdentity@0"
 glTranslatef_(a.f,b.f,c.f) As "_glTranslatef@12"
 glBegin_(a.i) As "_glBegin@4"
 glColor3f_(a.f,b.f,c.f) As "_glColor3f@12"
 glVertex3f_(a.f,b.f,c.f) As "_glVertex3f@12"
 glEnd_() As "_glEnd@0"
EndImport

Import "Glu32.lib"
 gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective@32"
EndImport

CompilerElse   
 
Import "Opengl32.lib" 
 glClear_(a.i) As "glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
 glMatrixMode_(a.i) As "glMatrixMode"
 glLoadIdentity_() As "glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
 glBegin_(a.i) As "glBegin"
 glColor3f_(a.f,b.f,c.f) As "glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
 glEnd_() As "glEnd"
EndImport

Import "Glu32.lib"
 gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport

CompilerEndIf

CompilerEndIf

CompilerIf (#PB_Compiler_OS = #PB_OS_Linux)

ImportC "-lGL" 
 glClear_(a.i) As "glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
 glMatrixMode_(a.i) As "glMatrixMode"
 glLoadIdentity_() As "glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
 glBegin_(a.i) As "glBegin"
 glColor3f_(a.f,b.f,c.f) As "glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
 glEnd_() As "glEnd"
EndImport

ImportC "-lGLU"
 gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport

CompilerEndIf
    
CompilerIf (#PB_Compiler_OS = #PB_OS_MacOS)

ImportC "/System/Library/Frameworks/OpenGL.framework/OpenGL"
 glClear_(a.i) As "_glClear"
 glViewport_(a.i,b.i,c.i,d.i) As "_glViewport"
 glMatrixMode_(a.i) As "_glMatrixMode"
 glLoadIdentity_() As "_glLoadIdentity"
 glTranslatef_(a.f,b.f,c.f) As "_glTranslatef"
 glBegin_(a.i) As "_glBegin"
 glColor3f_(a.f,b.f,c.f) As "_glColor3f"
 glVertex3f_(a.f,b.f,c.f) As "_glVertex3f"
 glEnd_() As "_glEnd"
 gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective"
EndImport

CompilerEndIf

    
Procedure Render()
  run=1  
 ;Static glist.l ; handle for the OpenGL list 
 glViewport_ (0, 0, 450, 450)
 ;gluPerspective_(30.0, Abs(640.0/480.0), 0.1, 100.0)
  
glMatrixMode_(#GL_PROJECTION)
gluOrtho2D_(-axrng, axrng, -axrng, axrng)
glMatrixMode_ (#GL_MODELVIEW)      
;glEnable_ (#GL_DEPTH_TEST)   ; Enabled, it slowdown a lot the rendering. It's to be sure than the

;glEnable_ (#GL_CULL_FACE)    ; This will enhance the rendering speed as all the back face will be
                     
 glLoadIdentity_()

 glPushMatrix_() 
      glClear_(#GL_COLOR_BUFFER_BIT);

       glClearColor_(1.0, 1.0, 1.0, 1.0);
        ;make the glist
        glist = glGenLists_(1);
        glNewList_(glist, #GL_COMPILE_AND_EXECUTE)
        ;glNewList_(glist, #GL_COMPILE)
        glLoadIdentity_()
        
        x=-axrng
        While x <= axrng
          x = x + 0.03
          y=-axrng
          While y <= axrng
            y = y + 0.03
            count = count + 1
            r = Cos(x) + Sin(y)
            glColor3f_(Cos(y*r), Cos(x*y*r), Sin(r*x))
            glPointSize_(6);
            glBegin_(#GL_POINTS)
            glVertex2f_(x, y) 
            glEnd_() ; finished 
             
           Wend
         Wend
         glEndList_()
         ;glutSwapBuffers
         glPopMatrix_()
         glFinish_()
         ;SwapBuffers_(hdc)
                 
EndProcedure
       



ProcedureC key_callback(window, key, scancode, action, mods)
 Debug "key = " + key + ", scancode = " + scancode + ", action = " + action + ", mode = " + mods
 If (key = #GLFW_KEY_ESCAPE And action = #GLFW_PRESS)
    glfwSetWindowShouldClose(window, 1)
 EndIf
EndProcedure

ProcedureC error_callback (err, *desc)
 Debug "Error: " + err + ", " + PeekS(*desc, -1, #PB_UTF8)
EndProcedure

Procedure main()
 Protected window
  
 If glfwBindLibrary ("../glfw-libs") 
 
    ;Debug PeekS(glfwGetVersionString(), -1, #PB_UTF8)
     
    If glfwInit()    
        window = glfwCreateWindow(450, 450, "Hello World", #Null, #Null)
                
        If window   
            glfwMakeContextCurrent(window)
            glfwSetKeyCallback(window, @key_callback())
            
            While (glfwWindowShouldClose(window) = 0)
                
                ;glfwSwapInterval( 1 );
                glfwSwapBuffers(window)
                glfwPollEvents()
                
                If run=0 ; compose the fractal the first time
                  Render()
                Else     ; calling the fractal list from memory
                  glPushMatrix_();
                  glCallList_(glist);
                  glPopMatrix_()
                EndIf 
                
                
            Wend
        
        EndIf
        
        glfwTerminate()
    EndIf
 Else
    ;MessageRequester("glfw", "Cannot open the dynamic library.")
 EndIf
EndProcedure


Main()
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by luis »

@ApplePi.

In short, what you are doing is correct in the sense you are storing a lot of vertices to be plotted using a display list, avoiding to calculate the lot every frame.
Display lists are only one of the methods to "quickening up" rendering in opengl (see also vertex buffers and framebuffer objects for example). Display lists are often ok and easy to use when dealing with static vertex data, like in your case. But they are usually used when you also need to render the static geometry from a different point of view, in a dynamic scene, applying view transformations to the static data. In your case you don't need to do so, your data is just flat 2D points, and it's ultimately just an image.
So a better approach would be to render the data to a texture, and then simply plot the texture on the screen on every frame. The speed gain would be enormous. Render to a texture could be accomplished in many ways (pbuffers, FBOs, etc) but also very simply by just rendering once in the backbuffer, reading the image data from the buffer (glReadPixels) , copying it to a texture (glCopyTexSubImage) and then plotting the texture every frame using a quad. Google around with the terms used here and you'll find plenty of material.

@poshu
I'll try it and report back my findings, but if the PB joystick lib works then that's more then enough for me. From glfw I only want window, opengl RC, mouse and keyboard handling. Anyway if there is a bug would be nice to report it to their forums. From the docs doesn't seem to be a way to be notified from insertion/removal of the joy, so I suppose you should check for its presence every time before using it ?
Anyway if it does returns always "true" then it's a bug or a limitation of the OS (hard to believe the latter , I imagine you tried to insert/remove the joystick while playing with some game and it worked as expected, right ?)


All this bring me on another point, I would like to hear it from all of you (or at least the ones interested in a opengl game lib for PB).

As I wrote some time ago, I started working on a little project of mine, called sgl (simple gl).
In few words, the aim is to write a lib to make very easy to setup opengl in PB, and build upon that making a nice (from my POV obviously) gaming library, initially 2d but maybe also 3d in the future. You'll have a better idea when I'll start to publish the initial list of commands available. The lib would be usable with practically 0 knowledge of opengl (using just its pb-like commands) or by using plain opengl functions only (taking advantage only of the helper features of the lib but not the higher lever commands) or mixing the two things.

NOW: I can do it under Windows (I have no problem with all the details needed to manage windowing, low level keyboard and mouse, context creation and binding, etc.).
Or I can do it in a cross platform way using the most light support library I can find. Here comes glfw, which does just what I need an no more (or very little more). SDL it is also very valid but it does a lot more. glfw also has very light dependencies with the OS, it's a very thin layer and that's very important for me.
The reason for using such a lib is I could do all by myself under windows, and from my experiments I could pull it off under linux, but I dont' really want to do all that under osx too, especially since it's harder to do all in plain PB since cocoa is object oriented and it would be better to write a support library anyway written in Objective-C, a language I'm not very fond of.

So, I have three options that I see:

1) Write all for just Windows. Total control on my part on everything. All written in PB. I can do practically anything I need under Win, meaning there are not really big technical barriers for me there.
The library will be written with cross-platform in mind anyway, it's already structured that way.
So it would not be a mess of PB code an API intermingled, and it would possible for me or something else to port the platform dependent portion of the code to another OS in time.
By the way, I tested this code under WINE and it does seem to run well.

2) Use the thinnest and easier to understand, build from source, an (in case) patch here and there support library to offer a cross-platform way to build windows, GL RC, and manage keyboard and mouse. glfw in my mind.
I lose some of the control but I gain support for all the platforms and all the PB community can use it.
Very negative point: I don't like very much to depend on someone else code, but probably it's more a psychological thing than anything. Seems people glue together libs coming for all sources and then put out a working product. So maybe I'm just paranoid.

3) Just do all this for myself, to learn, have fun with it and forget about releasing it and all the problematic above. After all I could easily end up to be the only one interested in using it, for various reasons (I'm the only one who likes the api, it does not do what you want, it's simply crap from your point of view, etc.). And if I release it, it will be what it has become and that's it !

Uhmm.. what should I do ? I'll give myself some more days to think about it, but I would happily hear some suggestions, what would you do ? :)
"Have you tried turning it off and on again ?"
A little PureBasic review
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by Poshu »

luis wrote:@poshu
I'll try it and report back my findings, but if the PB joystick lib works then that's more then enough for me. From glfw I only want window, opengl RC, mouse and keyboard handling. Anyway if there is a bug would be nice to report it to their forums. From the docs doesn't seem to be a way to be notified from insertion/removal of the joy, so I suppose you should check for its presence every time before using it ?
Anyway if it does returns always "true" then it's a bug or a limitation of the OS (hard to believe the latter , I imagine you tried to insert/remove the joystick while playing with some game and it worked as expected, right ?)


All this bring me on another point, I would like to hear it from all of you (or at least the ones interested in a opengl game lib for PB).
Yeaaah, but the bad part is, you can only use pb's joystick library with an opened pb's screen, plus those function are quite limited (ie: they can't use the analogs on some controller...).

And... I'd say #2.
Last edited by Poshu on Thu Jan 16, 2014 3:39 pm, edited 1 time in total.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by applePi »

thanks luis for providing me with hints and keywords to proceed .
secondly i prefer the number 2 option, a cross platform and a patched glfw.
but i suggest that to not do a prefix or suffix to any function such as abc_glplot3D(...) or glplot3D_abc(...) . it is making words longer and hard to fit in the brain nicely.
thanks for all your efforts.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by luis »

@poshu
Yeaaah, but the bad part is, you can only use pb's joystick library with an opened pb's screen
Are you sure ?
I've tried and it works without the need of opening a screen from my testing.

About the buttons problem: the problem was the second param is a pointer, you passed 1 (?) hence the IMA.

See the docs here -> http://www.glfw.org/docs/latest/group__input.html

I've put together a crude example and this seems to work correctly here.

Replace the "main" part of your second test with this:

Code: Select all

Procedure.s DumpJoyButtons (*jbuttons_data.Byte, jbuttons_data_size)
 Protected k, buttons$ = "|"
 For k = 1 To jbuttons_data_size 
    buttons$ + *jbuttons_data\b + "|"
    *jbuttons_data + 1
 Next
 ProcedureReturn buttons$
EndProcedure

Procedure.s DumpJoyAxes (*jaxes_data.Float, jaxes_data_size)
 Protected k, axes$ = "| "
 For k = 1 To jaxes_data_size 
    axes$ + RSet(Str(Int(127.5 * *jaxes_data\f + 127.5)), 3, "0") + " | "
    *jaxes_data + SizeOf(Float)
 Next
 ProcedureReturn axes$
EndProcedure


Procedure Main()
 Protected window, row$
 Protected jbuttons_data_size, *jbuttons_data
 Protected jaxes_data_size, *jaxes_data
     
 Debug PeekS(glfwGetVersionString(), -1, #PB_UTF8)

 glfwSetErrorCallback(@error_callback())   
   
 If glfwInit()       
    window = glfwCreateWindow(640, 480, "Hello World", #Null, #Null)
   
    If window   
        glfwMakeContextCurrent(window)
        glfwSetKeyCallback(window, @key_callback())
        Debug glfwJoystickPresent(0)
        While (glfwWindowShouldClose(window) = 0)
            Render(window)
            glfwSwapBuffers(window)
            glfwPollEvents()
            If glfwJoystickPresent(0)
               *jbuttons_data = glfwGetJoystickButtons(0, @jbuttons_data_size)
               *jaxes_data = glfwGetJoystickAxes(0, @jaxes_data_size)
               row$ = DumpJoyButtons (*jbuttons_data, jbuttons_data_size)
               row$ + " " + DumpJoyAxes (*jaxes_data, jaxes_data_size)
               Debug row$
            EndIf
        Wend           
    EndIf
         
    glfwTerminate()
 EndIf
EndProcedure

Main()
I've tested this on win, lin and osx and it works. On lin and osx the joypad need to be connected before launching the app, on win is detected when plugged/unplugged. Don't know yet if this is a limitation of the platforms or the lib. I've tried on mac an utility showing the joystick values like the code above, and that one too didn't worked after re-plugging the joypad.
"Have you tried turning it off and on again ?"
A little PureBasic review
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by Poshu »

luis wrote:Are you sure ?
I've tried and it works without the need of opening a screen from my testing.
Things must have changed then : there was an update to the joystick lib in 5.20 that I have still to try, nevermind.
luis wrote:About the buttons problem: the problem was the second param is a pointer, you passed 1 (?) hence the IMA.

See the docs here -> http://www.glfw.org/docs/latest/group__input.html
... RTFM... Yeah, I assumed it was like pb's function where you had to set the button you wanted to check as a parameter, sorry about that.
luis wrote:I've tested this on win, lin and osx and it works. On lin and osx the joypad need to be connected before launching the app, on win is detected when plugged/unplugged. Don't know yet if this is a limitation of the platforms or the lib. I've tried on mac an utility showing the joystick values like the code above, and that one too didn't worked after re-plugging the joypad.
Mmmmh, too bad, that is a major drawback for a drop in/drop out local multi-player game... I'll try with some other libraries to check whether it's lib or system related.
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by Poshu »

After a few search: https://github.com/glfw/glfw/issues/17 so, yeah, it's a known problem.
Yet the PB's joystick library is now quite OK (eventhough initsjoystick() is slow), so I guess it should be enough.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by luis »

Thanks, seems it is expected for 3.1, we'll see.
If I'll choose the crossplatform way, maybe I could wrap both under a common interface (if feasible) to give an option on which you would like to use.
"Have you tried turning it off and on again ?"
A little PureBasic review
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: GLFW 304 wrapper 1.03 (Win/Lin/OSX x86/x64 Static/Dynami

Post by Poshu »

What is really upsetting me is, of the 3 joystick lib I can use, none is complete:
_PB : you can't get which joystick got disconnected (have do guess or hope that they all got a different name)
_GLFW : you can't update the list of joysticks
_CSFML : you can't get the name of a joystick
Post Reply