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