Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / OpenGL Development / Re: getting started on: 2011-07-07 00:10:05
Aren't those using the old way of doing things?

I want a skeleton of the most basic code you need to have a LWJGL 3.x+ application up and running, not GL11?

Unless I'm misunderstanding something...
2  Java Game APIs & Engines / OpenGL Development / Re: getting started on: 2011-07-06 14:27:23
Know where I can find a skeleton of a 3.x+ LWJGL application to start experimenting and learning with?
3  Java Game APIs & Engines / OpenGL Development / Re: getting started on: 2011-07-06 03:17:40
So if you're new, and with OpenGL ES2 and version 4+ where I hear things are very different from how they used to be, should you focus on learning the new rather than the old?

Does LWJGL work just fine with a 4.1 approach? Most tutorials/examples I see use the GL11 import, I assume that stands for version 1.1? And that means it is using the old approach?
4  Game Development / Newbie & Debugging Questions / Re: Graphical User Interface - Buttons on: 2011-07-01 08:26:21
Is there a nice way to pull those FontMetrics calculations out of the draw method for your button? It seems silly do be doing them every frame when you would only need to do them once, or when you change the font.

I know there is a deprecated method you can use:

FontMetrics fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
5  Game Development / Game Mechanics / Re: Slight jerkiness on: 2011-05-31 15:13:39
Nope, I need the time before each loop begins. I use it, and the time after the render, to figure out how long my sleep should be. If I were to just sleep at a constant rate, say, 1000/60, thinking it is 60 frames per second, that would be ignoring how much time it actually takes to run the game loop, which will vary depending upon what is happening in the game.
6  Game Development / Game Mechanics / Slight jerkiness on: 2011-05-31 14:26:30
This has been bothering me all morning.

I have created the base of a custom game engine, and a test program using it that simply has some circles bouncing around the screen. It seems like there is some slight jerkiness to the movement, every half second or so, and I have no idea what is causing it.

If you have any thoughts, it would be much appreciated. Also, if you notice any glaring inefficiencies that I'd want to take care of, please do let me know.

Since the code is still relatively small, I'll post it in its entirety:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
package game;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;

/**
 * Base component for games and other similar graphics intensive
 * applications.
 *
 * Usage: Create instance of a GameComponent subclass, add to a
 * Container, and call the GameComponent's start() method. Parent
 * must be made visible prior to calling start(). Override update()
 * and draw() for custom game content.
 *
 * @author removed for forum
 *
 */

public abstract class GameComponent extends Canvas implements Runnable {

   private static final long nanosecondsPerSecond = 1000000000L;
   private static final long millisecondsPerNanosecond = 1000000L;

   protected int frameRate; // how many times per second to (ideally) run the game loop
   private long timePerFrame; // nanoseconds per frame based on framerate

   protected Dimension resolution; // holds the width/height of game's canvas

   private Image drawImage;
   protected Graphics2D drawGraphics; // reference to drawImage's graphics

   protected Color backgroundColor = Color.BLACK;

   protected boolean running;

   // creates GameComponent with default resolution of 800x600 at 60 fps
   public GameComponent() {
      this(new Dimension(800,600), 60);
   }

   // creates GameComponent with passed resolution at 60 fps
   public GameComponent(Dimension resolution) {
      this(resolution, 60);
   }

   // creates GameComponent with passed resolution at passed fps
   public GameComponent(Dimension resolution, int frameRate) {
      this.resolution = resolution;
      this.frameRate = frameRate;

      timePerFrame = nanosecondsPerSecond / frameRate;
      setPreferredSize(resolution);
   }

   // final set up and starts game loop
   public void start() {
      createBufferStrategy(2); // double buffering
      running = true;
      new Thread(this).start(); // start game loop
   }

   @Override public void run() {
      // game loop
      while(running) {
         long timeBeforeLoop = System.nanoTime(); // time when game loop starts

         update();
         draw();
         render();

         try {
            Thread.sleep(calculateSleepTime(timeBeforeLoop));
         } catch (InterruptedException e) {}
      }
   }

   // update everything in the game, should be overridden
   public void update() {}

   // draw everything in the game, should be overridden
   public void draw() {
      if(drawImage == null)
         drawImage = createImage((int)resolution.getWidth(), (int)resolution.getHeight());
     
      drawGraphics = (Graphics2D) drawImage.getGraphics();
      drawGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

      drawGraphics.setColor(backgroundColor);
      drawGraphics.fillRect(0, 0, getWidth(), getHeight()); // draw background
   }

   // render the graphics, buffer strategy uses volatile images, so deal with possible content loss
   private void render() {
      drawGraphics.dispose();
      BufferStrategy strategy = getBufferStrategy();
      do {
         do {
            Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
            g.drawImage(drawImage, 0, 0, null);
            g.dispose();
         } while (strategy.contentsRestored());

         strategy.show(); // Display the buffer
      } while (strategy.contentsLost()); // Repeat the rendering if the drawing buffer was lost
      Toolkit.getDefaultToolkit().sync();
   }

   // figure out how long the thread should sleep to keep a consistent frame rate
   private long calculateSleepTime(long beforeLoop) {
      long afterLoop = System.nanoTime(); // get time after loop
      long difference = afterLoop - beforeLoop; // calculate the difference
      long timeToSleep = (timePerFrame - difference) / millisecondsPerNanosecond; // calculate time to sleep in milliseconds

      // if you see this in the console when running, it means the game loop isn't keeping up with frame rate
      if(timeToSleep < 0)
         System.out.println("sleep time is < 0");

      return (timeToSleep > 0) ? timeToSleep : 0; // if timeToSleep is < 0, frame took longer than timePerFrame, so sleep time is 0
   }
}




And here is the test game using the above component:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
package game.test;

import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import game.GameComponent;
import javax.swing.JFrame;

public class GameTest extends GameComponent {
   private static final int NUM_CIRCLES = 5;
   private static final Circle[] circles = new Circle[NUM_CIRCLES];
   
   private static Random random = new Random();
   
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      GameComponent game = new GameTest();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(game);
      frame.pack();
      frame.setVisible(true);
      game.start();
   }
   
   public GameTest() {
      // create some random circles
      for(int i=0; i<circles.length; i++) {
         Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256),random.nextInt(256));
         int radius = random.nextInt(100) + 30;
         int x = random.nextInt(800-radius);
         int y = random.nextInt(600-radius);
         circles[i] = new Circle(x, y, radius, color);
      }
   }
   
   @Override public void update() {
      super.update();
     
      for(Circle circle : circles)
         circle.update();
   }
   
   @Override public void draw() {
      super.draw();
     
      for(Circle circle : circles)
         circle.draw();
   }
   
   class Circle {
      private int x, y, width, height, xVelocity, yVelocity;
      private Color color;
     
      public Circle(int x, int y, int radius, Color color) {
         this.x = x;
         this.y = y;
         width = height = radius;
         this.color = color;
         xVelocity = random.nextInt(6) + 4;
         //yVelocity = random.nextInt(6) + 4;
      }
     
      public void update() {
         if(x < 0 || x > getWidth()-width) // check left/right bounds
            xVelocity *= -1; // reverse horizontal velocity
         if(y < 0 || y > getHeight()-height) // check top/bottom bounds
            yVelocity *= -1; // reverse vertical velocity
         
         x += xVelocity;
         y += yVelocity;
      }
     
      public void draw() {
         drawGraphics.setColor(color);
         Shape circle = new Ellipse2D.Float(x, y, width, height);
         drawGraphics.fill(circle);
      }
   }
}


Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

PocketCrafter7 (12 views)
2014-11-28 16:25:35

PocketCrafter7 (8 views)
2014-11-28 16:25:09

PocketCrafter7 (9 views)
2014-11-28 16:24:29

toopeicgaming1999 (76 views)
2014-11-26 15:22:04

toopeicgaming1999 (66 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!