split engine from game
[vg.git] / vg_compiler.sh
1 #!/bin/bash
2 # Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
3
4 vg_root=`readlink $0`
5 vg_root=`dirname $vg_root`
6
7 #echo "VG Root: $vg_root"
8 #echo "Working dir: `pwd`"
9
10 cmd_lib_dir="-I. -L$vg_root/lib -L./ -I$vg_root"
11 cmd_release=false
12 cmd_defines=""
13 cmd_target="fishladder"
14
15 # Util
16 # ===========================================
17 error(){
18 echo -e "\033[1;31mError:\e[0m $@"
19 exit 1
20 }
21
22 warning(){
23 echo -e "\033[1;33mWarning:\e[0m $@"
24 }
25
26 success(){
27 echo -e "\033[1;32mSuccess:\e[0m $@"
28 }
29
30 logit(){
31 echo -e "\033[0;37m$@\e[0m"
32 }
33
34 # Platforms
35 # ===========================================
36
37 target_os_windows(){
38 target_ext=".exe"
39 target_compiler="i686-w64-mingw32-gcc"
40 target_libs="-lglfw3 -lopengl32 -lm -mwindows"
41 target_dir="build.win32"
42 target_steam_api="steam_api.dll"
43 if [ $cmd_release = true ]; then
44 target_opts="-O3"
45 else
46 target_opts="-ggdb3"
47 fi
48 }
49
50 target_os_linux(){
51 target_ext=""
52 target_compiler="gcc"
53 target_libs="-lGL -lglfw -lX11 -lXxf86vm -lXrandr -lm -lpthread -lXi -ldl"
54 target_dir="build.linux"
55 target_steam_api="libsteam_api.so"
56 if [ $cmd_release = true ]; then
57 target_opts="-O3"
58 else
59 target_opts="-fsanitize=address -ggdb3"
60 fi
61 }
62
63 compile_x(){
64 cmd_setup="$target_compiler -Wall -Wstrict-aliasing=3 -Wno-unused-function $cmd_lib_dir"
65 cmd_targets="$1 ${vg_root}/gl/glad.c -o $2$target_ext"
66 cmd_final="$target_libs -Wl,-rpath=./ $cmd_defines"
67 cmd="$cmd_setup $target_opts $cmd_targets $cmd_final"
68
69 echo "Compile -> $2$target_ext:"
70 logit " $cmd"
71 $cmd
72
73 if [ $? -ne 0 ]; then
74 error "compiler signaled fail"
75 exit 1
76 fi
77
78 success "$2$target_ext built"
79 echo ""
80 }
81
82 compile_main(){
83 echo ""
84 echo "compile_main()"
85 mkdir -p $target_dir
86
87 compile_x fishladder.c $target_dir/$cmd_target
88
89 echo "Setting up build structure"
90
91 # Setup build folder
92 mkdir $target_dir/cfg -p
93 mkdir $target_dir/textures -p
94 mkdir $target_dir/sound -p
95 mkdir $target_dir/maps -p
96 mkdir $target_dir/sav -p
97
98 # Copy libraries
99 cp $vg_root/steam/$target_steam_api $target_dir/$target_steam_api
100
101 # Clear and copy assets
102 rm -r $target_dir/textures
103 rm -r $target_dir/sound
104 rm -r $target_dir/maps
105
106 cp -r .temp_textures $target_dir
107 mv $target_dir/.temp_textures $target_dir/textures
108 cp -r sound $target_dir
109 cp -r maps $target_dir
110
111 success "Build made: $target_dir"
112 }
113
114 compile_tools(){
115 # These should only be compiled for native platform
116 echo ""
117 echo "compile_tools()"
118 mkdir tools -p
119
120 compile_x $vg_root/fontcomp.c tools/fontcomp
121 compile_x $vg_root/texsheet.c tools/texsheet
122 compile_x $vg_root/qoiconv.c tools/qoiconv
123 }
124
125 compile_assets(){
126 echo ""
127 echo "compile_assets()"
128 [[ -d .temp_textures ]] && rm -r .temp_textures
129 mkdir .temp_textures
130
131 # Convert all png to qoi
132 echo "Compile textures:"
133 for f in textures/*.png;
134 do logit " qoi: $f";
135 ./tools/qoiconv$target_ext $f .temp_textures/"$(basename "$f" .png).qoi"
136 done
137
138 # Autocombine textures
139 echo " [combine]:"
140
141 auto_combine=""
142 cd textures_combine
143 for f in *.png;
144 do logit " combine: $f";
145 auto_combine="$auto_combine $f"
146 done
147 ../tools/texsheet$taget_ext ../.temp_textures/autocombine.qoi ../sprites_autocombine.h sprites_auto_combine $auto_combine
148 cd ..
149
150 # Compile font file
151 echo ""
152 echo "Compile fonts:"
153 ./tools/fontcomp$target_ext $vg_root/fonts/vg_font.png $vg_root/vg/vg_pxfont.h
154 }
155
156 # ==============================================================
157 # Compile process
158
159 if [[ "$OSTYPE" != "linux-gnu"* ]]; then
160 echo "Host: Microsoft Windows (implied)"
161 host_is_linux=false
162 target_os_windows
163 else
164 echo "Host: GNU/Linux"
165 host_is_linux=true
166 target_os_linux
167 fi
168
169 # Main compile loop
170 while (( "$#" )); do
171 case $1 in
172 -r|--release)
173 cmd_release=true
174 ;;
175 -s|--no-steam)
176 cmd_defines="-DVG_NO_STEAM"
177 ;;
178 -t|--tools)
179 compile_tools
180 ;;
181 -a|-assets)
182 compile_assets
183 ;;
184 --build-linux)
185 target_os_linux
186 compile_main
187 ;;
188 --build-windows)
189 target_os_windows
190 compile_main
191
192 # Extra glfw.dll copy step
193 cp $vg_root/glfw/glfw3.dll $target_dir/glfw3.dll
194 ;;
195 -p|--play)
196 if [ $host_is_linux ]; then
197 target_os_linux
198 else
199 target_os_windows
200 fi
201
202 echo ""
203 logit "======= exec: $target_dir/$cmd_target$target_ext ======="
204 echo ""
205
206 cd $target_dir
207 ./$cmd_target
208 cd ./../
209 ;;
210 *)
211 echo "Unkown param: $1"
212 exit 1
213 ;;
214 esac
215 shift
216 done
217
218 # Cleanup
219 logit "cleaning up..."