Ejemplo n.º 1
0
 public static void Main()
 {
     SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
     Instance = new PulsarMainWindow();
     Instance.Run();
     Instance.Dispose();
 }
Ejemplo n.º 2
0
        //////// OPTIONAL ////////

        // Processs any SDL2 events manually if required.
        // Return false to not allow the default event handler to process it.
        public bool MyEventHandler(SDL2Window _self, SDL.SDL_Event e)
        {
            // We're replacing OnEvent and thus call ImGuiSDL2CSHelper.OnEvent manually.
            if (!ImGuiSDL2CSHelper.HandleEvent(e, ref g_MouseWheel, g_MousePressed))
            {
                return(false);
            }

            // Any custom event handling can happen here.

            return(true);
        }
Ejemplo n.º 3
0
        public SDL2GamepadCollection(SDL2Window window)
        {
            m_window   = window;
            m_gamepads = new List <SDL2Gamepad>();

            int numJoysticks = SDL.SDL_NumJoysticks();

            for (int joystickIndex = 0; joystickIndex < numJoysticks; ++joystickIndex)
            {
                if (SDL.SDL_IsGameController(joystickIndex) == SDL.SDL_bool.SDL_TRUE)
                {
                    m_gamepads.Add(new SDL2Gamepad(window, joystickIndex));
                }
            }
        }
Ejemplo n.º 4
0
        // Any custom game loop should end up here.
        // Setting the window.IsActive = false stops the loop.
        public void MyGameLoop(SDL2Window _self)
        {
            // This is the default implementation, except for the ClearColor not being 0.1f, 0.125f, 0.15f, 1f

            // Using minimal ImGuiSDL2CS.GL to provide access to OpenGL methods.
            // Alternatively, use SDL_GL_GetProcAddress on your own.
            GL.ClearColor(clear_color.X, clear_color.Y, clear_color.Z, 1f);
            GL.Clear(GL.Enum.GL_COLOR_BUFFER_BIT);

            // This calls ImGuiSDL2CSHelper.NewFrame, the overridden ImGuiLayout, ImGuiSDL2CSHelper.Render and renders it.
            // ImGuiSDL2CSHelper.NewFrame properly sets up ImGuiIO and ImGuiSDL2CSHelper.Render renders the draw data.
            ImGuiRender();

            // Finally, swap.
            Swap();
        }
Ejemplo n.º 5
0
        public SDL2Mouse(SDL2Window window)
        {
            m_window          = window;
            m_buttons         = new Dictionary <MouseButton, IButton>();
            m_buttonsReadOnly = m_buttons.ToReadOnly();
            foreach (MouseButton button in Enum.GetValues(typeof(MouseButton)))
            {
                if (button != MouseButton.None)
                {
                    m_buttons.Add(button, new SimpleButton());
                }
            }

            m_hadMouseFocus = false;
            Update();
        }
Ejemplo n.º 6
0
 public SDL2Keyboard(SDL2Window window)
 {
     m_window       = window;
     m_keys         = new Dictionary <Key, IButton>();
     m_keysReadOnly = m_keys.ToReadOnly();
     m_text         = "";
     m_pendingText  = new StringBuilder();
     foreach (Key key in Enum.GetValues(typeof(Key)))
     {
         if (key != Key.None)
         {
             m_keys.Add(key, new SimpleButton());
         }
     }
     m_hadFocus = false;
     Update();
 }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            Queue <string> argq = new Queue <string>(args);

            while (argq.Count > 0)
            {
                string arg = argq.Dequeue();
                if (arg == "--debug")
                {
                    Debugger.Launch();
                }
            }

            /*ImGuiSDL2CSWindow*/ Instance = new YourGameNamespace.YourGameWindow();
            Instance.Run();
            Instance.Dispose();
        }
        public SteamworksSteamControllerCollection(SDL2Window window, string[] actionSetNames, string[] digitalActionNames, string[] analogActionNames, string[] analogTriggerActionNames)
        {
            m_window = window;

            m_controllerHandles    = new ControllerHandle_t[Constants.STEAM_CONTROLLER_MAX_COUNT];
            m_numControllerHandles = 0;

            m_controllers = new List <SteamworksSteamController>();
            App.CheckSteamworksResult("SteamController::Init", SteamController.Init());

            m_actionSetHandles = new Dictionary <string, ControllerActionSetHandle_t>();
            for (int i = 0; i < actionSetNames.Length; ++i)
            {
                var name = actionSetNames[i];
                m_actionSetHandles.Add(name, SteamController.GetActionSetHandle(name));
            }

            m_digitalActionHandles = new Dictionary <string, ControllerDigitalActionHandle_t>();
            for (int i = 0; i < digitalActionNames.Length; ++i)
            {
                var name = digitalActionNames[i];
                m_digitalActionHandles.Add(name, SteamController.GetDigitalActionHandle(name));
            }

            m_analogActionHandles = new Dictionary <string, ControllerAnalogActionHandle_t>();
            m_analogActionNames   = analogActionNames;
            for (int i = 0; i < m_analogActionNames.Length; ++i)
            {
                var name = m_analogActionNames[i];
                m_analogActionHandles.Add(name, SteamController.GetAnalogActionHandle(name));
            }
            m_analogTriggerActionNames = analogTriggerActionNames;
            for (int i = 0; i < m_analogTriggerActionNames.Length; ++i)
            {
                var name = m_analogTriggerActionNames[i];
                m_analogActionHandles.Add(name, SteamController.GetAnalogActionHandle(name));
            }

            DetectControllers();
        }
Ejemplo n.º 9
0
        public SDL2Gamepad(SDL2Window window, int joystickIndex)
        {
            m_window         = window;
            m_joystickIndex  = joystickIndex;
            m_gameController = SDL.SDL_GameControllerOpen(m_joystickIndex);
            m_joystick       = SDL.SDL_GameControllerGetJoystick(m_gameController);
            m_instanceID     = SDL.SDL_JoystickInstanceID(m_joystick);
            m_name           = SDL.SDL_GameControllerName(m_gameController);
            m_joystickName   = SDL.SDL_JoystickName(m_joystick);
            DetectType();

            // Lets get ready to rumple
            m_haptic = SDL.SDL_HapticOpenFromJoystick(m_joystick);
            if (m_haptic != IntPtr.Zero)
            {
                if (SDL.SDL_HapticRumbleSupported(m_haptic) == (int)SDL.SDL_bool.SDL_FALSE ||
                    SDL.SDL_HapticRumbleInit(m_haptic) < 0)
                {
                    SDL.SDL_HapticClose(m_haptic);
                    m_haptic = IntPtr.Zero;
                }
            }

            // Axes
            m_axes         = new Dictionary <GamepadAxis, IAxis>();
            m_axesReadOnly = m_axes.ToReadOnly();
            foreach (GamepadAxis axis in Enum.GetValues(typeof(GamepadAxis)))
            {
                if (axis != GamepadAxis.None)
                {
                    var simpleAxis = new SimpleAxis();
                    switch (axis)
                    {
                    case GamepadAxis.LeftStickX:
                    case GamepadAxis.LeftStickY:
                    {
                        simpleAxis.DeadZone = 0.239f;         // Derived from XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE
                        break;
                    }

                    case GamepadAxis.RightStickX:
                    case GamepadAxis.RightStickY:
                    {
                        simpleAxis.DeadZone = 0.265f;         // Derived from XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE
                        break;
                    }

                    case GamepadAxis.LeftTrigger:
                    case GamepadAxis.RightTrigger:
                    {
                        simpleAxis.DeadZone = 0.117f;         // Derived from XINPUT_GAMEPAD_TRIGGER_THRESHOLD
                        break;
                    }
                    }
                    m_axes.Add(axis, simpleAxis);
                }
            }

            // Buttons
            m_buttons         = new Dictionary <GamepadButton, IButton>();
            m_buttonsReadOnly = m_buttons.ToReadOnly();
            foreach (GamepadButton button in Enum.GetValues(typeof(GamepadButton)))
            {
                if (button != GamepadButton.None && !button.IsVirtual())
                {
                    m_buttons.Add(button, new SimpleButton());
                }
            }
            m_buttons.Add(GamepadButton.LeftStickUp, new AxisButton(m_axes[GamepadAxis.LeftStickY], -0.5f));
            m_buttons.Add(GamepadButton.LeftStickDown, new AxisButton(m_axes[GamepadAxis.LeftStickY], 0.5f));
            m_buttons.Add(GamepadButton.LeftStickLeft, new AxisButton(m_axes[GamepadAxis.LeftStickX], -0.5f));
            m_buttons.Add(GamepadButton.LeftStickRight, new AxisButton(m_axes[GamepadAxis.LeftStickX], 0.5f));
            m_buttons.Add(GamepadButton.RightStickUp, new AxisButton(m_axes[GamepadAxis.RightStickY], -0.5f));
            m_buttons.Add(GamepadButton.RightStickDown, new AxisButton(m_axes[GamepadAxis.RightStickY], 0.5f));
            m_buttons.Add(GamepadButton.RightStickLeft, new AxisButton(m_axes[GamepadAxis.RightStickX], -0.5f));
            m_buttons.Add(GamepadButton.RightStickRight, new AxisButton(m_axes[GamepadAxis.RightStickX], 0.5f));
            m_buttons.Add(GamepadButton.LeftTrigger, new AxisButton(m_axes[GamepadAxis.LeftTrigger], 0.6f));   // Sometimes buggy triggers idle at 0.5, using 0.6 ensures we don't use these values
            m_buttons.Add(GamepadButton.RightTrigger, new AxisButton(m_axes[GamepadAxis.RightTrigger], 0.6f)); // Sometimes buggy triggers idle at 0.5, using 0.6 ensures we don't use these values

            // Joysticks
            m_joysticks         = new Dictionary <GamepadJoystick, IJoystick>();
            m_joysticksReadOnly = m_joysticks.ToReadOnly();
            m_joysticks.Add(GamepadJoystick.Left, new TwoAxisJoystick(m_axes[GamepadAxis.LeftStickX], m_axes[GamepadAxis.LeftStickY]));
            m_joysticks.Add(GamepadJoystick.Right, new TwoAxisJoystick(m_axes[GamepadAxis.RightStickX], m_axes[GamepadAxis.RightStickY]));

            // State
            m_connected = true;
            App.Log("{0} controller connected ({1}, {2})", m_type, m_name, m_joystickName);
            if (m_haptic != IntPtr.Zero)
            {
                App.Log("Rumble supported");
            }

            Update();
        }
Ejemplo n.º 10
0
        private bool MyEventHandler(SDL2Window window, SDL.SDL_Event e)
        {
            int mouseX;
            int mouseY;

            SDL.SDL_GetMouseState(out mouseX, out mouseY);

            if (!ImGuiSDL2CSHelper.HandleEvent(e, ref g_MouseWheel, g_MousePressed))
            {
                return(false);
            }

            if (e.type == SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN && e.button.button == 1 & !ImGui.GetIO().WantCaptureMouse)
            {
                _state.Camera.IsGrabbingMap        = true;
                _state.Camera.MouseFrameIncrementX = e.motion.x;
                _state.Camera.MouseFrameIncrementY = e.motion.y;
                mouseDownX = mouseX;
                mouseDownY = mouseY;
            }
            if (e.type == SDL.SDL_EventType.SDL_MOUSEBUTTONUP && e.button.button == 1)
            {
                _state.Camera.IsGrabbingMap = false;

                if (mouseDownX == mouseX && mouseDownY == mouseY)                                           //click on map.
                {
                    _state.MapClicked(_state.Camera.WorldCoordinate(mouseX, mouseY), MouseButtons.Primary); //sdl and imgu use different numbers for buttons.
                }
            }

            if (e.type == SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN && e.button.button == 3 & !ImGui.GetIO().WantCaptureMouse)
            {
                mouseDownAltX = mouseX;
                mouseDownAltY = mouseY;
            }
            if (e.type == SDL.SDL_EventType.SDL_MOUSEBUTTONUP && e.button.button == 3)
            {
                _state.Camera.IsGrabbingMap = false;

                if (mouseDownAltX == mouseX && mouseDownAltY == mouseY)                                 //click on map.
                {
                    _state.MapClicked(_state.Camera.WorldCoordinate(mouseX, mouseY), MouseButtons.Alt); //sdl and imgu use different numbers for buttons.
                }
            }

            if (_state.Camera.IsGrabbingMap)
            {
                int deltaX = _state.Camera.MouseFrameIncrementX - e.motion.x;
                int deltaY = _state.Camera.MouseFrameIncrementY - e.motion.y;
                _state.Camera.WorldOffset(deltaX, deltaY);

                _state.Camera.MouseFrameIncrementX = e.motion.x;
                _state.Camera.MouseFrameIncrementY = e.motion.y;
            }


            if (e.type == SDL.SDL_EventType.SDL_KEYUP)
            {
                if (e.key.keysym.sym == SDL.SDL_Keycode.SDLK_ESCAPE)
                {
                    MainMenuItems mainMenu = MainMenuItems.GetInstance();
                    mainMenu.IsActive = true;
                }
            }

            if (e.type == SDL.SDL_EventType.SDL_MOUSEWHEEL)
            {
                if (e.wheel.y > 0)
                {
                    _state.Camera.ZoomIn(mouseX, mouseY);
                }
                else if (e.wheel.y < 0)
                {
                    _state.Camera.ZoomOut(mouseX, mouseY);
                }
            }
            return(true);
        }
Ejemplo n.º 11
0
 public static void Main()
 {
     Instance = new PulsarMainWindow();
     Instance.Run();
     Instance.Dispose();
 }
Ejemplo n.º 12
0
        public Game()
        {
            m_promiseTasks = new List <PromiseTask>();

            m_over         = false;
            RenderUI       = true;
            UseDebugCamera = false;

            // Init network
            if (App.Steam)
            {
                m_network = new SteamworksNetwork(
                    AchievementExtensions.GetAllIDs(),
                    StatisticExtensions.GetAllIDs()
                    );
            }
            else
            {
                m_network = new BuiltinNetwork();
            }
            if (m_network.SupportsAchievements)
            {
                m_network.SetAchievementCorner(AchievementCorner.TopRight);
            }

            // Init user
            m_user = LoadUser();

            // Init window
            var title = App.Info.Title + " " + App.Info.Version.ToString();

            if (App.Debug && App.Steam)
            {
                title += " (Steam Debug build)";
            }
            else if (App.Debug)
            {
                title += " (Debug build)";
            }

            bool fullscreen = m_user.Settings.Fullscreen;
            bool vsync      = m_user.Settings.VSync;

            using (var icon = new Bitmap(Path.Combine(App.AssetPath, "icon.png")))
            {
                m_window = new SDL2Window(
                    title,
                    m_user.Settings.WindowWidth,
                    m_user.Settings.WindowHeight,
                    m_user.Settings.Fullscreen,
                    m_user.Settings.WindowMaximised,
                    m_user.Settings.VSync
                    );
                m_window.SetIcon(icon);
            }
            m_window.OnClosed += delegate(object sender, EventArgs e)
            {
                Over = true;
            };
            m_window.OnResized += delegate(object sender, EventArgs e)
            {
                Resize();
                if (!m_window.Fullscreen)
                {
                    if (m_window.Maximised)
                    {
                        m_user.Settings.WindowMaximised = true;
                    }
                    else
                    {
                        m_user.Settings.WindowMaximised = false;
                        m_user.Settings.WindowWidth     = m_window.Width;
                        m_user.Settings.WindowHeight    = m_window.Height;
                    }
                    m_user.Settings.Save();
                }
            };

            // Init audio
            if (App.Arguments.GetBool("nosound"))
            {
                m_audio = new NullAudio();
            }
            else
            {
                m_audio = new OpenALAudio();
            }
            m_audio.EnableSound = m_user.Settings.EnableSound;
            m_audio.SoundVolume = m_user.Settings.SoundVolume / 11.0f;
            m_audio.EnableMusic = m_user.Settings.EnableMusic;
            m_audio.MusicVolume = m_user.Settings.MusicVolume / 11.0f;
            m_gameAudio         = new GameAudio(m_audio);

            // Init input
            m_keyboard      = new SDL2Keyboard(m_window);
            m_mouse         = new SDL2Mouse(m_window);
            m_gamepads      = new SDL2GamepadCollection(m_window);
            m_activeGamepad = null;
            if (App.Steam)
            {
                m_steamControllers = new SteamworksSteamControllerCollection(
                    m_window,
                    SteamControllerActionSetExtensions.GetAllIDs(),
                    SteamControllerButtonExtensions.GetAllIDs(),
                    SteamControllerJoystickExtensions.GetAllIDs(),
                    SteamControllerAxisExtensions.GetAllIDs()
                    );
                m_readOnlySteamControllers = m_steamControllers;
                m_activeSteamController    = null;
            }
            else
            {
                m_steamControllers         = null;
                m_readOnlySteamControllers = new List <ISteamController>(0).ToReadOnly();
                m_activeSteamController    = null;
            }

            // Init tiles
            Tiles.Init();

            // Load early assets
            var earlyAssetFileStore = new FolderFileStore(Path.Combine(App.AssetPath, "early"));
            var earlyAssets         = new FileAssetSource("early", earlyAssetFileStore);

            Assets.AddSource(earlyAssets);
            Assets.LoadAll();

            // Find mods
            Mods.Refresh(Network);
            if (Network.SupportsWorkshop)
            {
                // See if any mods are worthy of the popular mod achievement
                var myModIDs = new List <ulong>();
                foreach (var mod in Mods.AllMods)
                {
                    if (mod.Source == ModSource.Editor &&
                        mod.SteamWorkshopID.HasValue)
                    {
                        myModIDs.Add(mod.SteamWorkshopID.Value);
                    }
                }
                if (myModIDs.Count > 0)
                {
                    QueuePromiseTask(
                        m_network.Workshop.GetItemInfo(myModIDs.ToArray()),
                        delegate(Promise <WorkshopItemInfo[]> result)
                    {
                        if (result.Status == Status.Complete)
                        {
                            var infos = result.Result;
                            int subs  = 0;
                            for (int i = 0; i < infos.Length; ++i)
                            {
                                var info = infos[i];
                                if (info.AuthorID == m_network.LocalUser.ID &&
                                    info.UpVotes >= info.DownVotes)
                                {
                                    subs = Math.Max(info.TotalSubscribers, subs);
                                }
                            }
                            int oldSubs = User.Progress.GetStatistic(Statistic.MostPopularModSubscriptions);
                            if (subs >= 25)
                            {
                                User.Progress.SetStatistic(Statistic.MostPopularModSubscriptions, subs);
                                User.Progress.UnlockAchievement(Achievement.CreatePopularMod);
                                User.Progress.Save();
                            }
                            else if (subs > oldSubs)
                            {
                                User.Progress.SetStatistic(Statistic.MostPopularModSubscriptions, subs);
                                User.Progress.IndicateAchievementProgress(Achievement.CreatePopularMod, subs, 25);
                                User.Progress.Save();
                            }
                        }
                    }
                        );
                }
            }

            // Load language
            SelectLanguage();

            // Load debug stuff
            m_debugCameraController = new DebugCameraController(this);

            // Load game
            Load();
        }