Advertisement
Guest User

Game class

a guest
May 21st, 2014
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. package com.joetannoury.game;
  2.  
  3. import org.lwjgl.LWJGLException;
  4. import org.lwjgl.Sys;
  5. import org.lwjgl.opengl.Display;
  6. import org.lwjgl.opengl.DisplayMode;
  7. import org.lwjgl.opengl.GL11;
  8.  
  9. public class Game {
  10.  
  11. /** Size of the window */
  12. public static int WIDTH = 1000, HEIGHT = (int) (WIDTH*0.6);
  13.  
  14. /** time at last frame */
  15. long lastFrame;
  16.  
  17. /** frames per second */
  18. int fps;
  19. /** last fps time */
  20. long lastFPS;
  21.  
  22. public void start() {
  23. try {
  24. Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
  25. Display.setTitle("WreckCurve");
  26. Display.create();
  27. } catch (LWJGLException e) {
  28. e.printStackTrace();
  29. System.exit(0);
  30. }
  31.  
  32. initGL(); // init OpenGL
  33. getDelta(); // call once before loop to initialise lastFrame
  34. lastFPS = getTime(); // call before loop to initialise fps timer
  35.  
  36. while (!Display.isCloseRequested()) {
  37. update(getDelta());
  38. renderGL();
  39.  
  40. Display.update();
  41. Display.sync(60); // cap the fps to 60fps
  42. }
  43.  
  44. Display.destroy();
  45. }
  46.  
  47. public void update(int delta) {
  48.  
  49. new Update(delta);
  50.  
  51. updateFPS(); // update FPS Counter
  52. }
  53.  
  54. /**
  55. * Calculate how many milliseconds have passed
  56. * since last frame.
  57. *
  58. * @return milliseconds passed since last frame
  59. */
  60. public int getDelta() {
  61. long time = getTime();
  62. int delta = (int) (time - lastFrame);
  63. lastFrame = time;
  64.  
  65. return delta;
  66. }
  67.  
  68. /**
  69. * Get the accurate system time
  70. *
  71. * @return The system time in milliseconds
  72. */
  73. public long getTime() {
  74. return (Sys.getTime() * 1000) / Sys.getTimerResolution();
  75. }
  76.  
  77. /**
  78. * Calculate the FPS
  79. */
  80. public void updateFPS() {
  81. if (getTime() - lastFPS > 1000) {
  82. //Display.setTitle("FPS: " + fps);
  83. fps = 0;
  84. lastFPS += 1000;
  85. }
  86. fps++;
  87. }
  88.  
  89. public void initGL() {
  90. GL11.glMatrixMode(GL11.GL_PROJECTION);
  91. GL11.glLoadIdentity();
  92. GL11.glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
  93. GL11.glMatrixMode(GL11.GL_MODELVIEW);
  94. }
  95.  
  96. public void renderGL() {
  97.  
  98. new Render();
  99.  
  100. }
  101.  
  102.  
  103.  
  104. public static void main(String[] argv) {
  105. Game game = new Game();
  106. game.start();
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement