0

所以我有这个函数可以将图像中的图块渲染到像素数组。我正在尝试制作一个新功能来将图像从 7*7 放大到 14*14

每个瓦片是8*8

  • xp = X 位置
  • yp = Y 位置
  • 瓷砖=瓷砖
  • 位置颜色 = 像素颜色

我相当确定这将是一项简单的任务,因为我认为我只需要扩展数组并共享位来填补空白。

如果您需要更多信息或需要我进一步简化代码,请告诉我。

public void scaledRender(int xp, int yp, int tile, int colors) {
    xp -= xOffset;
    yp -= yOffset;

    int xTile = tile % 32;
    int yTile = tile / 32;
    int toffs = xTile * 8 + yTile * 8 * sheet.width;

    int ys, xs;

    for (int y = 0; y < 8; y++) {
        ys = y;
        if (y + yp < 0 || y + yp >= h)
            continue;

        for (int x = 0; x < 8; x++) {
            if (x + xp < 0 || x + xp >= w)
                continue;

            xs = x;
            int col = (colors >> (sheet.pixels[xs + ys * sheet.width + toffs] * 8)) & 255;

            if (col < 255)
                pixels[((x + xp) + (y + yp) * w)] = Screen.colors[col] | 0xff000000;

        }
    }
}

编辑:

这行得通。感谢 GamerJosh。我不知道这是否是最有效的方法,但它确实有效!

public void scaledRender(int xp, int yp, int tile, int colors) {
xp -= xOffset;
yp -= yOffset;

int xTile = tile % 32;
int yTile = tile / 32;
int toffs = xTile * 8 + yTile * 8 * sheet.width;

int ys, xs;

for (int y = 0; y < 8; y++) {
    ys = y;
    if (y + yp < 0 || y + yp >= h)
        continue;

    for (int x = 0; x < 8; x++) {
        if (x + xp < 0 || x + xp >= w)
            continue;

        xs = x;
        int col = (colors >> (sheet.pixels[xs + ys * sheet.width + toffs] * 8)
            if (col < 255) {
                pixels[((x + xp)*2  ) + ((y + yp)*2  ) * w ] = Screen.colors[col] | 0xff000000;
                pixels[((x + xp)*2+1) + ((y + yp)*2  ) * w ] = Screen.colors[col] | 0xff000000;
                pixels[((x + xp)*2  ) + ((y + yp)*2+1) * w ] = Screen.colors[col] | 0xff000000;
                pixels[((x + xp)*2+1) + ((y + yp)*2+1) * w ] = Screen.colors[col] | 0xff000000;
            }
        }
    }
}
4

1 回答 1

3

使用字符串数组,这是一个代码片段,显示了一种可能的(尽管非常基本)数组放大算法。您应该能够根据需要对其进行修改。

// Initialize array
String [][] array = {{"00", "01", "02", "03", "04", "05", "06", "07"},
                     {"10", "11", "12", "13", "14", "15", "16", "17"},
                     {"20", "21", "22", "23", "24", "25", "26", "27"},
                     {"30", "31", "32", "33", "34", "35", "36", "37"},
                     {"40", "41", "42", "43", "44", "45", "46", "47"},
                     {"50", "51", "52", "53", "54", "55", "56", "57"},
                     {"60", "61", "62", "63", "64", "65", "66", "67"},
                     {"70", "71", "72", "73", "74", "75", "76", "77"},
                     };

// Simply output the original array so we can visually compare later
for (int i = 0; i < 8; ++i) {
    for (int j = 0; j < 8; ++j) {
        System.out.print(" " + array[i][j]);
    }
    System.out.println("");
}

// Create a new array that is twice the size as the original array
String[][] scaledArray = new String[16][16];

// Scale the original array into the new array
for (int i = 0; i < 8; ++i) {
    for (int j = 0; j < 8; ++j) {
        scaledArray[(i*2)][(j*2)] = array[i][j];
        scaledArray[(i*2) + 1][(j*2)] = array[i][j];
        scaledArray[(i*2)][(j*2) + 1] = array[i][j];
        scaledArray[(i*2) + 1][(j*2) + 1] = array[i][j];
    }
}

// Output the scaled array to see the result
System.out.println("\nSCALED: ");
for (int i = 0; i < 16; ++i) {
    for (int j = 0; j < 16; ++j) {
        System.out.print(" " + scaledArray[i][j]);
    }
    System.out.println("");
}

程序输出为:

00 01 02 03 04 05 06 07
10 11 12 13 14 15 16 17
20 21 22 23 24 25 26 27
30 31 32 33 34 35 36 37
40 41 42 43 44 45 46 47
50 51 52 53 54 55 56 57
60 61 62 63 64 65 66 67
70 71 72 73 74 75 76 77

SCALED: 
 00 00 01 01 02 02 03 03 04 04 05 05 06 06 07 07
 00 00 01 01 02 02 03 03 04 04 05 05 06 06 07 07
 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17
 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17
 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27
 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27
 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37
 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37
 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47
 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47
 50 50 51 51 52 52 53 53 54 54 55 55 56 56 57 57
 50 50 51 51 52 52 53 53 54 54 55 55 56 56 57 57
 60 60 61 61 62 62 63 63 64 64 65 65 66 66 67 67
 60 60 61 61 62 62 63 63 64 64 65 65 66 66 67 67
 70 70 71 71 72 72 73 73 74 74 75 75 76 76 77 77
 70 70 71 71 72 72 73 73 74 74 75 75 76 76 77 77
于 2013-09-17T19:06:55.607 回答