Guest User

Untitled

a guest
Feb 24th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. package Game;
  2.  
  3.  
  4.  
  5. import java.awt.Frame;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import javax.media.opengl.GL;
  9. import javax.media.opengl.GL2;
  10. import javax.media.opengl.GLAutoDrawable;
  11. import javax.media.opengl.GLCapabilities;
  12. import javax.media.opengl.GLEventListener;
  13. import javax.media.opengl.GLProfile;
  14. import javax.media.opengl.awt.GLCanvas;
  15. import javax.media.opengl.glu.GLU;
  16.  
  17. import com.jogamp.opengl.util.FPSAnimator;
  18.  
  19. import Controls.Keyboard;
  20. import Controls.Mouse;
  21. import Enemys.Zombies;
  22. import Level.Level;
  23. import Player.Player;
  24. import modelLoader.modelLoader;
  25.  
  26.  
  27.  
  28. public class Game implements Runnable, GLEventListener{
  29.  
  30.  
  31. private static Properties p = new Properties();
  32.  
  33.  
  34. boolean running = false;
  35.  
  36. private int höhe, breite, texture;
  37. private double camera_x,camera_y, camera_z,lookat_x,lookat_y,lookat_z,zoom;
  38. private String name;
  39.  
  40. protected GLCanvas canvas;
  41. private GLU glu;
  42.  
  43. Zombies zombies;
  44. Player player;
  45. Keyboard keyboard;
  46. Mouse mouse;
  47. Level level;
  48. Thread thread;
  49. Frame frame;
  50. modelLoader loader;
  51.  
  52. public Game(String name,int breite ,int höhe){
  53.  
  54. this.breite = breite;
  55. this.höhe = höhe;
  56. this.name = name;
  57.  
  58.  
  59. player = new Player();
  60. keyboard = new Keyboard();
  61. level = new Level();
  62. mouse = new Mouse();
  63. zombies = new Zombies();
  64. loader = new modelLoader();
  65. glu = new GLU();
  66.  
  67. GLProfile glp = GLProfile.getDefault();
  68. GLCapabilities caps = new GLCapabilities(glp);
  69. caps.setNumSamples(2);
  70. caps.setSampleBuffers(true);
  71. canvas = new GLCanvas(caps);
  72. frame = new Frame(name);
  73. frame.setSize(breite, höhe);
  74. frame.add(canvas);
  75. frame.setVisible(true);
  76. frame.addWindowListener(new WindowAdapter() {
  77. public void windowClosing(WindowEvent e) {
  78. System.exit(0);
  79. }
  80. });
  81.  
  82. canvas.addGLEventListener(this);
  83. canvas.setFocusable(true);
  84. canvas.addKeyListener(keyboard);
  85. canvas.addMouseListener(mouse);
  86. canvas.addMouseMotionListener(mouse);
  87. FPSAnimator animator = new FPSAnimator(canvas, 60);
  88. animator.start();
  89. }
  90. public long lastFrameTime = System.currentTimeMillis();
  91. private long frameMillis = 0;
  92. private final static long frameTime = 10;
  93.  
  94. public static void main(String [] args){
  95. Game game = new Game("3D Shooter", p.Width(),p.Height());
  96. // game.start();
  97. }
  98.  
  99. public void start(){
  100. running = true;
  101. thread = new Thread(this, "Game");
  102. thread.start();
  103. }
  104.  
  105. public synchronized void stop(){
  106. running = false;
  107. try {
  108. thread.join();
  109. }
  110. catch (InterruptedException e){
  111. e.printStackTrace();
  112. }
  113. }
  114.  
  115.  
  116.  
  117. public void update(){
  118. keyboard.update();
  119. mouse.update(frame.getWidth(),frame.getHeight());
  120. if(keyboard.i8)zoom++;
  121. if(keyboard.i9 && zoom > -90)zoom--;
  122.  
  123. player.update(keyboard, mouse, level, zombies, frame);
  124.  
  125. camera_x = player.posX();
  126. camera_y = 100+zoom;
  127. camera_z = player.posY()+20;
  128.  
  129. lookat_x = player.posX();
  130. lookat_y = 0;
  131. lookat_z = player.posY();
  132.  
  133.  
  134. zombies.update(player);
  135.  
  136. }
  137.  
  138.  
  139.  
  140.  
  141. public void display(GLAutoDrawable drawable) {
  142. frameMillis += (System.currentTimeMillis()-lastFrameTime);
  143. lastFrameTime = System.currentTimeMillis();
  144.  
  145. int c=0;
  146. while (frameMillis>frameTime){
  147. frameMillis -= frameTime;
  148. c++;
  149. if (c<10)update();
  150. }
  151.  
  152. GL2 gl = drawable.getGL().getGL2();
  153.  
  154. gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  155.  
  156. breite = frame.getWidth();
  157. höhe = frame.getHeight();
  158.  
  159. gl.glViewport(0, 0, breite, höhe);
  160. gl.glMatrixMode(GL2.GL_PROJECTION);
  161. gl.glLoadIdentity();
  162.  
  163. glu.gluPerspective(60, (float)breite / höhe, 0, p.range());
  164. glu.gluLookAt ( camera_x,camera_y,camera_z,
  165. lookat_x ,lookat_y , lookat_z,
  166. 0.0 , 0.0, -1) ;
  167. gl.glMatrixMode(GL2.GL_MODELVIEW);
  168.  
  169. player.render(gl, glu);
  170.  
  171. level.render(level.LevelTiles(), player.posX(), player.posY(), gl);
  172. zombies.render(drawable, player);
  173. }
  174.  
  175.  
  176. public void init(GLAutoDrawable drawable) {
  177.  
  178. GL2 gl = drawable.getGL().getGL2();
  179. gl.setSwapInterval(0);
  180. gl.glEnable(GL2.GL_DEPTH_TEST);
  181. gl.glDepthFunc(GL2.GL_LEQUAL);
  182.  
  183. System.out.println("##############< Info >#################");
  184. System.out.println("GL_VENDOR: " + gl.glGetString(GL2.GL_VENDOR));
  185. System.out.println("GL_RENDERER: " + gl.glGetString(GL2.GL_RENDERER));
  186. System.out.println("GL_VERSION: " + gl.glGetString(GL2.GL_VERSION));
  187. System.out.println("##############</Info >#################");
  188.  
  189. }
  190.  
  191.  
  192. public void reshape(GLAutoDrawable drawable,int x, int y, int w, int h) {
  193. GL2 gl = drawable.getGL().getGL2();
  194. gl.glViewport(0, 0, breite, höhe);
  195.  
  196. gl.glMatrixMode(GL2.GL_PROJECTION);
  197. gl.glLoadIdentity();
  198. glu.gluPerspective(60, (float) breite / höhe, 0, p.range());
  199. glu.gluLookAt ( camera_x,camera_y,camera_z,
  200. lookat_x ,lookat_y , lookat_z,
  201. 0.0 , 0.0, 1) ;
  202. gl.glMatrixMode(GL2.GL_MODELVIEW);
  203. gl.glLoadIdentity();
  204.  
  205. }
  206.  
  207.  
  208. @Override
  209. public void run() {
  210. long lastTime = System.nanoTime();
  211. long timer = System.currentTimeMillis();
  212. final double ns = 1000000000.0 / 100; // Hier Updates pro Sekunde einstellen
  213. double delta = 0;
  214. int frames = 0;
  215. int updates = 0;
  216.  
  217. while(running){
  218. long now = System.nanoTime();
  219. delta += (now-lastTime) / ns;
  220. lastTime = now;
  221. while (delta >= 1){
  222. update();
  223. updates++;
  224. delta--;
  225. }
  226.  
  227. canvas.display();
  228. frames++;
  229.  
  230. if (System.currentTimeMillis() - timer > 1000){
  231. timer += 1000;
  232. frame.setTitle( " | " + updates + " ups, " + frames + " fps");
  233. updates = 0;
  234. frames = 0;
  235. }
  236. }
  237. stop();
  238. }
  239.  
  240. @Override
  241. public void dispose(GLAutoDrawable arg0) {
  242. // TODO Auto-generated method stub
  243.  
  244. }
  245.  
  246. }
Advertisement
Add Comment
Please, Sign In to add comment