Advertisement
Guest User

Untitled

a guest
Aug 26th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.74 KB | None | 0 0
  1.  
  2. import static org.lwjgl.opengl.GL11.*;
  3.  
  4. import java.applet.Applet;
  5. import java.awt.BorderLayout;
  6. import java.awt.Canvas;
  7. import java.io.IOException;
  8.  
  9. import org.lwjgl.input.Keyboard;
  10. import org.lwjgl.input.Mouse;
  11. import org.lwjgl.opengl.*;
  12. import org.lwjgl.*;
  13. import org.newdawn.slick.opengl.Texture;
  14. import org.newdawn.slick.opengl.TextureLoader;
  15.  
  16. public class Main extends Applet{
  17.  
  18.     static Camera cam;
  19.     static float x=0;
  20.     static Texture wood, grass;
  21.     static int fps=0, upd=0;
  22.     static int startTime;
  23.     static int passedSec = 0;
  24.     static int width, height;
  25.     static Thread gameThread;
  26.     static boolean running =false;
  27.    
  28.      //applet
  29.     static Canvas display_parent;
  30.    
  31.     public void init(){
  32.         setLayout(new BorderLayout());
  33.         try {
  34.             display_parent = new Canvas() {
  35.                 /**
  36.                  *
  37.                  */
  38.                 private static final long serialVersionUID = -8951216526237771039L;
  39.                 public final void addNotify() {
  40.                     super.addNotify();
  41.                     startOGL();
  42.                 }
  43.                 public final void removeNotify() {
  44.                     stopOGL();
  45.                     super.removeNotify();
  46.                 }
  47.             };
  48.             //display_parent.setSize(getWidth(),getHeight());
  49.             setSize(640, 480);
  50.             display_parent.setSize(640, 480);
  51.             width = getWidth();
  52.             height = getHeight();
  53.             add(display_parent);
  54.             display_parent.setFocusable(true);
  55.             display_parent.requestFocus();
  56.             display_parent.setIgnoreRepaint(true);
  57.             setVisible(true);
  58.         } catch (Exception e) {
  59.             System.err.println(e);
  60.             throw new RuntimeException("Unable to create display");
  61.         }
  62.     }
  63.    
  64.     public void start(){
  65.        
  66.     }
  67.     public void stop(){
  68.        
  69.     }
  70.    
  71.     public void destroy() {
  72.         remove(display_parent);
  73.         super.destroy();
  74.     }
  75.    
  76.     //endapplet
  77.    
  78.     public static void stopOGL() {
  79.         running =false;
  80.         try{
  81.             gameThread.join();
  82.         }catch(InterruptedException e)
  83.         {
  84.             e.printStackTrace();
  85.         }
  86.     }
  87.    
  88.     public static void startOGL() {
  89.         gameThread = new Thread() {
  90.             public void run(){
  91.                 running=true;
  92.                 initDisplay();
  93.                 startTime = (int) System.currentTimeMillis();
  94.                 gameLoop();
  95.                 cleanUp(); //STOP game
  96.             }
  97.         };
  98.         gameThread.start();
  99.     }
  100.    
  101.     public static void initDisplay()
  102.     {
  103.        
  104.         try {
  105.             //Display.setDisplayMode(new DisplayMode(640, 480));
  106.             Display.setParent(display_parent);
  107.             Display.create();
  108.         } catch (Exception e) {
  109.             // TODO Auto-generated catch block
  110.             e.printStackTrace();
  111.         }
  112.        
  113.     }
  114.    
  115.     public static Texture loadTexture(String path)
  116.     {
  117.         try {
  118.             return TextureLoader.getTexture("png", Main.class.getResourceAsStream("res/"+path + ".png"));
  119.         } catch (IOException e) {
  120.             // TODO Auto-generated catch block
  121.             e.printStackTrace();
  122.         }
  123.        
  124.         return null;
  125.        
  126.     }
  127.    
  128.     public static void gameLoop()
  129.     {
  130.         cam = new Camera(70, (float)Display.getWidth()/(float)Display.getHeight(), 0.3F, 1000);
  131.         wood = loadTexture("wood");
  132.         grass = loadTexture("grass");
  133.         Mouse.setGrabbed(true);
  134.        
  135.         while(running)
  136.         {
  137.             update();
  138.         }
  139.         Display.destroy();
  140.     }
  141.    
  142.     static int lastCheck = 0;
  143.     static int updates = 0;
  144.     static boolean paused = false, lastPauseKey = false;
  145.     private static void update()
  146.     {
  147.         updates++;
  148.         passedSec = ((int)System.currentTimeMillis() - startTime)/60;
  149.         if(passedSec - lastCheck >= 1)
  150.         {
  151.             upd = updates;
  152.             //System.out.println("upd: " + upd + " || timepassed " + passedSec + " || lastCheck " + lastCheck);
  153.             lastCheck = passedSec;
  154.             updates = 0;
  155.         }
  156.        
  157.         if(Keyboard.isKeyDown(Keyboard.KEY_W))
  158.         {
  159.             cam.move(0.005f, 1);
  160.         }
  161.         if(Keyboard.isKeyDown(Keyboard.KEY_S))
  162.         {
  163.             cam.move(-0.003f, 1);
  164.         }
  165.         if(Keyboard.isKeyDown(Keyboard.KEY_A))
  166.         {
  167.             cam.move(0.003f, 0);
  168.         }
  169.         if(Keyboard.isKeyDown(Keyboard.KEY_D))
  170.         {
  171.             cam.move(-0.003f, 0);
  172.         }
  173.        
  174.         if(Keyboard.isKeyDown(Keyboard.KEY_LEFT))
  175.         {
  176.             cam.rotateY(-0.03F);
  177.         }
  178.         if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
  179.         {
  180.             cam.rotateY(0.03F);
  181.         }
  182.         if(Keyboard.isKeyDown(Keyboard.KEY_UP))
  183.         {
  184.             cam.rotateX(-0.03F);
  185.         }
  186.         if(Keyboard.isKeyDown(Keyboard.KEY_DOWN))
  187.         {
  188.             cam.rotateX(0.03F);
  189.         }
  190.         if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) && !lastPauseKey)
  191.         {
  192.             paused = !paused;
  193.             Mouse.setGrabbed(!paused);
  194.             System.out.println(paused);
  195.         }
  196.         lastPauseKey = Keyboard.isKeyDown(Keyboard.KEY_ESCAPE);
  197.        
  198.         if(!paused && (Mouse.getDX() != 0 || Mouse.getDY() != 0))
  199.             handleMouseMove(Mouse.getX(), Mouse.getY());
  200.        
  201.        
  202.         draw();
  203.     }
  204.    
  205.     //COPIE
  206.     static float camXRot=0,camYRot=90,camZRot=0;
  207.     static void handleMouseMove(int mouseX, int mouseY)
  208.     {
  209.         float vertMouseSensitivity  = 13.0f;
  210.         float horizMouseSensitivity = 15.0f;
  211.         int midWindowX = width/2;
  212.         int midWindowY = height/2;
  213.      
  214.         //cout << "Mouse cursor is at position (" << mouseX << ", " << mouseY << endl;
  215.      
  216.         int horizMovement = mouseX - midWindowX;
  217.         int vertMovement  = mouseY - midWindowY;
  218.      
  219.         camXRot -= vertMovement / vertMouseSensitivity;
  220.         camYRot += horizMovement / horizMouseSensitivity;
  221.      
  222.         // Control looking up and down with the mouse forward/back movement
  223.         // Limit loking up to vertically up
  224.         if (camXRot < -90.0f)
  225.         {
  226.             camXRot = -90.0f;
  227.         }
  228.      
  229.         // Limit looking down to vertically down
  230.         if (camXRot > 90.0f)
  231.         {
  232.             camXRot = 90.0f;
  233.         }
  234.      
  235.         // Looking left and right. Keep the angles in the range -180.0f (anticlockwise turn looking behind) to 180.0f (clockwise turn looking behind)
  236.         if (camYRot < -180.0f)
  237.         {
  238.             camYRot += 360.0f;
  239.         }
  240.      
  241.         if (camYRot > 180.0f)
  242.         {
  243.             camYRot -= 360.0f;
  244.         }
  245.      
  246.         // Reset the mouse position to the centre of the window each frame
  247.         if(!paused)
  248.             Mouse.setCursorPosition(midWindowX, midWindowY);
  249.         cam.setRot(camXRot, camYRot, camZRot);
  250.     }
  251.    
  252.     private static void draw()
  253.     {
  254.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  255.         glLoadIdentity();
  256.         cam.useView();
  257.        
  258.         glPushMatrix();
  259.         glRotatef(x,1,1,1);
  260.        
  261.         wood.bind();
  262.        
  263.         glBegin(GL_QUADS);
  264.         {
  265.             //Front
  266.             glColor3f(1,0,0);
  267.             glTexCoord2f(0,0);glVertex3f(-1,-1,1);
  268.             glTexCoord2f(0,1);glVertex3f(-1,1,1);
  269.             glTexCoord2f(1,1);glVertex3f(1, 1, 1);
  270.             glTexCoord2f(1,0);glVertex3f(1,-1,1);
  271.            
  272.             //back
  273.             glColor3f(0,1,0);
  274.             glTexCoord2f(0,0);glVertex3f(-1,-1,-1);
  275.             glTexCoord2f(0,1);glVertex3f(-1,1,-1);
  276.             glTexCoord2f(1,1);glVertex3f(1, 1, -1);
  277.             glTexCoord2f(1,0);glVertex3f(1,-1,-1);
  278.            
  279.             //bottom
  280.             glColor3f(0,0,1);
  281.             glTexCoord2f(0,0);glVertex3f(-1,-1,-1);
  282.             glTexCoord2f(0,1);glVertex3f(-1,-1,1);
  283.             glTexCoord2f(1,1);glVertex3f(-1,1,1);
  284.             glTexCoord2f(1,0);glVertex3f(-1,1,-1);
  285.            
  286.             //top
  287.             glColor3f(0,1,1);
  288.             glTexCoord2f(0,0);glVertex3f(1,-1,-1);
  289.             glTexCoord2f(0,1);glVertex3f(1,-1,1);
  290.             glTexCoord2f(1,1);glVertex3f(1,1,1);
  291.             glTexCoord2f(1,0);glVertex3f(1,1,-1);
  292.            
  293.             //left
  294.             glColor3f(1,1,0);
  295.             glTexCoord2f(0,0);glVertex3f(-1,-1,-1);
  296.             glTexCoord2f(0,1);glVertex3f(1,-1,-1);
  297.             glTexCoord2f(1,1);glVertex3f(1,-1,1);
  298.             glTexCoord2f(1,0);glVertex3f(-1,-1,1);
  299.            
  300.             //left
  301.             glColor3f(1,0,1);
  302.             glTexCoord2f(0,0);glVertex3f(-1,1,-1);
  303.             glTexCoord2f(0,1);glVertex3f(1,1,-1);
  304.             glTexCoord2f(1,1);glVertex3f(1,1,1);
  305.             glTexCoord2f(1,0);glVertex3f(-1,1,1);
  306.            
  307.         }
  308.         glEnd();
  309.         glPopMatrix();
  310.  
  311.         int xT = 10;
  312.         int yT = 10;
  313.        
  314.         for(int i=0;i<xT; i++)
  315.         {
  316.             for(int y=0;y<yT; y++)
  317.             {
  318.                 glPushMatrix();
  319.                 glTranslatef(-5 * xT/2, 0, -5 * yT/2);
  320.                 glColor3f(1,1,1);
  321.                 grass.bind();
  322.                 glBegin(GL_QUADS);
  323.                 {
  324.                     glTexCoord2f(0,0);glVertex3f(5*i, -2, 5*y);
  325.                     glTexCoord2f(0,1);glVertex3f(5*i, -2, 5*y + 5);
  326.                     glTexCoord2f(1,1);glVertex3f(5*i + 5, -2, 5*y + 5);
  327.                     glTexCoord2f(1,0);glVertex3f(5*i + 5, -2, 5*y);
  328.                 }
  329.                 glEnd();
  330.                 glPopMatrix();
  331.             }
  332.         }
  333.        
  334.         x+=0.1f;
  335.         Display.update();
  336.     }
  337.    
  338.     public static void cleanUp(){
  339.         Display.destroy();
  340.     }
  341.    
  342.  
  343.  
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement