Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // compile with:
- ./ g++ file.cpp `allegro-config --cflags --libs` -o pong
- #include <allegro.h>
- #include <cstdlib>
- namespace pong {
- struct types {
- typedef int i;
- typedef void v;
- typedef BITMAP* b;
- };
- /*
- * Paddle configuration
- */
- enum {
- paddle_w = 5,
- paddle_h = 30,
- ball_rad = 2
- };
- /*
- * Control configuration
- */
- enum {
- player_one_up = KEY_Q,
- player_one_dn = KEY_A,
- player_two_up = KEY_P,
- player_two_dn = KEY_L
- };
- /*
- * Pong engine utilities
- */
- namespace util
- {
- /*
- * Default whip
- */
- template<types::i(*F)()> types::i whip0() { return F(); }
- /*
- * Alternitive whips.
- */
- template<
- types::v (*F)(types::i)
- > types::i whip1(types::i AAAA) { F(AAAA); return 0; }
- template<
- types::b (*F)(
- types::i,
- types::i
- )
- > types::b whip2 (
- types::i AAAA,
- types::i BBBB) {
- return F(AAAA,BBBB);
- }
- template<
- types::i (*F)(
- types::i,
- types::i,
- types::i,
- types::i,
- types::i
- )
- > types::i whip5 (
- types::i AAAA,
- types::i BBBB,
- types::i CCCC,
- types::i DDDD,
- types::i EEEE) {
- return F(AAAA,BBBB,CCCC,DDDD,EEEE);
- }
- }
- /*
- * Pong initialization
- */
- types::i init_allegro = install_allegro(SYSTEM_AUTODETECT,&errno,atexit);
- types::i init_keyboard = util::whip0<install_keyboard>( );
- types::i init_colordep = util::whip1<set_color_depth> (16);
- types::i init_graphics = util::whip5<set_gfx_mode> (GFX_AUTODETECT_WINDOWED, 320, 240, 0, 0);
- types::b init_buffer = util::whip2<create_bitmap> (SCREEN_W, SCREEN_H);
- /*
- * Pong timer
- */
- volatile types::i priv_ticks = 0;
- types::v priv_thing() { priv_ticks++; }
- types::i priv_intex = install_int_ex(priv_thing, BPS_TO_TIMER(25));
- class Timer {
- public:
- static types::i Go() {
- return priv_ticks;
- }
- };
- class DecrementTimer {
- public:
- DecrementTimer() {
- --priv_ticks;
- }
- };
- /*
- * Template language
- */
- namespace lang
- {
- template<typename T>
- types::i Tick()
- {
- delete new T;
- return 0;
- }
- template<typename T>
- class Draw {
- public:
- Draw() {
- T::Draw();
- }
- };
- template<typename C, typename L>
- class While {
- public:
- While() {
- while(C::Go()) {
- Tick<L>();
- }
- }
- };
- template<typename L, typename R>
- class Compound {
- public:
- Compound() {
- Tick<L>();
- Tick<R>();
- }
- };
- template<
- typename C,
- typename T,
- typename F>
- class If {
- public:
- If() {
- if(C::Go()) {
- Tick<T>();
- } else {
- Tick<F>();
- }
- }
- };
- template<types::i& V, typename A>
- class AddEqual {
- public:
- AddEqual() {
- V += A::Go();
- }
- };
- template<types::i& V, typename A>
- class Set {
- public:
- Set() {
- V = A::Go();
- }
- };
- // expressions
- template<types::i T>
- class Keys {
- public:
- public: static types::i Go() { return key[T]; }
- };
- template<types::i& T>
- class Get {
- public:
- static types::i Go() { return T; }
- };
- template<types::i T>
- class Const {
- public:
- static types::i Go() { return T; }
- };
- template<typename T>
- class Not {
- public:
- static types::i Go() { return !T::Go(); }
- };
- template<typename T>
- class Negitive {
- public:
- static types::i Go () { return -T::Go(); }
- };
- template<typename T, typename R>
- class Add {
- public:
- static types::i Go () { return T::Go() + R::Go(); }
- };
- template<typename T, typename R>
- class And {
- public:
- static types::i Go () { return T::Go() && R::Go(); }
- };
- template<typename T, typename R>
- class Greater {
- public:
- static types::i Go() { return T::Go() > R::Go(); }
- };
- template<typename T, typename R>
- class Less {
- public:
- static types::i Go() { return T::Go() < R::Go(); }
- };
- class Nil {};
- }
- class Flip {
- public:
- static inline void Draw() {
- blit(init_buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
- clear_to_color(init_buffer,0);
- }
- };
- template<typename X, typename Y>
- class Paddle {
- public:
- static inline void Draw() {
- rectfill(init_buffer, X::Go(), Y::Go(), X::Go() + paddle_w, Y::Go() + paddle_h, -1);
- }
- };
- template<typename X, typename Y>
- class Ball {
- public:
- static inline void Draw() {
- circlefill(init_buffer, X::Go(), Y::Go(), ball_rad, -1);
- }
- };
- template<typename X, typename Y>
- class Score {
- public:
- static inline void Draw() {
- textprintf(init_buffer, font, 0, 0, -1, "%d", X::Go());
- textprintf_right(init_buffer, font, SCREEN_W, 0, -1, "%d"
Add Comment
Please, Sign In to add comment