Advertisement
Anim8

Main Class - Virtual Pet

Aug 3rd, 2013
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. package com.anim8.engine;
  2.  
  3.  
  4. /*
  5.  * public static int windowWidth = 427;
  6.     public static int windowHeight = 640;
  7.         public static String gameName = "Burch-A-Gotchi";
  8.                         gameState.render();
  9.  
  10.  
  11.  */
  12. import org.lwjgl.LWJGLException;
  13. import org.lwjgl.Sys;
  14. import org.lwjgl.opengl.Display;
  15. import org.lwjgl.opengl.DisplayMode;
  16. import org.lwjgl.opengl.GL11;
  17.  
  18. public class main {
  19.  
  20.     public enum State {
  21.         OPENING_CREDITS, MAIN_MENU, GAME,
  22.     }
  23.     private static State state = State.GAME;
  24.    
  25.     public static int windowWidth = 427;
  26.     public static int windowHeight = 640;
  27.     public static String gameName = "Burch-A-Gotchi";
  28.    
  29.    
  30.     /** time at last frame */
  31.     long lastFrame;
  32.  
  33.     /** frames per second */
  34.     int fps;
  35.     /** last fps time */
  36.     long lastFPS;
  37.  
  38.     public void start() {
  39.         try {
  40.             Display.setDisplayMode(new DisplayMode(windowWidth, windowHeight));
  41.             Display.create();
  42.         } catch (LWJGLException e) {
  43.             e.printStackTrace();
  44.             System.exit(0);
  45.         }
  46.  
  47.         initGL(); // init OpenGL
  48.         getDelta(); // call once before loop to initialise lastFrame
  49.         lastFPS = getTime(); // call before loop to initialise fps timer
  50.  
  51.         while (!Display.isCloseRequested()) {
  52.  
  53.             int delta = getDelta();
  54.            
  55.             switch(state){
  56.             case OPENING_CREDITS:
  57.                
  58.                 break;
  59.             case MAIN_MENU:
  60.                 break;
  61.             case GAME:
  62.                 gameState.render();
  63.                 break;
  64.         }
  65.  
  66.             update();
  67.            
  68.             glFlush();
  69.             Display.update();
  70.             Display.sync(60); // cap fps to 60fps
  71.         }
  72.  
  73.         Display.destroy();
  74.     }
  75.  
  76.     private void glFlush() {
  77.         // TODO Auto-generated method stub
  78.        
  79.     }
  80.  
  81.     public void update() {
  82.  
  83.         updateFPS(); // update FPS Counter
  84.     }
  85.  
  86.     /**
  87.      * Calculate how many milliseconds have passed
  88.      * since last frame.
  89.      *
  90.      * @return milliseconds passed since last frame
  91.      */
  92.     public int getDelta() {
  93.         long time = getTime();
  94.         int delta = (int) (time - lastFrame);
  95.         lastFrame = time;
  96.  
  97.         return delta;
  98.     }
  99.  
  100.     /**
  101.      * Get the accurate system time
  102.      *
  103.      * @return The system time in milliseconds
  104.      */
  105.     public long getTime() {
  106.         return (Sys.getTime() * 1000) / Sys.getTimerResolution();
  107.     }
  108.  
  109.     /**
  110.      * Calculate the FPS and set it in the title bar
  111.      */
  112.     public void updateFPS() {
  113.         if (getTime() - lastFPS > 1000) {
  114.             Display.setTitle(gameName + " | FPS: " + fps);
  115.             fps = 0;
  116.             lastFPS += 1000;
  117.         }
  118.         fps++;
  119.     }
  120.  
  121.     public void initGL() {
  122.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  123.         GL11.glLoadIdentity();
  124.         GL11.glOrtho(0, windowWidth, windowHeight, 0,  1, -1);
  125.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  126.         GL11.glEnable(GL11.GL_TEXTURE_2D);
  127.         GL11.glEnable(GL11.GL_BLEND);
  128.         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  129.     }
  130.  
  131.  
  132.     public static void main(String[] argv) {
  133.         main main = new main();
  134.         main.start();
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement