set up SDL2
-
- Posts: 3906
- Joined: Jan 01, 2009 7:03
- Location: Australia
set up SDL2
Recently I had decided to return to writing my programs in C++ using the code::blocks IDE. This meant using some graphic library to replace the FreeBASIC graphics and image commands and at this point in time SDL2 seems to be all I need to translate or duplicate my FreeBASIC programs to C++. After much hassle I was eventually able to set up the IDE to compile C++ programs using the SDL2 library.
Tonight I thought perhaps to look at FreeBASIC programming using the SDL2 library.
viewtopic.php?f=14&t=21725
Needless to say I am unsuccessful so far at getting the examples to work. Actually it was the showimage.bas example I tried to compile. I moved the .bi files into the examples folder along with the SDL2.dll and SDL2_image.dll without success.
It highlights an issue with FreeBASIC and libraries. The examples don't just work. The downloads do not place everything where they need to be in order to open/load and run the examples.
Now that I am able to compile and run the C++ SDL2 program examples successfully I thought it might be interesting to see if I could translate some of the simpler ones to FreeBASIC. Has anyone done this and knows how to set it all up?
Tonight I thought perhaps to look at FreeBASIC programming using the SDL2 library.
viewtopic.php?f=14&t=21725
Needless to say I am unsuccessful so far at getting the examples to work. Actually it was the showimage.bas example I tried to compile. I moved the .bi files into the examples folder along with the SDL2.dll and SDL2_image.dll without success.
It highlights an issue with FreeBASIC and libraries. The examples don't just work. The downloads do not place everything where they need to be in order to open/load and run the examples.
Now that I am able to compile and run the C++ SDL2 program examples successfully I thought it might be interesting to see if I could translate some of the simpler ones to FreeBASIC. Has anyone done this and knows how to set it all up?
Re: set up SDL2
Are you using 32 or 64 bit compiler? All the required .bi files need to be in the 'inc/SDL"' folder of your compiler. And all needed DLLs (they're a lot) need to go in the 'lib' folder. The only example that will work, if you're using SDL2, is 'sld2_hello.bas' (all the others are for the earlier version). This is the output you should get:BasicCoder2 wrote:Needless to say I am unsuccessful so far at getting the examples to work. Actually it was the showimage.bas example I tried to compile. I moved the .bi files into the examples folder along with the SDL2.dll and SDL2_image.dll without success.
Note that you'll also need a lot of other bindings for this to work (two of them are SDL2_image.dll and SDL2_ttf.dll, among many others). You can download these from here: https://www.libsdl.org/projects/
Re: set up SDL2
This is the 'corrected' (I just hacked it to work with SDL2) example of 'music_test1.bas' of the examples folder. Press any key when the window is opened to play some .ogg music clip:
Code: Select all
' SDL_music example adapted to freeBasic from:
' http://www.kekkai.org/roger/sdl/mixer/
'
' music.ogg is a freely distributed song by "Honest Bob and the
' Factory-to-Dealer Incentives" called "Another person you had sex with" for
' people who wanted an ogg file to test their programs with.
'
' Press any to start/stop the music, ESC to exit.
#include "SDL2\SDL2.bi"
#include "SDL2\SDL2_mixer.bi"
' Mix_Music actually holds the music information.
dim shared music as Mix_Music ptr
music = NULL
declare sub handlekey(byval key as SDL_KeyboardEvent ptr)
declare sub musicDone cdecl ()
'dim video as SDL_Surface ptr
dim event as SDL_Event
dim done as integer
done = 0
dim audio_rate as integer
dim audio_format as Uint16
dim audio_channels as integer
dim audio_buffers as integer
' We're going to be requesting certain things from our audio
' device, so we set them up beforehand
audio_rate = 44100
audio_format = AUDIO_S16
audio_channels = 2
audio_buffers = 4096
SDL_Init(SDL_INIT_VIDEO or SDL_INIT_AUDIO)
' This is where we open up our audio device. Mix_OpenAudio takes
' as its parameters the audio format we'd /like/ to have.
if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) then
print "Unable to open audio!"
end 1
end if
' If we actually care about what we got, we can ask here. In this
' program we don't, but I'm showing the function call here anyway
' in case we'd want to know later.
Mix_QuerySpec(@audio_rate, @audio_format, @audio_channels)
' We're going to be using a window onscreen to register keypresses
' in. We don't really care what it has in it, since we're not
' doing graphics, so we'll just throw something up there.
var video = SDL_CreateWindow("SDL2 test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0)
'video = SDL_SetVideoMode(320, 240, 0, 0)
do while (done = 0)
do while (SDL_PollEvent(@event))
select case (event.type)
case SDL_QUIT_
done = 1
case SDL_KEYDOWN
if( event.key.keysym.sym = SDLK_ESCAPE ) then
done = -1
end if
handleKey @event.key
end select
loop
' So we don't hog the CPU
SDL_Delay(50)
loop
' This is the cleaning up part
Mix_CloseAudio
SDL_Quit
sub handleKey (byval key as SDL_KeyboardEvent ptr)
dim keyEvent as SDL_KeyboardEvent
keyEvent = *key
' Here we're going to have the 'm' key toggle the music on and
' off. When it's on, it'll be loaded and 'music' will point to
' something valid. If it's off, music will be NULL.
if (music = NULL) then
' Actually loads up the music
music = Mix_LoadMUS("data/music.ogg")
' This begins playing the music - the first argument is a
' pointer to Mix_Music structure, and the second is how many
' times you want it to loop (use -1 for infinite, and 0 to
' have it just play once)
Mix_PlayMusic(music, 0)
' We want to know when our music has stopped playing so we
' can free it up and set 'music' back to NULL. SDL_Mixer
' provides us with a callback routine we can use to do
' exactly that
Mix_HookMusicFinished(@musicDone)
else
' Stop the music from playing
Mix_HaltMusic
' Unload the music from memory, since we don't need it
' anymore
Mix_FreeMusic(music)
music = NULL
end if
end sub
' This is the function that we told SDL_Mixer to call when the music
' was finished. In our case, we're going to simply unload the music
' as though the player wanted it stopped. In other applications, a
' different music file might be loaded and played.
sub musicDone cdecl ()
Mix_HaltMusic
Mix_FreeMusic(music)
music = NULL
end sub
-
- Posts: 3906
- Joined: Jan 01, 2009 7:03
- Location: Australia
Re: set up SDL2
I am using FreeBASIC-1.05.01-win32 so I assume it is 32 bitpaul doe wrote:Are you using 32 or 64 bit compiler?
Although I am using 64-bit Operating System, x64-based processor I downloaded the 32 bit SDL2 to run with C++ for as I understand it a compiled 64 bit program will not work on a 32 bit system whereas the 32 bit version will work on both.
Ok what happened was this:
I downloaded and unzipped FB-SDL-2.0.1 into the C:\FreeBasic\
Code: Select all
'C:
' > FreeBasic
' > bin
' > doc
' > docs
' > examples
' > FB-SDL-2.0.1
' > examples
' > graphics
' > SDL2
' > inc
' > SDL2
' > IDE
' > inc
' > SDL2
I then copied the .dll I have been using with the SDL2 C++ examples (LazyFoo and other examples) into C:\FreeBasic\FB-SDL-2.0.1\examples\graphics\SLD2 folder
window.bas worked ok.
Renderer.bas compiled and ran to produce a window without the sdl.bmp and then popped up a windows complaint,
renderer.exe has stopped working.
The examples seem to be from a dead link,
http://www.kekkai.org/roger/sdl/mixer/
downloaded,
we_wish_you_a_merry_xmas.ogg
https://stereoninjamusic.weebly.com/hq- ... -2015.html
and placed it in the same folder as your version of music.bas but although it compiled with a window I couldn't get any music from it although trying to toggle it with the m key.
Re: set up SDL2
This usually means it can't find a DLL. In Windows, DLLs should be in the same directory as the EXEcutable.BasicCoder2 wrote:Renderer.bas compiled and ran to produce a window without the sdl.bmp and then popped up a windows complaint,
renderer.exe has stopped working.
-
- Posts: 3906
- Joined: Jan 01, 2009 7:03
- Location: Australia
Re: set up SDL2
These are the dll files that I put into
C:\FreeBasic\FB-SDL-2.0.1\examaples\graphics\SDL2\
the same folder as the exe
libjpeg-9.dll
libpng16-16.dll
libtiff-5.dll
libwebp-7.dll
SDL2.dll
SDL2.image.dll
SDL2.mixer.dll
zlib1.dll
as well as the,
we_wish_you_a_merry_xmas.ogg file
C:\FreeBasic\FB-SDL-2.0.1\examaples\graphics\SDL2\
the same folder as the exe
libjpeg-9.dll
libpng16-16.dll
libtiff-5.dll
libwebp-7.dll
SDL2.dll
SDL2.image.dll
SDL2.mixer.dll
zlib1.dll
as well as the,
we_wish_you_a_merry_xmas.ogg file
Re: set up SDL2
Yes, I will zip my folders with tutorial c++ to fb translated examples and share with you. It has all the needed .dll files and a lot of working examples, but it is work in progress.BasicCoder2 wrote:Now that I am able to compile and run the C++ SDL2 program examples successfully I thought it might be interesting to see if I could translate some of the simpler ones to FreeBASIC. Has anyone done this and knows how to set it all up?
https://www.dropbox.com/s/fyhc06x72rl4ltg/SDL2.zip?dl=0
Cheers,
Mike
Re: set up SDL2
Every binding should be there. This is explained in the FB manual, BTW.BasicCoder2 wrote:I should have copied the contents of FB-SDL-2.0.1/inc/SDL2 into FreeBasic/inc/SDL2
Try to get the examples mentioned in this thread working first, then try with those. I have modified another example from the 'examples/graphics/SDL' folder that (should) come with your FB distro. Copy it to said folder (along with all DLLs needed), and see if it compiles and runs. You should see a white window and hear a repeating 'phaser' sound:BasicCoder2 wrote:I then copied the .dll I have been using with the SDL2 C++ examples (LazyFoo and other examples) into C:\FreeBasic\FB-SDL-2.0.1\examples\graphics\SLD2 folder
window.bas worked ok.
Renderer.bas compiled and ran to produce a window without the sdl.bmp and then popped up a windows complaint,
renderer.exe has stopped working.
The examples seem to be from a dead link,
http://www.kekkai.org/roger/sdl/mixer/
downloaded,
we_wish_you_a_merry_xmas.ogg
https://stereoninjamusic.weebly.com/hq- ... -2015.html
and placed it in the same folder as your version of music.bas but although it compiled with a window I couldn't get any music from it although trying to toggle it with the m key.
Code: Select all
' SDL_music example adapted to freeBasic from:
' http://www.kekkai.org/roger/sdl/mixer/
'
' phaser.wav is phaser noise which comes with the OpenAL library.
'
' Press any to start/stop the wav file, ESC to exit.
#include "SDL2\SDL2.bi"
#include "SDL2\SDL2_mixer.bi"
' Mix_Chunk is like Mix_Music, only it's for ordinary sounds.
dim shared phaser as Mix_Chunk ptr
phaser = NULL
' Every sound that gets played is assigned to a channel. Note that
' this is different from the number of channels you request when you
' open the audio device; a channel in SDL_mixer holds information
' about a sound sample that is playing, while the number of channels
' you request when opening the device is dependant on what sort of
' sound you want (1 channel = mono, 2 = stereo, etc)
dim shared phaserChannel as integer
phaserChannel = -1
declare sub handleKey(byval key as SDL_KeyboardEvent ptr)
'dim video as SDL_Surface ptr
dim event as SDL_Event
dim done as integer
done = 0
' Same setup as before
dim audio_rate as integer
dim audio_format as Uint16
dim audio_channels as integer
dim audio_buffers as integer
audio_rate = 22050
audio_format = AUDIO_S16
audio_channels = 2
audio_buffers = 4096
SDL_Init(SDL_INIT_VIDEO or SDL_INIT_AUDIO)
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) then
print "Unable to open audio!"
end 1
end if
' We're going to pre-load the sound effects that we need right here
phaser = Mix_LoadWAV("data/phaser.wav")
var video = SDL_CreateWindow("SDL2 test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0)
'video = SDL_SetVideoMode(320, 240, 0, 0)
do while (done = 0)
do while (SDL_PollEvent(@event))
select case (event.type)
case SDL_QUIT_
done = 1
case SDL_KEYDOWN
if( event.key.keysym.sym = SDLK_ESCAPE ) then
done = -1
end if
handleKey @event.key
end select
loop
SDL_Delay(50)
loop
Mix_CloseAudio
SDL_Quit
sub handleKey (byval key as SDL_KeyboardEvent ptr)
dim keyEvent as SDL_KeyboardEvent
keyEvent = *key
if (phaserChannel < 0) then
' Mix_PlayChannel takes, as its arguments, the channel that
' the given sound should be played on, the sound itself, and
' the number of times it should be looped. If you don't care
' what channel the sound plays on, just pass in -1. Looping
' works like Mix_PlayMusic. This function returns the channel
' that the sound was assigned to, which you'll need later.
phaserChannel = Mix_PlayChannel(-1, phaser, -1)
else
'Mix_HaltChannel stops a certain channel from playing - this
'is one of the reasons we kept track of which channel the
'phaser had been assigned to
Mix_HaltChannel(phaserChannel)
phaserChannel = -1
end if
end sub
SDL_image: https://www.libsdl.org/projects/SDL_image/
SDL_mixer: https://www.libsdl.org/projects/SDL_mixer/
SDL_ttf: https://www.libsdl.org/projects/SDL_ttf/
Along with SDL2 itself: https://www.libsdl.org/download-2.0.php
Copy all that to either the lib folder of FB, or to the folder where you have the FB examples (as caseih suggested), and try again. See if the three examples I mentioned before run (they should).
That's very generous of you, Mike, thanks. I'll see if I can compile a working distribution of SDL2 with everything included, and upload it to have it handy. It will take me some time, as I never used SDL before, but I hope to have both 32 and 64 versions of it ready soon.h4tt3n wrote:Yes, I will zip my folders with tutorial c++ to fb translated examples and share with you. It has all the needed .dll files and a lot of working examples, but it is work in progress.
-
- Posts: 3906
- Joined: Jan 01, 2009 7:03
- Location: Australia
Re: set up SDL2
Wow! Thanks Mike. All done I don't have to do it myself. Nice xmas present to play with over the next few weeks.h4tt3n wrote:I will zip my folders with tutorial c++ to fb translated examples and share with you.
-
- Posts: 3906
- Joined: Jan 01, 2009 7:03
- Location: Australia
Re: set up SDL2
I wonder how up to date they are kept?paul doe wrote:Every binding should be there. This is explained in the FB manual, BTW.
I have modified another example from the 'examples/graphics/SDL' folder that (should) come with your FB distro.
Your modified program compiled an ran as expected.
Re: set up SDL2
The ones that you linked worked just fine here, with SLD 2.0.7 (the latest stable version)BasicCoder2 wrote:I wonder how up to date they are kept?paul doe wrote:Every binding should be there. This is explained in the FB manual, BTW.
Perfect then. It's not really difficult, see, just messy. There's a crapton of binaries you need (and you have to make them work all together, so it's a good idea to keep your version of SDL segregated (so you'll know exactly which DLLs you need to run a particular example).BasicCoder2 wrote:Your modified program compiled an ran as expected.
-
- Posts: 3906
- Joined: Jan 01, 2009 7:03
- Location: Australia
Re: set up SDL2
So I should store all my current tutorials in a folder called SDL 2.0.7paul doe wrote:Perfect then. It's not really difficult, see, just messy. There's a crapton of binaries you need (and you have to make them work all together, so it's a good idea to keep your version of SDL segregated (so you'll know exactly which DLLs you need to run a particular example).
That it is "messy" is interesting in that using the graphics that come with FreeBASIC there is no mess involved and yet behind the scenes all this "mess" must be working itself out with no user setups required proving it can be done.
At the moment I am concentrating on writing in C++ which is the only reason I am using the SDL library because I found FreeBASIC has all the commands I needed and thus I had no need to use the SDL library with FB. However it might be easier to translate from a C++ program that uses SDL to a FB program that also uses SDL. Or maybe not. For example the grayscale example here,
http://gigi.nullneuron.net/gigilabs/wri ... tutorials/
can also be done using a FB image.
Some things might be harder to convert. In the LINES example it uses an iterator and list in a way I have not used in simple C programs.
for (std::list<Line>::const_iterator i = lines.begin(); i != lines.end(); ++i)
Re: set up SDL2
Not the example code, the binaries! You'll need to modify the headers to account for this distribution. I suggested this so, when you need to ship your code (if you want to share it, for example), you have all the binaries you need in a single place. If you modify the headers appropriately, you can get away with a simple '#include'.BasicCoder2 wrote:So I should store all my current tutorials in a folder called SDL 2.0.7
There's no comparing the two of them, SDL provides far more functionality than GFXLib2 (because they were designed with different goals in mind). Besides, the GFXLib2 link statically with your program when you call 'screenRes' or 'screen', so it's included in your executable. SDL2, while you can link statically to it (the links you provided have the needed import libraries), it will add considerable bloat to your .EXE (depending on what functionality you need). Of course it can be done, you just have to know how to do it.BasicCoder2 wrote:That it is "messy" is interesting in that using the graphics that come with FreeBASIC there is no mess involved and yet behind the scenes all this "mess" must be working itself out with no user setups required proving it can be done.
The SDL part should be equivalent. The rest of the code is the one you need to adapt (see, for example, this thread; the GL code is code you can find on numerous C++ tutorials, nothing fancy).BasicCoder2 wrote:At the moment I am concentrating on writing in C++ which is the only reason I am using the SDL library because I found FreeBASIC has all the commands I needed and thus I had no need to use the SDL library with FB. However it might be easier to translate from a C++ program that uses SDL to a FB program that also uses SDL. Or maybe not. For example the grayscale example here,
http://gigi.nullneuron.net/gigilabs/wri ... tutorials/
can also be done using a FB image.
Yes, there's no STL equivalent in FB, at least not natively provided. For a simple list implementation, you can use the one I shipped with the metaballs demo (which is called ArrayList).BasicCoder2 wrote:Some things might be harder to convert. In the LINES example it uses an iterator and list in a way I have not used in simple C programs.
for (std::list<Line>::const_iterator i = lines.begin(); i != lines.end(); ++i)
-
- Posts: 3906
- Joined: Jan 01, 2009 7:03
- Location: Australia
Re: set up SDL2
And there is the rub :)paul doe wrote:Of course it can be done, you just have to know how to do it. [my emphasis]
Interesting that most of the regular posters to the forum seem to know so much about C++ programming that they could easily do everything in C++ instead of FB and not have to worry about FB not implementing some C++ feature.Yes, there's no STL equivalent in FB, at least not natively provided.
The reason I took on FreeBASIC nearly nine years ago was because I was having trouble using SDL with the DevCPP IDE although I managed to get some SDL examples working with the DevPak automatic setup facility. My interest was not in computer languages as such, it was about getting my computer to do cool stuff despite my limitations in computer science. This was the original reason why the BASIC language was developed in the first place.
Anyway thanks for the response and I will see how I go using SDL with C++ and making FreeBASIC versions.
Re: set up SDL2
Yeah, I know C++ too (among others). I don't worry so much about the things that FB doesn't implement, for I don't need them. Most of my C/C++ code usually maps 1:1 to FreeBasic (which is why I'm slowly porting my entire framework to FB). The things that don't, I simply reimplement them natively (such as most data structures). FreeBasic IS an elegant and powerful language, if you stay away from all those stupid QB-isms that plague it, unfortulately.BasicCoder2 wrote:Interesting that most of the regular posters to the forum seem to know so much about C++ programming that they could easily do everything in C++ instead of FB and not have to worry about FB not implementing some C++ feature.
It's curious that most people have such great misconceptions regarding the invention of BASIC. The original one by Kemmeny and Kurtz was invented to aid in teaching CS concepts, not to be 'easy and accessible', and it's very much different that the BASIC we now know. Not to mention that things changed VERY much since BASIC inception.BasicCoder2 wrote:The reason I took on FreeBASIC nearly nine years ago was because I was having trouble using SDL with the DevCPP IDE although I managed to get some SDL examples working with the DevPak automatic setup facility. My interest was not in computer languages as such, it was about getting my computer to do cool stuff despite my limitations in computer science. This was the original reason why the BASIC language was developed in the first place.
You're welcome. I'll see if I can prepare a SDL2 distro and some examples, too.BasicCoder2 wrote:Anyway thanks for the response and I will see how I go using SDL using C++ and and making FreeBASIC versions.