Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<memory>
- #include<vector>
- #include<iostream>
- #include<allegro.h>
- //#include<winalleg.h>
- int const LOG = false;
- enum SNESButton
- {
- UNDEFINED = 0,
- PAD_START,
- PAD_SELECT,
- PAD_UP,
- PAD_DOWN,
- PAD_LEFT,
- PAD_RIGHT,
- PAD_A,PAD_B,PAD_X,PAD_Y,
- PAD_L,PAD_R,
- MAX_BUTTONS
- };
- std::vector<std::string> SNESButtonDico =
- {
- "UNDEFINED",
- "PAD_START",
- "PAD_SELECT",
- "PAD_UP",
- "PAD_DOWN",
- "PAD_LEFT",
- "PAD_RIGHT",
- "PAD_A","PAD_B","PAD_X","PAD_Y",
- "PAD_L", "PAD_R",
- "MAX_BUTTONS"
- };
- std::string getSNESButtonDico(unsigned id)
- {
- if (id >=0 && id<MAX_BUTTONS)
- return SNESButtonDico[id];
- return "Undefined";
- }
- struct Button
- {
- Button(int id = 0, int idKey = 0, int idJoy = 0, int idStick = 0, int idAxis = 0, int idDirection = 0, int idButton = 0):
- _id(id),
- _idKey(idKey),
- _idJoy(idJoy),
- _idStick(idStick),
- _idAxis(idAxis),
- _idDirection(idDirection),
- _idButton(idButton)
- {
- if (LOG) std::cout << "+++ Button \n";
- }
- virtual ~Button()
- {
- if (LOG) std::cout << "--- Button \n";
- }
- int _id; // SNESButton enum !
- int _idKey; // Keyboard Id
- int _idJoy; // Gamepad Id
- int _idStick; // Stick of Gamepad Id
- int _idAxis; // Axis of Stick Id
- int _idDirection; // Direction of the Axis Id
- int _idButton; // Button of Gamepad Id
- };
- class Controller
- {
- public:
- Controller()
- {
- for (unsigned i(1); i< MAX_BUTTONS; i++)
- {
- _arrayButton[i] = std::make_shared<Button>(0);
- }
- if (LOG) std::cout << "+++ Controller \n ";
- }
- virtual ~Controller()
- {
- // for (auto & it: _arrayButton)
- // {
- // delete it;
- // }
- //_arrayButton.empty();
- if (LOG) std::cout << "--- Controller \n ";
- }
- void showController() // for Debug !
- {
- std::cout << "*** show Controller *** \n";
- for (unsigned i(1); i< MAX_BUTTONS; i++)
- {
- if (_arrayButton[i]->_id != 0)
- {
- //std::cout << " -/*-+-/-*/ \n";
- std::cout << " id = " << _arrayButton[i]->_id << " :: " ;
- std::cout << " > " << getSNESButtonDico(_arrayButton[i]->_id) << " = "
- << _arrayButton[i]->_idKey << " "
- << _arrayButton[i]->_idJoy << " "
- << _arrayButton[i]->_idStick << " "
- << _arrayButton[i]->_idButton << "\n";
- }
- }
- }
- void setButton(int id, int idKey, int idJoy, int id_stick, int idAxis, int idDirection, int idButton)
- {
- _arrayButton[id] = std::make_shared<Button>(id, idKey, idJoy, id_stick, idAxis, idDirection, idButton);
- }
- void setButton(int id, std::shared_ptr<Button> button)
- {
- _arrayButton[id] = button;
- }
- int getButton(int id)
- {
- // Check Keyboard !
- if ( key[_arrayButton[id]->_idKey] )
- return 1;
- // Check Joystick Button !
- if ( joy[_arrayButton[id]->_idJoy].button[_arrayButton[id]->_idButton].b )
- return 2;
- // Check Joystick Stick !
- if (_arrayButton[id]->_idDirection == 1)
- if ( joy[_arrayButton[id]->_idJoy].stick[_arrayButton[id]->_idStick].axis[_arrayButton[id]->_idAxis].d1 )
- return 3;
- if (_arrayButton[id]->_idDirection == 2)
- if ( joy[_arrayButton[id]->_idJoy].stick[_arrayButton[id]->_idStick].axis[_arrayButton[id]->_idAxis].d2 )
- return 3;
- return 0;
- }
- protected:
- private:
- std::shared_ptr<Button> _arrayButton[MAX_BUTTONS] = {};
- };
- class Player
- {
- public:
- Player()
- {
- if (LOG) std::cout << "+++ Player \n";
- _life = 100;
- _force = 32;
- _power = 8;
- _controller = new Controller();
- }
- virtual ~Player()
- {
- if (LOG) std::cout << "--- Player \n";
- delete _controller;
- }
- Player * getData()
- {
- return this;
- }
- Controller * getController()
- {
- return _controller;
- }
- protected:
- private:
- int _life;
- int _force;
- int _power;
- Controller * _controller;
- };
- class PlayerSys
- {
- public:
- PlayerSys()
- {
- if (LOG) std::cout << "+++ Player System \n";
- }
- ~PlayerSys()
- {
- if (LOG) std::cout << "--- Player System \n";
- for (auto & it: _vecPlayer)
- {
- delete it;
- }
- _vecPlayer.clear();
- }
- void addPlayer(Player * player)
- {
- _vecPlayer.emplace_back(player);
- }
- int getNum()
- {
- return _vecPlayer.size();
- }
- Player * getData(unsigned index)
- {
- if (index < _vecPlayer.size())
- {
- return _vecPlayer[index]->getData();
- }
- return NULL;
- }
- protected:
- private:
- std::vector<Player*> _vecPlayer;
- };
- class Window
- {
- private:
- int _scrX;
- int _scrY;
- int _zoom;
- bool _quit;
- BITMAP * _buffer;
- protected:
- public:
- Window(int scrX, int scrY, int zoom):
- _scrX(scrX),
- _scrY(scrY),
- _zoom(zoom),
- _buffer(nullptr)
- {
- if (LOG) std::cout << "+++ Window \n";
- }
- virtual ~Window()
- {
- if (LOG) std::cout << "--- Window \n";
- remove_mouse();
- remove_joystick();
- remove_keyboard();
- destroy_bitmap(_buffer);
- }
- int init()
- {
- if (LOG) std::cout << "*** Window initialized ! \n";
- _quit = false;
- // --- Init Allegro 4
- if (allegro_init() != 0)
- return 1;
- install_keyboard();
- install_mouse();
- install_joystick(JOY_TYPE_AUTODETECT);
- set_palette(desktop_palette);
- set_color_depth(desktop_color_depth());
- if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, _scrX*_zoom, _scrY*_zoom, 0, 0) != 0)
- {
- allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
- return 1;
- }
- set_display_switch_mode(SWITCH_BACKGROUND);
- // --- Init
- _buffer = create_bitmap(_scrX, _scrY);
- return 0;
- }
- void beginRender()
- {
- acquire_bitmap(_buffer);
- clear_to_color(_buffer, makecol(25, 55, 55));
- }
- void endRender()
- {
- release_bitmap(_buffer);
- acquire_screen();
- stretch_blit(_buffer, screen, 0, 0, _scrX, _scrY, 0, 0, SCREEN_W, SCREEN_H);
- release_screen();
- }
- int getScrX()
- {
- return _scrX;
- }
- int getScrY()
- {
- return _scrY;
- }
- int getZoom()
- {
- return _zoom;
- }
- BITMAP * getBuffer()
- {
- return _buffer;
- }
- void setQuit()
- {
- _quit = true;
- }
- bool quit()
- {
- if (_quit) return true;
- if (key[KEY_ESC]) return true;
- return false;
- }
- };
- void drawGrid(BITMAP * targetbmp, int width, int height, int spaceX, int spaceY, int color)
- {
- for (int i(0); i<width; i++)
- {
- for(int j(0); j<height; j++)
- {
- rect(targetbmp, i*spaceX, j*spaceY, i*spaceX+spaceX, j*spaceY+spaceY, color);
- }
- }
- }
- void showGamePadInfo (int index)
- {
- std::cout << "-------------------------------------\n";
- std::cout << "- GamePad : " << index << " \n";
- std::cout << "-------------------------------------\n";
- std::cout << "- Num Stick = " << joy[index].num_sticks << " \n";
- std::cout << "- Num Buttons = " << joy[index].num_buttons << " \n";
- for (int i(0); i< joy[index].num_buttons; i++)
- {
- std::cout << " Button [ " << i << "] = " << joy[index].button[i].name << " \n";
- }
- for (int i(0); i< joy[index].num_sticks; i++)
- {
- std::cout << " Stick [ " << i << "] = " << joy[index].stick[i].name << " \n";
- for (int j(0); j< joy[index].stick[i].num_axis; j++)
- {
- std::cout << " Axis [ " << i << "] = " << joy[index].stick[i].axis[j].name << " \n";
- }
- }
- std::cout << "-------------------------------------\n";
- }
- int getKey()
- {
- for (int i(0); i<KEY_MAX; i++)
- {
- if (key[i]) return i;
- }
- return 0;
- }
- std::shared_ptr<Button> getButton(int id)
- {
- for (int i(0); i<KEY_MAX; i++)
- {
- if (key[i]) return std::make_shared<Button>(id, i, 0, 0, 0, 0, 0);
- }
- for (int i(0); i<num_joysticks; i++) // All Joysticks
- {
- for (int j(0); j<joy[i].num_buttons; j++) // All Buttons of the Joystick
- {
- if (joy[i].button[j].b) return std::make_shared<Button>(id, 0, i, 0, 0, 0, j);
- }
- for (int k(0); k<joy[i].num_sticks; k++) // All Sticks of the Joystick
- {
- for (int l(0); l<joy[i].stick[k].num_axis; l++) // All Axis of the Joystick
- {
- if (joy[i].stick[k].axis[l].d1)
- return std::make_shared<Button>(id, 0, i, k, l, 1, 0);
- if (joy[i].stick[k].axis[l].d2)
- return std::make_shared<Button>(id, 0, i, k, l, 2, 0);
- }
- }
- }
- return 0;
- }
- void assignButton (PlayerSys &playerSys, int player, int id) // Fonction bloquante , wait KEY or JOY inputs
- {
- std::cout << " > Assign "<< SNESButtonDico[id] << " Button < ";
- while (1)
- {
- poll_joystick();
- poll_keyboard();
- std::shared_ptr<Button> button = getButton(id);
- if ( button != 0)
- {
- playerSys.getData(player)->getController()->setButton(id, button);
- break;
- }
- }
- std::cout << " * OK * \n";
- rest(400);
- }
- int main(void)
- {
- Window win(640, 360, 2); // WindowSystem::create(win, 640, 360, 2);
- win.init();
- //showGamePadInfo(0);
- PlayerSys playerSys;
- playerSys.addPlayer(new Player()); // Add a player in the game !
- assignButton(playerSys, 0, PAD_START);
- //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
- playerSys.getData(0)->getController()->setButton(PAD_SELECT , 0, 0, 0, 0, 0, 8); // assign START BUTTON TO : GamePad[0] , button[] bouton select
- assignButton(playerSys, 0, PAD_UP);
- assignButton(playerSys, 0, PAD_DOWN);
- assignButton(playerSys, 0, PAD_LEFT);
- assignButton(playerSys, 0, PAD_RIGHT);
- // playerSys.getData(0)->getController()->setButton(PAD_UP , 0, 0, 2, 1, 1, 0);
- // playerSys.getData(0)->getController()->setButton(PAD_DOWN , 0, 0, 2, 1, 2, 0);
- // playerSys.getData(0)->getController()->setButton(PAD_LEFT , 0, 0, 2, 0, 1, 0);
- // playerSys.getData(0)->getController()->setButton(PAD_RIGHT , 0, 0, 2, 0, 2, 0);
- std::cout << " Number of player : " << playerSys.getNum() << "\n" ;
- playerSys.getData(0)->getController()->showController();
- // for (unsigned i(0); i< SNESButtonDico.size(); i++)
- // {
- // std::cout << " > " << getSNESButtonDico(i) << "\n" ;
- // }
- BITMAP * gamepad;
- BITMAP * cat;
- PALETTE pal;
- gamepad = load_bitmap("gamepad.tga", pal);
- cat = load_bitmap("yoshi.tga", pal);
- int pX(win.getScrX()/2);
- int pY(win.getScrY()/2);
- while (!win.quit())
- {
- // --- Input
- poll_keyboard();
- poll_joystick();
- if (playerSys.getData(0)->getController()->getButton(PAD_SELECT)) std::cout << "> PAD_SELECT < \n";
- if (playerSys.getData(0)->getController()->getButton(PAD_START))
- {
- std::cout << "> PAD_START < \n";
- //win.setQuit();
- }
- if (playerSys.getData(0)->getController()->getButton(PAD_UP)) pY -= 1;
- if (playerSys.getData(0)->getController()->getButton(PAD_DOWN)) pY += 1;
- if (playerSys.getData(0)->getController()->getButton(PAD_LEFT)) pX -= 1;
- if (playerSys.getData(0)->getController()->getButton(PAD_RIGHT)) pX += 1;
- // --- Update
- int xMouse = mouse_x/win.getZoom();
- int yMouse = mouse_y/win.getZoom();
- // --- Render
- win.beginRender(); // WindowSystem::beginRender(win);
- int caseSize(16);
- drawGrid(win.getBuffer(), win.getScrX()/caseSize, win.getScrY()/caseSize, caseSize, caseSize, makecol(40,80,100));
- textout_centre_ex(win.getBuffer(), font, "Mugen world!", win.getScrX()/2, win.getScrY()/2, makecol(200,200,0), -1);
- hline(win.getBuffer(), 0, yMouse, win.getScrX(), makecol(100,200,0));
- vline(win.getBuffer(), xMouse, 0, win.getScrY(), makecol(100,200,0));
- draw_sprite(win.getBuffer(), gamepad, xMouse-gamepad->w/2, yMouse-gamepad->h/2);
- draw_sprite(win.getBuffer(), cat, pX-cat->w/2, pY-cat->h/2);
- // textprintf_ex(win.getBuffer(), font, 10,10, makecol(255,255,0),-1," stick[2] : %i , %i , %i, %i | %i , %i ",
- // joy[0].stick[2].axis[0].d1,
- // joy[0].stick[2].axis[0].d2,
- // joy[0].stick[2].axis[1].d1,
- // joy[0].stick[2].axis[1].d2,
- // joy[0].stick[2].axis[0].pos,
- // joy[0].stick[2].axis[1].pos);
- //
- // textprintf_ex(win.getBuffer(), font, 10,20, makecol(255,255,0),-1," stick[1] : %i , %i , %i, %i | %i , %i ",
- // joy[0].stick[1].axis[0].d1,
- // joy[0].stick[1].axis[0].d2,
- // joy[0].stick[1].axis[1].d1,
- // joy[0].stick[1].axis[1].d2,
- // joy[0].stick[1].axis[0].pos,
- // joy[0].stick[1].axis[1].pos);
- //
- // textprintf_ex(win.getBuffer(), font, 10,30, makecol(255,255,0),-1," stick[0] : %i , %i , %i, %i | %i , %i ",
- // joy[0].stick[0].axis[0].d1,
- // joy[0].stick[0].axis[0].d2,
- // joy[0].stick[0].axis[1].d1,
- // joy[0].stick[0].axis[1].d2,
- // joy[0].stick[0].axis[0].pos,
- // joy[0].stick[0].axis[1].pos);
- textprintf_ex(win.getBuffer(), font, 2,40, makecol(255,255,0),-1," getKey(i) = %i", getKey());
- win.endRender();
- rest(5);
- }
- // --- Done
- destroy_bitmap(gamepad);
- destroy_bitmap(cat);
- return 0;
- }
- END_OF_MAIN()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement