Advertisement
archlinuxorg

Untitled

Nov 14th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <GL/freeglut.h>
  3. #include <list>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. namespace ui{
  9.  
  10.     class obj{
  11.  
  12.     public:
  13.  
  14.         obj(){
  15.             all.push_back(this);
  16.         }
  17.         virtual ~obj(){};
  18.  
  19.         double x, y;
  20.  
  21.         static list <obj*> all;
  22.         static void (*drawRect)(int x, int y, int w, int h);
  23.         static void (*drawStar)(int x, int y, int w, int h);
  24.  
  25.         static void draw(){
  26.             for(obj *p: all)
  27.                 p->_draw();
  28.         }
  29.         static void onClick(int x, int y){
  30.             for(obj *p: all)
  31.                 p->_onClick(x, y);
  32.         }
  33.         static void onTick(int ms){
  34.             for(obj *p: all)
  35.                 p->_onTick(ms);
  36.         }
  37.  
  38.         obj* place(int x, int y){
  39.             this->x = x;
  40.             this->y = y;
  41.             return this;
  42.         }
  43.  
  44.         virtual int contain(int x, int y) = 0;
  45.  
  46.     protected:
  47.  
  48.         virtual void _onClick(int x, int y) = 0;
  49.         virtual void _draw() = 0;
  50.         virtual void _onTick(int ms) = 0;
  51.     };
  52.     list <obj*> obj::all;
  53.  
  54.     class frame : public obj{
  55.  
  56.     public:
  57.  
  58.         frame() : obj() {}
  59.         virtual ~frame() {}
  60.  
  61.         int w, h;
  62.  
  63.         frame* resize(int w, int h){
  64.             this->w = w;
  65.             this->h = h;
  66.             return this;
  67.         }
  68.  
  69.     protected:
  70.  
  71.         virtual int contain(int x, int y){
  72.             if( x < this->x
  73.              || x >= this->x + w
  74.              || y < this->y
  75.              || y >= this->y + h ){
  76.                 return 0;
  77.             }
  78.             else{
  79.                 return 1;
  80.             }
  81.         }
  82.         virtual void _onClick(int x, int y){
  83.             if(contain(x, y))
  84.                 _onHit(x, y);
  85.             else
  86.                 _onMiss(x, y);
  87.         }
  88.  
  89.         virtual void _onHit(int x, int y) = 0;
  90.         virtual void _onMiss(int x, int y) = 0;
  91.     };
  92.  
  93.     class button : public frame{
  94.  
  95.     public:
  96.  
  97.         button(void (*cb)()) :
  98.             frame(), callback(cb)
  99.         {}
  100.         virtual ~button() {}
  101.  
  102.         void (*callback)();
  103.  
  104.     protected:
  105.  
  106.         virtual void _draw(){
  107.             drawRect(x, y, w, h);
  108.         }
  109.         virtual void _onHit(int x, int y){
  110.             callback();
  111.         }
  112.         virtual void _onMiss(int x, int y){
  113.         }
  114.         virtual void _onTick(int ms){
  115.         }
  116.     };
  117.  
  118.     class unit : public frame{
  119.  
  120.     public:
  121.  
  122.         unit() : frame() {}
  123.         virtual ~unit() {}
  124.  
  125.         int speed;
  126.         double dir_x, dir_y;
  127.         int selected = 0;
  128.  
  129.         unit* accelerate(int speed){
  130.             this->speed = speed;
  131.             return this;
  132.         }
  133.  
  134.         unit* rotate(int targ_x, int targ_y){
  135.             int dx = targ_x - x;
  136.             int dy = targ_y - y;
  137.             double distance = sqrt(dx * dx + dy * dy);
  138.             if(distance != 0){
  139.                 dir_x = dx / distance;
  140.                 dir_y = dy / distance;
  141.             }
  142.             else{
  143.                 dir_x = 0;
  144.                 dir_y = 0;
  145.             }
  146.             return this;
  147.         }
  148.  
  149.     protected:
  150.  
  151.         virtual void _onHit(int x, int y){
  152.             _toggleSelection();
  153.         }
  154.         virtual void _onMiss(int x, int y){
  155.             _setOrder(x, y);
  156.         }
  157.         virtual void _onTick(int ms){
  158.             _move(ms);
  159.             _think(ms);
  160.         }
  161.         virtual void _toggleSelection(){
  162.             selected = !selected;
  163.         }
  164.         virtual void _move(int ms){
  165.             x += dir_x * speed * ms;
  166.             y += dir_y * speed * ms;
  167.         }
  168.         virtual void _draw() = 0;
  169.         virtual void _think(int ms) = 0;
  170.         virtual void _setOrder(int x, int y) = 0;
  171.     };
  172.  
  173.     class missile : public unit{
  174.  
  175.     public:
  176.  
  177.         missile() : unit() {}
  178.         virtual ~missile() {}
  179.  
  180.         obj* target = nullptr;
  181.         missile* launch(obj* target){
  182.             this->target = target;
  183.             return this;
  184.         }
  185.  
  186.     protected:
  187.  
  188.         virtual void _setOrder(int x, int y){
  189.             if(selected){
  190.                 target = nullptr;
  191.                 rotate(x, y);
  192.                 for(obj* o: all){
  193.                     if(o->contain(x, y)){
  194.                         launch(o);
  195.                         break;
  196.                     }
  197.                 }
  198.             }
  199.         }
  200.         virtual void _think(int ms)
  201.         {
  202.             if(target){
  203.                 rotate(target->x, target->y);
  204.             }
  205.         }
  206.         virtual void _draw(){
  207.             drawStar(x, y, w, h);
  208.             if(selected)
  209.                 drawRect(x, y, w, h);
  210.         }
  211.     };
  212.  
  213.     class fighter : public missile{
  214.  
  215.     public:
  216.  
  217.         fighter() : missile() {}
  218.         virtual ~fighter() {}
  219.  
  220.         int reload_countdown = 0;
  221.         int super_aggressive = 0;
  222.  
  223.         fighter* apocalypse(){
  224.             super_aggressive = 1;
  225.             return this;
  226.         }
  227.  
  228.     protected:
  229.  
  230.         virtual void _think(int ms){
  231.             missile::_think(ms);
  232.             _tryFire();
  233.         }
  234.         virtual void _tryFire(){
  235.             if(reload_countdown--)
  236.                 return;
  237.             reload_countdown = 5;
  238.             if(!super_aggressive){
  239.                 if(target){
  240.                     _fire(target);
  241.                 }
  242.             }
  243.             else{
  244.                 int shoots_count = 6;
  245.                 for(obj* o: all){
  246.                     shoots_count--;
  247.                     if(o == this)
  248.                         continue;
  249.                     _fire(o);
  250.                     if(!shoots_count)
  251.                         break;
  252.                 }
  253.             }
  254.         }
  255.         virtual void _fire(obj* targ){
  256.             (new missile())
  257.                 ->launch(targ)->accelerate(15)
  258.                 ->resize(10,10)->place(x, y);
  259.         }
  260.     };
  261. }
  262.  
  263.  
  264. namespace gl{
  265.  
  266.     void drawRect(int x, int y, int w, int h){
  267.         glBegin(GL_LINE_LOOP);
  268.         glVertex2f(x, y);
  269.         glVertex2f(x, y + h);
  270.         glVertex2f(x + w, y + h);
  271.         glVertex2f(x + w, y);
  272.         glEnd();
  273.     }
  274.  
  275.     void drawStar(int x, int y, int w, int h){
  276.         glBegin(GL_LINE_LOOP);
  277.         glVertex2f(x, y + h);
  278.         glVertex2f(x + w/2, y);
  279.         glVertex2f(x + w, y + h);
  280.         glVertex2f(x, y + h/3);
  281.         glVertex2f(x + w, y + h/3);
  282.         glEnd();
  283.     }
  284.  
  285. }
  286.  
  287.  
  288. void (*ui::obj::drawRect) (int, int, int, int) = gl::drawRect;
  289. void (*ui::obj::drawStar) (int, int, int, int) = gl::drawStar;
  290. void (*glutTFuncWrapper)(int);
  291.  
  292.  
  293. int main(int argc, char** argv){
  294.  
  295.     (new ui::button([](){cout << "Hello\n";}))
  296.         ->resize(20, 20)->place(10, 10);
  297.     (new ui::button([](){cout << "WOW, IT'S LAMBDA\n";}))
  298.         ->resize(60, 60)->place(70, 30);
  299.     (new ui::fighter())
  300.         ->rotate(1, 1)->accelerate(1)->resize(30,30)->place(100,100);
  301.     (new ui::fighter())
  302.         ->rotate(1, .2)->accelerate(3)->resize(30,30)->place(200,200);
  303.     (new ui::fighter())
  304.         ->rotate(-1, .4)->accelerate(1)->resize(30,30)->place(200,100);
  305.     (new ui::fighter())
  306.         ->rotate(-.1, 1)->accelerate(2)->resize(30,30)->place(100,200);
  307.     (new ui::fighter())
  308.         ->apocalypse()->rotate(0, 0)->accelerate(1)->resize(30,30)->place(150,150);
  309.  
  310.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  311.     glutInitWindowSize(300, 300);
  312.     glutInit(&argc, argv);
  313.     glutCreateWindow("Fighter");
  314.     glutDisplayFunc([](){
  315.         glViewport(0, 0, 300, 300);
  316.         glMatrixMode(GL_PROJECTION);
  317.         glLoadIdentity();
  318.         glOrtho(0, 300, 300, 0, -1.0, 1.0);
  319.         glMatrixMode(GL_MODELVIEW);
  320.         glLoadIdentity();
  321.         glClear(GL_COLOR_BUFFER_BIT);
  322.         ui::obj::draw();
  323.         glutSwapBuffers();
  324.     });
  325.     glutMouseFunc([](int key, int state, int x, int y){
  326.         if (key == GLUT_LEFT_BUTTON
  327.          && state == GLUT_DOWN){
  328.             ui::obj::onClick(x, y);
  329.         }
  330.     });
  331.     glutTFuncWrapper = [](int ID){
  332.         glutTimerFunc(100, glutTFuncWrapper, 0);
  333.         ui::obj::onTick(1);
  334.         glutPostRedisplay();
  335.     };
  336.     glutTimerFunc(100, glutTFuncWrapper, 0);
  337.     glutMainLoop();
  338.     return 0;
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement