Java tutorial
/* * Copyright 2014 TriBlade9 <triblade9@mail.com>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.kubin.game; import de.matthiasmann.twl.GUI; import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer; import de.matthiasmann.twl.theme.ThemeManager; import java.io.IOException; import java.lang.reflect.Field; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; import net.kubin.Kubin; import net.kubin.gui.GUIManager; import net.kubin.gui.MainMenu; import org.lwjgl.LWJGLException; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; /** * * @author TriBlade9 <triblade9@mail.com> */ public class StartupManager { public void init() { Kubin._fpsDataBuffer = new int[16]; Kubin._fps = Kubin.getConfiguration().getFPS(); try { Display.setTitle("Kubin - " + Kubin.VERSION); setDisplayMode(Kubin.getConfiguration().getWidth(), Kubin.getConfiguration().getHeight(), Kubin.getConfiguration().isFullscreen()); if (Kubin.getConfiguration().getVSync()) { Display.setVSyncEnabled(true); } Display.create(); } catch (LWJGLException e) { e.printStackTrace(); System.exit(0); } //initOpenGL(); openMainMenu(); } /** * Set the display mode to be used * * @param width * The width of the display required * @param height * The height of the display required * @param fullscreen * True if we want fullscreen mode */ public void setDisplayMode(int width, int height, boolean fullscreen) { // return if requested DisplayMode is already set if ((Display.getDisplayMode().getWidth() == width) && (Display.getDisplayMode().getHeight() == height) && (Display.isFullscreen() == fullscreen)) { return; } try { DisplayMode targetDisplayMode = null; if (fullscreen) { DisplayMode[] modes = Display.getAvailableDisplayModes(); int freq = 0; for (DisplayMode current : modes) { if ((current.getWidth() == width) && (current.getHeight() == height)) { if ((targetDisplayMode == null) || (current.getFrequency() >= freq)) { if ((targetDisplayMode == null) || (current.getBitsPerPixel() > targetDisplayMode.getBitsPerPixel())) { targetDisplayMode = current; freq = targetDisplayMode.getFrequency(); } } // if we've found a match for bpp and frequence against // the // original display mode then it's probably best to go // for this one // since it's most likely compatible with the monitor if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel()) && (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())) { targetDisplayMode = current; break; } } } } else { targetDisplayMode = new DisplayMode(width, height); } if (targetDisplayMode == null) { System.out.println("Failed to find value mode: " + width + "x" + height + " fs=" + fullscreen); return; } Display.setDisplayMode(targetDisplayMode); Display.setFullscreen(fullscreen); Display.setResizable(true); } catch (LWJGLException e) { System.out.println("Unable to setup mode " + width + "x" + height + " fullscreen=" + fullscreen + e); } } public void openMainMenu() { MainMenu menu = new MainMenu(); try { GUIManager.renderer = new LWJGLRenderer(); } catch (LWJGLException ex) { Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex); } GUIManager.gui = new GUI(menu, GUIManager.renderer); //menu.efName.requestKeyboardFocus(); try { GUIManager.theme = ThemeManager.createThemeManager(MainMenu.class.getResource("/UITheme/Theme.xml"), GUIManager.renderer); //MainMenu.class.getResource("menu.xml"), GUIManager.renderer); } catch (IOException ex) { Logger.getLogger(StartupManager.class.getName()).log(Level.SEVERE, null, ex); } GUIManager.gui.applyTheme(GUIManager.theme); menu.timer = 0; if (!Kubin.DISPLAY_SPLASH) { menu.splash = false; menu.timer = 200; } while (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) && GUIManager.requestStop == false) { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); GUIManager.gui.update(); //Display Splash Screen if (menu.timer == 200) { menu.splash = false; menu.layout(); menu.timer = -1; } else if (menu.timer != -1) { menu.timer += 1; } //Handle Resizing if (Display.wasResized()) { handleResize(); } Display.update(); } GUIManager.gui.destroy(); GUIManager.theme.destroy(); //Display.destroy(); } public void handleResize() { //Prevent negative UI sizes if (Display.getWidth() < 250) { return; } if (Display.getHeight() < 250) { return; } //Resize the UI and Viewport Kubin.getConfiguration().setDisplaySettings(Display.getWidth(), Display.getHeight(), Display.isFullscreen()); GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight()); GUIManager.renderer.setViewport(0, 0, Display.getWidth(), Display.getHeight()); } }