split engine from game
[vg.git] / vg_compiler.sh
diff --git a/vg_compiler.sh b/vg_compiler.sh
new file mode 100755 (executable)
index 0000000..7a78e8f
--- /dev/null
@@ -0,0 +1,219 @@
+#!/bin/bash
+# Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
+
+vg_root=`readlink $0`
+vg_root=`dirname $vg_root`
+
+#echo "VG Root: $vg_root"
+#echo "Working dir: `pwd`"
+
+cmd_lib_dir="-I. -L$vg_root/lib -L./ -I$vg_root"
+cmd_release=false
+cmd_defines=""
+cmd_target="fishladder"
+
+# Util
+# ===========================================
+error(){
+    echo -e "\033[1;31mError:\e[0m $@"
+    exit 1
+}
+
+warning(){
+    echo -e "\033[1;33mWarning:\e[0m $@"
+}
+
+success(){
+   echo -e "\033[1;32mSuccess:\e[0m $@"
+}
+
+logit(){
+   echo -e "\033[0;37m$@\e[0m"
+}
+
+# Platforms
+# ===========================================
+
+target_os_windows(){
+   target_ext=".exe"
+   target_compiler="i686-w64-mingw32-gcc"
+       target_libs="-lglfw3 -lopengl32 -lm -mwindows"
+   target_dir="build.win32"
+       target_steam_api="steam_api.dll"
+   if [ $cmd_release = true ]; then
+      target_opts="-O3"
+   else
+      target_opts="-ggdb3"
+   fi
+}
+
+target_os_linux(){
+   target_ext=""
+   target_compiler="gcc"
+   target_libs="-lGL -lglfw -lX11 -lXxf86vm -lXrandr -lm -lpthread -lXi -ldl"
+   target_dir="build.linux"
+   target_steam_api="libsteam_api.so"
+   if [ $cmd_release = true ]; then
+      target_opts="-O3"
+   else
+      target_opts="-fsanitize=address -ggdb3"
+   fi
+}
+
+compile_x(){
+   cmd_setup="$target_compiler -Wall -Wstrict-aliasing=3 -Wno-unused-function $cmd_lib_dir"
+   cmd_targets="$1 ${vg_root}/gl/glad.c -o $2$target_ext"
+   cmd_final="$target_libs -Wl,-rpath=./ $cmd_defines"
+   cmd="$cmd_setup $target_opts $cmd_targets $cmd_final"
+
+   echo "Compile -> $2$target_ext:"
+   logit "    $cmd"
+   $cmd
+   
+       if [ $? -ne 0 ]; then
+               error "compiler signaled fail"
+               exit 1
+       fi
+
+       success "$2$target_ext built"
+   echo ""
+}
+
+compile_main(){
+   echo ""
+   echo "compile_main()"
+   mkdir -p $target_dir
+
+   compile_x fishladder.c $target_dir/$cmd_target
+
+   echo "Setting up build structure"
+
+   # Setup build folder
+   mkdir $target_dir/cfg -p
+   mkdir $target_dir/textures -p
+   mkdir $target_dir/sound -p
+   mkdir $target_dir/maps -p
+   mkdir $target_dir/sav -p
+
+   # Copy libraries
+   cp $vg_root/steam/$target_steam_api $target_dir/$target_steam_api
+
+   # Clear and copy assets
+   rm -r $target_dir/textures
+   rm -r $target_dir/sound
+   rm -r $target_dir/maps
+
+   cp -r .temp_textures $target_dir
+   mv $target_dir/.temp_textures $target_dir/textures
+   cp -r sound $target_dir
+   cp -r maps $target_dir
+
+   success "Build made: $target_dir"
+}
+
+compile_tools(){
+   # These should only be compiled for native platform
+   echo ""
+   echo "compile_tools()"
+       mkdir tools -p
+   
+   compile_x $vg_root/fontcomp.c tools/fontcomp
+   compile_x $vg_root/texsheet.c tools/texsheet
+   compile_x $vg_root/qoiconv.c tools/qoiconv
+}
+
+compile_assets(){
+   echo ""
+   echo "compile_assets()"
+   [[ -d .temp_textures ]] && rm -r .temp_textures
+   mkdir .temp_textures
+   
+   # Convert all png to qoi
+   echo "Compile textures:"
+   for f in textures/*.png;
+      do logit "  qoi: $f";
+      ./tools/qoiconv$target_ext $f .temp_textures/"$(basename "$f" .png).qoi"
+   done
+
+   # Autocombine textures
+   echo "  [combine]:"
+
+   auto_combine=""
+   cd textures_combine
+   for f in *.png;
+      do logit "    combine: $f";
+      auto_combine="$auto_combine $f"
+   done
+   ../tools/texsheet$taget_ext ../.temp_textures/autocombine.qoi ../sprites_autocombine.h sprites_auto_combine $auto_combine
+   cd ..
+
+   # Compile font file
+   echo ""
+   echo "Compile fonts:"
+   ./tools/fontcomp$target_ext $vg_root/fonts/vg_font.png $vg_root/vg/vg_pxfont.h
+}
+
+# ==============================================================
+# Compile process
+
+if [[ "$OSTYPE" != "linux-gnu"* ]]; then
+   echo "Host: Microsoft Windows (implied)"
+   host_is_linux=false
+   target_os_windows
+else
+   echo "Host: GNU/Linux"
+   host_is_linux=true
+   target_os_linux
+fi
+
+# Main compile loop
+while (( "$#" )); do
+       case $1 in
+               -r|--release) 
+         cmd_release=true
+               ;;
+               -s|--no-steam)
+                       cmd_defines="-DVG_NO_STEAM"
+               ;;
+               -t|--tools)
+         compile_tools
+               ;;
+      -a|-assets)
+         compile_assets
+      ;;
+      --build-linux)
+         target_os_linux
+         compile_main
+      ;;
+      --build-windows)
+         target_os_windows
+         compile_main
+
+         # Extra glfw.dll copy step
+         cp $vg_root/glfw/glfw3.dll $target_dir/glfw3.dll
+      ;;
+      -p|--play)
+         if [ $host_is_linux ]; then
+            target_os_linux
+         else
+            target_os_windows
+         fi
+
+         echo ""
+         logit "======= exec: $target_dir/$cmd_target$target_ext ======="
+         echo ""
+
+         cd $target_dir
+         ./$cmd_target
+         cd ./../
+      ;;
+               *) 
+                       echo "Unkown param: $1" 
+                       exit 1
+               ;;
+       esac
+       shift
+done
+
+# Cleanup
+logit "cleaning up..."