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 static MainFrame mainFrame;
public static GLCanvas joglFrame;
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
new Thread(() -> {
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();
frame.setVisible(true);
SwingGLCanvas2D canvas = new SwingGLCanvas2D(null);
SwingGLCanvas2D canvas = new SwingGLCanvas2D();
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.gl2.GLUT;
import lsystem.engine.Element;
import lsystem.engine.Parser;
import lsystem.engine.Rewrite;
import lsystem.utils.Pair;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
public abstract class AbstractCanvas {
private final Element lsystem;
private Element lsystem;
public State parsedState = State.FINISH_OR_NULL;
public JFrame frame;
protected FPSAnimator animator;
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)
protected AbstractCanvas(Element parsed) {
this.lsystem = parsed;
protected AbstractCanvas() {
GLProfile glProfile = GLProfile.getDefault();
GLCapabilities glCapabilities = new GLCapabilities(glProfile);
this.glCanvas = new GLCanvas(glCapabilities);
@ -37,6 +41,9 @@ public abstract class AbstractCanvas {
public void windowClosing(WindowEvent e) {
frame.dispose();
setVisible(false);
lsystem = null;
parsedState = State.FINISH_OR_NULL;
System.gc();
}
});
frame.getContentPane().add(glCanvas, BorderLayout.CENTER);
@ -53,4 +60,15 @@ public abstract class AbstractCanvas {
animator.stop();
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;
import lsystem.engine.Element;
import lsystem.screen.AbstractCanvas;
public class SwingGLCanvas2D extends AbstractCanvas {
public SwingGLCanvas2D(Element parsed) {
super(parsed);
}
@Override
protected void addEventsListeners() {
glCanvas.addGLEventListener(new JoglEventListener2D(this));

View File

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

View File

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

View File

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