/** Destroy parts of the Building */ public void destroy(final int x, final int y, final int radius){ Graphics2D graphic = buf.createGraphics(); graphic.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR)); // We don't need a destruction map. This way we could even use different weapons // or randomized damage. graphic.fillOval( (int) (x - getRenderPosition().x) - radius, (int) (y - getRenderPosition().y) - radius, radius*2, radius*2); // NOTE: We are allowed to use an AWT Buffered image, we can even use a graphics draw ontop of the BufferedImage // But no Texture Generation, since that will require an OpenGL Context. if (!Game.getInstance().isTestMode()) { try { slickImg = new org.newdawn.slick.Image(BufferedImageUtil.getTexture(null, buf)); } catch (Exception e) { e.printStackTrace();} } }
public int loadTexture(BufferedImage bi) { Texture texture = null; try { texture = BufferedImageUtil.getTexture("opencv", bi); } catch (IOException e) { e.printStackTrace(); } int textureID = texture.getTextureID(); textures.add(textureID); return textureID; }
private Image convertImage(String path) throws IOException { BufferedImage bufferedImage = ImageIO.read(DuskMoon.class.getResourceAsStream("/"+path)); Texture texture = BufferedImageUtil.getTexture("", bufferedImage); Image image = null; try { image = new Image(texture.getImageWidth(), texture.getImageHeight()); image.setTexture(texture); } catch (SlickException e) { e.printStackTrace(); } return image; }
/** * Used to create the texture to be used on both the player and enemies. Generating this using the * same math as the calculations we used previously (i.e. alpha is based on distance). */ public static void initializeTextures() { int iImageWidth = 128; int iImageHeight = 128; BufferedImage circleImage = new BufferedImage(iImageWidth, iImageHeight, BufferedImage.TYPE_INT_ARGB); float radius = 64.0f; for(int i = 0; i < circleImage.getWidth(); i++) { for(int j = 0; j < circleImage.getHeight(); j++) { float distToCenter = distance[Math.abs((int)(64 - i))][Math.abs((int)(64 - j))]; float fAlphaMultiplier = Math.abs((1 - (distToCenter / radius))); if(distToCenter > 64.0f) fAlphaMultiplier = 0.0f; circleImage.setRGB(i, j, new java.awt.Color(1.0f, 1.0f, 1.0f, fAlphaMultiplier).getRGB() ); } } try { if(texture == null && Display.isCreated()) texture = BufferedImageUtil.getTexture("enemy", circleImage); } catch(IOException e) { System.err.println("Could not load the specified texture."); e.printStackTrace(); } }
/** * Create and store the font * * @param customCharsArray Characters that should be also added to the cache. */ private void createSet( char[] customCharsArray ) { // If there are custom chars then I expand the font texture twice if (customCharsArray != null && customCharsArray.length > 0) { textureWidth *= 2; } // In any case this should be done in other way. Texture with size 512x512 // can maintain only 256 characters with resolution of 32x32. The texture // size should be calculated dynamicaly by looking at character sizes. try { BufferedImage imgTemp = new BufferedImage(textureWidth, textureHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = (Graphics2D) imgTemp.getGraphics(); g.setColor(new Color(255,255,255,1)); g.fillRect(0,0,textureWidth,textureHeight); int rowHeight = 0; int positionX = 0; int positionY = 0; int customCharsLength = ( customCharsArray != null ) ? customCharsArray.length : 0; for (int i = 0; i < 256 + customCharsLength; i++) { // get 0-255 characters and then custom characters char ch = ( i < 256 ) ? (char) i : customCharsArray[i-256]; BufferedImage fontImage = getFontImage(ch); IntObject newIntObject = new IntObject(); newIntObject.width = fontImage.getWidth(); newIntObject.height = fontImage.getHeight(); if (positionX + newIntObject.width >= textureWidth) { positionX = 0; positionY += rowHeight; rowHeight = 0; } newIntObject.storedX = positionX; newIntObject.storedY = positionY; if (newIntObject.height > fontHeight) { fontHeight = newIntObject.height; } if (newIntObject.height > rowHeight) { rowHeight = newIntObject.height; } // Draw it here g.drawImage(fontImage, positionX, positionY, null); positionX += newIntObject.width; if( i < 256 ) { // standard characters charArray[i] = newIntObject; } else { // custom characters customChars.put( new Character( ch ), newIntObject ); } fontImage = null; } fontTexture = BufferedImageUtil .getTexture(font.toString(), imgTemp); } catch (IOException e) { System.err.println("Failed to create font."); e.printStackTrace(); } }
/** Construct a skyscraper * @param topLeftPos * @param buildingWidth * @param mapWidth * @param mapHeight */ public Skyscraper(Vector2f topLeftPos, int buildingWidth, int mapWidth, int mapHeight) { super("Skyscaper"); int height = mapHeight - (int) topLeftPos.y; int width = buildingWidth; // Translate TopLeft to Center Position setPosition(new Vector2f(topLeftPos.x + width / 2, mapHeight - height / 2)); setSize(new Vector2f(width, height)); // We set the size explicitly since we have no render component // Building Random r = new Random(); Color color = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)); // Windows int winSize = 20; // Size of a window [px] int winColumns = r.nextInt(2) + 3; // Amount of vertical columns [3-4] int winSpacing = width / winColumns; // Spacing per Column int winPadding = (winSpacing - winSize) /2; // Padding per Window int winRows = height / winSpacing; // Amount of Horizontal rows // The image needs an Alpha-component so we can erase some parts of it. buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = buf.createGraphics(); /* Draw the Skyscraper Wall */ g.setColor(color); g.fillRect(0, 0, width, height); g.setColor(color.darker()); for(int j = 0; j <= winRows; j++) for(int i = 0; i < winColumns; i++) g.fillRect( (i*winSpacing) + winPadding, (j*winSpacing) + winPadding, winSize, winSize); // NOTE: We are allowed to use an AWT Buffered image, we can even use a graphics draw ontop of the BufferedImage // But no Texture Generation, since that will require an OpenGL Context. if (!Game.getInstance().isTestMode()) { try { slickImg = new org.newdawn.slick.Image(BufferedImageUtil.getTexture(null, buf)); } catch (Exception e) { e.printStackTrace();} } }