Reputation:
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. 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.
Upvotes: 1
Views: 3771
Reputation: 1402
There is a problem in your code: You have a while(true)
in render
but never break
.
That is not the only problem, though. You are drawing a pixels to the Pixmap
, but you are never rendering the Pixmap
itself. You need to create a Texture
from your 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();
}
}
}
}
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.
You should really add a condition for your while
loop, it's currently infinite.
Also, you should really not create a new Pixmap
every time the code is run, this is not only bad design but also wasteful.
If I missed anything I'll update my answer.
Upvotes: 2