- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

# progressbar.c

Go to the documentation of this file.

    00001
    00005 //    /**
    00006 //      *@defgroup progressbar progressbar.c
    00007 //      *Routines to draw and update progress bars
    00008 //      *@{
    00009 //      */
    00010
    00011 /*----------------------------------------------------------------------------------------------------------*/
    00012
    00013 #ifndef _ALLEGRO5_
    00014 #define _ALLEGRO5_
    00015 #include <allegro5/allegro5.h>
    00016 #endif // _ALLEGRO5_
    00017
    00018 #ifndef _ALLEGRO5FONT_
    00019 #define _ALLEGRO5FONT_
    00020 #include <allegro5/allegro_font.h>
    00021 #endif // _ALLEGRO5FONT_
    00022
    00023 #ifndef _ALLEGRO5PRIMITIVES_
    00024 #define _ALLEGRO5PRIMITIVES_
    00025 #include <allegro5/allegro_primitives.h>
    00026 #endif // _ALLEGRO5PRIMITIVES_
    00027
    00028 #ifndef _PROGRESSBAR_
    00029 #define _PROGRESSBAR_
    00030 #include "../include/progressbar.h"
    00031 #endif // _PROGRESSBAR_
    00032
    00033 /*----------------------------------------------------------------------------------------------------------*/
    00034
    00035 static PROGRESSBARPTR pbfirstptr;
    00036
    00037 /*----------------------------------------------------------------------------------------------------------*/
    00038
    00039 PROGRESSBARPTR get_first_element_from_progress_bar_list(void)
    00040 {
    00041     return pbfirstptr;
    00042 }
    00043
    00044 /*----------------------------------------------------------------------------------------------------------*/
    00045
    00046 PROGRESSBARPTR set_first_element_in_progress_bar_list(PROGRESSBARPTR *newfirst)
    00047 {
    00048     pbfirstptr = *newfirst;
    00049     return pbfirstptr;
    00050 }
    00051
    00052 /*----------------------------------------------------------------------------------------------------------*/
    00053
    00054 PROGRESSBARPTR create_new_progress_bar(char *label, float x, float y, float width, float height, float rx,
    00055                                        float ry, ALLEGRO_COLOR out_color, ALLEGRO_COLOR in_color,
    00056                                        float thickness, _Bool add_to_list)
    00057 {
    00058     PROGRESSBARPTR newptr;
    00059     if((newptr = (PROGRESSBARPTR)malloc(sizeof(PROGRESSBAR))) == NULL) return NULL;
    00060
    00061     newptr->pblabel = label;
    00062     newptr->x1 = x;
    00063     newptr->y1 = y;
    00064     newptr->width = width;
    00065     newptr->height = height;
    00066     newptr->x2 = x+width;
    00067     newptr->y2 = y+height;
    00068     newptr->line_thickness = thickness;
    00069     newptr->rx = rx;
    00070     newptr->ry = ry;
    00071     newptr->bar_width = 0;
    00072     newptr->increment = width/100;
    00073     newptr->out_color = out_color;
    00074     newptr->in_color = in_color;
    00075     newptr->counter = 0;
    00076     newptr->done = false;
    00077
    00078     if(add_to_list)
    00079         add_element_to_progress_bar_list(&newptr);
    00080
    00081     return newptr;
    00082 }
    00083
    00084 /*----------------------------------------------------------------------------------------------------------*/
    00085
    00086 PROGRESSBARPTR add_element_to_progress_bar_list(PROGRESSBARPTR *progbar)
    00087 {
    00088     PROGRESSBARPTR currentptr;
    00089     if(pbfirstptr == NULL)
    00090     {
    00091         pbfirstptr = *progbar;
    00092         (*progbar)->nextbar = *progbar;
    00093         (*progbar)->prevbar = *progbar;
    00094     }
    00095     else
    00096     {
    00097         currentptr = pbfirstptr->prevbar;
    00098         currentptr->nextbar = *progbar;
    00099         (*progbar)->prevbar = currentptr;
    00100         pbfirstptr->prevbar = *progbar;
    00101         (*progbar)->nextbar = pbfirstptr;
    00102     }
    00103     return pbfirstptr;
    00104 }
    00105
    00106 /*----------------------------------------------------------------------------------------------------------*/
    00107
    00108 PROGRESSBARPTR remove_element_from_progress_bar_list(char *label)
    00109 {
    00110     PROGRESSBARPTR currentptr;
    00111     currentptr = pbfirstptr;
    00112     if(currentptr == NULL || pbfirstptr == NULL) return NULL;
    00113     if((currentptr = search_progress_bar_list_for_element(label)) == NULL) return pbfirstptr;
    00114     if(currentptr == pbfirstptr && currentptr->nextbar == currentptr && currentptr->prevbar == currentptr)
    00115     {
    00116         free(currentptr);
    00117         pbfirstptr = NULL;
    00118     }
    00119     else
    00120     {
    00121         currentptr->nextbar->prevbar = currentptr->prevbar;
    00122         currentptr->prevbar->nextbar = currentptr->nextbar;
    00123         if(currentptr == pbfirstptr)
    00124             pbfirstptr = currentptr->nextbar;
    00125         free(currentptr);
    00126     }
    00127     return pbfirstptr;
    00128 }
    00129
    00130 /*----------------------------------------------------------------------------------------------------------*/
    00131
    00132 PROGRESSBARPTR search_progress_bar_list_for_element(char *label)
    00133 {
    00134     PROGRESSBARPTR currentptr = pbfirstptr;
    00135     if(currentptr == NULL) return NULL;
    00136     do
    00137     {
    00138         if(strcmp(currentptr->pblabel, label) == 0) break;
    00139         currentptr = currentptr->nextbar;
    00140     } while(currentptr != pbfirstptr);
    00141
    00142     if(strcmp(currentptr->pblabel, label) != 0) return NULL;
    00143     return currentptr;
    00144 }
    00145
    00146 /*----------------------------------------------------------------------------------------------------------*/
    00147
    00148 void render_progress_bars(void)
    00149 {
    00150     PROGRESSBARPTR currentptr = pbfirstptr;
    00151     if(pbfirstptr != NULL || currentptr != NULL)
    00152     {
    00153         do
    00154         {
    00155             if(currentptr->bar_width > currentptr->width)
    00156             {
    00157                 currentptr->bar_width = currentptr->width;
    00158                 currentptr->done = true;
    00159             }
    00160             al_draw_rounded_rectangle(currentptr->x1, currentptr->y1, currentptr->x2, currentptr->y2, currentptr->rx,
    00161                                       currentptr->ry, currentptr->out_color, currentptr->line_thickness);
    00162             al_draw_filled_rounded_rectangle(currentptr->x1, currentptr->y1, currentptr->x1+currentptr->bar_width,
    00163                                              currentptr->y2, currentptr->rx, currentptr->ry, currentptr->in_color);
    00164             currentptr = currentptr->nextbar;
    00165         } while(currentptr != pbfirstptr);
    00166     }
    00167 }
    00168
    00169 /*----------------------------------------------------------------------------------------------------------*/
    00170
    00171 _Bool empty_progress_bar_list(PROGRESSBARPTR firstptr)
    00172 {
    00173     PROGRESSBARPTR currentptr = firstptr;
    00174     if(firstptr == NULL) return false;
    00175     if(firstptr->nextbar == firstptr && firstptr->prevbar == firstptr)
    00176     {
    00177         free(firstptr);
    00178         firstptr = NULL;
    00179     }
    00180     else if(firstptr->nextbar == firstptr->prevbar)
    00181     {
    00182         currentptr = currentptr->nextbar;
    00183         free(firstptr);
    00184         free(currentptr);
    00185         firstptr = NULL;
    00186     }
    00187     else
    00188     {
    00189         currentptr = currentptr->nextbar;
    00190         firstptr->prevbar->nextbar = NULL;
    00191         currentptr->prevbar = NULL;
    00192         free(firstptr);
    00193         firstptr = currentptr;
    00194
    00195         while(currentptr != NULL)
    00196         {
    00197             currentptr = currentptr->nextbar;
    00198             free(firstptr);
    00199             firstptr = currentptr;
    00200         }
    00201         firstptr = NULL;
    00202     }
    00203     return true;
    00204 }
    00205
    00206 /*----------------------------------------------------------------------------------------------------------*/
    00207
    00208 //    /**
    00209 //      *@}
    00210 //      */