Advertisement
Cesar_Biker

Problem

May 15th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. package Actualitzacions;
  2.  
  3. import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
  4. import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
  5. import static org.lwjgl.opengl.GL11.GL_PROJECTION;
  6. import static org.lwjgl.opengl.GL11.GL_QUADS;
  7. import static org.lwjgl.opengl.GL11.glBegin;
  8. import static org.lwjgl.opengl.GL11.glClear;
  9. import static org.lwjgl.opengl.GL11.glClearColor;
  10. import static org.lwjgl.opengl.GL11.glColor3f;
  11. import static org.lwjgl.opengl.GL11.glEnd;
  12. import static org.lwjgl.opengl.GL11.glLoadIdentity;
  13. import static org.lwjgl.opengl.GL11.glMatrixMode;
  14. import static org.lwjgl.opengl.GL11.glOrtho;
  15. import static org.lwjgl.opengl.GL11.glPopMatrix;
  16. import static org.lwjgl.opengl.GL11.glPushMatrix;
  17. import static org.lwjgl.opengl.GL11.glRotatef;
  18. import static org.lwjgl.opengl.GL11.glTranslatef;
  19. import static org.lwjgl.opengl.GL11.glVertex2d;
  20.  
  21. import org.lwjgl.LWJGLException;
  22. import org.lwjgl.Sys;
  23. import org.lwjgl.input.Keyboard;
  24. import org.lwjgl.opengl.Display;
  25. import org.lwjgl.opengl.DisplayMode;
  26.  
  27. public class SenseLimit
  28. {
  29.     private static long tempsUltimFrame;
  30.     private static float rotacio,x = 300f,y = 200f;
  31.     private static double passat = 0;
  32.    
  33.     public static void main(String args[])
  34.     {
  35.         try
  36.         {
  37.             Display.setDisplayMode(new DisplayMode(600,400));
  38.             Display.setTitle("Actualitzacions");
  39.             Display.create();
  40.         }
  41.         catch(LWJGLException e) {
  42.             e.printStackTrace();
  43.         }
  44.        
  45.         getDelta();
  46.         iniciar();
  47.        
  48.         while(!Display.isCloseRequested())
  49.         {
  50.             double delta = getDelta();
  51.             passat += delta;//nem afegin els segons que passen per cada frame
  52.             System.out.println(passat);
  53.             if(passat >= 1000.0/60.0)//si els segons que han passat son mes o iguals que els qe tenen qe
  54.             //pasar perqe hi hagin 60 actualitzacions per segon, actualitzem
  55.             {
  56.                 System.out.println("ya");
  57.                 actualitzar(passat);//actualitzem depenen dels segons qe hagin passat, per tindre velocitat constant
  58.                 passat = 0;
  59.             }
  60.             renderitzar();
  61.             Display.update();
  62.         }
  63.         Display.destroy();
  64.     }
  65.  
  66.     public static int getDelta()//obte quants milisegons han passat desde l'ultim frame
  67.     {
  68.         long tempsms = (Sys.getTime() * 1000) / Sys.getTimerResolution();//obté temps en milisegons
  69.         int delta = (int) (tempsms - tempsUltimFrame);
  70.         tempsUltimFrame = tempsms;
  71.        
  72.         return delta;
  73.        
  74.     }
  75.    
  76.     public static void iniciar()
  77.     {
  78.         glMatrixMode(GL_PROJECTION);
  79.         glLoadIdentity();
  80.         glOrtho(0,600,0,400,1,-1);
  81.         glMatrixMode(GL_MODELVIEW);
  82.         glClearColor(0,0,0,0);
  83.     }
  84.    
  85.     public static void actualitzar(double delta)
  86.     {
  87.         if(Keyboard.isKeyDown(Keyboard.KEY_UP))
  88.             //rotacio += 0.1f * delta;
  89.             y += 0.5f * delta;
  90.         if(Keyboard.isKeyDown(Keyboard.KEY_DOWN))
  91.             //rotacio -= 0.1f * delta;
  92.             y -= 0.5f * delta;
  93.     }
  94.    
  95.     public static void renderitzar()
  96.     {
  97.         glClear(GL_COLOR_BUFFER_BIT);
  98.         glColor3f(0.f,0.5f,1.0f);
  99.        
  100.         glPushMatrix();
  101.        
  102.         glTranslatef(x,y,0);
  103.         glRotatef(rotacio, 0f, 0f, 1f);
  104.         glTranslatef(-x,-y,0);
  105.        
  106.         glBegin(GL_QUADS);
  107.             glVertex2d(x-50,y-50);
  108.             glVertex2d(x+50,y-50);
  109.             glVertex2d(x+50,y+50);
  110.             glVertex2d(x-50,y+50);
  111.         glEnd();
  112.         glPopMatrix();
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement