Package ast

Source Code of ast.Game

package ast;

import org.lwjgl.*;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.*;
import org.lwjgl.util.glu.GLU;

import ast.StateManager.State;
import ast.level.Level;
import ast.ui.MouseCursor;


import static org.lwjgl.opengl.GL11.*;

public class Game {

  public static int SCREEN_WIDTH = 1280, SCREEN_HEIGHT = 1024;
 
  private boolean running = true;
  private boolean fullscreen = false;
 
  private long lastFrame;
  private int fps;
  private long lastFPS;
  private Level level;
  DisplayMode displayMode;
 
 
  public static void main(String[] args){
    Game game = new Game();
    game.start();
  }

  /**
   * start game
   */
  private void start() {
   
    try {
      DisplayMode d[] = Display.getAvailableDisplayModes();
     
     
//      for(int i=0; i < d.length; i++){
//        System.out.println(i + ": " + d[i].getWidth() + "x" + d[i].getHeight());
//      }
     
      for(int i = 0; i < d.length; i++){
        if(d[i].getWidth() == SCREEN_WIDTH
            && d[i].getHeight() == SCREEN_HEIGHT
            && d[i].getBitsPerPixel() == 32){
          displayMode = d[i];
          break;
        }
      }
     
      Display.setDisplayMode(displayMode);
      Display.create()
    } catch (LWJGLException e) {
      e.printStackTrace();
    }
   
    initGL();
    getDelta();
    lastFPS = getTime();   
   
    AssetStore.load();
    level = new Level();
    level.init(this);
    StateManager.init(level, this);
   
    MouseCursor c = new MouseCursor();
    try {
      Mouse.setNativeCursor(c.cursor);
    } catch (LWJGLException e) {
      e.printStackTrace();
    }
   
    mainLoop();
   
    Display.destroy();
  }
 
  /**
   * start game loop
   */
  private void mainLoop(){
    while(running){
      int delta = getDelta();     
     
      update(delta);
      renderGL();
     
      if(Display.isCloseRequested()){ 
        System.exit(0);
      }
    }
  }
 
  /**
   *
   * @param delta time since last update(ms)
   */
  private void update(int delta) {
    updateFPS();
    //level.update(delta);
    StateManager.update(delta);
   
    if(Keyboard.isKeyDown(Keyboard.KEY_F1)){
      switchMode();
    }
   
    if(Keyboard.isKeyDown(Keyboard.KEY_R)){
      reset();
    }
  }
  /**
   * render game
   */
  private void renderGL() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    //level.render();
    StateManager.render();
    Display.update();
    Display.sync(100);
  }

  /**
   * Setup OpenGL
   */
  private void initGL(){
   
    glEnable(GL_TEXTURE_2D);   
    glEnable(GL_BLEND);
   
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
   
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
   
   
      //Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(
          45.0f,
          (float) displayMode.getWidth() / (float) displayMode.getHeight(),
          0.1f,
          300.0f);
   
    glMatrixMode(GL_MODELVIEW);
   
    // Really Nice Perspective Calculations
        glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
      
       
        glEnable(GL_LIGHT1);
        glEnable(GL_LIGHTING);
  }
 
  /**
   *
   * @return delta time since last loop(ms)
   */
  private int getDelta(){
    long time = getTime();
    int delta = (int) (time - lastFrame);
    lastFrame = time;
   
    return delta;
  }

  /**
   *
   * @return current time
   */
  private long getTime() {
    return (Sys.getTime() * 1000) / Sys.getTimerResolution();
  }
 
  /**
   * set title to resolution + fps
   */
  private void updateFPS(){
   
    if(getTime() - lastFPS > 1000){
      Display.setTitle(Display.getWidth() + "x" + Display.getHeight()  + " FPS: " + fps);
      fps = 0;
      lastFPS += 1000;
    }
    fps++;
  }
 
  /**
   * toggle fullscreen
   */
    private void switchMode() {
        fullscreen = !fullscreen;
        try {
            Display.setFullscreen(fullscreen);
            Display.setVSyncEnabled(fullscreen); //only use in fullscreen mode
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
  /**
   * start new level
   */
    public void reset(){
      level.init(this);
      StateManager.setLevel(level);
      StateManager.setState(State.Game);
     
    }
   
    public static void exit(){
      System.exit(0);
    }


  
}
TOP

Related Classes of ast.Game

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.