diff --git a/.gitignore b/.gitignore index 6a8bc10..aa4e436 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ -.vscode -build \ No newline at end of file +.cache +.vscode/* +!.vscode/tasks.json +!.vscode/launch.json +bin +obj +compile_commands.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..43342b3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,23 @@ +{ + "version": "2.0.0", + "configurations": [ + { + "name": "(lldb) C Launch", + "type": "lldb", + "request": "launch", + "program": "", + "linux": { + "program": "${workspaceFolder}/bin/linux-x86_64/breakout_clone", + "cwd": "${workspaceFolder}/bin/linux-x86_64", + "preLaunchTask": "build linux-x86_64", + }, + "windows": { + "program": "${workspaceFolder}/bin/win-x86_64/breakout_clone", + "cwd": "${workspaceFolder}/bin/win-x86_64", + "preLaunchTask": "build win-x86_64", + }, + "args": [], + "cwd": "${workspaceFolder}", + } + ] +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..e46ad28 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,53 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "shell", + "label": "build linux-x86_64", + "command": "make", + "args": [ + "linux-x86_64" + ], + "options": { + "cwd": "${workspaceFolder}" + }, + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "builds the program for linux x86_64" + }, + { + "type": "shell", + "label": "build win-x86_64", + "command": "make", + "args": [ + "win-x86_64" + ], + "options": { + "cwd": "${workspaceFolder}" + }, + "group": { + "kind": "build", + "isDefault": false + }, + "detail": "builds the program for windows x86_64" + }, + { + "type": "shell", + "label": "build web", + "command": "make", + "args": [ + "web" + ], + "options": { + "cwd": "${workspaceFolder}" + }, + "group": { + "kind": "build", + "isDefault": false + }, + "detail": "builds the program for web using emscripten" + } + ] +} diff --git a/build.sh b/build.sh deleted file mode 100755 index 756d52f..0000000 --- a/build.sh +++ /dev/null @@ -1,71 +0,0 @@ -#!/bin/bash - -# define variables -PROJECT_NAME=${PWD##*/} -BUILD_DIR="./build" -INCLUDE_IN_BUILD="./assets" - -# compilation targets -args_linux86_64() -{ - ARCHITECTURE="linux-86_64" - COMPILER="/bin/gcc" - ARGS="-lSDL2" - FILE_EXSTENSION="" -} - -args_win86-64() -{ - ARCHITECTURE="win-86_64" - COMPILER="/usr/bin/x86_64-w64-mingw32-gcc" - ARGS="-lmingw32 -lSDL2main -lSDL2 -mwindows" - INCLUDE_IN_BUILD="$INCLUDE_IN_BUILD /usr/x86_64-w64-mingw32/bin/SDL2.dll" - FILE_EXSTENSION=".exe" -} - -args_emscripten() -{ - ARCHITECTURE="web" - COMPILER="emcc" # just make sure it's in $PATH (it's a pain to install) - ARGS="-s USE_SDL=2" - FILE_EXSTENSION=".html" -} - -# handle arguments -if [ "$1" = "linux86_64" ]; then args_linux86_64 -elif [ "$1" = "win86_64" ]; then args_win86-64 -elif [ "$1" = "web" ]; then args_emscripten -else echo -e "\033[91mdidn't include any arguments! D:\033[0m" && exit 1 -fi - -# check whether $BUILD_DIR or $ARCHITECTURE isn't set -if [[ -z $BUILD_DIR ]] || [[ -z "$ARCHITECTURE" ]]; then - echo -e "\033[91mBUILD_DIR or ARCHITECTURE not set D:\033[0m" - exit 1 -fi - - -# make (and clear) the build directory -mkdir -p "$BUILD_DIR/$ARCHITECTURE" -rm -rf "${BUILD_DIR:?}/$ARCHITECTURE/*" - -# copy included files or directories to the build directory -if [[ -n $INCLUDE_IN_BUILD ]]; then - cp -r "$INCLUDE_IN_BUILD" "$BUILD_DIR/$ARCHITECTURE" -fi - -# get the executable path -EXE_PATH=$BUILD_DIR/$ARCHITECTURE/$PROJECT_NAME$FILE_EXSTENSION -echo "building at: $EXE_PATH" - -# check whether the compiler can actually be executed -if [ ! -x "$COMPILER" ] && ! command -v "$COMPILER" > /dev/null; then - echo -e "\033[91mCouldn't find an executable at path: \033[0m $COMPILER" - exit 1 -fi - -# compile the code -COMMAND="$COMPILER $(find ./src -name "*.c") -o $EXE_PATH -Wall -g -lm $ARGS" -echo "using command: $COMMAND" -$COMMAND -exit $? diff --git a/makefile b/makefile new file mode 100644 index 0000000..0c298e0 --- /dev/null +++ b/makefile @@ -0,0 +1,68 @@ +NAME = breakout_clone + +# compiler settings +CC := clang +STD := c17 +LANG = c +CFLAGS := $(shell pkg-config --cflags sdl2) -Wall -g -pedantic +LDFLAGS := $(shell pkg-config --libs sdl2) -lm + +# dirs +DIR_BIN := bin +DIR_OBJ := obj +DIR := $(DIR_BIN)/$(ARCH) $(DIR_OBJ)/$(ARCH) + +# source files +SRC := $(wildcard src/*.c) $(wildcard src/**/*.c) $(wildcard src/**/**/*.c) $(wildcard src/**/**/**/*.c) $(wildcard src/**/**/**/**/*.c) +SRC_ASSETS := $(wildcard assets/*) + +# output locations +OBJ := $(patsubst src/%,$(DIR_OBJ)/$(ARCH)/%,$(SRC:.c=.o)) +DEP := $(OBJ:.o=.d) +ASSETS := $(patsubst assets/%,$(DIR_BIN)/$(ARCH)/%,$(SRC_ASSETS)) +TARGET := $(DIR_BIN)/$(ARCH)/$(NAME)$(EXT) + + +# sets the variables for the different targets +linux-x86_64: + $(MAKE) build ARCH=linux-x86_64 CFLAGS="$(CFLAGS) -target x86_64-pc-linux-gnu" +win-x86_64: + $(MAKE) build ARCH=win-x86-64 CFLAGS="$(CFLAGS) -target x86_64-pc-windows-gnu" EXT=".exe" +web: + $(MAKE) build ARCH=web CC=emcc EXT=".html" + +all: linux-x86_64 win-x86_64 web +build: $(DIR) $(TARGET) $(ASSETS) compile_commands.json +clean: + rm -rf $(DIR_BIN) $(DIR_OBJ) + +# create the binary +$(TARGET): $(OBJ) + $(CC) -o $(TARGET) $^ $(CFLAGS) $(LDFLAGS) + +# create .o and .d files +$(DIR_OBJ)/$(ARCH)/%.o: src/%.c + @mkdir -p $(dir $@) + $(CC) -o $@ -MD -MP -c $< $(CFLAGS) -std=$(STD) -x $(LANG) -Wno-unused-command-line-argument + +# copy assets +$(DIR_BIN)/$(ARCH)/%: assets/% + @mkdir -p $(dir $@) + cp $< $@ + +# create directories +$(DIR): + @mkdir -p $@ + +# update compile commands if the makefile has been updated (for linting) +ifeq ($(NO_CMDS),1) +compile_commands.json: makefile +else +compile_commands.json: makefile + @touch compile_commands.json + $(MAKE) clean + bear -- make +endif + +# include the dependencies +-include $(DEP) diff --git a/src/main.c b/src/main.c index 59d8280..ea0e337 100644 --- a/src/main.c +++ b/src/main.c @@ -41,7 +41,7 @@ static void init(void) { // initialize audio level->audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096); - level->bounce_sfx = audio_load_wav(level->audio_device, "./assets/bounce.wav"); + level->bounce_sfx = audio_load_wav(level->audio_device, "./bounce.wav"); } // handles game application updating