Rename variables sectorbuf and verbose to avoid clashes in rbutil. Cleanup exports...
[Rockbox.git] / apps / plugins / dice.c
blobcde89e114475deb1cd4a3e7883d1ee45db58c452
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Brandon Low
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "plugin.h"
21 #include "pluginlib_actions.h"
22 #include "configfile.h"
24 #define MAX_DICES 12
25 #define INITIAL_NB_DICES 1
26 #define INITIAL_NB_SIDES 2 /* corresponds to 6 sides in the array */
28 #define DICE_QUIT PLA_QUIT
29 #define DICE_ROLL PLA_START
32 #define CFG_FILE "dice.cfg"
34 const struct button_mapping* plugin_contexts[]={generic_actions};
36 struct dices
38 int values[MAX_DICES];
39 int total;
40 int nb_dices;
41 int nb_sides;
44 #define PRINT_BUFFER_LENGTH MAX_DICES*4
45 PLUGIN_HEADER
47 static const struct plugin_api* rb;
48 static struct dices dice;
49 static int sides_index;
51 static struct opt_items nb_sides_option[8] = {
52 { "3", -1 },
53 { "4", -1 },
54 { "6", -1 },
55 { "8", -1 },
56 { "10", -1 },
57 { "12", -1 },
58 { "20", -1 },
59 { "100", -1 }
61 static int nb_sides_values[] = { 3, 4, 6, 8, 10, 12, 20, 100 };
62 static char *sides_conf[] = {"3", "4", "6", "8", "10", "12", "20", "100" };
63 static struct configdata config[] =
65 {TYPE_INT, 0, MAX_DICES, &dice.nb_dices, "dice count", NULL, NULL},
66 {TYPE_ENUM, 0, 8, &sides_index, "side count", sides_conf, NULL}
69 void dice_init(struct dices* dice);
70 void dice_roll(struct dices* dice);
71 void dice_print(struct dices* dice, struct screen* display);
72 bool dice_menu(struct dices* dice);
74 /* plugin entry point */
75 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter) {
76 (void)parameter;
77 rb = api;
78 int i, action;
80 dice_init(&dice);
81 rb->srand(*rb->current_tick);
83 configfile_init(rb);
84 configfile_load(CFG_FILE, config, 2, 0);
85 dice.nb_sides = nb_sides_values[sides_index];
86 if(!dice_menu(&dice))
88 configfile_save(CFG_FILE, config, 2, 0);
89 return PLUGIN_OK;
91 configfile_save(CFG_FILE, config, 2, 0);
92 dice_roll(&dice);
93 FOR_NB_SCREENS(i)
94 dice_print( &dice, rb->screens[i] );
95 while(true) {
96 action = pluginlib_getaction(rb, TIMEOUT_BLOCK,
97 plugin_contexts, 1);
98 switch(action) {
99 case DICE_ROLL:
100 dice_roll(&dice);
101 FOR_NB_SCREENS(i)
102 dice_print( &dice, rb->screens[i] );
103 break;
104 case DICE_QUIT:
105 return PLUGIN_OK;
110 void dice_init(struct dices* dice){
111 dice->nb_dices=INITIAL_NB_DICES;
112 sides_index=INITIAL_NB_SIDES;
116 void dice_roll(struct dices* dice) {
117 int i;
118 dice->total = 0;
119 for (i=0; i<dice->nb_dices; i++) {
120 dice->values[i] = rb->rand()%dice->nb_sides + 1;
121 dice->total+=dice->values[i];
125 void dice_print_string_buffer(struct dices* dice, char* buffer,
126 int start, int end){
127 int i, written;
128 for (i=start; i<end; i++) {
129 written=rb->snprintf(buffer, PRINT_BUFFER_LENGTH,
130 " %3d", dice->values[i]);
131 buffer=&(buffer[written]);
135 void dice_print(struct dices* dice, struct screen* display){
136 char buffer[PRINT_BUFFER_LENGTH];
137 /* display characteristics */
138 int char_height, char_width;
139 display->getstringsize("M", &char_width, &char_height);
140 int display_nb_row=display->height/char_height;
141 int display_nb_col=display->width/char_width;
143 int nb_dices_per_line=display_nb_col/4;/* 4 char per dice displayed*/
144 int nb_lines_required=dice->nb_dices/nb_dices_per_line;
145 int current_row=0;
146 if(dice->nb_dices%nb_dices_per_line!=0)
147 nb_lines_required++;
148 display->clear_display();
149 if(display_nb_row<nb_lines_required){
150 /* Put everything on the same scrolling line */
151 dice_print_string_buffer(dice, buffer, 0, dice->nb_dices);
152 display->puts_scroll(0, current_row, buffer);
153 current_row++;
154 }else{
155 int start=0;
156 int end=0;
157 for(;current_row<nb_lines_required;current_row++){
158 end=start+nb_dices_per_line;
159 if(end>dice->nb_dices)
160 end=dice->nb_dices;
161 dice_print_string_buffer(dice, buffer, start, end);
162 display->puts(0, current_row, buffer);
163 start=end;
166 rb->snprintf(buffer, PRINT_BUFFER_LENGTH, "Total: %d", dice->total);
167 display->puts_scroll(0, current_row, buffer);
168 display->update();
171 bool dice_menu(struct dices * dice) {
172 int selection;
173 bool menu_quit = false, result = false;
175 MENUITEM_STRINGLIST(menu,"Dice Menu",NULL,"Roll Dice","Number of Dice",
176 "Number of Sides","Quit");
179 while (!menu_quit) {
180 switch(rb->do_menu(&menu, &selection, NULL, false)){
181 case 0:
182 menu_quit = true;
183 result = true;
184 break;
186 case 1:
187 rb->set_int("Number of Dice", "", UNIT_INT, &(dice->nb_dices),
188 NULL, 1, 1, MAX_DICES, NULL );
189 break;
191 case 2:
192 rb->set_option("Number of Sides", &sides_index, INT,
193 nb_sides_option,
194 sizeof(nb_sides_values)/sizeof(int), NULL);
195 dice->nb_sides=nb_sides_values[sides_index];
196 break;
198 default:
199 menu_quit = true;
200 result = false;
201 break;
204 return result;