Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pf"></param>
        /// <param name="surf"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int GetIntPixel(Sdl.SDL_Surface s, Sdl.SDL_PixelFormat pf, IntPtr surf, int x, int y)
        {
            IntPtr ptr   = new IntPtr(s.pixels.ToInt32() + (y * s.pitch) + (x * pf.BytesPerPixel)); //* bytesPerPixel);
            int    value = 0;

            switch (pf.BytesPerPixel)
            {
            case 1:
                value = Marshal.ReadByte(ptr);
                break;

            case 2:
                value = Marshal.ReadInt16(ptr);
                break;

            case 3:
                value = MarshalHelper.ReadInt24(ptr);
                break;

            case 4:
                //value = Marshal.ReadInt32(ptr);
                break;
                //default:
                //throw new SdlException(Events.StringManager.GetString("UnknownBytesPerPixel", CultureInfo.CurrentUICulture));
            }
            return(value);
        }
Beispiel #2
0
 internal Surface(IntPtr surface)
 {
     this.surfacePtrs[0] = surface;
     this.surface        = (Sdl.SDL_Surface)Marshal.PtrToStructure(this.SurfacePtr(), typeof(Sdl.SDL_Surface));
     this.pixelFormat    = (Sdl.SDL_PixelFormat)Marshal.PtrToStructure(this.surface.format, typeof(Sdl.SDL_PixelFormat));
     this.SourceColorKey = Color.Magenta;
     this.ResetClipRect();
     this.currentSurfaceCount++;
 }
Beispiel #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="w"></param>
 /// <param name="h"></param>
 /// <param name="newbpp"></param>
 public Surface(int w, int h, int newbpp)
 {
     bpp = newbpp;
     this.surfacePtrs[0] = Sdl.SDL_CreateRGBSurface(flags, w, h, bpp, 0, 0, 0, 0);
     this.surface        = (Sdl.SDL_Surface)Marshal.PtrToStructure(this.SurfacePtr(), typeof(Sdl.SDL_Surface));
     pixelFormat         = (Sdl.SDL_PixelFormat)Marshal.PtrToStructure(this.surface.format, typeof(Sdl.SDL_PixelFormat));
     this.ResetClipRect();
     this.currentSurfaceCount++;
 }
Beispiel #4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="filename"></param>
 public Surface(string filename)
 {
     this.surfacePtrs[0] = Tao.Sdl.SdlImage.IMG_Load(filename);
     this.surface        = (Sdl.SDL_Surface)Marshal.PtrToStructure(this.SurfacePtr(), typeof(Sdl.SDL_Surface));
     this.pixelFormat    = (Sdl.SDL_PixelFormat)Marshal.PtrToStructure(this.surface.format, typeof(Sdl.SDL_PixelFormat));
     this.SourceColorKey = Color.Magenta;
     this.ResetClipRect();
     this.currentSurfaceCount++;
     this._filename = filename;
 }
Beispiel #5
0
        public void SDL_MapRGBA()
        {
            IntPtr surfacePtr = VideoSetup();

            Sdl.SDL_Surface surface =
                (Sdl.SDL_Surface)Marshal.PtrToStructure(surfacePtr, typeof(Sdl.SDL_Surface));
            surfaceFormatPtr = surface.format;
            Sdl.SDL_PixelFormat surfaceFormat =
                (Sdl.SDL_PixelFormat)Marshal.PtrToStructure(surfaceFormatPtr, typeof(Sdl.SDL_PixelFormat));
            int result = Sdl.SDL_MapRGBA(surfaceFormatPtr, 255, 255, 0, 0);

            Assert.AreEqual(surfaceFormat.BitsPerPixel, 16);
            Assert.AreEqual(result, 65504);
            Sdl.SDL_FreeSurface(surfacePtr);
        }
        public void initFromSurface(IntPtr surf)
        {
            // Marshal in info
            Sdl.SDL_Surface managedSurf = (Sdl.SDL_Surface)Marshal.PtrToStructure(surf, typeof(Sdl.SDL_Surface));
            width  = managedSurf.w;
            height = managedSurf.h;

            IntPtr format = managedSurf.format;

            Sdl.SDL_PixelFormat managedFormat = (Sdl.SDL_PixelFormat)Marshal.PtrToStructure(format, typeof(Sdl.SDL_PixelFormat));

            if (managedFormat.BytesPerPixel == 4)
            {
                if (managedFormat.Rmask == 0x000000ff)
                {
                    texture_format = Gl.GL_RGBA;
                }
                else
                {
                    texture_format = Gl.GL_BGRA;
                }
            }
            else if (managedFormat.BytesPerPixel == 3)
            {
                if (managedFormat.Rmask == 0x000000ff)
                {
                    texture_format = Gl.GL_RGB;
                }
                else
                {
                    texture_format = Gl.GL_BGR;
                }
            }
            else
            {
                throw new Exception("Only true color textures supported.");
            }

            Gl.glGenTextures(1, out handle);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, handle);
            // Nearest neighbor scaling
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_NEAREST);
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_NEAREST);

            // Load texture data into video memory
            Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, managedFormat.BytesPerPixel, width, height, 0, texture_format, Gl.GL_UNSIGNED_BYTE, managedSurf.pixels);
        }
Beispiel #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <param name="pf"></param>
        public Surface(int w, int h, IntPtr pf)
        {
            if (pf == IntPtr.Zero)
            {
                this.surfacePtrs[0] = Sdl.SDL_CreateRGBSurface(flags, w, h, bpp, 0, 0, 0, 0);
                this.surface        = (Sdl.SDL_Surface)Marshal.PtrToStructure(this.SurfacePtr(), typeof(Sdl.SDL_Surface));
                pixelFormat         = (Sdl.SDL_PixelFormat)Marshal.PtrToStructure(this.surface.format, typeof(Sdl.SDL_PixelFormat));
            }
            else
            {
                pixelFormat         = (Sdl.SDL_PixelFormat)Marshal.PtrToStructure(pf, typeof(Sdl.SDL_PixelFormat));
                this.surfacePtrs[0] = Sdl.SDL_CreateRGBSurface(flags, w, h, bpp, pixelFormat.Rmask, pixelFormat.Gmask, pixelFormat.Bmask, pixelFormat.Amask);
                this.surface        = (Sdl.SDL_Surface)Marshal.PtrToStructure(this.SurfacePtr(), typeof(Sdl.SDL_Surface));
            }

            this.SourceColorKey = Color.Magenta;
            this.ResetClipRect();
            this.currentSurfaceCount++;
        }
Beispiel #8
0
        public void SDL_GetRGB()
        {
            int    pixel      = 65504;
            IntPtr surfacePtr = VideoSetup();
            byte   r;
            byte   g;
            byte   b;

            Sdl.SDL_Surface surface =
                (Sdl.SDL_Surface)Marshal.PtrToStructure(surfacePtr, typeof(Sdl.SDL_Surface));
            surfaceFormatPtr = surface.format;
            Sdl.SDL_PixelFormat surfaceFormat =
                (Sdl.SDL_PixelFormat)Marshal.PtrToStructure(surfaceFormatPtr, typeof(Sdl.SDL_PixelFormat));
            Sdl.SDL_GetRGB(pixel, surfaceFormatPtr, out r, out g, out b);
            Assert.AreEqual(r, 255);
            Assert.AreEqual(g, 255);
            Assert.AreEqual(b, 0);
            Sdl.SDL_FreeSurface(surfacePtr);
        }
Beispiel #9
0
        public Video()
        {
            m_Screen     = Sdl.SDL_SetVideoMode(640, 512, 32, (Sdl.SDL_SWSURFACE | Sdl.SDL_DOUBLEBUF | Sdl.SDL_ANYFORMAT));
            m_ScreenSurf = (Sdl.SDL_Surface)Marshal.PtrToStructure(m_Screen, typeof(Sdl.SDL_Surface));
            Sdl.SDL_PixelFormat format = (Sdl.SDL_PixelFormat)Marshal.PtrToStructure(m_ScreenSurf.format, typeof(Sdl.SDL_PixelFormat));

            for (int i = 0; i < 512; i++)
            {
                int b = ((i) & 0x7) * 0x49 >> 1;
                int r = ((i >> 3) & 0x7) * 0x49 >> 1;
                int g = ((i >> 6) & 0x7) * 0x49 >> 1;

                PALETTE[i] =
                    (r << format.Rshift & format.Rmask) |
                    (g << format.Gshift & format.Gmask) |
                    (b << format.Bshift & format.Bmask);
            }
            m_ScreenSurf.pitch /= 4;

            Sdl.SDL_WM_SetCaption("TurboSharp", null);

            m_VRAM      = new ushort[0x10000];
            m_SAT       = new SpriteAttribute[0x40];
            m_VCE       = new ushort[0x200];
            m_VCE_Index = 0;

            for (int i = 0; i < 0x40; i++)
            {
                m_SAT[i] = new SpriteAttribute();
            }

            m_RenderLine = 0;
            m_DoSAT_DMA  = false;
            m_WaitingIRQ = false;

            m_VCE_DotClock  = DotClock.MHZ_5;
            m_VDC_Increment = 1;

            m_VDC_BSY = false;  // We don't halt the CPU
        }
Beispiel #10
0
        private void Go()
        {
            splashscreen = new Splash();
            splashscreen.Show();
            splashscreen.BringToFront();
            splashscreen.Refresh();
            Core.Init(null, null, null, new ProgressHandler(UpdateMessage), true);
            world = new WorldDefinition(new Distance(150, 150, 7), 3);
            WorldDefinition.World = world;
            mainWindowMDI         = new MainWindow();
            timer = new System.Windows.Forms.Timer();
            Events.KeyboardDown    += new EventHandler <KeyboardEventArgs>(this.KeyDown);
            Events.MouseButtonDown +=
                new EventHandler <MouseButtonEventArgs>(this.MouseButtonDown);
            Events.MouseButtonUp +=
                new EventHandler <MouseButtonEventArgs>(this.MouseButtonUp);
            Events.Quit          += new EventHandler <QuitEventArgs>(this.Quit);
            Events.Tick          += new EventHandler <TickEventArgs>(this.Tick);
            Events.VideoResize   += new EventHandler <SdlDotNet.Graphics.VideoResizeEventArgs>(this.Resize);
            Events.MusicFinished +=
                new EventHandler <MusicFinishedEventArgs>(this.MusicFinished);
            Events.MouseMotion     += new EventHandler <MouseMotionEventArgs>(this.MouseMotion);
            dragStartMousePosition  = new Point(0, 0);
            dragStartScrollPosition = new Point(0, 0);
            ScrollPosition          = new Point(0, 0);
            this.width  = Properties.Settings.Default.RenderWindowWidth;
            this.height = Properties.Settings.Default.RenderWindowHeight;

            try
            {
                SdlDotNet.Graphics.Video.WindowIcon();
                SdlDotNet.Graphics.Video.WindowCaption = Translation.GetString("MAIN_WINDOW_TITLE");
                SdlDotNet.Graphics.Video.Initialize();
                splashscreen.BringToFront();
            }
            catch
            {
                MessageBox.Show(Translation.GetString("SDL_NOT_FOUND"));
                return;
            }

            Mixer.Initialize();

            SdlMixer.MusicFinishedDelegate musicStopped = new SdlMixer.MusicFinishedDelegate(MusicHasStopped);
            SdlMixer.Mix_HookMusicFinished(musicStopped);


            screen = SdlDotNet.Graphics.Video.SetVideoMode(width, height, currentBpp, true);
            screen.SourceColorKey = Color.Magenta;
            IntPtr videoInfoPointer = Sdl.SDL_GetVideoInfo();

            if (videoInfoPointer != IntPtr.Zero)
            {
                videoInfo   = (Sdl.SDL_VideoInfo)Marshal.PtrToStructure(videoInfoPointer, typeof(Sdl.SDL_VideoInfo));
                pixelFormat = (Sdl.SDL_PixelFormat)Marshal.PtrToStructure(videoInfo.vfmt, typeof(Sdl.SDL_PixelFormat));
            }
            splashscreen.status.AppendText("FreeTrain SDL Starting...");
            splashscreen.status.AppendText("\n");
            splashscreen.Refresh();

            FinalDraw();

            splashscreen.status.AppendText("Loading plugins...");
            splashscreen.status.AppendText("\n");
            splashscreen.Refresh();
            FinalDraw();


            weatherOverlay = NullWeatherOverlay.theInstance;
            FinalDraw();
            qView = new QuarterViewDrawer(world, new Rectangle(0, 0, world.Size.x * 32 - 16, (world.Size.y - 2 * world.Size.z - 1) * 8));
            qView.OffscreenBuffer = new Surface(world.Size.x * 32 - 16, (world.Size.y - 2 * world.Size.z - 1) * 8, screen.CreateCompatibleSurface().Pixels);
            qView.OffscreenBuffer.SourceColorKey = Color.Magenta;
            qView.RecreateDrawBuffer(new Size(width, height), true);
            splashscreen.status.AppendText("Creating Map...");
            splashscreen.status.AppendText("\n");
            splashscreen.Refresh();
            FinalDraw();

            qView.Draw(new Rectangle(0, 0, world.Size.x * 32 - 16, (world.Size.y - 2 * world.Size.z - 1) * 8), null);
            timer.Tick    += new EventHandler(TimerTick);
            timer.Interval = 33;
            timer.Enabled  = true;
            timer.Start();

            mainWindowMDI.Show();
            splashscreen.BringToFront();
            splashscreen.Close();

            Events.Run();
        }
Beispiel #11
0
        ///// <summary>
        /////
        ///// </summary>
        ///// <param name="buf"></param>
        //public void SetPixels(Int32[] buf)
        //{
        //    Sdl.SDL_LockSurface(this.surfacePtrs[0]);
        //    Marshal.Copy(buf, 0, new IntPtr(this.surface.pixels.ToInt32()), buf.Length);
        //    Sdl.SDL_UnlockSurface(this.surfacePtrs[0]);
        //    /*
        //    try
        //    {
        //        int bytesPerPixel = this.pixelFormat.BytesPerPixel;
        //        //int pixels = this.surface.pixels.ToInt32() + point.X * bytesPerPixel;
        //        int pitch = this.surface.pitch;
        //        if (bytesPerPixel == 4)
        //        {
        //            //the buffer for a row of pixels.
        //            //Int32[] buffer = new Int32[colors.GetLength(0)];
        //            DateTime d = DateTime.Now;
        //            /*for (short x = 0; x < colors.GetLength(0); x++)
        //            {
        //                //gets only the pixels in the row that are required.
        //                for (short y = 0; y < colors.GetLength(1); y++)
        //                {
        //                    //converts the pixel to a color value.
        //                    //buffer[x] = Sdl.SDL_MapRGB(this.surface.format, colors[x, y].R, colors[x, y].G, colors[x, y].B);
        //                    SdlGfx.pixelRGBA(this.surfacePtrs[0], x, y, colors[x, y].R, colors[x, y].G, colors[x, y].B, 255);
        //                }
        //                //then copies them to the image.
        //                //Marshal.Copy(buffer, 0, new IntPtr(pixels + (y + point.Y) * pitch), buffer.Length);

        //            }

        //            Marshal.Copy(buf, 0, new IntPtr(pixels), buf.Length);
        //            Console.WriteLine((DateTime.Now - d).TotalMilliseconds + "ms");
        //        }
        //        else if (bytesPerPixel == 3)
        //        {
        //            //the buffer for a row of pixels.
        //            Int32[] buffer = new Int32[colors.GetLength(0)];
        //            for (int y = 0; y < colors.GetLength(1); ++y)
        //            {
        //                //gets only the pixels in the row that are required.
        //                for (int x = 0; x < buffer.Length; ++x)
        //                {
        //                    //converts the pixel to a color value.
        //                    buffer[x] = Sdl.SDL_MapRGB(this.surface.format, colors[x, y].R, colors[x, y].G, colors[x, y].B);
        //                }
        //                //then copies them to the image.
        //                MarshalHelper.CopyInt24(buffer, 0, new IntPtr(pixels + (y + point.Y) * pitch), buffer.Length);
        //            }
        //        }
        //        else if (bytesPerPixel == 2)
        //        {
        //            //the buffer for a row of pixels.
        //            Int16[] buffer = new Int16[colors.GetLength(0)];
        //            for (int y = 0; y < colors.GetLength(1); ++y)
        //            {
        //                //gets only the pixels in the row that are required.
        //                for (int x = 0; x < buffer.Length; ++x)
        //                {
        //                    //converts the pixel to a color value.
        //                    buffer[x] = (short)Sdl.SDL_MapRGB(this.surface.format, colors[x, y].R, colors[x, y].G, colors[x, y].B);
        //                }
        //                //then copies them to the image.
        //                Marshal.Copy(buffer, 0, new IntPtr(pixels + (y + point.Y) * pitch), buffer.Length);
        //            }
        //        }
        //        else if (bytesPerPixel == 1)
        //        {
        //            //the buffer for a row of pixels.
        //            Byte[] buffer = new Byte[colors.GetLength(0)];
        //            for (int y = 0; y < colors.GetLength(1); ++y)
        //            {
        //                //gets only the pixels in the row that are required.
        //                for (int x = 0; x < buffer.Length; ++x)
        //                {
        //                    //converts the pixel to a color value.
        //                    buffer[x] = (byte)Sdl.SDL_MapRGB(this.surface.format, colors[x, y].R, colors[x, y].G, colors[x, y].B);
        //                }
        //                //then copies them to the image.
        //                Marshal.Copy(buffer, 0, new IntPtr(pixels + (y + point.Y) * pitch), buffer.Length);
        //            }
        //        }
        //    }
        //    finally
        //    {
        //        Sdl.SDL_UnlockSurface(this.surfacePtrs[0]);
        //    }*/
        //}

        /// <summary>
        ///
        /// </summary>
        public void BuildNightImage()
        {
            /*byte[] src = new byte[16],src2 = new byte[16], dst = new byte[16];
             * for (int i = 0; i < 16; i++)
             * {
             *  src[i] = (byte)i;
             *  src2[i] = 4;
             * }
             * SdlGfx.SDL_imageFilterDiv(src, src2, dst, 16);*/


            if (this.pixelFormat.BytesPerPixel < 3)
            {
                this.mask           = Sdl.SDL_CreateRGBSurface(flags, this.surface.w, this.surface.h, bpp, 0, 0, 0, 0);
                surface             = (Sdl.SDL_Surface)Marshal.PtrToStructure(this.mask, typeof(Sdl.SDL_Surface));
                this.surfacePtrs[0] = Sdl.SDL_ConvertSurface(this.surfacePtrs[0], surface.format, flags);
                surface             = (Sdl.SDL_Surface)Marshal.PtrToStructure(this.surfacePtrs[0], typeof(Sdl.SDL_Surface));
                pixelFormat         = (Sdl.SDL_PixelFormat)Marshal.PtrToStructure(surface.format, typeof(Sdl.SDL_PixelFormat));
                Sdl.SDL_FreeSurface(this.mask);
            }

            Sdl.SDL_LockSurface(this.surfacePtrs[0]);
            try
            {
                int bytesPerPixel = this.pixelFormat.BytesPerPixel;
                int pixels        = this.surface.pixels.ToInt32();// +point.X * bytesPerPixel;
                int pitch         = this.surface.pitch;
                //These 3 �if� blocks are a perfect candidate for generics unfortunately C#
                //does not have the required constrains for this case. So I�m reduced to copying code.

                /*if (bytesPerPixel == 4)
                 * {
                 *  //the buffer for a row of pixels.
                 *  Int32[] buffer = new Int32[colors.GetLength(0)];
                 *  for (int y = 0; y < colors.GetLength(1); ++y)
                 *  {
                 *      //gets only the pixels in the row that are required.
                 *      for (int x = 0; x < buffer.Length; ++x)
                 *      {
                 *          //converts the pixel to a color value.
                 *          buffer[x] = GetColorValue(colors[x, y]);
                 *      }
                 *      //then copies them to the image.
                 *      Marshal.Copy(buffer, 0, new IntPtr(pixels + (y + point.Y) * pitch), buffer.Length);
                 *  }
                 * }
                 * else*/
                if (bytesPerPixel >= 3)
                {
                    //the buffer for a row of pixels.
                    Int32[] buffer = new Int32[this.surface.w], original = new Int32[this.surface.w];
                    for (int y = 0; y < this.surface.h; ++y)
                    {
                        if (bytesPerPixel == 3)
                        {
                            MarshalHelper.CopyInt24(new IntPtr(pixels + y * pitch), original, 0, buffer.Length);
                        }
                        else
                        {
                            Marshal.Copy(new IntPtr(pixels + y * pitch), original, 0, buffer.Length);
                        }
                        //gets only the pixels in the row that are required.
                        for (int x = 0; x < buffer.Length; ++x)
                        {
                            //converts the pixel to a color value.
                            Int32 lower24 = original[x] & 0x00FFFFFF;

                            if (lower24 == Sdl.SDL_MapRGB(this.surface.format, 8, 0, 0))
                            {
                                buffer[x] = (Int32)(original[x] & 0xFF000000) | (Int32)Sdl.SDL_MapRGB(this.surface.format, 255, 8, 8);
                            }
                            else
                            if (lower24 == Sdl.SDL_MapRGB(this.surface.format, 0, 8, 0))
                            {
                                buffer[x] = (Int32)(original[x] & 0xFF000000) | (Int32)Sdl.SDL_MapRGB(this.surface.format, 252, 243, 148);
                            }
                            else
                            if (lower24 == Sdl.SDL_MapRGB(this.surface.format, 0, 0, 8))
                            {
                                buffer[x] = (Int32)(original[x] & 0xFF000000) | (Int32)Sdl.SDL_MapRGB(this.surface.format, 255, 227, 99);
                            }
                            else
                            {
                                buffer[x] = (Int32)(original[x] & 0xFF000000) | (Int32)((original[x] & 0x00FCFCFC) >> 2);        // pix /= 4
                            }
                            if (x == 0 && y == 0)
                            {
                                this.colorKey = buffer[x];
                            }
                        }
                        //then copies them to the image.
                        if (bytesPerPixel == 3)
                        {
                            MarshalHelper.CopyInt24(buffer, 0, new IntPtr(pixels + y * pitch), buffer.Length);
                        }
                        else
                        {
                            Marshal.Copy(buffer, 0, new IntPtr(pixels + y * pitch), buffer.Length);
                        }
                    }
                }
                else if (bytesPerPixel == 2)
                {
                    /*//the buffer for a row of pixels.
                     * Int16[] buffer = new Int16[colors.GetLength(0)];
                     * for (int y = 0; y < colors.GetLength(1); ++y)
                     * {
                     *  //gets only the pixels in the row that are required.
                     *  for (int x = 0; x < buffer.Length; ++x)
                     *  {
                     *      //converts the pixel to a color value.
                     *      buffer[x] = (Int16)GetColorValue(colors[x, y]);
                     *  }
                     *  //then copies them to the image.
                     *  Marshal.Copy(buffer, 0, new IntPtr(pixels + (y + point.Y) * pitch), buffer.Length);
                     * }*/
                }
                else if (bytesPerPixel == 1)
                {
                    //the buffer for a row of pixels.
                    byte[] buffer = new byte[this.surface.w];
                    for (int y = 0; y < this.surface.h; ++y)
                    {
                        Marshal.Copy(new IntPtr(pixels + y * pitch), buffer, 0, buffer.Length);
                        //gets only the pixels in the row that are required.
                        for (int x = 0; x < buffer.Length; ++x)
                        {
                            //converts the pixel to a color value.
                            darken16((byte)Sdl.SDL_MapRGB(this.surface.format, 8, 0, 0),
                                     (byte)Sdl.SDL_MapRGB(this.surface.format, 0, 8, 0),
                                     (byte)Sdl.SDL_MapRGB(this.surface.format, 0, 0, 8),
                                     (byte)Sdl.SDL_MapRGB(this.surface.format, 255, 8, 8),
                                     (byte)Sdl.SDL_MapRGB(this.surface.format, 252, 243, 148),
                                     (byte)Sdl.SDL_MapRGB(this.surface.format, 255, 227, 99),
                                     0x33, ref buffer[x]);

                            if (x == 0 && y == 0)
                            {
                                this.colorKey = buffer[x];
                            }
                        }
                        //then copies them to the image.
                        Marshal.Copy(buffer, 0, new IntPtr(pixels + y * pitch), buffer.Length);
                    }
                }
                else
                {
                    //throw new SdlException(Events.StringManager.GetString("UnknownBytesPerPixel", CultureInfo.CurrentUICulture));
                }
            }
            finally
            {
                Sdl.SDL_UnlockSurface(this.surfacePtrs[0]);
            }
        }
Beispiel #12
0
        /// <summary>
        ///
        /// </summary>
        public void Run()
        {
            int  flags    = (Sdl.SDL_HWSURFACE | Sdl.SDL_DOUBLEBUF | Sdl.SDL_ANYFORMAT);
            int  bpp      = 16;
            int  width    = 640;
            int  height   = 480;
            bool quitFlag = false;

            Random rand = new Random();

            string filepath = @"../../";

            if (File.Exists("Data/SdlExamples.Rectangles.sound.ogg"))
            {
                filepath = "";
            }

            string musicFile = filepath + "Data/SdlExamples.Rectangles.sound.ogg";

            Sdl.SDL_Event evt;

            try
            {
                Sdl.SDL_Init(Sdl.SDL_INIT_EVERYTHING);
                Sdl.SDL_WM_SetCaption("Tao.Sdl Example - Rectangles", "");
                IntPtr surfacePtr = Sdl.SDL_SetVideoMode(
                    width,
                    height,
                    bpp,
                    flags);

                int result = SdlMixer.Mix_OpenAudio(
                    SdlMixer.MIX_DEFAULT_FREQUENCY,
                    (short)SdlMixer.MIX_DEFAULT_FORMAT,
                    2,
                    1024);
                IntPtr chunkPtr = SdlMixer.Mix_LoadMUS(musicFile);

                SdlMixer.MusicFinishedDelegate musicStopped =
                    new SdlMixer.MusicFinishedDelegate(this.musicHasStopped);
                SdlMixer.Mix_HookMusicFinished(musicStopped);

                result = SdlMixer.Mix_PlayMusic(chunkPtr, 1);
                if (result == -1)
                {
                    Console.WriteLine("Music Error: " + Sdl.SDL_GetError());
                }

                int rmask = 0x00000000;
                int gmask = 0x00ff0000;
                int bmask = 0x0000ff00;
                int amask = 0x000000ff;

                IntPtr rgbSurfacePtr = Sdl.SDL_CreateRGBSurface(
                    flags,
                    width,
                    height,
                    bpp,
                    rmask,
                    gmask,
                    bmask,
                    amask);

                Sdl.SDL_Rect rect = new Sdl.SDL_Rect(
                    0,
                    0,
                    (short)width,
                    (short)height);
                result = Sdl.SDL_FillRect(rgbSurfacePtr, ref rect, 0);

                Sdl.SDL_Rect rect2;

                IntPtr videoInfoPointer = Sdl.SDL_GetVideoInfo();
                if (videoInfoPointer == IntPtr.Zero)
                {
                    throw new ApplicationException(string.Format("Video query failed: {0}", Sdl.SDL_GetError()));
                }

                Sdl.SDL_VideoInfo videoInfo = (Sdl.SDL_VideoInfo)
                                              Marshal.PtrToStructure(videoInfoPointer,
                                                                     typeof(Sdl.SDL_VideoInfo));
                Console.WriteLine("Hi There");

                Sdl.SDL_PixelFormat pixelFormat = (Sdl.SDL_PixelFormat)
                                                  Marshal.PtrToStructure(videoInfo.vfmt,
                                                                         typeof(Sdl.SDL_PixelFormat));

                Console.WriteLine(videoInfo.hw_available);
                Console.WriteLine(videoInfo.wm_available);
                Console.WriteLine(videoInfo.blit_hw);
                Console.WriteLine(videoInfo.blit_hw_CC);
                Console.WriteLine(videoInfo.blit_hw_A);
                Console.WriteLine(videoInfo.blit_sw);
                Console.WriteLine(videoInfo.blit_hw_CC);
                Console.WriteLine(videoInfo.blit_hw_A);
                Console.WriteLine(videoInfo.blit_fill);
                Console.WriteLine(videoInfo.video_mem);
                Console.WriteLine(pixelFormat.BitsPerPixel);
                Console.WriteLine(pixelFormat.BytesPerPixel);
                Console.WriteLine(pixelFormat.Rmask);
                Console.WriteLine(pixelFormat.Gmask);
                Console.WriteLine(pixelFormat.Bmask);
                Console.WriteLine(pixelFormat.Amask);

                int             numevents = 10;
                Sdl.SDL_Event[] events    = new Sdl.SDL_Event[numevents];
                events[0].type           = Sdl.SDL_KEYDOWN;
                events[0].key.keysym.sym = (int)Sdl.SDLK_p;
                events[1].type           = Sdl.SDL_KEYDOWN;
                events[1].key.keysym.sym = (int)Sdl.SDLK_z;
                int result2 = Sdl.SDL_PeepEvents(events, numevents, Sdl.SDL_ADDEVENT, Sdl.SDL_KEYDOWNMASK);
                Console.WriteLine("Addevent result: " + result2);

                while (quitFlag == false)
                {
                    result = Sdl.SDL_PollEvent(out evt);

                    if (evt.type == Sdl.SDL_QUIT)
                    {
                        quitFlag = true;
                    }
                    else if (evt.type == Sdl.SDL_KEYDOWN)
                    {
                        if ((evt.key.keysym.sym == (int)Sdl.SDLK_ESCAPE) ||
                            (evt.key.keysym.sym == (int)Sdl.SDLK_q))
                        {
                            quitFlag = true;
                        }
                        if (evt.key.keysym.sym == (int)Sdl.SDLK_p)
                        {
                            Console.WriteLine("Key p event was added");
                        }
                        if (evt.key.keysym.sym == (int)Sdl.SDLK_z)
                        {
                            Console.WriteLine("Key z event was added");
                        }
                    }

                    rect2 = new Sdl.SDL_Rect(
                        (short)rand.Next(-300, width),
                        (short)rand.Next(-300, height),
                        (short)rand.Next(20, 300),
                        (short)rand.Next(20, 300));

                    try
                    {
                        result = Sdl.SDL_FillRect(
                            surfacePtr,
                            ref rect2,
                            rand.Next(100000));
                        result = Sdl.SDL_BlitSurface(
                            rgbSurfacePtr,
                            ref rect2,
                            surfacePtr,
                            ref rect);
                        result = Sdl.SDL_Flip(surfacePtr);
                    }
                    catch (Exception) {}
                }
            }
            catch
            {
                Sdl.SDL_Quit();
                throw;
            }
        }
Beispiel #13
0
        /// <summary>
        ///
        /// </summary>
        public void Run()
        {
            //sLogToFile=Environment.NewLine+"//  Comment:  Starting Window.  "+HTMLPage.DateTimePathString(true);
            bool bGood = true;

            retroengine = new RetroEngine(RetroEngine.sDataFolderSlash + "website.html", 800, 600);
            sFuncNow    = "Run()";
            int flags = (Sdl.SDL_HWSURFACE | Sdl.SDL_DOUBLEBUF | Sdl.SDL_ANYFORMAT);

            bGood = UpdateScreenVars(24);           //called again after surface bitdepth is found
            bool   bContinue      = true;
            Random rand           = new Random();
            string sFileTestMusic = RetroEngine.sDataFolderSlash + "music-test.ogg";
            string sFileTestSound = RetroEngine.sDataFolderSlash + "sound-test.wav";

            Sdl.SDL_Event sdleventX;
            try {
                int    iResultInit           = Sdl.SDL_Init(Sdl.SDL_INIT_EVERYTHING);
                IntPtr iptrSurfaceBackbuffer = Sdl.SDL_SetVideoMode(
                    iTargetWidth,
                    iTargetHeight,
                    iTargetBPP,
                    flags);

                //debug NYI: replace with Tao.OpenAl

                int iResult = SdlMixer.Mix_OpenAudio(
                    SdlMixer.MIX_DEFAULT_FREQUENCY,
                    (short)SdlMixer.MIX_DEFAULT_FORMAT,
                    2,
                    1024);

                /*
                 * IntPtr iptrChunkMusic = SdlMixer.Mix_LoadMUS(sFileTestMusic);
                 * IntPtr iptrChunkSound = SdlMixer.Mix_LoadWAV(sFileTestSound);
                 * SdlMixer.MusicFinishedDelegate delMusicFinished=new SdlMixer.MusicFinishedDelegate(this.MusicHasStopped);
                 * SdlMixer.Mix_HookMusicFinished(delMusicFinished);
                 *
                 * iResult = SdlMixer.Mix_PlayMusic (iptrChunkMusic, 2);
                 * if (iResult == -1) {
                 *      sLastErr="Music Error: " + Sdl.SDL_GetError();
                 * }
                 * iResult = SdlMixer.Mix_PlayChannel(1,iptrChunkSound,1);
                 * if (iResult == -1) {
                 *      sLastErr="Sound Error: " + Sdl.SDL_GetError();
                 * }
                 */

                int rmask = 0x00000000;                //doesn't matter since shifted all the way right
                int gmask = 0x00ff0000;
                int bmask = 0x0000ff00;
                int amask = 0x000000ff;

                IntPtr videoInfoPointer = Sdl.SDL_GetVideoInfo();
                if (videoInfoPointer == IntPtr.Zero)
                {
                    throw new ApplicationException(string.Format("Video query failed: {0}", Sdl.SDL_GetError()));
                }

                Sdl.SDL_VideoInfo videoInfo = (Sdl.SDL_VideoInfo)
                                              Marshal.PtrToStructure(videoInfoPointer,
                                                                     typeof(Sdl.SDL_VideoInfo));

                Sdl.SDL_PixelFormat pixelFormat = (Sdl.SDL_PixelFormat)
                                                  Marshal.PtrToStructure(videoInfo.vfmt,
                                                                         typeof(Sdl.SDL_PixelFormat));
                try {
                    UpdateScreenVars(pixelFormat.BitsPerPixel);
                    retroengine.WriteLine("hw_available:" + videoInfo.hw_available);
                    retroengine.WriteLine("wm_available:" + videoInfo.wm_available);
                    retroengine.WriteLine("blit_hw:" + videoInfo.blit_hw);
                    retroengine.WriteLine("blit_hw_CC:" + videoInfo.blit_hw_CC);
                    retroengine.WriteLine("blit_hw_A:" + videoInfo.blit_hw_A);
                    retroengine.WriteLine("blit_sw:" + videoInfo.blit_sw);
                    retroengine.WriteLine("blit_hw_CC:" + videoInfo.blit_hw_CC);
                    retroengine.WriteLine("blit_hw_A:" + videoInfo.blit_hw_A);
                    retroengine.WriteLine("blit_fill:" + videoInfo.blit_fill);
                    retroengine.WriteLine("video_mem:" + videoInfo.video_mem);
                    retroengine.WriteLine("BitsPerPixel:" + pixelFormat.BitsPerPixel);
                    retroengine.WriteLine("BytesPerPixel:" + pixelFormat.BytesPerPixel);
                    retroengine.WriteLine("Rmask:" + pixelFormat.Rmask);
                    retroengine.WriteLine("Gmask:" + pixelFormat.Gmask);
                    retroengine.WriteLine("Bmask:" + pixelFormat.Bmask);
                    retroengine.WriteLine("Amask:" + pixelFormat.Amask);
                }
                catch (Exception exn) {
                    sLastErr = "Exception error displaying screen info--" + e.ToString();
                }
                int             numeventarr = 10;
                Sdl.SDL_Event[] eventarr    = new Sdl.SDL_Event[numeventarr];
                //eventarr[0].type = Sdl.SDL_KEYDOWN;
                //eventarr[0].key.keysym.sym = (int)Sdl.SDLK_p;
                //eventarr[1].type = Sdl.SDL_KEYDOWN;
                //eventarr[1].key.keysym.sym = (int)Sdl.SDLK_z;
                //int iResult2 = Sdl.SDL_PeepEvents(eventarr, numeventarr, Sdl.SDL_ADDEVENT, Sdl.SDL_KEYDOWNMASK);
                //retroengine.WriteLine("Addevent iResult: " + iResult2);
                //TODO: discard clicks if interface is in a transition

                int   iSDLKeys        = 65536;
                int   iNow            = 0;
                int[] iarrAsciiOfSDLK = new int[iSDLKeys];
                try {
                    //debug NYI finish this:
                    iNow = (int)Sdl.SDLK_ASTERISK;
                    if (iNow >= iSDLKeys)
                    {
                        iSDLKeys = iNow + 1;
                    }
                    iarrAsciiOfSDLK[iNow]            = 42;
                    iarrAsciiOfSDLK[Sdl.SDLK_0]      = 48;
                    iarrAsciiOfSDLK[Sdl.SDLK_a]      = 97;
                    iarrAsciiOfSDLK[Sdl.SDLK_RSHIFT] = -32;                  //since a - 32 is A
                    iarrAsciiOfSDLK[Sdl.SDLK_LSHIFT] = -32;                  //since a - 32 is A
                    //debug NYI the rest of the above integers
                    retroengine.TextMessage("This is a long test sentence to test writing text over the edge of the screen.");
                }
                catch (Exception exn) {
                    sLastErr = "Exception while setting iarrAsciiOfSDLK; maximum array size needed was " + iSDLKeys.ToString() + "--" + e.ToString();
                }
                Sdl.SDL_WM_SetCaption(retroengine.sCaption, "default");
                Sdl.SDL_EnableKeyRepeat(0, 9999);
                Sdl.SDL_EnableUNICODE(1);
                #region Main Event Loop
                while (bContinue == true)
                {
                    Sdl.SDL_PumpEvents();
                    iResult = Sdl.SDL_PollEvent(out sdleventX);
                    try {
                        if (sdleventX.type == Sdl.SDL_QUIT)
                        {
                            try {
                                if (true)
                                {
                                    bContinue = false;                                    //TODO: if (retroengine.ConfirmQuit()) bContinue=false;
                                }
                            }
                            catch (Exception exn) {
                                sFuncNow = "Run()";
                                sLastErr = "Exception error trying to confirm, so quitting without confirmation--" + e.ToString();
                            }
                        }
                        else if (sdleventX.type == Sdl.SDL_KEYDOWN)
                        {
                            try {
                                if (sdleventX.key.keysym.sym == (int)Sdl.SDLK_ESCAPE)
                                {
                                    //((sdleventX.key.keysym.sym == (int)Sdl.SDLK_ESCAPE) ||
                                    //(sdleventX.key.keysym.sym == (int)Sdl.SDLK_q))
                                    try {
                                        if (true)
                                        {
                                            bContinue = false;                                             //TODO: if (retroengine.ConfirmQuit()) bContinue=false;
                                        }
                                    }
                                    catch (Exception exn) {
                                        sFuncNow = "Run()";
                                        sLastErr = "Exception error trying to confirm Esc (escape), so quitting without confirmation--" + e.ToString();
                                    }
                                }
                                //else if (sdleventX.key.keysym.sym == (int)Sdl.SDLK_p) {
                                //	retroengine.WriteLine("Key p event was added");
                                //}
                                //else if (sdleventX.key.keysym.sym == (int)Sdl.SDLK_z) {
                                //	retroengine.WriteLine("Key z event was added");
                                //}
                                if ((int)sdleventX.key.keysym.sym != 0)
                                {
                                    retroengine.KeyEvent(sdleventX.key.keysym.sym, sdleventX.key.keysym.unicode, true);
                                    //string sNow=Char.ToString((char)sdleventX.key.keysym.unicode);
                                    //short shNow=sdleventX.key.keysym.unicode;
                                    //retroengine.WriteLine("key="+shNow.ToString()+"    ");
                                }
                            }
                            catch (Exception exn) {
                                sLastErr = "Exception during KEYDOWN processing--" + e.ToString();
                            }
                        }
                        else if (sdleventX.type == Sdl.SDL_KEYUP)
                        {
                            //Sdl.SDL_
                            //byarrKeysNow=Sdl.SDL_GetKeyState(256); //debug internationalization
                            //ushort wKey=Base.GetUnsignedLossless(sdleventX.key.keysym.scancode);
                            //sdleventX.key.type
                            retroengine.KeyEvent(sdleventX.key.keysym.sym, 0, false);
                            //retroengine.WriteLine("keyup="+sdleventX.key.keysym.sym.ToString()+"    ");
                        }
                        else if (sdleventX.type == Sdl.SDL_MOUSEMOTION)
                        {
                            retroengine.WriteLine("mousemove=(" + sdleventX.motion.x + "," + sdleventX.motion.y + ")");
                        }
                        else if (sdleventX.type == Sdl.SDL_MOUSEBUTTONDOWN)
                        {
                            retroengine.WriteLine("mousedown=(" + sdleventX.button.x + "," + sdleventX.motion.y + ")");
                        }
                        else if (sdleventX.type == Sdl.SDL_MOUSEBUTTONUP)
                        {
                            retroengine.WriteLine("mouseup=(" + sdleventX.button.x + "," + sdleventX.motion.y + ")");
                        }
                    }
                    catch (Exception exn) {
                        sLastErr = "Exception during controller input processing--" + e.ToString();
                    }

                    try {
                        //if (iTryNow<iTargetBytesTotal){
                        //	byarrTemp[iTryNow]=255;
                        //	iTryNow++;
                        //}
                        //if (iTryNow%30!=0) continue;

                        //debug performance: run Draw code below as separate thread.
                        bGood   = retroengine.DrawFrame();
                        iResult = Sdl.SDL_LockSurface(iptrSurfaceBackbuffer);
                        if (iTargetBPP == 32)
                        {
                            fixed(byte *lpSrc = retroengine.gbScreen.byarrData)
                            {
                                byte *lpSrcNow = lpSrc;

                                Sdl.SDL_Surface *lpsurface = (Sdl.SDL_Surface *)iptrSurfaceBackbuffer;
                                byte *           lpDestNow = (byte *)lpsurface->pixels;

                                for (int i = iTargetChunks64Total; i != 0; i--)
                                {
                                    *((UInt64 *)lpDestNow) = *((UInt64 *)lpSrcNow);
                                    lpDestNow += 8;
                                    lpSrcNow  += 8;
                                }
                            }
                        }
                        else if (iTargetBPP == 24)
                        {
                            fixed(byte *lpSrc = retroengine.gbScreen.byarrData)
                            {
                                byte *lpSrcNow = lpSrc;

                                Sdl.SDL_Surface *lpsurface = (Sdl.SDL_Surface *)iptrSurfaceBackbuffer;
                                byte *           lpDestNow = (byte *)lpsurface->pixels;

                                for (int i = (iTargetBytesTotal / 3) - 1; i != 0; i--)                          //-1 to avoid overwrite problems
                                {
                                    *((UInt32 *)lpDestNow) = *((UInt32 *)lpSrcNow);
                                    lpDestNow += 3;
                                    lpSrcNow  += 4;
                                }
                                //now copy the odd pixel:
                                *lpDestNow = *lpSrcNow;
                                lpDestNow++;
                                lpSrcNow++;
                                *lpDestNow = *lpSrcNow;
                                lpDestNow++;
                                lpSrcNow++;
                                *lpDestNow = *lpSrcNow;
                            }
                        }
                        else if (iTargetBPP < 24)
                        {
                            //NYI add other bit depths.
                            sLastErr  = "Wrong bit depth.";
                            bContinue = false;
                        }
                        iResult = Sdl.SDL_UnlockSurface(iptrSurfaceBackbuffer);
                        iResult = Sdl.SDL_Flip(iptrSurfaceBackbuffer);
                    }
                    catch (Exception) {
                        sLastErr = "Exception error copying buffer to screen";
                    }
                }                //end while bContinue
                #endregion Main Event Loop

                if (iTargetBPP < 24)
                {
                    MessageBox.Show("You must change your Display settings to 32-bit (recommended) or at least 24-bit to run this program.");
                }
            }
            catch (Exception exn) {
                sFuncNow = "Run()";
                sLastErr = "Exception error--" + e.ToString();
                //Sdl.SDL_Quit();
            }
            try {
                if (true)                  //TODO: if (RetroEngine.statusq.TrackedErrors>0) {
                {
                    RetroEngine.statusq.Enq(".statusq {"
                                            + RetroEngine.statusq.DumpTrackedErrorsToStyleNotation()
                                            + "}"
                                            );
                }
            }
            catch (Exception exn) {
                sFuncNow = "Run, after main event loop";
                sLastErr = "Exception error saving exception stats--" + e.ToString();
            }
        }