com.dbi.games.fortress.ui.MainAppWindow.java Source code

Java tutorial

Introduction

Here is the source code for com.dbi.games.fortress.ui.MainAppWindow.java

Source

/*
 Copyright (C) 2014  Nathan Nelson
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
 Author E-mail: falken224@gmail.com
 */
package com.dbi.games.fortress.ui;

import com.dbi.games.fortress.engine.MainEngine;
import com.dbi.games.fortress.engine.graphics.Sprite;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author nnels2
 */
public class MainAppWindow extends javax.swing.JFrame {

    private boolean needValidation = true;
    private boolean needUpdateViewport = true;

    enum OS {
        Mac, Windows, Linux;

        public static OS fromName(String name) {
            if (name == null)
                name = System.getProperty("os.name");
            if (name.toLowerCase().contains("windows"))
                return Windows;
            if (name.toLowerCase().contains("mac"))
                return Mac;
            return Linux;
        }

        public static boolean is64Bit(String archName) {
            if (archName == null)
                archName = System.getProperty("sun.arch.data.model");
            return archName.contains("64");
        }
    }

    private void setNeedValidation() {
        needValidation = true;
        needUpdateViewport = true;
    }

    public Rectangle getViewportBounds() {
        return glCanvas.getBounds();
    }

    /**
     * Creates new form MainAppWin
     */
    public MainAppWindow() {
        initComponents();

        glCanvas.addComponentListener(new ComponentListener() {
            public void componentShown(ComponentEvent e) {
                setNeedValidation();
            }

            public void componentResized(ComponentEvent e) {
                setNeedValidation();
            }

            public void componentMoved(ComponentEvent e) {
                setNeedValidation();
            }

            public void componentHidden(ComponentEvent e) {
                setNeedValidation();
            }
        });

        File currentDir = new File("lib/natives");
        if (!currentDir.exists()) {
            //            JOptionPane.showMessageDialog(null, "directory location: "+currentDir.getAbsolutePath()+" does not exist!");
            currentDir = new File("build/libs/natives");
        }
        Logger.getLogger("LWJGLThread").log(Level.INFO, "LWJGL directory is " + currentDir.getAbsolutePath());

        System.setProperty("org.lwjgl.librarypath", currentDir.getAbsolutePath());

        Thread glThread = new Thread("GLThread") {

            @Override
            public void run() {
                long lastFrame = System.nanoTime();
                long currentFrame;
                try {
                    Display.setParent(glCanvas);
                    Display.create();

                    GL11.glEnable(GL11.GL_TEXTURE_2D);
                    GL11.glEnable(GL11.GL_BLEND);
                    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
                    GL11.glDisable(GL11.GL_DEPTH_TEST);
                    GL11.glClearColor(0f, 0f, 0f, 0f);
                    GL11.glMatrixMode(GL11.GL_PROJECTION);
                    GL11.glLoadIdentity();
                    GL11.glOrtho(0f, (float) glCanvas.getWidth(), (float) glCanvas.getHeight(), 0f, -1, 1);
                    GL11.glMatrixMode(GL11.GL_MODELVIEW);
                    GL11.glLoadIdentity();
                    GL11.glViewport(0, 0, glCanvas.getWidth(), glCanvas.getHeight());

                    MainEngine.get().graphicsEngine().buildDefaultRenderers();

                    while (MainEngine.get().isRunning()) {
                        try {
                            currentFrame = System.nanoTime();

                            MainEngine.get().renderScene(currentFrame - lastFrame);

                            lastFrame = currentFrame;

                            Display.update();

                            if (needUpdateViewport) {
                                needUpdateViewport = false;

                                GL11.glMatrixMode(GL11.GL_PROJECTION);
                                GL11.glLoadIdentity();
                                GL11.glOrtho(0, glCanvas.getWidth(), glCanvas.getHeight(), 0, -1, 1);
                                GL11.glViewport(0, 0, glCanvas.getWidth(), glCanvas.getHeight());
                            }
                        } catch (Throwable ex) {
                            ex.printStackTrace();
                            Logger.getLogger("LWJGLThread").log(Level.SEVERE,
                                    "There was a SEROUS uncaught error in the render thread.  Shutting down.  Please restart!",
                                    ex);
                            MainEngine.get().shutdown();
                        }
                    }
                } catch (Throwable ex) {
                    throw new RuntimeException("There was an issue starting LWJGL!", ex);
                }
            }

        };
        glThread.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        glCanvas = new java.awt.Canvas();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnuWindow = new javax.swing.JMenu();
        mnuItemShowLog = new javax.swing.JCheckBoxMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mnuItemQuit = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setPreferredSize(new java.awt.Dimension(800, 600));
        setResizable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(glCanvas,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(glCanvas,
                        javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

        mnuWindow.setText("Window");

        mnuItemShowLog.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L,
                java.awt.event.InputEvent.CTRL_MASK));
        mnuItemShowLog.setText("Show Log");
        mnuItemShowLog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuItemShowLogActionPerformed(evt);
            }
        });
        mnuWindow.add(mnuItemShowLog);
        mnuWindow.add(jSeparator1);

        mnuItemQuit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q,
                java.awt.event.InputEvent.CTRL_MASK));
        mnuItemQuit.setText("Quit");
        mnuItemQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuItemQuitActionPerformed(evt);
            }
        });
        mnuWindow.add(mnuItemQuit);

        jMenuBar1.add(mnuWindow);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnuItemQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuItemQuitActionPerformed
        MainEngine.get().shutdown();
    }//GEN-LAST:event_mnuItemQuitActionPerformed

    private void mnuItemShowLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuItemShowLogActionPerformed
        MainEngine.get().logWindow().setVisible(!MainEngine.get().logWindow().isVisible());
        resetWindowMenuCheckboxes();
    }//GEN-LAST:event_mnuItemShowLogActionPerformed

    public void resetWindowMenuCheckboxes() {
        MainEngine engine = MainEngine.get();
        mnuItemShowLog.setSelected(engine.logWindow().isVisible());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Canvas glCanvas;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JMenuItem mnuItemQuit;
    private javax.swing.JCheckBoxMenuItem mnuItemShowLog;
    private javax.swing.JMenu mnuWindow;
    // End of variables declaration//GEN-END:variables
}