Frage

Well i have been watching a couple of videos of youtube on how take sprites from a spritesheet (8x8) and i really liked the tutorial by DesignsByZepher. However the method he uses results in him importing a sorite sheet and then changing the colors to in-code selected colours.

http://www.youtube.com/watch?v=6FMgQNDNMJc displaying the sheet

http://www.youtube.com/watch?v=7eotyB7oNHE for the color rendering

The code that i have made from watching his video is:

package exikle.learn.game.gfx;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {

    public String path;
    public int width;
    public int height;

    public int[] pixels;

    public SpriteSheet(String path) {
        BufferedImage image = null;
        try {
            image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (image == null) { return; }

        this.path = path;
        this.width = image.getWidth();
        this.height = image.getHeight();

        pixels = image.getRGB(0, 0, width, height, null, 0, width);

        for (int i = 0; i < pixels.length; i++) {
            pixels[i] = (pixels[i] & 0xff) / 64;
        }
    }
}

^This is the code where an image gets imported

package exikle.learn.game.gfx;

public class Colours {

    public static int get(int colour1, int colour2, int colour3, int colour4) {
        return (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;
    }
}

^ and the code which i think deals with all the colors but im kinda confused about this.

My question is how do i remove the color modifier and just import and display the sprite sheet as is, so with the color it already has?

War es hilfreich?

Lösung

So you're fiddling with the Minicraft source, I see. The thing about Notch's code is that he substantially limited himself technically in this game. What the engine is doing is basically saying every sprite/tile can have 4 colors (from the grey-scaled spritesheet), he generates his own color palette that he retrieves colors from and sets accordingly during rendering. I can't remember exactly how many bits per channel he set and such.

However, you obviously are very new to programming and imo there's nothing better than fiddling with and analyzing other people's code.. that is, if you actually can do so. The Screen class is where the rendering takes place and hence it's what uses the spritesheet and therefore gives color accordingly to whatever tile you tell it to get. Markus is quite clever, despite poorly written code (which is completely forgiven as he did have 48 hours to make the damned thing ;))

if you want to just display the spritesheet as is, you can either rewrite the render function or overload it to something like this... (in class Screen)

public void render() {
     for(int y = 0; y < h; y++) {
          if(y >= sheet.h) continue; //prevent going out of bounds on y-axis
          for(int x = 0; x < w; x++) {
              if(x >= sheet.w) continue; //prevent going out of bounds on x-axis
                  pixels[x + y * w] = sheet.pixels[x + y * sheet.w];
          }
     }
}

This will just put whatever of the sheet it can fit into the screen for rendering (it's a really simple piece of code, but should work), the next step will be copying the pixels over to the actual raster for display, which I'm sure you can handle. (If you have copy-pasted all of the minicraft source code or some other slightly modified source code, you might want to change some things about that as well.)

All the cheers!

Andere Tipps

This basics would be to replace the get(int) method...

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;
    return colour;
}

I'd also get rid of

for (int i = 0; i < pixels.length; i++) {
    pixels[i] = (pixels[i] & 0xff) / 64;
}

From the main method

But to be honest, wouldn't it be easier to simply use BufferedImage#getSubImage?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top