author | Wuzzy <Wuzzy2@mail.ru> |
Sat, 21 Oct 2017 18:38:28 +0200 | |
changeset 12734 | 4e0e59255856 |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
1 |
/* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
4 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
6 |
* modify it under the terms of the GNU General Public License |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
7 |
* as published by the Free Software Foundation; either version 2 |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
9 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
13 |
* GNU General Public License for more details. |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
14 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
18 |
*/ |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
19 |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
20 |
package org.hedgewars.hedgeroid; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
import java.io.File; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
import java.io.FileNotFoundException; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
import org.hedgewars.hedgeroid.Datastructures.MapFile; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
import org.hedgewars.hedgeroid.Datastructures.MapRecipe; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
27 |
import org.hedgewars.hedgeroid.EngineProtocol.PascalExports; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
import org.hedgewars.hedgeroid.frontlib.Flib; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
import org.hedgewars.hedgeroid.frontlib.Frontlib; |
7588 | 30 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.ByteArrayPtr; |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.MapRecipePtr; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.MapconnPtr; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.MapimageCallback; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.StrCallback; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
35 |
import org.hedgewars.hedgeroid.util.FileUtils; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
import android.content.Context; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
import android.graphics.Bitmap; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
import android.graphics.Bitmap.Config; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
import android.graphics.Color; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
import android.graphics.drawable.BitmapDrawable; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
import android.graphics.drawable.Drawable; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
43 |
import android.os.Handler; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
44 |
import android.os.Looper; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
45 |
import android.util.Log; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
46 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
47 |
import com.sun.jna.Pointer; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
48 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
49 |
/** |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
50 |
* A class that asynchronously generates a map preview from a MapRecipe. |
10017 | 51 |
* |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
52 |
* For named maps, this will load the preview image from the filesystem. For others, |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
53 |
* it will call the Hedgewars engine to generate a preview image. The result is sent |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
54 |
* back to a listener on the UI thread. |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
55 |
*/ |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
56 |
public final class MapPreviewGenerator implements Runnable { |
10017 | 57 |
private static final String TAG = MapPreviewGenerator.class.getSimpleName(); |
58 |
private static final Handler mainHandler = new Handler(Looper.getMainLooper()); |
|
59 |
private static final long TIMEOUT_NS = 20l * 1000 * 1000 * 1000; |
|
60 |
||
61 |
private final Context appContext; |
|
62 |
private final MapRecipe map; |
|
63 |
private final Listener listener; |
|
64 |
||
65 |
private boolean resultAvailable; |
|
66 |
private Drawable result; |
|
67 |
||
68 |
public static interface Listener { |
|
69 |
/** |
|
70 |
* This is called on the UI thread once the preview is ready or failed. |
|
71 |
* In case of failure, null is passed. |
|
72 |
*/ |
|
73 |
void onMapPreviewResult(Drawable preview); |
|
74 |
} |
|
75 |
||
76 |
private MapPreviewGenerator(Context appContext, MapRecipe map, Listener listener) { |
|
77 |
this.appContext = appContext; |
|
78 |
this.map = map; |
|
79 |
this.listener = listener; |
|
80 |
} |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
81 |
|
10017 | 82 |
public void run() { |
83 |
if (map.mapgen == Frontlib.MAPGEN_NAMED) { |
|
84 |
postToListener(loadPreviewFromFile(appContext, map.name)); |
|
85 |
} else { |
|
86 |
resultAvailable = false; |
|
87 |
result = null; |
|
88 |
MapconnPtr conn = Flib.INSTANCE.flib_mapconn_create(MapRecipePtr.createJavaOwned(map)); |
|
89 |
if (conn == null) { |
|
90 |
postToListener(null); |
|
91 |
return; |
|
92 |
} |
|
93 |
try { |
|
94 |
int port = Flib.INSTANCE.flib_mapconn_getport(conn); |
|
95 |
Flib.INSTANCE.flib_mapconn_onSuccess(conn, successCb, null); |
|
96 |
Flib.INSTANCE.flib_mapconn_onFailure(conn, failureCb, null); |
|
97 |
||
98 |
String configPath; |
|
99 |
try { |
|
100 |
configPath = FileUtils.getCachePath(appContext).getAbsolutePath(); |
|
101 |
} catch(FileNotFoundException e) { |
|
102 |
return; |
|
103 |
} |
|
104 |
||
105 |
startEngine(configPath, port); |
|
106 |
long startTime = System.nanoTime(); |
|
107 |
do { |
|
108 |
Flib.INSTANCE.flib_mapconn_tick(conn); |
|
109 |
try { |
|
110 |
Thread.sleep(50); |
|
111 |
} catch (InterruptedException e) { |
|
112 |
// ignore |
|
113 |
} |
|
114 |
if(System.nanoTime()-startTime > TIMEOUT_NS) { |
|
115 |
Log.w(TAG, "Error generating map preview: timeout"); |
|
116 |
resultAvailable = true; |
|
117 |
} |
|
118 |
} while(!resultAvailable); |
|
119 |
} finally { |
|
120 |
Flib.INSTANCE.flib_mapconn_destroy(conn); |
|
121 |
postToListener(result); |
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
public static void startPreviewGeneration(Context appContext, MapRecipe map, Listener listener) { |
|
127 |
new Thread(new MapPreviewGenerator(appContext, map, listener)).start(); |
|
128 |
} |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
129 |
|
10017 | 130 |
private static Drawable loadPreviewFromFile(Context appContext, String mapName) { |
131 |
if(!mapName.startsWith("+")) { |
|
132 |
try { |
|
133 |
File previewFile = MapFile.getPreviewFile(appContext, mapName); |
|
134 |
return Drawable.createFromPath(previewFile.getAbsolutePath()); |
|
135 |
} catch (FileNotFoundException e) { |
|
136 |
Log.w("MapPreviewGenerator", "Preview for map "+mapName+" not found."); |
|
137 |
} |
|
138 |
} |
|
139 |
return null; |
|
140 |
} |
|
141 |
||
142 |
private static void startEngine(final String configPath, final int port) { |
|
143 |
new Thread(new Runnable() { |
|
144 |
public void run() { |
|
145 |
Log.d(TAG, "Starting engine "+port); |
|
146 |
PascalExports.synchronizedGenLandPreview(port); |
|
147 |
Log.d(TAG, "Engine finished"); |
|
148 |
} |
|
149 |
}).start(); |
|
150 |
} |
|
151 |
||
152 |
private void postToListener(final Drawable result) { |
|
153 |
mainHandler.post(new Runnable() { |
|
154 |
public void run() { |
|
155 |
listener.onMapPreviewResult(result); |
|
156 |
} |
|
157 |
}); |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Let's be extra nice here and clip off the left and right sides, so the preview is centered... |
|
162 |
* Since the image is present in bytes, we can save some effort by checking entire byte-columns first. |
|
163 |
*/ |
|
164 |
private final MapimageCallback successCb = new MapimageCallback() { |
|
165 |
public void callback(Pointer context, ByteArrayPtr buffer, int hedgehogCount) { |
|
166 |
byte[] mapdata = buffer.deref(Frontlib.MAPIMAGE_BYTES); |
|
167 |
||
168 |
int leftmostPixel = Frontlib.MAPIMAGE_WIDTH; |
|
169 |
int rightmostPixel = -1; |
|
170 |
int bytesPerLine = Frontlib.MAPIMAGE_WIDTH/8; |
|
171 |
||
172 |
// Find the leftmost pixel |
|
173 |
for(int xbyte=0; xbyte<bytesPerLine; xbyte++) { |
|
174 |
for(int y=0; y<Frontlib.MAPIMAGE_HEIGHT; y++) { |
|
175 |
int b = 0xff&mapdata[xbyte+y*bytesPerLine]; |
|
176 |
if(b != 0) { |
|
177 |
leftmostPixel = Math.min(leftmostPixel, Integer.numberOfLeadingZeros(b)-24+xbyte*8); |
|
178 |
} |
|
179 |
} |
|
180 |
if(leftmostPixel!=Frontlib.MAPIMAGE_WIDTH) break; |
|
181 |
} |
|
182 |
||
183 |
// Find the rightmost pixel |
|
184 |
for(int xbyte=bytesPerLine-1; xbyte>=0; xbyte--) { |
|
185 |
for(int y=0; y<Frontlib.MAPIMAGE_HEIGHT; y++) { |
|
186 |
int b = mapdata[xbyte+y*bytesPerLine]; |
|
187 |
if(b != 0) { |
|
188 |
rightmostPixel = Math.max(rightmostPixel, xbyte*8+7-Integer.numberOfTrailingZeros(b)); |
|
189 |
} |
|
190 |
} |
|
191 |
if(rightmostPixel!=-1) break; |
|
192 |
} |
|
193 |
||
194 |
// No pixel was set at all -> use default width |
|
195 |
if(rightmostPixel==-1) { |
|
196 |
leftmostPixel = 0; |
|
197 |
rightmostPixel = Frontlib.MAPIMAGE_WIDTH-1; |
|
198 |
} |
|
199 |
||
200 |
Bitmap bitmap = Bitmap.createBitmap(rightmostPixel-leftmostPixel+1, Frontlib.MAPIMAGE_HEIGHT, Config.ARGB_8888); |
|
201 |
for(int y=0; y<Frontlib.MAPIMAGE_HEIGHT; y++) { |
|
202 |
for(int x=0; x<bitmap.getWidth(); x++) { |
|
203 |
bitmap.setPixel(x, y, isPixelSet(mapdata, x+leftmostPixel, y) ? Color.YELLOW : Color.TRANSPARENT); |
|
204 |
} |
|
205 |
} |
|
206 |
result = new BitmapDrawable(bitmap); |
|
207 |
resultAvailable = true; |
|
208 |
} |
|
209 |
}; |
|
210 |
||
211 |
private static boolean isPixelSet(byte[] imgdata, int x, int y) { |
|
212 |
int pixelnum = x+Frontlib.MAPIMAGE_WIDTH*y; |
|
213 |
return (imgdata[pixelnum>>3] & (128>>(pixelnum&7))) != 0; |
|
214 |
} |
|
215 |
||
216 |
private final StrCallback failureCb = new StrCallback() { |
|
217 |
public void callback(Pointer context, String reason) { |
|
218 |
Log.w(TAG, "Error generating map preview: "+reason); |
|
219 |
result = null; |
|
220 |
resultAvailable = true; |
|
221 |
} |
|
222 |
}; |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
223 |
} |