SilverTES

Template Allegro 5 Ready to code

Dec 3rd, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.27 KB | None | 0 0
  1. //*******************************************************************************
  2. // main.cpp *********************************************************************
  3. //*******************************************************************************
  4. //------------------------------------------------------------------------------ ALLEGRO 5 template
  5. // By Silver TES 2016
  6. //------------------------------------------------------------------------------
  7. #include "TemplateAllegro5.h"
  8. //------------------------------------------------------------------------------ Init
  9. int init()
  10. {
  11.     initWindow(640,360,2);
  12.     //--- INIT ALL HERE !
  13.  
  14.     return log(0,"- init OK !\n");
  15. }
  16.  
  17. //------------------------------------------------------------------------------ Done
  18. int done()
  19. {
  20.     //--- DONE ALL HERE !
  21.  
  22.     return log(0,"- done OK !\n");
  23. }
  24.  
  25. //------------------------------------------------------------------------------ Update
  26. void update()
  27. {
  28.     //--- UPDATE SOMETHING HERE !
  29.  
  30. }
  31.  
  32. //------------------------------------------------------------------------------ Render
  33. void render()
  34. {
  35.     beginRender(_black);
  36.     //--- DRAW SOMETHING HERE !
  37.  
  38.     endRender(_black);
  39. }
  40.  
  41. //------------------------------------------------------------------------------ Main entry
  42. int main()
  43. {
  44.     run();
  45.     return log(0,"Program terminated OK !\n");
  46. }
  47.  
  48. //*******************************************************************************
  49. // TemplateAllegro5.h ***********************************************************
  50. //*******************************************************************************
  51.  
  52. #ifndef TEMPLATEALLEGRO5_H_INCLUDED
  53. #define TEMPLATEALLEGRO5_H_INCLUDED
  54.  
  55. //------------------------------------------------------------------------------  Major dependances
  56. #include <allegro5/allegro.h>
  57. #include <allegro5/allegro_font.h>
  58. #include <allegro5/allegro_ttf.h>
  59. #include <allegro5/allegro_primitives.h>
  60. #include <allegro5/allegro_image.h>
  61. #include <allegro5/allegro_native_dialog.h>
  62. #include <allegro5/allegro_audio.h>
  63. #include <allegro5/allegro_acodec.h>
  64. #include <allegro5/allegro_windows.h>
  65.  
  66. #include <cstdio>
  67. #include <memory>
  68. #include <vector>
  69.  
  70. #include <iostream>
  71. #include <functional>
  72. #include <fstream>
  73. #include <cstdlib>
  74. #include <ctime>
  75.  
  76. //------------------------------------------------------------------------------ Debug Methods
  77. template <class E, class M> E log(E error, M msg)
  78. {
  79.     std::cout << msg;
  80.     return error;
  81. }
  82.  
  83. //------------------------------------------------------------------------------ Globals Var
  84. bool _quit(false);
  85.  
  86. int _screenW(640);
  87. int _screenH(360);
  88. int _scaleWin(2);
  89.  
  90. ALLEGRO_COLOR _black({0,0,0});
  91.  
  92. ALLEGRO_KEYBOARD_STATE _keyState;
  93. ALLEGRO_DISPLAY * _windowDisplay(NULL);
  94. ALLEGRO_BITMAP * _windowBuffer(NULL);
  95.  
  96. //------------------------------------------------------------------------------ Method declarations
  97. int init(); // Init all here !
  98. int done(); // Done all here !
  99. int initTemplate();
  100. int doneTemplate();
  101. int initWindow(int screenW, int screenH, int scaleWin);
  102.  
  103. int run(); // Main loop entry !
  104. void update(); // Update in main loop !
  105. void render(); // Render in main loop !
  106.  
  107. void beginRender(ALLEGRO_COLOR color); // call before begin drawing !
  108. void endRender(ALLEGRO_COLOR color); // call after end drawing !
  109.  
  110.  
  111. //------------------------------------------------------------------------------ Render methods
  112. void beginRender(ALLEGRO_COLOR color)
  113. {
  114.     al_set_target_bitmap(_windowBuffer);
  115.     al_clear_to_color(color);
  116. }
  117.  
  118. void endRender(ALLEGRO_COLOR color)
  119. {
  120.     al_set_target_backbuffer(_windowDisplay);
  121.     al_clear_to_color(color);
  122.  
  123.     al_draw_scaled_bitmap(_windowBuffer,
  124.                           0,0,_screenW,_screenH,
  125.                           0,0,_screenW*_scaleWin,_screenH*_scaleWin,
  126.                           0);
  127. }
  128.  
  129. //------------------------------------------------------------------------------ Main loop
  130. int run()
  131. {
  132.     if (initTemplate())
  133.         _quit = true;
  134.     if (init())
  135.         _quit = true;
  136.  
  137.     while(!_quit)
  138.     {
  139.         al_get_keyboard_state(&_keyState);
  140.         if (al_key_down(&_keyState, ALLEGRO_KEY_ESCAPE))
  141.         _quit = true;
  142.         update();
  143.         render();
  144.         al_flip_display();
  145.     }
  146.     if (done())
  147.         return log(1,"- done Program error !\n");
  148.  
  149.     if (doneTemplate())
  150.         return log(1,"- done Program error !\n");
  151.  
  152.     return log(0,"- Main loop terminated OK !\n");
  153. }
  154.  
  155. //------------------------------------------------------------------------------ Setup Methods
  156. int initWindow(int screenW , int screenH, int scaleWin)
  157. {
  158.     _screenW = screenW;
  159.     _screenH = screenH;
  160.     _scaleWin = scaleWin;
  161.  
  162.     _windowDisplay = al_create_display(_screenW*_scaleWin,_screenH*_scaleWin);
  163.     if (!_windowDisplay)
  164.         return log(1,"- Unable to create display !\n");
  165.  
  166.     _windowBuffer = al_create_bitmap(_screenW,_screenH);
  167.     if (!_windowBuffer)
  168.         return log(1,"- Unable to create buffer !\n");
  169.  
  170.     return log(0,"- Init Window OK !\n");
  171. }
  172.  
  173. int initTemplate()
  174. {
  175.     // --- Init Allegro 5
  176.     if (!al_init())
  177.         return log(1,"- Unable to init Allegro 5 !\n");
  178.  
  179.     // --- Install All devices
  180.     if (!al_install_audio())
  181.         return log(1,"- Unable to install Audio ! \n");
  182.     if (!al_install_keyboard())
  183.         return log(1,"- Unable to install Keyboard ! \n");
  184.     if (!al_install_mouse())
  185.         return log(1,"- Unable to install Mouse ! \n");
  186.     if (!al_install_joystick())
  187.         return log(1,"- Unable to install Joystick ! \n");
  188.  
  189.     // --- Init All addons
  190.     if (!al_init_primitives_addon())
  191.         return log(1,"- Unable to init Primitives Addon ! \n");
  192.     if (!al_init_image_addon())
  193.         return log(1,"- Unable to init Image Addon ! \n");
  194.     if (!al_init_font_addon())
  195.         return log(1,"- Unable to init Font Addon ! \n");
  196.     if (!al_init_ttf_addon())
  197.         return log(1,"- Unable to init TTF Addon ! \n");
  198.     if (!al_init_acodec_addon())
  199.         return log(1,"- Unable to init ACODEC Addon ! \n");
  200.  
  201.     return log(0,"- init Template OK !\n");
  202. }
  203.  
  204. int doneTemplate()
  205. {
  206.     if (_windowDisplay) al_destroy_display(_windowDisplay);
  207.     if (_windowBuffer) al_destroy_bitmap(_windowBuffer);
  208.  
  209.     return log(0,"- done Template OK !\n");
  210. }
  211.  
  212.  
  213. #endif // TEMPLATEALLEGRO5_H_INCLUDED
Add Comment
Please, Sign In to add comment