Guest User

Untitled

a guest
Feb 27th, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 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.  
  72. // caps.setDepthBits(16);
  73. canvas = new GLCanvas(caps);
  74. canvas = new GLCanvas(caps);
  75. frame = new Frame(name);
  76. frame.setSize(breite, höhe);
  77. frame.add(canvas);
  78. frame.setVisible(true);
  79. frame.addWindowListener(new WindowAdapter() {
  80. public void windowClosing(WindowEvent e) {
  81. System.exit(0);
  82. }
  83. });
  84.  
  85. canvas.addGLEventListener(this);
  86. canvas.setFocusable(true);
  87. canvas.addKeyListener(keyboard);
  88. canvas.addMouseListener(mouse);
  89. canvas.addMouseMotionListener(mouse);
  90.  
  91. FPSAnimator animator = new FPSAnimator(canvas, 60);
  92. animator.start();
  93. }
  94.  
  95. public static void main(String [] args){
  96. Game game = new Game("3D Shooter", p.Width(),p.Height());
  97. //game.start();
  98. }
  99.  
  100. public void start(){
  101. running = true;
  102. thread = new Thread(this, "Game");
  103. thread.start();
  104. }
  105.  
  106. public synchronized void stop(){
  107. running = false;
  108. try {
  109. thread.join();
  110. }
  111. catch (InterruptedException e){
  112. e.printStackTrace();
  113. }
  114. }
  115.  
  116.  
  117.  
  118. public void update(){
  119. keyboard.update();
  120. mouse.update(frame.getWidth(),frame.getHeight());
  121. if(keyboard.i8)zoom++;
  122. if(keyboard.i9 && zoom > -90)zoom--;
  123.  
  124. player.update(keyboard, mouse, level, zombies, frame);
  125.  
  126. camera_x = player.posX();
  127. camera_y = 100+zoom;
  128. camera_z = player.posY()+20;
  129.  
  130. lookat_x = player.posX();
  131. lookat_y = 0;
  132. lookat_z = player.posY();
  133.  
  134.  
  135. zombies.update(player);
  136.  
  137. }
  138.  
  139.  
  140. int t = 0;
  141.  
  142. public void display(GLAutoDrawable drawable) {
  143. update();
  144. GL2 gl = drawable.getGL().getGL2();
  145.  
  146.  
  147. breite = frame.getWidth();
  148. höhe = frame.getHeight();
  149.  
  150. gl.glViewport(0, 0, breite, höhe);
  151. gl.glMatrixMode(GL2.GL_PROJECTION);
  152. gl.glLoadIdentity();
  153.  
  154. glu.gluPerspective(60, (float)breite / höhe, 0, p.range());
  155. glu.gluLookAt ( camera_x,camera_y,camera_z,
  156. lookat_x ,lookat_y , lookat_z,
  157. 0.0 , 0.0, -1) ;
  158. gl.glMatrixMode(GL2.GL_MODELVIEW);
  159. //player.render(gl, glu);
  160. gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  161. level.render(level.LevelTiles(), player.posX(), player.posY(), gl);
  162. //zombies.render(drawable, player);
  163.  
  164. }
  165.  
  166.  
  167. public void init(GLAutoDrawable drawable) {
  168.  
  169. GL2 gl = drawable.getGL().getGL2();
  170. gl.setSwapInterval(0);
  171. gl.glEnable(GL2.GL_DEPTH_TEST);
  172. gl.glDepthFunc(GL2.GL_LEQUAL);
  173.  
  174. System.out.println("##############< Info >#################");
  175. System.out.println("GL_VENDOR: " + gl.glGetString(GL2.GL_VENDOR));
  176. System.out.println("GL_RENDERER: " + gl.glGetString(GL2.GL_RENDERER));
  177. System.out.println("GL_VERSION: " + gl.glGetString(GL2.GL_VERSION));
  178. System.out.println("##############</Info >#################");
  179.  
  180. }
  181.  
  182.  
  183. public void reshape(GLAutoDrawable drawable,int x, int y, int w, int h) {
  184. GL2 gl = drawable.getGL().getGL2();
  185. gl.glViewport(0, 0, breite, höhe);
  186.  
  187. gl.glMatrixMode(GL2.GL_PROJECTION);
  188. gl.glLoadIdentity();
  189. glu.gluPerspective(60, (float) breite / höhe, 0, p.range());
  190. glu.gluLookAt ( camera_x,camera_y,camera_z,
  191. lookat_x ,lookat_y , lookat_z,
  192. 0.0 , 0.0, 1) ;
  193. gl.glMatrixMode(GL2.GL_MODELVIEW);
  194. gl.glLoadIdentity();
  195.  
  196. }
  197.  
  198.  
  199. @Override
  200. public void run() {
  201. long lastTime = System.nanoTime();
  202. long timer = System.currentTimeMillis();
  203. final double ns = 1000000000.0 / 100; // Hier Updates pro Sekunde einstellen
  204. double delta = 0;
  205. int frames = 0;
  206. int updates = 0;
  207.  
  208. while(running){
  209. long now = System.nanoTime();
  210. delta += (now-lastTime) / ns;
  211. lastTime = now;
  212. while (delta >= 1){
  213. update();
  214. updates++;
  215. delta--;
  216. }
  217.  
  218. canvas.display();
  219. frames++;
  220.  
  221. if (System.currentTimeMillis() - timer > 1000){
  222. timer += 1000;
  223. frame.setTitle( " | " + updates + " ups, " + frames + " fps");
  224. updates = 0;
  225. frames = 0;
  226. }
  227. }
  228. stop();
  229. }
  230.  
  231. @Override
  232. public void dispose(GLAutoDrawable arg0) {
  233. // TODO Auto-generated method stub
  234.  
  235. }
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment