Advertisement
SilverTES

MugEngine Proto

Apr 28th, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.53 KB | None | 0 0
  1. //-------------------------------------------------------------------------------------------------------------------------------------
  2. // Setup : CodeBlocks 16.01 + Allegro 5.0.10 + gcc 4.9.2 tdm
  3. //-------------------------------------------------------------------------------------------------------------------------------------
  4. -static -lstdc++
  5. -static -lgcc
  6. -lboost_system-mgw47-mt-1_58
  7. -lboost_filesystem-mgw47-mt-1_58
  8. -lallegro-5.0.10-monolith-static-mt
  9. -lFLAC-1.2.1-static-mt
  10. -lfreetype-2.4.8-static-mt
  11. -lallegro_acodec-5.0.10-static-mt
  12. -lallegro_audio-5.0.10-static-mt
  13. -lvorbisfile-1.3.2-static-mt
  14. -lvorbis-1.3.2-static-mt
  15. -ldumb-0.9.3-static-mt
  16. -logg-1.2.1-static-mt
  17. -lopenal-1.14-static-mt
  18. -lwinmm
  19. -lgdi32
  20. -luser32
  21. -lkernel32
  22. -lopengl32
  23. -ldxguid
  24. -ldinput8
  25. -ld3d9
  26. -lpsapi
  27. -lpng
  28. -ljpeg
  29. -lz
  30. -lole32
  31. -ld3dx9d
  32. -ld3dim
  33. -ld3drm
  34. -lgdiplus
  35. -ld3dxof
  36. -lws2_32
  37. -lcomdlg32
  38. -lshlwapi
  39. -lstdc++
  40. -luuid
  41.  
  42. //-------------------------------------------------------------------------------------------------------------------------------------
  43. // origin.h
  44. //-------------------------------------------------------------------------------------------------------------------------------------
  45. #ifndef ORIGIN_H
  46. #define ORIGIN_H
  47. //{
  48. #include <allegro5/allegro.h>
  49. #include <allegro5/allegro_font.h>
  50. #include <allegro5/allegro_ttf.h>
  51. #include <allegro5/allegro_primitives.h>
  52. #include <allegro5/allegro_image.h>
  53. #include <allegro5/allegro_native_dialog.h>
  54. #include <allegro5/allegro_audio.h>
  55. #include <allegro5/allegro_acodec.h>
  56. #include <allegro5/allegro_windows.h>
  57.  
  58. #include <stdio.h>
  59. #include <cmath>
  60. #include <iostream>
  61. #include <vector>
  62.  
  63. #include <boost/filesystem/operations.hpp>
  64. #include <ctime>
  65.  
  66. #define IDM_FILE_NEW 1
  67. #define IDM_FILE_OPEN 2
  68. #define IDM_FILE_QUIT 3
  69. //}
  70. class Origin
  71. {
  72.     private:
  73.  
  74.         // Quit application
  75.         bool quit;
  76.  
  77.         // Configuration file
  78.         ALLEGRO_CONFIG* mainConfig;
  79.         std::time_t mainConfigTime;
  80.         std::time_t mainConfigLastTime;
  81.  
  82.         // Screen
  83.         int monitor;
  84.         int screenH;
  85.         int screenW;
  86.         int actualMonitor;
  87.         int nbAdapter;
  88.         bool fullScreen;
  89.  
  90.         // Window
  91.         int actualMonitorW;
  92.         int actualMonitorH;
  93.         bool stretchScreen;
  94.         bool antiAliasing;
  95.         ALLEGRO_DISPLAY* windowDisplay;
  96.         ALLEGRO_BITMAP* windowBuffer;
  97.  
  98.         // Events
  99.         ALLEGRO_EVENT_QUEUE* eventQueue;
  100.  
  101.         // Framerate control
  102.         const float FPS;
  103.         int nbFrame;
  104.         int nbFps;
  105.         double gameTime;
  106.         double oldTime;
  107.         ALLEGRO_TIMER* frameRateTimer;
  108.  
  109.         // Mouse position
  110.         int mouseXi;
  111.         int mouseYi;
  112.         float mouseXf;
  113.         float mouseYf;
  114.  
  115.         // Keyboard button on/off
  116.         bool keyAlt;
  117.         bool keyEnter;
  118.         bool keyCtrl;
  119.         bool keyAltEnter;
  120.         ALLEGRO_KEYBOARD_STATE keyState;
  121.  
  122.         // Resources
  123.  
  124.         ALLEGRO_TRANSFORM trans;
  125.         ALLEGRO_FONT* mainFont;
  126.  
  127. //        ALLEGRO_BITMAP * myBmp;
  128. //        ALLEGRO_BITMAP * myBrik;
  129. //        ALLEGRO_BITMAP * mySol;
  130. //        ALLEGRO_BITMAP * pause_img;
  131. //        ALLEGRO_THREAD *thread;
  132.     public:
  133.  
  134.         //DATA myInput;
  135.         Origin();
  136.         virtual ~Origin();
  137.  
  138.         int init();
  139.         int done();
  140.         int run();
  141.         int update();
  142.         int input();
  143.         int render();
  144.         int refresh();
  145.         int load();
  146.         int save();
  147.  
  148.         int calc_framerate();
  149.         int switch_fullScreen(bool isFullscreen);
  150.  
  151.         void get_desktop_resolution(int adapter, int &w, int &h);
  152.  
  153.     protected:
  154. };
  155. #endif // ORIGIN_H
  156. //-------------------------------------------------------------------------------------------------------------------------------------
  157. // origin.cpp
  158. //-------------------------------------------------------------------------------------------------------------------------------------
  159. #include "origin.h"
  160.  
  161. Origin::Origin(): FPS(60)
  162. {
  163.     std::cout << "Origin::Origin // Init members !" << std::endl;
  164.  
  165.     // Quit application
  166.     quit = false;
  167.  
  168.     // Quit application
  169.     mainConfigTime = 0;
  170.     mainConfigLastTime = 0;
  171.     mainConfig = NULL;
  172.  
  173.     // Screen
  174.     monitor = 0;
  175.     screenW = 0;
  176.     screenH = 0;
  177.     actualMonitor = 0;
  178.     nbAdapter = 0;
  179.     fullScreen = false;
  180.  
  181.     // Window
  182.     actualMonitorW = 0;
  183.     actualMonitorH = 0;
  184.     stretchScreen = false;
  185.     antiAliasing = false;
  186.     windowDisplay = NULL;
  187.     windowBuffer = NULL;
  188.  
  189.     // Events
  190.     eventQueue = NULL;
  191.  
  192.     // Framerate control
  193.     nbFrame = 0;
  194.     nbFps = 0;
  195.     gameTime = 0;
  196.     oldTime = 0;
  197.     frameRateTimer = NULL;
  198.  
  199.     // Mouse position
  200.     mouseXi = 0;
  201.     mouseYi = 0;
  202.     mouseXf = 0;
  203.     mouseYf = 0;
  204.  
  205.     // Keyboard button on/off
  206.     keyAlt = false;
  207.     keyEnter = false;
  208.     keyCtrl = false;
  209.     keyAltEnter = false;
  210.  
  211.     // Resources
  212.  
  213.     mainFont = NULL;
  214.  
  215. //    myBmp = NULL;
  216. //    myBrik = NULL;
  217. //    mySol = NULL;
  218. //    pause_img = NULL;
  219. //    thread = NULL;
  220. }
  221.  
  222. Origin::~Origin()
  223. {
  224.     std::cout << "Origin::~Origin // Quit application !" << std::endl;
  225. }
  226.  
  227. int Origin::init()
  228. {
  229.     std::cout << "Origin::init" << std::endl;
  230.  
  231.     try
  232.     {
  233.         al_set_app_name("Mugen SoftWare");
  234.  
  235.         if (!al_init()) throw 1;
  236.         if (!al_install_audio()) throw 2;
  237.         if (!al_init_acodec_addon()) throw 3;
  238.  
  239.         // nombre de son simultannée dans le "Voice" par défaut  [default mixer]
  240.         if (!al_reserve_samples(32)) throw 4;
  241.  
  242.         mainConfig = al_load_config_file ("config.ini");
  243.  
  244.         stretchScreen = atoi(al_get_config_value(mainConfig, "Display", "stretchScreen"));
  245.         antiAliasing = atoi(al_get_config_value(mainConfig, "Display", "antialiasing"));
  246.  
  247.         monitor = atoi(al_get_config_value(mainConfig, "Display", "monitor"));
  248.         screenW = atoi(al_get_config_value(mainConfig, "Display", "size_w"));
  249.         screenH = atoi(al_get_config_value(mainConfig, "Display", "size_h"));
  250.  
  251.         al_set_new_display_adapter(monitor);
  252.         al_set_new_display_option(ALLEGRO_VSYNC, 2, ALLEGRO_SUGGEST);
  253.        //al_set_new_display_flags(ALLEGRO_OPENGL);
  254.  
  255.         if (antiAliasing)
  256.             al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1,
  257.                                       ALLEGRO_SUGGEST |
  258.                                       ALLEGRO_FULLSCREEN);
  259.  
  260.         windowDisplay = al_create_display(screenW, screenH);
  261.  
  262.         if(!windowDisplay) throw 5;
  263.  
  264.         //--- Init Addons ---
  265.         al_init_font_addon();
  266.         al_init_ttf_addon();
  267.         al_init_image_addon();
  268.         al_init_primitives_addon();
  269.  
  270.         al_install_keyboard();
  271.         al_install_mouse();
  272.         al_hide_mouse_cursor(windowDisplay);
  273.  
  274.         eventQueue = al_create_event_queue();
  275.  
  276.         frameRateTimer = al_create_timer(1.00/FPS);
  277.         al_register_event_source (eventQueue,al_get_timer_event_source(frameRateTimer));
  278.         al_start_timer(frameRateTimer);
  279.  
  280.         if (Origin::load()) throw 6;
  281.     }
  282.  
  283.     catch (int error)
  284.     {
  285.         switch (error)
  286.         {
  287.         case 1:
  288.         {
  289.             std::cout << "-- Error at Origin::init() --" <<
  290.                       "// al_init : failed to initializing Allegro 5!" << std::endl;
  291.             return -1;
  292.         }
  293.         case 2:
  294.         {
  295.             std::cout << "-- Error at Origin::init() --" <<
  296.                       "// al_init : failed to initialize Audio!" << std::endl;
  297.             return -1;
  298.         }
  299.         case 3:
  300.         {
  301.             std::cout << "-- Error at Origin::init() --" <<
  302.                       "// al_init : failed to initialize audio codecs!" << std::endl;
  303.             return -1;
  304.         }
  305.         case 4:
  306.         {
  307.             std::cout << "-- Error at Origin::init() --" <<
  308.                       "// al_init : failed to reserve samples!" << std::endl;
  309.             return -1;
  310.         }
  311.         case 5:
  312.         {
  313.             std::cout << "-- Error at Origin::init() --" <<
  314.                       "// al_init : failed to create windowDisplay!" << std::endl;
  315.             return -1;
  316.         }
  317.         case 6:
  318.         {
  319.             std::cout << "-- Error at Origin::init() --" <<
  320.                       "// al_init : failed to load resources!" << std::endl;
  321.             return -1;
  322.         }
  323.         }
  324.     }
  325.  
  326.     return 0;
  327. }
  328.  
  329. int Origin::done()
  330. {
  331.     std::cout << "Origin::done" << std::endl;
  332.  
  333.     al_destroy_font(mainFont);
  334.     al_destroy_event_queue(eventQueue);
  335.     al_destroy_timer(frameRateTimer);
  336.     al_destroy_display(windowDisplay);
  337.     al_destroy_config(mainConfig);
  338.  
  339.     return 0;
  340. }
  341.  
  342. int Origin::load()
  343. {
  344.     std::cout << "Origin::load" << std::endl;
  345.  
  346.     mainFont = al_load_font (al_get_config_value(mainConfig, "Display", "font"),
  347.                              atoi(al_get_config_value (mainConfig, "Display", "font_size")),0);
  348.     if (!mainFont)
  349.     {
  350.         printf("Error loading mainFont \n");
  351.         return -2;
  352.     }
  353.  
  354.     actualMonitor = al_get_new_display_adapter();
  355.     nbAdapter = al_get_num_video_adapters();
  356.  
  357.     get_desktop_resolution(actualMonitor, actualMonitorW, actualMonitorH);
  358.     windowBuffer = al_create_bitmap(actualMonitorW,actualMonitorH);
  359.  
  360.     mainConfigTime = boost::filesystem::last_write_time("config.ini");
  361.     mainConfigLastTime = boost::filesystem::last_write_time("config.ini");
  362.  
  363.     return 0;
  364. }
  365.  
  366. int Origin::save()
  367. {
  368.     return 0;
  369. }
  370.  
  371. int Origin::run()
  372. {
  373.     std::cout << "Origin::run" << std::endl;
  374.     while (!quit)
  375.     {
  376.         Origin::input();
  377.         Origin::update();
  378.         Origin::calc_framerate();
  379.         Origin::render();
  380.     }
  381.     return 0;
  382. }
  383.  
  384. int Origin::input()
  385. {
  386.     int xWindow(0);  // Window x position
  387.     int yWindow(0);  // Window y position
  388.     al_get_window_position (windowDisplay, &xWindow, &yWindow);
  389.  
  390.     al_get_mouse_cursor_position(&mouseXi, &mouseYi);
  391.  
  392.     if (fullScreen)
  393.     {
  394.         mouseXf = float(mouseXi-xWindow) / (float(actualMonitorW)/float(screenW));
  395.         mouseYf = float(mouseYi-yWindow) / (float(actualMonitorH)/float(screenH));
  396.     }
  397.     else
  398.     {
  399.         mouseXf = float(mouseXi-xWindow)-2;
  400.         mouseYf = float(mouseYi-yWindow)-24;
  401.     }
  402.  
  403.     // Mouse limit zone !
  404.     if (mouseXf>screenW) mouseXf = screenW;
  405.     if (mouseXf<0) mouseXf = 0;
  406.     if (mouseYf>screenH) mouseYf = screenH;
  407.     if (mouseYf<0) mouseYf = 0;
  408.  
  409.     mouseXi = (int)mouseXf;
  410.     mouseYi = (int)mouseYf;
  411.  
  412.     ALLEGRO_EVENT event;
  413.     al_wait_for_event(eventQueue, &event);
  414.     al_get_keyboard_state(&keyState);
  415.  
  416.     if (al_key_down(&keyState, ALLEGRO_KEY_ESCAPE))
  417.     {
  418.         quit = true;
  419.         //myInput.quit = true;
  420.     }
  421.  
  422.     if (al_key_down(&keyState, ALLEGRO_KEY_F5))
  423.     {
  424.         al_destroy_config(mainConfig);
  425.         mainConfig = al_load_config_file ("config.ini");
  426.         mainFont = al_load_font (al_get_config_value(mainConfig, "Display", "font"),
  427.                                  atoi(al_get_config_value (mainConfig, "Display", "font_size")),0);
  428.         if (!mainFont)
  429.         {
  430.             printf("Error loading mainFont \n");
  431.             return -2;
  432.         }
  433.     }
  434.  
  435.     keyAlt = al_key_down(&keyState, ALLEGRO_KEY_ALT);
  436.     keyEnter = al_key_down(&keyState, ALLEGRO_KEY_ENTER);
  437.  
  438.     if (!keyAlt && !keyEnter) keyAltEnter = false;
  439.  
  440.     if (keyAlt && keyEnter && !keyAltEnter)
  441.     {
  442.         keyAltEnter = true;
  443.         switch_fullScreen(fullScreen);
  444.     }
  445.     return 0;
  446. }
  447.  
  448. int Origin::update()
  449. {
  450.     mainConfigTime = boost::filesystem::last_write_time("config.ini");
  451.  
  452.     if (mainConfigTime != mainConfigLastTime)
  453.     {
  454.         al_destroy_config(mainConfig);
  455.         mainConfig = al_load_config_file ("config.ini");
  456.         mainFont = al_load_font (al_get_config_value(mainConfig, "Display", "font"),
  457.                                  atoi(al_get_config_value (mainConfig, "Display", "font_size")),0);
  458.         if (!mainFont)
  459.         {
  460.             printf("Error loading mainFont \n");
  461.             return -2;
  462.         }
  463.         mainConfigLastTime = mainConfigTime;
  464.     }
  465.     return 0;
  466. }
  467.  
  468. int Origin::calc_framerate()
  469. {
  470.     gameTime = al_get_time();
  471.     if (gameTime - oldTime >= 1.0)
  472.     {
  473.         nbFps = nbFrame / (gameTime - oldTime) ;
  474.         nbFrame = 1;
  475.         oldTime = gameTime;
  476.     }
  477.     nbFrame ++;
  478.     return 0;
  479. }
  480.  
  481. int Origin::render()
  482. {
  483.     al_set_target_bitmap(windowBuffer);
  484.     al_clear_to_color(al_map_rgb(0,62,84));
  485.  
  486.     // Affichage Texte !
  487.     al_draw_textf(mainFont, al_map_rgb(200,250,250), screenW-60, 10, -1, "FPS: %i", nbFps); // -1 en parametre pour centrer le texte !
  488.     al_draw_text (mainFont, al_map_rgb(155,255,0), 2, screenH-16, 0, "[ Escape ] -> Quit");
  489.     al_draw_text (mainFont, al_map_rgb(255,125,0), 160, screenH-16, 0, "[ Alt+Enter ] -> Switch FullScreen/Windowed");
  490.     // Viseur !
  491.     al_draw_line(mouseXi,0,mouseXi,screenH, al_map_rgb(80, 140, 200), 0);
  492.     al_draw_line(0, mouseYi,screenW, mouseYi, al_map_rgb(80, 140, 200), 0);
  493.     // Dessin du curseur !
  494.     al_draw_line(mouseXi,mouseYi,mouseXi,mouseYi+16, al_map_rgb(200,150,50), 0);
  495.     al_draw_line(mouseXi,mouseYi,mouseXi+16,mouseYi+16, al_map_rgb(200,150,50), 0);
  496.     al_draw_line(mouseXi+4,mouseYi+10,mouseXi+16,mouseYi+16, al_map_rgb(200,150,50), 0);
  497.     al_draw_line(mouseXi+4,mouseYi+10,mouseXi,mouseYi+16, al_map_rgb(200,150,50), 0);
  498.  
  499.     al_set_target_backbuffer(windowDisplay);
  500.     al_clear_to_color(al_map_rgb(0, 0, 0));
  501.  
  502.     Origin::refresh();
  503.  
  504.     return 0;
  505. }
  506.  
  507. int Origin::refresh()
  508. {
  509.     get_desktop_resolution(actualMonitor, actualMonitorW, actualMonitorH);
  510.  
  511.     // calculate scaling factor
  512.     int sx       = actualMonitorW / screenW;
  513.     int sy       = actualMonitorH / screenH;
  514.     int scale    = std::min(sx, sy);
  515.  
  516.     // calculate how much the windowBuffer should be scaled
  517.     float scaleW = screenW * scale;
  518.     float scaleH = screenH * scale;
  519.     float scaleX = (actualMonitorW - scaleW) / 2;
  520.     float scaleY = (actualMonitorH - scaleH) / 2;
  521.  
  522.     if (fullScreen)
  523.         al_draw_scaled_bitmap(windowBuffer, 0, 0, screenW, screenH,
  524.                               scaleX, scaleY, scaleW, scaleH, 0);
  525.     else
  526.         al_draw_scaled_bitmap(windowBuffer, 0, 0, screenW, screenH,
  527.                               1, 1, screenW, screenH, 0);
  528.  
  529.     al_flip_display();
  530.  
  531.     return 0;
  532. }
  533.  
  534. int Origin::switch_fullScreen(bool isFullscreen)
  535. {
  536.     if (!isFullscreen)
  537.     {
  538.         std::cout << "Origin::switch_fullScreen // Fullscreen Mode !" << std::endl;
  539.         al_toggle_display_flag(windowDisplay, ALLEGRO_FULLSCREEN_WINDOW, true);
  540.         fullScreen = true;
  541.     }
  542.     else
  543.     {
  544.         std::cout << "Origin::switch_fullScreen // Windowed Mode !" << std::endl;
  545.         al_toggle_display_flag(windowDisplay, ALLEGRO_FULLSCREEN_WINDOW, false);
  546.         al_identity_transform(&trans);
  547.         al_scale_transform(&trans, 1, 1);
  548.         al_use_transform(&trans);
  549.         fullScreen = false;
  550.     }
  551.     return 0;
  552. }
  553.  
  554. void Origin::get_desktop_resolution(int adapter, int &w, int &h)
  555. {
  556.     ALLEGRO_MONITOR_INFO info;
  557.     al_get_monitor_info(adapter, &info);
  558.     w = info.x2 - info.x1;
  559.     h = info.y2 - info.y1;
  560. }
  561. //-------------------------------------------------------------------------------------------------------------------------------------
  562. // main.cpp
  563. //-------------------------------------------------------------------------------------------------------------------------------------
  564. #include "origin.h"
  565.  
  566. int main()
  567. {
  568.     Origin myGame;
  569.  
  570.     try
  571.     {
  572.         if (myGame.init()) throw 1;
  573.         if (myGame.run()) throw 2;
  574.         if (myGame.done()) throw 3;
  575.     }
  576.  
  577.     catch(int error)
  578.     {
  579.         switch (error)
  580.         {
  581.             case 1:
  582.             {
  583.                 std::cout << "-- Error at Origin::init() --" << std::endl;
  584.             }
  585.             case 2:
  586.             {
  587.                 std::cout << "-- Error at Origin::run() --" << std::endl;
  588.             }
  589.             case 3:
  590.             {
  591.                 std::cout << "-- Error at Origin::done() --" << std::endl;
  592.             }
  593.         }
  594.     }
  595.  
  596.     return 0;
  597. }
  598. //-------------------------------------------------------------------------------------------------------------------------------------
  599. // config.ini
  600. //-------------------------------------------------------------------------------------------------------------------------------------
  601. #--- Configuration File Allegro 5 Base
  602. [Display]
  603. monitor = 0      // Choose the monitor where the game will be windowDisplay
  604. size_w  = 640
  605. size_h  = 480
  606. stretchScreen = 0
  607. antialiasing = 0
  608. framerate = 60
  609. font_size = 8
  610. font = kyrou.ttf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement