Bridgecommand Compile for the Raspberry Pi 3

This is a fork from https://github.com/bridgecommand/bc
The modifications in this fork are intended to allow you to run and compile bridgecommand on the Raspberry Pi 3.

Differences:
- Binaries (when done) will be ready for the Pi 3 Raspbian
- Height maps in the World folder are 256x256, to prevent invisible land masses due to graphic driver limitations
- ini file settings are set to lower perfomance so bridgecommand will work on the Pi 3

The Pi 3 and Raspbian have the ability to use GL drivers. The Pi 3 only has 1 GB of RAM, which limits the possible size of textures, etc.

Things I like to set up on Raspbian first:
-A large swap file. Mine is 2 GB. https://www.bitpi.co/2015/02/11/how-to-change-raspberry-pis-swapfile-size-on-rasbian/#:~:text=The%20Raspbian%20distribution%20comes%20with,available%20RAM%20on%20the%20machine.
-If using a USB device to boot from: "dtoverlay=sdtweak,poll_once" in config.txt. See: https://github.com/raspberrypi/linux/issues/2567
-logs to ram, not SD card, or turn logs off: https://raspberrypi.stackexchange.com/questions/62533/how-can-i-reduce-the-writing-to-log-files

Things you must set up:
-Use a high quality sd card or ssd drive. Perfomance is drastically affected by storage media. https://www.raspberrypi.org/documentation/installation/sd-cards.md
-Ensure Raspbian is up to date:
  sudo apt-get update
  sudo apt-get dist-upgrade
-Enable opengl in Raspbian:
  Run: raspi-config
  choose '7 - advanced' then 'A7 GL Driver' then 'G2 - GL Fake KMS'
  Reboot
-For sound, we must type this in a terminal: 
  export PA_ALSA_PLUGHW=1 (or we get the following error: Pa_OpenDefaultStream failed. Segmentation fault)
  You can set it permanently by: sudo nano /etc/enviornment
  Then adding: PA_ALSA_PLUGHW=1
  to the last line then saving. Then reboot.

Running tips:
- Run bridgecommand-ini first
  - set 'water_segments'. Set it to 8 or lower. The processing power required for the wave motion is just too much for the Raspberry Pi. To fix this I changed the setting in MovingWater.cpp. I changed 'segments = 32' to 'segments=8'. This is on line 86. Mr. Packer wrote this setting into his code. I haven't had a crash or a freeze on the Pi since this setting was available and set appropriately.
  - Turn off Shading
  - set width and height to: 800x600 or less 640x480 and start in windowed mode. It will not work well at higher resolutions.
  - set 'use_directx' to 0
- Run bridgecommand-bc alone. It often freezes the gui when the browser or other memory intense programs are open.
- Boot off a fast SD card, not a USB stick unless it is very fast. I use an external, low power, 60 GB SSD and it is very fast.
- Use IP addresses, not the pc network name, on the Pi for repeater and network settings.

--------------

Compiling with Raspbian:

- Download source code from:
  https://github.com/bridgecommand/bc
  Extract to your desktop. This will be reffered to as  below.

- Install required libraries:
  sudo apt-get install mesa-common-dev libxxf86vm-dev freeglut3-dev libxext-dev libxcursor-dev
  (if sound is required)
  sudo apt install libsndfile1 libportaudio2 libportaudiocpp0  libsndfile1-dev portaudio19-dev libasound2-dev 

Instructions to compile the sound library files are in CompilingLinuxAndMac.txt, they do not work with the Raspbian OS. 
The following are ways I have built Bridgecommand with sound in Raspbian.
If you don't need sound, jump down to 'Compiling without sound:'

1. The easiest way is to add sound is to compile Bridgecommand using pre-installed system SHARED libraries we installed earlier.
sudo apt install libsndfile1 libportaudio2 libportaudiocp0  libsndfile1-dev portaudio19-dev libasound2-dev 
Jump down to 'Compiling with sound:'

2. If you need to have a version with STATIC sound libraries built in to the image, do the following.
Make sure there are no installed sound libraries already.
sudo apt remove libsndfile1 libportaudio2 libportaudiocpp0  libsndfile1-dev portaudio19-dev 
(you may want to install afterwards as other programs may use these libraries)

Get source from: (clone)
https://github.com/erikd/libsndfile
as per instructions:
./autogen.sh
./configure --enable-werror
sudo make
sudo make check

Alternately you can install and use cmake:
sudo apt install cmake
cmake
sudo make

Then find and copy libsndfile.a over to /home/pi/Desktop/bc-master/libs/libsndfile/lib

Get source from:
https://app.assembla.com/spaces/portaudio/git/source  (see clone)
cmake ./
sudo make
sudo make install
copy libportaudio.a only from /usr/local/lib to /home/pi/Desktop/bc-master/libs/portaudio/lib

Jump down to 'Compiling with sound:'

3. Instead of downloading and compiling the libsndfile.a and libportaudio.a, I copy them from the apt installs we did earlier. 
sudo apt install libsndfile1 libportaudio2 libportaudiocpp0  libsndfile1-dev portaudio19-d libsndfile.a and libportaudio.aev libasound2-dev 
The  libsndfile.a and libportaudio.a files are in ‘ /usr/lib/arm-linux-gnueabihf/’ and you can copy them to /libs/libsndfile/ and /libs/portaudio/lib/
Now remove those two sound libraries so that the shared libraries are not the ones linked to.
sudo apt remove libsndfile1 libportaudio2 libportaudiocpp0  libsndfile1-dev portaudio19-dev
Add the following to the USERLDFLAGS in MakefileWithSound
-lFLAC
-logg
-ljack
-lvorbis
-lvorbisenc

Compiling with sound:
Start from a clean build: "make -f MakefileWithSound clean". 
In a terminal at  type ‘make -f MakefileWithSound’

Compiling without sound:
Start from a clean build: "make clean". 
In a terminal at  type ‘make’

-------------------

Compiling with CMAKE with sound:

sudo apt install portaudio19-dev  libsndfile1-dev libasound2-dev

We must type this in a terminal:: 
export PA_ALSA_PLUGHW=1
or we get:
Pa_OpenDefaultStream failed.
Segmentation fault

In CmakeLists.txt
add:
set(CMAKE_CXX_FLAGS "-DWITH_SOUND")

Add in target_link_libraries(bridgecommand-bc PRIVATE :
-lsndfile 
-lportaudio
-lasound

#(this goes in every CmakeLists.txt that fails with thread errors)
-lpthread 

Install cmake:
Sudo apt install cmake

Then run the following in a terminal at :
Cmake ./
Make

---------------

Compile Bridgecommand for the Raspberry Pi 3 (using Gentoo Linux 64 OS):

Gentoo Linux can be downloaded here: https://github.com/sakaki-/gentoo-on-rpi-64bit
edit bc-master/libs/Irrlicht/irrlicht-svn/source/Irrlicht/libpng/pngpriv.h and change:
define PNG_FILTER_OPTIMIZATIONS
to
define NO_PNG_FILTER_OPTIMIZATIONS
then run 'make'

------------------------------------------------------

Additional tools: https://github.com/vpelss/BridgeCommand_NEMA_UDP_to_File_HTML

-------------

Note: These libraries are installed by default on Raspbian
libsndfile1-1.0.28-6
libportaudio2-19.6.0-1
libasound2-1.1.8+1
libasound2-data-1.1.8
to re-add them
sudo apt install libsndfile1 libportaudio2 libasound2-dev libasound2-data

.so share
.a archive
note: dev lib are required to link against .so files plus provide headers