Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Game;
- import java.awt.Frame;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import javax.media.opengl.GL;
- import javax.media.opengl.GL2;
- import javax.media.opengl.GLAutoDrawable;
- import javax.media.opengl.GLCapabilities;
- import javax.media.opengl.GLEventListener;
- import javax.media.opengl.GLProfile;
- import javax.media.opengl.awt.GLCanvas;
- import javax.media.opengl.glu.GLU;
- import com.jogamp.opengl.util.FPSAnimator;
- import Controls.Keyboard;
- import Controls.Mouse;
- import Enemys.Zombies;
- import Level.Level;
- import Player.Player;
- import modelLoader.modelLoader;
- public class Game implements Runnable, GLEventListener{
- private static Properties p = new Properties();
- boolean running = false;
- private int höhe, breite, texture;
- private double camera_x,camera_y, camera_z,lookat_x,lookat_y,lookat_z,zoom;
- private String name;
- protected GLCanvas canvas;
- private GLU glu;
- Zombies zombies;
- Player player;
- Keyboard keyboard;
- Mouse mouse;
- Level level;
- Thread thread;
- Frame frame;
- modelLoader loader;
- public Game(String name,int breite ,int höhe){
- this.breite = breite;
- this.höhe = höhe;
- this.name = name;
- player = new Player();
- keyboard = new Keyboard();
- level = new Level();
- mouse = new Mouse();
- zombies = new Zombies();
- loader = new modelLoader();
- glu = new GLU();
- GLProfile glp = GLProfile.getDefault();
- GLCapabilities caps = new GLCapabilities(glp);
- caps.setNumSamples(2);
- caps.setSampleBuffers(true);
- canvas = new GLCanvas(caps);
- frame = new Frame(name);
- frame.setSize(breite, höhe);
- frame.add(canvas);
- frame.setVisible(true);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- canvas.addGLEventListener(this);
- canvas.setFocusable(true);
- canvas.addKeyListener(keyboard);
- canvas.addMouseListener(mouse);
- canvas.addMouseMotionListener(mouse);
- FPSAnimator animator = new FPSAnimator(canvas, 60);
- animator.start();
- }
- public long lastFrameTime = System.currentTimeMillis();
- private long frameMillis = 0;
- private final static long frameTime = 10;
- public static void main(String [] args){
- Game game = new Game("3D Shooter", p.Width(),p.Height());
- // game.start();
- }
- public void start(){
- running = true;
- thread = new Thread(this, "Game");
- thread.start();
- }
- public synchronized void stop(){
- running = false;
- try {
- thread.join();
- }
- catch (InterruptedException e){
- e.printStackTrace();
- }
- }
- public void update(){
- keyboard.update();
- mouse.update(frame.getWidth(),frame.getHeight());
- if(keyboard.i8)zoom++;
- if(keyboard.i9 && zoom > -90)zoom--;
- player.update(keyboard, mouse, level, zombies, frame);
- camera_x = player.posX();
- camera_y = 100+zoom;
- camera_z = player.posY()+20;
- lookat_x = player.posX();
- lookat_y = 0;
- lookat_z = player.posY();
- zombies.update(player);
- }
- public void display(GLAutoDrawable drawable) {
- frameMillis += (System.currentTimeMillis()-lastFrameTime);
- lastFrameTime = System.currentTimeMillis();
- int c=0;
- while (frameMillis>frameTime){
- frameMillis -= frameTime;
- c++;
- if (c<10)update();
- }
- GL2 gl = drawable.getGL().getGL2();
- gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
- breite = frame.getWidth();
- höhe = frame.getHeight();
- gl.glViewport(0, 0, breite, höhe);
- gl.glMatrixMode(GL2.GL_PROJECTION);
- gl.glLoadIdentity();
- glu.gluPerspective(60, (float)breite / höhe, 0, p.range());
- glu.gluLookAt ( camera_x,camera_y,camera_z,
- lookat_x ,lookat_y , lookat_z,
- 0.0 , 0.0, -1) ;
- gl.glMatrixMode(GL2.GL_MODELVIEW);
- player.render(gl, glu);
- level.render(level.LevelTiles(), player.posX(), player.posY(), gl);
- zombies.render(drawable, player);
- }
- public void init(GLAutoDrawable drawable) {
- GL2 gl = drawable.getGL().getGL2();
- gl.setSwapInterval(0);
- gl.glEnable(GL2.GL_DEPTH_TEST);
- gl.glDepthFunc(GL2.GL_LEQUAL);
- System.out.println("##############< Info >#################");
- System.out.println("GL_VENDOR: " + gl.glGetString(GL2.GL_VENDOR));
- System.out.println("GL_RENDERER: " + gl.glGetString(GL2.GL_RENDERER));
- System.out.println("GL_VERSION: " + gl.glGetString(GL2.GL_VERSION));
- System.out.println("##############</Info >#################");
- }
- public void reshape(GLAutoDrawable drawable,int x, int y, int w, int h) {
- GL2 gl = drawable.getGL().getGL2();
- gl.glViewport(0, 0, breite, höhe);
- gl.glMatrixMode(GL2.GL_PROJECTION);
- gl.glLoadIdentity();
- glu.gluPerspective(60, (float) breite / höhe, 0, p.range());
- glu.gluLookAt ( camera_x,camera_y,camera_z,
- lookat_x ,lookat_y , lookat_z,
- 0.0 , 0.0, 1) ;
- gl.glMatrixMode(GL2.GL_MODELVIEW);
- gl.glLoadIdentity();
- }
- @Override
- public void run() {
- long lastTime = System.nanoTime();
- long timer = System.currentTimeMillis();
- final double ns = 1000000000.0 / 100; // Hier Updates pro Sekunde einstellen
- double delta = 0;
- int frames = 0;
- int updates = 0;
- while(running){
- long now = System.nanoTime();
- delta += (now-lastTime) / ns;
- lastTime = now;
- while (delta >= 1){
- update();
- updates++;
- delta--;
- }
- canvas.display();
- frames++;
- if (System.currentTimeMillis() - timer > 1000){
- timer += 1000;
- frame.setTitle( " | " + updates + " ups, " + frames + " fps");
- updates = 0;
- frames = 0;
- }
- }
- stop();
- }
- @Override
- public void dispose(GLAutoDrawable arg0) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment