forked from simple2d/simple2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple2d.sh
executable file
·1312 lines (1056 loc) · 31.8 KB
/
simple2d.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# The Simple 2D Command-Line Utility for Unix-like Systems
#
# Run from the web using:
# bash <(curl -fsSL https://script_url_here)
# or...
# bash <(wget -qO - https://script_url_here)
# ------------------------------------------------------------------------------
# Set Constants ################################################################
# The installed version
VERSION='1.1.0'
# URL to this script in the repo
SCRIPT_URL="https://raw.githubusercontent.com/simple2d/simple2d/master/bin/simple2d.sh"
# MinGW Simple 2D installer
s2d_mingw_installer_fname="simple2d-windows-mingw-${VERSION}.zip"
s2d_mingw_installer_url="https://github.com/simple2d/simple2d/releases/download/v${VERSION}/${s2d_mingw_installer_fname}"
# SDL minimum version
SDL_MIN_VERSION='2.0.4'
# SDL download paths
libsdl_url="https://www.libsdl.org"
sdl_fname="SDL2-2.0.9"
sdl_url="${libsdl_url}/release/${sdl_fname}.tar.gz"
image_fname="SDL2_image-2.0.4"
image_url="${libsdl_url}/projects/SDL_image/release/${image_fname}.tar.gz"
mixer_fname="SDL2_mixer-2.0.4"
mixer_url="${libsdl_url}/projects/SDL_mixer/release/${mixer_fname}.tar.gz"
ttf_fname="SDL2_ttf-2.0.14"
ttf_url="${libsdl_url}/projects/SDL_ttf/release/${ttf_fname}.tar.gz"
# SDL config
sdl_arm_config_flags="\
--disable-video-opengl --disable-video-x11 \
--disable-pulseaudio --disable-esd \
--disable-video-mir --disable-video-wayland"
# Colors
BOLD='\033[1;39m' # default, bold
UNDERLINE='\033[4m' # underline
TASK='\033[1;34m' # blue, bold
BLUE=$TASK
INFO='\033[1;36m' # cyan, underline
WARN='\033[1;33m' # yellow, underline
ERROR='\033[1;31m' # red, underline
SUCCESS='\033[1;32m' # green, bold
NORMAL='\033[0m' # reset
# Set Variables ################################################################
platform='unknown'
platform_display='unknown'
platform_rpi=false
ret='' # for storing function return values
confirmed=false # to skip confirmation prompts
# Use xcpretty for nicer output: gem install xcpretty
if xcpretty -v &>/dev/null; then
XCPRETTY=xcpretty
else
XCPRETTY=cat
fi
# Helper Functions #############################################################
# Prints an information messages
# params:
# $1 String Message text
info_msg() {
echo -e "${INFO}Info:${NORMAL} $1\n"
}
warning_msg() {
echo -e "${WARN}Warning:${NORMAL} $1\n"
}
error_msg() {
echo -e "${ERROR}Error:${NORMAL} $1\n"
}
success_msg() {
echo -e "${SUCCESS}$1${NORMAL}\n"
}
# Prompts to continue, or exits
# params:
# $1 String Message before y/n prompt
prompt_to_continue() {
if ! $confirmed; then
printf "${BOLD}$1${NORMAL} "
read -p "(y/n) " ans
if [[ $ans != "y" ]]; then
echo -e "\nQuitting...\n"
exit
fi
echo
fi
}
# Prints the task in progress
# params:
# $1 String Name of task
# $2 String Optional white space, e.g. "\n\n"
print_task() {
printf "${TASK}==>${BOLD} $1${NORMAL}$2"
}
# Prints and runs a command
# params:
# $1 String The command
print_and_run() {
echo "==> $1"; $1
}
# Checks if a library is installed
# params:
# $1 String Name of the library, e.g. SDL2, without the "-l"
have_lib?() {
print_task "Checking for $1... "
if $(ld -o /dev/null -l$1 2>&1 | grep -qE '(library not found|ld: cannot find -l)'); then
echo "no"
return 1
else
echo "yes"
return 0
fi
}
# Gets the text from a remote resource
# params:
# $1 String URL of the resource
get_remote_str() {
which curl &>/dev/null && cmd='curl -fsSL' || cmd='wget -qO -';
ret=$($cmd $1)
}
# Compares version numbers in the form `#.#.#`
# Adapted from:
# stackoverflow.com/questions/4023830/bash-how-compare-two-strings-in-version-format
# params:
# $1 String First version number to compare
# $2 String Second version number to compare
# returns:
# 0 If $1 is equal to $2
# 1 If $1 is newer than $2
# 2 If $1 is older than $2
compare_versions() {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
# Build #######################################################################
# Builds a Simple 2D program
# If directory contains an Xcode project, it will call `xcodebuild`.
# If $1 is a C or C++ source file, it will attempt to compile using `gcc`.
build() {
# If no input, print build usage
if [[ $1 == '' ]]; then print_usage_build; exit; fi
# If C or C++ source file given, e.g.:
# build app.c; build app.cpp
if [[ ${1: -2} == '.c' || ${1: -4} == '.cpp' ]]; then
# Compile
gcc -std=c11 $1 `simple2d --libs` -o ${1%.*}
exit
fi
# Check if current directory has an Xcode project
has_xcodeproj?() {
if [[ $(ls *.xcodeproj 2>/dev/null) ]]; then
return 0
else
return 1
fi
}
# Build an Xcode project
build_with_xcodebuild() {
if [[ $XCPRETTY != 'xcpretty' ]]; then
echo -e "xcpretty not found: Run \`gem install xcpretty\` for nicer xcodebuild output.\n"
fi
case $1 in
--ios)
xcodebuild -sdk iphonesimulator | $XCPRETTY; exit;;
--ios-device)
xcodebuild -sdk iphoneos | $XCPRETTY; exit;;
--tvos)
xcodebuild -sdk appletvsimulator | $XCPRETTY; exit;;
--tvos-device)
xcodebuild -sdk appletvos | $XCPRETTY; exit;;
esac
}
# If bad SDK flag, print build usage
if ! [[ $1 =~ ^(--ios|--ios-device|--tvos|--tvos-device)$ ]]; then
print_usage_build; exit 1
fi
# If current directory has an Xcode project, e.g.:
# build --[ios|tvos][-device]
if has_xcodeproj?; then
build_with_xcodebuild $1
fi
# If an Xcode project path and options are provided, e.g.:
# build --ios[-device] build/ios
# build --tvos[-device] build/tvos
# build --ios[-device] build/ios/MyApp.xcodeproj
# build --tvos[-device] build/tvos/MyApp.xcodeproj
if [[ ${2: -10} == '.xcodeproj' ]]; then
cd $(dirname $2)
elif [[ -d $2 ]]; then
cd $2
else
error_msg "\"$2\" is not a directory or Xcode project"; exit 1
fi
if ! has_xcodeproj?; then
error_msg "The directory does not contain an Xcode project"; exit 1
fi
build_with_xcodebuild $1
}
# Installation #################################################################
# Checks installed versions of SDL
check_sdl_versions() {
print_task "Checking SDL2 version... "
# Check SDL version installed
SDL_INSTALLED_VERSION=$(sdl2-config --version)
compare_versions $SDL_MIN_VERSION $SDL_INSTALLED_VERSION
if [[ $? == 1 ]]; then
echo $SDL_INSTALLED_VERSION
error_msg "Installed version of SDL2 is too old (must be at least $SDL_MIN_VERSION)"
return 1
else
echo "$SDL_INSTALLED_VERSION"
return 0
fi
}
# Checks if SDL libraries are installed
have_sdl_libs?() {
have_all_libs=true
have_sdl2_lib=true
have_image_lib=true
have_mixer_lib=true
have_ttf_lib=true
if ! have_lib? 'SDL2'; then
have_sdl2_lib=false
have_all_libs=false
else
if ! check_sdl_versions; then
have_sdl2_lib=false
have_all_libs=false
fi
fi
if ! have_lib? 'SDL2_image'; then
have_image_lib=false
have_all_libs=false
fi
if ! have_lib? 'SDL2_mixer'; then
have_mixer_lib=false
have_all_libs=false
fi
if ! have_lib? 'SDL2_ttf'; then
have_ttf_lib=false
have_all_libs=false
fi
if $have_all_libs; then
return 0
else
return 1
fi
}
# Installs SDL on BSD
install_sdl_bsd() {
echo "The following packages will be installed:"
echo " sdl2"
echo " sdl2_image"
echo " sdl2_mixer"
echo " sdl2_ttf"
prompt_to_continue "Install SDL now?"
print_task "Updating packages" "\n\n"
print_and_run "sudo pkg update"
echo; print_task "Installing packages" "\n\n"
print_and_run "sudo pkg install sdl2 sdl2_image sdl2_mixer sdl2_ttf"
echo
if ! have_sdl_libs?; then
echo; error_msg "SDL libraries did not install correctly"; exit 1
else
echo; info_msg "SDL was installed successfully"
fi
}
# Installs SDL on Linux
install_sdl_linux() {
echo "The following packages will be installed:"
# Fedora and CentOS
if which yum &>/dev/null; then
echo " SDL2-devel"
echo " SDL2_image-devel"
echo " SDL2_mixer-devel"
echo " SDL2_ttf-devel"
# Arch
elif which pacman &>/dev/null; then
echo " sdl2"
echo " sdl2_image"
echo " sdl2_mixer"
echo " sdl2_ttf"
# openSUSE
elif which zypper &>/dev/null; then
echo " libSDL2-devel"
echo " libSDL2_image-devel"
echo " libSDL2_mixer-devel"
echo " libSDL2_ttf-devel"
# Ubuntu, Debian, and Mint
# `apt` must be last because openSUSE has it aliased to `zypper`
elif which apt &>/dev/null; then
echo " libsdl2-dev"
echo " libsdl2-image-dev"
echo " libsdl2-mixer-dev"
echo " libsdl2-ttf-dev"
else
error_msg "Could not find a package manager to install SDL"; exit 1
fi
echo
prompt_to_continue "Install SDL now?"
print_task "Updating packages" "\n\n"
if which yum &>/dev/null; then
print_and_run "yum check-update"
elif which pacman &>/dev/null; then
print_and_run "sudo pacman -Syy"
elif which zypper &>/dev/null; then
print_and_run "sudo zypper refresh"
elif which apt &>/dev/null; then
print_and_run "sudo apt update"
fi
echo; print_task "Installing packages" "\n\n"
if which yum &>/dev/null; then
print_and_run "sudo yum install -y SDL2-devel SDL2_image-devel SDL2_mixer-devel SDL2_ttf-devel"
elif which pacman &>/dev/null; then
print_and_run "sudo pacman -S --noconfirm sdl2 sdl2_image sdl2_mixer sdl2_ttf"
elif which zypper &>/dev/null; then
print_and_run "sudo zypper install -y libSDL2-devel libSDL2_image-devel libSDL2_mixer-devel libSDL2_ttf-devel"
elif which apt &>/dev/null; then
print_and_run "sudo apt install -y libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev"
fi
echo
if ! have_sdl_libs?; then
echo; error_msg "SDL libraries did not install correctly"; exit 1
else
echo; info_msg "SDL was installed successfully"
fi
}
# Installs SDL from source
# Takes about 30 min on RPI 2
install_sdl_source() {
echo "SDL will be compiled and installed from source."; echo
prompt_to_continue "Install SDL now?"
# Install SDL dependencies
print_task "Installing SDL2 dependencies" "\n\n"
sudo apt update
libs=(
build-essential # This may be missing on some ARM platforms
# SDL2
libudev-dev
libdbus-1-dev
libasound2-dev
# SDL2_image
libjpeg9-dev
libpng-dev
# SDL2_mixer
libmpg123-dev
libvorbis-dev
libflac-dev
# SDL2_ttf
libgl1-mesa-dev # Solves error missing -lGL
libfreetype6-dev
)
sudo apt install -y ${libs[*]}
# Downloads, compiles, and installs an SDL library from source
# params:
# $1 String URL of the tar archive
# $2 String Name of the tar file
# $3 String ./configure flags
install_sdl_lib () {
cd /tmp
wget -N $1
tar -xzf $2.tar.gz
cd $2
print_task "Configuring" "\n\n"
print_and_run "./configure $3"
echo; print_task "Compiling" "\n\n"
make
echo; print_task "Installing" "\n\n"
sudo make install
rm /tmp/$2.tar.gz
rm -rf /tmp/$2
}
# Note that `$have_*_lib` variables are set by `have_sdl_libs?()`
if [[ $have_sdl2_lib == 'false' ]]; then
echo; print_task "Downloading SDL2" "\n\n"
if [[ $platform == 'arm' ]]; then
if $platform_rpi; then
config="--host=arm-raspberry-linux-gnueabihf $sdl_arm_config_flags"
else
config=$sdl_arm_config_flags
fi
install_sdl_lib $sdl_url $sdl_fname "$config"
else
install_sdl_lib $sdl_url $sdl_fname
fi
fi
if [[ $have_image_lib == 'false' ]]; then
echo; print_task "Downloading SDL2_image" "\n\n"
install_sdl_lib $image_url $image_fname
fi
if [[ $have_mixer_lib == 'false' ]]; then
echo; print_task "Downloading SDL2_mixer" "\n\n"
install_sdl_lib $mixer_url $mixer_fname
fi
if [[ $have_ttf_lib == 'false' ]]; then
echo; print_task "Downloading SDL2_ttf" "\n\n"
install_sdl_lib $ttf_url $ttf_fname
fi
echo
if ! have_sdl_libs?; then
error_msg "SDL libraries did not install correctly"; exit 1
else
echo; info_msg "SDL was installed successfully"
fi
}
# Installs SDL
install_sdl() {
if [[ $platform == 'linux' ]]; then
install_sdl_linux
elif [[ $platform == 'bsd' ]]; then
install_sdl_bsd
elif [[ $platform == 'arm' ]]; then
install_sdl_source
fi
}
# Installs Simple 2D for MinGW environments
install_s2d_mingw() {
tmp_dir="/tmp/simple2d"
mkdir -p $tmp_dir
print_and_run "wget -NP $tmp_dir $s2d_mingw_installer_url"
print_and_run "pacman -S unzip --needed --noconfirm"
print_and_run "unzip -q $tmp_dir/$s2d_mingw_installer_fname -d $tmp_dir"
print_and_run "cd $tmp_dir"
print_and_run "bash install.sh -y"
print_and_run "rm -rf $tmp_dir"
}
# Installs Simple 2D
# params:
# $1 String Version to install, either 'master' or $VERSION
install_s2d() {
tmp_dir="/tmp/simple2d"
mkdir $tmp_dir
if [[ $1 == 'master' ]]; then
f_name=$1
else
f_name="v$1"
fi
url="https://github.com/simple2d/simple2d/archive/$f_name.zip"
print_task "Downloading Simple 2D" "\n\n"
# Linux and Raspberry Pi may not have curl installed by default
if which curl &>/dev/null; then
print_and_run "curl -L $url -o $tmp_dir/$f_name.zip"
else
print_and_run "wget -NP $tmp_dir $url"
fi
# Check if archive was downloaded properly
if [[ ! -f "$tmp_dir/$f_name.zip" ]]; then
echo; error_msg "Simple 2D could not be downloaded"; exit 1
fi
echo; print_task "Unpacking" "\n\n"
print_and_run "unzip -q $tmp_dir/$f_name.zip -d $tmp_dir"
# Check if archive was unpacked properly
if [[ $? != 0 ]]; then
echo; error_msg "Could not unpack. The downloaded Simple 2D package may be damaged."; exit 1
fi
print_and_run "cd $tmp_dir/simple2d-$1"
if [[ $platform == 'bsd' ]]; then
echo; print_and_run "gmake"
echo; print_and_run "sudo gmake install"
else
echo; print_and_run "make"
echo; print_and_run "sudo make install"
fi
echo; print_task "Cleaning up" "\n"; echo
print_and_run "rm -rf $tmp_dir"; echo
# Check if S2D installed correctly
if ! have_lib? 'simple2d'; then
echo; error_msg "Simple 2D did not install correctly"; exit 1
fi
echo
}
# Main entry point to install Simple 2D and SDL
# params:
# $1 String Flags used, e.g. `--sdl`
install() {
# If macOS, print message and quit
if [[ $platform == 'macos' ]]; then
macos_homebrew_message $1
fi
# If SDL flag, install only SDL and quit
if [[ $1 == '--sdl' ]]; then
echo
if have_sdl_libs?; then
echo; info_msg "SDL is already installed"
else
echo; install_sdl
fi
exit
fi
# Welcome message
echo -e "\n${BOLD}Welcome to Simple 2D!${NORMAL}"
echo -e "${BLUE}---------------------${NORMAL}\n"
echo -e "Platform detected: ${BOLD}${platform_display}${NORMAL}\n"
# If MinGW, install Simple 2D release and quit
if [[ $platform == 'mingw' ]]; then
prompt_to_continue "Continue to install?"
install_s2d_mingw
exit
fi
if have_lib? 'simple2d' > /dev/null; then
warning_msg "Simple 2D is already installed. Proceeding will reinstall."
fi
echo -e "Simple 2D will be installed to the following locations:
/usr/local/include/simple2d.h
/usr/local/lib/libsimple2d.a
/usr/local/bin/simple2d"
echo
if [[ $1 == '--HEAD' ]]; then
echo -e "This will install Simple 2D from the \`master\` development branch.\n"
fi
prompt_to_continue "Continue?"
if have_sdl_libs?; then
echo
else
echo; install_sdl
fi
if [[ $1 == '--HEAD' ]]; then
install_s2d 'master'
else
install_s2d $VERSION
fi
success_msg "Simple 2D installed successfully!"
}
# Uninstall ####################################################################
# Uninstalls SDL on BSD
uninstall_sdl_bsd() {
echo "The following packages will be removed:"
echo " sdl2"
echo " sdl2_image"
echo " sdl2_mixer"
echo " sdl2_ttf"
prompt_to_continue "Uninstall SDL now?"
echo; print_task "Uninstalling packages" "\n\n"
print_and_run "sudo pkg remove sdl2 sdl2_image sdl2_mixer sdl2_ttf"
echo
if have_sdl_libs?; then
echo; error_msg "SDL libraries did not uninstall correctly"
else
echo; info_msg "SDL was uninstalled successfully"
fi
}
# Uninstalls SDL on Linux
uninstall_sdl_linux() {
echo "The following packages will be removed:"
# Fedora and CentOS
if which yum &>/dev/null; then
echo " SDL2-devel"
echo " SDL2_image-devel"
echo " SDL2_mixer-devel"
echo " SDL2_ttf-devel"
# Arch
elif which pacman &>/dev/null; then
echo " sdl2"
echo " sdl2_image"
echo " sdl2_mixer"
echo " sdl2_ttf"
# openSUSE
elif which zypper &>/dev/null; then
echo " libSDL2-devel"
echo " libSDL2_image-devel"
echo " libSDL2_mixer-devel"
echo " libSDL2_ttf-devel"
# Ubuntu, Debian, and Mint
# `apt` must be last because openSUSE has it aliased to `zypper`
elif which apt &>/dev/null; then
echo " libsdl2-dev"
echo " libsdl2-image-dev"
echo " libsdl2-mixer-dev"
echo " libsdl2-ttf-dev"
else
error_msg "Could not find a package manager to uninstall SDL"; exit 1
fi
echo
prompt_to_continue "Uninstall SDL now?"
print_task "Uninstalling packages" "\n\n"
if which yum &>/dev/null; then
print_and_run "sudo yum remove -y SDL2-devel SDL2_image-devel SDL2_mixer-devel SDL2_ttf-devel"
elif which pacman &>/dev/null; then
print_and_run "sudo pacman -Rs --noconfirm sdl2 sdl2_image sdl2_mixer sdl2_ttf"
elif which zypper &>/dev/null; then
print_and_run "sudo zypper remove -y libSDL2-devel libSDL2_image-devel libSDL2_mixer-devel libSDL2_ttf-devel"
elif which apt &>/dev/null; then
print_and_run "sudo apt remove -y --purge libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev"
fi
echo
if have_sdl_libs?; then
echo; error_msg "SDL libraries did not uninstall correctly"
else
echo; info_msg "SDL was uninstalled successfully"
fi
}
# Uninstalls SDL from source
uninstall_sdl_source() {
prompt_to_continue "Uninstall SDL now?"
# Uninstall packages in case they were used
print_and_run "sudo apt remove -y --purge libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev"
uninstall_sdl_lib () {
cd /tmp
wget -N $1
tar -xzf $2.tar.gz
cd $2
./configure $3
sudo make uninstall
rm /tmp/$2.tar.gz
rm -rf /tmp/$2
}
echo; print_task "Uninstalling SDL2_image" "\n\n"
uninstall_sdl_lib $image_url $image_fname
echo; print_task "Uninstalling SDL2_mixer" "\n\n"
uninstall_sdl_lib $mixer_url $mixer_fname
echo; print_task "Uninstalling SDL2_ttf" "\n\n"
uninstall_sdl_lib $ttf_url $ttf_fname
echo; print_task "Uninstalling SDL2" "\n\n"
if [[ $platform == 'arm' ]]; then
uninstall_sdl_lib $sdl_url $sdl_fname "$sdl_arm_config_flags"
else
uninstall_sdl_lib $sdl_url $sdl_fname
fi
echo
if have_sdl_libs?; then
echo; error_msg "SDL libraries did not uninstall correctly"; exit 1
else
echo; info_msg "SDL was uninstalled successfully"
fi
}
# Uninstalls SDL
uninstall_sdl() {
if have_sdl_libs?; then
echo
else
echo; info_msg "SDL appears to be already uninstalled"
prompt_to_continue "Try uninstalling SDL anyways?"
fi
if [[ $platform == 'linux' ]]; then
uninstall_sdl_linux
elif [[ $platform == 'bsd' ]]; then
uninstall_sdl_bsd
elif [[ $platform == 'arm' ]]; then
uninstall_sdl_source
fi
}
# Uninstalls Simple 2D
# params:
# $1 String Flags used, e.g. `--sdl`
uninstall() {
if [[ $platform == 'macos' ]]; then
macos_homebrew_message $1
fi
# If MinGW, print message and quit
if [[ $platform == 'mingw' ]]; then
mingw_not_implemented_message
fi
if [[ $1 == '--sdl' ]]; then
echo; uninstall_sdl
exit
fi
echo; echo -e "The following files will be removed:
/usr/local/include/simple2d.h
/usr/local/lib/libsimple2d.a
/usr/local/bin/simple2d"
echo; prompt_to_continue "Uninstall Simple 2D?"
# Use hard-coded, absolute paths for safety
print_and_run "rm -f /usr/local/include/simple2d.h"
print_and_run "rm -f /usr/local/lib/libsimple2d.a"
print_and_run "rm -f /usr/local/bin/simple2d"
# Check if files were actually deleted
files=(/usr/local/include/simple2d.h
/usr/local/lib/libsimple2d.a
/usr/local/bin/simple2d)
if [ -f ${files[0]} -o -f ${files[1]} -o -f ${files[2]} ]; then
echo; error_msg "Simple 2D files could not be removed. Try using \`sudo\`?"
else
echo; success_msg "Simple 2D uninstalled!"
fi
}
# Update #######################################################################
# Updates Simple 2D to latest version or commit
# params:
# $1 String Flags used, e.g. `--HEAD`
update() {
if [[ $platform == 'macos' ]]; then
macos_homebrew_message
fi
# If MinGW, print message and quit
if [[ $platform == 'mingw' ]]; then
mingw_not_implemented_message
fi
# Check if Simple 2D is installed
if ! have_lib? 'simple2d' > /dev/null; then
error_msg "Simple 2D isn't currently installed"
echo -e "Use the \`install\` command to install Simple 2D.\n"
exit 1
fi
# Check if SDL is installed
update_check_sdl() {
if have_sdl_libs?; then
echo
else
echo; error_msg "SDL libraries missing"
echo -e "Run \`simple2d install --sdl\` to install SDL.\n"
exit 1
fi
}
# Read this script from repo, get the version number
get_remote_str $SCRIPT_URL
LATEST_VERSION=$(bash -c "$ret" -- -v)
compare_versions $LATEST_VERSION $VERSION
# $LATEST_VERSION is newer $VERSION (the version installed)
if [[ $? == 1 ]]; then
echo -e "A new version of Simple 2D is available.\n"
prompt_to_continue "Upgrade from v$VERSION to v$LATEST_VERSION?"
update_check_sdl
install_s2d $LATEST_VERSION
success_msg "Simple 2D has been updated to $LATEST_VERSION!"
echo -e "View the release notes:
${UNDERLINE}https://github.com/simple2d/simple2d/releases/latest${NORMAL}"; echo
# $LATEST_VERSION is the same as installed version
else
echo "Simple 2D is already up to date!"
fi
}
# Doctor #######################################################################
# Runs the diagnostics
doctor() {
echo; echo -e "Running diagnostics...\n"
errors=false
if ! have_sdl_libs?; then
errors=true
echo; error_msg "SDL libraries missing"
fi
if have_lib? 'simple2d'; then
echo
else
errors=true
echo; error_msg "Simple 2D library missing"
fi
if $errors; then
echo -e "Issues were found.\n"
else
success_msg "No issues found!"
fi
}
# Simulator ####################################################################
# Get list of booted iOS or tvOS simulators, store in $booted
# $1 Equals 'cmd' if called from command line
simulator_booted() {
booted=`xcrun simctl list devices | grep Booted`
if [[ $1 == 'cmd' ]]; then
if [[ $booted == '' ]]; then
echo "No devices are booted."
else
echo $booted
fi
fi
}
# List all iOS and tvOS simulator devices available
simulator_list() {
xcrun simctl list devices | grep -iv unavailable
}
# Open an iOS or tvOS simulator
# $1 The device name, e.g. iPhone X
# See `simulator_list` function for available names
simulator_open() {
device=$1
# Check if device name exists
if ! xcrun simctl list devices | grep -q "$device"; then
error_msg "\"$device\" does not match any simulator device names"
echo -e "Choose a device name from the following:\n"
simulator_list
exit 1
fi
# Get the device UDID
device_udid=`xcrun simctl list devices | grep "$device" | head -n1 | egrep -wo '\([-0-9A-F]+\)' | tr -d '\(\)'`
echo "Requested device: $device with UDID $device_udid"
simulator_booted
# If the current simulator running is not the requested device, then quit
if [[ $booted != '' && $booted != *$device* ]]; then
echo "Quitting current device simulator..."