1
Java / Obligatory 'loading resources on a thread' post.
« on: January 29, 2013, 10:49:26 pm »
Hello there,
I'm attempting to use a worker thread load textures and subsequently use these textures on the main thread but I'm running into a problem, specifically that these textures do not appear to be rendering.
I've included a minimal example that reproduces the problem below. I wonder if anyone can provide some insight into what may be going wrong here?
I'm using the January 29th JSFML test release on Windows (64 bit) with Intel HD Graphics 4000 integrated graphics.
I'm attempting to use a worker thread load textures and subsequently use these textures on the main thread but I'm running into a problem, specifically that these textures do not appear to be rendering.
I've included a minimal example that reproduces the problem below. I wonder if anyone can provide some insight into what may be going wrong here?
I'm using the January 29th JSFML test release on Windows (64 bit) with Intel HD Graphics 4000 integrated graphics.
import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.ConcurrentHashMap;
import org.jsfml.graphics.*;
import org.jsfml.window.*;
import org.jsfml.window.event.Event;
public class Example {
// Texture cache
public static final ConcurrentHashMap<String, ConstTexture> cache =
new ConcurrentHashMap<String, ConstTexture>();
private static final String KEY = "key_string";
public static void main(String[] args) {
// Initialise window
RenderWindow renderWindow = new RenderWindow();
renderWindow.create(new VideoMode(800, 600), "Eg.");
// On another thread, create a Texture from an image file then cache it
Thread worker = new Thread(new Runnable() {
public void run() {
Texture texture = new Texture();
try {
texture.loadFromFile(Paths.get("canyonlands_large.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
cache.put(KEY, texture);
}
});
worker.start();
// Wait for the image to be loaded and
while (worker.isAlive()) {
//...Show loading screen...
}
// Create a sprite using the loaded texture
Sprite sprite = new Sprite(cache.get(KEY));
// Main loop
while (renderWindow.isOpen()) {
// Handle events
for (Event e : renderWindow.pollEvents()) {
if (e.type == Event.Type.CLOSED)
renderWindow.close();
}
// Draw sprite
renderWindow.clear(Color.BLACK);
renderWindow.draw(sprite);
renderWindow.display();
}
}
}
import java.nio.file.Paths;
import java.util.concurrent.ConcurrentHashMap;
import org.jsfml.graphics.*;
import org.jsfml.window.*;
import org.jsfml.window.event.Event;
public class Example {
// Texture cache
public static final ConcurrentHashMap<String, ConstTexture> cache =
new ConcurrentHashMap<String, ConstTexture>();
private static final String KEY = "key_string";
public static void main(String[] args) {
// Initialise window
RenderWindow renderWindow = new RenderWindow();
renderWindow.create(new VideoMode(800, 600), "Eg.");
// On another thread, create a Texture from an image file then cache it
Thread worker = new Thread(new Runnable() {
public void run() {
Texture texture = new Texture();
try {
texture.loadFromFile(Paths.get("canyonlands_large.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
cache.put(KEY, texture);
}
});
worker.start();
// Wait for the image to be loaded and
while (worker.isAlive()) {
//...Show loading screen...
}
// Create a sprite using the loaded texture
Sprite sprite = new Sprite(cache.get(KEY));
// Main loop
while (renderWindow.isOpen()) {
// Handle events
for (Event e : renderWindow.pollEvents()) {
if (e.type == Event.Type.CLOSED)
renderWindow.close();
}
// Draw sprite
renderWindow.clear(Color.BLACK);
renderWindow.draw(sprite);
renderWindow.display();
}
}
}