Within Level.java:
// method to draw the current camera view of the level on the screen
public void drawLevel(Graphics gr, int x, int y)
{
gr.drawImage(cameraImage, x + offsetX, y + offsetY, null);
}
// method to render the actual image before drawing it
public void renderLevel()
{
// keeps track of graphics coordinate
int x, y;
// keeps track of tile to draw
int tileX, tileY;
tileY = coordY;
// draw all the tiles based on offsets, layer by layer
for (y = 0; y < SCREEN_HEIGHT; y += TILE_SIZE)
{
tileX = coordX;
for (x = 0; x < SCREEN_WIDTH; x += TILE_SIZE)
{
// determine which tile to draw based on tile color in array
switch (this.getTile(tileX, tileY))
{
case SPACE_COLOR:
cameraG.drawImage(tiles[0], x, y, null);
break;
case WALL_COLOR:
cameraG.drawImage(tiles[1], x, y, null);
break;
}
tileX++;
}
tileY++;
}
// steps to take in case of an offset
if (offsetX > 0)
{
}
if (offsetX < 0)
{
}
if (offsetY < 0)
{
}
if (offsetY > 0)
{
}
}
// method to update the level's current position for the camera
public void updateLevel(int input)
{
switch (input)
{
// up
case 0:
// update offset up if not too far up
if (coordY > 30)
{
offsetY += 2;
}
// if a tile length has been moved, then offset becomes 0 and coordY is decreased
if (offsetY >= TILE_SIZE)
{
offsetY = 0;
coordY--;
this.renderLevel();
}
break;
// left
case 1:
// update offset to the left if not too far left
if (coordX > 30)
{
offsetX += 2;
}
// if a tile length has been moved, then offset becomes 0 and coordX is decreased
if (offsetX >= TILE_SIZE)
{
offsetX = 0;
coordX--;
this.renderLevel();
}
break;
// right
case 2:
// update offset to the right if not too far right
if (coordX < width - 30)
{
offsetX -= 2;
}
// if a tile length has been moved, then offset becomes 0 and coordX is increased
if (offsetX <= -TILE_SIZE)
{
offsetX = 0;
coordX++;
this.renderLevel();
}
break;
// down
case 3:
// update offset down if not too far down
if (coordY < height - 30)
{
offsetY -= 2;
}
// if a tile legnth has been moved, then offset becomes 0 and coordY is increased
if (offsetY <= -TILE_SIZE)
{
offsetY = 0;
coordY++;
this.renderLevel();
}
break;
// jump
case 4:
break;
}
}
And within LevelRenderer.java:
// Run method for class
public void run(DisplayMode dm)
{
setBackground(Color.WHITE);
s = new Screen();
s.setFullScreen(dm, this);
this.createBufferStrategy( 2 );
buffer = this.getBufferStrategy();
while (!endGame)
{
try
{
// edit player and level
myPlayer.animatePlayer();
myLevel.drawLevel(gr, 0, 0);
myPlayer.drawPlayer(gr, (SCREEN_WIDTH / 2) - TILE_SIZE / 2, (SCREEN_HEIGHT / 2) - TILE_SIZE);
graphics = buffer.getDrawGraphics();
graphics.drawImage(buffer2, 0, 0, null);
if( !buffer.contentsLost() )
{
buffer.show();
}
}
catch (Exception ex)
{
System.err.println("Game Update Error: " + ex);
}
try
{
Thread.sleep(10);
}
catch (Exception ex)
{
System.out.println("Can't sleep!");
}
}
s.restoreScreen();
}
I made sure the code only calls to redraw the map once the user moves past a given offset of the screen where new tiles must be rendered and that it will simply shift the current image drawn of the map until that happens, as opposed to redrawing the entire view every loop. Did I implement this properly, or am I missing something? Your help is greatly appreciated! Btw, I haven't accounted yet for the increased tile sizes, but even still, shouldn't this be significantly faster? Thanks again.
Best regards,
Colton