1
General discussions / SFML 2.0
« on: January 14, 2011, 05:40:42 pm »
Cool. Don't get bitten by the dirty Java
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#define SPRITES 50000
#define randf() ((float)rand() / RAND_MAX)
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "bench");
// Load a sprite to display
sf::Image image;
if (!image.LoadFromFile("badlogicsmall.jpg"))
return EXIT_FAILURE;
sf::Image image2;
if (!image2.LoadFromFile("bob.png"))
return EXIT_FAILURE;
sf::Sprite* sprites[SPRITES];
for(int i = 0; i < SPRITES; i++) {
sf::Sprite* sprite;
if(i < SPRITES / 2) {
sprite = new sf::Sprite(image);
sprite->SetBlendMode(sf::Blend::None);
sprite->SetPosition(randf() * 800, randf() * 600);
sprite->SetOrigin(16, 16);
} else {
sprite = new sf::Sprite(image2);
sprite->SetBlendMode(sf::Blend::Alpha);
sprite->SetPosition(randf() * 800, randf() * 600);
sprite->SetOrigin(16, 16);
}
sprites[i] = sprite;
}
sf::Clock clock;
float timePassed = 0;
unsigned frames = 0;
while (window.IsOpened())
{
sf::Event event;
while (window.GetEvent(event)) {
if (event.Type == sf::Event::Closed)
window.Close();
}
float elapsed = clock.GetElapsedTime();
float scale = timePassed>0.5?1-timePassed / 2:0.5f + timePassed / 2;
clock.Reset();
window.Clear();
for(int i = 0; i < SPRITES; i++) {
sprites[i]->SetRotation(sprites[i]->GetRotation() + elapsed * 45);
sprites[i]->SetScale(scale, scale);
window.Draw(*sprites[i]);
}
window.Display();
timePassed += elapsed;
frames++;
if(timePassed > 1.0f) {
printf("fps: %d\n", frames);
timePassed = 0;
frames = 0;
}
}
return EXIT_SUCCESS;
}
package com.badlogic.gdx.tests;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.tests.utils.GdxTest;
public class SpritePerformanteTest2 extends GdxTest {
static final int SPRITES = 50000;
Texture image;
Texture image2;
Sprite[] sprites;
SpriteBatch batch;
float timePassed = 0;
int frames = 0;
@Override public void create() {
image = Gdx.graphics.newTexture(Gdx.files.internal("data/badlogicsmall.jpg"),
TextureFilter.Linear, TextureFilter.Linear,
TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
image2 = Gdx.graphics.newTexture(Gdx.files.internal("data/bobargb8888-32x32.png"),
TextureFilter.Linear, TextureFilter.Linear,
TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
sprites = new Sprite[SPRITES];
for(int i = 0; i < SPRITES; i++) {
Sprite sprite = new Sprite(i < SPRITES / 2?image:image2);
sprite.setPosition((float)Math.random() * 800, (float)Math.random() * 600);
sprites[i] = sprite;
}
batch = new SpriteBatch();
}
@Override public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
float elapsed = Gdx.graphics.getDeltaTime();
float scale = timePassed>0.5?1-timePassed / 2:0.5f + timePassed / 2;
batch.begin();
batch.disableBlending();
for(int i = 0; i < SPRITES / 2; i++) {
sprites[i].setRotation(sprites[i].getRotation() + elapsed * 45);
sprites[i].setScale(scale, scale);
sprites[i].draw(batch);
}
batch.end();
batch.begin();
batch.enableBlending();
for(int i = SPRITES / 2; i < SPRITES; i++) {
sprites[i].setRotation(sprites[i].getRotation() + elapsed * 45);
sprites[i].setScale(scale, scale);
sprites[i].draw(batch);
}
batch.end();
timePassed += elapsed;
frames++;
if(timePassed > 1.0f) {
Gdx.app.log("SpritePerformanceTest2", "fps: " + frames);
timePassed = 0;
frames = 0;
}
}
@Override public boolean needsGL20 () {
return false;
}
}
SFML:
fps: 7
fps: 6
fps: 6
fps: 6
fps: 6
fps: 6
Libgdx:
SpritePerformanceTest2: fps: 24
SpritePerformanceTest2: fps: 24
SpritePerformanceTest2: fps: 24
SpritePerformanceTest2: fps: 24
SpritePerformanceTest2: fps: 24
SpritePerformanceTest2: fps: 24
SpritePerformanceTest2: fps: 24
SpritePerformanceTest2: fps: 24
java -jar bench.jar