SDL2 lib

Mac OSX specific forum
User avatar
Piero
Addict
Addict
Posts: 961
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

SDL2 lib

Post by Piero »

Sooo… I built libSDL2-2.0.0.dylib, universal, compatible with ARM Macs down to the very old ones…

Can some Guru here show me the best way to "include/call" it, after placing it INSIDE a PB app?

It should include

Code: Select all

ImportC "/inside/app/libSDL2-2.0.0.dylib"
I suppose… Edit: 😱

Thanks in advance, and please forgive me if I wrote some PB/C blasphemy :oops: :oops: :oops:

Image
Last edited by Piero on Wed Sep 10, 2025 6:41 pm, edited 1 time in total.
User avatar
Piero
Addict
Addict
Posts: 961
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: SDL2 lib

Post by Piero »

Code: Select all

Procedure.s UpperDir(path$, level = 1, setcurrdir = #False)
   Protected i, oldcurrdir$ = GetCurrentDirectory()
   path$ = GetPathPart(path$) ; in case of file
   For i = 1 To level : path$ + ".." + #PS$ : Next
   SetCurrentDirectory(path$)
   path$ = GetCurrentDirectory()
   If Not setcurrdir : SetCurrentDirectory(oldcurrdir$) : EndIf
   ProcedureReturn path$
EndProcedure

sdl2.s=UpperDir(ProgramFilename())+"Resources/Libraries/libSDL2-2.0.0.dylib"
OpenLibrary(0, sdl2) ; no error if it doesn't exist (so you can test the app later after adding it…)

Debug sdl2
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: SDL2 lib

Post by kenmo »

Does this mean you've got it working now, by OpenLibrary?

I've used SDL2 and SDL3 with PB across Windows - Mac - Linux now, and I absolutely found Mac to be the most confusing, complicated, difficult to get working correctly and portable. The way it handles dylibs and frameworks seems so much more obtuse than on Windows/Linux. (Perhaps if you're using Xcode or other Apple suites, it's simplified.)

Here are some forum threads you might find helpful, including me being very confused about PB + SDL2 + Mac over 10 years ago...
viewtopic.php?p=448264
viewtopic.php?p=435318
viewtopic.php?f=13&t=57548

See the install_name_tool -add_rpath and otool commandline magic...
User avatar
Piero
Addict
Addict
Posts: 961
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: SDL2 lib

Post by Piero »

MANY THANKS!

PS: I have a (simple) project in mind, just making some tests; I think I will wait to also test with inline C… :)
User avatar
Piero
Addict
Addict
Posts: 961
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: SDL2 lib

Post by Piero »

Old Forum example just needed a small fix:

Code: Select all

; SDL_Test.app
;
; Download: https://github.com/libsdl-org/SDL/releases/tag/release-2.30.11
;
; To make a standalone .app, copy SDL2.framework into your app.
; Put it into folder: your.app/Contents/Frameworks/
;
; Now run the following command on the command line (from the same directory where your .app is):
; install_name_tool -add_rpath @executable_path/../Frameworks ./SDL_Test.app/Contents/MacOS/SDL_Test
; This adds the .app internal 'Frameworks' directory to the search path for frameworks.
; YOU NEED TO DO IT EACH TIME YOU COMPILE "SDL_Test.app"!
;
; Now test if it is working:
; ./SDL_Test.app/Contents/MacOS/SDL_Test (or run it from Finder)

PrototypeC.i _SDL_Init(flags.l)
PrototypeC _SDL_Quit()
PrototypeC _SDL_PumpEvents()
PrototypeC.i _SDL_CreateWindowAndRenderer(width.i, height.i, window_flags.l, *pWindow, *pRenderer)
PrototypeC _SDL_DestroyRenderer(*renderer)
PrototypeC _SDL_DestroyWindow(*window)
PrototypeC.i _SDL_SetRenderDrawColor(*renderer, r.a, g.a, b.a, a.a)
PrototypeC.i _SDL_RenderClear(*renderer)
PrototypeC _SDL_RenderPresent(*renderer)

If OpenLibrary(0, "SDL2.framework/SDL2")
   Global SDL_Init._SDL_Init = GetFunction(0, "SDL_Init")
   Global SDL_Quit._SDL_Quit = GetFunction(0, "SDL_Quit")
   Global SDL_PumpEvents._SDL_PumpEvents = GetFunction(0, "SDL_PumpEvents")
   Global SDL_CreateWindowAndRenderer._SDL_CreateWindowAndRenderer = GetFunction(0, "SDL_CreateWindowAndRenderer")
   Global SDL_DestroyRenderer._SDL_DestroyRenderer = GetFunction(0, "SDL_DestroyRenderer")
   Global SDL_DestroyWindow._SDL_DestroyWindow = GetFunction(0, "SDL_DestroyWindow")
   Global SDL_SetRenderDrawColor._SDL_SetRenderDrawColor = GetFunction(0, "SDL_SetRenderDrawColor")
   Global SDL_RenderClear._SDL_RenderClear = GetFunction(0, "SDL_RenderClear")
   Global SDL_RenderPresent._SDL_RenderPresent = GetFunction(0, "SDL_RenderPresent")
   If (SDL_Init($20) = 0)
      If (SDL_CreateWindowAndRenderer(640, 480, 0, @*win, @*ren) = 0)
         SDL_SetRenderDrawColor(*ren, 0, 255, 0, 255)
         SDL_RenderClear(*ren)
         SDL_RenderPresent(*ren)
         For i=0 to 100
            SDL_PumpEvents() ; fix for Mac
         Next
         Delay(2000)
         SDL_DestroyRenderer(*ren)
         SDL_DestroyWindow(*win)
      EndIf
      SDL_Quit()
   EndIf
EndIf
User avatar
Piero
Addict
Addict
Posts: 961
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

SDL2 easy test

Post by Piero »

SDL2 Test "Easy" version :)
Image

Code: Select all

; SDL Test
;
; Download: https://github.com/libsdl-org/SDL/releases/tag/release-2.32.10
;
; To make a standalone .app, copy SDL2.framework into your app.
; Put it into folder: your.app/Contents/Frameworks/ (needed only once!)

PrototypeC.i _SDL_Init(flags.l)
PrototypeC _SDL_Quit()
PrototypeC _SDL_PumpEvents()
PrototypeC.i _SDL_CreateWindowAndRenderer(width.i, height.i, window_flags.l, *pWindow, *pRenderer)
PrototypeC _SDL_DestroyRenderer(*renderer)
PrototypeC _SDL_DestroyWindow(*window)
PrototypeC.i _SDL_SetRenderDrawColor(*renderer, r.a, g.a, b.a, a.a)
PrototypeC.i _SDL_RenderClear(*renderer)
PrototypeC _SDL_RenderPresent(*renderer)

Procedure.s UpperDir(path$, level = 1, setcurrdir = #False)
   Protected i, oldcurrdir$ = GetCurrentDirectory()
   path$ = GetPathPart(path$) ; in case of file
   For i = 1 To level : path$ + ".." + #PS$ : Next
   SetCurrentDirectory(path$)
   path$ = GetCurrentDirectory()
   If Not setcurrdir : SetCurrentDirectory(oldcurrdir$) : EndIf
   ProcedureReturn path$
EndProcedure

Macro dq : "
EndMacro

Macro Fun(f,i=0)
   Global f#._#f = GetFunction(i, dq#f#dq)
EndMacro

If OpenLibrary(0, UpperDir(ProgramFilename())+"Frameworks/SDL2.framework/SDL2")
   Fun(SDL_Init)
   Fun(SDL_Quit)
   Fun(SDL_PumpEvents)
   Fun(SDL_CreateWindowAndRenderer)
   Fun(SDL_DestroyRenderer)
   Fun(SDL_DestroyWindow)
   Fun(SDL_SetRenderDrawColor)
   Fun(SDL_RenderClear)
   Fun(SDL_RenderPresent)
   If (SDL_Init($20) = 0)
      If (SDL_CreateWindowAndRenderer(640, 480, 0, @*win, @*ren) = 0)
         SDL_SetRenderDrawColor(*ren, 0, 255, 0, 255)
         SDL_RenderClear(*ren)
         SDL_RenderPresent(*ren)
         For i=0 to 100
            SDL_PumpEvents() ; fix for Mac
         Next
         Delay(2000)
         SDL_DestroyRenderer(*ren)
         SDL_DestroyWindow(*win)
      EndIf
      SDL_Quit()
   EndIf
EndIf
Edit: Added a bit more info, refined macro for general use and updated SDL2 link
Last edited by Piero on Sat Sep 13, 2025 5:05 pm, edited 4 times in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 6254
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: SDL2 lib

Post by mk-soft »

You do not have to copy the dylib into the APP by hand.
It belongs in "your.app/Contents/Resources"

Link: PB IDE Tool MyAppData
Link: PathHelper
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Piero
Addict
Addict
Posts: 961
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: SDL2 lib

Post by Piero »

mk-soft wrote: Fri Sep 12, 2025 1:30 am You do not have to copy the dylib into the APP by hand.
Well, from what I've seen the framework seems more convenient than the dylib (I'll eventually test with the incoming inline C…)
I already knew your tools, but I just fixed and IMPROVED an old post…
Anyway, once you created the "Frameworks" folder and put stuff inside, recompiling/overwriting is NOT a problem with my method (at least with PB 6); the framework stays inside the app!

Your tools are very useful for "more complex" apps (Info.plist etc.); THANKS!
Post Reply