Multithreading in lwjgl getting rid of sleep.
Posted
by
pangaea
on Game Development
See other posts from Game Development
or by pangaea
Published on 2013-06-27T21:40:26Z
Indexed on
2013/06/27
22:29 UTC
Read the original article
Hit count: 256
I'm trying to use multithreading in my game. However, I can't seem to get rid of the sleep.
If I don't it's a blank screen, as there is no time for the computer to actually render the triangleMob as it can't access getArrayList(), in my main class I have a TriangleMob arraylist. If I delay it, then it can access the previousMob and it renders. If I don't, then it's blank screen.
Can I get rid of the delay? Also, is this a bad way to multithread?
Surely, this should be fast. I need multithreading so can you please not suggest not using it.
public class TriangleMob extends Thread implements Runnable {
private static int count=0;
private int objectDisplayList;
private static ArrayList<TriangleMob> previousMob = new ArrayList<TriangleMob>();
private static ArrayList<TriangleMob> currentMob = new ArrayList<TriangleMob>();
private static ArrayList<TriangleMob> laterMob = new ArrayList<TriangleMob>();
private Vector3f position = new Vector3f(0f,0f,0f);
private Vector3f movement = new Vector3f(0f,0f,0f);
public TriangleMob()
{
// Create the display list
CreateDisplayList();
count++;
}
public TriangleMob(Vector3f position)
{
// Create the display list
CreateDisplayList();
this.position = position;
count++;
}
private void CreateDisplayList()
{
objectDisplayList = glGenLists(1);
glNewList(objectDisplayList, GL_COMPILE);
{
double topPoint = 0.75;
glBegin(GL_TRIANGLES);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -0.75, -4);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -.75, -4);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -0.75, -4);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -0.75, -6);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -0.75, -6);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -.75, -6);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -0.75, -6);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -.75, -4);
glEnd();
glColor4f(1, 1, 1, 1);
}
glEndList();
}
public static int getCount() {
return count;
}
public Vector3f getMovement() {
return movement;
}
public Vector3f getPosition() {
return position;
}
public synchronized int getObjectList() {
return objectDisplayList;
}
public synchronized static ArrayList<TriangleMob> getArrayList(){
if(previousMob != null) {
return previousMob;
}
previousMob.add(new TriangleMob());
return previousMob;
}
public synchronized void move(Vector3f movement) {
// If you want to move in all 3 axis
position.x += movement.x;
position.y += movement.y;
position.z += movement.z;
}
public synchronized void render() {
glPushMatrix();
glTranslatef(-position.x, -position.y, -position.z);
glCallList(objectDisplayList);
glPopMatrix();
}
public synchronized static void setTriangleMob(ArrayList<TriangleMob> triangleMobSet) {
laterMob = triangleMobSet;
}
private synchronized void setPreTriangleMob(ArrayList<TriangleMob> currentMob2) {
previousMob = currentMob2;
}
public void run(){
while(true) {
if(laterMob == null) {
currentMob = laterMob;
System.out.println("Copying");
}
for(int i=0; i<currentMob.size(); i++) {
currentMob.get(i).move(new Vector3f(0.1f,0.01f,0.01f));
}
setPreTriangleMob(currentMob);
try {
sleep(1L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
© Game Development or respective owner