public VolatileImage renderBackgroundLayer(int LayerNumber) { // create hardware accellerated background layer (Volatile Image) backgroundLayer[LayerNumber] = this.getGraphicsConfiguration() .createCompatibleVolatileImage( loadedLevel.getWidth() * 16, loadedLevel.getHeight() * 16, Transparency.TRANSLUCENT); Graphics2D g2d = backgroundLayer[LayerNumber].createGraphics(); g2d.setComposite(AlphaComposite.Src); // Clear the image. g2d.setColor(new Color(0, 0, 0, 0)); g2d.fillRect( 0, 0, backgroundLayer[LayerNumber].getWidth(), backgroundLayer[LayerNumber].getHeight()); g2d.setBackground(new Color(0, 0, 0, 0)); g2d.setColor(new Color(1f, 1f, 1f, 1f)); for (int i = 0; i < backgroundLayer[LayerNumber].getWidth(this) / backgroundImage[LayerNumber].getWidth(this); i++) { g2d.drawImage( backgroundImage[LayerNumber], i * backgroundImage[LayerNumber].getWidth(this), 0, this); } return backgroundLayer[LayerNumber]; }
public void initialize() { // openLevelFile images: try { boxSpriteSheet = ImageIO.read(new File("ItemContainer.png")); coinSpriteSheet = ImageIO.read(new File("Coin.png")); } catch (Exception e) { } if (openLevelFile == false) { try { loadLevel(initLevel); } catch (Exception e) { System.out.println(e); } } else { loadLevel(FileOpenDialog("Open ...")); } // create tile layer: renderTileLayer(); // create bg layer: backgroundLayer = new VolatileImage[backgroundImage.length]; // render layer: for (int i = 0; i < backgroundImage.length; i++) { renderBackgroundLayer(0); renderBackgroundLayer(1); } // create hardware accellerated rendering layer: renderImage = this.getGraphicsConfiguration() .createCompatibleVolatileImage( loadedLevel.getWidth() * 16, loadedLevel.getHeight() * 16, Transparency.TRANSLUCENT); Graphics2D g2d = renderImage.createGraphics(); g2d.setComposite(AlphaComposite.Src); // Clear the image. g2d.setColor(new Color(0, 0, 0, 0)); g2d.fillRect(0, 0, renderImage.getWidth(), renderImage.getHeight()); g2d.setBackground(new Color(0, 0, 0, 0)); }
public VolatileImage renderTileLayer() { // create hardware accellerated tile layer (Volatile Image) tileLayer = this.getGraphicsConfiguration() .createCompatibleVolatileImage( loadedLevel.getWidth() * 16, loadedLevel.getHeight() * 16, Transparency.TRANSLUCENT); Graphics2D g2d = tileLayer.createGraphics(); g2d.setComposite(AlphaComposite.Src); // Clear the image. g2d.setColor(new Color(0, 0, 0, 0)); g2d.fillRect(0, 0, tileLayer.getWidth(), tileLayer.getHeight()); g2d.setBackground(new Color(0, 0, 0, 0)); g2d.setColor(new Color(1f, 1f, 1f, 1f)); for (int i = 0; i < numberOfTiles; i++) { tile[i].draw(g2d, this); } return tileLayer; }
public static void loadLevel(File levelFile) { // clean up old loads: loadedLevel.clean(); if (levelFile != null) { GameObject[] go = new GameObject[0]; try { loadedLevel = new Level(levelFile.getPath()); camera = loadedLevel.getCamera(); go = loadedLevel.getGameObjects(); } catch (Exception e) { System.out.println(e); } // Reset numberOf ... numberOfBoxes = 0; numberOfSprites = 0; numberOfTiles = 0; for (int i = 0; i < actor.length; i++) { actor[i] = null; } backgroundImage = new Image[2]; try { tileSheet = ImageIO.read(new File(loadedLevel.levelName + "/tilesheet.png")); backgroundImage[0] = ImageIO.read(new File(loadedLevel.levelName + "/bg0.png")); backgroundImage[1] = ImageIO.read(new File(loadedLevel.levelName + "/bg1.png")); } catch (Exception e) { System.out.println("ERROR loading images: " + e); } int MapWidth = loadedLevel.getWidth(); int MapHeight = loadedLevel.getHeight(); for (int y = 0; y < MapHeight; y++) { for (int x = 0; x < MapWidth; x++) { // Number entered in the position represents tileNumber; // position of the sprite x*16, y*16 // get char at position X/Y in the levelLoaded string char CharAtXY = loadedLevel.level.substring(MapWidth * y, loadedLevel.level.length()).charAt(x); // Load objects into the engine/game for (int i = 0; i < go.length; i++) { if (CharAtXY == go[i].objectChar) { try { invoke( "game.objects." + go[i].name, "new" + go[i].name, new Class[] {Point.class}, new Object[] {new Point(x * 16, y * 16)}); } catch (Exception e) { System.out.println("ERROR trying to invoke method: " + e); } } } // Load tiles into engine/game // 48 = '0' , 57 = '9' if ((int) CharAtXY >= 48 && (int) CharAtXY <= 57) { tileObject[gameMain.numberOfTiles] = new WorldTile(Integer.parseInt(CharAtXY + "")); tileObject[gameMain.numberOfTiles - 1].sprite.setPosition(x * 16, y * 16); } } } // clean up: loadedLevel.clean(); // additional game-specific loading options: camera.forceSetPosition(new Point(mario.spawn.x, camera.prefHeight)); pCoin = new PopupCoin(new Point(-80, -80)); levelLoaded = true; } else { System.out.println("Loading cancelled..."); } }
// -- Main Loop public void run() { // called only once: initialize(); // create me a timer Timer t = new Timer(); // start main loop: while (true) { if (levelLoaded == true) { camera.follow(mario.sprite); // act() all actors that are actable: int a = 0; while (actor[a] != null && actor[a] instanceof Actable) { Actable actable = (Actable) actor[a]; actable.act(); a++; } pCoin.fly(); for (int i = 0; i < numberOfBoxes; i++) { try { box[i].open(); } catch (Exception e) { System.out.println("ERROR: " + e); } } for (int i = 0; i < numberOfCoins; i++) { try { coin[i].collect(); } catch (Exception e) { System.out.println("ERROR: " + e); } } // reset mario if fallen off from screen: if (mario.sprite.posy > loadedLevel.getHeight() * 16) { camera.position = new Point(width / 2, camera.prefHeight + camera.tolerance); mario.sprite.setPosition(new Point(gameMain.mario.spawn.x, gameMain.mario.spawn.y)); } } try { // Draw to panel if not Fullscreen if (fullscreen == false) { t.start(); render(); repaint(); System.out.println("FPS: " + (int) (((100 / (double) t.stop())) * 2)); } else { t.start(); long sleeptime = 5 - t.stop(); // calculate sleep time (max fps) if (sleeptime < 0) { sleeptime = 0; } main.sleep(1L + sleeptime); fps = (int) (((100 / (double) t.stop())) * 2); System.out.println("FPS: " + fps); } } catch (Exception e) { } } }