Пример #1
0
		public BitmapFont (Stream stream, Shader shader = null) {
			ThreadContext.Current.EnsureGLContext();

			if (shader == null)
				shader = new SpriteShader();

			var tr = new TarReader(stream);
			while (tr.MoveNext(false)) {
				switch (tr.FileInfo.FileName) {
					case "font.atlas":
						using (var atlasStream = new MemoryStream()) {
							tr.Read(atlasStream);
							atlasStream.Position = 0;
							this.ParseStream(atlasStream);
						}
						break;
					case "font.png":
						using (var textureStream = new MemoryStream()) {
							tr.Read(textureStream);
							textureStream.Position = 0;

							_material = new SpriteMaterial(shader, new Texture (textureStream));
						}
						break;
					default:
						throw new ContentException("Unrecognized font file " + tr.FileInfo.FileName);
				}
			}

			if (_chars == null)
				throw new ContentException("Missing font atlas.");
			if (_material == null)
				throw new ContentException("Missing font texture.");
		}
Пример #2
0
        public BitmapFont(Stream stream, Shader shader = null)
        {
            ThreadContext.Current.EnsureGLContext();

            if (shader == null)
            {
                shader = new SpriteShader();
            }

            var tr = new TarReader(stream);

            while (tr.MoveNext(false))
            {
                switch (tr.FileInfo.FileName)
                {
                case "font.atlas":
                    using (var atlasStream = new MemoryStream()) {
                        tr.Read(atlasStream);
                        atlasStream.Position = 0;
                        this.ParseStream(atlasStream);
                    }
                    break;

                case "font.png":
                    using (var textureStream = new MemoryStream()) {
                        tr.Read(textureStream);
                        textureStream.Position = 0;

                        _material = new SpriteMaterial(shader, new Texture(textureStream));
                    }
                    break;

                default:
                    throw new ContentException("Unrecognized font file " + tr.FileInfo.FileName);
                }
            }

            if (_chars == null)
            {
                throw new ContentException("Missing font atlas.");
            }
            if (_material == null)
            {
                throw new ContentException("Missing font texture.");
            }
        }
Пример #3
0
		public SlicedSprite (SpriteMaterial material, Vector2 pos, Vector2 size, Vector2 origin, Vector4 color, Vector4 border, bool tileX, bool tileY, bool hollow)
			: base(material, size, new VertexBuffer(VertexFormat.PositionColorUV), new IndexBuffer(), 0, 0, true) {
			ThreadContext.Current.EnsureGLContext();

			_border = border;
			_origin = origin;
			_color = color;
			_tileX = tileX;
			_tileY = tileY;
			_hollow = hollow;

			var tsz = _texelSize = material.Texture.TexelSize;
			_outerUV = new Vector4(pos.X * tsz.X, (pos.Y + size.Y) * tsz.Y, (pos.X + size.X) * tsz.X, pos.Y * tsz.Y);
			_innerUV = new Vector4((pos.X + border.X) * tsz.X, (pos.Y + size.Y - border.W) * tsz.Y, (pos.X + size.X - border.Z) * tsz.X, (pos.Y + border.Y) * tsz.Y);
			_tileSize = new Vector2(size.X - border.X - border.Z, size.Y - border.Y - border.W);

			this.Resize(size);
		}
Пример #4
0
        public SlicedSprite(SpriteMaterial material, Vector2 pos, Vector2 size, Vector2 origin, Vector4 color, Vector4 border, bool tileX, bool tileY, bool hollow)
            : base(material, size, new VertexBuffer(VertexFormat.PositionColorUV), new IndexBuffer(), 0, 0, true)
        {
            ThreadContext.Current.EnsureGLContext();

            _border = border;
            _origin = origin;
            _color  = color;
            _tileX  = tileX;
            _tileY  = tileY;
            _hollow = hollow;

            var tsz = _texelSize = material.Texture.TexelSize;

            _outerUV  = new Vector4(pos.X * tsz.X, (pos.Y + size.Y) * tsz.Y, (pos.X + size.X) * tsz.X, pos.Y * tsz.Y);
            _innerUV  = new Vector4((pos.X + border.X) * tsz.X, (pos.Y + size.Y - border.W) * tsz.Y, (pos.X + size.X - border.Z) * tsz.X, (pos.Y + border.Y) * tsz.Y);
            _tileSize = new Vector2(size.X - border.X - border.Z, size.Y - border.Y - border.W);

            this.Resize(size);
        }
Пример #5
0
        void Initialize(Stream stream, Shader shader = null)
        {
            ThreadContext.Current.EnsureGLContext();

            if (shader == null)
            {
                shader = new SpriteShader();
            }

            int filterMode = 0;
            var defs       = new Dictionary <string, SpriteDefinition>();

            var tr = new TarReader(stream);

            while (tr.MoveNext(false))
            {
                switch (tr.FileInfo.FileName)
                {
                case "atlas.bin":
                    using (var atlasStream = new MemoryStream()) {
                        tr.Read(atlasStream);
                        atlasStream.Position = 0;
                        using (var br = new BinaryReader(atlasStream)) {
                            filterMode = br.ReadInt32();
                            for (var count = br.ReadInt32(); count > 0; --count)
                            {
                                var key   = br.ReadString();
                                var value = SpriteDefinition.Read(br);
                                defs.Add(key, value);
                            }
                        }
                    }
                    break;

                case "sheet.png":
                    using (var sheetStream = new MemoryStream((int)tr.FileInfo.SizeInBytes)) {
                        tr.Read(sheetStream);
                        sheetStream.Position = 0;
                        var tex = new Texture(sheetStream, new TextureSettings {
                            MagFilter = filterMode > 0 ? TextureFilter.Linear : TextureFilter.Nearest,
                            MinFilter = filterMode == 1 ? TextureFilter.Linear : filterMode == 2 ? TextureFilter.Trilinear : TextureFilter.Nearest,
                        });
                        _material = new SpriteMaterial(shader, tex);
                    }
                    break;

                default:
                    throw new ContentException("Unrecognized atlas file " + tr.FileInfo.FileName);
                }
            }

            if (defs == null)
            {
                throw new ContentException("Missing atlas file.");
            }
            if (_material == null)
            {
                throw new ContentException("Missing image file.");
            }

            _sprites = new Dictionary <string, Sprite>();

            _vbuffer = new VertexBuffer(VertexFormat.PositionColorUV);
            _ibuffer = new IndexBuffer();
            var vstride = _vbuffer.Format.Stride * 4;

            int vOffset = 0, iOffset = 0, vcount = 0;
            var vertices = new float[vstride * defs.Count];
            var indices  = new int[6 * defs.Count];
            var tsz      = new Vector2(_material.Texture.Size.Width, _material.Texture.Size.Height);

            foreach (var kvp in defs)
            {
                var def   = kvp.Value;
                var pos   = def.Position;
                var sz    = def.Size;
                var orig  = def.Origin;
                var color = ParseColor(def.Color);
                orig.X *= sz.X;
                orig.Y *= sz.Y;

                if (def.Border != Vector4.Zero)
                {
                    _sprites.Add(kvp.Key, new SlicedSprite(_material, pos, sz, orig, color, def.Border, def.TileX, def.TileY, def.Hollow));
                    continue;
                }

                var uv = new Vector4(
                    pos.X / tsz.X,
                    ((pos.Y + sz.Y)) / tsz.Y,
                    (pos.X + sz.X) / tsz.X,
                    (pos.Y) / tsz.Y
                    );

                Array.Copy(new[] {
                    -orig.X, -orig.Y, 0f, color.X, color.Y, color.Z, color.W, uv.X, uv.Y,
                    sz.X - orig.X, -orig.Y, 0f, color.X, color.Y, color.Z, color.W, uv.Z, uv.Y,
                    sz.X - orig.X, sz.Y - orig.Y, 0f, color.X, color.Y, color.Z, color.W, uv.Z, uv.W,
                    -orig.X, sz.Y - orig.Y, 0f, color.X, color.Y, color.Z, color.W, uv.X, uv.W
                }, 0, vertices, vOffset, vstride);

                Array.Copy(new[] {
                    vcount + 0, vcount + 1, vcount + 2, vcount + 2, vcount + 3, vcount + 0
                }, 0, indices, iOffset, 6);

                var sprite = new Sprite(_material, sz, _vbuffer, _ibuffer, iOffset, 6);
                _sprites.Add(kvp.Key, sprite);
                vOffset += vstride;
                iOffset += 6;
                vcount  += 4;
            }

            _vbuffer.Data = vertices;
            _vbuffer.Commit();

            _ibuffer.Data = indices;
            _ibuffer.Commit();
        }