Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  yet another Timer question  (Read 1077 times)
0 Members and 1 Guest are viewing this topic.
Offline BMan

Senior Newbie




Java games rock!


« Posted 2005-06-24 22:04:08 »

I apolgize ahead of time for this question.  I've gone through all the forums trying to figure out how to add a simple little timer
to my 3d pong game made with JOGL.  It seems like lots of folks have different ways of doing it, so would like to ask of a simple one.
This is going to be a very basic game, so trying to avoid 3rd party stuff like GateTimer.

I am doing this in 1.5, so looks like I need to use the nanoTimer().
But would someone be able to supply  a little help on what lines of code to use?

Does all of the Timer code go in display() ? 

Again sorry about this.

Also, as I'm doing this, I am creating a walkthrough/ride along of each step to add a new piece to the game.  I am going to turn it into a tutorial that may be helpful to newbies like myself.  Not sure where the best place to submit/host this would be, if there is anything like that on these sites, please let me know. 

thanks in advance for all the help!!!
Offline zhaoyoubing

Senior Newbie




Java games rock!


« Reply #1 - Posted 2005-06-25 08:13:21 »

What's the timer used for ? for FPS or some other stuff?

As to where to put the timer, I think it depends the thread model you use.

If you use Animator like rendering that call JOGL display in your own thread  then you can put the timer where you like, however, if you leave the rendering thread controlled by system I'm afraid that you have to put the code in display.
Offline BMan

Senior Newbie




Java games rock!


« Reply #2 - Posted 2005-06-25 19:01:58 »

Sorry, poorly described question above.

I am looking for help with a timer to control the movement of my objects: ball/paddles,
So they just don't move based off of the speed of your cpu, but with some control.

thanks!


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BMan

Senior Newbie




Java games rock!


« Reply #3 - Posted 2005-06-25 19:55:37 »

ok, found the following on this forum elsewhere, not sure where to put it, but going to mess around with it.
Any hints would be appreciated.  I'm guessing this goes in the display() method.

int frameCount = 0;
long startTime = System.nanoTime();
final int NANO_FRAME_LENGTH = 1000000000/60; //frame length in nanoseconds for 60fps
while(true)
{
// game code
frameCount++;
bs.show();
while((System.nanoTime()-startTime)/NANO_FRAME_LENGTH <frameCount)
{
  Thread.yield();
}
}
Offline BMan

Senior Newbie




Java games rock!


« Reply #4 - Posted 2005-06-25 20:00:54 »

OK, this may be helpful, I"ll post what I have so far, basically it's a ball moving left to right. 
How can I get this to be constant with the timer, versus just going as fast as your computer flips through the code?
Thanks for help in advance!  Sorry for such newbie question.


import java.awt.*;
import java.awt.event.*;
import net.java.games.jogl.*;

public class Pong2
{
  static Animator animator = null;
 
  static class Renderer
 
    implements GLEventListener, KeyListener
    {
   
     //Variables
    static GLUquadric q;         // Quadratic For Drawing A Sphere
    float ballx = 0.0f, bally = 0.0f, ballz = 0.0f;         //location of ball   

   
    public void display(GLDrawable gLDrawable)
    {
      final GLU glu = gLDrawable.getGLU();
      final GL gl = gLDrawable.getGL();
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
      gl.glLoadIdentity();
     
      //Set up Camera  (first 3 = camara loc, second 3 where we look)
      glu.gluLookAt(0, 10, 10, 0, 0, 0, 0, 1, 0);
        
     //Draw Ball
       gl.glPushMatrix();
        gl.glColor3f(1.0f,1.0f,0.0f);         // Set The Color To Yellow
       gl.glTranslatef(ballx, bally, ballz);
       glu.gluSphere(q, .5f, 32, 16);   
       gl.glEnd();
        gl.glFlush();
        gl.glPopMatrix();
       
        ballx=ballx+.025f;
    }
   
   
   public void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean deviceChanged)
    {
    }
   
    public void init(GLDrawable gLDrawable)
    {
      final GL gl = gLDrawable.getGL();
      final GLU glu = gLDrawable.getGLU();
      gl.glShadeModel(GL.GL_SMOOTH);              // Enable Smooth Shading
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    // Black Background
      gl.glClearDepth(1.0f);                      // Depth Buffer Setup
      gl.glEnable(GL.GL_DEPTH_TEST);                     // Enables Depth Testing
      gl.glDepthFunc(GL.GL_LEQUAL);                        // The Type Of Depth Testing To Do
      gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);   // Really Nice Perspective Calculations
      gLDrawable.addKeyListener(this);
     
      //Initialize for Sphere
      q = glu.gluNewQuadric();                                    // Initialize Quadratic
      glu.gluQuadricNormals(q,GL.GL_SMOOTH);                      // Enable Smooth Normal Generation
      glu.gluQuadricTexture(q,false);                             // Disable Auto Texture Coords
    }

   public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height)
    {
      final GL gl = gLDrawable.getGL();
      final GLU glu = gLDrawable.getGLU();

      if (height <= 0) // avoid a divide by zero error!
        height = 1;
      final float h = (float)width / (float)height;
      gl.glViewport(0, 0, width, height);
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
      glu.gluPerspective(45.0f, h, 1.0, 100.0);
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
    }

    public void keyPressed(KeyEvent e)
    {
      //Checks to see if Escape is pressed
      if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
      {
        animator.stop();
        System.exit(0);
      }
    }

    public void keyReleased(KeyEvent e) {}

    public void keyTyped(KeyEvent e) {}
  }

  public static void main(String[] args)
  {
    //Generic Set up of windowed view
     Frame frame = new Frame("Pong");
    GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
    canvas.addGLEventListener(new Renderer());
    frame.add(canvas);
    frame.setSize(640, 480);
    animator = new Animator(canvas);
    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        animator.stop();
        System.exit(0);
      }
    });
    frame.show();
    animator.start();
    canvas.requestFocus();
  }
}
Offline BMan

Senior Newbie




Java games rock!


« Reply #5 - Posted 2005-06-25 20:16:08 »

OK folks, I think I figured it out.  Seems to be doing what I want...

import java.awt.*;
import java.awt.event.*;
import net.java.games.jogl.*;

public class Pong2
{
  static Animator animator = null;
 
  static class Renderer
 
    implements GLEventListener, KeyListener
    {
   
     //Variables
    static GLUquadric q;         // Quadratic For Drawing A Sphere
    float ballx = 0.0f, bally = 0.0f, ballz = 0.0f;         //location of ball   

   
    public void display(GLDrawable gLDrawable)
    {
      final GLU glu = gLDrawable.getGLU();
      final GL gl = gLDrawable.getGL();
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
      gl.glLoadIdentity();
     
     
      int frameCount = 0;
      long startTime = System.nanoTime();
      final int NANO_FRAME_LENGTH = 1000000000/60; //frame length in nanoseconds for 60fps
      //while(true)
      //{
      //       game code
     
         
        //Set up Camera  (first 3 = camara loc, second 3 where we look)
        glu.gluLookAt(0, 10, 10, 0, 0, 0, 0, 1, 0);
          
       //Draw Ball
         gl.glPushMatrix();
          gl.glColor3f(1.0f,1.0f,0.0f);         // Set The Color To Yellow
         gl.glTranslatef(ballx, bally, ballz);
         glu.gluSphere(q, .5f, 32, 16);   
         gl.glEnd();
          gl.glFlush();
          gl.glPopMatrix();
         
          ballx=ballx+.025f;
         
         
         
         
      frameCount++;
      //bs.show();
      while((System.nanoTime()-startTime)/NANO_FRAME_LENGTH <frameCount)
      {
        Thread.yield();
      }
      //}

     

    }
   
   
   public void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean deviceChanged)
    {
    }
   
    public void init(GLDrawable gLDrawable)
    {
      final GL gl = gLDrawable.getGL();
      final GLU glu = gLDrawable.getGLU();
      gl.glShadeModel(GL.GL_SMOOTH);              // Enable Smooth Shading
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    // Black Background
      gl.glClearDepth(1.0f);                      // Depth Buffer Setup
      gl.glEnable(GL.GL_DEPTH_TEST);                     // Enables Depth Testing
      gl.glDepthFunc(GL.GL_LEQUAL);                        // The Type Of Depth Testing To Do
      gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);   // Really Nice Perspective Calculations
      gLDrawable.addKeyListener(this);
     
      //Initialize for Sphere
      q = glu.gluNewQuadric();                                    // Initialize Quadratic
      glu.gluQuadricNormals(q,GL.GL_SMOOTH);                      // Enable Smooth Normal Generation
      glu.gluQuadricTexture(q,false);                             // Disable Auto Texture Coords
    }

   public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height)
    {
      final GL gl = gLDrawable.getGL();
      final GLU glu = gLDrawable.getGLU();

      if (height <= 0) // avoid a divide by zero error!
        height = 1;
      final float h = (float)width / (float)height;
      gl.glViewport(0, 0, width, height);
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
      glu.gluPerspective(45.0f, h, 1.0, 100.0);
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
    }

    public void keyPressed(KeyEvent e)
    {
      //Checks to see if Escape is pressed
      if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
      {
        animator.stop();
        System.exit(0);
      }
    }

    public void keyReleased(KeyEvent e) {}

    public void keyTyped(KeyEvent e) {}
  }

  public static void main(String[] args)
  {
    //Generic Set up of windowed view
     Frame frame = new Frame("Pong");
    GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
    canvas.addGLEventListener(new Renderer());
    frame.add(canvas);
    frame.setSize(640, 480);
    animator = new Animator(canvas);
    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        animator.stop();
        System.exit(0);
      }
    });
    frame.show();
    animator.start();
    canvas.requestFocus();
  }
}

Offline zhaoyoubing

Senior Newbie




Java games rock!


« Reply #6 - Posted 2005-06-26 03:53:45 »

yes, it can work, but not the best way. The best way should write an custom animator by yourself Smiley

And I don't think using the static class a good method.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (53 views)
2014-04-15 18:08:23

BurntPizza (51 views)
2014-04-15 03:46:01

UprightPath (65 views)
2014-04-14 17:39:50

UprightPath (48 views)
2014-04-14 17:35:47

Porlus (65 views)
2014-04-14 15:48:38

tom_mai78101 (89 views)
2014-04-10 04:04:31

BurntPizza (149 views)
2014-04-08 23:06:04

tom_mai78101 (245 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!