Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*******************************************************************************
- // main.cpp *********************************************************************
- //*******************************************************************************
- //------------------------------------------------------------------------------ ALLEGRO 5 template
- // By Silver TES 2016
- //------------------------------------------------------------------------------
- #include "TemplateAllegro5.h"
- //------------------------------------------------------------------------------ Init
- int init()
- {
- initWindow(640,360,2);
- //--- INIT ALL HERE !
- return log(0,"- init OK !\n");
- }
- //------------------------------------------------------------------------------ Done
- int done()
- {
- //--- DONE ALL HERE !
- return log(0,"- done OK !\n");
- }
- //------------------------------------------------------------------------------ Update
- void update()
- {
- //--- UPDATE SOMETHING HERE !
- }
- //------------------------------------------------------------------------------ Render
- void render()
- {
- beginRender(_black);
- //--- DRAW SOMETHING HERE !
- endRender(_black);
- }
- //------------------------------------------------------------------------------ Main entry
- int main()
- {
- run();
- return log(0,"Program terminated OK !\n");
- }
- //*******************************************************************************
- // TemplateAllegro5.h ***********************************************************
- //*******************************************************************************
- #ifndef TEMPLATEALLEGRO5_H_INCLUDED
- #define TEMPLATEALLEGRO5_H_INCLUDED
- //------------------------------------------------------------------------------ Major dependances
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_font.h>
- #include <allegro5/allegro_ttf.h>
- #include <allegro5/allegro_primitives.h>
- #include <allegro5/allegro_image.h>
- #include <allegro5/allegro_native_dialog.h>
- #include <allegro5/allegro_audio.h>
- #include <allegro5/allegro_acodec.h>
- #include <allegro5/allegro_windows.h>
- #include <cstdio>
- #include <memory>
- #include <vector>
- #include <iostream>
- #include <functional>
- #include <fstream>
- #include <cstdlib>
- #include <ctime>
- //------------------------------------------------------------------------------ Debug Methods
- template <class E, class M> E log(E error, M msg)
- {
- std::cout << msg;
- return error;
- }
- //------------------------------------------------------------------------------ Globals Var
- bool _quit(false);
- int _screenW(640);
- int _screenH(360);
- int _scaleWin(2);
- ALLEGRO_COLOR _black({0,0,0});
- ALLEGRO_KEYBOARD_STATE _keyState;
- ALLEGRO_DISPLAY * _windowDisplay(NULL);
- ALLEGRO_BITMAP * _windowBuffer(NULL);
- //------------------------------------------------------------------------------ Method declarations
- int init(); // Init all here !
- int done(); // Done all here !
- int initTemplate();
- int doneTemplate();
- int initWindow(int screenW, int screenH, int scaleWin);
- int run(); // Main loop entry !
- void update(); // Update in main loop !
- void render(); // Render in main loop !
- void beginRender(ALLEGRO_COLOR color); // call before begin drawing !
- void endRender(ALLEGRO_COLOR color); // call after end drawing !
- //------------------------------------------------------------------------------ Render methods
- void beginRender(ALLEGRO_COLOR color)
- {
- al_set_target_bitmap(_windowBuffer);
- al_clear_to_color(color);
- }
- void endRender(ALLEGRO_COLOR color)
- {
- al_set_target_backbuffer(_windowDisplay);
- al_clear_to_color(color);
- al_draw_scaled_bitmap(_windowBuffer,
- 0,0,_screenW,_screenH,
- 0,0,_screenW*_scaleWin,_screenH*_scaleWin,
- 0);
- }
- //------------------------------------------------------------------------------ Main loop
- int run()
- {
- if (initTemplate())
- _quit = true;
- if (init())
- _quit = true;
- while(!_quit)
- {
- al_get_keyboard_state(&_keyState);
- if (al_key_down(&_keyState, ALLEGRO_KEY_ESCAPE))
- _quit = true;
- update();
- render();
- al_flip_display();
- }
- if (done())
- return log(1,"- done Program error !\n");
- if (doneTemplate())
- return log(1,"- done Program error !\n");
- return log(0,"- Main loop terminated OK !\n");
- }
- //------------------------------------------------------------------------------ Setup Methods
- int initWindow(int screenW , int screenH, int scaleWin)
- {
- _screenW = screenW;
- _screenH = screenH;
- _scaleWin = scaleWin;
- _windowDisplay = al_create_display(_screenW*_scaleWin,_screenH*_scaleWin);
- if (!_windowDisplay)
- return log(1,"- Unable to create display !\n");
- _windowBuffer = al_create_bitmap(_screenW,_screenH);
- if (!_windowBuffer)
- return log(1,"- Unable to create buffer !\n");
- return log(0,"- Init Window OK !\n");
- }
- int initTemplate()
- {
- // --- Init Allegro 5
- if (!al_init())
- return log(1,"- Unable to init Allegro 5 !\n");
- // --- Install All devices
- if (!al_install_audio())
- return log(1,"- Unable to install Audio ! \n");
- if (!al_install_keyboard())
- return log(1,"- Unable to install Keyboard ! \n");
- if (!al_install_mouse())
- return log(1,"- Unable to install Mouse ! \n");
- if (!al_install_joystick())
- return log(1,"- Unable to install Joystick ! \n");
- // --- Init All addons
- if (!al_init_primitives_addon())
- return log(1,"- Unable to init Primitives Addon ! \n");
- if (!al_init_image_addon())
- return log(1,"- Unable to init Image Addon ! \n");
- if (!al_init_font_addon())
- return log(1,"- Unable to init Font Addon ! \n");
- if (!al_init_ttf_addon())
- return log(1,"- Unable to init TTF Addon ! \n");
- if (!al_init_acodec_addon())
- return log(1,"- Unable to init ACODEC Addon ! \n");
- return log(0,"- init Template OK !\n");
- }
- int doneTemplate()
- {
- if (_windowDisplay) al_destroy_display(_windowDisplay);
- if (_windowBuffer) al_destroy_bitmap(_windowBuffer);
- return log(0,"- done Template OK !\n");
- }
- #endif // TEMPLATEALLEGRO5_H_INCLUDED
Add Comment
Please, Sign In to add comment