]> de.git.xonotic.org Git - xonotic/mediasource.git/blob - gfx/minigames/build.sh
Minor tweaks to the minigame texture build script
[xonotic/mediasource.git] / gfx / minigames / build.sh
1 #!/bin/bash
2
3 CONVERT=convert
4 SED="sed -r"
5 MKDIR="mkdir -p"
6
7 OUT_DIR=/tmp/minigames
8 JPEG_QUALITY=75
9 TEXTURE_SIZE=2048
10 FORMAT=tga
11
12 # Converts images into $FORMAT
13 # Synopsis: convert_image input.png output
14 # Genreates output.jpg and output_alpha.jpg
15 function convert_image()
16 {
17         local covert_flags="-layers flatten"
18         if [ "$FORMAT" = "jpg" ]
19         then
20                 covert_flags="$covert_flags -quality $JPEG_QUALITY"
21         fi
22         convert -background none "$1" $covert_flags "$2.$FORMAT"
23         if [ "$FORMAT" = "jpg" ]
24         then
25                 convert -background none "$1" $covert_flags -alpha Extract "$2_alpha.jpg"
26         fi
27 }
28
29 # Ensure that the output jpg are of the appropriate size
30 # Synopsis: scale_image output
31 # Where "output" is the same as $2 in convert_image
32 function scale_image()
33 {
34         let max_w=$TEXTURE_SIZE
35         
36         if echo -n "$1" | grep -q ".*/piece.*"
37         then
38                 let max_w/=4
39         fi
40         
41         if [ "$max_w" -gt 0 ]
42         then
43                 let img_w=$(identify -format %w "$1.$FORMAT")
44                 if [ "$img_w" -gt "$max_w" ]
45                 then
46                         local scale=$(echo "scale=10; $max_w/$img_w*100" | bc)
47                         convert "$1.$FORMAT" -resize "$scale%" "$1.$FORMAT"
48                         if [ "$FORMAT" = "jpg" ]
49                         then
50                                 convert "$1_alpha.jpg" -resize "$scale%" "$1_alpha.jpg"
51                         fi
52                 fi
53         fi
54 }
55
56 # Create the files for packaging
57 function generate()
58 {
59         for src_file in $(git ls-files '*/*')
60         do
61                 local out_dir="$OUT_DIR/gfx/hud/default/minigames/"
62                 local out_file="$out_dir/$src_file"
63                 $MKDIR "$out_dir/$(dirname "$src_file")"
64                 
65                 local file_extension="$(echo "$src_file" | sed -r "s/^(.*)\.([^.]+)$/\2/")"
66                 local base_file="$(echo "$out_file" | sed -r "s/^(.*)\.([^.]+)$/\1/")"
67                 
68                 if echo -n "$file_extension" | grep -Eq "^(png|svg|tga|xcf)$" && 
69                         [ "$file_extension" != "$FORMAT" ]
70                 then
71                         out_file="$base_file.$FORMAT"
72                         if [ "$src_file" -nt "$out_file" ]
73                         then
74                                 echo -e "Converting \x1b[1m$src_file\x1b[0m"
75                                 convert_image "$src_file" "$base_file"
76                                 scale_image "$base_file"
77                                 continue
78                         fi
79                 elif [ "$src_file" -nt "$out_file" ]
80                 then
81                         echo -e "Copying \x1b[1m$src_file\x1b[0m"
82                         cp -T "$src_file" "$out_file"
83                         continue
84                 fi
85                 
86                 echo -e "Skipping \x1b[1m$src_file\x1b[0m"
87                 
88         done
89 }
90
91 SYSTEM_NAME=$(uname)
92 if [ "$SYSTEM_NAME" = Darwin -o "$SYSTEM_NAME" = FreeBSD ]
93 then
94         SED="sed -E"
95 fi
96
97
98 $MKDIR "$OUT_DIR"
99
100
101 while [ "$1" ]
102 do
103         case $1 in
104                 help|-h|--help)
105                         echo TODO
106                         exit
107                         ;;
108                 clean)
109                         echo -e "Removing old files"
110                         [ -n "$OUT_DIR" -a -e "$OUT_DIR" ] && rm -rf "$OUT_DIR"
111                         ;;
112                 *)
113                         echo 1>&2 "Unknown option: $1"
114                         ;;
115         esac
116         shift
117 done
118
119 generate