source: trunk/core/games/gui_mastermind.c @ 1594

Revision 1594, 8.3 KB checked in by philmoz, 16 months ago (diff)

Some cleanup of the main trunk by moving all the game related files to a separate directory.
Also fixes a problem in the curves code introduced when it was converted to a module.

  • Property svn:eol-style set to native
Line 
1#include "stdlib.h"
2#include "keyboard.h"
3#include "platform.h"
4#include "core.h"
5#include "lang.h"
6#include "conf.h"
7#include "gui.h"
8#include "gui_draw.h"
9#include "gui_lang.h"
10#include "gui_batt.h"
11#include "gui_mbox.h"
12
13#include "module_load.h"
14
15void gui_module_menu_kbd_process();
16void gui_mastermind_kbd_process();
17void gui_mastermind_draw(int enforce_redraw);
18
19gui_handler GUI_MODE_MASTERMIND =
20    /*GUI_MODE_MASTERMIND*/ { GUI_MODE_MODULE,   gui_mastermind_draw,  gui_mastermind_kbd_process, gui_module_menu_kbd_process, GUI_MODE_FLAG_NODRAWRESTORE, GUI_MODE_MAGICNUM };
21
22
23#define BORDER                          20
24#define RECT_SIZE                       10
25#define COLOR_LIGHT_GRAY        MAKE_COLOR(COLOR_SPLASH_GREY,COLOR_SPLASH_GREY)
26#define BK_COLOR                        MAKE_COLOR(COLOR_GREY,COLOR_GREY)
27#define TEXT_COLOR          MAKE_COLOR(COLOR_GREY,COLOR_BLACK)
28
29int curr_x;
30int curr_y;
31int answer[4];
32char colors[6];
33int curr_color[4];
34int GameGo;
35static char buf[128];
36
37static void guess_box(int pos, color col)
38{
39        draw_filled_rect(camera_screen.ts_button_border+100+(pos*10), BORDER+(2*curr_y*10)+2+2*curr_y, camera_screen.ts_button_border+100+(pos*10)+6, BORDER+(2*curr_y*10)+8+2*curr_y , MAKE_COLOR(col,col));
40}
41
42char WinQuary(){
43        char pos=0;
44        int i, j;
45       
46        for (i=0; i<4; i++)
47    {
48                if (answer[i] == curr_color[i])
49        {
50            guess_box(pos,COLOR_BLACK);
51                        pos++;
52                }
53        }
54       
55        if (pos == 4)
56    {
57                GameGo = 0;
58                return 1;
59        }
60       
61        for (i=0; i<4; i++)
62    {
63        for (j=0; j<4; j++)
64        {
65                    if ((answer[i] == curr_color[j]) && (i != j))
66            {
67                guess_box(pos,COLOR_WHITE);
68                            pos++;
69            }
70                }
71        }
72
73        if(curr_y==0)
74    {
75                GameGo = 0;
76                return 2;
77        }
78
79        return 0;
80}
81
82char CanNext(){
83        if (curr_color[0]==99 || curr_color[1]==99 || curr_color[2]==99 || curr_color[3]==99)
84                return 0;
85        else if (curr_color[0]==curr_color[1] || curr_color[0]==curr_color[2] || curr_color[0]==curr_color[3] ||
86                     curr_color[1]==curr_color[2] || curr_color[1]==curr_color[3] || curr_color[2]==curr_color[3])
87    {
88                draw_string(camera_screen.ts_button_border+167, 130, lang_str(LANG_MENU_GAMES_DCOLOR), TEXT_COLOR);
89                return 0;
90        }
91        return 1;
92}
93
94void CreateColorCombo(){
95        char tmp=0;
96        int i=0;
97       
98        for (i=0; i<4; i++) answer[i] = 99;
99        i = 0;
100        while (i<4)
101    {
102                tmp = rand() % 6;
103                if (answer[0]!=tmp && answer[1]!=tmp && answer[2]!=tmp && answer[3]!=tmp)
104        {
105                        answer[i] = tmp;
106                        i++;
107                }
108        }
109}
110
111int gui_mastermind_init() {
112        int i,j;
113
114        curr_x=0;
115        curr_y=7;
116       
117        draw_filled_rect( 0, 0, camera_screen.width-1, camera_screen.height-1, BK_COLOR);
118
119        for (i=0;i<4;i++)
120                for (j=0;j<8;j++)
121                        draw_filled_rect(camera_screen.ts_button_border+BORDER+(2*i*10), BORDER+(2*j*10)+2*j, camera_screen.ts_button_border+BORDER+(2*i*10)+10, BORDER+(2*j*10)+2*j+10 , COLOR_LIGHT_GRAY);
122
123        draw_filled_rect(camera_screen.ts_button_border+10, BORDER+(2*j*10)+2*j, camera_screen.ts_button_border+150,BORDER+(2*j*10)+2*j+1, MAKE_COLOR(COLOR_GREY,COLOR_WHITE));
124        draw_filled_rect(camera_screen.ts_button_border+148, 10, camera_screen.ts_button_border+149,230, MAKE_COLOR(COLOR_GREY,COLOR_WHITE));
125        draw_filled_rect(camera_screen.ts_button_border+151, 10, camera_screen.ts_button_border+152,230, MAKE_COLOR(COLOR_GREY,COLOR_WHITE));
126       
127        for (i=0; i<6;i++)
128                draw_filled_rect(camera_screen.ts_button_border+BORDER+(2*(i+7)*10)+20, 40, camera_screen.ts_button_border+BORDER+(2*(i+7)*10)+35,55, MAKE_COLOR(colors[i],colors[i]));
129               
130        draw_filled_rect(camera_screen.ts_button_border+BORDER+(2*7*10)+20, 65, camera_screen.ts_button_border+BORDER+(2*7*10)+35,80, MAKE_COLOR(COLOR_BLACK,COLOR_BLACK));
131        draw_string(camera_screen.ts_button_border+BORDER+(2*7*10)+50, 65 , lang_str(LANG_MENU_GAMES_RIGHT_PLACE), TEXT_COLOR);
132       
133        draw_filled_rect(camera_screen.ts_button_border+BORDER+(2*7*10)+20, 90, camera_screen.ts_button_border+BORDER+(2*7*10)+35,105, MAKE_COLOR(COLOR_WHITE,COLOR_WHITE));
134        draw_string(camera_screen.ts_button_border+BORDER+(2*7*10)+50, 90 , lang_str(LANG_MENU_GAMES_C_IN_ANSWER), TEXT_COLOR);
135                                       
136        draw_string(camera_screen.ts_button_border+173, 20 , lang_str(LANG_MENU_GAMES_AC_COLORS), TEXT_COLOR);
137               
138        draw_string(camera_screen.ts_button_border+167, 200 , lang_str(LANG_MENU_GAMES_CURSOR1), TEXT_COLOR);
139        draw_string(camera_screen.ts_button_border+167, 185 , lang_str(LANG_MENU_GAMES_CURSOR2), TEXT_COLOR);
140        draw_string(camera_screen.ts_button_border+167, 170 , lang_str(LANG_MENU_GAMES_CURSOR3), TEXT_COLOR);
141
142        for(i=0;i<4;i++) curr_color[i]=99;
143       
144    gui_set_mode(&GUI_MODE_MASTERMIND);
145        return 1;
146}
147
148//-------------------------------------------------------------------
149static void draw_box(color border)
150{
151    if (curr_color[curr_x] == 99) curr_color[curr_x] = 0;
152    draw_filled_rect(camera_screen.ts_button_border+BORDER+(2*curr_x*10), BORDER+(2*curr_y*10)+2*curr_y, camera_screen.ts_button_border+BORDER+(2*curr_x*10)+10, BORDER+(2*curr_y*10)+2*curr_y+10 , MAKE_COLOR(colors[curr_color[curr_x]],border));
153}
154
155static void end_game(int msg)
156{
157    int i;
158    draw_string(camera_screen.ts_button_border+198, 130 , lang_str(msg), TEXT_COLOR);
159        for (i=0; i<4;i++)
160            draw_filled_rect(camera_screen.ts_button_border+BORDER+(2*i*10), 213, camera_screen.ts_button_border+BORDER+(2*i*10)+10,223,  MAKE_COLOR(colors[answer[i]],colors[answer[i]]));
161}
162
163static void chg_box(int inc_box, int inc_val)
164{
165    draw_box(colors[curr_color[curr_x]]);
166    curr_x = (curr_x + inc_box) & 3;
167    if (curr_color[curr_x] == 99) curr_color[curr_x] = 0;
168    curr_color[curr_x] = (curr_color[curr_x] + inc_val);
169    if (curr_color[curr_x] < 0) curr_color[curr_x] = 5;
170    else if (curr_color[curr_x] > 5) curr_color[curr_x] = 0;
171    draw_box(COLOR_BLACK);
172}
173
174void gui_mastermind_kbd_process() {
175        int i=0;
176        rand();
177    long key = kbd_get_autoclicked_key();
178    if (key)
179    {
180        if (GameGo == 1)
181        {
182            draw_string(camera_screen.ts_button_border+167, 130 , "                       ", TEXT_COLOR);
183                switch (kbd_get_autoclicked_key())
184                {
185                        case KEY_SET:
186                                if(CanNext())
187                    {
188                                        if(WinQuary() == 1)
189                        {
190                            end_game(LANG_MENU_GAMES_RIGHT);
191                                        }
192                                        else if(WinQuary() == 2)
193                        {
194                            end_game(LANG_MENU_GAMES_GAME_OVER);
195                                        }
196                                        else
197                        {
198                            draw_box(colors[curr_color[curr_x]]);
199                                                for(i=0;i<4;i++) curr_color[i]=99;
200                                                curr_y--;
201                                                curr_x=0;
202                            draw_box(COLOR_BLACK);
203                                        }
204                                }
205                                break;
206                        case KEY_LEFT:
207                    chg_box(-1,0);
208                                break;
209                        case KEY_RIGHT:
210                    chg_box(1,0);
211                                break;
212                        case KEY_UP:
213                    chg_box(0,1);
214                                break;
215                        case KEY_DOWN:
216                    chg_box(0,-1);
217                                break;
218                }
219        }
220        else if (key == KEY_SET)
221        {
222                        gui_mastermind_init();
223                        CreateColorCombo();
224            draw_box(COLOR_BLACK);
225                        GameGo=1;
226        }
227    }
228}
229//-------------------------------------------------------------------
230void gui_mastermind_draw(int enforce_redraw) {
231        unsigned long t;
232    static struct tm *ttm;
233
234    draw_txt_string(camera_screen.ts_button_border/FONT_WIDTH+15, 0, lang_str(LANG_MENU_GAMES_MASTERMIND), MAKE_COLOR(COLOR_GREY, COLOR_WHITE));
235
236    t = time(NULL);
237    ttm = localtime(&t);
238    sprintf(buf, "Time: %2u:%02u  Batt:%3d%%", ttm->tm_hour, ttm->tm_min, get_batt_perc());
239    draw_txt_string((camera_screen.width-camera_screen.ts_button_border)/FONT_WIDTH-2-1-1-9-2-5-4, camera_screen.height/FONT_HEIGHT-1, buf, TEXT_COLOR);
240}
241
242
243int basic_module_init() {
244        colors[0] = COLOR_HISTO_R_PLAY;
245        colors[1] = COLOR_HISTO_G_PLAY;
246        colors[2] = COLOR_HISTO_B_PLAY;
247        colors[3] = COLOR_YELLOW;
248        colors[4] = COLOR_WHITE;
249        colors[5] = COLOR_BLACK;
250
251        return gui_mastermind_init();
252}
253
254extern int module_idx;
255void gui_module_menu_kbd_process() {
256        gui_default_kbd_process_menu_btn();
257        module_async_unload(module_idx);
258}
259
260/******************** Module Information structure ******************/
261
262struct ModuleInfo _module_info = {      MODULEINFO_V1_MAGICNUM,
263                                                                        sizeof(struct ModuleInfo),
264
265                                                                        ANY_CHDK_BRANCH, 0,                     // Requirements of CHDK version
266                                                                        ANY_PLATFORM_ALLOWED,           // Specify platform dependency
267                                                                        0,                                                      // flag
268                                                                        -LANG_MENU_GAMES_MASTERMIND,// Module name
269                                                                        1, 0,                                           // Module version
270                                                                        (int32_t)"Game"
271                                                                 };
Note: See TracBrowser for help on using the repository browser.