Sunday, September 16, 2012

Creating a Progress Bar in SDL and C++

Progress Bar

#include "sdl/sdl.h"
#include "sdl/sdl_image.h"
#include "SDL/SDL_ttf.h"
#include "sdl/sdl_mixer.h"
#include <iostream>
#include <sstream>
#include <string>
#include <sstream>
#include <windows.h>

using namespace std;
void putpixel(SDL_Surface* screen, int x, int y);
int MemFun();
void DrawBar(SDL_Surface* screen, int xpos, int xposmax, int ypos, int yposmax, int value, int width, int hieght, SDL_Rect* box, SDL_Rect* bgbox, Uint32 color);

                                                            
int main(int argc, char** args)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    TTF_Init();
    TTF_Font *font = TTF_OpenFont("fonts/hacka.ttf", 12);
    SDL_Surface* screen, *text;
    screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    Uint32 color2 = SDL_MapRGB(screen->format, 0xbb, 0x99, 0x00);
    Uint32 color0 = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
    SDL_Color color1 = {0xff, 0xff, 0xff};
    SDL_Event events;
    bool running  = true;
    const int FPS = 20;
    Uint32 start;
    stringstream ss;
    SDL_Rect box;

    SDL_Rect bgbox;

    SDL_Rect valBox;
    valBox.x = 300;
    valBox.y = 210;

    while(running)
    {
        start = SDL_GetTicks();
        while(SDL_PollEvent(&events))
        {
            switch(events.type)
            {
                case SDL_QUIT:
                    running = false;
                    break;
            }
        }
    ss.str(" ");
    ss << MemFun();
    text = TTF_RenderText_Solid(font, ss.str().c_str(), color1);

    SDL_FillRect(screen, &screen->clip_rect, color0);
    DrawBar(screen, 300, 315, 200, 200, MemFun(), 10, 100, &box, &bgbox, color2);

    SDL_BlitSurface(text, NULL, screen, &valBox);

    SDL_Flip(screen);
    if(1000/FPS > SDL_GetTicks() - start)
       SDL_Delay(1000/FPS - (SDL_GetTicks() - start));

    }

    SDL_FreeSurface(text);
    SDL_Quit();
    TTF_Quit();
    return 0;
}

void putpixel(SDL_Surface* screen, int x, int y)
{
    Uint32 *pixel = (Uint32*)screen->pixels;
    Uint32* p = pixel + y*screen->pitch/4 + x;
    *p = SDL_MapRGB(screen->format, 0xff, 0xaa,0x00);
}

int MemFun()
{
    MEMORYSTATUS mem;
    mem.dwLength = sizeof(mem);
    GlobalMemoryStatus(&mem);
    return mem.dwMemoryLoad;
}

void DrawBar(SDL_Surface* screen, int xpos, int xposmax, int ypos, int yposmax, int value, int width, int hieght, SDL_Rect* box, SDL_Rect* bgbox, Uint32 color)
{
    box->x = xpos;
    box->y = ypos;
    box->w = width;
    box->h = hieght;

    bgbox->x = xpos - 3;
    bgbox->y = ypos - 99;
    bgbox->w = width + 11;
    bgbox->h = hieght;

    SDL_FillRect(screen, bgbox, color);
    for(int i = ypos; i>= (yposmax - value); i--)
    {
        for(int j= xpos; j<xposmax; j++)
        {
            putpixel(screen, j, i);
        }

    }
}

No comments:

Post a Comment

How to create Custom push button in QT5

To be honest this is not my work its an exact copy from this link. http://mcmtechtips.blogspot.com/2012/03/shape-qpushbutton-in-your-own-st...