Bonjour,
Je suis en train de développer un éditeur de map en mélangeant swt et opengl. Pour le moment j'affiche juste une liste à gauche avec les futurs apercus des textures, une liste à droite vide et un composite au milieu avec un GLCanvas pour pouvoir dessiner ma map à l'intérieur. Je me contente de dessiner seulement les cases de la map en mode polygone mais lorsque j'essaie de me déplacer sur la map grâce aux touches droite/gauche/haut/bas il se produit un lag et ca fait des à coups.
Voila la classe map qui s'occupe de gérer l'affiche de la carte dans le composite du milieu.
Si quelqu'un a déjà eu un problème similaire ou a une idée d'ou cela peut venir, je le remercie de lire la suite :)
public class Map {
GLCanvas canvas;
int width;
int height;
static double tan60 = 1.73;
float x = 0, y = 0, vx = 0, vy = 0;
long fps = 50;
void drawCase(float x, float y, float z, float d)
{
GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
GL11.glPushMatrix();
GL11.glTranslatef(x, y, z);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2d(0 - (tan60 * d) / 2.0, 0 + d / 2);
GL11.glVertex2d(0, 0);
GL11.glVertex2d(0 + (tan60 * d) / 2.0, 0 + d / 2);
GL11.glVertex2d(0, 0 + d);
GL11.glEnd();
GL11.glPopMatrix();
// GL11.glPolygonMode(0, 0);
// GL11.glDisable(GL11.GL_POLYGON_MODE);
}
void init(Composite comp)
{
GLData data = new GLData ();
data.doubleBuffer = true;
canvas = new GLCanvas(comp, SWT.NONE, data);
canvas.setCurrent();
try
{
GLContext.useContext(canvas);
}
catch (LWJGLException e)
{
e.printStackTrace();
}
GL11.glDisable(GL11.GL_DEPTH_TEST);
}
Map(Composite comp)
{
init(comp);
//
// Si il y a une resize on remet à jour l'écran.
//
canvas.addListener(SWT.Resize, new Listener()
{
public void handleEvent(Event event)
{
Rectangle bounds = canvas.getBounds();
canvas.setCurrent();
try
{
GLContext.useContext(canvas);
}
catch (LWJGLException e)
{
e.printStackTrace();
}
GL11.glViewport(0, 0, bounds.width, bounds.height);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluOrtho2D(0.0f, bounds.width, 0.0f, bounds.height);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
}
});
//
// Pour se déplacer sur la map on check les touches pressées.
//
canvas.addKeyListener(new KeyListener ()
{
public void keyPressed(KeyEvent key)
{
if (key.keyCode == SWT.ARROW_RIGHT)
vx = -5;
if (key.keyCode == SWT.ARROW_LEFT)
vx = 5;
if (key.keyCode == SWT.ARROW_DOWN)
vy = 5;
if (key.keyCode == SWT.ARROW_UP)
vy = -5;
}
public void keyReleased(KeyEvent key)
{
if (key.keyCode == SWT.ARROW_RIGHT)
vx = 0;
if (key.keyCode == SWT.ARROW_LEFT)
vx = 0;
if (key.keyCode == SWT.ARROW_DOWN)
vy = 0;
if (key.keyCode == SWT.ARROW_UP)
vy = 0;
}
});
//
// Affichage de la map
//
Display.getCurrent().asyncExec(new Runnable()
{
int rot = 0;
public void run()
{
if (!canvas.isDisposed())
{
canvas.setCurrent();
try
{
GLContext.useContext(canvas);
}
catch (LWJGLException e)
{
e.printStackTrace();
}
long currentTime1 = System.currentTimeMillis();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glLoadIdentity();
GL11.glTranslatef(0.0f, 0.0f, 0.0f);
float d = 60.0f;
int pair = 0;
x += vx;
y += vy;
drawCase(x, y, 0, d);
canvas.swapBuffers();
Display.getCurrent().asyncExec(this);
// Synchronisation FPS
long currentTime2 = System.currentTimeMillis();
long res = 1000 / fps - currentTime2 - currentTime1;
if (res > 0)
{
try
{
wait(res);
}
catch (InterruptedException ex)
{
Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
});
}
}
--
Marie