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