Непоследовательное обнаружение ввода кнопки джойстика с помощью SDL на C# с использованием TPL.C#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Непоследовательное обнаружение ввода кнопки джойстика с помощью SDL на C# с использованием TPL.

Сообщение Anonymous »


So I made a code that will detect joystick button input in a TPL where all joysticks' inputs are detected simultaneously. Good thing is that if there's only one joystick connected, it works consistently. If there are two or more connected joysticks, it does not work consistently as I expected to be. I'm not sure if TPL is the problem or it's the SDL is the problem.

Please help me. Apologies in advance if the code seems to be not consistent due to editing it in here and thanks in advance for helping me.

Here is my code:

Program.cs using System; using SDL2; using System.Threading.Tasks; using SDLCSharp; using System.Collections.Concurrent; class Program { static void Main(string[] args) { SDL.SDL_Init(SDL.SDL_INIT_JOYSTICK); Dictionary joystickInfo = new Dictionary(); int numJoysticks = SDL.SDL_NumJoysticks(); for (int i = 0; i < numJoysticks; ++i) { IntPtr joystick = SDL.SDL_JoystickOpen(i); if (joystick != IntPtr.Zero) { int getProductIDs = SDL.SDL_JoystickGetProduct(joystick); int instanceID = SDL.SDL_JoystickInstanceID(joystick); joystickInfo[getProductIDs] = Tuple.Create(i, instanceID); SDL.SDL_JoystickClose(joystick); } } Console.WriteLine("Connected Joysticks:"); foreach (var entry in joystickInfo) { int productID = entry.Key; int joystickIndex = entry.Value.Item1; int instanceID = entry.Value.Item2; Console.WriteLine($"Product ID: {productID}, Joystick Index: {joystickIndex}, Instance ID: {instanceID}"); } List tasks = new List(); foreach (var entry in joystickInfo) { int productID = entry.Key; Tuple joystickDetails = entry.Value; switch (productID) { case 12345: Joystick1 joy1 = new Joystick1(); tasks.Add(Task.Run(() => joy1.ActivateJoy1(joystickDetails))); break; case 12346: Joystick2 joy2 = new Joystick2(); tasks.Add(Task.Run(() => joy2.ActivateJoy2(joystickDetails))); break; default: break; } } Task.WaitAll(tasks.ToArray()); } } Activate.cs using SDL2; namespace SDLCSharp { public class Joystick1 { public void ActivateJoy1(Tuple joystickInfo) { int numIndex = joystickInfo.Item1; int instanceID = joystickInfo.Item2; bool quit = false; IntPtr joy1 = SDL.SDL_JoystickOpen(numIndex); while (!quit) { SDL.SDL_Event e; while (SDL.SDL_PollEvent(out e) != 0) { if(e.type == SDL.SDL_EventType.SDL_JOYBUTTONDOWN && e.jbutton.which == instanceID) { byte button0 = SDL.SDL_JoystickGetButton(joy1, 0); byte button1 = SDL.SDL_JoystickGetButton(joy1, 1); byte button2 = SDL.SDL_JoystickGetButton(joy1, 2); byte button3 = SDL.SDL_JoystickGetButton(joy1, 3); if (button0 == 1) { Console.Write("\nOff"); } if (button1 == 1) { Console.Write("\nLeft"); } if (button2 == 1) { Console.Write("\nCenter"); } if (button3 == 1) { Console.Write("\nRight"); } } } } } } public class Joystick2 { public void ActivateJoy2(Tuple joystickInfo) { int numIndex = joystickInfo.Item1; int instanceID = joystickInfo.Item2; bool quit = false; IntPtr joy2 = SDL.SDL_JoystickOpen(numIndex); while (!quit) { SDL.SDL_Event e; while (SDL.SDL_PollEvent(out e) != 0) { if(e.type == SDL.SDL_EventType.SDL_JOYBUTTONDOWN && e.jbutton.which == instanceID) { byte button4 = SDL.SDL_JoystickGetButton(joy2, 4); byte button5 = SDL.SDL_JoystickGetButton(joy2, 5); byte button6 = SDL.SDL_JoystickGetButton(joy2, 6); byte button7 = SDL.SDL_JoystickGetButton(joy2, 7); if (button4 == 1) { Console.Write("\nSouth"); } if (button5 == 1) { Console.Write("\nWest"); } if (button6 == 1) { Console.Write("\nNorth"); } if (button7 == 1) { Console.Write("\nEast"); } } } } } } } My expectations are that if there are two or more joysticks that are connected, pressing certain buttons from certain joysticks should display the messages according to which button is pressed consistently. It only works consistently when it's one joystick connected. Just not two or more.


Источник: https://stackoverflow.com/questions/781 ... th-the-use
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»