Exemple #1
0
        private static IntPtr GetScaledSurface(byte[] data, int srcW, int srcH, int dstW, int dstH)
        {
            // Create an SDL_Surface*, write the pixel data
            IntPtr surface = SDL_CreateRGBSurface(0, srcW, srcH, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);

            SDL_LockSurface(surface);
            unsafe
            {
                SDL_Surface *surPtr = (SDL_Surface *)surface;
                Marshal.Copy(data, 0, surPtr->pixels, data.Length);
            }
            SDL_UnlockSurface(surface);

            // Blit to a scaled surface of the size we want, if needed.
            if (srcW != dstW || srcH != dstH)
            {
                IntPtr scaledSurface = SDL_CreateRGBSurface(0, dstW, dstH, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
                SDL_SetSurfaceBlendMode(surface, SDL_BlendMode.SDL_BLENDMODE_NONE);
                SDL_BlitScaled(surface, IntPtr.Zero, scaledSurface, IntPtr.Zero);
                SDL_FreeSurface(surface);
                surface = scaledSurface;
            }

            return(surface);
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SDL2PlatformNativeSurface"/> class.
        /// </summary>
        /// <param name="src">A pointer to the native SDL surface.</param>
        public SDL2PlatformNativeSurface(SDL_Surface *src)
        {
            if (src == null)
            {
                throw new ArgumentNullException("src");
            }

            var dst = SDL_CreateRGBSurface(0, src->w, src->h, 32, rmask, gmask, bmask, amask);

            if (dst == null)
            {
                throw new SDL2Exception();
            }

            if (SDL_SetSurfaceBlendMode(dst, SDL_BLENDMODE_NONE) < 0)
            {
                throw new SDL2Exception();
            }

            if (SDL_BlitSurface(src, null, dst, null) < 0)
            {
                throw new SDL2Exception();
            }

            this.ptr = dst;
        }
Exemple #3
0
        private static unsafe IntPtr INTERNAL_getSurfacePixels(IntPtr surface)
        {
            IntPtr result;

            unsafe
            {
                SDL_Surface *surPtr = (SDL_Surface *)surface;
                result = surPtr->pixels;
            }
            return(result);
        }
Exemple #4
0
        private static unsafe int INTERNAL_getSurfaceHeight(IntPtr surface)
        {
            int result;

            unsafe
            {
                SDL_Surface *surPtr = (SDL_Surface *)surface;
                result = surPtr->h;
            }
            return(result);
        }
Exemple #5
0
        internal override void TextureDataFromStream(
            Stream stream,
            out int width,
            out int height,
            out byte[] pixels
            )
        {
            // Load the Stream into an SDL_RWops*
            byte[]   mem    = new byte[stream.Length];
            GCHandle handle = GCHandle.Alloc(mem, GCHandleType.Pinned);

            stream.Read(mem, 0, mem.Length);
            IntPtr rwops = SDL.SDL_RWFromMem(mem, mem.Length);

            // Load the SDL_Surface* from RWops, get the image data
            IntPtr surface = SDL_image.IMG_Load_RW(rwops, 1);

            if (surface == IntPtr.Zero)
            {
                // File not found, supported, etc.
                width  = 0;
                height = 0;
                pixels = null;
                return;
            }
            surface = INTERNAL_convertSurfaceFormat(surface);
            unsafe
            {
                SDL_Surface *surPtr = (SDL_Surface *)surface;
                width  = surPtr->w;
                height = surPtr->h;
                pixels = new byte[width * height * 4];                 // MUST be SurfaceFormat.Color!
                Marshal.Copy(surPtr->pixels, pixels, 0, pixels.Length);
            }
            SDL.SDL_FreeSurface(surface);
            handle.Free();

            /* Ensure that the alpha pixels are... well, actual alpha.
             * You think this looks stupid, but be assured: Your paint program is
             * almost certainly even stupider.
             * -flibit
             */
            for (int i = 0; i < pixels.Length; i += 4)
            {
                if (pixels[i + 3] == 0)
                {
                    pixels[i]     = 0;
                    pixels[i + 1] = 0;
                    pixels[i + 2] = 0;
                }
            }
        }
Exemple #6
0
        public static void LoadImage(string file, out int width, out int height, out byte[] data)
        {
            if (!File.Exists(file))
            {
                throw new FileNotFoundException("Texture file not found.", file);
            }

            IntPtr surfaceID = SDL_image.IMG_Load(file);

            // If image load fails, generate a default pink error texture
            if (surfaceID == IntPtr.Zero)
            {
                //throw new FileNotFoundException($"TextureDataFromStream: {SDL_GetError()}", file);

                surfaceID = SDL_CreateRGBSurfaceWithFormat(0, 32, 32, 0, SDL_PIXELFORMAT_ABGR8888);

                unsafe
                {
                    SDL_Surface *    surfacePtr     = (SDL_Surface *)surfaceID;
                    SDL_PixelFormat *pixelFormatPtr = (SDL_PixelFormat *)surfacePtr->format;
                    SDL_FillRect(surfaceID, IntPtr.Zero, SDL_MapRGB((IntPtr)pixelFormatPtr, 255, 20, 147));
                }
            }

            surfaceID = ConvertSurfaceFormat(surfaceID);

            unsafe
            {
                SDL_Surface *surface = (SDL_Surface *)surfaceID;
                width  = surface->w;
                height = surface->h;
                data   = new byte[width * height * 4];
                Marshal.Copy(surface->pixels, data, 0, data.Length);
            }

            SDL_FreeSurface(surfaceID);

            // Enforce alpha
            for (int i = 0; i < data.Length; i += 4)
            {
                if (data[i + 3] == 0)
                {
                    data[i]     = 0;
                    data[i + 1] = 0;
                    data[i + 2] = 0;
                }
            }

            data = FlipImageData(width, height, 4, data);
        }
Exemple #7
0
        public static unsafe Texture FromPath(string path)
        {
            IntPtr origSurface = IMG_Load(path);
            IntPtr newSurface  = SDL_ConvertSurfaceFormat(origSurface, SDL_PIXELFORMAT_ABGR8888, 0);

            SDL_Surface *surface = (SDL_Surface *)newSurface;

            Texture texture = FromData((uint)surface->w, (uint)surface->h, surface->pixels);

            SDL_FreeSurface(origSurface);
            SDL_FreeSurface(newSurface);

            return(texture);
        }
Exemple #8
0
        private static unsafe IntPtr INTERNAL_convertSurfaceFormat(IntPtr surface)
        {
            IntPtr result = surface;

            unsafe
            {
                SDL_Surface *        surPtr         = (SDL_Surface *)surface;
                SDL.SDL_PixelFormat *pixelFormatPtr = (SDL.SDL_PixelFormat *)surPtr->format;

                // SurfaceFormat.Color is SDL_PIXELFORMAT_ABGR8888
                if (pixelFormatPtr->format != SDL.SDL_PIXELFORMAT_ABGR8888)
                {
                    // Create a properly formatted copy, free the old surface
                    result = SDL.SDL_ConvertSurfaceFormat(surface, SDL.SDL_PIXELFORMAT_ABGR8888, 0);
                    SDL.SDL_FreeSurface(surface);
                }
            }
            return(result);
        }
 private static extern SDL_Cursor *INTERNAL_SDL_CreateColorCursor(SDL_Surface *surface, Int32 hot_x, Int32 hot_y);
 public override sealed Int32 SDL_GetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode *blendMode) => INTERNAL_SDL_GetSurfaceBlendMode(surface, blendMode);
Exemple #11
0
 public static extern SDL_Surface *SDL_ConvertSurface(SDL_Surface *src, SDL_PixelFormat *fmt, uint flags);
 private static extern Int32 INTERNAL_SDL_FillRect(SDL_Surface *surface, SDL_Rect *rect, UInt32 color);
Exemple #13
0
 public static SDL_Cursor *SDL_CreateColorCursor(SDL_Surface *surface, Int32 hot_x, Int32 hot_y) => impl.SDL_CreateColorCursor(surface, hot_x, hot_y);
        internal static bool Open(int w, int h, bool fullscreen, string caption)
        {
            if (IsSdlOpened)
            {
                AbormalTermination("SDL subsystem already open");
            }

            if (SDL_Init(0) == -1 || SDL_InitSubSystem((int)Init.Video) != 0)
            {
                AbormalTermination(SDL_GetError());
            }

            int flags = (int)(Video.HWSurface);
            if (fullscreen)
            {
                flags |= (int)(Video.FullScreen);
            }

            FrontSurface = SDL_SetVideoMode(w, h, 32, flags);
            if (FrontSurface == null)
            {
                AbormalTermination(SDL_GetError());
            }

            SDL_WM_SetCaption(caption, null);
            SDL_ShowCursor((int)Enable.Disable);

            if (SDL_InitSubSystem((int)Init.Joystick) != 0)
            {
                AbormalTermination(SDL_GetError());
            }

            OpenJoystickDevice(0);
            OpenJoystickDevice(1);

            SdlNativeMethods.FillRect(Color.Black);

            IsSdlOpened = true;
            return IsSdlOpened;
        }
Exemple #15
0
 public static Int32 SDL_BlitScaled(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect) => impl.SDL_BlitScaled(src, srcrect, dst, dstrect);
Exemple #16
0
 public static Int32 SDL_FillRect(SDL_Surface *surface, SDL_Rect *rect, UInt32 color) => impl.SDL_FillRect(surface, rect, color);
Exemple #17
0
 public static Int32 SDL_LockSurface(SDL_Surface *surface) => impl.SDL_LockSurface(surface);
Exemple #18
0
 public static Int32 SDL_SaveBMP_RW(SDL_Surface *surface, IntPtr dst, Int32 freedst) => impl.SDL_SaveBMP_RW(surface, dst, freedst);
 public override sealed Int32 SDL_FillRects(SDL_Surface *dst, SDL_Rect *rects, Int32 count, UInt32 colors) => INTERNAL_SDL_FillRects(dst, rects, count, colors);
 private static extern Int32 INTERNAL_SDL_FillRects(SDL_Surface *dst, SDL_Rect *rects, Int32 count, UInt32 colors);
 public override sealed Int32 SDL_FillRect(SDL_Surface *surface, SDL_Rect *rect, UInt32 color) => INTERNAL_SDL_FillRect(surface, rect, color);
Exemple #22
0
 public static void SDL_FreeSurface(SDL_Surface *surface) => impl.SDL_FreeSurface(surface);
 public override sealed SDL_Cursor *SDL_CreateColorCursor(SDL_Surface *surface, Int32 hot_x, Int32 hot_y) => INTERNAL_SDL_CreateColorCursor(surface, hot_x, hot_y);
Exemple #24
0
 public static void SDL_UnlockSurface(SDL_Surface *surface) => impl.SDL_UnlockSurface(surface);
Exemple #25
0
 public void draw(SDL_Surface Surface)
 {
     // Code for Draw
     m_surface = Surface;     // maybe, need to check datatype.
 }
Exemple #26
0
 public static Int32 SDL_GetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode *blendMode) => impl.SDL_GetSurfaceBlendMode(surface, blendMode);
Exemple #27
0
 public static extern void SDL_FreeSurface(SDL_Surface *surface);
Exemple #28
0
 public static Int32 SDL_FillRects(SDL_Surface *dst, SDL_Rect *rects, Int32 count, UInt32 colors) => impl.SDL_FillRects(dst, rects, count, colors);
Exemple #29
0
 public static extern int SDL_LockSurface(SDL_Surface *surface);
Exemple #30
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
        }
Exemple #31
0
 public static extern void SDL_UnlockSurface(SDL_Surface *surface);