Hey all, I hope someone is able to shed some light on a thing that baffles me. So far I’ve been using slick2d to build a game, which is nice because it gives me hardware accelleration and all. However, I just played around a bit with libdgx, and the way I’m doing things it appears libdgx is about 12x faster! I’m not sure if this is because I’m using slick2d wrong, my test is flawed, or whether there simply is such a difference. Can anyone help me with this?
Here’s some code I used to test this. Both run at roughly the same FPS on my machine, even though the libgdx one draws 12 x as many images. I draw the images in a circle so every image is drawn visibly to avoid differences in clipping or whatever (just so every image is actually rendered in the viewport and not all on the same spot).
Slick2d:
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
public class SlickTest extends BasicGame {
private Image image;
private long lastFpsTime;
private float angle;
public SlickTest() {
super("Test");
}
@Override
public void render(GameContainer container, Graphics graphics) throws SlickException {
for (int i = 0; i < 2500; i++) {
angle += 6.28f / 200f;
image.draw(200 + (float) Math.cos(angle) * 100f, 200 + (float) Math.sin(angle) * 100f);
}
}
@Override
public void init(GameContainer container) throws SlickException {
image = new Image("image/test.png");
lastFpsTime = System.nanoTime();
}
@Override
public void update(GameContainer container, int delta) throws SlickException {
if (System.nanoTime() - lastFpsTime > 1000000000) {
System.out.println(container.getFPS());
lastFpsTime = System.nanoTime();
}
}
public static void main(String[] args) throws SlickException {
AppGameContainer app = new AppGameContainer(new SlickTest());
app.setShowFPS(false);
app.setDisplayMode(800, 600, false);
app.start();
}
}
And for libgdx:
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class ConstellationControl implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
long lastFpsTime;
private float angle;
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(w, h);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/test.png"));
sprite = new Sprite(texture);
lastFpsTime = System.nanoTime();
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
@Override
public void render() {
if (System.nanoTime() - lastFpsTime > 1000000000) {
System.out.println(Gdx.graphics.getFramesPerSecond());
lastFpsTime = System.nanoTime();
}
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
for (int i = 0; i < 30000; i++) {
angle += 6.28f / 200f;
sprite.setPosition(100 + (float) Math.cos(angle) * 100f, 100 + (float) Math.sin(angle) * 100f);
sprite.draw(batch);
}
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "constellationcontrol";
cfg.useGL20 = false;
cfg.width = 800;
cfg.height = 600;
new LwjglApplication(new ConstellationControl(), cfg);
}
}