Пример #1
0
        public void LoadTexture(NamespacedId id)
        {
            if (LoadedTextures.ContainsKey(id))
            {
                Log.Info($"Texture {id} is already loaded. Reloading...");
                LoadedTextures[id].Dispose();
            }

            Log.Info($"Loading texture {id}...");

            try
            {
                var data = ImageLoader.Load(ResolveTexturePath(id));
                if (data == null)
                {
                    throw new Exception("Not found");
                }

                LoadedTextures[id] = Engine.Renderer.CreateTexture(
                    data.Value.Data,
                    data.Value.Width,
                    data.Value.Height);
            }
            catch (Exception e)
            {
                Log.Error($"Couldn't load texture {id}");
                Log.Error($"{e.GetType().Name}: {e.Message}");
                Log.Error($"{e.StackTrace}");
                LoadedTextures[id] = ErrorTexture;
#if DEBUG
                throw;
#endif
            }
        }
Пример #2
0
        /// <summary>
        /// </summary>
        /// <param name="drawData"></param>
        /// <exception cref="InvalidOperationException"></exception>
        private void RenderCommandLists(ImDrawDataPtr drawData)
        {
            var lastIndecies         = GraphicsDevice.Indices;
            var lastScissorRectangle = GraphicsDevice.ScissorRectangle;

            GraphicsDevice.SetVertexBuffer(VertexBuffer);
            GraphicsDevice.Indices = IndexBuffer;

            var vtxOffset = 0;
            var idxOffset = 0;

            for (var n = 0; n < drawData.CmdListsCount; n++)
            {
                var cmdList = drawData.CmdListsRange[n];

                for (var cmdi = 0; cmdi < cmdList.CmdBuffer.Size; cmdi++)
                {
                    var drawCmd = cmdList.CmdBuffer[cmdi];

                    if (!LoadedTextures.ContainsKey(drawCmd.TextureId))
                    {
                        throw new InvalidOperationException($"Could not find a texture with id '{drawCmd.TextureId}', please check your bindings");
                    }

                    GraphicsDevice.ScissorRectangle = new Rectangle(
                        (int)drawCmd.ClipRect.X,
                        (int)drawCmd.ClipRect.Y,
                        (int)(drawCmd.ClipRect.Z - drawCmd.ClipRect.X),
                        (int)(drawCmd.ClipRect.W - drawCmd.ClipRect.Y)
                        );

                    var effect = UpdateEffect(LoadedTextures[drawCmd.TextureId]);

                    foreach (var pass in effect.CurrentTechnique.Passes)
                    {
                        pass.Apply();

#pragma warning disable CS0618 // // FNA does not expose an alternative method.
                        GraphicsDevice.DrawIndexedPrimitives(
                            PrimitiveType.TriangleList,
                            vtxOffset,
                            0,
                            cmdList.VtxBuffer.Size,
                            idxOffset,
                            (int)drawCmd.ElemCount / 3
                            );
#pragma warning restore CS0618
                    }

                    idxOffset += (int)drawCmd.ElemCount;
                }

                vtxOffset += cmdList.VtxBuffer.Size;
            }

            GraphicsDevice.SetVertexBuffer(null);
            GraphicsDevice.Indices          = lastIndecies;
            GraphicsDevice.ScissorRectangle = lastScissorRectangle;
        }
Пример #3
0
        /// <summary>
        ///     Creates a pointer to a texture, which can be passed through ImGui calls such as <see cref="MediaTypeNames.Image" />.
        ///     That pointer is then used by ImGui to let us know what texture to draw
        /// </summary>
        public IntPtr BindTexture(Texture2D texture)
        {
            var id = new IntPtr(TextureId++);

            LoadedTextures.Add(id, texture);

            return(id);
        }
Пример #4
0
 public void UnloadResources()
 {
     foreach (var kv in LoadedTextures)
     {
         kv.Value.Dispose();
     }
     LoadedTextures.Clear();
 }
 public void LoadContent()
 {
     _moneyOverlay = new MoneyAccountOverlay(new MoneyAccount(), new Vector2(100, 10));
     _moneyOverlay.LoadContent();
     _overlay = new BuildingSelectionOverlay();
     _overlay.LoadContent();
     _textures = new LoadedTextures("grass1", "building1", "building2");
     _textures.LoadContent();
 }
Пример #6
0
 public Texture Get(NamespacedId id, bool loadIfUnloaded = true)
 {
     if (!LoadedTextures.ContainsKey(id))
     {
         if (!loadIfUnloaded)
         {
             return(ErrorTexture);
         }
         LoadTexture(id);
     }
     return(LoadedTextures[id]);
 }
Пример #7
0
        public int LoadTexture(string name)
        {
            var textureResource = VrfGuiContext.LoadFileByAnyMeansNecessary(name + "_c");

            if (textureResource == null)
            {
                return(GetErrorTexture());
            }

            LoadedTextures.Add(name);

            return(LoadTexture(textureResource));
        }
        int GenerateMapTextureButton(string loadPath, string absolutePath, GameObject Prefab)
        {
            Texture2D LoadedTex;

            string RelativePath = "/" + absolutePath.Replace(MapLuaParser.LoadedMapFolderPath, MapLuaParser.RelativeLoadedMapFolderPath);

            //Debug.Log(RelativePath);

            try
            {
                LoadedTex = GetGamedataFile.LoadTexture2D(RelativePath, false, false);
            }
            catch (System.Exception e)
            {
                LoadedTex = new Texture2D(128, 128);
                Debug.LogWarning("Can't load DDS texture: " + e);
                return(0);
            }


            string TexPath = "";

            if (RelativePath.EndsWith(".dds"))
            {
                TexPath = RelativePath.Replace(".dds", "");
            }
            else if (RelativePath.EndsWith(".DDS"))
            {
                TexPath = RelativePath.Replace(".DDS", "");
            }


            GameObject NewButton = Instantiate(Prefab) as GameObject;

            NewButton.transform.SetParent(Pivot, false);
            NewButton.GetComponent <ResourceObject>().SetImages(LoadedTex);
            NewButton.GetComponent <ResourceObject>().InstanceId     = LoadedTextures.Count;
            NewButton.GetComponent <ResourceObject>().NameField.text = TexPath;
            LoadedTextures.Add(LoadedTex);
            LoadedPaths.Add(RelativePath);

            if (RelativePath.ToLower() == SelectedObject.ToLower())
            {
                LastSelection = NewButton.GetComponent <ResourceObject>().Selected;
                LastSelection.SetActive(true);
                Pivot.GetComponent <RectTransform>().anchoredPosition = Vector2.up * 250 * Mathf.FloorToInt(LoadedPaths.Count / 5f);
            }

            return(1);
        }
Пример #9
0
        public int LoadTexture(string name)
        {
            var textureResource = FileExtensions.LoadFileByAnyMeansNecessary(name + "_c", CurrentFileName, CurrentPackage);

            if (textureResource == null)
            {
                Console.Error.WriteLine("File " + name + " not found");

                return(GetErrorTexture());
            }

            LoadedTextures.Add(name);

            return(LoadTexture(textureResource));
        }
Пример #10
0
        public static Texture2D GetTexture(string texture)
        {
            if (LoadedTextures.ContainsKey(texture))
            {
                return(LoadedTextures[texture]);
            }

            Debug.Log($"{texture} is not already loaded.");
            WWW www = new WWW(ImageRepository.GetPathInModFolder(texture));

            //Debug.Log($"{texture} - {www.texture.width.ToString()}x{www.texture.height.ToString()}");
            Texture2D tex = new Texture2D(www.texture.width, www.texture.height, TextureFormat.DXT1, false);

            tex.filterMode = ImageRepository.Files[texture].FilterMode;

            www.LoadImageIntoTexture(tex);
            LoadedTextures[texture] = tex;

            return(tex);
        }
Пример #11
0
 public void LoadSpritesInMemory(bool shuffle = false)
 {
     LoadTexturesInMemory(shuffle);
     m_loadedSprites = LoadedTextures.Select(t => t.ToSprite()).ToList();
 }
Пример #12
0
        private int LoadTexture(string name)
        {
            var textureResource = FileExtensions.LoadFileByAnyMeansNecessary(name + "_c", CurrentFileName, CurrentPackage);

            if (textureResource == null)
            {
                Console.Error.WriteLine("File " + name + " not found");

                return(GetErrorTexture());
            }

            LoadedTextures.Add(name);

            var tex = (Texture)textureResource.Blocks[BlockType.DATA];

            var id = GL.GenTexture();

            GL.BindTexture(TextureTarget.Texture2D, id);

            var textureReader = textureResource.Reader;

            textureReader.BaseStream.Position = tex.Offset + tex.Size;

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, tex.NumMipLevels - 1);

            var width  = tex.Width / (int)Math.Pow(2.0, tex.NumMipLevels);
            var height = tex.Height / (int)Math.Pow(2.0, tex.NumMipLevels);

            int blockSize;
            PixelInternalFormat format;

            if (tex.Format.HasFlag(VTexFormat.DXT1))
            {
                blockSize = 8;
                format    = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
            }
            else if (tex.Format.HasFlag(VTexFormat.DXT5))
            {
                blockSize = 16;
                format    = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
            }
            else if (tex.Format.HasFlag(VTexFormat.RGBA8888))
            {
                //blockSize = 4;
                //format = PixelInternalFormat.Rgba8i;
                Console.Error.WriteLine("Don't support RGBA8888 but don't want to crash either. Using error texture!");
                return(GetErrorTexture());
            }
            else
            {
                throw new Exception("Unsupported texture format: " + tex.Format);
            }

            for (var i = tex.NumMipLevels - 1; i >= 0; i--)
            {
                if ((width *= 2) == 0)
                {
                    width = 1;
                }

                if ((height *= 2) == 0)
                {
                    height = 1;
                }

                var size = ((width + 3) / 4) * ((height + 3) / 4) * blockSize;

                GL.CompressedTexImage2D(TextureTarget.Texture2D, i, format, width, height, 0, size, textureReader.ReadBytes(size));
            }

            // Dispose texture otherwise we run out of memory
            // TODO: This might conflict when opening multiple files due to shit caching
            textureResource.Dispose();

            if (MaxTextureMaxAnisotropy > 0)
            {
                GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, MaxTextureMaxAnisotropy);
            }

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)(tex.Flags.HasFlag(VTexFlags.SUGGEST_CLAMPS) ? TextureWrapMode.Clamp : TextureWrapMode.Repeat));
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)(tex.Flags.HasFlag(VTexFlags.SUGGEST_CLAMPT) ? TextureWrapMode.Clamp : TextureWrapMode.Repeat));

            return(id);
        }
Пример #13
0
 /// <summary>
 ///     Removes a previously created texture pointer, releasing its reference and allowing it to be deallocated
 /// </summary>
 public void UnbindTexture(IntPtr textureId) => LoadedTextures.Remove(textureId);