Guest User

Untitled

a guest
Nov 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.99 KB | None | 0 0
  1. // compile with:
  2. ./ g++ file.cpp `allegro-config --cflags --libs` -o pong
  3.  
  4. #include <allegro.h>
  5. #include <cstdlib>
  6.  
  7. namespace pong {
  8.     struct types {
  9.         typedef int     i;
  10.         typedef void    v;
  11.         typedef BITMAP* b;
  12.     };
  13.    
  14.     /*
  15.      * Paddle configuration
  16.      */
  17.     enum {
  18.         paddle_w = 5,
  19.         paddle_h = 30,
  20.         ball_rad = 2
  21.     };
  22.    
  23.     /*
  24.      * Control configuration
  25.      */
  26.     enum {
  27.         player_one_up = KEY_Q,
  28.         player_one_dn = KEY_A,
  29.         player_two_up = KEY_P,
  30.         player_two_dn = KEY_L
  31.     };
  32.    
  33.     /*
  34.      * Pong engine utilities
  35.      */
  36.     namespace util
  37.     {
  38.         /*
  39.          * Default whip
  40.          */
  41.         template<types::i(*F)()> types::i whip0() { return F(); }
  42.        
  43.         /*
  44.          * Alternitive whips.
  45.          */
  46.         template<
  47.             types::v  (*F)(types::i)
  48.         >   types::i whip1(types::i AAAA) { F(AAAA); return 0; }
  49.         template<
  50.             types::b  (*F)(
  51.                 types::i,
  52.                 types::i
  53.             )
  54.         >   types::b whip2 (
  55.             types::i AAAA,
  56.             types::i BBBB) {
  57.                 return F(AAAA,BBBB);
  58.             }
  59.         template<
  60.             types::i  (*F)(
  61.                 types::i,
  62.                 types::i,
  63.                 types::i,
  64.                 types::i,
  65.                 types::i
  66.             )
  67.         >   types::i whip5 (
  68.             types::i AAAA,
  69.             types::i BBBB,
  70.             types::i CCCC,
  71.             types::i DDDD,
  72.             types::i EEEE) {
  73.                 return F(AAAA,BBBB,CCCC,DDDD,EEEE);
  74.             }
  75.     }
  76.    
  77.     /*
  78.      * Pong initialization
  79.      */
  80.     types::i init_allegro  = install_allegro(SYSTEM_AUTODETECT,&errno,atexit);
  81.     types::i init_keyboard = util::whip0<install_keyboard>(  );
  82.     types::i init_colordep = util::whip1<set_color_depth> (16);
  83.     types::i init_graphics = util::whip5<set_gfx_mode>    (GFX_AUTODETECT_WINDOWED, 320, 240, 0, 0);
  84.     types::b init_buffer   = util::whip2<create_bitmap>   (SCREEN_W, SCREEN_H);
  85.    
  86.     /*
  87.      * Pong timer
  88.      */
  89.     volatile types::i priv_ticks = 0;
  90.              types::v priv_thing() { priv_ticks++; }
  91.              types::i priv_intex = install_int_ex(priv_thing, BPS_TO_TIMER(25));
  92.              
  93.    
  94.     class Timer {
  95.     public:
  96.         static types::i Go() {
  97.             return priv_ticks;
  98.         }
  99.     };
  100.     class DecrementTimer {
  101.     public:
  102.         DecrementTimer() {
  103.             --priv_ticks;
  104.         }
  105.     };
  106.    
  107.     /*
  108.      * Template language
  109.      */
  110.     namespace lang
  111.     {
  112.         template<typename T>
  113.         types::i Tick()
  114.         {
  115.             delete new T;
  116.             return 0;
  117.         }
  118.         template<typename T>
  119.         class Draw {
  120.         public:
  121.             Draw() {
  122.                 T::Draw();
  123.             }
  124.         };
  125.        
  126.         template<typename C, typename L>
  127.         class While {
  128.         public:
  129.             While() {
  130.                 while(C::Go()) {
  131.                     Tick<L>();
  132.                 }
  133.             }
  134.         };
  135.         template<typename L, typename R>
  136.         class Compound {
  137.         public:
  138.             Compound() {
  139.                 Tick<L>();
  140.                 Tick<R>();
  141.             }
  142.         };
  143.         template<
  144.             typename C,
  145.             typename T,
  146.             typename F>
  147.         class If {
  148.         public:
  149.             If() {
  150.                 if(C::Go()) {
  151.                     Tick<T>();
  152.                 } else {
  153.                     Tick<F>();
  154.                 }
  155.             }
  156.         };
  157.        
  158.         template<types::i& V, typename A>
  159.         class AddEqual {
  160.         public:
  161.             AddEqual() {
  162.                 V += A::Go();
  163.             }
  164.         };
  165.        
  166.         template<types::i& V, typename A>
  167.         class Set {
  168.         public:
  169.             Set() {
  170.                 V = A::Go();
  171.             }
  172.         };
  173.        
  174.         // expressions
  175.         template<types::i T>
  176.         class Keys {
  177.         public:
  178.             public: static types::i Go() { return key[T]; }
  179.         };
  180.         template<types::i& T>
  181.         class Get {
  182.         public:
  183.             static types::i Go() { return T; }
  184.         };
  185.         template<types::i T>
  186.         class Const {
  187.         public:
  188.             static types::i Go() { return T; }
  189.         };
  190.         template<typename T>
  191.         class Not {
  192.         public:
  193.             static types::i Go() { return !T::Go(); }
  194.         };
  195.         template<typename T>
  196.         class Negitive {
  197.         public:
  198.             static types::i Go () { return -T::Go(); }
  199.         };
  200.         template<typename T, typename R>
  201.         class Add {
  202.         public:
  203.             static types::i Go () { return T::Go() + R::Go(); }
  204.         };
  205.         template<typename T, typename R>
  206.         class And {
  207.         public:
  208.             static types::i Go () { return T::Go() && R::Go(); }
  209.         };
  210.         template<typename T, typename R>
  211.         class Greater {
  212.         public:
  213.             static types::i Go() { return T::Go() > R::Go(); }
  214.         };
  215.         template<typename T, typename R>
  216.         class Less {
  217.         public:
  218.             static types::i Go() { return T::Go() < R::Go(); }
  219.         };
  220.        
  221.         class Nil {};
  222.     }
  223.    
  224.     class Flip {
  225.     public:
  226.         static inline void Draw() {
  227.             blit(init_buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
  228.             clear_to_color(init_buffer,0);
  229.         }
  230.     };
  231.     template<typename X, typename Y>
  232.     class Paddle {
  233.     public:
  234.         static inline void Draw() {
  235.             rectfill(init_buffer, X::Go(), Y::Go(), X::Go() + paddle_w, Y::Go() + paddle_h, -1);
  236.         }
  237.     };
  238.     template<typename X, typename Y>
  239.     class Ball {
  240.     public:
  241.         static inline void Draw() {
  242.             circlefill(init_buffer, X::Go(), Y::Go(), ball_rad, -1);
  243.         }
  244.     };
  245.     template<typename X, typename Y>
  246.     class Score {
  247.     public:
  248.         static inline void Draw() {
  249.             textprintf(init_buffer, font, 0, 0, -1, "%d", X::Go());
  250.             textprintf_right(init_buffer, font, SCREEN_W, 0, -1, "%d"
Add Comment
Please, Sign In to add comment