Alright, so I wrote like three classes that I am uses to do my rendering in my game. For some reason, when I increase the tick rate of the thread ( increased tick = less sleep time ) the elapsed time of calling a single g.drawString() increases alot. I am thinking even if I have a thread that sleeps for 1ms vs a thread that sleeps for 1000ms, the rendering the thread does should take the same amount of time. I will paste my three classes so you can understand.
Any help at all would be much appreciated
package qo.engine.util;
public abstract class GameThread implements Runnable
{
//boolean for the infinate loop in run method
protected boolean running;
//the sleep time in milliseconds
protected long desiredTickTime;
public GameThread(String threadName, int tick, int priority)
{
//makes this a thread
Thread t = new Thread(this, threadName);
t.setPriority(priority);
//set the sleepTime with a given tick;
desiredTickTime = (1000 / tick) * 1000000;
//starts thread (run will be called first after thread starts)
t.start();
}
//Called when this (a thread in general) is started
//contains the code to make it continue to loop infinatly
public void run()
{
running = true;
long beforeTime, elapsedTime, tillTickMillis, tillTick;
int tillTickNano;
while (running)
{
//see how long it took to run the tick method
beforeTime = System.nanoTime();
tick();
elapsedTime = System.nanoTime() - beforeTime;
System.out.println(elapsedTime + " " + this.toString());
//get the amount of time it should sleep for in nanos
tillTick = desiredTickTime - elapsedTime;
//checks if the clients computer is running too slow to afford
//to sleep or not. tillTick will be < 0 if their
//computer took longer than desiredTickTime
if (tillTick >= 0)
{
//convert to milliseconds
tillTickMillis = (long)(tillTick / 1000000);
//take out the millis, and return the left over nano
tillTickNano = (int)(tillTick % 1000000);
try
{
Thread.sleep(tillTickMillis, (int)tillTickNano);
}
catch (InterruptedException ex)
{
}
}
else //computer took longer than desiredTickTime
{
System.out.println("Skipped a sleep in " + this.toString());
}
}
}
//stops the thread
public void destroy()
{
running = false;
}
//Tick is called every time the thread awakes from the run function
public abstract void tick();
}
package qo.engine.rendering;
import qo.engine.util.GameThread;
import java.awt.*;
import java.util.*;
import qo.engine.graphics.ScreenManager;
public abstract class RenderManager extends GameThread
{
protected static final int FONT_SIZE = 24;
protected static final DisplayMode POSSIBLE_MODES[] = {
new DisplayMode(800, 600, 32, 0),
new DisplayMode(800, 600, 24, 0),
new DisplayMode(800, 600, 16, 0),
new DisplayMode(640, 480, 32, 0),
new DisplayMode(640, 480, 24, 0),
new DisplayMode(640, 480, 16, 0)
};
protected boolean initialized = false;
protected ScreenManager screen;
protected Window window;
protected ArrayList<Object> entities;
//screen.restoreScreen(); used for closing
public RenderManager(String threadName, int TICK)
{
super(threadName, TICK, 8);
screen = new ScreenManager();
DisplayMode displayMode =
screen.findFirstCompatibleMode(POSSIBLE_MODES);
screen.setFullScreen(displayMode);
window = screen.getFullScreenWindow();
window.setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE));
window.setBackground(Color.blue);
window.setForeground(Color.white);
initialized = true;
}
public void tick()
{
if (!initialized)
return;
// draw the screen
Graphics2D g = screen.getGraphics();
//clears the screen
g.fillRect(0,0, screen.getWidth(), screen.getHeight());//maybe use window.getHeight instead ?
draw(g);
g.dispose();
screen.update();
}
public void initEntities(ArrayList<Object> entities)
{
this.entities = entities;
}
public Window getWindow()
{
return window;
}
public ArrayList<Object> getEntities()
{
return entities;
}
//Draw everything in this function
//Function passes the graphics, and every object in the game
public abstract void draw(Graphics2D g);
}
package game;
import qo.engine.rendering.RenderManager;
import java.awt.*;
import java.util.*;
public class RenderProcessor extends RenderManager
{
public RenderProcessor(String threadName, int TICK)
{
super(threadName, TICK);
}
public void draw(Graphics2D g)
{
g.setColor(Color.BLACK);
//g.draw((Rectangle)getEntities().get(0));
g.drawString("hi", 10, 100);
}
}