Example #1
0
        public Texture CreateTextureFromFile(string path, int width, int height)
        {
            var texture = new Texture(path, width, height);

            texture.Pointer = SDL_image.IMG_LoadTexture(_render, path);
            return(texture);
        }
Example #2
0
        private bool InitTextures(ref IntPtr renderer)
        {
            if (renderer == IntPtr.Zero)
            {
                return(false);
            }

            try
            {
                List <string> texturesToLoad = new List <string>
                {
                    "./assets/covid.png"
                };
                foreach (string s in texturesToLoad)
                {
                    IntPtr tex = SDL_image.IMG_LoadTexture(renderer, s);
                    if (tex == IntPtr.Zero)
                    {
                        Console.WriteLine("SDL_CreateTexture Error: {0}", SDL.SDL_GetError());
                        return(false);
                    }
                    Textures.Add(tex);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }
            return(true);
        }
Example #3
0
    /// <summary>
    /// Loads a resizable texture from the Assets directory. Supports the following formats: BMP, GIF, JPEG, PNG, SVG, TGA, TIFF, WEBP.
    /// See the documentation for an explanation of what these parameters _actually_ mean.
    /// </summary>
    /// <param name="path">The path to the texture file, relative to the Assets directory.</param>
    /// <param name="leftOffset">The resize offset from the left of the texture (in pixels).</param>
    /// <param name="rightOffset">The resize offset from the right of the texture (in pixels).</param>
    /// <param name="topOffset">The resize offset from the top of the texture (in pixels).</param>
    /// <param name="bottomOffset">The resize offset from the bottom of the texture (in pixels).</param>
    public static ResizableTexture LoadResizableTexture(string path, int leftOffset, int rightOffset, int topOffset, int bottomOffset)
    {
        IntPtr handle = SDL_image.IMG_LoadTexture(Renderer, GetAssetPath(path));

        if (handle == IntPtr.Zero)
        {
            throw new Exception("Failed to load texture.");
        }

        uint format;
        int  access, width, height;

        SDL.SDL_QueryTexture(handle, out format, out access, out width, out height);

        // Convert the relative offsets (from the edges) into absolute offsets (from the origin):
        rightOffset  = width - rightOffset - 1;
        bottomOffset = height - bottomOffset - 1;

        if (leftOffset < 0 || rightOffset >= width || topOffset < 0 || bottomOffset >= height || leftOffset > rightOffset || topOffset > bottomOffset)
        {
            throw new Exception("Invalid offset parameter.");
        }

        return(new ResizableTexture(handle, width, height, leftOffset, rightOffset, topOffset, bottomOffset));
    }
Example #4
0
        /// <summary>
        ///  Create a new SDL texture from a file on disk.
        /// </summary>
        /// <remarks>
        ///  I'm normally not a huge fan of classes that load themselves, much less a constructor
        ///  that loads itself from a file. I have to make an exception for the moment because
        ///  SDL_image's API only supports loading from a file... once someone (maybe me) adds the
        ///  load from memory calls I can remove this and create a proper byte[] constructor
        ///  instead. (And leave the file loading up to the caller).
        /// </remarks>
        /// <param name="surface"></param>
        public SDLTexture(SDLRenderer renderer, string imagePath)
        {
            // Verify that our references are valid before proceeding.
            if (renderer == null)
            {
                throw new ArgumentNullException("renderer");
            }

            if (imagePath == null)
            {
                throw new ArgumentNullException("imagePath");
            }

            // Make sure the image actually exists on disk before attempting to ask SDL to load it.
            //  (Bonus: SDL doesn't return a null when the image does not exist on disk!)
            if (!File.Exists(imagePath))
            {
                throw new FileNotFoundException(imagePath);
            }

            // Attempt to load the bitmap from disk.
            mTexture = SDL_image.IMG_LoadTexture(renderer.RendererPtr, imagePath);

            // Test to make sure the texture was successfully created.
            if (mTexture == null)
            {
                throw new SDLException("Failed to load image from disk into SDL texture");
            }

            ReadAndStoreTextureProperties();
        }
Example #5
0
        public override Texture GetTexture(string textureId, IFileResolver fileResolver)
        {
            string assetName = fileResolver.Resolve(textureId);
            IntPtr tex       = SDL_image.IMG_LoadTexture(game.ren, assetName);

            return(new SDLTexture(textureId, tex));
        }
Example #6
0
        public IntPtr LoadTexture(IntPtr renderer, string filename, string key)
        {
            var texture = SDL_image.IMG_LoadTexture(renderer, filename);

            textures.Add(key, texture);

            return(texture);
        }
Example #7
0
        public static void LoadInit(IntPtr rendererPtr, string friendlyFireTexturePath, string enemyFireTexturePath)
        {
            friendlyTexture = SDL_image.IMG_LoadTexture(rendererPtr, friendlyFireTexturePath);
            enemyTexture    = SDL_image.IMG_LoadTexture(rendererPtr, enemyFireTexturePath);
            SDL.SDL_QueryTexture(texture, out _, out _, out width, out height);

            center.x = width / 2 * scale;
            center.y = height / 2 * scale;
        }
        public static new void LoadInit(IntPtr rendererPtr, string spriteSheetPath)
        {
            SpriteSheet = SDL_image.IMG_LoadTexture(rendererPtr, spriteSheetPath);
            Console.WriteLine(SDL.SDL_GetError());

            SDL.SDL_QueryTexture(SpriteSheet, out _, out _, out int w, out int h);
            width  = w / 12;
            height = h / 2;
        }
Example #9
0
        public static new void LoadInit(IntPtr rendererPtr, string texturePath)
        {
            texture = SDL_image.IMG_LoadTexture(rendererPtr, texturePath);

            SDL.SDL_QueryTexture(texture, out _, out _, out width, out height);

            center.x = width / 2 * scale;
            center.y = height / 2 * scale;
        }
Example #10
0
        public Sprite LoadSprite(string path)
        {
            IntPtr handle = SDL_image.IMG_LoadTexture(platformSystem.Renderer, path);

            return(new Sprite
            {
                Handle = handle,
            });
        }
Example #11
0
        static (IntPtr, SDL.SDL_Rect) Load(string path, IntPtr renderer)
        {
            IntPtr texture = SDL_image.IMG_LoadTexture(renderer, path);

            SDL.SDL_QueryTexture(texture, out uint a, out int b, out int _w, out int _h);
            return(texture, new SDL.SDL_Rect()
            {
                h = _h, w = _w
            });
Example #12
0
        public Texture LoadTexture(string imgSource)
        {
            IntPtr texture = SDL_image.IMG_LoadTexture(_renderer, imgSource);

            if (texture == IntPtr.Zero)
            {
                Console.WriteLine("Could not load texture. {0}", SDL.SDL_GetError());
            }
            return(new Texture(texture));
        }
Example #13
0
        public static Font LoadFromChevyRayFolder(IntPtr renderer, string path)
        {
            var fontTexture = SDL_image.IMG_LoadTexture(renderer, Path.Combine(path, "atlas32.png"));

            SDL.SDL_QueryTexture(fontTexture, out _, out _, out var fontTextureWidth, out var fontTextureHeight);

            var fontMetrics = FontMetrics.FromChevyRayJson(Path.Combine(path, "metrics.json"));

            return(new Font(renderer, new TextureArea(fontTexture, new Rectangle(0, 0, fontTextureWidth, fontTextureHeight)), fontMetrics));
        }
Example #14
0
        public static IntPtr LoadTexture(string path)
        {
            if (path == "")
            {
                return(IntPtr.Zero);
            }

            // Load the image as a surface
            return(SDL_image.IMG_LoadTexture(renderer, path));
        }
Example #15
0
        public static IntPtr LoadTextureFromImage(string fileName, IntPtr renderer)
        {
            var texture = SDL_image.IMG_LoadTexture(renderer, FilePath.Get(fileName));

            if (texture == IntPtr.Zero)
            {
                Logger.Error(nameof(SDL_image.IMG_LoadTexture));
            }

            return(texture);
        }
        public static IntPtr LoadTextureFromImage(string path, IntPtr renderer)
        {
            var texture = SDL_image.IMG_LoadTexture(renderer, path);

            if (texture == IntPtr.Zero)
            {
                SdlLogger.Error(nameof(SDL_image.IMG_LoadTexture));
            }

            return(texture);
        }
Example #17
0
        public void UpdateImage(string srcImage, SDL.SDL_Rect dstRect)
        {
            var iconTexture = SDL_image.IMG_LoadTexture(_renderer, srcImage);

            if (iconTexture == IntPtr.Zero)
            {
                Debug.WriteLine($"Could not create texture: {SDL.SDL_GetError()}");
                return;
            }
            SDL.SDL_RenderCopy(_renderer, iconTexture, ref _rectOrigin, ref dstRect);
        }
Example #18
0
        public virtual void LoadInit(IntPtr rendererPtr, string texturePath)
        {
            texture = SDL_image.IMG_LoadTexture(rendererPtr, texturePath);

            SDL.SDL_QueryTexture(texture, out _, out _, out int w, out int h);

            dstRect.w = w * scale;
            dstRect.h = h * scale;

            center.x = w / 2 * scale;
            center.y = h / 2 * scale;
        }
Example #19
0
        public IntPtr GetTexture(string imageFile)
        {
            var imagePath = Path.Combine(ImagesPath, imageFile);

            if (!TexturesByPath.TryGetValue(imagePath, out var texture))
            {
                texture = SDL_image.IMG_LoadTexture(WRenderer, imagePath);
                TexturesByPath[imagePath] = texture;
            }

            return(texture);
        }
Example #20
0
        public SDLImage loadImage(string path)
        {
            SDLImage image   = null;
            IntPtr   texture = IntPtr.Zero;

            texture = SDL_image.IMG_LoadTexture(this.mRenderer, path);

            if (texture != IntPtr.Zero)
            {
                image = new SDLImage(texture);
            }

            return(image);
        }
Example #21
0
        /// <summary>
        /// Create a texture from an image file
        /// </summary>
        /// <param name="renderer">The renderer that will render this texture</param>
        /// <param name="name">The path to the image from the resource folder</param>
        public Texture(Renderer renderer, string name)
        {
            // Construct the full path
            string path = Path.Combine(Graphics.RESOURCE_PATH, name);

            IntPtr texture = SDL_image.IMG_LoadTexture(renderer.RenPtr, path);

            if (texture == IntPtr.Zero)
            {
                throw new SDLException("IMG_LoadTexture");
            }

            TexPtr = texture;
        }
    static int LoadSprite(string file, System.IntPtr renderer)
    {
        Sprite = SDL_image.IMG_LoadTexture(renderer, file);
        uint dummy1;
        int  dummy2;

        SDL.SDL_QueryTexture(Sprite, out dummy1, out dummy2, out Sprite_w, out Sprite_h);
        if (Sprite_w == 0)
        {
            //	    SDL.SDL_LogError(SDL.SDL_LOG_CATEGORY_APPLICATION, "{0}:{1}\n", __arglist(file, SDL.SDL_GetError()));
            System.Console.WriteLine(System.String.Format("{0}:{1}", file, SDL.SDL_GetError()));
            return(-1);
        }
        return(0);
    }
Example #23
0
            public override void Load(string path, ContentLoader contentLoader)
            {
                var sdlCanvas = contentLoader.Canvas as SDLCanvas;

                FilePath = path;

                SDL_Image = SDL_image.IMG_LoadTexture(sdlCanvas.SDLRenderer, FilePath);
                if (SDL_Image == SDL_Image.Zero)
                {
                    Logger.Log(LogLevel.Error, $"Error loading SDLTexture '{path}' with error : {SDL2.SDL.SDL_GetError()}");
                }

                SDL2.SDL.SDL_QueryTexture(SDL_Image, out _, out _, out int w, out int h);
                Size = new Vec2(w, h);
            }
        public void LoadInit(IntPtr rendererPtr)
        {
            carTexture = SDL_image.IMG_LoadTexture(rendererPtr, "media/car_img.png");

            int w, h;

            SDL.SDL_QueryTexture(carTexture, out _, out _, out w, out h);

            dstRect.w = w * scale;
            dstRect.h = h * scale;

            center.x = w / 2 * scale;
            center.y = h / 2 * scale;

            halfCarLen = w / 2 * scale;
        }
Example #25
0
    /// <summary>
    /// Loads a texture from the Assets directory. Supports the following formats: BMP, GIF, JPEG, PNG, SVG, TGA, TIFF, WEBP.
    /// </summary>
    /// <param name="path">The path to the texture file, relative to the Assets directory.</param>
    public static Texture LoadTexture(string path)
    {
        IntPtr handle = SDL_image.IMG_LoadTexture(Renderer, GetAssetPath(path));

        if (handle == IntPtr.Zero)
        {
            throw new Exception("Failed to load texture.");
        }

        uint format;
        int  access, width, height;

        SDL.SDL_QueryTexture(handle, out format, out access, out width, out height);

        return(new Texture(handle, width, height));
    }
        public void LoadInit(IntPtr rendererPtr, string texturePath)
        {
            backgroundTexture = SDL_image.IMG_LoadTexture(rendererPtr, texturePath);

            if (backgroundTexture == IntPtr.Zero)
            {
                Console.WriteLine("Error loading background texture. {0}", SDL.SDL_GetError());
            }

            SDL.SDL_QueryTexture(backgroundTexture, out _, out _, out int w, out int h);

            dstRect.w = w * SCALE;
            dstRect.h = h * SCALE;

            center.x = w / 2 * SCALE;
            center.y = h / 2 * SCALE;
        }
        public override void LoadInit(IntPtr rendererPtr, string spriteSheetPath)
        {
            spriteSheet = SDL_image.IMG_LoadTexture(rendererPtr, spriteSheetPath);
            Console.WriteLine(SDL.SDL_GetError());

            SDL.SDL_QueryTexture(spriteSheet, out _, out _, out int w, out int h);
            w /= 12;
            h /= 2;

            dstRect.w = w * SCALE;
            dstRect.h = h * SCALE;

            center.x = w / 2 * SCALE;
            center.y = h / 2 * SCALE;

            srcRect.x = 0;
            srcRect.y = 0;
            srcRect.w = SPRITE_PIXELS;
            srcRect.h = SPRITE_PIXELS;
        }
Example #28
0
        public void LoadInit(IntPtr rendererPtr, string spriteSheetPath, int rows, int columns, float scale)
        {
            spriteSheet = SDL_image.IMG_LoadTexture(rendererPtr, spriteSheetPath);
            SDL.SDL_QueryTexture(spriteSheet, out _, out _, out int totalW, out int totalH);

            this.rows    = rows;
            this.columns = columns;

            width  = totalW / rows;
            height = totalH / columns;

            dstRect.w = (int)(width * scale);
            dstRect.h = (int)(height * scale);

            srcRect.x = 0;
            srcRect.y = 0;
            srcRect.w = width;
            srcRect.h = height;

            center.x = (int)(srcRect.w / 2 * scale);
            center.y = (int)(srcRect.h / 2 * scale);
        }
Example #29
0
        public void Run()
        {
            SDL.SDL_Init(SDL.SDL_INIT_VIDEO);
            SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_PNG | SDL_image.IMG_InitFlags.IMG_INIT_JPG);

            var window = SDL.SDL_CreateWindow("Testing SDL2", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, 400, 800, 0);

            renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC);

            field = new int[M, N];

            tilesTexture = SDL_image.IMG_LoadTexture(renderer, @"img\Tiles.png");
            if (tilesTexture == IntPtr.Zero)
            {
                Console.WriteLine("Error loading texture: " + SDL.SDL_GetError());
            }


            for (int i = 0; i < 4; i++)
            {
                point[i].X = shapes[shapeToDraw, i] % 2;
                point[i].Y = shapes[shapeToDraw, i] / 2;
            }

            running = true;

            while (running)
            {
                HandelInput();
                Update();
                Render();
            }

            SDL.SDL_DestroyWindow(window);
            SDL.SDL_Quit();
        }
 public static void LoadInit(IntPtr rendererPtr, string texturePath)
 {
     texture = SDL_image.IMG_LoadTexture(rendererPtr, texturePath);
     SDL.SDL_QueryTexture(texture, out _, out _, out width, out height);
 }