project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MapPreviewGenerator.java
changeset 7582 714310efad8f
parent 7508 763d3961400b
child 7584 7831c84cc644
equal deleted inserted replaced
7580:c92596feac0d 7582:714310efad8f
    87 					try {
    87 					try {
    88 						Thread.sleep(50);
    88 						Thread.sleep(50);
    89 					} catch (InterruptedException e) {
    89 					} catch (InterruptedException e) {
    90 						// ignore
    90 						// ignore
    91 					}
    91 					}
    92 				} while(!resultAvailable && System.nanoTime()-startTime < 15000000000l); // 15 seconds timeout
    92 					if(System.nanoTime()-startTime > 15000000000l) {
       
    93 						Log.w(TAG, "Error generating map preview: timeout");
       
    94 						resultAvailable = true;
       
    95 					}
       
    96 				} while(!resultAvailable); // 15 seconds timeout
    93 			} finally {
    97 			} finally {
    94 				Flib.INSTANCE.flib_mapconn_destroy(conn);
    98 				Flib.INSTANCE.flib_mapconn_destroy(conn);
    95 				postToListener(result);
    99 				postToListener(result);
    96 			}
   100 			}
    97 		}
   101 		}
   115 	
   119 	
   116 	private static void startEngine(final String configPath, final int port) {
   120 	private static void startEngine(final String configPath, final int port) {
   117 		new Thread(new Runnable() {
   121 		new Thread(new Runnable() {
   118 			public void run() {
   122 			public void run() {
   119 				Log.d(TAG, "Starting engine "+port);
   123 				Log.d(TAG, "Starting engine "+port);
   120 				synchronized(PascalExports.engineMutex) {
   124 				PascalExports.synchronizedGenLandPreview(port);
   121 					PascalExports.HWGenLandPreview(port);
       
   122 				}
       
   123 				Log.d(TAG, "Engine finished");
   125 				Log.d(TAG, "Engine finished");
   124 			}
   126 			}
   125 		}).start();
   127 		}).start();
   126 	}
   128 	}
   127 	
   129 	
   137 	 * Let's be extra nice here and clip off the left and right sides, so the preview is centered...
   139 	 * Let's be extra nice here and clip off the left and right sides, so the preview is centered...
   138 	 * Since the image is present in bytes, we can save some effort by checking entire byte-columns first.
   140 	 * Since the image is present in bytes, we can save some effort by checking entire byte-columns first.
   139 	 */
   141 	 */
   140 	private final MapimageCallback successCb = new MapimageCallback() {
   142 	private final MapimageCallback successCb = new MapimageCallback() {
   141 		public void callback(Pointer context, Pointer buffer, int hedgehogCount) {
   143 		public void callback(Pointer context, Pointer buffer, int hedgehogCount) {
   142 			Log.d(TAG, "Running success handler");
       
   143 			byte[] mapdata = buffer.getByteArray(0, Frontlib.MAPIMAGE_BYTES);
   144 			byte[] mapdata = buffer.getByteArray(0, Frontlib.MAPIMAGE_BYTES);
   144 			
   145 			
   145 			int leftmostPixel = Frontlib.MAPIMAGE_WIDTH;
   146 			int leftmostPixel = Frontlib.MAPIMAGE_WIDTH;
   146 			int rightmostPixel = -1;
   147 			int rightmostPixel = -1;
   147 			int bytesPerLine = Frontlib.MAPIMAGE_WIDTH/8;
   148 			int bytesPerLine = Frontlib.MAPIMAGE_WIDTH/8;
   190 		return (imgdata[pixelnum>>3] & (128>>(pixelnum&7))) != 0;
   191 		return (imgdata[pixelnum>>3] & (128>>(pixelnum&7))) != 0;
   191 	}
   192 	}
   192 	
   193 	
   193 	private final StrCallback failureCb = new StrCallback() {
   194 	private final StrCallback failureCb = new StrCallback() {
   194 		public void callback(Pointer context, String reason) {
   195 		public void callback(Pointer context, String reason) {
   195 			Log.e(TAG, "Error generating map preview: "+reason);
   196 			Log.w(TAG, "Error generating map preview: "+reason);
   196 			result = null;
   197 			result = null;
   197 			resultAvailable = true;
   198 			resultAvailable = true;
   198 		}
   199 		}
   199 	};
   200 	};
   200 }
   201 }