2

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;
        }
    }
}
Trevi Awater
  • 2,387
  • 2
  • 31
  • 53
  • Provide the necessary code here and do not link external sources please. – Thomas Uhrig Jun 04 '14 at 19:19
  • Totally unrelated but possibly helpful in the long run: take a look at maven (or anything similar) to standardize your project, making it easier for anyone to download, build and avoid committing your 3rd party libraries – Morfic Jun 04 '14 at 19:32
  • Thanks for editing. Can you explain a little bit more what you mean with "the frame rate drops"? Are your animations slowing down? Is it getting better after a while? – Thomas Uhrig Jun 04 '14 at 19:38
  • In fact the frames go down to 1 per second exactly according to my counter. Animations and keyinput are slow and only update once a second. It does get better when I kill the player (reload the image) a couple more times. Already tried running it with more memory or on a different OS but no result. – Trevi Awater Jun 04 '14 at 19:42
  • Also something interesting is that whenever the frames drop the ticks are getting up to redicules levels, above 220 while they are not supposed to go over 60. the thread is now also posted in the question. – Trevi Awater Jun 04 '14 at 20:00
  • And then the tread kills itself. – Trevi Awater Jun 04 '14 at 20:31

1 Answers1

3

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?

Community
  • 1
  • 1
Trevi Awater
  • 2,387
  • 2
  • 31
  • 53