So, after about ten or twelve games made, I noticed that I was re-using a LOT of code from previous projects that usually ended up being the first thirty to forty minutes of every new project. I had heard of people making their own libraries before, and thought I might give it a try.
Anyways, after about two hours, I finally got the first usable version done, which feels awesome.
Now, if I use my library, I only need to right this code…
package com.natekramber;
import mylib.*;
import mylib.gfx.*;
public class Game extends BaseGame {
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public void start() {
new Main(this, "title", 200, 100, 3);
}
public void tick() {
}
public void render() {
Font.draw("Test", 40, 40, Col.get(0x00ffff));
}
}
…to get this result…
I don’t know why, but it feels really good to be able to get something up and running in one minute or less as opposed to thirty.
Cheers,
-Nathan
EDIT: Also, this is the game-loop I’m using…
public void run() {
long lastTime = System.nanoTime();
double unprocessed = 0;
double nsPerTick = 1000000000.0 / 60;
int frames = 0;
int ticks = 0;
long lastTimer1 = System.currentTimeMillis();
init();
while (running) {
long now = System.nanoTime();
unprocessed += (now - lastTime) / nsPerTick;
lastTime = now;
while (unprocessed >= 1) {
ticks++;
tick();
frames++;
render();
unprocessed = 0;
}
if (System.currentTimeMillis() - lastTimer1 > 1000) {
lastTimer1 += 1000;
System.out.println(ticks + " ticks, " + frames + " fps");
frames = 0;
ticks = 0;
}
}
System.exit(0);
}