简体   繁体   English

slick2d图像丢失

[英]slick2d image gets lost

I have a problem with the Slick 2D API. 我对Slick 2D API有问题。 I am building a little Zelda like 2d game. 我正在建立一个类似2D游戏的小塞尔达传说。 So i have built a little testing world. 所以我建立了一个小测试世界。 The world consists of chunks which have an image as floor. 世界由块组成,这些块具有作为地板的图像。 Then inside the render method the first all chunks are drawn, and after the meshes are drawn on top of it. 然后在render方法内部,首先绘制所有块,然后在其顶部绘制网格。

Mainly I use the Image class of slick to draw and modify images on the screen. 我主要使用光滑的Image类在屏幕上绘制和修改图像。

I load the images inside the init() method. 我将图像加载到init()方法中。 In the render() method I call image.draw(x,y) to draw the image. 在render()方法中,我调用image.draw(x,y)绘制图像。 But it seems that the last chunk drawn is not drawn. 但似乎未绘制最后一个块。 Instead a black hole appears. 而是出现一个黑洞。

As a test I tried to write the specific image to the hard disc. 作为测试,我尝试将特定的图像写入硬盘。 It succeeds from the init method, but when I save the image from the render method I get a black image. 它是通过init方法成功完成的,但是当我从render方法保存图像时,则会得到黑色图像。

Somehow the image gets lost after the init method, but the other images remain. 在初始化方法后,图像以某种方式丢失了,但是其他图像仍然保留。

Extra information: When starting to build the game I kept in mind to seperate game logic from system dependent tasks. 额外信息:开始制作游戏时,我谨记将游戏逻辑与系统相关任务分开。 That's why I have an interface for some basic image manipulation. 这就是为什么我有一些基本图像操作界面的原因。 The game logic uses that interface. 游戏逻辑使用该界面。 Then I used java2d for rendering, but the performance was poor. 然后我使用java2d进行渲染,但是性能很差。 Now I switched to slick and I wrote an adapter and it works fine except from "world hole". 现在,我切换到光滑的,我写了一个适配器,除了从“ world hole”开始,它工作正常。

Here is the interface: 这是界面:

public interface ImageRGBA {
public static final int CHANNEL_A = 3;
public static final int CHANNEL_R = 2;
public static final int CHANNEL_G = 1;
public static final int CHANNEL_B = 0;
public static final int CHANNEL_COUNT = 4;

public int getHeight();
public int getWidth();

public void setPixel(int x, int y, int channel, byte value);
public void setPixel(int x, int y, byte[] argb);

public byte getPixel(int x, int y, int channel);
public byte[] getPixel(int x, int y);

public int getPixelARGB(int x, int y);
public void setPixelARGB(int x, int y, int argb);

public void insertImage(int x, int y, ImageRGBA image);
}

And the slick implementation: 精巧的实现:

public class SlickImageAdapter implements ImageRGBA {
private final Image image;

public SlickImageAdapter(Image image){
    this.image = image;
}

@Override
public int getHeight() {
    return this.image.getHeight();
}

@Override
public int getWidth() {
    return this.image.getWidth();
}

@Override
public void setPixel(int x, int y, int channel, byte value) {
    Color color = this.image.getColor(x,y);
    int[] components = new int[ImageRGBA.CHANNEL_COUNT];
    components[ImageRGBA.CHANNEL_R] = color.getRed();
    components[ImageRGBA.CHANNEL_G] = color.getGreen();
    components[ImageRGBA.CHANNEL_B] = color.getBlue();
    components[ImageRGBA.CHANNEL_A] = color.getAlpha();

    components[channel] = value - Byte.MIN_VALUE;

    try {
        Graphics graphics = this.image.getGraphics();
        graphics.setColor(new Color(
                components[ImageRGBA.CHANNEL_R],
                components[ImageRGBA.CHANNEL_G],
                components[ImageRGBA.CHANNEL_B],
                components[ImageRGBA.CHANNEL_A]
                ));
        graphics.fillRect(x, y, 1, 1);
       // graphics.destroy();
    } catch (SlickException e) {
        e.printStackTrace();
    }
}

@Override
public void setPixel(int x, int y, byte[] argb) {
    try {
        Graphics graphics = this.image.getGraphics();
        graphics.setColor(new Color(
                argb[ImageRGBA.CHANNEL_R] - Byte.MIN_VALUE,
                argb[ImageRGBA.CHANNEL_G] - Byte.MIN_VALUE,
                argb[ImageRGBA.CHANNEL_B] - Byte.MIN_VALUE,
                argb[ImageRGBA.CHANNEL_A] - Byte.MIN_VALUE
        ));
        graphics.fillRect(x,y,1,1);
        //graphics.destroy();
    } catch (SlickException e){
        e.printStackTrace();
    }
}

@Override
public byte getPixel(int x, int y, int channel) {
    return this.getPixel(x,y)[channel];
}

@Override
public byte[] getPixel(int x, int y) {
    Color color = this.image.getColor(x,y);
    byte[] bytes = new byte[ImageRGBA.CHANNEL_COUNT];
    bytes[ImageRGBA.CHANNEL_R] = (byte) (color.getRedByte() + Byte.MIN_VALUE);
    bytes[ImageRGBA.CHANNEL_G] = (byte) (color.getGreenByte() + Byte.MIN_VALUE);
    bytes[ImageRGBA.CHANNEL_B] = (byte) (color.getBlueByte() + Byte.MIN_VALUE);
    bytes[ImageRGBA.CHANNEL_A] = (byte) (color.getAlphaByte() + Byte.MIN_VALUE);
    return bytes;
}

@Override
public int getPixelARGB(int x, int y) {
    ImageManager imageManager = Device.getDevice().getImageManager();
    return imageManager.byteArrayToIntARGB(this.getPixel(x,y));
}

@Override
public void setPixelARGB(int x, int y, int argb) {
    ImageManager imageManager = Device.getDevice().getImageManager();
    this.setPixel(x,y, imageManager.intARGBToByteArray(argb));
}

@Override
public void insertImage(int x, int y, ImageRGBA image) {
    try{
        Graphics graphics = this.image.getGraphics();
        graphics.drawImage(((SlickImageAdapter) image).getImage(), x, y);
        //graphics.destroy();
    }catch (SlickException e){
        e.printStackTrace();
    }
}

public Image getImage(){
    return this.image;
}
}

处理图像后,我没有调用Graphics.flush()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM