Hi, I'm new.

Looks like a great community. I was considering posting this in the newbies/problems section but I get the idea this one might be more suitable. Would appreciate a move if it's wrong.
So, I'm looking to create a relatively basic game in Java2D. I know, I've looked into Slick/Pulp a bit and it may make it easier, but I want to try Java2D and my own classes first. Some may call it a waste of time, I see it more as an academic persuit.

If I max out Java2D's capabilities, then great I've understood it, but I don't aim on getting near that point anyway.
At first I was using a basic buffered image as a back buffer and using "passive" rendering, and a very basic game loop. I then read up a bit on active rendering techniques (including buffer strategies, which I didn't even realise could be used in applets!) and using a more sophisicated way of controlling game speed. Before, the inaccuracies of the timing method became more and more pronounced as you wanted the game to speed up. There is a danger of it becoming totally out of sync with the rendering at high speeds.
This forum has been a great help, especially Gudradain and ra4king with the loop/rendering (I hope you don't mind

).
I'd be grateful for any feedback/criticism of this basic functionality before I get deeper into the game's development and find I may have to go back and alter a lot of stuff (though I'm sure this will inevitably happen anyway...). I've just commented on some stuff to help me try to understand what's going on. Maybe it'll be useful for other newbies as well!
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| package games;
import java.applet.Applet; import java.awt.Canvas; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferStrategy;
public class GameApplet extends Applet implements Runnable, MouseListener{ static int width = 800; static int height = 600; Canvas canvas; BufferStrategy bufferStrategy; boolean running = true;
long desiredFPS = 60; long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
Thread gameloop; ImageEntity background; int frameCount = 0; int frameRate = 0; long startTime = System.currentTimeMillis(); @Override public void init() { background = new ImageEntity(this); background.load("bg.jpg"); System.out.println("loaded background"); if (bufferStrategy == null) { setPreferredSize(new Dimension(width, height)); this.setSize(new Dimension(width, height)); this.setIgnoreRepaint(true); canvas = new Canvas(); canvas.setBounds(0, 0, width, height);
canvas.setIgnoreRepaint(true); add(canvas); canvas.createBufferStrategy(2); bufferStrategy = canvas.getBufferStrategy(); canvas.requestFocus(); } }
@Override public void start() { gameloop = new Thread(this); gameloop.start(); running = true; }
@Override public void run() { long beginLoopTime; long endLoopTime; long currentUpdateTime = System.nanoTime(); long lastUpdateTime; long deltaLoop;
while (!isActive()) { Thread.yield(); } while (running) { beginLoopTime = System.nanoTime(); render(); lastUpdateTime = currentUpdateTime; currentUpdateTime = System.nanoTime(); update((int) ((currentUpdateTime - lastUpdateTime) / (1000 * 1000))); endLoopTime = System.nanoTime(); deltaLoop = endLoopTime - beginLoopTime;
frameCount++; if (System.currentTimeMillis() > startTime + 1000) { startTime = System.currentTimeMillis(); frameRate = frameCount; System.out.println(frameRate); frameCount = 0; } if (deltaLoop > desiredDeltaLoop) { } else { try { Thread.sleep((desiredDeltaLoop - deltaLoop) / (1000 * 1000)); } catch (InterruptedException e) { } } if (!isActive()) { System.out.println("Inactive"); return; } } }
@Override public void stop() { running = false; }
public void render() { try { do { do { Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics(); render(g); g.dispose(); } while (bufferStrategy.contentsRestored()); bufferStrategy.show(); } while (bufferStrategy.contentsLost()); } catch (Exception exc) { exc.printStackTrace(); } }
public void update(int deltaTime){ }
protected void render(Graphics2D g) { g.drawImage(background.getImage(), 0, 0, this); }
@Override public void mouseClicked(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); }
@Override public void mousePressed(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); }
@Override public void mouseReleased(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); }
@Override public void mouseEntered(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); }
@Override public void mouseExited(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); }
} |