Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL/SDL.h>
- #include <cstdlib>
- using namespace std;
- class MyInt
- {
- public:
- MyInt(unsigned var, unsigned maxvalue = 100):
- var(var),
- maxvalue(maxvalue)
- {
- ;
- }
- operator unsigned()
- {
- return var;
- }
- MyInt operator+(const MyInt& m) const
- {
- unsigned tmp = var + m.var;
- while(tmp > maxvalue) tmp -= maxvalue;
- return MyInt(tmp);
- }
- MyInt& operator++()
- {
- if((++var) >= maxvalue)
- var = 0;
- return *this;
- }
- bool operator<(const MyInt& m) const
- {
- return var < m.var;
- }
- bool operator>(const MyInt& m) const
- {
- return var > m.var;
- }
- private:
- unsigned var;
- const unsigned maxvalue;
- };
- class ProgressBar
- {
- public:
- ProgressBar(unsigned len, unsigned width,
- unsigned height, unsigned colorfg,
- unsigned colorbg):
- len(len),
- width(width),
- height(height),
- posx(0, len),
- posy(0),
- colorfg(colorfg),
- colorbg(colorbg)
- {
- ;
- }
- void run()
- {
- while(true)
- {
- drawRect(0, 0, len, height, colorbg);
- MyInt tmpx = posx + (MyInt)width;
- if(posx > tmpx)
- {
- drawRect(posx, posy, len-(unsigned)posx, height, colorfg);
- drawRect(0, posy, tmpx, height, colorfg);
- }
- else
- {
- drawRect(posx, posy, width, height, colorfg);
- }
- SDL_Flip(SDL_GetVideoSurface());
- ++posx;
- SDL_Delay(50);
- }
- }
- private:
- void drawRect(MyInt x, MyInt y, unsigned w, unsigned h, unsigned color)
- {
- SDL_Rect r;
- r.w = w;
- r.h = h;
- r.x = x;
- r.y = y;
- SDL_Surface* screen = SDL_GetVideoSurface();
- SDL_FillRect(screen, &r, color);
- }
- private:
- const unsigned len;
- const unsigned width;
- const unsigned height;
- MyInt posx;
- MyInt posy;
- unsigned colorfg;
- unsigned colorbg;
- };
- int main()
- {
- if(SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- return -1;
- }
- atexit(SDL_Quit);
- if(!SDL_SetVideoMode(320, 32, 32, SDL_HWSURFACE))
- {
- return -1;
- }
- ProgressBar MPB(320, 32, 32, 0x00FF00, 0x0000);
- MPB.run();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement