Minigame texture sources
[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=png
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/"
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                         echo "$base_file"
73                         if [ "$src_file" -nt "$out_file" ]
74                         then
75                                 echo -e "Compressing \x1b[1m$src_file\x1b[0m"
76                                 convert_image "$src_file" "$base_file"
77                                 scale_image "$base_file"
78                                 continue
79                         fi
80                 elif [ "$src_file" -nt "$out_file" ]
81                 then
82                         echo -e "Copying \x1b[1m$src_file\x1b[0m"
83                         cp -T "$src_file" "$out_file"
84                         continue
85                 fi
86                 
87                 echo -e "Skipping \x1b[1m$src_file\x1b[0m"
88                 
89         done
90 }
91
92 SYSTEM_NAME=$(uname)
93 if [ "$SYSTEM_NAME" = Darwin -o "$SYSTEM_NAME" = FreeBSD ]
94 then
95         SED="sed -E"
96 fi
97
98
99 $MKDIR "$OUT_DIR"
100
101
102 while [ "$1" ]
103 do
104         case $1 in
105                 help|-h|--help)
106                         echo TODO
107                         exit
108                         ;;
109                 clean)
110                         echo -e "Removing old files"
111                         [ -n "$OUT_DIR" -a -e "$OUT_DIR" ] && rm -rf "$OUT_DIR"
112                         ;;
113                 *)
114                         echo 1>&2 "Unknown option: $1"
115                         ;;
116         esac
117         shift
118 done
119
120 generate