Guest User

Untitled

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