Esempio n. 1
0
        public void LoadTextureFromPNG(string pathToFile, IntPtr renderer)
        {
            int initted = Image.IMG_Init(Image.IMG_InitFlags.IMG_INIT_PNG);

            if ((initted & (int)Image.IMG_InitFlags.IMG_INIT_PNG) != (int)Image.IMG_InitFlags.IMG_INIT_PNG)
            {
                throw new Exception($"SDL_Image was not initialized (PNG). Error: {SDL.GetError()}");
            }

            var surface = Image.IMG_Load(pathToFile);

            if (surface == IntPtr.Zero)
            {
                throw new Exception($"PNG was not loaded. Error: {SDL.GetError()}");
            }

            _texture = SDL.CreateTextureFromSurface(renderer, surface);
            SDL.FreeSurface(surface);
            if (_texture == IntPtr.Zero)
            {
                throw new Exception($"Texture was not create. Error: {SDL.GetError()}");
            }

            Image.IMG_Quit();
        }
Esempio n. 2
0
        public void LoadTextureFromBMP(string pathToFile, IntPtr renderer)
        {
            if (_texture != IntPtr.Zero)
            {
                SDL.DestroyTexture(_texture);
            }

            var surface = SDL.LoadBMP(pathToFile);

            if (surface == IntPtr.Zero)
            {
                throw new Exception($"BMP was not loaded. Error: {SDL.GetError()}");
            }

            _texture = SDL.CreateTextureFromSurface(renderer, surface);
            SDL.FreeSurface(surface);
            if (_texture == IntPtr.Zero)
            {
                throw new Exception($"Texture was not create. Error: {SDL.GetError()}");
            }
        }
Esempio n. 3
0
        public void Load()
        {
            if (!Loaded)
            {
                IntPtr surface = IMG.Load_RW(Resources.CreateRWFromFile(Filename, FileAssembly), 1);
                if (surface != IntPtr.Zero)
                {
                    TextureHandle = SDL.CreateTextureFromSurface(Game.RendererHandle, surface);
                    SDL.FreeSurface(surface);

                    if (TextureHandle != IntPtr.Zero)
                    {
                        uint format;
                        int  access, w, h;
                        SDL.QueryTexture(TextureHandle, out format, out access, out w, out h);

                        if (AutoSize)
                        {
                            FrameSize = new Point(w, h);
                            IndexSize = new Point(1, 1);
                        }
                        else
                        {
                            IndexSize = new Point((float)Math.Floor(w / FrameSize.X), (float)Math.Floor(h / FrameSize.Y));
                        }

                        Loaded = true;
                    }
                    else
                    {
                        throw new FileLoadException(SDL.GetError());
                    }
                }
                else
                {
                    throw new FileLoadException(SDL.GetError());
                }
            }
        }
Esempio n. 4
0
 public Texture(Renderer renderer, string filePath)
 {
     //SDL.QueryTexture((IntPtr)texture, out format, out access, out w, out h);
     this.FilePath = filePath;
     //int access, h, w;
     //uint format;
     this.Surface = new Surface(filePath);
     this.IntPtr  = SDL.CreateTextureFromSurface(renderer.IntPtr, Surface.IntPtr);
     //this.IntPtr = Image.IMG_LoadTexture(renderer.IntPtr, filePath);
     SDL.QueryTexture(this.IntPtr, out uint format, out int access, out int w, out int h);
     this.Access = access;
     this.Rect   = new SDL.Rect()
     {
         h = h, w = w
     };
     this.Format   = format;
     this.Renderer = renderer;
     this.Rect     = new SDL.Rect()
     {
         x = 0, y = 0, w = Width, h = Height
     };
 }
Esempio n. 5
0
        public void Draw(string str, Point pos, Color color, Alignment alignment = Alignment.TopLeft, int depth = 0)
        {
            if (!Loaded)
            {
                throw new NotLoadedException();
            }

            //replace tab with 4 spaces because sdl_ttf doesn't
            str = str.Replace("\t", "    ");

            //create rows by splitting on newline character
            string[] rows        = str.Split('\n');
            IntPtr[] rowTextures = new IntPtr[rows.Length];

            //add independent drawjob for each line
            for (int i = 0; i < rows.Length; i++)
            {
                //leave blank if there's no use for a texture there (whitespace)
                if (String.IsNullOrWhiteSpace(rows[i]))
                {
                    rowTextures[i] = IntPtr.Zero;
                    continue;
                }

                Tuple <string, Color> key = new Tuple <string, Color>(rows[i], color);
                if (Textures.ContainsKey(key))                 //use an already rendered texture
                {
                    rowTextures[i]           = Textures[key];
                    TexturesDrawsUnused[key] = 0;
                }
                else                 //generate a new texture
                {
                    IntPtr textSurface = TTF.RenderUTF8_Blended(Handle, rows[i], color);
                    rowTextures[i] = SDL.CreateTextureFromSurface(Game.RendererHandle, textSurface);
                    SDL.FreeSurface(textSurface);

                    Textures.Add(key, rowTextures[i]);
                    TexturesDrawsUnused.Add(key, 0);
                }

                uint format;
                int  access, w, h;
                SDL.QueryTexture(rowTextures[i], out format, out access, out w, out h);

                float horizontalOffset = 0f;
                float verticalOffset   = 0f;

                //horizontal alignment
                if (alignment.HasFlag(Alignment.Center))
                {
                    horizontalOffset = -w / 2f;
                }
                if (alignment.HasFlag(Alignment.Right))
                {
                    horizontalOffset = -w;
                }

                //vertical alignment
                if (alignment.HasFlag(Alignment.Top))
                {
                    verticalOffset = i * LineHeight;
                }
                if (alignment.HasFlag(Alignment.Middle))
                {
                    verticalOffset = -rows.Length * LineHeight / 2f + i * LineHeight;
                }
                if (alignment.HasFlag(Alignment.Bottom))
                {
                    verticalOffset = -rows.Length * LineHeight + i * LineHeight;
                }

                Point texturePos  = pos + new Point(horizontalOffset, verticalOffset);
                Point textureSize = new Point(w, h);

                DrawX.DrawJobs.Add(new TextureDrawJob(depth, new Rectangle(texturePos, textureSize), rowTextures[i], new Rectangle(Point.Zero, new Point(w, h)), new ComplexRectangle(texturePos, textureSize)));
            }
        }
Esempio n. 6
0
 public Texture(Renderer renderer, Surface surface)
 {
     TexturePtr = SDL.CreateTextureFromSurface(renderer.RendererPtr, surface.SurfacePtr);
     Util.ThrowIfFailed(TexturePtr);
 }