Reputation: 2407
I am developing a platform game that loads level's from images. It loops trough all the pixels of the image and checks if the RGB value matches a certain object, for example yellow for coins, white for walls.
Once the player dies the level gets reloaded, however when there is also a turret in the scene the game drops in frame rate.
The method that loops trough all the pixels.
// Loads the requested level based on a image
public void loadLevel(BufferedImage Image)
{
clearLevel();
int imageWidth = Image.getWidth();
int imageHeight = Image.getHeight();
currentLevel = Image;
for(int Par1 = 0; Par1 < imageHeight; Par1++)
{
for(int Par2 = 0; Par2 < imageWidth; Par2++)
{
int currentPixel = Image.getRGB(Par1, Par2);
int redValue = (currentPixel >> 16) & 0xff;
int greenValue = (currentPixel >> 8) & 0xff;
int blueValue = (currentPixel) & 0xff;
if(redValue == 255 && greenValue == 255 && blueValue == 255)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 0, 0, 0, Identifier.Block));
}
if(redValue == 255 && greenValue == 0 && blueValue == 0)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 1, 0, 0, Identifier.Lava));
}
if(redValue == 255 && greenValue == 0 && blueValue == 255)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 5, 0, 8, Identifier.Platform));
}
if(redValue == 0 && greenValue == 255 && blueValue == 255)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 4, 1, 8, Identifier.Platform));
}
if(redValue == 255 && greenValue == 255 && blueValue == 0)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 2, 0, 0, Identifier.Coin));
}
if(redValue == 0 && greenValue == 255 && blueValue == 0)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 3, 0, 0, Identifier.Flag));
}
if(redValue == 0 && greenValue == 0 && blueValue == 255)
{
addObject(new Player((Par1 * 32), (Par2 * 32), this, Identifier.Player));
}
if(redValue == 255 && greenValue == 175 && blueValue == 0)
{
addObject(new Turret((Par1 * 32), (Par2 * 32), this, Identifier.Turret));
}
}
}
Main.currentState = Main.State.Game;
}
// Reload level
public void reloadLevel()
{
loadLevel(currentLevel);
}
The gameloop
// Runs when the thread starts, also starts the game loop and creates a separate tick and frames per second line
public void run()
{
try
{
preInitialisation();
initialisation();
LogHandler.log("Initialisation complete.");
}
catch(FileNotFoundException Error)
{
Error.printStackTrace();
}
catch(FontFormatException Error)
{
Error.printStackTrace();
}
catch(IOException Error)
{
Error.printStackTrace();
}
requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double nanoSeconds = 1000000000 / amountOfTicks;
double deltaValue = 0;
long currentTime;
long loopTimer = System.currentTimeMillis();
while(isRunning)
{
currentTime = System.nanoTime();
deltaValue += (currentTime - lastTime) / nanoSeconds;
lastTime = currentTime;
while(deltaValue >= 1)
{
tick();
deltaValue--;
temporaryTicks++;
}
render();
temporaryFrames++;
if(System.currentTimeMillis() - loopTimer > 1000)
{
loopTimer += 1000;
absoluteFrames = temporaryFrames;
absoluteTicks = temporaryTicks;
temporaryTicks = 0;
temporaryFrames = 0;
}
}
}
Upvotes: 2
Views: 199
Reputation: 2407
Turned out adding the LWJGL libary and then enable vsync worked out.
Display.setVSyncEnabled(true);
I also changed all of the LinkedLists to ArrayLists since its way faster, When to use LinkedList over ArrayList?
Upvotes: 3