Пример #1
0
        public SdlRenderer()
        {
            SDL.Init(InitFlags.Video);

            _window        = SDL.CreateWindow(".NET Core SDL2-CS Tutorial", SDL.WINDOWPOS_CENTERED, SDL.WINDOWPOS_CENTERED, 1024, 768, WindowFlags.Resizable);
            _renderer      = SDL.CreateRenderer(_window, -1, RendererFlags.Accelerated);
            this.blockSize = 20;
        }
Пример #2
0
        /// <summary>
        /// Creates a new window and attaches it to the current context.
        /// </summary>
        /// <param name="caption">The window's caption text.</param>
        /// <param name="x">The x-coordinate at which to position the window's top-left corner.</param>
        /// <param name="y">The y-coordinate at which to position the window's top-left corner.</param>
        /// <param name="width">The width of the window's client area in pixels.</param>
        /// <param name="height">The height of the window's client area in pixels.</param>
        /// <param name="flags">A set of WindowFlags values indicating how to create the window.</param>
        /// <returns>The Ultraviolet window that was created.</returns>
        public IUltravioletWindow Create(String caption, Int32 x, Int32 y, Int32 width, Int32 height, WindowFlags flags = WindowFlags.None)
        {
            var sdlflags = SDL_WindowFlags.OPENGL;

            if (Ultraviolet.SupportsHighDensityDisplayModes)
            {
                sdlflags |= SDL_WindowFlags.ALLOW_HIGHDPI;
            }

            if ((flags & WindowFlags.Resizable) == WindowFlags.Resizable)
            {
                sdlflags |= SDL_WindowFlags.RESIZABLE;
            }

            if ((flags & WindowFlags.Borderless) == WindowFlags.Borderless)
            {
                sdlflags |= SDL_WindowFlags.BORDERLESS;
            }

            if ((flags & WindowFlags.Hidden) == WindowFlags.Hidden)
            {
                sdlflags |= SDL_WindowFlags.HIDDEN;
            }
            else
            {
                sdlflags |= SDL_WindowFlags.SHOWN;
            }

            var sdlptr = SDL.CreateWindow(caption ?? String.Empty, x, y, width, height, sdlflags);

            if (sdlptr == IntPtr.Zero)
            {
                throw new SDL2Exception();
            }

            var win = new OpenGLUltravioletWindow(Ultraviolet, sdlptr);

            windows.Add(win);

            Ultraviolet.Messages.Subscribe(win, SDL2UltravioletMessages.SDLEvent);

            OnWindowCreated(win);

            return(win);
        }
Пример #3
0
        public override void Init(string title, int width, int height)
        {
            //Initialize SDL
            var initFlags = SubSystem.Video | SubSystem.Timer | SubSystem.Events | SubSystem.Joystick | SubSystem.Haptic;

            if (SDL.Init(initFlags) != 0)
            {
                SDL.Quit();
                throw new Exception(SDL.GetError());
            }

            //OpenGL attributes
            SDL.GLSetAttribute(GLAttr.ContextMajorVersion, 3);
            SDL.GLSetAttribute(GLAttr.ContextMinorVersion, 3);
            SDL.GLSetAttribute(GLAttr.ContextProfileMask, (int)GLContextProfile.Core);
            SDL.GLSetAttribute(GLAttr.ContextFlags, (int)GLContextFlag.ForwardCompatible);
            SDL.GLSetAttribute(GLAttr.DoubleBuffer, 1);
            //SDL.GLSetAttribute(GLAttr.MultisampleBuffers, 1);
            //SDL.GLSetAttribute(GLAttr.MultisampleSamples, 3);

            //Create the window
            var windowFlags = WindowFlags.Shown | WindowFlags.OpenGL | WindowFlags.AllowHighDpi;// | WindowFlags.Resizable;

            window = SDL.CreateWindow(title, SDL.WindowPosCentered, SDL.WindowPosCentered, width, height, windowFlags);
            if (!window)
            {
                SDL.Quit();
                throw new Exception(SDL.GetError());
            }
            windowID = SDL.GetWindowID(window);

            //Create the OpenGL context
            context = SDL.GLCreateContext(window);
            if (!context)
            {
                SDL.Quit();
                throw new Exception(SDL.GetError());
            }
            SDL.GLMakeCurrent(window, context);
            SetVSync(true);
        }
Пример #4
0
        public Sdl2NativeWindow(int x, int y, int width, int height,
                                string title, GameWindowFlags options, DisplayDevice device)
        {
            lock (sync)
            {
                SDL.DisableScreenSaver();

                var bounds = device.Bounds;
                var flags  = TranslateFlags(options);
                flags |= WindowFlags.OPENGL;
                flags |= WindowFlags.HIDDEN;
                if (Toolkit.Options.EnableHighResolution)
                {
                    flags |= WindowFlags.ALLOW_HIGHDPI;
                }

                if ((flags & WindowFlags.FULLSCREEN_DESKTOP) != 0 ||
                    (flags & WindowFlags.FULLSCREEN) != 0)
                {
                    window_state = WindowState.Fullscreen;
                }

                if ((flags & WindowFlags.RESIZABLE) == 0)
                {
                    window_border = WindowBorder.Fixed;
                }

                IntPtr handle;
                lock (SDL.Sync)
                {
                    handle = SDL.CreateWindow(title, bounds.Left + x, bounds.Top + y, width, height, flags);
                    exists = true;
                }
                ProcessEvents();
                window    = new Sdl2WindowInfo(handle, null);
                window_id = SDL.GetWindowID(handle);
                windows.Add(window_id, this);
            }
        }
Пример #5
0
        public Window(string title, int left, int top, int width, int height, SDL.SDL_WindowFlags flags, bool createRenderer = false)
        {
            this.Title  = title;
            this.Left   = left;
            this.Top    = top;
            this.Width  = width;
            this.Height = height;
            this.Flags  = flags;
            this.IntPtr = SDL.CreateWindow(title, left, top, width, height, flags);
            if (createRenderer)
            {
                //IntPtr windowIntPtr;// = new IntPtr();
                //IntPtr rendrerIntPtr;// = new IntPtr();

                //var rv=SDL.CreateWindowAndRenderer(width, height, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN, out windowIntPtr, out rendrerIntPtr);
                //if (Convert.ToBoolean(windowIntPtr.ToInt64()) && Convert.ToBoolean(rendrerIntPtr.ToInt64()))
                //{
                //this.Renderer = new Renderer() { IntPtr = rendrerIntPtr };//
                this.Renderer = (Renderer)SDL.CreateRenderer(this.IntPtr, 0, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED);
                //}
            }
        }
Пример #6
0
        /// <summary>
        /// Sets up a window and enters the gameloop. (Code after this call won't run until the game has exited.)
        /// </summary>
        public static void Run(float speed = 100f)
        {
            SDL.Init(SDL.InitFlags.EVERYTHING);
            IMG.Init(IMG.InitFlags.EVERYTHING);
            Mix.Init(Mix.InitFlags.EVERYTHING);
            TTF.Init();

            //open window
            SDL.SetHint(SDL.HINT_RENDER_VSYNC, "1");
            SDL.SetHint(SDL.HINT_RENDER_SCALE_QUALITY, "2");

            WindowHandle   = SDL.CreateWindow("HatlessEngine", SDL.WINDOWPOS_UNDEFINED, SDL.WINDOWPOS_UNDEFINED, 800, 600, SDL.WindowFlags.WINDOW_SHOWN | SDL.WindowFlags.WINDOW_RESIZABLE | SDL.WindowFlags.WINDOW_INPUT_FOCUS);
            RendererHandle = SDL.CreateRenderer(WindowHandle, -1, (uint)(SDL.RendererFlags.RENDERER_ACCELERATED | SDL.RendererFlags.RENDERER_PRESENTVSYNC));
            SDL.SetRenderDrawBlendMode(RendererHandle, SDL.BlendMode.BLENDMODE_BLEND);

            Window.SetIcon();

            //add default view that spans the current window
            new View("default", new Rectangle(Point.Zero, Window.Size), new Rectangle(Point.Zero, new Point(1f, 1f)));

            //initialize audio system and let Resources handle sound expiration
            Mix.OpenAudio(44100, SDL.AUDIO_S16SYS, 2, 4096);
            Mix.AllocateChannels((int)(speed * 2));             //might want to dynamically create and remove channels during runtime
            Mix.ChannelFinished(new Mix.ChannelFinishedDelegate(Resources.SoundChannelFinished));
            Mix.HookMusicFinished(new Mix.MusicFinishedDelegate(Resources.MusicFinished));

            //initialize loop
            StepsPerSecond = speed;

            Running = true;

            if (Started != null)
            {
                Started(null, EventArgs.Empty);
            }

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            //do a step before the first draw can occur
            Step();

            long lastStepTick = 0;
            long lastDrawTick = 0;
            long lastStepTime = 0;
            long lastDrawTime = 0;

            while (Running)
            {
                //perform step when needed
                if (stopWatch.ElapsedTicks >= lastStepTick + TicksPerStep)
                {
                    if (CatchUpSteps)
                    {
                        lastStepTick = lastStepTick + TicksPerStep;
                    }
                    else
                    {
                        lastStepTick = stopWatch.ElapsedTicks;
                    }
                    Step();

                    _ActualSPS   = (int)(Stopwatch.Frequency / (stopWatch.ElapsedTicks - lastStepTime));
                    lastStepTime = stopWatch.ElapsedTicks;
                }

                //perform draw when ready for a new one
                if (!RenderframeReady && Running && stopWatch.ElapsedTicks >= lastDrawTick + TicksPerDraw)
                {
                    lastDrawTick = lastDrawTick + TicksPerDraw;
                    Draw();

                    _ActualFPS   = (int)(Stopwatch.Frequency / (stopWatch.ElapsedTicks - lastDrawTime));
                    lastDrawTime = stopWatch.ElapsedTicks;
                }
            }

            //cleanup and uninitialization
            Resources.UnloadAllExternalResources();
            Log.CloseAllStreams();
            Input.CloseGamepads();

            SDL.DestroyWindow(WindowHandle);
            WindowHandle = IntPtr.Zero;
            SDL.DestroyRenderer(RendererHandle);
            RendererHandle = IntPtr.Zero;

            Mix.CloseAudio();

            SDL.Quit();
            IMG.Quit();
            Mix.Quit();
            TTF.Quit();
        }
        /// <summary>
        /// Initializes the context's primary window.
        /// </summary>
        /// <param name="configuration">The Ultraviolet Framg</param>
        private void InitializePrimaryWindow(OpenGLUltravioletConfiguration configuration)
        {
            // Retrieve the caption for our window.
            var caption = Localization.Strings.Contains("WINDOW_CAPTION") ?
                          Localization.Get("WINDOW_CAPTION") : UltravioletStrings.DefaultWindowCaption.Value;

            // Set the OpenGL attributes for the window we're about to create.
            if (SDL.GL_SetAttribute(SDL_GLattr.MULTISAMPLEBUFFERS, configuration.MultiSampleBuffers) < 0)
            {
                throw new SDL2Exception();
            }

            if (SDL.GL_SetAttribute(SDL_GLattr.MULTISAMPLESAMPLES, configuration.MultiSampleSamples) < 0)
            {
                throw new SDL2Exception();
            }

            // If we're running on Android, we can't create a headless context.
            var isRunningOnAndroid = (Ultraviolet.Platform == UltravioletPlatform.Android);

            if (isRunningOnAndroid && configuration.Headless)
            {
                throw new InvalidOperationException(OpenGLStrings.CannotCreateHeadlessContextOnAndroid);
            }

            // Initialize the hidden master window used to create the OpenGL context.
            var masterWidth  = 0;
            var masterHeight = 0;
            var masterFlags  = SDL_WindowFlags.OPENGL;

            if (isRunningOnAndroid)
            {
                masterFlags |= SDL_WindowFlags.FULLSCREEN | SDL_WindowFlags.RESIZABLE;
            }
            else
            {
                masterFlags |= SDL_WindowFlags.HIDDEN;
            }

            // Attempt to create the OpenGL window. If that fails, reduce our requirements and try again before failing.
            var masterptr = SDL.CreateWindow(isRunningOnAndroid ? caption : String.Empty, 0, 0, masterWidth, masterHeight, masterFlags);

            if (masterptr == IntPtr.Zero)
            {
                if (SDL.GL_SetAttribute(SDL_GLattr.MULTISAMPLEBUFFERS, 0) < 0)
                {
                    throw new SDL2Exception();
                }

                if (SDL.GL_SetAttribute(SDL_GLattr.MULTISAMPLESAMPLES, 0) < 0)
                {
                    throw new SDL2Exception();
                }

                masterptr = SDL.CreateWindow(isRunningOnAndroid ? caption : String.Empty, 0, 0, masterWidth, masterHeight, masterFlags);
                if (masterptr == IntPtr.Zero)
                {
                    throw new SDL2Exception();
                }
            }

            this.master = new OpenGLUltravioletWindow(Ultraviolet, masterptr);

            // Set SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT so that enlisted windows
            // will be OpenGL-enabled and set to the correct pixel format.
            if (!SDL.SetHint(SDL_Hint.VIDEO_WINDOW_SHARE_PIXEL_FORMAT, masterptr.ToStringHex()))
            {
                throw new SDL2Exception();
            }

            // If this is not a headless context, create the primary application window.
            if (!configuration.Headless)
            {
                if (isRunningOnAndroid)
                {
                    this.windows.Add(this.master);
                    DesignatePrimary(this.master);
                }
                else
                {
                    var flags = configuration.WindowIsVisible ? WindowFlags.None : WindowFlags.Hidden;

                    if (configuration.WindowIsResizable)
                    {
                        flags |= WindowFlags.Resizable;
                    }

                    if (configuration.WindowIsBorderless)
                    {
                        flags |= WindowFlags.Borderless;
                    }

                    var primary = Create(caption,
                                         configuration.InitialWindowPosition.X,
                                         configuration.InitialWindowPosition.Y,
                                         configuration.InitialWindowPosition.Width,
                                         configuration.InitialWindowPosition.Height, flags);
                    DesignatePrimary(primary);
                }
            }
        }
Пример #8
0
 public Window(string title, int x, int y, int w, int h)
 {
     ThrowIfFailed(SDL.InitSubSystem(SDL_InitFlags.SDL_INIT_VIDEO));
     WindowPtr = SDL.CreateWindow(title, x, y, w, h, 0);
     ThrowIfFailed(WindowPtr);
 }