Skip to content

Commit

Permalink
Add README and make dice setup script universal to Unix systems
Browse files Browse the repository at this point in the history
  • Loading branch information
yutotakano committed Aug 11, 2024
1 parent d0232cd commit ac4b671
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 26 deletions.
66 changes: 61 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
On Linux/OSX, SDL2 is found in the system package manager.
On Linux, run: apt-get install libsdl2-dev
On OSX, run: brew install sdl2
# Haskell Game Jam Template

For Windows, SDL2 development libraries are provided within the repository.
Run `win_setup.bat` to create a file that specifies where to find these libraries.
This is a template repository for a game written in Haskell!

Comments are sprinkled wherever possible to make it easier to understand.

The "core" (where the update/draw loop is) is in `Game.hs`, check out the `updateState` and `drawState` functions!

## Using this template

This project has been tested on GHC 8.8.4, 8.10.7, and 9.4.8, on Windows 11, macOS Sonoma (Arm), and DICE Ubuntu.

This project uses SDL2 as the cross-platform window/graphics abstraction library.
So to build this project correctly, we'll need to install SDL2 so Haskell can find it.
You can do this either system-wide or local to this project.

- System-wide SDL2 installation
- On DICE:
- This is unsupported.
- On Windows:
- This is unsupported.
- On Linux (Ubuntu):
- Run `apt-get install libsdl2-dev`
- On Linux (other):
- Find the SDL2 development package from your system package manager.
- On MacOS:
- Run `brew install sdl2` assuming you have Homebrew. If you do not, follow the local installation.
- Local SDL2 installation
- On DICE/MacOS/Ubuntu/other:
- Run `./unix_setup.sh`, which relies only on `tar`, `curl`, `make` and standard C compilation tools.
- On Windows:
- Run `.\win_setup.sh`.

After SDL2 is setup, run the following to build the project and run it:

```
cabal run
```

### Understanding

The project template starts a fixed-FPS game loop (at 60FPS), which handles the movement of a 2D sprite.

Feel free to expand this in any direction you want! (Even 3D is possible if you wrangle with it enough)

### Adding dependencies

To add dependencies to the project, edit the `<project-name>.cabal` file's `build-depends` clause.

### Packaging

Packaging the game can be a little complex if you're on Linux or Mac, since the game binary might contain an absolute path reference to the SDL libraries, which will be broken if you move it to another machine. Please investigate "SDL game distribution" on your own :(

For Windows:
- Run `cabal list-bin Game` to find where the built binary is
- Copy the SDL2.dll file, your game binary, and whatever other assets/resources you need, into a new folder
- Test by double-clicking the game binary to see that it runs.
- ZIP that folder up for distribution.

### Stack

This project does not support using Stack. You can try, but if you already have Cabal installed, it's much quicker to use that.
14 changes: 7 additions & 7 deletions dice_setup.sh → unix_setup.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/sh

# Don't run if the sdl2_dice folder or cabal.project.local already exists
if [ -d "sdl2_dice" ] || [ -f "cabal.project.local" ]; then
echo "sdl2_dice folder or cabal.project.local already exists."
# Don't run if the sdl2_local folder or cabal.project.local already exists
if [ -d "sdl2_local" ] || [ -f "cabal.project.local" ]; then
echo "sdl2_local folder or cabal.project.local already exists."
echo "Please remove them before running this script."
exit 1
fi
Expand All @@ -26,7 +26,7 @@ mkdir build
cd build || (printf "\033[31mError: SDL2-2.30.6/build folder not found.\033[0m\n" && exit 1)

printf "\033[32minfo \033[0m Configuring SDL2 to prepare build...\n"
if ! output=$(../configure --prefix="$(pwd)/../../sdl2_dice" 2>&1); then
if ! output=$(../configure --prefix="$(pwd)/../../sdl2_local" 2>&1); then
printf "\033[31mError configuring SDL2. Check sdl_build_log.txt for more information.\033[0m\n"
echo "$output" >> sdl_build_log.txt
exit 1
Expand All @@ -37,7 +37,7 @@ if ! output=$(make 2>&1); then
echo "$output" >> sdl_build_log.txt
exit 1
fi
printf "\033[32minfo \033[0m Copying built libraries to new sdl2_dice folder...\n"
printf "\033[32minfo \033[0m Copying built libraries to new sdl2_local folder...\n"
if ! output=$(make install 2>&1); then
printf "\033[31mError copying SDL2. Check sdl_build_log.txt for more information.\033[0m\n"
echo "$output" >> sdl_build_log.txt
Expand All @@ -55,8 +55,8 @@ printf "\033[32minfo \033[0m Creating cabal.project.local file...\n"
cat <<EOF >> cabal.project.local
package sdl2
flags: -pkgconfig
extra-lib-dirs: $(pwd)/sdl2_dice/lib
extra-include-dirs: $(pwd)/sdl2_dice/include/SDL2
extra-lib-dirs: $(pwd)/sdl2_local/lib
extra-include-dirs: $(pwd)/sdl2_local/include/SDL2
EOF

printf "\033[32mSetup complete! Try running '\033[107m\033[30mcabal run\033[0m\033[32m' to build the project.\033[0m\n"
Expand Down
28 changes: 14 additions & 14 deletions win_setup.bat
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
@echo off

rem Don't run if the sdl2_win_mingw folder or cabal.project.local already exists
if exist sdl2_win_mingw (
echo [31mError: The 'sdl2_win_mingw' folder already exists.[0m
echo If you want to re-run the setup, delete the 'sdl2_win_mingw' folder and 'cabal.project.local' file and try again.
rem Don't run if the sdl2_local_win folder or cabal.project.local already exists
if exist sdl2_local_win (
echo [31mError: The 'sdl2_local_win' folder already exists.[0m
echo If you want to re-run the setup, delete the 'sdl2_local_win' folder and 'cabal.project.local' file and try again.
exit /b
)
if exist cabal.project.local (
echo Error: The 'cabal.project.local' file already exists.
echo If you want to re-run the setup, delete the 'sdl2_win_mingw' folder and 'cabal.project.local' file and try again.
echo If you want to re-run the setup, delete the 'sdl2_local_win' folder and 'cabal.project.local' file and try again.
exit /b
)

Expand All @@ -26,13 +26,13 @@ rem Remove the zip file
echo.info  Removing downloaded SDL2-devel-2.30.6-mingw.zip file...
del SDL2-devel-2.30.6-mingw.zip

rem Copy the bin, include and lib folders to the sdl2_win_mingw folder
echo.[32minfo [0m Making new sdl2_win_mingw folder...
mkdir sdl2_win_mingw
echo.[32minfo [0m Copying SDL2 libraries to new sdl2_win_mingw folder...
xcopy /E /Y SDL2-devel-2.30.6-mingw\SDL2-2.30.6\x86_64-w64-mingw32\bin sdl2_win_mingw\bin\ > nul
xcopy /E /Y SDL2-devel-2.30.6-mingw\SDL2-2.30.6\x86_64-w64-mingw32\include sdl2_win_mingw\include\ > nul
xcopy /E /Y SDL2-devel-2.30.6-mingw\SDL2-2.30.6\x86_64-w64-mingw32\lib sdl2_win_mingw\lib\ > nul
rem Copy the bin, include and lib folders to the sdl2_local_win folder
echo.[32minfo [0m Making new sdl2_local_win folder...
mkdir sdl2_local_win
echo.[32minfo [0m Copying SDL2 libraries to new sdl2_local_win folder...
xcopy /E /Y SDL2-devel-2.30.6-mingw\SDL2-2.30.6\x86_64-w64-mingw32\bin sdl2_local_win\bin\ > nul
xcopy /E /Y SDL2-devel-2.30.6-mingw\SDL2-2.30.6\x86_64-w64-mingw32\include sdl2_local_win\include\ > nul
xcopy /E /Y SDL2-devel-2.30.6-mingw\SDL2-2.30.6\x86_64-w64-mingw32\lib sdl2_local_win\lib\ > nul

rem Remove the extracted SDL2 folder
echo.info  Removing extracted SDL2-devel-2.30.6-mingw folder...
Expand All @@ -45,8 +45,8 @@ echo.package sdl2
echo. -- Disable pkg-config, as we instead supply the path to the SDL2 libraries manually
echo. flags: -pkgconfig
echo. -- Supply the path to the local SDL2 libraries (must be absolute path^)
echo. extra-lib-dirs: "%cd:\=\\%\\sdl2_win_mingw\\lib"
echo. extra-include-dirs: "%cd:\=\\%\\sdl2_win_mingw\\include\\SDL2"
echo. extra-lib-dirs: "%cd:\=\\%\\sdl2_local_win\\lib"
echo. extra-include-dirs: "%cd:\=\\%\\sdl2_local_win\\include\\SDL2"
echo.package text
echo. -- Disable use of SIMD, which makes text fails to build on Win + GHC 8.8.4
echo. flags: -simdutf
Expand Down

0 comments on commit ac4b671

Please sign in to comment.