Advertisement
Guest User

progressbar

a guest
May 9th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. class MyInt
  6. {
  7. public:
  8.     MyInt(unsigned var, unsigned maxvalue = 100):
  9.         var(var),
  10.         maxvalue(maxvalue)
  11.     {
  12.         ;
  13.     }
  14.  
  15.     operator unsigned()
  16.     {
  17.         return var;
  18.     }
  19.  
  20.     MyInt operator+(const MyInt& m) const
  21.     {
  22.         unsigned tmp = var + m.var;
  23.         while(tmp > maxvalue) tmp -= maxvalue;
  24.         return MyInt(tmp);
  25.     }
  26.  
  27.     MyInt& operator++()
  28.     {
  29.         if((++var) >= maxvalue)
  30.             var = 0;
  31.         return *this;
  32.     }
  33.  
  34.     bool operator<(const MyInt& m) const
  35.     {
  36.         return var < m.var;
  37.     }
  38.  
  39.     bool operator>(const MyInt& m) const
  40.     {
  41.         return var > m.var;
  42.     }
  43.  
  44. private:
  45.     unsigned var;
  46.     const unsigned maxvalue;
  47. };
  48.  
  49. class ProgressBar
  50. {
  51. public:
  52.     ProgressBar(unsigned len, unsigned width,
  53.                 unsigned height, unsigned colorfg,
  54.                 unsigned colorbg):
  55.         len(len),
  56.         width(width),
  57.         height(height),
  58.         posx(0, len),
  59.         posy(0),
  60.         colorfg(colorfg),
  61.         colorbg(colorbg)
  62.     {
  63.         ;
  64.     }
  65.  
  66.     void run()
  67.     {
  68.         while(true)
  69.         {
  70.             drawRect(0, 0, len, height, colorbg);
  71.  
  72.             MyInt tmpx = posx + (MyInt)width;
  73.             if(posx > tmpx)
  74.             {
  75.                 drawRect(posx, posy, len-(unsigned)posx, height, colorfg);
  76.                 drawRect(0, posy, tmpx, height, colorfg);
  77.             }
  78.             else
  79.             {
  80.                 drawRect(posx, posy, width, height, colorfg);
  81.             }
  82.             SDL_Flip(SDL_GetVideoSurface());
  83.             ++posx;
  84.             SDL_Delay(50);
  85.         }
  86.     }
  87.  
  88. private:
  89.     void drawRect(MyInt x, MyInt y, unsigned w, unsigned h, unsigned color)
  90.     {
  91.         SDL_Rect r;
  92.         r.w = w;
  93.         r.h = h;
  94.         r.x = x;
  95.         r.y = y;
  96.  
  97.         SDL_Surface* screen = SDL_GetVideoSurface();
  98.  
  99.         SDL_FillRect(screen, &r, color);
  100.     }
  101.  
  102. private:
  103.     const unsigned len;
  104.     const unsigned width;
  105.     const unsigned height;
  106.     MyInt posx;
  107.     MyInt posy;
  108.     unsigned colorfg;
  109.     unsigned colorbg;
  110. };
  111.  
  112. int main()
  113. {
  114.     if(SDL_Init(SDL_INIT_VIDEO) < 0)
  115.     {
  116.         return -1;
  117.     }
  118.  
  119.     atexit(SDL_Quit);
  120.  
  121.     if(!SDL_SetVideoMode(320, 32, 32, SDL_HWSURFACE))
  122.     {
  123.         return -1;
  124.     }
  125.  
  126.     ProgressBar MPB(320, 32, 32, 0x00FF00, 0x0000);
  127.     MPB.run();
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement