Inheritance: IDisposable
Exemple #1
0
 private static Texture GetTexture(string fileName, bool linear, bool repeatx, bool repeaty)
 {
     if (textures == null)
         textures = new Dictionary<Tuple<string, bool, bool, bool>, Texture>();
     var key = Tuple.Create(fileName, linear, repeatx, repeaty);
     if (!textures.ContainsKey(key))
         textures[key] = new Texture(fileName, linear, repeatx, repeaty);
     return textures[key];
 }
Exemple #2
0
        public Tilemap(string csvFile, string textureName)
        {
            string mapBody = File.ReadAllText(csvFile).TrimEnd(new char[] { '\n' });
            string[] rows = mapBody.Split('\n');
            int index = 0;
            height = rows.Length;
            foreach (string row in rows)
            {
                string[] cols = row.Split(',');
                width = cols.Length;
                if (this.map == null)
                {
                    this.map = new int[cols.Length * rows.Length];
                }
                foreach (string col in cols)
                {
                    this.map[index] = int.Parse(col);
                    index++;
                }
            }

            this.mapMesh = new Mesh();
            List<float> vertices = new List<float>();
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    vertices.Add(x * 70);
                    vertices.Add(y * 70);

                    vertices.Add(x * 70);
                    vertices.Add((y + 1) * 70);

                    vertices.Add((x + 1) * 70);
                    vertices.Add(y * 70);

                    vertices.Add((x + 1) * 70);
                    vertices.Add(y * 70);

                    vertices.Add((x + 1) * 70);
                    vertices.Add((y + 1) * 70);

                    vertices.Add(x * 70);
                    vertices.Add((y + 1) * 70);
                }
            }

            this.mapMesh.v = vertices.ToArray();
            this.mapMesh.uv = new float[this.mapMesh.v.Length];
            this.mapMesh.Update();
            this.tileSheet = new Texture(textureName);
            // use nearest mode for sampling
            this.tileSheet.SetNearest();
        }
Exemple #3
0
 public static void DrawRectangleOnTexture(Texture texture, int x, int y, int width, int height, Color4 color)
 {
     if (x + width < 0 || y + height < 0 || x > texture.Width || y > texture.Height)
         return;
     for (var by = 0; by < height; by++)
     {
         for (var bx = 0; bx < width; bx++)
         {
             PutPixel(texture, bx + x, by + y, 0f, color);
         }
     }
 }
Exemple #4
0
 public static void DrawLine(Texture texture, Vector3 from, Vector3 to, Color4 color)
 {
     var near = 300f;
     // not entirely correct but quite fast
     if (from.X < -near || to.X < -near || from.Y < -near || to.X < -near ||
         from.X > texture.Width + near || to.X > texture.Width + near ||
         from.Y > texture.Height + near || to.Y > texture.Height + near)
         return;
     var x = (int)from.X;
     var y = (int)from.Y;
     var x2 = (int)to.X;
     var y2 = (int)to.Y;
     var w = x2 - x;
     var h = y2 - y;
     int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
     if (w < 0) dx1 = -1;
     else if (w > 0) dx1 = 1;
     if (h < 0) dy1 = -1;
     else if (h > 0) dy1 = 1;
     if (w < 0) dx2 = -1;
     else if (w > 0) dx2 = 1;
     var longest = Math.Abs(w);
     var shortest = Math.Abs(h);
     if (!(longest > shortest))
     {
         longest = Math.Abs(h);
         shortest = Math.Abs(w);
         if (h < 0) dy2 = -1;
         else if (h > 0) dy2 = 1;
         dx2 = 0;
     }
     var numerator = longest >> 1;
     for (var i = 0; i <= longest && (x < texture.Width || y < texture.Height); i++)
     {
         PutPixel(texture, x, y, 0f, color);
         numerator += shortest;
         if (!(numerator < longest))
         {
             numerator -= longest;
             x += dx1;
             y += dy1;
         }
         else
         {
             x += dx2;
             y += dy2;
         }
     }
 }
Exemple #5
0
 public static void PutPixel(Texture texture, int x, int y, float z, Color4 color)
 {
     if (x < 0 || y < 0 || x >= texture.Width || y >= texture.Height || 
         (ZBuffer[y * texture.Width + x] != 0f && ZBuffer[y * texture.Width + x] < z))
         return;
     ZBuffer[y*texture.Width + x] = z;
     var position = y * texture.Width * 4 + x * 4;
     if (position + 3 < texture.Bitmap.Length && position >= 0)
     {
         texture.Bitmap[position] = (byte)(color.R * 255);
         texture.Bitmap[position + 1] = (byte)(color.G * 255);
         texture.Bitmap[position + 2] = (byte)(color.B * 255);
         texture.Bitmap[position + 3] = (byte)(color.A * 255);
     }
 }
Exemple #6
0
		public void DrawTexture (Texture tex, int x, int y, int width, int height)
		{
			float deltaW = 1f / tex.Width;
			float deltaH = 1f / tex.Height;
			float left = x * deltaW;
			float right = (x + width) * deltaW;
			float top = y * deltaH;
			float bottom = (y + height) * deltaH;
			this.uv = new float[] {
				left, top,
				right, top,
				left, bottom,
				right, top,
				right, bottom,
				left, bottom
			};
			this.UpdateUV ();
			base.DrawTexture (tex);
		}
Exemple #7
0
        public TextConfig(
            Asset fontAsset, Dictionary<char, Tuple<Vector2, Vector2>> charToSprite,
            Color fontBaseColor = default(Color), float spaceWidth = 44f, float spaceHeight = 31f,
            float padding = float.MinValue, Func<float, float> paddingFunc = null, bool staticColor = true)
        {
            if (fontBaseColor == default(Color))
                fontBaseColor = Color.Black;
            if (paddingFunc == null && padding == float.MinValue)
                padding = 0;
            FontAsset = fontAsset;
            FontTexture = new Texture(fontAsset.FileName);
            CharToSprite = charToSprite;
            FontBaseColor = fontBaseColor;
            SpaceWidth = spaceWidth;
            SpaceHeight = spaceHeight;
            Padding = padding;
            PaddingFunc = paddingFunc;
            StaticColor = staticColor;

            if (Default == null)
                Default = this;
        }
Exemple #8
0
 public override void DrawTexture(Texture tex)
 {
     this.DrawTexture(tex, 0, 0, tex.Width, tex.Height);
 }
Exemple #9
0
 public void DrawTexture(Texture tex, int x, int y)
 {
     this.DrawTexture(tex, x, y, tex.Width, tex.Height);
 }
Exemple #10
0
		public void DrawTexture (Texture tex)
		{
			this.Bind ();
			tex.Bind ();
			this.shader.Use ();
			this.ApplyMatrix ();
			GL.DrawArrays (PrimitiveType.Triangles, 0, this.v.Length / 2);

		}
Exemple #11
0
 public CityLevel():base()
 {
     city = new Texture("../../Assets/City.png");
     cityMesh = new Sprite(city.Width, city.Height);
 }
Exemple #12
0
 public static void ChangeTextureColor(Texture texture, Color4 color)
 {
     //DrawRectangleOnTexture(texture, 0, 0, Manager.Texture.Width, Manager.Texture.Height, color);
     if (color.R != 0 || color.G != 0 || color.B != 0 || color.A != 1f)
     {
         if (CachedBackground.ContainsKey(color))
             texture.Bitmap = (byte[])CachedBackground[color].Clone();
         else
         {
             DrawRectangleOnTexture(texture, 0, 0, texture.Width, texture.Height, color);
             CachedBackground[color] = (byte[])texture.Bitmap.Clone();
         }
     }
     else
     {
         texture.Bitmap = new byte[texture.Bitmap.Length];
     }
 }
Exemple #13
0
 public MaskEffect(Texture mask)
     : base(fragmentShader)
 {
     this.maskTexture = mask;
 }
Exemple #14
0
 public MaskEffect(string fileName)
     : base(fragmentShader)
 {
     this.maskTexture = new Texture(fileName);
 }
Exemple #15
0
 public virtual void DrawTexture(Texture tex)
 {
     if (this.v == null || this.uv == null)
         return;
     // upload fake vcs (if required) to avoid crashes
     if (this.vc == null && this.hasVertexColors)
     {
         this.vc = new float[this.v.Length * 2];
         this.UpdateVertexColor();
     }
     this.Bind();
     tex.Bind();
     if (this.requireUseTexture)
         this.shader.SetUniform("use_texture", 1f);
     this.shader.Use();
     if (this.shaderSetupHook != null)
     {
         this.shaderSetupHook(this);
     }
     this.ApplyMatrix();
     if (instances <= 1)
     {
     #if !__MOBILE__
         GL.DrawArrays(PrimitiveType.Triangles, 0, this.v.Length / 2);
     #else
         GL.DrawArrays(BeginMode.Triangles, 0, this.v.Length / 2);
     #endif
     }
     else
     {
         GL.DrawArraysInstanced(PrimitiveType.Triangles, 0, this.v.Length / 2, instances);
     }
 }
Exemple #16
0
        protected void Initialize()
        {
            // crappy
            //new AudioSource();

            Camera = new Camera();

            // create dictionaries
            Objects = new Dictionary<string, GameObject>();
            SortedObjects = new SortedSet<GameObject>(new GameObjectComparer());
            Assets = new Dictionary<string, Asset>();

            dirtyObjects = new Dictionary<GameObject, int>();

            Joysticks = new Joystick[8];

            debugCollisionsCuboids = new Dictionary<GameObject.HitBox, Cuboid>();

            Timer = new TimerManager(this);

            WorkingTexture = new Texture(Width, Height);
            WorkingSprite = new Sprite(Width, Height)
            {
                Rotation = (float)Math.PI, 
                pivot = new OpenTK.Vector2(Width / 2f, Height / 2f)
            };
            WorkingSprite.position = WorkingSprite.pivot;
        }