public static BitsyFont LoadTextureFont(Texture2D texture) { if (texture == null) { return(null); } if (texture.width < TEXTUREFONT_DIM || texture.height < TEXTUREFONT_DIM) { Debug.LogWarning("Attempted to load BitsyFont from a texture with the wrong proportions."); return(null); } var font = new BitsyFont(); byte[] arr = new byte[BitsyGame.FONT_WIDTH * BitsyGame.FONT_HEIGHT]; for (int i = 0; i < 256; i++) { var gfx = new GfxTileSheet(BitsyGame.FONT_WIDTH, BitsyGame.FONT_HEIGHT, 1); int x = (i % BitsyUnityUtils.TEXTUREFONT_FONTTILE_EDGE_CNT) * BitsyUnityUtils.TEXTUREFONT_FONTTILE_DIM; int y = (i / BitsyUnityUtils.TEXTUREFONT_FONTTILE_EDGE_CNT) * BitsyUnityUtils.TEXTUREFONT_FONTTILE_DIM; x += 1; y = TEXTUREFONT_DIM - y - 1; for (int j = 0; j < arr.Length; j++) { int ix = j % BitsyGame.FONT_WIDTH; int iy = j / BitsyGame.FONT_WIDTH; var c = texture.GetPixel(x + ix, y - iy); arr[j] = (c.r > 0.5f) ? (byte)1 : (byte)0; } gfx.SetPixels(arr); font.SetCharGfx((char)i, gfx); } return(font); }
//[MenuItem("Bitsy/ExportFont")] public static void DoExportFont() { var font = BitsyFont.CreateDefault(); var text = BitsyUnityUtils.DrawFontToTexture(font); var bytes = text.EncodeToTGA(); var path = Application.dataPath + "/Bitsy/Unity/DefaultFont.tga"; if (File.Exists(path)) { File.Delete(path); } File.WriteAllBytes(path, bytes); }
public Environment Parse(TextReader reader, BitsyInput.PollInputActiveCallback getInputCallback, IFont font = null) { _environment = new Environment(); _environment.Inputs.GetInputActive = getInputCallback; _environment.Font = font ?? BitsyFont.CreateDefault(); _variables = new Dictionary <string, string>(); _spriteStartLocations = new Dictionary <string, BitsyGame.Loc>(); try { _environment.Title = reader.ReadLineSafe(); string line; while ((line = reader.ReadLineSafe()) != null) { if (string.IsNullOrEmpty(line) || line[0] == '#') { continue; } switch (GetLineArg(line, 0)) { case "PAL": this.ParsePalette(reader, line); break; case "ROOM": case "SET": this.ParseRoom(reader, line); break; case "TIL": this.ParseTile(reader, line); break; case "SPR": this.ParseSprite(reader, line); break; case "ITM": this.ParseItem(reader, line); break; case "DRW": this.ParseDrawing(reader, line); break; case "DLG": this.ParseDialog(reader, line); break; case "END": this.ParseEnding(reader, line); break; case "VAR": this.ParseVariable(reader, line); break; case "!": this.ParseFlag(reader, line); break; } } } catch (System.Exception ex) { throw new System.FormatException("Failed to parse world data, malformed.", ex); } this.InitializeSprites(); this.InitializeVariables(); var result = _environment; this.Cleanup(); return(result); }