Advertisement
SilverTES

Allegro 5 MultiMonitor Manager

Nov 29th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4. #include <allegro5/allegro.h>
  5. #include <allegro5/allegro_image.h>
  6. #include <allegro5/allegro_primitives.h>
  7. #include <allegro5/allegro_windows.h>
  8. #include <allegro5/allegro_ttf.h>
  9. #include <allegro5/allegro_font.h>
  10. #include <allegro5/allegro_windows.h>
  11.  
  12.  
  13. ALLEGRO_KEYBOARD_STATE _keyState;
  14. ALLEGRO_MOUSE_STATE _mouseState;
  15. ALLEGRO_FONT * mainFont(NULL);
  16.  
  17. int _screenW(320);
  18. int _screenH(180);
  19. int _scale(4);
  20.  
  21. int _sx;
  22. int _sy;
  23. int _scaleMax;
  24. int _scaleW;
  25. int _scaleH;
  26. int _scaleX;
  27. int _scaleY;
  28. int _actualMonitorW;
  29. int _actualMonitorH;
  30.  
  31. unsigned const MAX_WINDOW(8);
  32.  
  33. template <class E, class M>
  34. E log (E error, M msg)
  35. {
  36.     std::cout << msg;
  37.     return error;
  38. }
  39.  
  40. class Window
  41. {
  42.     public:
  43.         Window()
  44.         {
  45.         }
  46.         virtual ~Window()
  47.         {
  48.             clearWindows();
  49.             _vecWindowDisplay.clear();
  50.         }
  51.         int init(int w, int h, int scale, const char* title)
  52.         {
  53.             _windowBuffer = al_create_bitmap(w, h);
  54.  
  55.             for (int i(0); i<al_get_num_video_adapters(); i++)
  56.                 addWindow(_screenW,_screenH,_scale,title,i);
  57.  
  58.             setMainWindow(0); // Important Method to call after addWindow !!
  59.  
  60.             return log(0,"- Init Window ! \n");
  61.         }
  62.         int done()
  63.         {
  64.             al_destroy_bitmap(_windowBuffer);
  65.             return log(0,"- Done Window ! \n");
  66.         }
  67.  
  68.         int addWindow(int w, int h, int scale, const char* title, int adapter)
  69.         {
  70.             {
  71.                 ALLEGRO_DISPLAY * display(NULL);
  72.                 al_set_new_display_adapter(adapter);
  73.                 display = al_create_display(w*scale,h*scale);
  74.  
  75.                 if (!display)
  76.                     return log(1,"- Unable to create display ! \n");
  77.  
  78.                 al_set_window_title(display, title);
  79.                 _vecWindowDisplay.push_back(display);
  80.                 log(0, "+ Display created ! \n");
  81.             }
  82.  
  83.             return log(0,"+++ Window Created !\n");
  84.         }
  85.  
  86.         void setMainWindow(int index)
  87.         {
  88.             for(int i(0); i<al_get_num_video_adapters(); i++)
  89.             {
  90.                 if (i != index) delWindow(i);
  91.             }
  92.         }
  93.  
  94.         int setWindow(int index, int w, int h, int scale, const char* title, int adapter)
  95.         {
  96.             {
  97.                 al_set_new_display_adapter(adapter);
  98.                 _vecWindowDisplay[index] = al_create_display(w*scale,h*scale);
  99.  
  100.                 if (!_vecWindowDisplay[index])
  101.                     return log(1,"- Unable to create display ! \n");
  102.  
  103.                 al_set_window_title(_vecWindowDisplay[index], title);
  104.                 log(0, "+ Display created ! \n");
  105.             }
  106.  
  107.             return log(0,"+++ Window["+std::to_string(index)+ "] Modified !\n");
  108.         }
  109.  
  110.         void delWindow(int index)
  111.         {
  112.             if (_vecWindowDisplay[index] != NULL)
  113.             {
  114.                 al_destroy_display(_vecWindowDisplay[index]);
  115.                 log(0,"- delete Display ! \n");
  116.             }
  117.  
  118.             _vecWindowDisplay[index] = NULL;
  119.             log(0,"--- Window["+std::to_string(index)+ "] Deleted !\n");
  120.         }
  121.  
  122.         void clearWindows()
  123.         {
  124.             for (auto & it: _vecWindowDisplay)
  125.             {
  126.                 if (it!=NULL)
  127.                 {
  128.                     al_destroy_display(it);
  129.                     it=NULL;
  130.                     log(1,"- Display Deleted !\n");
  131.                 }
  132.             }
  133.         }
  134.  
  135.         ALLEGRO_DISPLAY * getDisplay(int index)
  136.         {
  137.             if (_vecWindowDisplay[index] != NULL)
  138.                 return log(_vecWindowDisplay[index], "");
  139.             return log<ALLEGRO_DISPLAY*>(NULL,"");
  140.         }
  141.  
  142.         ALLEGRO_BITMAP * getBuffer()
  143.         {
  144.             return _windowBuffer;
  145.         }
  146.  
  147.     protected:
  148.     private:
  149.         std::vector<ALLEGRO_DISPLAY*> _vecWindowDisplay;
  150.         //ALLEGRO_DISPLAY * _windowDisplay;
  151.         ALLEGRO_BITMAP * _windowBuffer;
  152. };
  153.  
  154. void setFullScreen(int screenW, int screenH, int adapter)
  155. {
  156.     ALLEGRO_MONITOR_INFO info;
  157.     al_get_monitor_info(adapter, &info);
  158.     _actualMonitorW = info.x2 - info.x1;
  159.     _actualMonitorH = info.y2 - info.y1;
  160.  
  161.     // calculate scaling factor
  162.     _sx       = _actualMonitorW / screenW;
  163.     _sy       = _actualMonitorH / screenH;
  164.     _scaleMax    = std::min(_sx, _sy);
  165.  
  166.     // calculate how much the windowBuffer should be scaled
  167.     _scaleW = screenW * _scaleMax;
  168.     _scaleH = screenH * _scaleMax;
  169.     _scaleX = (_actualMonitorW - _scaleW) / 2;
  170.     _scaleY = (_actualMonitorH - _scaleH) / 2;
  171. }
  172.  
  173.  
  174. int currentMonitor (ALLEGRO_DISPLAY * display) // Find the current monitor where the window is
  175. {
  176.     for (int i(0); i<al_get_num_video_adapters(); i++)
  177.     {
  178.         ALLEGRO_MONITOR_INFO info;
  179.         al_get_monitor_info(i, &info);
  180.  
  181.         int xWin(0);
  182.         int yWin(0);
  183.  
  184.         al_get_window_position(display, &xWin, &yWin);
  185.  
  186.         if (xWin>=info.x1 &&
  187.                 yWin>=info.y1 &&
  188.                 xWin<info.x2 &&
  189.                 yWin<info.y2)
  190.             return i;
  191.     }
  192.     return 0; // Main/Default monitor
  193. }
  194.  
  195.  
  196. int main()
  197. {
  198.     // --- Init
  199.     if (!al_init())
  200.         return log(1,"- Unable to init Allegro 5 !\n");
  201.  
  202.     if (!al_init_image_addon())
  203.         return log(1,"- Unable to init Addon image !\n");
  204.  
  205.     if (!al_init_primitives_addon())
  206.         return log(1,"- Unable to init Addon primitives !\n");
  207.  
  208.     if (!al_init_font_addon())
  209.         return log(1,"- Unable to init Addon font !\n");
  210.  
  211.     if (!al_init_ttf_addon())
  212.         return log(1,"- Unable to init Addon ttf !\n");
  213.  
  214.     if (!al_install_keyboard())
  215.         return log(2,"- Unable to install Keyboard !\n");
  216.  
  217.     if (!al_install_mouse())
  218.         return log(2,"- Unable to install Keyboard !\n");
  219.  
  220.     mainFont = al_load_font("Kyrou.ttf",8,0);
  221.  
  222.     Window _window;
  223.  
  224.     _window.init(_screenW,_screenH,_scale,"MultiMonitor");
  225.  
  226.     bool _quit(false);
  227.     int _index(0);
  228.  
  229.     bool _isFullScreen(false);
  230.  
  231.     bool _keyFullScreen(false);
  232.     bool _keyTabScreen(false);
  233.  
  234.     float xMouse(0);
  235.     float yMouse(0);
  236.  
  237.     while (!_quit)
  238.     {
  239.  
  240.     // --- Input
  241.  
  242.         // Keyboard !
  243.         al_get_keyboard_state(&_keyState);
  244.  
  245.         if (al_key_down(&_keyState, ALLEGRO_KEY_ESCAPE)) _quit=true;
  246.  
  247.         if (!al_key_down(&_keyState, ALLEGRO_KEY_TAB)) _keyTabScreen = false;
  248.         if (al_key_down(&_keyState, ALLEGRO_KEY_TAB) && !_keyTabScreen)
  249.         {
  250.             _keyTabScreen = true;
  251.  
  252.             _index++;
  253.             if (_index>al_get_num_video_adapters()-1) _index =0;
  254.  
  255.             {
  256.                 _window.clearWindows();
  257.                 _window.setWindow(_index,_screenW,_screenH,_scale,"new Window",_index);
  258.             }
  259.  
  260.             al_set_display_flag(_window.getDisplay(_index), ALLEGRO_FULLSCREEN_WINDOW, _isFullScreen);
  261.             setFullScreen(_screenW,_screenH,_index);
  262.         }
  263.  
  264.  
  265.         if (!al_key_down(&_keyState, ALLEGRO_KEY_F)) _keyFullScreen = false;
  266.         if (al_key_down(&_keyState, ALLEGRO_KEY_F) && !_keyFullScreen)
  267.         {
  268.             _keyFullScreen = true;
  269.             int activeMonitor(currentMonitor(_window.getDisplay(_index)));
  270.  
  271.             if (_index != activeMonitor)
  272.             {
  273.                 _window.delWindow(_index);
  274.                 _index = activeMonitor; // Set index at current Monitor !
  275.                 _window.setWindow(_index,_screenW,_screenH,_scale,"new Window",_index);
  276.             }
  277.  
  278.             al_set_display_flag(_window.getDisplay(_index), ALLEGRO_FULLSCREEN_WINDOW, _isFullScreen = !_isFullScreen);
  279.             setFullScreen(_screenW,_screenH,_index);
  280.         }
  281.  
  282.         al_get_mouse_state(&_mouseState);
  283.  
  284.         // Mouse !
  285.         if (_isFullScreen)
  286.         {
  287.             xMouse = (_mouseState.x-_scaleX)/ float(_actualMonitorW/_screenW);
  288.             yMouse = (_mouseState.y-_scaleY)/ float(_actualMonitorH/_screenH);
  289.         }
  290.         else
  291.         {
  292.             xMouse = _mouseState.x/_scale;
  293.             yMouse = _mouseState.y/_scale;
  294.         }
  295.  
  296.         if (xMouse < 0)        xMouse = 0;
  297.         if (xMouse > _screenW) xMouse = _screenW;
  298.         if (yMouse < 0)        yMouse = 0;
  299.         if (yMouse > _screenH) yMouse = _screenH;
  300.  
  301.     // --- Render
  302.  
  303.         al_set_target_bitmap(_window.getBuffer());
  304.         al_clear_to_color(al_map_rgb(25,30,50));
  305.  
  306.         int _windowX(0);
  307.         int _windowY(0);
  308.  
  309.         al_get_window_position(_window.getDisplay(_index), &_windowX, &_windowY);
  310.  
  311.         int _windowW(al_get_display_width(_window.getDisplay(_index)));
  312.         int _windowH(al_get_display_height(_window.getDisplay(_index)));
  313.  
  314.         al_draw_textf(mainFont, al_map_rgb(205,250,200), 4, 2, 0,
  315.                       "currentMonitor : %i", currentMonitor(_window.getDisplay(_index)));
  316.  
  317.         al_draw_textf(mainFont, al_map_rgb(205,250,200), 4, 12, 0,
  318.                       "_index        : %i", _index);
  319.  
  320.         al_draw_textf(mainFont, al_map_rgb(250,120,50), 4, 36, 0,
  321.                       "Window Pos : %i , %i", _windowX, _windowY);
  322.  
  323.         al_draw_textf(mainFont, al_map_rgb(0,250,250), 4, 46, 0,
  324.                       "Window Size : %i , %i", _windowW, _windowH);
  325.  
  326.         al_draw_textf(mainFont, al_map_rgb(50,150,250), 4, 106, 0,
  327.                       "Mouse Pos : %i , %i", int(xMouse), int(yMouse));
  328.  
  329.         al_draw_line(0,yMouse+.5,
  330.                      _screenW,yMouse+.5,
  331.                      al_map_rgba(55,155,100,25),0);
  332.  
  333.         al_draw_line(xMouse+.5,0,
  334.                      xMouse+.5,_screenH,
  335.                      al_map_rgba(55,155,100,25),0);
  336.  
  337.         al_set_target_backbuffer(_window.getDisplay(_index));
  338.         al_clear_to_color(al_map_rgb(0,0,0));
  339.  
  340.         if (_isFullScreen)
  341.             al_draw_scaled_bitmap(_window.getBuffer(),
  342.                                   0,0,_screenW,_screenH,
  343.                                   _scaleX,_scaleY,_scaleW,_scaleH,
  344.                                   0);
  345.         else
  346.             al_draw_scaled_bitmap(_window.getBuffer(),
  347.                                   0,0,_screenW,_screenH,
  348.                                   0,0,_screenW*_scale,_screenH*_scale,
  349.                                   0);
  350.  
  351.         al_flip_display();
  352.     }
  353.  
  354.     // --- Done
  355.  
  356.     _window.done();
  357.     al_destroy_font(mainFont);
  358.  
  359.     return log(0,"- Program terminated normally !\n");
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement