Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <memory>
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_image.h>
- #include <allegro5/allegro_primitives.h>
- #include <allegro5/allegro_windows.h>
- #include <allegro5/allegro_ttf.h>
- #include <allegro5/allegro_font.h>
- #include <allegro5/allegro_windows.h>
- ALLEGRO_KEYBOARD_STATE _keyState;
- ALLEGRO_MOUSE_STATE _mouseState;
- ALLEGRO_FONT * mainFont(NULL);
- int _screenW(320);
- int _screenH(180);
- int _scale(4);
- int _sx;
- int _sy;
- int _scaleMax;
- int _scaleW;
- int _scaleH;
- int _scaleX;
- int _scaleY;
- int _actualMonitorW;
- int _actualMonitorH;
- unsigned const MAX_WINDOW(8);
- template <class E, class M>
- E log (E error, M msg)
- {
- std::cout << msg;
- return error;
- }
- class Window
- {
- public:
- Window()
- {
- }
- virtual ~Window()
- {
- clearWindows();
- _vecWindowDisplay.clear();
- }
- int init(int w, int h, int scale, const char* title)
- {
- _windowBuffer = al_create_bitmap(w, h);
- for (int i(0); i<al_get_num_video_adapters(); i++)
- addWindow(_screenW,_screenH,_scale,title,i);
- setMainWindow(0); // Important Method to call after addWindow !!
- return log(0,"- Init Window ! \n");
- }
- int done()
- {
- al_destroy_bitmap(_windowBuffer);
- return log(0,"- Done Window ! \n");
- }
- int addWindow(int w, int h, int scale, const char* title, int adapter)
- {
- {
- ALLEGRO_DISPLAY * display(NULL);
- al_set_new_display_adapter(adapter);
- display = al_create_display(w*scale,h*scale);
- if (!display)
- return log(1,"- Unable to create display ! \n");
- al_set_window_title(display, title);
- _vecWindowDisplay.push_back(display);
- log(0, "+ Display created ! \n");
- }
- return log(0,"+++ Window Created !\n");
- }
- void setMainWindow(int index)
- {
- for(int i(0); i<al_get_num_video_adapters(); i++)
- {
- if (i != index) delWindow(i);
- }
- }
- int setWindow(int index, int w, int h, int scale, const char* title, int adapter)
- {
- {
- al_set_new_display_adapter(adapter);
- _vecWindowDisplay[index] = al_create_display(w*scale,h*scale);
- if (!_vecWindowDisplay[index])
- return log(1,"- Unable to create display ! \n");
- al_set_window_title(_vecWindowDisplay[index], title);
- log(0, "+ Display created ! \n");
- }
- return log(0,"+++ Window["+std::to_string(index)+ "] Modified !\n");
- }
- void delWindow(int index)
- {
- if (_vecWindowDisplay[index] != NULL)
- {
- al_destroy_display(_vecWindowDisplay[index]);
- log(0,"- delete Display ! \n");
- }
- _vecWindowDisplay[index] = NULL;
- log(0,"--- Window["+std::to_string(index)+ "] Deleted !\n");
- }
- void clearWindows()
- {
- for (auto & it: _vecWindowDisplay)
- {
- if (it!=NULL)
- {
- al_destroy_display(it);
- it=NULL;
- log(1,"- Display Deleted !\n");
- }
- }
- }
- ALLEGRO_DISPLAY * getDisplay(int index)
- {
- if (_vecWindowDisplay[index] != NULL)
- return log(_vecWindowDisplay[index], "");
- return log<ALLEGRO_DISPLAY*>(NULL,"");
- }
- ALLEGRO_BITMAP * getBuffer()
- {
- return _windowBuffer;
- }
- protected:
- private:
- std::vector<ALLEGRO_DISPLAY*> _vecWindowDisplay;
- //ALLEGRO_DISPLAY * _windowDisplay;
- ALLEGRO_BITMAP * _windowBuffer;
- };
- void setFullScreen(int screenW, int screenH, int adapter)
- {
- ALLEGRO_MONITOR_INFO info;
- al_get_monitor_info(adapter, &info);
- _actualMonitorW = info.x2 - info.x1;
- _actualMonitorH = info.y2 - info.y1;
- // calculate scaling factor
- _sx = _actualMonitorW / screenW;
- _sy = _actualMonitorH / screenH;
- _scaleMax = std::min(_sx, _sy);
- // calculate how much the windowBuffer should be scaled
- _scaleW = screenW * _scaleMax;
- _scaleH = screenH * _scaleMax;
- _scaleX = (_actualMonitorW - _scaleW) / 2;
- _scaleY = (_actualMonitorH - _scaleH) / 2;
- }
- int currentMonitor (ALLEGRO_DISPLAY * display) // Find the current monitor where the window is
- {
- for (int i(0); i<al_get_num_video_adapters(); i++)
- {
- ALLEGRO_MONITOR_INFO info;
- al_get_monitor_info(i, &info);
- int xWin(0);
- int yWin(0);
- al_get_window_position(display, &xWin, &yWin);
- if (xWin>=info.x1 &&
- yWin>=info.y1 &&
- xWin<info.x2 &&
- yWin<info.y2)
- return i;
- }
- return 0; // Main/Default monitor
- }
- int main()
- {
- // --- Init
- if (!al_init())
- return log(1,"- Unable to init Allegro 5 !\n");
- if (!al_init_image_addon())
- return log(1,"- Unable to init Addon image !\n");
- if (!al_init_primitives_addon())
- return log(1,"- Unable to init Addon primitives !\n");
- if (!al_init_font_addon())
- return log(1,"- Unable to init Addon font !\n");
- if (!al_init_ttf_addon())
- return log(1,"- Unable to init Addon ttf !\n");
- if (!al_install_keyboard())
- return log(2,"- Unable to install Keyboard !\n");
- if (!al_install_mouse())
- return log(2,"- Unable to install Keyboard !\n");
- mainFont = al_load_font("Kyrou.ttf",8,0);
- Window _window;
- _window.init(_screenW,_screenH,_scale,"MultiMonitor");
- bool _quit(false);
- int _index(0);
- bool _isFullScreen(false);
- bool _keyFullScreen(false);
- bool _keyTabScreen(false);
- float xMouse(0);
- float yMouse(0);
- while (!_quit)
- {
- // --- Input
- // Keyboard !
- al_get_keyboard_state(&_keyState);
- if (al_key_down(&_keyState, ALLEGRO_KEY_ESCAPE)) _quit=true;
- if (!al_key_down(&_keyState, ALLEGRO_KEY_TAB)) _keyTabScreen = false;
- if (al_key_down(&_keyState, ALLEGRO_KEY_TAB) && !_keyTabScreen)
- {
- _keyTabScreen = true;
- _index++;
- if (_index>al_get_num_video_adapters()-1) _index =0;
- {
- _window.clearWindows();
- _window.setWindow(_index,_screenW,_screenH,_scale,"new Window",_index);
- }
- al_set_display_flag(_window.getDisplay(_index), ALLEGRO_FULLSCREEN_WINDOW, _isFullScreen);
- setFullScreen(_screenW,_screenH,_index);
- }
- if (!al_key_down(&_keyState, ALLEGRO_KEY_F)) _keyFullScreen = false;
- if (al_key_down(&_keyState, ALLEGRO_KEY_F) && !_keyFullScreen)
- {
- _keyFullScreen = true;
- int activeMonitor(currentMonitor(_window.getDisplay(_index)));
- if (_index != activeMonitor)
- {
- _window.delWindow(_index);
- _index = activeMonitor; // Set index at current Monitor !
- _window.setWindow(_index,_screenW,_screenH,_scale,"new Window",_index);
- }
- al_set_display_flag(_window.getDisplay(_index), ALLEGRO_FULLSCREEN_WINDOW, _isFullScreen = !_isFullScreen);
- setFullScreen(_screenW,_screenH,_index);
- }
- al_get_mouse_state(&_mouseState);
- // Mouse !
- if (_isFullScreen)
- {
- xMouse = (_mouseState.x-_scaleX)/ float(_actualMonitorW/_screenW);
- yMouse = (_mouseState.y-_scaleY)/ float(_actualMonitorH/_screenH);
- }
- else
- {
- xMouse = _mouseState.x/_scale;
- yMouse = _mouseState.y/_scale;
- }
- if (xMouse < 0) xMouse = 0;
- if (xMouse > _screenW) xMouse = _screenW;
- if (yMouse < 0) yMouse = 0;
- if (yMouse > _screenH) yMouse = _screenH;
- // --- Render
- al_set_target_bitmap(_window.getBuffer());
- al_clear_to_color(al_map_rgb(25,30,50));
- int _windowX(0);
- int _windowY(0);
- al_get_window_position(_window.getDisplay(_index), &_windowX, &_windowY);
- int _windowW(al_get_display_width(_window.getDisplay(_index)));
- int _windowH(al_get_display_height(_window.getDisplay(_index)));
- al_draw_textf(mainFont, al_map_rgb(205,250,200), 4, 2, 0,
- "currentMonitor : %i", currentMonitor(_window.getDisplay(_index)));
- al_draw_textf(mainFont, al_map_rgb(205,250,200), 4, 12, 0,
- "_index : %i", _index);
- al_draw_textf(mainFont, al_map_rgb(250,120,50), 4, 36, 0,
- "Window Pos : %i , %i", _windowX, _windowY);
- al_draw_textf(mainFont, al_map_rgb(0,250,250), 4, 46, 0,
- "Window Size : %i , %i", _windowW, _windowH);
- al_draw_textf(mainFont, al_map_rgb(50,150,250), 4, 106, 0,
- "Mouse Pos : %i , %i", int(xMouse), int(yMouse));
- al_draw_line(0,yMouse+.5,
- _screenW,yMouse+.5,
- al_map_rgba(55,155,100,25),0);
- al_draw_line(xMouse+.5,0,
- xMouse+.5,_screenH,
- al_map_rgba(55,155,100,25),0);
- al_set_target_backbuffer(_window.getDisplay(_index));
- al_clear_to_color(al_map_rgb(0,0,0));
- if (_isFullScreen)
- al_draw_scaled_bitmap(_window.getBuffer(),
- 0,0,_screenW,_screenH,
- _scaleX,_scaleY,_scaleW,_scaleH,
- 0);
- else
- al_draw_scaled_bitmap(_window.getBuffer(),
- 0,0,_screenW,_screenH,
- 0,0,_screenW*_scale,_screenH*_scale,
- 0);
- al_flip_display();
- }
- // --- Done
- _window.done();
- al_destroy_font(mainFont);
- return log(0,"- Program terminated normally !\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement