[英]Drawing individual pixels LibGDX
I am writing a program using LibGDX in Java and I want to update the screen every 10 frames with new pixels on the screen. 我正在使用Java中的LibGDX编写程序,我希望每隔10帧用屏幕上的新像素更新屏幕。 I have tried using a pixelmaps but it does't seem to work.
我尝试过使用像素图,但似乎无法正常工作。 Here is my code:
这是我的代码:
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();
}
}
}
}
Whenever I run this it just returns a black screen. 每当我运行它,它只会返回一个黑屏。 Thanks in advance.
提前致谢。
There is a problem in your code: You have a while(true)
in render
but never break
. 您的代码中存在一个问题:您在
render
有一段while(true)
但从不break
。
That is not the only problem, though. 不过,这不是唯一的问题。 You are drawing a pixels to the
Pixmap
, but you are never rendering the Pixmap
itself. 您正在为
Pixmap
绘制像素,但您永远不会渲染Pixmap
本身。 You need to create a Texture
from your Pixmap
: 您需要从
Pixmap
创建Texture
:
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();
}
}
}
}
I have commented things I've changed (except for the private
modifiers at the beginning of the class) but left a few problematic parts of your code intact. 我已经评论了我已经改变的东西(除了课程开头的
private
修饰符),但是代码的一些有问题的部分保持不变。
You should really add a condition for your while
loop, it's currently infinite. 你应该为你的
while
循环添加一个条件,它目前是无限的。 Also, you should really not create a new Pixmap
every time the code is run, this is not only bad design but also wasteful. 此外,每次运行代码时都不应该创建新的
Pixmap
,这不仅设计糟糕而且浪费时间。
If I missed anything I'll update my answer. 如果我错过了什么,我会更新我的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.