project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MapPreviewGenerator.java
author koda
Tue, 15 Oct 2013 01:15:39 +0200
changeset 9543 1826b5315369
parent 7588 27e5857da6af
child 10017 de822cd3df3a
permissions -rw-r--r--
Fix bug #656 by using the right resolution values
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
27e5857da6af Hedgeroid improvements:
Medo <smaxein@googlemail.com>
parents: 7584
diff changeset
    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.
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
 * 
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 {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
	private static final String TAG = MapPreviewGenerator.class.getSimpleName();
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
	private static final Handler mainHandler = new Handler(Looper.getMainLooper());
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    59
	private static final long TIMEOUT_NS = 20l * 1000 * 1000 * 1000;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
	private final Context appContext;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
	private final MapRecipe map;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
	private final Listener listener;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
	private boolean resultAvailable;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
	private Drawable result;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
	public static interface Listener {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
		/**
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
		 * This is called on the UI thread once the preview is ready or failed.
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
		 * In case of failure, null is passed.
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
		 */
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
		void onMapPreviewResult(Drawable preview);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
	}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
	private MapPreviewGenerator(Context appContext, MapRecipe map, Listener listener) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
		this.appContext = appContext;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
		this.map = map;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
		this.listener = listener;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
	}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
	public void run() {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
		if (map.mapgen == Frontlib.MAPGEN_NAMED) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
			postToListener(loadPreviewFromFile(appContext, map.name));
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
		} else {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
			resultAvailable = false;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
			result = null;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
			MapconnPtr conn = Flib.INSTANCE.flib_mapconn_create(MapRecipePtr.createJavaOwned(map));
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
			if (conn == null) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
				postToListener(null);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
				return;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
			}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
			try {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
				int port = Flib.INSTANCE.flib_mapconn_getport(conn);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
				Flib.INSTANCE.flib_mapconn_onSuccess(conn, successCb, null);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    96
				Flib.INSTANCE.flib_mapconn_onFailure(conn, failureCb, null);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    97
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
				String configPath;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
				try {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
					configPath = FileUtils.getCachePath(appContext).getAbsolutePath();
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   101
				} catch(FileNotFoundException e) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
					return;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   103
				}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
				
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   105
				startEngine(configPath, port);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   106
				long startTime = System.nanoTime();
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   107
				do {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108
					Flib.INSTANCE.flib_mapconn_tick(conn);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   109
					try {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   110
						Thread.sleep(50);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   111
					} catch (InterruptedException e) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
						// ignore
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   113
					}
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
   114
					if(System.nanoTime()-startTime > TIMEOUT_NS) {
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   115
						Log.w(TAG, "Error generating map preview: timeout");
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   116
						resultAvailable = true;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   117
					}
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
   118
				} while(!resultAvailable); 
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
			} finally {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   120
				Flib.INSTANCE.flib_mapconn_destroy(conn);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
				postToListener(result);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   122
			}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   123
		}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   124
	}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   125
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   126
	public static void startPreviewGeneration(Context appContext, MapRecipe map, Listener listener) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   127
		new Thread(new MapPreviewGenerator(appContext, map, listener)).start();
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   128
	}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   130
	private static Drawable loadPreviewFromFile(Context appContext, String mapName) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   131
		if(!mapName.startsWith("+")) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   132
			try {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   133
				File previewFile = MapFile.getPreviewFile(appContext, mapName);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   134
				return Drawable.createFromPath(previewFile.getAbsolutePath());
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   135
			} catch (FileNotFoundException e) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   136
				Log.w("MapPreviewGenerator", "Preview for map "+mapName+" not found.");
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   137
			}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   138
		}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   139
		return null;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   140
	}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   141
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   142
	private static void startEngine(final String configPath, final int port) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   143
		new Thread(new Runnable() {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   144
			public void run() {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   145
				Log.d(TAG, "Starting engine "+port);
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   146
				PascalExports.synchronizedGenLandPreview(port);
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   147
				Log.d(TAG, "Engine finished");
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   148
			}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   149
		}).start();
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   150
	}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   151
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   152
	private void postToListener(final Drawable result) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   153
		mainHandler.post(new Runnable() {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   154
			public void run() {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   155
				listener.onMapPreviewResult(result);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   156
			}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   157
		});
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   158
	}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   159
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   160
	/**
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   161
	 * Let's be extra nice here and clip off the left and right sides, so the preview is centered...
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   162
	 * Since the image is present in bytes, we can save some effort by checking entire byte-columns first.
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   163
	 */
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   164
	private final MapimageCallback successCb = new MapimageCallback() {
7588
27e5857da6af Hedgeroid improvements:
Medo <smaxein@googlemail.com>
parents: 7584
diff changeset
   165
		public void callback(Pointer context, ByteArrayPtr buffer, int hedgehogCount) {
27e5857da6af Hedgeroid improvements:
Medo <smaxein@googlemail.com>
parents: 7584
diff changeset
   166
			byte[] mapdata = buffer.deref(Frontlib.MAPIMAGE_BYTES);
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   167
			
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   168
			int leftmostPixel = Frontlib.MAPIMAGE_WIDTH;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   169
			int rightmostPixel = -1;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   170
			int bytesPerLine = Frontlib.MAPIMAGE_WIDTH/8;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   171
			
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   172
			// Find the leftmost pixel
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   173
			for(int xbyte=0; xbyte<bytesPerLine; xbyte++) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   174
				for(int y=0; y<Frontlib.MAPIMAGE_HEIGHT; y++) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   175
					int b = 0xff&mapdata[xbyte+y*bytesPerLine];
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   176
					if(b != 0) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   177
						leftmostPixel = Math.min(leftmostPixel, Integer.numberOfLeadingZeros(b)-24+xbyte*8);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   178
					}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   179
				}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   180
				if(leftmostPixel!=Frontlib.MAPIMAGE_WIDTH) break;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   181
			}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   182
			
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   183
			// Find the rightmost pixel
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   184
			for(int xbyte=bytesPerLine-1; xbyte>=0; xbyte--) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   185
				for(int y=0; y<Frontlib.MAPIMAGE_HEIGHT; y++) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   186
					int b = mapdata[xbyte+y*bytesPerLine];
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   187
					if(b != 0) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   188
						rightmostPixel = Math.max(rightmostPixel, xbyte*8+7-Integer.numberOfTrailingZeros(b));
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   189
					}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   190
				}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   191
				if(rightmostPixel!=-1) break;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   192
			}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   193
		
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   194
			// No pixel was set at all -> use default width
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   195
			if(rightmostPixel==-1) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   196
				leftmostPixel = 0;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   197
				rightmostPixel = Frontlib.MAPIMAGE_WIDTH-1;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   198
			}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   199
			
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   200
			Bitmap bitmap = Bitmap.createBitmap(rightmostPixel-leftmostPixel+1, Frontlib.MAPIMAGE_HEIGHT, Config.ARGB_8888);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   201
			for(int y=0; y<Frontlib.MAPIMAGE_HEIGHT; y++) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   202
				for(int x=0; x<bitmap.getWidth(); x++) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   203
					bitmap.setPixel(x, y, isPixelSet(mapdata, x+leftmostPixel, y) ? Color.YELLOW : Color.TRANSPARENT);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   204
				}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   205
			}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   206
			result = new BitmapDrawable(bitmap);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   207
			resultAvailable = true;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   208
		}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   209
	};
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   210
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   211
	private static boolean isPixelSet(byte[] imgdata, int x, int y) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   212
		int pixelnum = x+Frontlib.MAPIMAGE_WIDTH*y;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   213
		return (imgdata[pixelnum>>3] & (128>>(pixelnum&7))) != 0;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   214
	}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   215
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   216
	private final StrCallback failureCb = new StrCallback() {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   217
		public void callback(Pointer context, String reason) {
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   218
			Log.w(TAG, "Error generating map preview: "+reason);
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   219
			result = null;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   220
			resultAvailable = true;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   221
		}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   222
	};
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   223
}