渲染8种单色的精灵

时间:2015-07-22 11:43:38

标签: java rendering

我不擅长编程,但我正在努力学习,我一直在关注youtube的教程,youtuber使用4种颜色的单色,但我觉得很难制作具有这种限制的精灵,所以我试着去它有8种色调但是当我尝试改变它时它只是循环前4种颜色。 下面是颜色类,屏幕类和tile类。我可以上传你需要的任何其他课程。

package com.alan.game.level.tiles;

import com.alan.game.gfx.Colours;
import com.alan.game.gfx.Screen;
import com.alan.game.level.Level;

public abstract class Tile {

public static final Tile[] tiles = new Tile[256];
public static final Tile VOID = new BasicTile(0, 0, 0, Colours.get(000, -1, -1, -1, -1, -1, -1, -1));
public static final Tile STONE = new BasicTile(1, 1, 0, Colours.get(000, 002, 003, 004, 000, 000, 000, 000));
public static final Tile GRASS = new BasicTile(2, 2, 0, Colours.get(-1, 131, 141, -1, -1, -1,- 1, -1));

protected byte id;
protected boolean solid;
protected boolean emitter;

public Tile(int id, boolean isSolid, boolean isEmitter){
    this.id = (byte) id;
    if(tiles[id] != null) throw new RuntimeException("Duplicate tile id on " + id);
    this.solid = isSolid;
    this.emitter = isEmitter;
    tiles[id] = this;
}

public byte getId(){
    return id;
}

public boolean isSolid(){
    return solid;
}

public boolean isEmitter(){
    return emitter;
}
public abstract void render(Screen screen, Level level, int x, int y);
}

package com.alan.game.gfx;

public class Screen {

public static final int MAP_WIDTH = 64;
public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;

public int[] pixels;

public int xOffset = 0;
public int yOffset = 0;

public int width;
public int height;

public SpriteSheet sheet;

public Screen(int width, int height, SpriteSheet sheet){
    this.width = width;
    this.height = height;
    this.sheet = sheet;

    for(int i = 0; i < MAP_WIDTH * MAP_WIDTH; i++){
        pixels = new int[width * height];
    }
}

public void setOffset(int xOffset, int yOffset){
    this.xOffset = xOffset;
    this.yOffset = yOffset;
}
public void render(int xPos, int yPos, int tileIndex, int colour){
    render(xPos, yPos, tileIndex, colour, false, false);
}

public void render(int xPos, int yPos, int tileIndex, int colour, boolean mirrorX, boolean mirrorY){
    xPos -= xOffset;
    yPos -= yOffset;

    int xTile = tileIndex % 32;
    int yTile = tileIndex / 32;
    int tileOffset = (xTile << 3) + (yTile << 3) * sheet.width;
    for(int y = 0; y < 8; y++){
        int ySheet = y;
        if(mirrorY) ySheet = 7 - y;
        if(y + yPos < 0 || y + yPos >= height) continue;
        for(int x = 0; x < 8; x++){
            int xSheet = x;
            if(mirrorX) xSheet = 7 - x;
            if(x + xPos < 0 || x + xPos >= width) continue;
            int col = (colour >> (sheet.pixels[xSheet + ySheet * sheet.width + tileOffset] * 8)) & 255;
            if(col < 255) pixels[(x + xPos) + (y + yPos) * width] = col;
        }
    }
}
}

package com.alan.game.gfx;

public class Colours {

public static int get(int colour1, int colour2, int colour3, int colour4, int colour5, int colour6, int colour7, int colour8){

    return (get(colour8) << 56) + (get(colour7) << 48) + (get(colour6) << 40) + (get(colour5) << 32) + (get(colour4) << 24) + (get(colour3) << 16) + (get(colour2) << 8) + get(colour1);
}

private static int get(int colour){
    if(colour < 0) return 255;
    int r = colour / 100 % 10;
    int g = colour / 10 % 10;
    int b = colour % 10;
    return r * 36 + g * 6 + b;
}
}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我已经想到的第一件事是我们在颜色8上进行位移56.一个整数最多可以包含32位。因此,通过转移它56,我们完全摆脱了我们在那里的任何数据(如果我是正确的)。

所以我尝试改变它以返回长,但这也没有解决任何问题。我知道这不是一个真正的答案,但我希望这会引起一些关注,因为我现在也坚持这个!