Show Posts
|
|
Pages: [1]
|
|
3
|
Java Game APIs & Engines / OpenGL Development / Re: getting started
|
on: 2011-07-06 05: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 10: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 17: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 16: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;
public abstract class GameComponent extends Canvas implements Runnable {
private static final long nanosecondsPerSecond = 1000000000L; private static final long millisecondsPerNanosecond = 1000000L;
protected int frameRate; private long timePerFrame; protected Dimension resolution; private Image drawImage; protected Graphics2D drawGraphics; protected Color backgroundColor = Color.BLACK;
protected boolean running;
public GameComponent() { this(new Dimension(800,600), 60); }
public GameComponent(Dimension resolution) { this(resolution, 60); }
public GameComponent(Dimension resolution, int frameRate) { this.resolution = resolution; this.frameRate = frameRate;
timePerFrame = nanosecondsPerSecond / frameRate; setPreferredSize(resolution); }
public void start() { createBufferStrategy(2); running = true; new Thread(this).start(); }
@Override public void run() { while(running) { long timeBeforeLoop = System.nanoTime(); update(); draw(); render();
try { Thread.sleep(calculateSleepTime(timeBeforeLoop)); } catch (InterruptedException e) {} } }
public void update() {}
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()); }
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(); } while (strategy.contentsLost()); Toolkit.getDefaultToolkit().sync(); }
private long calculateSleepTime(long beforeLoop) { long afterLoop = System.nanoTime(); long difference = afterLoop - beforeLoop; long timeToSleep = (timePerFrame - difference) / millisecondsPerNanosecond; if(timeToSleep < 0) System.out.println("sleep time is < 0");
return (timeToSleep > 0) ? timeToSleep : 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() { 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; } public void update() { if(x < 0 || x > getWidth()-width) xVelocity *= -1; if(y < 0 || y > getHeight()-height) yVelocity *= -1; x += xVelocity; y += yVelocity; } public void draw() { drawGraphics.setColor(color); Shape circle = new Ellipse2D.Float(x, y, width, height); drawGraphics.fill(circle); } } } |
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|