Bob the Builder
Bob the Builder

Reputation: 43

JSplitPane re-executes initGL()

I'm trying to make a GUI-based game engine (for learning with experience)

I have my initGL() method here

@Override
public void initGL() {
    createCapabilities();
    glEnable(GL_DEPTH_TEST);
    glCullFace(GL_BACK);
    Main.init();
    try {
        Main.main(null);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

(This is just for testing i will improve it later) The main class was my class which used GLFW before I tried making it with Swing, I didn't want to move everything so that is how I left it. The issue arises when I try to use a JSplitPane. I'm making a GUI based game engine like I said, so customizing the size of each window would be useful. But whenever I try resizing the window, it restarts OpenGL or at least executes the initGL() method again.

How would I know that it's getting executed again? My Main.main(null) method creates a JFrame which contains JSliders to modify certain properties of rendering. Everytime I resize the window it opens another one of those JFrames

And for all those who want a MRE, I've customized the sample provided on their GitHub page to add a JSplitPane

import static org.lwjgl.opengl.GL.createCapabilities;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2f;
import static org.lwjgl.opengl.GL11.glViewport;
import static org.lwjgl.opengl.GL11.*;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.awt.AWTGLCanvas;
import org.lwjgl.opengl.awt.GLData;

public class AWTTest {
    
    public static void main(String[] args) {
        JFrame frame = new JFrame("AWT test");
        JPanel emptyPanel = new JPanel();
        JSplitPane splitPane;
        GLData data = new GLData();
        AWTGLCanvas canvas;
        
        emptyPanel.add(new JButton("E"));

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setPreferredSize(new Dimension(600, 600));
        frame.add(canvas = new AWTGLCanvas(data) {
            private static final long serialVersionUID = 1L;

            @Override
            public void initGL() {
                System.out.println("OpenGL version: " + effective.majorVersion + "." + effective.minorVersion
                        + " (Profile: " + effective.profile + ")");
                createCapabilities();
                glClearColor(0.3f, 0.4f, 0.5f, 1);
            }

            @Override
            public void paintGL() {
                int w = 600;
                int h = 600;
                float aspect = (float) w / h;
                double now = System.currentTimeMillis() * 0.001;
                float width = (float) Math.abs(Math.sin(now * 0.3));
                glClear(GL_COLOR_BUFFER_BIT);
                glViewport(0, 0, w, h);
                glBegin(GL_QUADS);
                glColor3f(0.4f, 0.6f, 0.8f);
                glVertex2f(-0.75f * width / aspect, 0.0f);
                glVertex2f(0, -0.75f);
                glVertex2f(+0.75f * width / aspect, 0);
                glVertex2f(0, +0.75f);
                glEnd();
                swapBuffers();
            }
        }, BorderLayout.CENTER);

        splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, emptyPanel, canvas);

        frame.add(splitPane);
        frame.pack();
        frame.setVisible(true);
        frame.transferFocus();

        Runnable renderLoop = new Runnable() {
            @Override
            public void run() {
                canvas.render();
                SwingUtilities.invokeLater(this);
            }
        };
        SwingUtilities.invokeLater(renderLoop);
    }
}

Thank you (This is reposted from GitHub as i was not getting any response)

Upvotes: 1

Views: 38

Answers (0)

Related Questions