Guest User

Game.java

a guest
Mar 30th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. package me.willchill.game;
  2.  
  3. import me.willchill.game.entity.Entity;
  4. import me.willchill.game.input.GameKeyboard;
  5.  
  6. import org.lwjgl.LWJGLException;
  7. import org.lwjgl.opengl.Display;
  8. import org.lwjgl.opengl.DisplayMode;
  9. import org.lwjgl.opengl.GL11;
  10.  
  11. public class Game{
  12.    
  13.     private GameKeyboard keyboard;
  14.     private String gameStats = " ";
  15.     private Entity player;
  16.    
  17.     public Game(){
  18.         keyboard = new GameKeyboard();
  19.     }
  20.    
  21.     private void start(){
  22.        
  23.         try{
  24.             Display.setDisplayMode(new DisplayMode(800,600));
  25.             Display.create();
  26.             Display.setTitle("Game");
  27.             Display.setVSyncEnabled(true);
  28.         } catch (LWJGLException e){
  29.             e.printStackTrace();
  30.             System.exit(0);
  31.         }
  32.        
  33.         initGL();
  34.         player = new Entity("/res/player.png", 400, 300);
  35.        
  36.         long lastTime = System.nanoTime();
  37.         long timer = System.currentTimeMillis();
  38.         double ns = 1000000000.0 / 60.0;
  39.         double delta = 0;
  40.         int frames = 0;
  41.         int updates = 0;
  42.        
  43.        
  44.         while(!Display.isCloseRequested()){
  45.            
  46.             long now = System.nanoTime();
  47.             delta += (now - lastTime) / ns;
  48.             lastTime = now;
  49.            
  50.             while(delta >= 1){
  51.                 update();
  52.                 updates++;
  53.                 delta--;
  54.             }
  55.            
  56.             frames++;
  57.            
  58.             if(System.currentTimeMillis() - timer > 1000){
  59.                 timer += 1000;
  60.                 gameStats = "FPS: " + frames + ", UPS: " + updates;
  61.                 Display.setTitle("Game " + gameStats);
  62.                 System.out.println(gameStats);
  63.                 updates = 0;
  64.                 frames = 0;
  65.             }
  66.            
  67.             render();
  68.             Display.update();
  69.         }
  70.        
  71.         Display.destroy();
  72.     }
  73.    
  74.     private void render(){
  75.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
  76.        
  77.         GL11.glBegin(GL11.GL_QUADS);
  78.             GL11.glVertex2f(player.x, player.y ); //Top-left
  79.             GL11.glVertex2f(player.x + player.texture.getTextureWidth(), player.y);//Top-right
  80.             GL11.glVertex2f(player.x + player.texture.getTextureWidth(), player.y + player.texture.getTextureHeight());//Bottom-right
  81.             GL11.glVertex2f(player.x, player.y + player.texture.getTextureHeight());//Bottom-left
  82.         GL11.glEnd();
  83.     }
  84.    
  85.     private void update() {
  86.         keyboard.pollInput();
  87.     }
  88.  
  89.     public void initGL() {
  90.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  91.         GL11.glLoadIdentity();
  92.         GL11.glOrtho(0, 800, 0, 600, 1, -1);
  93.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  94.         GL11.glEnable(GL11.GL_TEXTURE_2D);
  95.     }
  96.    
  97.     public static void main(String args[]){
  98.         Game game = new Game();
  99.         game.start();
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment