# Building PolyGun
## `CMakeLists.txt` documentation
`CMakeLists.txt` has following variables which can be customized to your needs
(however if your configuration is pretty standard then everything should be detected automatically and nothing has to be set):
- `BUILD_FLAG_SUPPRESS_WARNING_MODE`, if true compiling with highest warning mode will be omitted.
- `BUILD_CLIENT`, if true client will be added to resulting executable.
	Please note that this will require all libraries which client depends on to be installed.
- `BUILD_SERVER`, if true server will be added to resulting executable.
	If building multi-configuration executable (i.e. client and server) then server can be
	started from command line using `server` argument, example: `./polygun server`.
	If `BUILD_SERVER` is only configuration enabled then executable name will be changed to `polygun_server`.
- `RENDERER_GL`, if true rendering with OpenGL support. This option is only valid if `BUILD_CLIENT` is enabled.
	By default enabled on every platform except Apple operating systems (i.e. macOS and iOS) and SerenityOS.
- `RENDERER_LEGACY_GL`, if true rendering with OpenGL 1.x will be enabled. Only valid if `BUILD_CLIENT` is enabled.
	By default only enabled on SerenityOS. If your hardware/operating system supports OpenGL 2.0+ then use RENDERER_GL instead.
- `WINDOW_WIN32`, if true window creation using Win32 API will be enabled. It's Windows only and enabled there by default.
- `WINDOW_X11`, if true window creation using X11 will be enabled. It's only valid on Unix-like systems and is detected there automatically.
- `WINDOW_WAYLAND`, if true window creation using Wayland will be enabled. It's only valid on Linux and is detected there automatically.
- `WINDOW_SERENITYOS` if true window creation on SerenityOS will be supported.
- `HAVE_STRSIGNAL`, if true `strsignal` from C library will be used, otherwise PolyGun will provide it's own
	implementation. Note that you shouldn't have to set this manually because it's automatically detected.
- `X11_INCLUDE_DIR`, only valid if `WINDOW_X11` was set to `TRUE`, it's path to X11 headers. It should point to folder with `X11` directory.
- `X11_LIBRARIES`, only valid if `WINDOW_X11` was set to `TRUE`, it's path to X11 libraries separated by `;`
- `WaylandClient_INCLUDE_DIR`, only valid if `WINDOW_WAYLAND` was set to `TRUE`, it's path to libwayland-client headers.
- `WaylandClient_LIBRARY`, only valid if `WINDOW_WAYLAND` was set to `TRUE`, it's path to libwayland-client library.
- `WaylandEGL_INCLUDE_DIR`, only valid if `WINDOW_WAYLAND` and `RENDERER_GL` were set to `TRUE`, it's path to libwayland-egl headers.
- `WaylandEGL_LIBRARY`, only valid if `WINDOW_WAYLAND` and `RENDERER_GL` were set to `TRUE`, it's path to libwayland-egl library.
- `WaylandCursor_INCLUDE_DIR`, only valid if `WINDOW_WAYLAND` was set to `TRUE`, it's path to libwayland-cursor headers.
- `WaylandCursor_LIBRARY`, only valid if `WINDOW_WAYLAND` was set to `TRUE`, it's path to libwayland-cursor library.
- `Libdecor_INCLUDE_DIR`, only valid if `WINDOW_WAYLAND` was set to `TRUE`, it's path to libdecor headers.
- `Libdecor_LIBRARY`, only valid if `WINDOW_WAYLAND` was set to `TRUE`, it's path to libdecor library.
- `XKBCommon_INCLUDE_DIR`, path to XKBCommon headers. It should point to directory with `xkbcommon` folder.
- `XKBCommon_LIBRARY`, path to XKBCommon library.
- `OPENGL_INCLUDE_DIR`, path to OpenGL headers. It should point to folder which contains `GL` directory. You shouldn't have to set this because
	usually OpenGL is provided with C++ compiler.
- `OPENGL_LIBRARIES`, path to OpenGL libraries separated by `;`. Similar to OpenGL headers you shouldn't have to set this manually.
- `GLM_INCLUDE_DIRS`, path to GLM headers. This should point to folder with `glm` directory.
- `OPENAL_INCLUDE_DIR`, path to OpenAL headers. This should point to `AL` folder.
- `OPENAL_LIBRARY`, path to OpenAL library, either static or shared.
- `OGG_INCLUDE_DIR`, path to libogg headers. This should point to folder with `ogg` directory.
- `OGG_LIBRARY`, path to libogg library, either static or shared.
- `VORBIS_INCLUDE_DIR`, path to libvorbis headers. This should point to folder with `vorbis` directory.
- `VORBIS_LIBRARY`, path to libvorbis library, either static or shared.
- `PNG_INCLUDE_DIRS`, path to libpng headers. This should point to folder with `png.h` file.
- `PNG_LIBRARIES`, path to libpng library, either static or shared.
- `ZLIB_INCLUDE_DIRS`, path to zlib headers. This should point to folder with `deflate.h` file.
- `ZLIB_LIBRARIES`, path to zlib library, either static or shared.
- `FREETYPE_INCLUDE_DIRS`, path to FreeType 2 headers. This should point to folder with `ft2build.h` file.
- `FREETYPE_LIBRARIES`, path to FreeType 2 library, either static or shared.

## Required libraries
For `BUILD_CLIENT`:
| Library                                                                    | Description                    | Required for                     | Custom CMake variables                                     |
|----------------------------------------------------------------------------|--------------------------------|----------------------------------|------------------------------------------------------------|
| OpenGL                                                                     | Rendering via OpenGL api       | `RENDERER_{LEGACY}_GL`           | *none*                                                     |
| [Xlib](https://www.x.org/wiki)                                             | Window creation using X11      | `WINDOW_X11`                     | - `X11_INCLUDE_DIR`<br>- `X11_LIBRARIES`                   |
| [libwayland-client](https://wayland.freedesktop.org)                       | Window creation using Wayland  | `WINDOW_WAYLAND`                 | - `WaylandClient_INCLUDE_DIR`<br>- `WaylandClient_LIBRARY` |
| [libwayland-egl](https://wayland.freedesktop.org)                          | Using EGL with Wayland         | `WINDOW_WAYLAND` + `RENDERER_GL` | - `WaylandEGL_INCLUDE_DIR`<br>- `WaylandClient_LIBRARY`    |
| [libwayland-cursor](https://wayland.freedesktop.org)                       | Cursor management for Wayland  | `WINDOW_WAYLAND`                 | - `WaylandCursor_INCLUDE_DIR`<br>- `WaylandCursor_LIBRARY` |
| [libdecor](https://gitlab.freedesktop.org/libdecor/libdecor)               | Window management for Wayland  | `WINDOW_WAYLAND`                 | - `Libdecor_INCLUDE_DIR`<br>- `Libdecor_LIBRARY`           |
| [XKBCommon](https://xkbcommon.org)                                         | Text input handling            | `WINDOW_WAYLAND`                 | - `XKBCommon_INCLUDE_DIR`<br>- `XKBCommon_LIBRARY`         |
| [GLM](https://glm.g-truc.net/0.9.9/index.html)                             | Vector and matrix maths        | *always*                         | `GLM_INCLUDE_DIRS`                                         |
| [OpenAL](https://www.openal.org) or [OpenAL Soft](https://openal-soft.org) | Playing sounds                 | *always*                         | - `OPENAL_INCLUDE_DIR`<br>- `OPENAL_LIBRARY`               |
| [libogg](https://www.xiph.org/ogg)                                         | Loading Ogg files              | *always*                         | - `OGG_INCLUDE_DIR`<br>- `OGG_LIBRARY`                     |
| [libvorbis](https://www.xiph.org/vorbis)                                   | Decoding Vorbis encoded audio  | *always*                         | - `VORBIS_INCLUDE_DIR`<br>- `VORBIS_LIBRARY`               |
| [libpng](http://www.libpng.org/pub/png/libpng.html)                        | Loading PNG images             | *always*                         | - `PNG_INCLUDE_DIRS`<br>- `PNG_LIBRARIES`                  |
| [zlib](https://www.zlib.net)                                               | libpng dependency              | *always*                         | - `ZLIB_INCLUDE_DIRS`<br>- `ZLIB_LIBRARIES`                |
| [FreeType 2](https://freetype.org)                                         | Text rendering                 | *always*                         | - `FREETYPE_INCLUDE_DIRS`<br>- `FREETYPE_LIBRARIES`        |

For `BUILD_SERVER`:
| Library                                              | Description           | Required for          | Custom CMake variables |
|------------------------------------------------------|-----------------------|-----------------------|------------------------|
| [AngelScript](https://www.angelcode.com/angelscript) | server side scripting | *always*              | *none*                 |

If we forgot to mention some library then please create issue.

## Building
### GNU/Linux, Alpine Linux
1. Install required tools if you don't have them (you can use Clang instead of g++ or Ninja instead of GNU Make if you like):
   - for Debian based distributions use: `sudo apt install g++ make cmake git` or `sudo apt install clang ninja-build cmake git`.
   - for Fedora use: `sudo dnf install gcc-c++ make cmake git` or `sudo dnf install clang ninja-build cmake git`.
   - for openSUSE use: `sudo zypper install --no-recommends g++ make cmake git` or `sudo zypper install --no-recommends clang ninja cmake git`.
   - for Alpine Linux use: `sudo apk add g++ make cmake git` or `sudo apk add clang ninja-build cmake git`.
2. Clone repository with following command: `git clone http://git.cubesoftware.xyz:20524/PolyGun/PolyGun.git`. You can add `--depth=1` argument
to reduce used disk space via not downloading entire git history.
3. Enter directory with downloaded PolyGun repository using `cd PolyGun`.
4. Install required libraries. Libraries may vary depending on what build configuration you will use. This tutorial assumes full build with X11 windowing.
   - for Debian based distributions use: `sudo apt install libx11-dev libglm-dev libopenal-dev libvorbis-dev libpng-dev libfreetype-dev`. `libogg-dev` and `libzlib-dev` will be
   installed automatically because they are dependencies of `libvorbis-dev` and `libpng-dev` respectively.
   - for Fedora use: `sudo dnf install libX11-devel glm-devel openal-soft-devel libogg-devel libvorbis-devel libpng-devel freetype-devel`. `libz-devel` will be installed automatically
   because it's dependency of `libpng-devel`.
   - for openSUSE use: `sudo zypper install libx11-dev glm-devel libopenal-dev libogg libvorbis libpng libfreetype-dev`. `zlib` will be installed automatically because it's
   dependency of `libpng`
   - for Alpine Linux use: `sudo apk add libx11-dev glm-dev openal-soft-dev libvorbis-dev libpng-dev freetype-dev`. `libogg-dev` and `zlib-dev` will be installed automatically
   because they are dependencies of `libvorbis-dev` and `libpng-dev` respectively.
   - (distribution agnositic) download and extract latest AngelScript source package to `external/angelscript` inside of repository root.
5. Make build directory using `mkdir build`.
6. Enter build directory using `cd build`.
7. Generate build files using `cmake ..` or `cmake .. -G Ninja` if you are using Ninja build system. You may want to add additional arguments to CMake, see *`CMakeLists.txt` documentation* section.
8. Build project using `make -j$(nproc)` or `ninja` depending on your selected build system.

### Windows with MSYS2
1. Install all the required libs through pacman. (see MSYS2 dependencies below)
2. Download and extract latest AngelScript source package to `external/angelscript` inside of repository root.
3. Clone master na go into the root dir.
4. Open MSYS2 MinGW64 terminal.
5. cmake . -G"MSYS Makefiles" -DBUILD_CLIENT=ON -DBUILD_SERVER=ON -DRENDERER_GL=ON 
6. make -j[how many processors]
7. Make sure to add `C:\msys64\mingw64\bin` (or wherever your MSYS2 is installed) to PATH if you're getting missing dll errors.

### MSYS2 dependencies install commands
```
pacman -S mingw-w64-x86_64-glm
pacman -S mingw-w64-x86_64-openal
pacman -S mingw-w64-x86_64-libogg
pacman -S mingw-w64-x86_64-libvorbis
pacman -S mingw-w64-x86_64-libpng
pacman -S mingw-w64-x86_64-zlib
pacman -S mingw-w64-x86_64-freetype
```
If we forgot to mention some library then please create issue.

### Windows with vcpkg and Visual Studio
1. Install all the required libs through vcpkg. (see vcpkg dependencies below),
2. Download and extract the latest AngelScript source package to external/angelscript inside of the repository root,
3. Clone master and enter the root directory,
4. Create a folder called 'build' in the root directory,
5. Open CMakeLists.txt and add the following commands (see the commands below),
6. Open CMake(cmake-gui) and enter valid paths for the building process,
7. Enter the build folder and find a .sln file,
8. Open the solution file and select the appropriate architecture,
9. Starting compiling the Visual Studio project.

In case of any missing .dll files or unexpected errors while trying to open the .exe file:
- Make sure you have all of the required .dll files in the same folder as the .exe,
- Make sure the .dll files are the same architecture as the project was built in.

### CMakeLists.txt commands
```
SET(GLM_INCLUDE_DIR "your glm include dir")
SET(OPENAL_INCLUDE_DIR "your openAL include dir")
SET(OPENAL_LIBRARY "your openAL .lib file dir")
SET(OGG_INCLUDE_DIR "your ogg include dir (from libvorbis)")
SET(VORBIS_INCLUDE_DIR "your vorbis include dir")
SET(OGG_LIBRARY "your ogg .lib file dir")
SET(VORBIS_LIBRARY "your vorbis .lib file dir")
SET(PNG_PNG_INCLUDE_DIR "your libpng include dir")
SET(PNG_LIBRARY "your libpng .lib file dir")
SET(ZLIB_INCLUDE_DIR "your zlib.h file dir")
SET(ZLIB_LIBRARY "your zlib.lib file dir")
SET(FREETYPE_INCLUDE_DIRS "your ft2build.h file dir")
SET(FREETYPE_LIBRARIES "your freetype.lib file dir")
SET(BUILD_FLAG_SUPPRESS_WARNING_MODE ON CACHE BOOL "NO WARNINGS")
```

### Vcpkg dependencies install commands
```
vcpkg install glm
vcpkg install openal-soft
vcpkg install libvorbis
vcpkg install libpng
vcpkg install zlib
vcpkg install freetype
```
### Specifying architecture
```
vcpkg install [package-name]:[your-architecture]-windows
```

Inside of the square brackets provide the appropiarate information

### Windows with Cygwin
1. Download Cygwin installer from [here](https://www.cygwin.com) and run it.
2. Follow installation util package selection screen
3. Select following programs (these are for 64-bit Windows, however you use any other architecture):
`cmake`, `make`, `git`, `mingw64-x86_64-gcc-core`, `mingw64-x86_64-gcc-g++`
4. Select following libraries (same as above, these are for 64-bit Windows):
`mingw64-x86_64-glm`, `mingw64-x86_64-libogg`, `mingw64-x86_64-libvorbis`, `mingw64-x86_64-libpng`,
`mingw64-x86_64-openal`, `mingw64-x86_64-freetype`
5. Open Cygwin terminal and change into desired directory
6. Clone repository with following command: `git clone http://git.cubesoftware.xyz:20524/PolyGun/PolyGun.git`. You can add `--depth=1` argument
to reduce used disk space via not downloading entire git history.
7. Download and extract latest AngelScript source package to `external/angelscript` inside of repository root.
8. Enter directory with downloaded PolyGun repository using `cd PolyGun`.
9. Make build directory using `mkdir build`.
10. Enter build directory using `cd build`.
11. Generate build files using `cmake ..`. You may want to add additional arguments to CMake, see *`CMakeLists.txt` documentation* section.
12. Build project using `make -j$(nproc)`.

### SerenityOS
This documentation assumes full build (client and server).
1. Install following ports: `gcc` (alternatively `clang`), `cmake`, `glm`, `libopenal`, `libvorbis`, `libpng`, `freetype`, `git`.
2. Clone repository with following command: `git clone http://git.cubesoftware.xyz:20524/PolyGun/PolyGun.git`. You can add `--depth=1` argument
to reduce used disk space via not downloading entire git history.
3. Enter directory with downloaded PolyGun repository using `cd PolyGun`.
4. Download and extract latest AngelScript source package to `external/angelscript` inside of repository root.
5. Make build directory using `mkdir build`.
6. Enter build directory using `cd build`.
7. Generate build files using `cmake ..`. You may want to add additional arguments to CMake, see *`CMakeLists.txt` documentation* section.
8. Build project using `make -j$(nproc)`.
