package com.voxel.engine.core; import com.voxel.engine.core.Chunk.Chunk; import org.lwjgl.LWJGLException; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; import org.lwjgl.util.glu.GLU; /** * Created with IntelliJ IDEA. * User: Toby's PC * Date: 06/01/14 * Time: 15:47 */ public class Core { public Cube cube; public Chunk chunk; public Chunk chunk2; public static final int SCREEN_WIDTH = 640; public static final int SCREEN_HEIGHT = 480; public static final String TITLE = "Voxel Engine V0.01"; private float playerX, playerY, playerZ; /** * Method that is run at the start */ public void start(){ try{ createWindow(SCREEN_WIDTH, SCREEN_HEIGHT, TITLE); initGL(); run(); }catch(Exception e){ e.printStackTrace(); } } /** * Creates our window based on values given * * @param width, Width of our screen * @param height, Height of our screen * @param title, The title of the game */ public void createWindow(int width, int height, String title){ try { Display.setFullscreen(false); Display.setDisplayMode(new DisplayMode(width, height)); Display.setTitle(title); Display.create(); Display.setVSyncEnabled(true); } catch (LWJGLException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } /** * Init all the graphics components */ public void initGL(){ GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glShadeModel(GL11.GL_SMOOTH); GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); GL11.glClearDepth(1.0); GL11.glEnable(GL11.GL_DEPTH_TEST); GL11.glDepthFunc(GL11.GL_LEQUAL); GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY); GL11.glEnableClientState(GL11.GL_COLOR_ARRAY); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GLU.gluPerspective(45.0f, (float) Display.getDisplayMode().getWidth()/ (float)Display.getDisplayMode().getHeight(), 0.1f, 300.0f); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); } public void run(){ cube = new Cube(); chunk = new Chunk(0,0,0); chunk2 = new Chunk(32,0,0); float rotateYaw = 1; while(!Display.isCloseRequested()){ try{ inputManager(); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glLoadIdentity(); GL11.glTranslatef(-30f + playerX, -40f + playerY, -160f + playerZ); GL11.glRotatef(45f, 0.4f, 1.0f, 0.1f); GL11.glRotatef(45f, 0f, 1.0f, 0f); rotateYaw += 1; if(rotateYaw % 60 == 0){ chunk.rebuildMesh(0,0,0); chunk2.rebuildMesh(32,0,0); } render(); Display.update(); Display.sync(60); }catch(Exception e){ e.printStackTrace(); } } Display.destroy(); } public void inputManager() { if(Keyboard.isKeyDown(Keyboard.KEY_W)){ playerY--; } if(Keyboard.isKeyDown(Keyboard.KEY_S)){ playerY++; } if(Keyboard.isKeyDown(Keyboard.KEY_A)){ playerX++; } if(Keyboard.isKeyDown(Keyboard.KEY_D)){ playerX--; } if(Keyboard.isKeyDown(Keyboard.KEY_E)){ playerZ--; } if(Keyboard.isKeyDown(Keyboard.KEY_Q)){ playerZ++; } } public void render(){ chunk.render(); chunk2.render(); } public static void main(String[] args){ Core core = new Core(); core.start(); } }