Skip to content

Instantly share code, notes, and snippets.

@blakelead
Last active January 14, 2023 09:34
Show Gist options
  • Save blakelead/7fc3efb62091bde0b0ea64c1fa9cba3e to your computer and use it in GitHub Desktop.
Save blakelead/7fc3efb62091bde0b0ea64c1fa9cba3e to your computer and use it in GitHub Desktop.
Create an empty Raylib and Box2D C++ project for MacOS
#! /bin/bash
# Enable xtrace if the DEBUG environment variable is set
if [[ ${DEBUG-} =~ ^1|yes|true$ ]]; then
set -o xtrace
fi
set -o errexit
APP_NAME=""
while getopts ":n:" opt; do
case $opt in
n) APP_NAME="$OPTARG"
;;
esac
done
if [ -z "${APP_NAME}" ]; then
echo "Usage: $0 -n project_name"
exit 1
fi
WORKDIR=$(pwd)/${APP_NAME}
RAYLIB_URL=https://github.com/raysan5/raylib
RAYLIB_VERSION=4.2.0
RAYLIB_CPP_URL=https://github.com/RobLoach/raylib-cpp.git
RAYLIB_CPP_VERSION=v4.2.7
BOX2D_URL=https://github.com/erincatto/box2d.git
BOX2D_VERSION=v2.4.1
echo "Create directory structure"
mkdir -p ${WORKDIR}/tmp
mkdir -p ${WORKDIR}/src
mkdir -p ${WORKDIR}/include
mkdir -p ${WORKDIR}/lib/macOS
mkdir -p ${WORKDIR}/lib/raylib
mkdir -p ${WORKDIR}/lib/box2d
mkdir -p ${WORKDIR}/docs
echo "Setup raylib"
git clone -b ${RAYLIB_VERSION} ${RAYLIB_URL} ${WORKDIR}/tmp/raylib >/dev/null 2>&1
make -C ${WORKDIR}/tmp/raylib/src >/dev/null 2>&1
cp ${WORKDIR}/tmp/raylib/src/libraylib.a ${WORKDIR}/lib/macOS
cp ${WORKDIR}/tmp/raylib/src/raylib.h ${WORKDIR}/lib/raylib
cp ${WORKDIR}/tmp/raylib/src/raymath.h ${WORKDIR}/lib/raylib
git clone -b ${RAYLIB_CPP_VERSION} ${RAYLIB_CPP_URL} ${WORKDIR}/tmp/raylib-cpp >/dev/null 2>&1
cp ${WORKDIR}/tmp/raylib-cpp/include/*.hpp ${WORKDIR}/lib/raylib
echo "Setup box2d"
git clone -b ${BOX2D_VERSION} ${BOX2D_URL} ${WORKDIR}/tmp/box2d >/dev/null 2>&1
cd ${WORKDIR}/tmp/box2d && ./build.sh >/dev/null 2>&1
cp ${WORKDIR}/tmp/box2d/build/bin/libbox2d.a ${WORKDIR}/lib/macOS
cp ${WORKDIR}/tmp/box2d/include/box2d/*.h ${WORKDIR}/lib/box2d
echo "Create default main file"
cat <<EOF >>${WORKDIR}/src/Main.cpp
#include <raylib-cpp.hpp>
int main() {
raylib::Color textColor = raylib::Color::LightGray();
raylib::Window window(800, 600, "${APP_NAME}");
while (!window.ShouldClose()) {
window.BeginDrawing();
window.ClearBackground(raylib::Color::RayWhite());
textColor.DrawText("Hello, world!", 350, 290, 20);
window.EndDrawing();
}
return 0;
}
EOF
echo "Create Makefile"
cat <<'EOF' >>${WORKDIR}/Makefile
# Define custom functions
rwildcard = $(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
# Set macros
buildDir := bin
executable := app
target := $(buildDir)/$(executable)
sources := $(call rwildcard,src/,*.cpp)
objects := $(patsubst src/%, $(buildDir)/%, $(patsubst %.cpp, %.o, $(sources)))
depends := $(patsubst %.o, %.d, $(objects))
compileFlags := -std=c++20 -I include -isystem lib/raylib -isystem lib/box2d -g -Wall -Werror
linkFlags = -L lib/macOS -l raylib -l Box2D
linkFlags += -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL
CXX = clang++
# Lists phony targets for Makefile
.PHONY: clean build execute
# Default target, cleans, compiles and executes
all: clean build execute
# Link the program and create the executable
$(target): $(objects)
@$(CXX) $(objects) -o $(target) $(linkFlags)
# Add all rules from dependency files
-include $(depends)
# Compile objects to the build directory
$(buildDir)/%.o: src/%.cpp Makefile
@mkdir -p $(buildDir)
@$(CXX) -MMD -MP -c $(compileFlags) $< -o $@
# Run the executable
build: $(target)
# Run the executable
execute: $(target)
@$(target)
# Clean up all relevant files
clean:
@rm -rf $(buildDir)
EOF
echo "Init git project"
cd ${WORKDIR}
git init >/dev/null 2>&1
cat <<'EOF' >>${WORKDIR}/.gitignore
bin
tmp
EOF
echo "Init doc"
cat <<EOF >>${WORKDIR}/README.md
# ${APP_NAME}
EOF
echo "Cleanup"
rm -rf ${WORKDIR}/tmp
echo "Build the game"
make -C ${WORKDIR} all
@blakelead
Copy link
Author

blakelead commented Jan 9, 2023

Usage:

chmod +x new-raylib-cpp-project.sh
./new-raylib-cpp-project.sh -n my-new-project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment