Пример #1
0
 unsafe UIntPtr NativeWrite(SDL_RWops *io, byte *ptr, UIntPtr size, UIntPtr num)
 {
     if (Implementation == null)
     {
         SetError($"{nameof(RWOpsFromInterface)} is missing an implementation to call");
         return(UIntPtr.Zero);
     }
     try
     {
         for (ulong i = 0; i < (ulong)num; ++i)
         {
             var span = new Span <byte>(ptr + i * (ulong)size, (int)size);
             try
             {
                 int written = Implementation.Write(span);
                 if (written < (int)size)
                 {
                     return((UIntPtr)i - 1);
                 }
             }
             catch (Exception ex)
             {
                 SetError(ex);
                 return((UIntPtr)i - 1);
             }
         }
         return(num);
     }
     catch (Exception ex)
     {
         SetError(ex);
         return(UIntPtr.Zero);
     }
 }
Пример #2
0
 unsafe long NativeSeek(SDL_RWops *io, long offset, int whence)
 {
     if (Implementation == null)
     {
         SetError($"{nameof(RWOpsFromInterface)} is missing an implementation to call");
         return(-1);
     }
     try
     {
         long ret;
         if (whence == 1)
         {
             ret = Implementation.Seek(offset - partial.Count, whence);
         }
         else
         {
             ret = Implementation.Seek(offset, whence);
         }
         if (ret >= 0)
         {
             partial = partial.Slice(0, 0);
         }
         return(ret);
     }
     catch (Exception ex)
     {
         SetError(ex);
         return(-1);
     }
 }
Пример #3
0
        private static Int64 RWops_seek_callback(SDL_RWops *rwops, Int64 pos, Int32 flags)
        {
            var handle = GCHandle.FromIntPtr(rwops->data1);
            var target = (SDL2StreamWrapper)handle.Target;

            if (target == null || target.disposed || !target.stream.CanSeek)
            {
                return(-1);
            }

            switch (flags)
            {
            case RW_SEEK_SET:
                target.stream.Seek(pos, SeekOrigin.Begin);
                break;

            case RW_SEEK_END:
                target.stream.Seek(pos, SeekOrigin.End);
                break;

            default:
                target.stream.Seek(pos, SeekOrigin.Current);
                break;
            }

            return(target.stream.Position);
        }
Пример #4
0
        private static UInt32 RWops_write_callback(SDL_RWops *rwops, IntPtr ptr, UInt32 size, UInt32 num)
        {
            var handle = GCHandle.FromIntPtr(rwops->data1);
            var target = (SDL2StreamWrapper)handle.Target;

            if (target == null || target.disposed || !target.stream.CanWrite)
            {
                return(0);
            }

            var objCount  = 0u;
            var objBuffer = new Byte[size];

            var input = (Byte *)ptr.ToPointer();

            for (uint i = 0; i < num; i++)
            {
                Marshal.Copy((IntPtr)input, objBuffer, 0, objBuffer.Length);
                input += size;

                target.stream.Write(objBuffer, 0, objBuffer.Length);

                objCount++;
            }

            return(objCount);
        }
Пример #5
0
        private static UInt32 RWops_read_callback(SDL_RWops *rwops, IntPtr ptr, UInt32 size, UInt32 maxnum)
        {
            var handle = GCHandle.FromIntPtr(rwops->data1);
            var target = (SDL2StreamWrapper)handle.Target;

            if (target == null || target.disposed || !target.stream.CanRead)
            {
                return(0);
            }

            var objCount  = 0u;
            var objBuffer = new Byte[size];

            var output = (Byte *)ptr.ToPointer();

            for (uint i = 0; i < maxnum; i++)
            {
                if (target.stream.Read(objBuffer, 0, objBuffer.Length) < objBuffer.Length)
                {
                    break;
                }

                Marshal.Copy(objBuffer, 0, (IntPtr)output, objBuffer.Length);
                output += size;

                objCount++;
            }

            return(objCount);
        }
Пример #6
0
        private static Int64 RWops_size_callback(SDL_RWops *rwops)
        {
            var handle = GCHandle.FromIntPtr(rwops->data1);
            var target = (SDL2StreamWrapper)handle.Target;

            if (target == null || target.disposed)
            {
                return(-1);
            }

            return(target.stream.Length);
        }
Пример #7
0
 unsafe int NativeClose(SDL_RWops *io)
 {
     try
     {
         this.Dispose();
         return(0);
     }
     catch (Exception ex)
     {
         SetError(ex);
         return(-1);
     }
 }
Пример #8
0
        private static Int32 RWops_close_callback(SDL_RWops *rwops)
        {
            var handle = GCHandle.FromIntPtr(rwops->data1);
            var target = (SDL2StreamWrapper)handle.Target;

            if (target == null || target.disposed)
            {
                return(-1);
            }

            target.Dispose();
            return(0);
        }
Пример #9
0
 unsafe long NativeSize(SDL_RWops *io)
 {
     if (Implementation == null)
     {
         SetError($"{nameof(RWOpsFromInterface)} is missing an implementation to call");
         return(-1);
     }
     try
     {
         return(Implementation.Size());
     }
     catch (Exception ex)
     {
         SetError(ex);
         return(-1);
     }
 }
Пример #10
0
        public Color[,] LoadInternal(int *w, int *h, int *pixeldepth, Stream file)
        {
            if (file == null)
            {
                throw new ArgumentNullException();
            }
            if (w == null || h == null || pixeldepth == null)
            {
                throw  new ArgumentNullException();
            }



#if SYSTEM_DRAWING
            using (Bitmap bmp = new Bitmap(file))
            {
                BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                Color[,] rz_mp = new Color[bmp.Width, bmp.Height];
                *   pixeldepth = 4;
                *   w          = bmp.Width;
                *   h          = bmp.Height;
                int scan0size  = bmp.Width * bmp.Height;
                for (int display = 0; display < (scan0size); display++)
                {
                    UniColor4 cur = ((UniColor4 *)bmp_data.Scan0.ToPointer())[display];
                    fixed(Color *p = &rz_mp[0, 0])
                    {
                        Color *pix = p + display;

                        pix->A = cur.A;
                        pix->R = cur.B;
                        pix->G = cur.G;
                        pix->B = cur.R;
                    }
                }

                bmp.UnlockBits(bmp_data);
                return(rz_mp);
            }
#else
            byte[] rw = new byte[file.Length];
            file.Read(rw, 0, rw.Length);
            SDL_RWops *  pops = null;
            SDL_Surface *sf   = null;

            fixed(byte *rws = &rw[0])
            pops = SDL.SDL_RWFromMem(rws, rw.Length);

            if (pops == null)
            {
                throw new FunctionReturnedNullException("SDL_RWFromMem");
            }

            sf = SDL.IMG_Load_RW(pops, 1);
            if (sf == null)
            {
                SDL.SDL_FreeRW(pops);
                throw new FunctionReturnedNullException("IMG_Load_RW");
            }
            try
            {
                Color[,] rz_mp = new Color[sf->w, sf->h];
                *   pixeldepth = sf->format->BytesPerPixel;
                *   w          = sf->w;
                *   h          = sf->h;
                int scan0size  = sf->h * sf->w;
                if (*pixeldepth == 1)
                {
                    for (int display = 0; display < (scan0size); display++)
                    {
                        UniColor1 cur = ((UniColor1 *)sf->pixels)[display];
                        fixed(Color *p = &rz_mp[0, 0])
                        {
                            Color *pix = p + display;

                            pix->A = 255;
                            pix->R = cur.C;
                            pix->G = cur.C;
                            pix->B = cur.C;
                        }
                    }
                }
                if (*pixeldepth == 3)
                {
                    for (int display = 0; display < (scan0size); display++)
                    {
                        UniColor3 cur = ((UniColor3 *)sf->pixels)[display];
                        fixed(Color *p = &rz_mp[0, 0])
                        {
                            Color *pix = p + display;

                            pix->A = 255;
                            pix->R = cur.R;
                            pix->G = cur.G;
                            pix->B = cur.B;
                        }
                    }
                }
                if (*pixeldepth == 4)
                {
                    for (int display = 0; display < (scan0size); display++)
                    {
                        UniColor4 cur = ((UniColor4 *)sf->pixels)[display];
                        fixed(Color *p = &rz_mp[0, 0])
                        {
                            Color *pix = p + display;

                            pix->A = cur.A;
                            pix->R = cur.R;
                            pix->G = cur.G;
                            pix->B = cur.B;
                        }
                    }
                }
                SDL.SDL_FreeSurface(sf);
                SDL.SDL_FreeRW(pops);
                return(rz_mp);
            }
            catch (Exception r)
            {
                SDL.SDL_FreeSurface(sf);
                SDL.SDL_FreeRW(pops);
                throw r;
            }
#endif
        }
Пример #11
0
        unsafe UIntPtr NativeRead(SDL_RWops *io, byte *ptr, UIntPtr size, UIntPtr maxnum)
        {
            if (Implementation == null)
            {
                SetError($"{nameof(RWOpsFromInterface)} is missing an implementation to call");
                return(UIntPtr.Zero);
            }
            try
            {
                for (ulong i = 0; i < (ulong)maxnum; ++i)
                {
                    if (partial.Count < (int)size)
                    {
                        if (partial.Count > 0)
                        {
                            var arr = partial.Array;
                            System.Diagnostics.Debug.Assert(arr.Length > 0);
                            var l = partial.Count;
                            partial.CopyTo(arr);
                            partial = arr;
                            partial = partial.Slice(l);
                        }
                        else
                        {
                            partial = partial.Array;
                        }

                        Span <byte> buffer = partial;
                        try
                        {
                            int read = Implementation.Read(buffer);
                            if (read == 0)
                            {
                                return((UIntPtr)i - 1);
                            }
                            if (read < partial.Count)
                            {
                                partial = partial.Slice(0, read);
                            }
                        }
                        catch (Exception ex)
                        {
                            SetError(ex);
                            return((UIntPtr)i - 1);
                        }

                        if (partial.Count < (int)size)
                        {
                            return((UIntPtr)i - 1);
                        }
                    }

                    var         target = new Span <byte>(ptr + i * (ulong)size, (int)size);
                    Span <byte> src    = partial.Slice(0, (int)size);
                    partial = partial.Slice((int)size);
                    src.CopyTo(target);
                }
                return(maxnum);
            }
            catch (Exception ex)
            {
                SetError(ex);
                return(UIntPtr.Zero);
            }
        }
Пример #12
0
 public static extern SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src);
Пример #13
0
 public static extern SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc);
Пример #14
0
 public static extern void SDL_FreeRW(SDL_RWops *are);