绘制单个像素LibGDX

时间:2015-06-10 10:24:37

标签: java libgdx

我正在使用Java中的LibGDX编写程序,我希望每隔10帧用屏幕上的新像素更新屏幕。我尝试过使用像素图,但似乎无法正常工作。这是我的代码:

package com.mhsjlw.heat;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Heat extends ApplicationAdapter {

SpriteBatch batch;
Texture img;
private OrthographicCamera camera;

FlirClient flir = new FlirClient("x.x.x.x.x", 2525);
int[] palette = FlirPalette.IRONBOW_PALETTE;
byte[] frame = new byte[4800];

@Override
public void create() {

    flir.run();

    camera = new OrthographicCamera();
    camera.setToOrtho(false, 80, 60);

    Pixmap pixmap = new Pixmap( 80, 60, Format.RGB888);

    pixmap.drawPixel(1, 1);


}

@Override
public void render() {

    camera.update();

    Pixmap pixmap = new Pixmap( 80, 60, Format.RGB888);



    while(true) {

        frame = flir.getFrame();

        for (int row=0; row<60; row++) {
            for (int col=0; col<0; col++) {
                int ndx = row * 80 + col;
                pixmap.drawPixel(col, row, FlirPalette.getRgb888(frame[ndx], palette));
            }
        }

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
}

每当我运行它时,它只会返回一个黑屏。提前谢谢。

1 个答案:

答案 0 :(得分:3)

您的代码存在问题:while(true)中有renderbreakPixmap没有。{/ p>

但这不是唯一的问题。您正在向Pixmap绘制像素,但您永远不会渲染Texture本身。您需要从Pixmap

创建package com.mhsjlw.heat; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Pixmap.Format; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class Heat extends ApplicationAdapter { private SpriteBatch batch; private Texture img; private OrthographicCamera camera; private FlirClient flir = new FlirClient("69.165.165.194", 2525); private int[] palette = FlirPalette.IRONBOW_PALETTE; private byte[] frame = new byte[4800]; @Override public void create() { flir.run(); camera = new OrthographicCamera(); camera.setToOrtho(false, 80, 60); batch = new SpriteBatch(); //this is useless here Pixmap pixmap = new Pixmap( 80, 60, Format.RGB888); pixmap.drawPixel(1, 1); } @Override public void render() { camera.update(); Pixmap pixmap = new Pixmap( 80, 60, Format.RGB888); while(true) { //add a condition! frame = flir.getFrame(); for (int row=0; row<60; row++) { for (int col=0; col<0; col++) { int ndx = row * 80 + col; pixmap.drawPixel(col, row, FlirPalette.getRgb888(frame[ndx], palette)); } } img = new Texture(pixmap); //create Texture from Pixmap pixmap.dispose(); //do not leak memory batch.begin(); batch.draw(img, 100, 100); //draw pixmap texture batch.end(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }
private

我已经评论了我已经更改过的内容(除了课程开头的while修饰符),但是代码的一些有问题的部分保持不变。

你应该为你的Pixmap循环添加一个条件,它目前是无限的。 此外,每次运行代码时都不应该创建新的crypto:start/0,这不仅设计糟糕而且浪费。

如果我遗漏了任何内容,我会更新我的答案。