@Override public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException { g.resetTransform(); g.clear(); float lw = 400.0f; float lh = 50.0f; float lx = (Globals.WIDTH / 2) - (lw / 2); float ly = (Globals.HEIGHT / 2) - (lh / 2); float loadWidth = lw * percentLoaded; g.setColor(new Color(0x808080)); g.fillRect(lx, ly, lw, lh); g.setColor(new Color(0x9B2111)); g.fillRect(lx, ly, loadWidth, lh); g.setColor(Color.white); g.drawRect(lx, ly, lw, lh); g.setColor(Color.white); UnicodeFont uni = assets.getFont("PressStart2P-Regular_large"); if(uni != null) { g.setFont(uni); FontUtils.drawCenter(uni, "Loading...", ((Globals.WIDTH / 2) - 200), (int)(ly - uni.getLineHeight() - 10), (int)lw, g.getColor()); } }
@SuppressWarnings("unchecked") public void addFont(String key, String file, int size, boolean bold, boolean italic, Effect [] effects) throws SlickException { try { AssetManager.ASSETS_TO_LOAD++; UnicodeFont uni = new UnicodeFont(file, size, bold, italic); uni.addAsciiGlyphs(); uni.addGlyphs(400, 600); uni.getEffects().add(new ColorEffect(Color.WHITE)); uni.getEffects().addAll(Arrays.asList(effects)); uni.loadGlyphs(); if((fonts != null) && (uni != null)) { fonts.put(key, uni); AssetManager.ASSETS_LOADED++; System.out.println(String.format("Font Loaded: %s", key)); } } catch(Exception ex) { ex.printStackTrace(); System.out.printf("ERROR: Font \"%s\" could not be loaded!\n", file); } }
/** * Initializes all fonts. * @throws SlickException if ASCII glyphs could not be loaded * @throws FontFormatException if any font stream data does not contain the required font tables * @throws IOException if a font stream cannot be completely read */ public static void init() throws SlickException, FontFormatException, IOException { float fontBase = 12f * GameImage.getUIscale(); Font javaFont = Font.createFont(Font.TRUETYPE_FONT, ResourceLoader.getResourceAsStream(Constants.FONT_NAME)); Font font = javaFont.deriveFont(Font.PLAIN, (int) (fontBase * 4 / 3)); DEFAULT = new UnicodeFont(font); BOLD = new UnicodeFont(font.deriveFont(Font.BOLD)); XLARGE = new UnicodeFont(font.deriveFont(fontBase * 3)); LARGE = new UnicodeFont(font.deriveFont(fontBase * 2)); MEDIUM = new UnicodeFont(font.deriveFont(fontBase * 3 / 2)); MEDIUMBOLD = new UnicodeFont(font.deriveFont(Font.BOLD, fontBase * 3 / 2)); SMALL = new UnicodeFont(font.deriveFont(fontBase)); SMALLBOLD = new UnicodeFont(font.deriveFont(Font.BOLD, fontBase)); ColorEffect colorEffect = new ColorEffect(); loadFont(DEFAULT, colorEffect); loadFont(BOLD, colorEffect); loadFont(XLARGE, colorEffect); loadFont(LARGE, colorEffect); loadFont(MEDIUM, colorEffect); loadFont(MEDIUMBOLD, colorEffect); loadFont(SMALL, colorEffect); loadFont(SMALLBOLD, colorEffect); }
/** * Adds and loads glyphs for a font. * @param font the font to add the glyphs to * @param s the string containing the glyphs to load */ public static void loadGlyphs(UnicodeFont font, String s) { if (s == null || s.isEmpty()) return; // get set of added strings HashSet<String> set = loadedGlyphs.get(font); if (set == null) { set = new HashSet<String>(); loadedGlyphs.put(font, set); } else if (set.contains(s)) return; // string already in set // load glyphs font.addGlyphs(s); set.add(s); try { font.loadGlyphs(); } catch (SlickException e) { Log.warn(String.format("Failed to load glyphs for string '%s'.", s), e); } }
@SuppressWarnings("unchecked") public UnicodeFontRenderer(Font awtFont) { super(Minecraft.getMinecraft().gameSettings, new ResourceLocation("textures/font/ascii.png"), Minecraft.getMinecraft().getTextureManager(), false); font = new UnicodeFont(awtFont); font.addAsciiGlyphs(); font.getEffects().add(new ColorEffect(Color.WHITE)); try { font.loadGlyphs(); } catch (SlickException exception) { throw new RuntimeException(exception); } String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"; FONT_HEIGHT = font.getHeight(alphabet) / 4; }
/** * @see org.newdawn.slick.font.effects.Effect#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, org.newdawn.slick.UnicodeFont, org.newdawn.slick.font.Glyph) */ public void draw(BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) { g = (Graphics2D)g.create(); g.translate(xDistance, yDistance); g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), Math.round(opacity * 255))); g.fill(glyph.getShape()); // Also shadow the outline, if one exists. for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext();) { Effect effect = (Effect)iter.next(); if (effect instanceof OutlineEffect) { Composite composite = g.getComposite(); g.setComposite(AlphaComposite.Src); // Prevent shadow and outline shadow alpha from combining. g.setStroke(((OutlineEffect)effect).getStroke()); g.draw(glyph.getShape()); g.setComposite(composite); break; } } g.dispose(); if (blurKernelSize > 1 && blurKernelSize < NUM_KERNELS && blurPasses > 0) blur(image); }
public static float getWidth(String s, double dScale) { UnicodeFont unicodeFont = null;// = fontStyleList.get(0).get(4); int iFontIndex = 0; //grab the font that is closest to the scale we are drawing. for(int i = 0; i < fontSizes.length; i++) { if(iStandardSize * dScale * resolutionScale <= fontSizes[i]) { unicodeFont = currentFont.get(i); iFontIndex = i; break; } } float fFontScale = (float) (((float)iStandardSize * dScale * resolutionScale) / fontSizes[iFontIndex] ); return (float) (unicodeFont.getWidth(s) * fFontScale); }
public static float getHeight(String s, double dScale) { String sFontName = inputFontList[iCurrentFontNum]; if(sFontName.equals("fonts/Base6.ttf")) dScale *= .75f; UnicodeFont unicodeFont = null;// = fontStyleList.get(0).get(4); int iFontIndex = 0; //grab the font that is closest to the scale we are drawing. for(int i = 0; i < fontSizes.length; i++) { if(iStandardSize * dScale * resolutionScale <= fontSizes[i]) { unicodeFont = currentFont.get(i); iFontIndex = i; break; } } float fFontScale = (float) (((float)iStandardSize * dScale * resolutionScale) / fontSizes[iFontIndex] ); return (float) (unicodeFont.getHeight(s) * fFontScale); }
/** * @see org.newdawn.slick.font.effects.Effect#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, org.newdawn.slick.UnicodeFont, org.newdawn.slick.font.Glyph) */ public void draw(BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) { int ascent = unicodeFont.getAscent(); float height = (ascent) * scale; float top = -glyph.getYOffset() + unicodeFont.getDescent() + offset + ascent / 2 - height / 2; g.setPaint(new GradientPaint(0, top, topColor, 0, top + height, bottomColor, cyclic)); g.fill(glyph.getShape()); }
/** * Create a new glyph * * @param codePoint The code point in which this glyph can be found * @param bounds The bounds that this glrph can fill * @param vector The vector this glyph is part of * @param index The index of this glyph within the vector * @param unicodeFont The font this glyph forms part of */ public Glyph(int codePoint, Rectangle bounds, GlyphVector vector, int index, UnicodeFont unicodeFont) { this.codePoint = codePoint; GlyphMetrics metrics = vector.getGlyphMetrics(index); int lsb = (int)metrics.getLSB(); if (lsb > 0) lsb = 0; int rsb = (int)metrics.getRSB(); if (rsb > 0) rsb = 0; int glyphWidth = bounds.width - lsb - rsb; int glyphHeight = bounds.height; if (glyphWidth > 0 && glyphHeight > 0) { int padTop = unicodeFont.getPaddingTop(); int padRight = unicodeFont.getPaddingRight(); int padBottom = unicodeFont.getPaddingBottom(); int padLeft = unicodeFont.getPaddingLeft(); int glyphSpacing = 1; // Needed to prevent filtering problems. width = (short)(glyphWidth + padLeft + padRight + glyphSpacing); height = (short)(glyphHeight + padTop + padBottom + glyphSpacing); yOffset = (short)(unicodeFont.getAscent() + bounds.y - padTop); } shape = vector.getGlyphOutline(index, -bounds.x + unicodeFont.getPaddingLeft(), -bounds.y + unicodeFont.getPaddingTop()); isMissing = !unicodeFont.getFont().canDisplay((char)codePoint); }
@Override public void init(GameContainer container) throws SlickException { this.container = container; this.map = new TiledMap("/home/enzo/newmap.tmx"); SpriteSheet spriteSheet = new SpriteSheet("/home/enzo/SpriteSheetAnim.png", 64, 64); this.animations[0] = loadAnimation(spriteSheet, 0, 1, 0); this.animations[1] = loadAnimation(spriteSheet, 0, 1, 1); this.animations[2] = loadAnimation(spriteSheet, 0, 1, 2); this.animations[3] = loadAnimation(spriteSheet, 0, 1, 3); this.animations[4] = loadAnimation(spriteSheet, 1, 9, 0); this.animations[5] = loadAnimation(spriteSheet, 1, 9, 1); this.animations[6] = loadAnimation(spriteSheet, 1, 9, 2); this.animations[7] = loadAnimation(spriteSheet, 1, 9, 3); Font font = new Font("Verdana", Font.BOLD, 20); UnicodeFont uFont = new UnicodeFont(font, font.getSize(), font.isBold(), font.isItalic()); nameInput = new TextField(container, uFont, 150,20,500,35, new ComponentListener() { public void componentActivated(AbstractComponent source) { message = "Entered1: "+nameInput.getText(); nameInput.setFocus(true); } }); // ComponentListener listener = new ComponentListener(); // TextField nameInput = new TextField(arg0, truetypefont, 150,20,500,35, listener); // // { // public void componentActivated(AbstractComponent source) { // System.out.println("Entered1: "+nameInput.getText()); // } // }); }
public void render(Graphics g) { Image img = AssetManager.getManager().getImage(image); if(img == null) { // Draw the text representing the button. UnicodeFont fnt = AssetManager.getManager().getFont(FONT_NAME); Color color = mouseOver() ? DEFAULT_HOVER : DEFAULT_TEXT; g.setColor(color); g.setFont(fnt); g.drawString(text, position.x, position.y); } else g.drawImage(img, position.x, position.y); }
public boolean inBounds(float x, float y) { UnicodeFont fnt = AssetManager.getManager().getFont(FONT_NAME); float w = fnt.getWidth(text); float h = fnt.getHeight(text); return ((x > position.x) && (y > position.y) && (x < (position.x + w)) && (y < (position.y + h))); }
@Override public void init(GameContainer gc, StateBasedGame game) throws SlickException { assets = AssetManager.getManager(); UnicodeFont uni = assets.getFont("manaspc"); menuButton = new MenuButton(new Pair<Float>((float)((Globals.WIDTH / 2) - (uni.getWidth("Main Menu") / 2)), (Globals.HEIGHT - 200.0f)), "Main Menu"); exitButton = new MenuButton(new Pair<Float>((float)((Globals.WIDTH / 2) - (uni.getWidth("Give Up") / 2)), (Globals.HEIGHT - 150.0f)), "Give Up"); time = 0L; }
public UnicodeFont getFont(String key) { if((fonts != null) && (key != null) && (!key.equals(""))) { return fonts.get(key); } return null; }
/** * Loads a Unicode font and its ASCII glyphs. * @param font the font to load * @param effect the font effect * @throws SlickException if the glyphs could not be loaded */ @SuppressWarnings("unchecked") private static void loadFont(UnicodeFont font, Effect effect) throws SlickException { font.addAsciiGlyphs(); font.getEffects().add(effect); font.loadGlyphs(); }
@SuppressWarnings("unchecked") //loads the fonts for the game including different font sizes. public static void loadFonts() { int arrayLength = GameConstants.GAME_FONT.length; try { for(int i = 0; i < arrayLength; i++) { UnicodeFont font = new UnicodeFont(FONT_LOCATION, (int) ((16+10*i)/GameConstants.WINDOW_SCALE), false, false); font.addAsciiGlyphs(); font.getEffects().add(new ColorEffect()); font.loadGlyphs(); GameConstants.GAME_FONT[i] = font; } } catch(Exception e) { e.printStackTrace(); } }
private static void loadFonts() { font = new UnicodeFont(new java.awt.Font("Times New Roman", java.awt.Font.PLAIN, 24)); font.getEffects().add(new ColorEffect(java.awt.Color.white)); font.addAsciiGlyphs(); try { font.loadGlyphs(); } catch (SlickException e) { e.printStackTrace(); } }
/** * @see org.newdawn.slick.font.effects.Effect#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, org.newdawn.slick.UnicodeFont, org.newdawn.slick.font.Glyph) */ public void draw(BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) { g = (Graphics2D)g.create(); if (stroke != null) g.setStroke(stroke); else g.setStroke(getStroke()); g.setColor(color); g.draw(glyph.getShape()); g.dispose(); }
@SuppressWarnings("unchecked") @Override public void init(GameContainer container, StateBasedGame game) throws SlickException { this.game = game; Font font = new Font(Font.MONOSPACED, Font.PLAIN, 60); ufont = new UnicodeFont(font); ufont.addAsciiGlyphs(); ufont.addGlyphs(32, 127); ufont.getEffects().add(new ColorEffect(Color.WHITE)); ufont.loadGlyphs(); }
public TTFRenderer(String fontName, int fontType, int size, float kerning) { this.fontName = fontName; this.fontType = fontType; this.size = size; this.unicodeFont = new UnicodeFont(new Font(fontName, fontType, size)); this.kerning = kerning; this.unicodeFont.addAsciiGlyphs(); this.unicodeFont.getEffects().add(new ColorEffect(java.awt.Color.WHITE)); try { this.unicodeFont.loadGlyphs(); } catch(Exception e) { e.printStackTrace(); } for(int i = 0; i < 32; i++) { int shadow = (i >> 3 & 1) * 85; int red = (i >> 2 & 1) * 170 + shadow; int green = (i >> 1 & 1) * 170 + shadow; int blue = (i >> 0 & 1) * 170 + shadow; if(i == 6) { red += 85; } if(i >= 16) { red /= 4; green /= 4; blue /= 4; } this.colorCodes[i] = (red & 255) << 16 | (green & 255) << 8 | blue & 255; } }