Advertisement
SilverTES

Test Retro2D : C++

Jun 28th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.87 KB | None | 0 0
  1. #include "Retro2D.h"
  2.  
  3. namespace Retro2D
  4. {
  5.     class Game1 : public Game
  6.     {
  7.         #pragma region Vars
  8.  
  9.         int _screenW = 640;
  10.         int _screenH = 360;
  11.  
  12.         bool _pause = false;
  13.  
  14.         Json _myJson;
  15.  
  16.         NODE _area;
  17.  
  18.         ALLEGRO_FONT* _mainFont = nullptr;
  19.  
  20.         ALLEGRO_SAMPLE* _mySample = nullptr;
  21.  
  22.         #pragma endregion
  23.  
  24.         int init() override
  25.         {
  26.             #pragma region Window Setup
  27.             setup
  28.             (
  29.                 "DEMO RETRO2D",     // Window Title
  30.                 Api::OPENGL,        // OpenGL API
  31.                 _screenW, _screenH, // Screen Size
  32.                 2, 0,               // ScaleFull , ScaleWin
  33.                 false,              // isFullscreen
  34.                 false,              // isVsync
  35.                 false,              // isSmooth
  36.                 60,                 // limitFPS
  37.                 true                // isLimitFPS  
  38.             );
  39.             #pragma endregion
  40.  
  41.             _mainFont = al_load_font("data/FFFGE___.TTF",8,0);
  42.             _mySample = al_load_sample("data/bubble1.wav");
  43.  
  44.             SCRIPT demo1Update = CODE{
  45.                
  46.                 ON_FRAME(20)
  47.                 {
  48.                     PLAY_AT(0);
  49.                 }
  50.  
  51.                 IS_PLAY()
  52.                 {
  53.                     _Y = Easing::value(Easing::backEaseOut, _CURFRAME, -10, 10, 20);
  54.                     _X = _Y;
  55.                 }
  56.  
  57.             };
  58.  
  59.             _node["demo1"] = {
  60.  
  61.                 MAKE_NODE("demo1")
  62.                 ->appendTo(_root)
  63.                 ->setActive(true)
  64.                 ->setSize(_screenW, _screenH)
  65.                 ->onInit(CODE{
  66.  
  67.  
  68.                 })
  69.                 ->onUpdate(demo1Update)
  70.                 ->onRender(CODE{
  71.  
  72.                     Draw::grid(0, 0, _screenW, _screenH, 16, 16, al_map_rgb(0, 80, 120));
  73.                     //Draw::circleFill(Circle{ Input::Mouse::_x, Input::Mouse::_y, 16 }, al_map_rgb(150, 140, 50));
  74.                     Draw::circle(Circle{ 320, 180, 16 }, al_map_rgb(250, 140, 50),2);
  75.                     Draw::rect(_ABSRECT, al_map_rgb(250,120,0),0);
  76.  
  77.                     _THIS->renderChild();
  78.  
  79.                 })
  80.                 ->init()
  81.             };
  82.  
  83.             _node["demo2"] = {
  84.                
  85.                 MAKE_NODE("demo2")
  86.                 ->appendTo(_root)
  87.                 ->setActive(true)
  88.                 ->setSize(_screenW, _screenH)
  89.                 ->attach<Addon::Viewport>()
  90.                 ->VIEWPORT->setViewport(_window, 0, Rect{ 0,0,200,200 })
  91.                 ->onInit(CODE{
  92.  
  93.  
  94.                 })
  95.                 ->onUpdate(CODE{
  96.  
  97.                     _VIEWPORT->updateViewport(0, Rect{ 0,0,_mouseX,_mouseY });
  98.  
  99.                     float v = 2;
  100.  
  101.                     //if (KEY_DOWN(ALLEGRO_KEY_LEFT))  _X += v;
  102.                     //if (KEY_DOWN(ALLEGRO_KEY_RIGHT)) _X += -v;
  103.                     //if (KEY_DOWN(ALLEGRO_KEY_UP))    _Y += v;
  104.                     //if (KEY_DOWN(ALLEGRO_KEY_DOWN))  _Y += -v;
  105.                 })
  106.                 ->onRender(CODE{
  107.  
  108.                     _VIEWPORT->beginRenderView(_window, 0);
  109.                     al_clear_to_color(al_map_rgb(10,20,10));
  110.  
  111.                     Draw::grid(_ABSX, _ABSY, _screenW, _screenH, 16, 16, al_map_rgb(0, 80, 20));
  112.                     Draw::circleFill(Circle{ 320, 180, 32 }, al_map_rgb(250, 140, 50));
  113.                     Draw::circle(Circle{ 320, 180, 48 }, al_map_rgb(50, 140, 250),2);
  114.                     Draw::rect(_ABSRECT, al_map_rgb(250,120,0),0);
  115.  
  116.                     _THIS->renderChild();
  117.  
  118.                     _VIEWPORT->endRenderView(_window, 0);
  119.  
  120.                 })
  121.                 ->init()
  122.             };
  123.  
  124.             _node["demo3"] = {
  125.                
  126.                 MAKE_NODE("demo3")
  127.                 ->appendTo(_root)
  128.                 ->setActive(true)
  129.                 ->setSize(_screenW,_screenH)
  130.  
  131.             };
  132.  
  133.             _node["ball"] = {
  134.  
  135.                 MAKE_NODE("ball")
  136.                 ->setActive(true)
  137.                 ->appendTo(_node["demo1"])
  138.                 ->attach<Addon::Physic>()
  139.                 ->onInit(CODE{
  140.                    
  141.                     _X = _screenW / 2;
  142.                     _Y = _screenH / 2;
  143.  
  144.                     IS(_PHYSIC)
  145.                     {
  146.                         _PHYSIC->setVelocity(1, 1);
  147.                     }
  148.                 })
  149.                 ->onUpdate(CODE{
  150.  
  151.                     _X += _PHYSIC->_vx;
  152.                     _Y += _PHYSIC->_vy;
  153.  
  154.                     if (_X < 0)       _PHYSIC->_vx = 1;
  155.                     if (_X > _screenW) _PHYSIC->_vx = -1;
  156.                     if (_Y < 0)       _PHYSIC->_vy = 1;
  157.                     if (_Y > _screenH) _PHYSIC->_vy = -1;
  158.  
  159.                
  160.  
  161.                 })
  162.                 ->onRender(CODE{
  163.  
  164.                     Draw::circleFill(Circle{ _ABSX, _ABSY, 16 }, al_map_rgb(250, 40, 50));
  165.  
  166.                 })
  167.                 ->init()
  168.             };
  169.  
  170.             _node["ball2"] = {
  171.                
  172.                 MAKE_NODE("ball2")
  173.                 ->appendTo(_node["demo1"])
  174.                 ->setActive(true)
  175.                 ->attach<Addon::Draggable>()
  176.                 ->DRAGGABLE->setLimitRect(Rect{0,0,(VAR)_screenW,(VAR)_screenH})
  177.                 ->setPosition(200,80)
  178.                 ->setSize(48,48)
  179.                 //->setPivot(CENTER)
  180.                 ->onUpdate(CODE{
  181.                     _DRAGGABLE->update();
  182.                 })
  183.                 ->onRender(CODE{
  184.  
  185.                     //Draw::circleFill(Circle{ _ABSX+24, _ABSY+24, 24 }, al_map_rgb(50, 40, 250));
  186.  
  187.                     Draw::rectFill(_ABSRECT, al_map_rgba(50,150,80,150));
  188.                     Draw::rect(_ABSRECT, al_map_rgb(250,250,180),0);
  189.  
  190.                     //_DRAGGABLE->render();
  191.                 })
  192.             };
  193.  
  194.             _area = {
  195.  
  196.                 MAKE_NODE("area")
  197.                 ->setActive(true)
  198.                 ->appendTo(_node["demo1"])
  199.                 ->setPosition(320,180)
  200.                 ->setSize(24,24)
  201.                 ->setPivot(CENTER)
  202.                 ->setNumber("speed",12)
  203.                 ->onUpdate(CODE{
  204.                     if (Input::Button::onPress("aLeft",KEY_DOWN(ALLEGRO_KEY_LEFT),10))    _X += -_NUMBER("speed");
  205.                     if (Input::Button::onPress("aRight", KEY_DOWN(ALLEGRO_KEY_RIGHT),10)) _X += _NUMBER("speed");
  206.                     if (Input::Button::onPress("aUp", KEY_DOWN(ALLEGRO_KEY_UP),10))       _Y += -_NUMBER("speed");
  207.                     if (Input::Button::onPress("aDown", KEY_DOWN(ALLEGRO_KEY_DOWN),10))   _Y += _NUMBER("speed");
  208.  
  209.                 })
  210.                 ->onRender(CODE{
  211.                     Draw::rectFill(_ABSRECT + Rect{_OX,_OY,0,0}, al_map_rgba(250,150,80,150));
  212.                     Draw::rect(_ABSRECT + Rect{ _OX,_OY,0,0 }, al_map_rgb(250,250,180),0);
  213.  
  214.                     _THIS->showPivot(al_map_rgb(255, 0, 0), 2);
  215.                 })
  216.  
  217.             };
  218.  
  219.  
  220.             MAKE_CLONE_ALONE(_area, "areaClone")
  221.             ->setActive(true)
  222.             ->appendTo(_node["demo2"]);
  223.  
  224.            
  225.             for (int i=0; i<100; ++i)
  226.             {
  227.                 MAKE_CLONE_ALONE(_node["ball"], "ball3")
  228.                 ->appendTo(_node["demo1"])
  229.                 ->onRender(CODE{
  230.                     Draw::circleFill(Circle{ _ABSX, _ABSY, 8 }, al_map_rgb(50, 240, 50));
  231.                 })
  232.                 ->setPosition(Misc::random(0,_screenW), Misc::random(0,_screenH));
  233.             }
  234.  
  235.             Screen::goTo(_node["demo1"]);
  236.             //Screen::current()->pause();
  237.  
  238.             Node::showNodeInfo();
  239.  
  240.             // JSON FILE
  241.             _myJson = File::loadJson("test00.json");
  242.            
  243.             std::cout << _myJson["array"] << " / ";
  244.             std::cout << _myJson["string"];
  245.  
  246.             // XML FILE
  247.  
  248.             pugi::xml_document doc;
  249.             pugi::xml_parse_result result = doc.load_file("test00.xml");
  250.  
  251.             std::cout << "load result = " << result.description() << "\n";
  252.  
  253.             pugi::xml_node root_node = doc.child("Root");
  254.  
  255.             //pugi::xml_node child_node = root_node.first_child();
  256.  
  257.             for (pugi::xml_node child = root_node.first_child(); child; child = child.next_sibling())
  258.             {
  259.                 std::cout << child.name() << std::endl;
  260.  
  261.                 for (pugi::xml_attribute attr = child.first_attribute(); attr; attr = attr.next_attribute())
  262.                 {
  263.                     std::cout << " " << attr.name() << " = " << attr.value() << std::endl;
  264.                 }
  265.                 std::cout << "Content = " << child.text().as_string() << "\n";
  266.                 std::cout << std::endl;
  267.             }
  268.  
  269.             return 0;
  270.         }
  271.  
  272.         int done() override
  273.         {
  274.             al_destroy_font(_mainFont);
  275.             al_destroy_sample(_mySample);
  276.  
  277.             return 0;
  278.         }
  279.  
  280.         void update() override
  281.         {
  282.             if (KEY_DOWN(ALLEGRO_KEY_ESCAPE)) _quit = true;
  283.  
  284.             if (Input::Button::onePress("space", KEY_DOWN(ALLEGRO_KEY_SPACE))) _window->toggleFullScreen(-1);
  285.             if (Input::Button::onePress("tab", KEY_DOWN(ALLEGRO_KEY_TAB))) _window->switchMonitor(-1);
  286.  
  287.             if (Input::Button::onePress("f1", KEY_DOWN(ALLEGRO_KEY_F1))) Screen::goTo(_node["demo1"]);
  288.             if (Input::Button::onePress("f2", KEY_DOWN(ALLEGRO_KEY_F2))) Screen::goTo(_node["demo2"]);
  289.             if (Input::Button::onePress("f3", KEY_DOWN(ALLEGRO_KEY_F3))) Screen::goTo(_node["demo3"]);
  290.  
  291.             if (Input::Button::onePress("enter", KEY_DOWN(ALLEGRO_KEY_ENTER)))
  292.                 al_play_sample(_mySample, 0.4f, 0.0f, 1.0f, ALLEGRO_PLAYMODE_ONCE, NULL);
  293.  
  294.             if (Input::Button::onePress("pause", KEY_DOWN(ALLEGRO_KEY_P)))
  295.             {
  296.                 (_pause = !_pause) ? Screen::current()->resume(): Screen::current()->pause();
  297.             }
  298.  
  299.             Screen::current()->update()->updateChild();
  300.         }
  301.  
  302.         void render() override
  303.         {
  304.             al_clear_to_color(al_map_rgb(0, 20, 50));
  305.  
  306.             Screen::current()->render();
  307.  
  308.             Draw::List list;
  309.             Draw::lineBegin(list);
  310.             Draw::sight(list, _mouseX, _mouseY, _screenW, _screenH, al_map_rgb(150, 200, 250));
  311.             Draw::lineEnd(list);
  312.  
  313.             al_draw_text(_mainFont, al_map_rgb(250, 0, 160), _screenW / 2, _screenH / 2, -1, "- P A U S E -");
  314.  
  315.         }
  316.     };
  317. }
  318.  
  319. int main()
  320. {
  321.     Retro2D::Game1 game;
  322.     game.run();
  323.     return 0;
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement