greatly improved performance

This commit is contained in:
Quentin Legot 2021-03-09 20:13:58 +01:00
parent dcb4ad767b
commit 4129724064
7 changed files with 52 additions and 31 deletions

View File

@ -5,9 +5,15 @@ import lsystem.screen.main.MainFrame;
public class Main { public class Main {
public static MainFrame mainFrame;
public static GLCanvas joglFrame;
public static void main(String[] args) { public static void main(String[] args) {
MainFrame frame = new MainFrame(); new Thread(() -> {
frame.setVisible(true); mainFrame = new MainFrame();
mainFrame.setVisible(true);
}).start();
new Thread(() -> joglFrame = new GLCanvas()).start();
} }
} }

View File

@ -9,7 +9,7 @@ public class Main2D {
MainFrame frame = new MainFrame(); MainFrame frame = new MainFrame();
frame.setVisible(true); frame.setVisible(true);
SwingGLCanvas2D canvas = new SwingGLCanvas2D(null); SwingGLCanvas2D canvas = new SwingGLCanvas2D();
canvas.setVisible(true); canvas.setVisible(true);
} }

View File

@ -7,15 +7,20 @@ import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.FPSAnimator; import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.gl2.GLUT; import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.engine.Element; import lsystem.engine.Element;
import lsystem.engine.Parser;
import lsystem.engine.Rewrite;
import lsystem.utils.Pair;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.util.List;
public abstract class AbstractCanvas { public abstract class AbstractCanvas {
private final Element lsystem; private Element lsystem;
public State parsedState = State.FINISH_OR_NULL;
public JFrame frame; public JFrame frame;
protected FPSAnimator animator; protected FPSAnimator animator;
public final GLCanvas glCanvas; public final GLCanvas glCanvas;
@ -25,8 +30,7 @@ public abstract class AbstractCanvas {
0f, 0f, 0f}; // camera rotation yaw(x-axis), pitch(y-axis), roll(z-axis) 0f, 0f, 0f}; // camera rotation yaw(x-axis), pitch(y-axis), roll(z-axis)
protected AbstractCanvas(Element parsed) { protected AbstractCanvas() {
this.lsystem = parsed;
GLProfile glProfile = GLProfile.getDefault(); GLProfile glProfile = GLProfile.getDefault();
GLCapabilities glCapabilities = new GLCapabilities(glProfile); GLCapabilities glCapabilities = new GLCapabilities(glProfile);
this.glCanvas = new GLCanvas(glCapabilities); this.glCanvas = new GLCanvas(glCapabilities);
@ -37,6 +41,9 @@ public abstract class AbstractCanvas {
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
frame.dispose(); frame.dispose();
setVisible(false); setVisible(false);
lsystem = null;
parsedState = State.FINISH_OR_NULL;
System.gc();
} }
}); });
frame.getContentPane().add(glCanvas, BorderLayout.CENTER); frame.getContentPane().add(glCanvas, BorderLayout.CENTER);
@ -53,4 +60,15 @@ public abstract class AbstractCanvas {
animator.stop(); animator.stop();
frame.setVisible(bl); frame.setVisible(bl);
} }
public void setLsystem(String axiom, List<Pair<String, String>> rules, int iterations) {
parsedState = State.LOAD;
this.lsystem = Parser.parse(Rewrite.rewrite(axiom, rules, iterations));
parsedState = State.FINISH_OR_NULL;
}
public enum State {
LOAD,
FINISH_OR_NULL;
}
} }

View File

@ -1,15 +1,10 @@
package lsystem.screen.gl2d; package lsystem.screen.gl2d;
import lsystem.engine.Element;
import lsystem.screen.AbstractCanvas; import lsystem.screen.AbstractCanvas;
public class SwingGLCanvas2D extends AbstractCanvas { public class SwingGLCanvas2D extends AbstractCanvas {
public SwingGLCanvas2D(Element parsed) {
super(parsed);
}
@Override @Override
protected void addEventsListeners() { protected void addEventsListeners() {
glCanvas.addGLEventListener(new JoglEventListener2D(this)); glCanvas.addGLEventListener(new JoglEventListener2D(this));

View File

@ -1,15 +1,10 @@
package lsystem.screen.gl3d; package lsystem.screen.gl3d;
import lsystem.engine.Element;
import lsystem.screen.AbstractCanvas; import lsystem.screen.AbstractCanvas;
public class GLCanvas extends AbstractCanvas { public class GLCanvas extends AbstractCanvas {
public GLCanvas(Element parsed) {
super(parsed);
}
@Override @Override
protected void addEventsListeners() { protected void addEventsListeners() {
glCanvas.addGLEventListener(new GLEventListener(this)); glCanvas.addGLEventListener(new GLEventListener(this));

View File

@ -1,8 +1,8 @@
package lsystem.screen.main; package lsystem.screen.main;
import lsystem.Main;
import lsystem.engine.Parser; import lsystem.engine.Parser;
import lsystem.engine.Rewrite; import lsystem.screen.AbstractCanvas;
import lsystem.screen.gl3d.GLCanvas;
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@ -48,24 +48,29 @@ public class Listener implements ActionListener, KeyListener {
String axiom = tab.getAxiom(); String axiom = tab.getAxiom();
List<String> rules = tab.getRules(); List<String> rules = tab.getRules();
Parser parser = new Parser(axiom, rules, tab.getNbIterations()); Parser parser = new Parser(axiom, rules, tab.getNbIterations());
if (!parser.isCorrect()) { if(Main.joglFrame.frame.isVisible()) {
JOptionPane.showMessageDialog(null, "Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); openDialog("Veuillez fermer la fenêtre 3D avant de lancer une nouvelle génération");
new Listener(null, index, "Clear",tab); } else if(Main.joglFrame.parsedState == AbstractCanvas.State.LOAD) {
openDialog("Une génération est actuellement en cours, impossible d'en relancer un autre");
} else if (!parser.isCorrect()) {
openDialog("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer");
} else { } else {
Thread thread = new Thread(() -> { new Thread(() -> {
GLCanvas canvas = new GLCanvas( Main.joglFrame.setLsystem(axiom, parser.parseRules(), tab.getNbIterations());
Parser.parse(Rewrite.rewrite(axiom, parser.parseRules(), tab.getNbIterations())) Main.joglFrame.setVisible(true);
); }).start();
canvas.setVisible(true);
});
thread.setDaemon(true);
thread.start();
} }
break; break;
} }
} }
private void openDialog(String message) {
JOptionPane.showMessageDialog(null, message);
new Listener(null, index, "Clear", tab);
}
@Override @Override
public void keyTyped(KeyEvent ke) { public void keyTyped(KeyEvent ke) {

View File

@ -2,9 +2,11 @@ package lsystem.screen.main;
import lsystem.screen.Constants; import lsystem.screen.Constants;
import java.awt.event.*;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MainFrame extends JFrame { public class MainFrame extends JFrame {
@ -49,7 +51,7 @@ public class MainFrame extends JFrame {
} }
public void newHelp() { public void newHelp() {
if(helpWindow != false){ if(helpWindow){
JOptionPane.showMessageDialog(null, "Une fenêtre d'aide est déjà ouverte."); JOptionPane.showMessageDialog(null, "Une fenêtre d'aide est déjà ouverte.");
} }
else { else {