Want more features on Pastebin? Sign Up, it's FREE!
Guest

Core Class

By: a guest on Jan 9th, 2014  |  syntax: Java  |  size: 4.11 KB  |  views: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.voxel.engine.core;
  2.  
  3. import com.voxel.engine.core.Chunk.Chunk;
  4. import org.lwjgl.LWJGLException;
  5. import org.lwjgl.input.Keyboard;
  6. import org.lwjgl.opengl.Display;
  7. import org.lwjgl.opengl.DisplayMode;
  8. import org.lwjgl.opengl.GL11;
  9. import org.lwjgl.util.glu.GLU;
  10.  
  11. /**
  12.  * Created with IntelliJ IDEA.
  13.  * User: Toby's PC
  14.  * Date: 06/01/14
  15.  * Time: 15:47
  16.  */
  17. public class Core {
  18.  
  19.     public Cube cube;
  20.     public Chunk chunk;
  21.     public Chunk chunk2;
  22.  
  23.     public static final int SCREEN_WIDTH = 640;
  24.     public static final int SCREEN_HEIGHT = 480;
  25.     public static final String TITLE = "Voxel Engine V0.01";
  26.  
  27.     private float playerX, playerY, playerZ;
  28.  
  29.     /**
  30.      * Method that is run at the start
  31.      */
  32.     public void start(){
  33.         try{
  34.             createWindow(SCREEN_WIDTH, SCREEN_HEIGHT, TITLE);
  35.             initGL();
  36.             run();
  37.         }catch(Exception e){
  38.             e.printStackTrace();
  39.         }
  40.     }
  41.  
  42.     /**
  43.      * Creates our window based on values given
  44.      *
  45.      * @param width, Width of our screen
  46.      * @param height, Height of our screen
  47.      * @param title, The title of the game
  48.      */
  49.     public void createWindow(int width, int height, String title){
  50.         try {
  51.             Display.setFullscreen(false);
  52.             Display.setDisplayMode(new DisplayMode(width, height));
  53.             Display.setTitle(title);
  54.             Display.create();
  55.             Display.setVSyncEnabled(true);
  56.         } catch (LWJGLException e) {
  57.             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
  58.         }
  59.     }
  60.  
  61.     /**
  62.      * Init all the graphics components
  63.      */
  64.     public void initGL(){
  65.         GL11.glEnable(GL11.GL_TEXTURE_2D);
  66.         GL11.glShadeModel(GL11.GL_SMOOTH);
  67.         GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  68.         GL11.glClearDepth(1.0);
  69.         GL11.glEnable(GL11.GL_DEPTH_TEST);
  70.         GL11.glDepthFunc(GL11.GL_LEQUAL);
  71.  
  72.         GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
  73.         GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
  74.  
  75.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  76.         GL11.glLoadIdentity();
  77.  
  78.         GLU.gluPerspective(45.0f, (float) Display.getDisplayMode().getWidth()/ (float)Display.getDisplayMode().getHeight(), 0.1f, 300.0f);
  79.  
  80.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  81.         GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
  82.     }
  83.  
  84.  
  85.  
  86.     public void run(){
  87.         cube = new Cube();
  88.         chunk = new Chunk(0,0,0);
  89.         chunk2 = new Chunk(32,0,0);
  90.         float rotateYaw = 1;
  91.         while(!Display.isCloseRequested()){
  92.             try{
  93.                 inputManager();
  94.                 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  95.                 GL11.glLoadIdentity();
  96.                 GL11.glTranslatef(-30f + playerX, -40f + playerY, -160f + playerZ);
  97.                 GL11.glRotatef(45f, 0.4f, 1.0f, 0.1f);
  98.                 GL11.glRotatef(45f, 0f, 1.0f, 0f);
  99.                 rotateYaw += 1;
  100.                 if(rotateYaw % 60 == 0){
  101.                     chunk.rebuildMesh(0,0,0);
  102.                     chunk2.rebuildMesh(32,0,0);
  103.                 }
  104.                 render();
  105.  
  106.                 Display.update();
  107.                 Display.sync(60);
  108.             }catch(Exception e){
  109.                 e.printStackTrace();
  110.             }
  111.         }
  112.         Display.destroy();
  113.     }
  114.  
  115.     public void inputManager() {
  116.         if(Keyboard.isKeyDown(Keyboard.KEY_W)){
  117.             playerY--;
  118.         }
  119.         if(Keyboard.isKeyDown(Keyboard.KEY_S)){
  120.             playerY++;
  121.         }
  122.         if(Keyboard.isKeyDown(Keyboard.KEY_A)){
  123.             playerX++;
  124.         }
  125.         if(Keyboard.isKeyDown(Keyboard.KEY_D)){
  126.             playerX--;
  127.         }
  128.         if(Keyboard.isKeyDown(Keyboard.KEY_E)){
  129.             playerZ--;
  130.         }
  131.         if(Keyboard.isKeyDown(Keyboard.KEY_Q)){
  132.             playerZ++;
  133.         }
  134.     }
  135.  
  136.     public void render(){
  137.         chunk.render();
  138.         chunk2.render();
  139.     }
  140.  
  141.     public static void main(String[] args){
  142.         Core core = new Core();
  143.         core.start();
  144.     }
  145. }
clone this paste RAW Paste Data