Game Development without an engine

Making a game from scratch (in pure Java) has taught me a lot about how games work; from creating my own tilemap loader and game loop to writing logic for a camera, following this tutorial series is really helping me to understand the core concepts better.

Using Java and Java Swing (JFrame / JPanel)

Using Java Swing for the window means I don’t have anything prebuilt in terms of game development. No library like raylib to simply GetKeyPressed(). I had to write a KeyHandler class which implements java.awt.KeyListener and manages public variables for different keys. I had to account for the width and height of the tiles, and manage an array to store different tile images.

Game Loop

The first thing I did was implement a basic loop: update the positions of entities render the screen wait for the frame time to be correct. This was then changed to calculate and use delta time instead of Thread.sleep() for better accuracy. The current loop looks like this:

public void run() {
	double drawInterval = 1_000_000_000 / FPS;  
	double delta = 0;  
	long lastTime = System.nanoTime();  
	long currentTime;
	
	while (gameThread != null) {  
  
    currentTime = System.nanoTime();  
  
    delta += (currentTime - lastTime) / drawInterval;  
    timer += currentTime - lastTime;  
    lastTime = currentTime;  
  
    if (delta >= 1) {  
        update();  
        repaint();  
        delta--;   
    }
}

It calculates the delta time, and if enough time has passed, update() and repaint() perform the update + render logic. Delta time is then decreased by 1. This loops until the game is closed.