Advertisement
Guest User

Untitled

a guest
Jan 16th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. package com.voxel.engine.core;
  2.  
  3. import com.voxel.engine.core.Block.Block;
  4. import com.voxel.engine.core.Camera.FPCameraController;
  5. import com.voxel.engine.core.Chunk.ChunkManager;
  6. import org.lwjgl.LWJGLException;
  7. import org.lwjgl.input.Keyboard;
  8. import org.lwjgl.opengl.*;
  9. import org.lwjgl.util.glu.GLU;
  10.  
  11. import java.util.Random;
  12.  
  13. import static org.lwjgl.opengl.GL11.*;
  14.  
  15. /**
  16. * Created with IntelliJ IDEA.
  17. * User: Toby's PC
  18. * Date: 06/01/14
  19. * Time: 15:47
  20. */
  21. public class Core {
  22.  
  23. public static final int WORLD_WIDTH = 8;
  24. public static final int WORLD_HEIGHT = 1;
  25. public static final int BLOCK_WIDTH = 16;
  26. public static final int BLOCK_HEIGHT = 16;
  27.  
  28. private Cube cube = new Cube();
  29. private Block.BlockType blockType = Block.BlockType.BLOCK_TYPE_STONE;
  30.  
  31. FPCameraController camera = FPCameraController.getInstance();
  32.  
  33. public static final int SCREEN_WIDTH = 640;
  34. public static final int SCREEN_HEIGHT = 480;
  35. public static final String TITLE = "Voxel Engine V0.01";
  36.  
  37. private float playerX, playerY, playerZ;
  38.  
  39. /**
  40. * Method that is run at the start
  41. */
  42. public void start(){
  43. try{
  44. createWindow(SCREEN_WIDTH, SCREEN_HEIGHT, TITLE);
  45.  
  46. PixelFormat pixelFormat = new PixelFormat();
  47. ContextAttribs contextAttribs = new ContextAttribs(3, 0);
  48.  
  49. Display.create(pixelFormat, contextAttribs);
  50. glViewport(0, 0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight());
  51.  
  52. if(GLContext.getCapabilities().OpenGL21){
  53. System.out.println("OpenGL21 Supported");
  54. }
  55.  
  56. Random random = new Random();
  57. for(int x = 0;x < 16;x++){
  58. for (int z = 0;z < 16;z++){
  59. ChunkManager.getInstance().addChunk(ChunkManager.ChunkType.CHUNK_TYPE_RANDOM, x, 0, z);
  60. }
  61. }
  62.  
  63. initGL();
  64. run();
  65. }catch(Exception e){
  66. e.printStackTrace();
  67. }
  68. }
  69.  
  70. /**
  71. * Creates our window based on values given
  72. *
  73. * @param width, Width of our screen
  74. * @param height, Height of our screen
  75. * @param title, The title of the game
  76. */
  77. public void createWindow(int width, int height, String title){
  78. try {
  79. Display.setFullscreen(false);
  80. Display.setDisplayMode(new DisplayMode(width, height));
  81. Display.setTitle(title);
  82. Display.setVSyncEnabled(true);
  83. } catch (LWJGLException e) {
  84. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  85. }
  86. }
  87.  
  88. /**
  89. * Init all the graphics components
  90. */
  91. public void initGL(){
  92. GL11.glMatrixMode(GL_PROJECTION);
  93. GL11.glLoadIdentity();
  94. GLU.gluPerspective(45.0f, (float) Display.getDisplayMode().getWidth() / (float) Display.getDisplayMode().getHeight(), 0.1f, 300.0f);
  95. GL11.glEnable(GL11.GL_FOG);
  96.  
  97. GL11.glTranslatef(0, -1f, 0f);
  98. GL11.glRotatef(180.0f, 0, 1.0f, 0);
  99. GL11.glMatrixMode(GL_MODELVIEW);
  100. GL11.glLoadIdentity();
  101. GL11.glShadeModel(GL_LINE_SMOOTH);
  102. GL11.glClearColor(0.1f, 0.5f, 0.7f, 0.0f);
  103. GL11.glClearDepth(1.0);
  104. GL11.glEnable(GL_DEPTH_TEST);
  105. GL11.glDepthFunc(GL11.GL_LEQUAL);
  106. GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
  107. GL11.glCullFace(GL11.GL_BACK);
  108. GL11.glEnable(GL11.GL_CULL_FACE);
  109. }
  110.  
  111.  
  112.  
  113. public void run(){
  114. float rotateYaw = 1;
  115. while(!Display.isCloseRequested()){
  116. try{
  117. inputManager();
  118. GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  119. GL11.glLoadIdentity();
  120. GL11.glTranslatef(-30f + playerX, -40f + playerY, -160f + playerZ);
  121. GL11.glRotatef(45f, 0.4f, 1.0f, 0.1f);
  122. GL11.glRotatef(45f, 0f, 1.0f, 0f);
  123. rotateYaw += 1;
  124. render();
  125.  
  126. Display.update();
  127. Display.sync(60);
  128. }catch(Exception e){
  129. e.printStackTrace();
  130. }
  131. }
  132. Display.destroy();
  133. }
  134.  
  135. public void inputManager() {
  136. if(Keyboard.isKeyDown(Keyboard.KEY_W)){
  137. playerY--;
  138. }
  139. if(Keyboard.isKeyDown(Keyboard.KEY_S)){
  140. playerY++;
  141. }
  142. if(Keyboard.isKeyDown(Keyboard.KEY_A)){
  143. playerX++;
  144. }
  145. if(Keyboard.isKeyDown(Keyboard.KEY_D)){
  146. playerX--;
  147. }
  148. if(Keyboard.isKeyDown(Keyboard.KEY_E)){
  149. playerZ--;
  150. }
  151. if(Keyboard.isKeyDown(Keyboard.KEY_Q)){
  152. playerZ++;
  153. }
  154. }
  155.  
  156. public void render(){
  157. ChunkManager.getInstance().render();
  158. }
  159.  
  160. public static void main(String[] args){
  161. Core core = new Core();
  162. core.start();
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement