策略游戏地图加载算法中的性能相关问题 (Java, lwjgl)

Performance Related Problem in strategy-game map loading algorithm (Java, lwjgl)

我正在创建一款游戏,您可以在其中选择一个国家并管理它,但由于大量计算(缺乏性能),我无法找到一种不会导致程序崩溃的加载地图的方法。

我做了一个算法,循环遍历包含地图省份(游戏中的空间单位)的图像的每个像素,每个像素都有自己的颜色,这样,当我遇到尚未在一个像素,我知道这是一个新的省份,因此我可以使用文件中的信息将其加载到新的 Province() 实例中。

上面所说的一切都工作得很好,几乎不花时间,但要在不同国家互相攻击时编辑地图,我需要一种方法来单独渲染每个省份,用着色器赋予它国家的颜色。

我添加了这段获取当前像素位置的代码,并将其缩小到 openGL 坐标,将其保存在 arrayList (currVertices) 中,然后将其放入另一个 ArrayList ( provinceVertices) of float[] 一旦找到一个新的省份。

(我知道代码不漂亮而且我不是专家程序员(我也才 14 岁)所以请在告诉我我做错了什么时尽量友善, 我试过每 4 个像素存储一个顶点来缩小列表,但它仍然崩溃)

List<Float> currVertices = new ArrayList<Float>(); // the vertices of the current province          
for (int y = 0; y < worldImage.getHeight(); y++) {
    for (int x = 0; x < worldImage.getWidth(); x++) {
        if (!currColors.contains(worldImage.getRGB(x, y))) {
            if (!currVertices.isEmpty())
                provinceVertices.add(Utils.toFloatArray(currVertices)); // store the current province's vertices into the total database
            currVertices.clear();
        }
        if (x % 4 == 0)
            currVertices.add((float) (x) / EngineManager.getWindowWidth());
        if (y % 4 == 0)
            currVertices.add((float) (y) / EngineManager.getWindowHeight());
    }
}

我只包含了表示顶点加载的代码

public static float[] toFloatArray(List<Float> list) {
    float[] array = new float[list.size()];
    ListIterator<Float> iterator = list.listIterator();
    while (iterator.hasNext()) {
        array[iterator.nextIndex()] = list.get(iterator.nextIndex());
    }
    return array;
}

第二个 ArrayList 的目标是让所有顶点的顺序正确,但是当我尝试将 currVertices 添加到 provinceVertices 时,游戏就崩溃了没有错误消息,这就是为什么我猜测问题与性能有关。 (顶点可以很好地加载到 currVertices 列表中)

使用 nextIndex() 不会增加索引。尝试改用:

while (iterator.hasNext()) {
    array[iterator.nextIndex()] = iterator.next();
}