Esempio n. 1
0
        public SDL2GameView(string title, int width, int height, bool fullscreen = false, bool vsync = true,
                            int x = SDL.SDL_WINDOWPOS_CENTERED, int y = SDL.SDL_WINDOWPOS_CENTERED)
        {
            _refCount++;
            _width  = width;
            _height = height;

            _frameArgs = new FrameArgs();
            _frameArgs.Enqueue(new Start(new Vector2(_width, _height), 1.0f));
            _frameArgs.Enqueue(new Resize(new Vector2(_width, _height), 1.0f));
            _sdlEvents = new ConcurrentQueue <SDL.SDL_Event>();

            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 3);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 1);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_RED_SIZE, 8);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_GREEN_SIZE, 8);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_BLUE_SIZE, 8);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DEPTH_SIZE, 24);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_MULTISAMPLEBUFFERS, 1);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_MULTISAMPLESAMPLES, 4);
            if (vsync)
            {
                SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DOUBLEBUFFER, 1);
            }

            _window = SDL.SDL_CreateWindow(
                title,
                x, y,
                width, height,
                SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL
                | (fullscreen ? SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN : 0)
                );
            if (_window == IntPtr.Zero)
            {
                throw new SDL2Exception();
            }

            if (_alContext == null)
            {
                _alContext = new AudioContext();
                _alContext.MakeCurrent();
            }

            if (vsync)
            {
                SDL.SDL_GL_SetSwapInterval(1);
            }
            _windowId = SDL.SDL_GetWindowID(_window);
        }
Esempio n. 2
0
		public SDL2GameView (string title, int width, int height, bool fullscreen = false, bool vsync = true, 
		                     int x = SDL.SDL_WINDOWPOS_CENTERED, int y = SDL.SDL_WINDOWPOS_CENTERED) {
			_refCount++;
			_width = width;
			_height = height;

			_frameArgs = new FrameArgs();
			_frameArgs.Enqueue(new Start(new Vector2(_width, _height), 1.0f));
			_frameArgs.Enqueue(new Resize(new Vector2(_width, _height), 1.0f));
			_sdlEvents = new ConcurrentQueue<SDL.SDL_Event>();

			SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 2);
			SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 1);
			SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
			SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_RED_SIZE, 8);
			SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_GREEN_SIZE, 8);
			SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_BLUE_SIZE, 8);
			SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DEPTH_SIZE, 24);
			SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_MULTISAMPLEBUFFERS, 1);
			SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_MULTISAMPLESAMPLES, 4);

			_window = SDL.SDL_CreateWindow(
				title,
				x, y,
				width, height,
				SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL
				| (fullscreen ? SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN : 0)
			);
			if (_window == IntPtr.Zero)
				throw new SDL2Exception();

			if (_alContext == null) {
				_alContext = new AudioContext();
				_alContext.MakeCurrent();
			}

			if(vsync)
				SDL.SDL_GL_SetSwapInterval(1);
			_windowId = SDL.SDL_GetWindowID(_window);
		}
Esempio n. 3
0
        void OnUpdate()
        {
            bool lowMem = false;

            while (_events.Count > 0)
            {
                EventBase e;
                _events.TryDequeue(out e);
                _frame.Enqueue(e);

                if (e is Pause)
                {
                    DoUpdate();
                    _link.RemoveFromRunLoop(NSRunLoop.Current, NSRunLoop.NSDefaultRunLoopMode);
                    _link.Dispose();
                    return;
                }
                else if (e is LowMemory)
                {
                    lowMem = true;
                }
                else if (e is Resize)
                {
                    this.InvokeOnMainThread(() => {
                        EAGLContext.SetCurrentContext(_glContext);
                        this.Initialize(false);
                    });
                }
            }

            DoUpdate();
            _frame.ClearEvents();

            if (lowMem)
            {
                _lowMemHandled = true;
            }
        }
Esempio n. 4
0
        void IRenderer.OnDrawFrame(Javax.Microedition.Khronos.Opengles.IGL10 gl)
        {
            EventBase e;

            if (this.IsPaused)
            {
                while (_queue.TryPeek(out e))
                {
                    if (e is Resume)
                    {
                        this.IsPaused = false;
                        return;
                    }
                    else
                    {
                        _queue.TryDequeue(out e);
                    }
                }
                Thread.Sleep(_minFrameTicks);
                return;
            }

            var ticks     = Environment.TickCount;
            var diffTicks = ticks - _lastTicks;

            if (diffTicks <= 0)
            {
                return;
            }

            if (diffTicks < _minFrameTicks)
            {
                Thread.Sleep(_minFrameTicks - diffTicks);
                ticks     = Environment.TickCount;
                diffTicks = ticks - _lastTicks;
            }

            _lastTicks = ticks;

            _event.Time      = (double)ticks / 1000.0;
            _event.DeltaTime = (float)diffTicks / 1000f;

            if (_loadFrame > 0)
            {
                _event.DeltaTime = 1f / 60f;
                _loadFrame--;
            }

            while (_queue.TryDequeue(out e))
            {
                _event.Enqueue(e);
                if (e is Pause)
                {
                    this.IsPaused = true;
                }
            }

            ThreadContext.Current.GLContext = this.Handle;

            if (this.Update != null)
            {
                this.Update(this, _event);
            }
            if (this.Render != null)
            {
                this.Render(this, _event);
            }

            _event.ClearEvents();
        }
Esempio n. 5
0
        void ProcessEvent(SDL.SDL_Event e)
        {
            if (Event != null)
            {
                Event(this, new SDL2EventArgs(e));
            }

            switch (e.type)
            {
            case SDL.SDL_EventType.SDL_QUIT:
                this.CloseWindow();
                break;

            case SDL.SDL_EventType.SDL_WINDOWEVENT:
                if (e.window.windowID == _windowId && e.window.windowEvent == SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE)
                {
                    this.CloseWindow();
                }
                break;

            case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN:
            case SDL.SDL_EventType.SDL_MOUSEBUTTONUP:
                if (e.button.windowID != _windowId)
                {
                    return;
                }
                if (e.button.which != SDL.SDL_TOUCH_MOUSEID && e.button.button == SDL.SDL_BUTTON_LEFT)
                {
                    _frameArgs.Enqueue(new Touch(
                                           e.type == SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN ? TouchState.Start : TouchState.End,
                                           NormalizeToViewport(e.button.x, e.button.y),
                                           new Vector2(e.button.x, _height - e.button.y),
                                           0,
                                           true
                                           ));
                    _mouseDrag = e.type == SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN;
                }
                break;

            case SDL.SDL_EventType.SDL_MOUSEMOTION:
                if (e.motion.windowID != _windowId)
                {
                    return;
                }
                if (e.motion.which != SDL.SDL_TOUCH_MOUSEID && _mouseDrag)
                {
                    _frameArgs.Enqueue(new Touch(
                                           TouchState.Move,
                                           NormalizeToViewport(e.button.x, e.button.y),
                                           new Vector2(e.button.x, _height - e.button.y),
                                           0,
                                           true
                                           ));
                }
                break;

            case SDL.SDL_EventType.SDL_KEYDOWN:
            case SDL.SDL_EventType.SDL_KEYUP:
                if (e.key.windowID != _windowId)
                {
                    return;
                }
                var sym = (int)e.key.keysym.sym;
                _frameArgs.Enqueue(new KeyEvent(
                                       e.key.repeat > 0 ? KeyState.Repeat : (e.key.type == SDL.SDL_EventType.SDL_KEYUP ? KeyState.Down : KeyState.Up),
                                       (KeyModifier)e.key.keysym.mod,
                                       (int)e.key.keysym.scancode,
                                       (char)(sym <= 255 ? sym : 0)
                                       ));
                break;

            case SDL.SDL_EventType.SDL_FINGERUP:
            case SDL.SDL_EventType.SDL_FINGERDOWN:
            case SDL.SDL_EventType.SDL_FINGERMOTION:
                _frameArgs.Enqueue(new Touch(
                                       e.type == SDL.SDL_EventType.SDL_FINGERDOWN ? TouchState.Start : (e.type == SDL.SDL_EventType.SDL_FINGERUP ? TouchState.End : TouchState.Move),
                                       NormalizeToViewport((int)e.tfinger.x, (int)e.tfinger.y),
                                       new Vector2(e.tfinger.x, _height - e.tfinger.y),
                                       e.tfinger.fingerId
                                       ));
                break;
            }
        }