Advertisement
SilverTES

Controller Mapping System

Nov 16th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.26 KB | None | 0 0
  1. #include<memory>
  2. #include<vector>
  3. #include<iostream>
  4. #include<allegro.h>
  5. //#include<winalleg.h>
  6.  
  7. int const LOG = false;
  8.  
  9. enum SNESButton
  10. {
  11.     UNDEFINED = 0,
  12.     PAD_START,
  13.     PAD_SELECT,
  14.     PAD_UP,
  15.     PAD_DOWN,
  16.     PAD_LEFT,
  17.     PAD_RIGHT,
  18.     PAD_A,PAD_B,PAD_X,PAD_Y,
  19.     PAD_L,PAD_R,
  20.     MAX_BUTTONS
  21. };
  22.  
  23. std::vector<std::string> SNESButtonDico =
  24. {
  25.     "UNDEFINED",
  26.     "PAD_START",
  27.     "PAD_SELECT",
  28.     "PAD_UP",
  29.     "PAD_DOWN",
  30.     "PAD_LEFT",
  31.     "PAD_RIGHT",
  32.     "PAD_A","PAD_B","PAD_X","PAD_Y",
  33.     "PAD_L", "PAD_R",
  34.     "MAX_BUTTONS"
  35. };
  36.  
  37. std::string getSNESButtonDico(unsigned id)
  38. {
  39.     if (id >=0 && id<MAX_BUTTONS)
  40.         return SNESButtonDico[id];
  41.     return "Undefined";
  42. }
  43.  
  44.  
  45. struct Button
  46. {
  47.     Button(int id = 0, int idKey = 0, int idJoy = 0, int idStick = 0, int idAxis = 0, int idDirection = 0, int idButton = 0):
  48.     _id(id),
  49.     _idKey(idKey),
  50.     _idJoy(idJoy),
  51.     _idStick(idStick),
  52.     _idAxis(idAxis),
  53.     _idDirection(idDirection),
  54.     _idButton(idButton)
  55.     {
  56.         if (LOG) std::cout << "+++ Button \n";
  57.     }
  58.     virtual ~Button()
  59.     {
  60.         if (LOG) std::cout << "--- Button \n";
  61.     }
  62.  
  63.     int _id;      // SNESButton enum !
  64.     int _idKey;     // Keyboard Id
  65.     int _idJoy;     // Gamepad Id
  66.     int _idStick;   // Stick of Gamepad Id
  67.     int _idAxis;   // Axis of Stick Id
  68.     int _idDirection; // Direction of the Axis Id
  69.     int _idButton;  // Button of Gamepad Id
  70.  
  71. };
  72.  
  73. class Controller
  74. {
  75.     public:
  76.         Controller()
  77.         {
  78.             for (unsigned i(1); i< MAX_BUTTONS; i++)
  79.             {
  80.                 _arrayButton[i] = std::make_shared<Button>(0);
  81.             }
  82.  
  83.             if (LOG) std::cout << "+++ Controller \n ";
  84.  
  85.         }
  86.  
  87.         virtual ~Controller()
  88.         {
  89. //            for (auto & it: _arrayButton)
  90. //            {
  91. //                delete it;
  92. //            }
  93.             //_arrayButton.empty();
  94.             if (LOG) std::cout << "--- Controller \n ";
  95.         }
  96.  
  97.         void showController() // for Debug !
  98.         {
  99.             std::cout << "*** show Controller *** \n";
  100.  
  101.             for (unsigned i(1); i< MAX_BUTTONS; i++)
  102.             {
  103.                 if (_arrayButton[i]->_id != 0)
  104.                 {
  105.  
  106.                     //std::cout << " -/*-+-/-*/ \n";
  107.                     std::cout << " id = " << _arrayButton[i]->_id << " :: " ;
  108.                     std::cout << " > " << getSNESButtonDico(_arrayButton[i]->_id) << " = "
  109.                     << _arrayButton[i]->_idKey << " "
  110.                     << _arrayButton[i]->_idJoy << " "
  111.                     << _arrayButton[i]->_idStick << " "
  112.                     << _arrayButton[i]->_idButton << "\n";
  113.  
  114.                 }
  115.             }
  116.         }
  117.  
  118.         void setButton(int id, int idKey, int idJoy, int id_stick, int idAxis, int idDirection, int idButton)
  119.         {
  120.             _arrayButton[id] = std::make_shared<Button>(id, idKey, idJoy, id_stick, idAxis, idDirection, idButton);
  121.         }
  122.  
  123.         void setButton(int id, std::shared_ptr<Button> button)
  124.         {
  125.             _arrayButton[id] = button;
  126.         }
  127.  
  128.         int getButton(int id)
  129.         {
  130.             // Check Keyboard !
  131.             if ( key[_arrayButton[id]->_idKey] )
  132.                 return 1;
  133.  
  134.             // Check Joystick Button !
  135.             if ( joy[_arrayButton[id]->_idJoy].button[_arrayButton[id]->_idButton].b )
  136.                 return 2;
  137.  
  138.             // Check Joystick Stick !
  139.             if (_arrayButton[id]->_idDirection == 1)
  140.                 if ( joy[_arrayButton[id]->_idJoy].stick[_arrayButton[id]->_idStick].axis[_arrayButton[id]->_idAxis].d1 )
  141.                     return 3;
  142.             if (_arrayButton[id]->_idDirection == 2)
  143.                 if ( joy[_arrayButton[id]->_idJoy].stick[_arrayButton[id]->_idStick].axis[_arrayButton[id]->_idAxis].d2 )
  144.                     return 3;
  145.  
  146.             return 0;
  147.         }
  148.  
  149.     protected:
  150.     private:
  151.         std::shared_ptr<Button> _arrayButton[MAX_BUTTONS] = {};
  152. };
  153.  
  154.  
  155.  
  156. class Player
  157. {
  158.     public:
  159.         Player()
  160.         {
  161.             if (LOG) std::cout << "+++ Player \n";
  162.             _life = 100;
  163.             _force = 32;
  164.             _power = 8;
  165.             _controller = new Controller();
  166.         }
  167.         virtual ~Player()
  168.         {
  169.             if (LOG) std::cout << "--- Player \n";
  170.             delete _controller;
  171.         }
  172.         Player * getData()
  173.         {
  174.             return this;
  175.         }
  176.         Controller * getController()
  177.         {
  178.             return _controller;
  179.         }
  180.     protected:
  181.     private:
  182.         int _life;
  183.         int _force;
  184.         int _power;
  185.         Controller  * _controller;
  186. };
  187.  
  188. class PlayerSys
  189. {
  190.     public:
  191.         PlayerSys()
  192.         {
  193.             if (LOG) std::cout << "+++ Player System \n";
  194.         }
  195.  
  196.         ~PlayerSys()
  197.         {
  198.             if (LOG) std::cout << "--- Player System \n";
  199.             for (auto & it: _vecPlayer)
  200.             {
  201.                 delete it;
  202.             }
  203.             _vecPlayer.clear();
  204.         }
  205.  
  206.         void addPlayer(Player * player)
  207.         {
  208.             _vecPlayer.emplace_back(player);
  209.         }
  210.  
  211.         int getNum()
  212.         {
  213.             return _vecPlayer.size();
  214.         }
  215.  
  216.         Player * getData(unsigned index)
  217.         {
  218.             if (index < _vecPlayer.size())
  219.             {
  220.                 return _vecPlayer[index]->getData();
  221.             }
  222.             return NULL;
  223.         }
  224.     protected:
  225.     private:
  226.         std::vector<Player*> _vecPlayer;
  227.  
  228. };
  229.  
  230.  
  231. class Window
  232. {
  233.     private:
  234.         int _scrX;
  235.         int _scrY;
  236.         int _zoom;
  237.         bool _quit;
  238.  
  239.         BITMAP * _buffer;
  240.  
  241.     protected:
  242.  
  243.     public:
  244.  
  245.         Window(int scrX, int scrY, int zoom):
  246.             _scrX(scrX),
  247.             _scrY(scrY),
  248.             _zoom(zoom),
  249.             _buffer(nullptr)
  250.         {
  251.             if (LOG) std::cout << "+++ Window \n";
  252.         }
  253.         virtual ~Window()
  254.         {
  255.             if (LOG) std::cout << "--- Window \n";
  256.             remove_mouse();
  257.             remove_joystick();
  258.             remove_keyboard();
  259.             destroy_bitmap(_buffer);
  260.         }
  261.  
  262.         int init()
  263.         {
  264.             if (LOG) std::cout << "*** Window initialized ! \n";
  265.  
  266.             _quit = false;
  267.  
  268.             // --- Init Allegro 4
  269.             if (allegro_init() != 0)
  270.                 return 1;
  271.  
  272.             install_keyboard();
  273.             install_mouse();
  274.             install_joystick(JOY_TYPE_AUTODETECT);
  275.  
  276.             set_palette(desktop_palette);
  277.             set_color_depth(desktop_color_depth());
  278.  
  279.             if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, _scrX*_zoom, _scrY*_zoom, 0, 0) != 0)
  280.             {
  281.                 allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
  282.                 return 1;
  283.             }
  284.  
  285.             set_display_switch_mode(SWITCH_BACKGROUND);
  286.  
  287.             // --- Init
  288.             _buffer = create_bitmap(_scrX, _scrY);
  289.             return 0;
  290.         }
  291.  
  292.         void beginRender()
  293.         {
  294.             acquire_bitmap(_buffer);
  295.             clear_to_color(_buffer, makecol(25, 55, 55));
  296.         }
  297.  
  298.         void endRender()
  299.         {
  300.             release_bitmap(_buffer);
  301.             acquire_screen();
  302.             stretch_blit(_buffer, screen, 0, 0, _scrX, _scrY, 0, 0, SCREEN_W, SCREEN_H);
  303.             release_screen();
  304.         }
  305.  
  306.         int getScrX()
  307.         {
  308.             return _scrX;
  309.         }
  310.         int getScrY()
  311.         {
  312.             return _scrY;
  313.         }
  314.         int getZoom()
  315.         {
  316.             return _zoom;
  317.         }
  318.         BITMAP * getBuffer()
  319.         {
  320.             return _buffer;
  321.         }
  322.  
  323.         void setQuit()
  324.         {
  325.             _quit = true;
  326.         }
  327.         bool quit()
  328.         {
  329.             if (_quit) return true;
  330.             if (key[KEY_ESC]) return true;
  331.  
  332.             return false;
  333.         }
  334.  
  335. };
  336.  
  337. void drawGrid(BITMAP * targetbmp, int width, int height, int spaceX, int spaceY, int color)
  338. {
  339.     for (int i(0); i<width; i++)
  340.     {
  341.         for(int j(0); j<height; j++)
  342.         {
  343.             rect(targetbmp, i*spaceX, j*spaceY, i*spaceX+spaceX, j*spaceY+spaceY, color);
  344.         }
  345.     }
  346. }
  347.  
  348. void showGamePadInfo (int index)
  349. {
  350.     std::cout << "-------------------------------------\n";
  351.     std::cout << "- GamePad : " << index << " \n";
  352.     std::cout << "-------------------------------------\n";
  353.     std::cout << "- Num Stick   = " << joy[index].num_sticks << " \n";
  354.     std::cout << "- Num Buttons = " << joy[index].num_buttons << " \n";
  355.  
  356.     for (int i(0); i< joy[index].num_buttons; i++)
  357.     {
  358.         std::cout << " Button [ " << i << "] = " << joy[index].button[i].name << " \n";
  359.     }
  360.  
  361.  
  362.     for (int i(0); i< joy[index].num_sticks; i++)
  363.     {
  364.         std::cout << " Stick [ " << i << "] = " << joy[index].stick[i].name << " \n";
  365.  
  366.         for (int j(0); j< joy[index].stick[i].num_axis; j++)
  367.         {
  368.             std::cout << " Axis [ " << i << "] = " << joy[index].stick[i].axis[j].name << " \n";
  369.         }
  370.  
  371.     }
  372.  
  373.     std::cout << "-------------------------------------\n";
  374. }
  375.  
  376. int getKey()
  377. {
  378.     for (int i(0); i<KEY_MAX; i++)
  379.     {
  380.         if (key[i]) return i;
  381.     }
  382.     return 0;
  383. }
  384.  
  385. std::shared_ptr<Button> getButton(int id)
  386. {
  387.     for (int i(0); i<KEY_MAX; i++)
  388.     {
  389.         if (key[i]) return std::make_shared<Button>(id, i, 0, 0, 0, 0, 0);
  390.     }
  391.  
  392.     for (int i(0); i<num_joysticks; i++) // All Joysticks
  393.     {
  394.         for (int j(0); j<joy[i].num_buttons; j++) // All Buttons of the Joystick
  395.         {
  396.             if (joy[i].button[j].b) return std::make_shared<Button>(id, 0, i, 0, 0, 0, j);
  397.         }
  398.  
  399.         for (int k(0); k<joy[i].num_sticks; k++) // All Sticks of the Joystick
  400.         {
  401.             for (int l(0); l<joy[i].stick[k].num_axis; l++) // All Axis of the Joystick
  402.             {
  403.                 if (joy[i].stick[k].axis[l].d1)
  404.                     return std::make_shared<Button>(id, 0, i, k, l, 1, 0);
  405.                 if (joy[i].stick[k].axis[l].d2)
  406.                     return std::make_shared<Button>(id, 0, i, k, l, 2, 0);
  407.  
  408.             }
  409.         }
  410.  
  411.  
  412.     }
  413.     return 0;
  414. }
  415.  
  416. void assignButton (PlayerSys &playerSys, int player, int id) // Fonction bloquante , wait KEY or JOY inputs
  417. {
  418.  
  419.     std::cout << " > Assign "<< SNESButtonDico[id]  << " Button < ";
  420.     while (1)
  421.     {
  422.         poll_joystick();
  423.         poll_keyboard();
  424.         std::shared_ptr<Button> button  = getButton(id);
  425.         if ( button != 0)
  426.         {
  427.             playerSys.getData(player)->getController()->setButton(id, button);
  428.             break;
  429.         }
  430.     }
  431.     std::cout << " * OK * \n";
  432.     rest(400);
  433.  
  434. }
  435.  
  436. int main(void)
  437. {
  438.     Window win(640, 360, 2); // WindowSystem::create(win, 640, 360, 2);
  439.     win.init();
  440.  
  441.     //showGamePadInfo(0);
  442.  
  443.     PlayerSys playerSys;
  444.  
  445.     playerSys.addPlayer(new Player()); // Add a player in the game !
  446.  
  447.     assignButton(playerSys, 0, PAD_START);
  448.  
  449.     //playerSys.getData(0)->getController()->setButton(PAD_START  , 0, 0, 0, 0, 0, 9); // assign START BUTTON TO : GamePad[0] , button[] bouton start logitech rumble pad 2
  450.     playerSys.getData(0)->getController()->setButton(PAD_SELECT , 0, 0, 0, 0, 0, 8); // assign START BUTTON TO : GamePad[0] , button[] bouton select
  451.  
  452.     assignButton(playerSys, 0, PAD_UP);
  453.     assignButton(playerSys, 0, PAD_DOWN);
  454.     assignButton(playerSys, 0, PAD_LEFT);
  455.     assignButton(playerSys, 0, PAD_RIGHT);
  456. //    playerSys.getData(0)->getController()->setButton(PAD_UP    , 0, 0, 2, 1, 1, 0);
  457. //    playerSys.getData(0)->getController()->setButton(PAD_DOWN  , 0, 0, 2, 1, 2, 0);
  458. //    playerSys.getData(0)->getController()->setButton(PAD_LEFT  , 0, 0, 2, 0, 1, 0);
  459. //    playerSys.getData(0)->getController()->setButton(PAD_RIGHT , 0, 0, 2, 0, 2, 0);
  460.  
  461.     std::cout << " Number of player : " << playerSys.getNum() << "\n" ;
  462.  
  463.     playerSys.getData(0)->getController()->showController();
  464.  
  465.  
  466. //    for (unsigned i(0); i< SNESButtonDico.size(); i++)
  467. //    {
  468. //        std::cout << " > " << getSNESButtonDico(i) << "\n" ;
  469. //    }
  470.  
  471.     BITMAP * gamepad;
  472.     BITMAP * cat;
  473.  
  474.     PALETTE pal;
  475.     gamepad = load_bitmap("gamepad.tga", pal);
  476.     cat = load_bitmap("yoshi.tga", pal);
  477.  
  478.  
  479.     int pX(win.getScrX()/2);
  480.     int pY(win.getScrY()/2);
  481.  
  482.     while (!win.quit())
  483.     {
  484.  
  485.         // --- Input
  486.         poll_keyboard();
  487.         poll_joystick();
  488.  
  489.         if (playerSys.getData(0)->getController()->getButton(PAD_SELECT)) std::cout << "> PAD_SELECT < \n";
  490.         if (playerSys.getData(0)->getController()->getButton(PAD_START))
  491.         {
  492.             std::cout << "> PAD_START < \n";
  493.             //win.setQuit();
  494.         }
  495.  
  496.         if (playerSys.getData(0)->getController()->getButton(PAD_UP)) pY -= 1;
  497.         if (playerSys.getData(0)->getController()->getButton(PAD_DOWN)) pY += 1;
  498.         if (playerSys.getData(0)->getController()->getButton(PAD_LEFT)) pX -= 1;
  499.         if (playerSys.getData(0)->getController()->getButton(PAD_RIGHT)) pX += 1;
  500.  
  501.         // --- Update
  502.  
  503.         int xMouse = mouse_x/win.getZoom();
  504.         int yMouse = mouse_y/win.getZoom();
  505.  
  506.         // --- Render
  507.         win.beginRender(); // WindowSystem::beginRender(win);
  508.  
  509.         int caseSize(16);
  510.  
  511.         drawGrid(win.getBuffer(), win.getScrX()/caseSize, win.getScrY()/caseSize, caseSize, caseSize, makecol(40,80,100));
  512.         textout_centre_ex(win.getBuffer(), font, "Mugen world!",  win.getScrX()/2, win.getScrY()/2, makecol(200,200,0), -1);
  513.         hline(win.getBuffer(), 0, yMouse, win.getScrX(), makecol(100,200,0));
  514.         vline(win.getBuffer(), xMouse, 0, win.getScrY(), makecol(100,200,0));
  515.         draw_sprite(win.getBuffer(), gamepad, xMouse-gamepad->w/2, yMouse-gamepad->h/2);
  516.  
  517.         draw_sprite(win.getBuffer(), cat, pX-cat->w/2, pY-cat->h/2);
  518.  
  519. //        textprintf_ex(win.getBuffer(), font, 10,10, makecol(255,255,0),-1," stick[2] : %i , %i , %i, %i | %i , %i ",
  520. //                      joy[0].stick[2].axis[0].d1,
  521. //                      joy[0].stick[2].axis[0].d2,
  522. //                      joy[0].stick[2].axis[1].d1,
  523. //                      joy[0].stick[2].axis[1].d2,
  524. //                      joy[0].stick[2].axis[0].pos,
  525. //                      joy[0].stick[2].axis[1].pos);
  526. //
  527. //        textprintf_ex(win.getBuffer(), font, 10,20, makecol(255,255,0),-1," stick[1] : %i , %i , %i, %i | %i , %i ",
  528. //                      joy[0].stick[1].axis[0].d1,
  529. //                      joy[0].stick[1].axis[0].d2,
  530. //                      joy[0].stick[1].axis[1].d1,
  531. //                      joy[0].stick[1].axis[1].d2,
  532. //                      joy[0].stick[1].axis[0].pos,
  533. //                      joy[0].stick[1].axis[1].pos);
  534. //
  535. //        textprintf_ex(win.getBuffer(), font, 10,30, makecol(255,255,0),-1," stick[0] : %i , %i , %i, %i | %i , %i ",
  536. //                      joy[0].stick[0].axis[0].d1,
  537. //                      joy[0].stick[0].axis[0].d2,
  538. //                      joy[0].stick[0].axis[1].d1,
  539. //                      joy[0].stick[0].axis[1].d2,
  540. //                      joy[0].stick[0].axis[0].pos,
  541. //                      joy[0].stick[0].axis[1].pos);
  542.  
  543.          textprintf_ex(win.getBuffer(), font, 2,40, makecol(255,255,0),-1," getKey(i) = %i", getKey());
  544.  
  545.         win.endRender();
  546.  
  547.         rest(5);
  548.     }
  549.  
  550.     // --- Done
  551.  
  552.     destroy_bitmap(gamepad);
  553.     destroy_bitmap(cat);
  554.  
  555.     return 0;
  556. }
  557. END_OF_MAIN()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement