cleaner steamworks switches
[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 vg_version="0.1"
7
8 opt_assets=false
9 opt_release=false
10 opt_play=false
11 opt_linux=false
12 opt_windows=false
13 opt_tools=false
14 opt_full=false
15 opt_gcc=""
16 opt_steam=""
17
18 # Util
19 # ===========================================
20 error(){
21 echo -e "\033[1;31mError:\e[0m $@"
22 exit 1
23 }
24
25 warning(){
26 echo -e "\033[1;33mWarning:\e[0m $@"
27 }
28
29 success(){
30 echo -e "\033[1;32mSuccess:\e[0m $@"
31 }
32
33 logit(){
34 echo -e "\033[0;37m$@\e[0m"
35 }
36
37 titleit(){
38 echo ""
39 echo -e "\033[1;35m$@\e[0m"
40 echo "=========================="
41 echo ""
42 }
43
44 if [[ -f vg.conf ]]; then
45 source vg.conf
46 else
47 error "Directory is not a VG project"
48 exit 1
49 fi
50
51 # Platforms
52 # ===========================================
53
54 target_os_windows(){
55 target_ext=".exe"
56 target_compiler="i686-w64-mingw32-gcc"
57 target_libs="-lglfw3 -lopengl32 -lm -mwindows"
58 target_dir="build.win32"
59 target_steam_api="steam_api.dll"
60 if [ $opt_release = true ]; then
61 target_opts="-O3"
62 else
63 target_opts="-ggdb3"
64 fi
65 }
66
67 target_os_linux(){
68 target_ext=""
69 target_compiler="gcc"
70 target_libs="-lGL -lglfw -lX11 -lXxf86vm -lXrandr -lm -lpthread -lXi -ldl -lsteam_api"
71 target_dir="build.linux"
72 target_steam_api="libsteam_api.so"
73 if [ $opt_release = true ]; then
74 target_opts="-O3"
75 else
76 target_opts="-fsanitize=address -ggdb3"
77 fi
78 }
79
80 compile_x(){
81 paths="-I. -L$vg_root/dep/glfw -L$vg_root/dep/steam -I$vg_root/dep -I$vg_root/src"
82 setup="$target_compiler $opt_gcc -Wall -Wstrict-aliasing=3 -Wno-unused-function $paths"
83 targets="$1 $vg_root/dep/glad/glad.c -o $2$target_ext"
84 final="$target_libs -Wl,-rpath=./ $opt_steam"
85 cmd="$setup $target_opts $targets $final"
86
87 logit " $cmd"
88 $cmd
89
90 if [ $? -ne 0 ]; then
91 error "compiler signaled fail"
92 exit 1
93 fi
94
95 success "$2$target_ext built"
96 }
97
98 compile_main(){
99 titleit "Main build: $target_dir"
100 mkdir -p $target_dir
101
102 compile_x $vg_src $target_dir/$vg_target
103 echo ""
104 logit "Setting up build structure"
105
106 # Setup build folder
107 mkdir $target_dir/cfg -p
108 mkdir $target_dir/textures -p
109 mkdir $target_dir/sound -p
110 mkdir $target_dir/maps -p
111 mkdir $target_dir/sav -p
112
113 # Copy libraries
114 if [ "$opt_steam" != "" ]; then
115 cp $vg_root/dep/steam/$target_steam_api $target_dir/$target_steam_api
116 fi
117
118 # Clear and copy assets
119 rm -r $target_dir/textures
120 rm -r $target_dir/sound
121 rm -r $target_dir/maps
122
123 cp -r .temp_textures $target_dir
124 mv $target_dir/.temp_textures $target_dir/textures
125 cp -r sound $target_dir
126 cp -r maps $target_dir
127 }
128
129 compile_tools(){
130 # These should only be compiled for native platform
131 titleit "Tools"
132
133 mkdir $vg_root/bin -p
134
135 compile_x $vg_root/src/fontcomp.c $vg_root/bin/fontcomp
136 compile_x $vg_root/src/texsheet.c $vg_root/bin/texsheet
137 compile_x $vg_root/src/qoiconv.c $vg_root/bin/qoiconv
138 }
139
140 compile_assets(){
141 titleit "Assets"
142 [[ -d .temp_textures ]] && rm -r .temp_textures
143 mkdir .temp_textures
144
145 # Convert all png to qoi
146 echo "Compile textures:"
147 for f in textures/*.png;
148 do logit " qoi: $f";
149 $vg_root/bin/qoiconv$target_ext $f .temp_textures/"$(basename "$f" .png).qoi"
150 done
151
152 # Autocombine textures
153 echo " [combine]:"
154
155 auto_combine=""
156 cd textures_combine
157 for f in *.png;
158 do logit " combine: $f";
159 auto_combine="$auto_combine $f"
160 done
161 $vg_root/bin/texsheet$taget_ext ../.temp_textures/autocombine.qoi ../sprites_autocombine.h sprites_auto_combine $auto_combine
162 cd ..
163
164 # Compile font file
165 echo ""
166 echo "Compile fonts:"
167 $vg_root/bin/fontcomp$target_ext $vg_root/src/fonts/vg_font.png $vg_root/src/vg/vg_pxfont.h
168 }
169
170 # ==============================================================
171 # Compile process
172
173 if [[ "$OSTYPE" != "linux-gnu"* ]]; then
174 host_os=win32
175 target_os_windows
176 else
177 host_os=linux
178 target_os_linux
179 fi
180
181 options=rptlwaq
182 longopts=release,build-linux,build-windows,steam,play,build-tools,assets,full
183 parsed=$(getopt --options=$options --longoptions=$longopts --name "vgc" -- "$@")
184
185 if [ $? -ne 0 ]; then
186 error "getopt failed"
187 exit 1
188 fi
189
190 eval set -- "$parsed"
191 while true; do
192 case "$1" in
193 -q)
194 opt_gcc=""
195 shift;
196 ;;
197 -a|--assets)
198 opt_assets=true
199 shift;
200 ;;
201 -r|--release)
202 opt_release=true
203 shift;
204 ;;
205 -p|--play)
206 opt_play=true
207 shift;
208 ;;
209 -l|--build-linux)
210 opt_linux=true
211 shift;
212 ;;
213 -w|--build-windows)
214 opt_windows=true
215 shift;
216 ;;
217 -t|--build-tools)
218 opt_tools=true
219 shift;
220 ;;
221 --full)
222 opt_full=true
223 opt_assets=true
224 opt_release=true
225 opt_play=true
226 opt_linux=true
227 opt_windows=true
228 opt_tools=true
229 shift;
230 ;;
231 --steam)
232 opt_steam="-DVG_STEAM"
233 shift;
234 ;;
235 --)
236 shift
237 break
238 ;;
239 *)
240 error "programming error"
241 break
242 ;;
243 esac
244 done
245
246 titleit " vgc ver: $vg_version\n host: $host_os"
247 logit " assets: $opt_assets"
248 logit " release: $opt_release"
249 logit " play: $opt_play"
250 logit " build-linux: $opt_linux"
251 logit "build-windows: $opt_windows"
252 logit " build-tools: $opt_tools"
253 logit " steam: $opt_steam"
254
255 # Main build steps
256
257 if [ $opt_tools = true ]; then compile_tools; fi
258 if [ $opt_assets = true ]; then compile_assets; fi
259 if [ $opt_linux = true ]; then target_os_linux; compile_main; fi
260 if [ $opt_windows = true ]; then
261 target_os_windows
262 compile_main
263
264 cp $vg_root/dep/glfw/glfw3.dll $target_dir/glfw3.dll
265 fi
266
267 if [ $opt_play = true ]; then
268
269 if [ $host_os = linux ]; then
270 target_os_linux
271 else
272 target_os_windows
273 fi
274
275 echo ""
276 logit "======= exec: $target_dir/$vg_target$target_ext ======="
277 echo ""
278
279 cd $target_dir
280 ./$vg_target$target_ext
281 cd ./../
282
283 fi