/** * Renders the map to the canvas. * @param g The graphics object to use to render things to the canvas with. * @param area The area that the player is currently in. * @param p The player. * @param width The width of the canvas. * @param height The height of the canvas. */publicvoidrender(finalGraphicsg, finalAreaarea, finalCamerac, finalintwidth, finalintheight, finalintscaleMultiplier) {intareaID = area.getMapID();if(areaID != currentMapID || currentMapID == -1) {loadMapFromFile(areaID);currentMapID = areaID; }

intxOffset = c.getOriginX() >> 5; // The x-axis tile coordinate of where the cameras origin is currently.intyOffset = c.getOriginY() >> 5; // The y-axis tile coordinate of where the cameras origin is currently.intnumberOfXTiles = (width >> 5); // Takes whatever the width of the canvas is, divides it by 32 and then that's then number of XTiles to draw.intnumberOfYTiles = (height >> 5); // Takes whatever the height of the canvas is, divides it by 32 and then that's then number of YTiles to draw.

publicclassCamera {privateintmapSizeInPixels;/** The origin for the are that the camera is currently viewing. */privateintoriginX = 0, originY = 0;/** The start and end bounds of the map. */privateintstart = 0, end;

/** * The constructor for the player object. * @param nameIn The name of the player being constructed. * @param playerLevelIn The level of the player being constructed. * @param playerMaxLevelIn The maximum level of the player being constructed. * @param combatTypeIn The combat type of the player being constructed. */publicPlayer(finalStringnameIn, finalintplayerLevelIn, finalintplayerMaxLevelIn, finalCombatTypecombatTypeIn) {setName(nameIn);setUsesArmor(true);setUsesWeapon(true);setLevel(playerLevelIn);setMaxLevel(playerMaxLevelIn);setExperience(0);doublehealth = (playerLevelIn * 25.0);setHealth(health);setMaxHealth(health);

/** * This class defines a creature object. * * @author Valkryst * --- Last Edit 24-Oct-2013 */publicclassCreatureextendsEntity {publicfinalAITacticsTACTICS = newAITactics();/** The id of the creature. */finalintCREATURE_ID;/** The drop table of the creature. */privateDropsdrops;/** Whether the creature has been looted or not. If the creature has been looted then it can be removed from the area it's on. */privatebooleanhasBeenLooted = false;

/** * The constructor method for the Creature object. * @param creatureIdIn The ID of the creature being created. * @param nameIn The name of the creature being created. * @param creatureLevelIn The level of the creature being created. * @param creatureMaxLevelIn The maximum level of the creature being created. * @param combatTypeIn The combat type of the creature being created. */publicCreature(finalintcreatureIdIn, finalStringnameIn, finalintcreatureLevelIn, finalintcreatureMaxLevelIn, finalCombatTypecombatTypeIn) {CREATURE_ID = creatureIdIn; // Set the creature's ID.setName(nameIn);// Set the creature's name.

// Keep looping until the game ends.while (isGameRunning) {longnow = System.nanoTime();longupdateLength = now - lastLoopTime;lastLoopTime = now;delta += updateLength / ((double)OPTIMAL_TIME); // Work out how long its been since the last update. This will be used to calculate how far the entities should move this loop.

//Update the game's logic and then render the screen.while(delta >= 1) {updateLogic(delta);delta--; }

render();

// we want each frame to take 10 milliseconds, to do this// we've recorded when we started the frame. We add 10 milliseconds// to this and then factor in the current time to give // us our final value to wait for// remember this is in ms, whereas our lastLoopTime etc. vars are in ns.try {longtempLong = (lastLoopTime-System.nanoTime() + OPTIMAL_TIME)/1000000;if(tempLong <= 0) { continue; } // Skips the sleep()Thread.sleep(tempLong); } catch (InterruptedExceptione) {continue; } }stop(); }

// When called this updates all of the game's logic.publicvoidupdateLogic(doubledelta) {Cast.setCurrentTime();currentArea.pruneCreatures(); // Removes all creatures that have been looted from the area.currentArea.updateLogic(camera); }

Well, in Java2D I would recommend creating a rectangle that is the same size of the viewport, and every time the camera position changes, check through all the tiles and see if they are intersecting rectangle, if they are, render them.

Well, in Java2D I would recommend creating a rectangle that is the same size of the viewport, and every time the camera position changes, check through all the tiles and see if they are intersecting rectangle, if they are, render them.

Wouldn't that require me to create rectangles for every single tile and test for intersection every time?

Draw only tiles on screen:i dont know how you have your map, but if it is something like this:0,0,0,0,0,0,0,0,0,00,0,0,0,0,0,0,0,0,00,0,0,0,0,0,0,0,0,00,0,0,0,0,0,0,0,0,00,0,0,0,0,0,0,0,0,00,0,0,0,0,0,0,0,0,00,0,0,0,0,0,0,0,0,0

you can loop each tile and calculate x and y position. if position is on screen you can draw.

Use the same method with player, enemies, objects etc.Calculate first your "camera" position (i calculate mine with left-top corner and add width and height) and check if everything is on screen to draw.To use zoom, g.draw(image, x , y, ZOOM x, ZOOM y, null).I don't see much difference with performance scaling this before draw. Just do it for everything you need to draw.

This is pseudo-code. Sorry for my english.If you need more help show me your map file.

It must always have the same width and height, I'll probably change that limitation eventually.

I've managed to figure out your example, mostly. I'm not sure how the for loops you wrote so I changed them to what I thought would work but the rest was fairly easy to integrate with my camera. The code I currently have is:

publicvoidcheckBounds() {// Checks if the originX is below 0, if it is then set it to 0.if(originX < 0) {originX = 0; }

// Checks if the originY is below 0, if it is then set it to 0.if(originY < mapSizeInPixels) {originY = 0; }

// Checks if the originX is nearing the end of the screen on the x-axis, if it is then set it to the map size in pixels minus the screen width.if(originX > ((originX + widthOfScreen) - mapSizeInPixels)) {originX = mapSizeInPixels - widthOfScreen; }

// Checks if the originY is nearing the end of the screen on the y-axis, if it is then set it to the map size in pixels minus the screen height.if(originY > ((originY + heightOfScreen) - mapSizeInPixels)) {originY = mapSizeInPixels - heightOfScreen; } }

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org