Guest User

CL2.java

a guest
Nov 26th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. package game;
  2.  
  3. //LWJGL
  4.  
  5. import org.lwjgl.LWJGLException;
  6. import org.lwjgl.opengl.Display;
  7. import org.lwjgl.opengl.DisplayMode;
  8. import org.lwjgl.opengl.GL11;
  9. import org.lwjgl.Sys;
  10. import org.lwjgl.input.Keyboard;
  11.  
  12. public class CL2
  13. {
  14.     private Player player;
  15.  
  16.     private long lastLoopTime = getTime();
  17.     private static long timerTicksPerSecond = Sys.getTimerResolution();
  18.     public static long delta;
  19.     private long lastFPS;
  20.     private int fps;
  21.    
  22.     private boolean vsync = false;
  23.    
  24.     public CL2()
  25.     {
  26.         lastFPS = getTime();
  27.         openGLInit();
  28.         player = new Player(100,475);
  29.         gameLoop();
  30.     }
  31.     private void openGLInit()
  32.     {
  33.         try
  34.         {
  35.             Display.setDisplayMode(new DisplayMode(800,600));
  36.             Display.setFullscreen(false);
  37.             Display.create();
  38.            
  39.             Display.setVSyncEnabled(false);
  40.            
  41.             GL11.glEnable(GL11.GL_BLEND);
  42.             GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);  
  43.            
  44.             GL11.glEnable(GL11.GL_TEXTURE_2D);
  45.             GL11.glDisable(GL11.GL_DEPTH_TEST);
  46.            
  47.             GL11.glMatrixMode(GL11.GL_PROJECTION);
  48.             GL11.glLoadIdentity();
  49.            
  50.             GL11.glOrtho(0, 800, 600, 0, -1, 1);
  51.             GL11.glMatrixMode(GL11.GL_MODELVIEW);
  52.             GL11.glLoadIdentity();
  53.             GL11.glViewport(0, 0, 800, 600);
  54.            
  55.             GL11.glClearColor(.3f, .3f, .3f, 0f);
  56.         }
  57.         catch (LWJGLException le)
  58.         {
  59.             le.printStackTrace();
  60.         }
  61.     }
  62.     private void gameLoop()
  63.     {
  64.         while(!Display.isCloseRequested())
  65.         {
  66.             GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  67.             GL11.glMatrixMode(GL11.GL_MODELVIEW);
  68.             GL11.glLoadIdentity();
  69.            
  70.             delta = getTime() - lastLoopTime;
  71.             lastLoopTime = getTime();
  72.            
  73.             if (getTime() - lastFPS > 1000)
  74.             {
  75.                 Display.setTitle("FPS: " + fps);
  76.                 fps = 0;
  77.                 lastFPS += 1000;
  78.             }
  79.             fps++;
  80.            
  81.             applyGameControls();        //Take input from keyboard
  82.             moveEntities();             //Move entities
  83.             render();                   //Draw entities
  84.            
  85.             Display.update();
  86.             Display.sync(60);
  87.         }
  88.         Display.destroy();
  89.         System.exit(0);
  90.     }
  91.     private void moveEntities()
  92.     {
  93.         player.move();
  94.     }
  95.     private void render()
  96.     {
  97.         player.draw();
  98.     }
  99.     private void applyGameControls()
  100.     {
  101.         while (Keyboard.next())
  102.         {
  103.             if(Keyboard.getEventKeyState()) //KEY PRESSED
  104.             {
  105.                 if (Keyboard.getEventKey() == Keyboard.KEY_LEFT)
  106.                 {
  107.                     player.left(true);
  108.                 }
  109.                 else if (Keyboard.getEventKey() == Keyboard.KEY_RIGHT)
  110.                 {
  111.                     player.right(true);
  112.                 }
  113.                 else if(Keyboard.getEventKey() == Keyboard.KEY_V)
  114.                 {
  115.                     if(vsync)
  116.                     {
  117.                         vsync = false;
  118.                     }
  119.                     else
  120.                     {
  121.                         vsync = true;
  122.                     }
  123.                     Display.setVSyncEnabled(vsync);
  124.                 }
  125.             }
  126.             else    //KEY RELEASED
  127.             {
  128.                 if (Keyboard.getEventKey() == Keyboard.KEY_LEFT)
  129.                 {
  130.                     player.left(false);
  131.                 }
  132.                 else if (Keyboard.getEventKey() == Keyboard.KEY_RIGHT)
  133.                 {
  134.                     player.right(false);
  135.                 }
  136.             }
  137.         }
  138.     }
  139.     public static long getTime()
  140.     {
  141.         return (Sys.getTime() * 1000) / timerTicksPerSecond;
  142.     }
  143.     public static void main(String[] args)
  144.     {
  145.         new CL2();
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment