CHDK_DE Vorschauversion  Trunk Rev. 5163
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
gui_sokoban.c-Dateireferenz
#include "camera_info.h"
#include "stdlib.h"
#include "keyboard.h"
#include "modes.h"
#include "lang.h"
#include "conf.h"
#include "gui.h"
#include "gui_draw.h"
#include "gui_lang.h"
#include "gui_batt.h"
#include "gui_mbox.h"
#include "module_def.h"
#include "simple_module.h"
+ Include-Abhängigkeitsdiagramm für gui_sokoban.c:

gehe zum Quellcode dieser Datei

Datenstrukturen

struct  SokobanConf
 

Makrodefinitionen

#define FIELD_WIDTH   15
 
#define FIELD_HEIGHT   15
 
#define WALL_COLOR_1   COLOR_GREY
 
#define WALL_COLOR_2   COLOR_BLACK
 
#define BOX_COLOR_1   COLOR_RED
 
#define BOX_COLOR_2   COLOR_BLACK
 
#define BOX_COLOR_3   COLOR_YELLOW
 
#define PLACE_COLOR_1   COLOR_BLUE
 
#define PLACE_COLOR_2   COLOR_BLACK
 
#define PLAYER_COLOR_1   COLOR_GREEN
 
#define PLAYER_COLOR_2   COLOR_BLACK
 
#define MARKER_WALL   '#'
 
#define MARKER_BOX   '$'
 
#define MARKER_PLACE   '.'
 
#define MARKER_BOX_PLACE   '*'
 
#define MARKER_PLAYER   '@'
 
#define MARKER_PLAYER_PLACE   '+'
 
#define MARKER_EMPTY   '_'
 
#define MARKER_LINE_END   '\n'
 
#define MARKER_LEVEL_END   '!'
 
#define LEVEL_CHARS   "#$.*@+_"
 
#define UNDO_SIZE   1000
 
#define MAX_LEVELS   200
 

Funktionen

void gui_module_menu_kbd_process ()
 
int gui_sokoban_kbd_process ()
 
void gui_sokoban_draw ()
 
static void sokoban_undo_add (int dx, int dy, int box)
 
static void sokoban_undo ()
 
static void sokoban_redo ()
 
static void sokoban_undo_reset ()
 
static void sokoban_set_level (int lvl)
 
static int sokoban_finished ()
 
static void sokoban_next_level ()
 
static int sokoban_move (int dx, int dy)
 
static void sokoban_draw_box (int x, int y, twoColors cl)
 
int gui_sokoban_init ()
 
int _run ()
 
int _module_loader (unsigned int *chdk_export_list)
 
int _module_unloader ()
 
int _module_can_unload ()
 
int _module_exit_alt ()
 

Variablen

SokobanConf sconf
 
static ConfInfo conf_info []
 
gui_handler GUI_MODE_SOKOBAN
 
static const char * level_file_name ="A/CHDK/GAMES/SOKOBAN.LEV"
 
static unsigned short level_start_list [MAX_LEVELS]
 
static unsigned char level_length_list [MAX_LEVELS]
 
static unsigned num_levels
 
static int need_redraw
 
static int need_redraw_all
 
static int moves
 
static char field [FIELD_HEIGHT][FIELD_WIDTH]
 
static int cell_size
 
static int xPl
 
static int yPl
 
static int undo [UNDO_SIZE/10]
 
static int undo_begin
 
static int undo_end
 
static int undo_curr
 
static int running = 0
 
libsimple_sym _librun
 
ModuleInfo _module_info
 

Makro-Dokumentation

#define BOX_COLOR_1   COLOR_RED

Definiert in Zeile 43 der Datei gui_sokoban.c.

#define BOX_COLOR_2   COLOR_BLACK

Definiert in Zeile 44 der Datei gui_sokoban.c.

#define BOX_COLOR_3   COLOR_YELLOW

Definiert in Zeile 45 der Datei gui_sokoban.c.

#define FIELD_HEIGHT   15

Definiert in Zeile 39 der Datei gui_sokoban.c.

#define FIELD_WIDTH   15

Definiert in Zeile 38 der Datei gui_sokoban.c.

#define LEVEL_CHARS   "#$.*@+_"

Definiert in Zeile 61 der Datei gui_sokoban.c.

#define MARKER_BOX   '$'

Definiert in Zeile 52 der Datei gui_sokoban.c.

#define MARKER_BOX_PLACE   '*'

Definiert in Zeile 54 der Datei gui_sokoban.c.

#define MARKER_EMPTY   '_'

Definiert in Zeile 57 der Datei gui_sokoban.c.

#define MARKER_LEVEL_END   '!'

Definiert in Zeile 59 der Datei gui_sokoban.c.

#define MARKER_LINE_END   '\n'

Definiert in Zeile 58 der Datei gui_sokoban.c.

#define MARKER_PLACE   '.'

Definiert in Zeile 53 der Datei gui_sokoban.c.

#define MARKER_PLAYER   '@'

Definiert in Zeile 55 der Datei gui_sokoban.c.

#define MARKER_PLAYER_PLACE   '+'

Definiert in Zeile 56 der Datei gui_sokoban.c.

#define MARKER_WALL   '#'

Definiert in Zeile 51 der Datei gui_sokoban.c.

#define MAX_LEVELS   200

Definiert in Zeile 67 der Datei gui_sokoban.c.

#define PLACE_COLOR_1   COLOR_BLUE

Definiert in Zeile 46 der Datei gui_sokoban.c.

#define PLACE_COLOR_2   COLOR_BLACK

Definiert in Zeile 47 der Datei gui_sokoban.c.

#define PLAYER_COLOR_1   COLOR_GREEN

Definiert in Zeile 48 der Datei gui_sokoban.c.

#define PLAYER_COLOR_2   COLOR_BLACK

Definiert in Zeile 49 der Datei gui_sokoban.c.

#define UNDO_SIZE   1000

Definiert in Zeile 63 der Datei gui_sokoban.c.

#define WALL_COLOR_1   COLOR_GREY

Definiert in Zeile 41 der Datei gui_sokoban.c.

#define WALL_COLOR_2   COLOR_BLACK

Definiert in Zeile 42 der Datei gui_sokoban.c.

Dokumentation der Funktionen

int _module_can_unload ( )

Definiert in Zeile 512 der Datei gui_sokoban.c.

513 {
514  return running == 0;
515 }
int _module_exit_alt ( )

Definiert in Zeile 517 der Datei gui_sokoban.c.

518 {
519  running = 0;
520  return 0;
521 }
int _module_loader ( unsigned int *  chdk_export_list)

Definiert in Zeile 495 der Datei gui_sokoban.c.

496 {
497  sconf.sokoban_level = 0;
498  config_restore(&conf_info[0], "A/CHDK/MODULES/CFG/sokoban.cfg", 0);
499  return 0;
500 }
int _module_unloader ( )

Definiert in Zeile 506 der Datei gui_sokoban.c.

507 {
508  config_save(&conf_info[0], "A/CHDK/MODULES/CFG/sokoban.cfg", 99000);
509  return 0;
510 }
int _run ( )

Definiert in Zeile 476 der Datei gui_sokoban.c.

477 {
479  {
481  }
482  else
483  {
484  running = 1;
486  }
487 
488  return 0;
489 }
void gui_module_menu_kbd_process ( )

Definiert in Zeile 464 der Datei gui_sokoban.c.

465 {
466  running = 0;
468 }
void gui_sokoban_draw ( )

Definiert in Zeile 401 der Datei gui_sokoban.c.

401  {
402  int y, x;
403  static char str[16];
404 
405  if (need_redraw_all) {
407  need_redraw_all = 0;
408  need_redraw = 1;
409  }
410 
411  if (need_redraw) {
412  need_redraw = 0;
413  for (y=0; y<FIELD_HEIGHT; ++y) {
414  for (x=0; x<FIELD_WIDTH; ++x) {
415  switch (field[y][x]) {
416  case MARKER_WALL:
417  draw_rectangle(camera_screen.disp_left+x*cell_size, y*cell_size, camera_screen.disp_left+x*cell_size+cell_size-1, y*cell_size+cell_size-1, MAKE_COLOR(WALL_COLOR_1, WALL_COLOR_2), RECT_BORDER1|DRAW_FILLED);
418  break;
419  case MARKER_BOX:
421  break;
422  case MARKER_PLACE:
423  draw_rectangle(camera_screen.disp_left+x*cell_size, y*cell_size, camera_screen.disp_left+x*cell_size+cell_size-1, y*cell_size+cell_size-1, MAKE_COLOR(COLOR_BLACK, COLOR_BLACK), RECT_BORDER0|DRAW_FILLED);
424  draw_rectangle(camera_screen.disp_left+x*cell_size+4, y*cell_size+4, camera_screen.disp_left+x*cell_size+cell_size-1-4, y*cell_size+cell_size-1-4, MAKE_COLOR(PLACE_COLOR_1, PLACE_COLOR_2), RECT_BORDER1|DRAW_FILLED);
425  break;
426  case MARKER_BOX_PLACE:
428  break;
429  case MARKER_PLAYER:
430  case MARKER_PLAYER_PLACE:
431  draw_rectangle(camera_screen.disp_left+x*cell_size, y*cell_size, camera_screen.disp_left+x*cell_size+cell_size-1, y*cell_size+cell_size-1, MAKE_COLOR(COLOR_BLACK, COLOR_BLACK), RECT_BORDER0|DRAW_FILLED);
432  draw_ellipse(camera_screen.disp_left+x*cell_size+(cell_size>>1)-1, y*cell_size+(cell_size>>1)-1, (cell_size>>1)-3, (cell_size>>1)-3, PLAYER_COLOR_1, DRAW_FILLED);
433  break;
434  case MARKER_EMPTY:
435  default:
436  draw_rectangle(camera_screen.disp_left+x*cell_size, y*cell_size, camera_screen.disp_left+x*cell_size+cell_size-1, y*cell_size+cell_size-1, MAKE_COLOR(COLOR_BLACK, COLOR_BLACK), RECT_BORDER0|DRAW_FILLED);
437  break;
438  }
439  }
440  }
441 
442  draw_line(camera_screen.disp_left+cell_size*FIELD_WIDTH, 0, camera_screen.disp_left+cell_size*FIELD_WIDTH, camera_screen.height-1, COLOR_WHITE);
443  draw_line(camera_screen.disp_left+cell_size*FIELD_WIDTH+1, 0, camera_screen.disp_left+cell_size*FIELD_WIDTH+1, camera_screen.height-1, COLOR_BLACK);
444 
446  draw_string(camera_screen.disp_left+cell_size*FIELD_WIDTH+2, 8, str, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
447  sprintf(str, "%s: %-6d", lang_str(LANG_SOKOBAN_TEXT_MOVES), moves);
449 
450  if (sokoban_finished()) {
453  }
454  }
455 
456  sprintf(str, "Batt:%3d%%", get_batt_perc());
459 }
int gui_sokoban_init ( )

Definiert in Zeile 283 der Datei gui_sokoban.c.

283  {
284  /* first time through, load the file and make an index
285  if would could tell when the user left sokoban,
286  we could avoid this and malloc all the data structures
287  unfortunately, gui_mode gets set all over the place */
288  if(!num_levels) {
289  char *buf,*p;
290  FILE *fd;
291  struct stat st;
292 
293  if (stat((char *)level_file_name,&st) != 0 || st.st_size==0)
294  return 0;
295 
296  fd=fopen(level_file_name,"rb");
297  if(!fd)
298  return 0;
299 
300  buf=malloc(st.st_size+1);
301  if(!buf) {
302  fclose(fd);
303  return 0;
304  }
305 
306  fread(buf,1,st.st_size,fd);
307  buf[st.st_size]=0;
308  fclose(fd);
309  p = buf;
310  do {
311  // skip to the first level char
312  p = strpbrk(p,LEVEL_CHARS);
313  // found a level char, store the start
314  if (p) {
315  unsigned pos = p - buf;
316  if ( pos > 65535 ) {
317  break;
318  }
319  level_start_list[num_levels] = (unsigned short)pos;
321  // found the end char, store the end
322  if(p) {
323  unsigned len = p - (buf + level_start_list[num_levels]);
324  // bail on invalid level
325  if ( len > 255 ) {
326  break;
327  }
328  level_length_list[num_levels] = (unsigned char)len;
329  ++num_levels;
330  }
331  }
332  } while(p && num_levels < MAX_LEVELS);
333  free(buf);
334  }
335  if(!num_levels) {
336  return 0;
337  }
338  else if(sconf.sokoban_level >= num_levels) {
339  sconf.sokoban_level = 0;
340  }
342  // worst case scenario (640x480)
344  }
345  else {
347  }
349  // if the file is no longer readable, set_level will set this
350  if(!num_levels) {
351  return 0;
352  }
353  need_redraw_all = 1;
354 
356  return 1;
357 }
int gui_sokoban_kbd_process ( )

Definiert in Zeile 360 der Datei gui_sokoban.c.

360  {
361  switch (kbd_get_autoclicked_key()) {
362  case KEY_UP:
363  moves+=sokoban_move(0, -1);
364  need_redraw = 1;
365  break;
366  case KEY_DOWN:
367  moves+=sokoban_move(0, +1);
368  need_redraw = 1;
369  break;
370  case KEY_LEFT:
371  moves+=sokoban_move(-1, 0);
372  need_redraw = 1;
373  break;
374  case KEY_RIGHT:
375  moves+=sokoban_move(+1, 0);
376  need_redraw = 1;
377  break;
378  case KEY_SET:
379  if (moves == 0) {
381  }
382  break;
383  case KEY_ZOOM_OUT:
384  sokoban_undo();
385  need_redraw = 1;
386  break;
387  case KEY_ZOOM_IN:
388  sokoban_redo();
389  need_redraw = 1;
390  break;
391  case KEY_ERASE:
392  case KEY_DISPLAY:
394  need_redraw_all = 1;
395  break;
396  }
397  return 0;
398 }
static void sokoban_draw_box ( int  x,
int  y,
twoColors  cl 
)
static

Definiert in Zeile 274 der Datei gui_sokoban.c.

274  {
275  draw_rectangle(camera_screen.disp_left+x*cell_size, y*cell_size, camera_screen.disp_left+x*cell_size+cell_size-1, y*cell_size+cell_size-1, cl, RECT_BORDER1|DRAW_FILLED);
276  draw_line(camera_screen.disp_left+x*cell_size+2, y*cell_size, camera_screen.disp_left+x*cell_size+2, y*cell_size+cell_size-1, FG_COLOR(cl));
277  draw_line(camera_screen.disp_left+x*cell_size+cell_size-1-2, y*cell_size, camera_screen.disp_left+x*cell_size+cell_size-1-2, y*cell_size+cell_size-1, FG_COLOR(cl));
278  draw_line(camera_screen.disp_left+x*cell_size+2, y*cell_size+2, camera_screen.disp_left+x*cell_size+cell_size-1-2, y*cell_size+2, FG_COLOR(cl));
279  draw_line(camera_screen.disp_left+x*cell_size+2, y*cell_size+cell_size-1-2, camera_screen.disp_left+x*cell_size+cell_size-1-2, y*cell_size+cell_size-1-2, FG_COLOR(cl));
280 }
static int sokoban_finished ( )
static

Definiert in Zeile 228 der Datei gui_sokoban.c.

228  {
229  int x, y;
230 
231  for (y=0; y<FIELD_HEIGHT; ++y)
232  for (x=0; x<FIELD_WIDTH; ++x)
233  if (field[y][x]==MARKER_BOX)
234  return 0;
235  return 1;
236 }
static int sokoban_move ( int  dx,
int  dy 
)
static

Definiert in Zeile 246 der Datei gui_sokoban.c.

246  {
247  switch (field[yPl+dy][xPl+dx]) {
248  case MARKER_WALL:
249  return 0;
250  break;
251  case MARKER_BOX:
252  case MARKER_BOX_PLACE:
253  if (field[yPl+dy*2][xPl+dx*2]==MARKER_WALL || field[yPl+dy*2][xPl+dx*2]==MARKER_BOX || field[yPl+dy*2][xPl+dx*2]==MARKER_BOX_PLACE)
254  return 0;
255  break;
256  case MARKER_PLACE:
257  case MARKER_EMPTY:
258  break;
259  }
261  xPl+=dx; yPl+=dy;
265  sokoban_undo_add(dx, dy, 1);
266  } else {
267  sokoban_undo_add(dx, dy, 0);
268  }
270  return 1;
271 }
static void sokoban_next_level ( )
static

Definiert in Zeile 239 der Datei gui_sokoban.c.

239  {
242  need_redraw_all = 1;
243 }
static void sokoban_redo ( )
static

Definiert in Zeile 130 der Datei gui_sokoban.c.

130  {
131  int dx=0, dy=0, value;
132 
133  if (undo_curr!=undo_end) {
134  value = (undo[undo_curr/10]>>((undo_curr%10)*3))&7;
135  if (value&1) dy=1; else dx=1;
136  if (value&2) {dy=-dy; dx=-dx;}
137 
139  xPl+=dx; yPl+=dy;
140  if (value&4) {
143  }
145  ++moves;
146 
147  ++undo_curr;
148  if (undo_curr==UNDO_SIZE) undo_curr=0;
149  }
150 }
static void sokoban_set_level ( int  lvl)
static

Definiert in Zeile 158 der Datei gui_sokoban.c.

158  {
159  int x=0, y, w=0, h=0;
160  const char *p;
161  char *buf;
162  FILE *fd;
163  int start,len;
164 
165  len=level_length_list[lvl];
166  start=level_start_list[lvl];
167  fd=fopen(level_file_name,"rb");
168  if(!fd) {
169  num_levels=0;
170  return;
171  }
172 
173  buf=malloc(len+1);
174  if(!buf) {
175  fclose(fd);
176  return;
177  }
178 
179  if(fseek(fd,start,SEEK_SET) != 0) {
180  fclose(fd);
181  free(buf);
182  return;
183  }
184  fread(buf,1,len,fd);
185  buf[len]=0;
186  fclose(fd);
187 
188  p=buf;
189 
190  // determine dimensions
191  while (*p) {
192  if (*p==MARKER_LINE_END) {
193  ++h;
194  if (x>w) w=x;
195  x=0;
196  } else {
197  ++x;
198  }
199  ++p;
200  }
201  if (x>w) w=x;
202  h-=1; //the last line didn't previously have an end marker
203 
204  // clear field
205  for (y=0; y<FIELD_HEIGHT; ++y)
206  for (x=0; x<FIELD_WIDTH; ++x)
207  field[y][x]=MARKER_EMPTY;
208 
209  // place maze at the center
210  p=buf;
211  for (y=(FIELD_HEIGHT-h)/2; y<FIELD_HEIGHT; ++y, ++p) {
212  for (x=(FIELD_WIDTH-w)/2; x<FIELD_WIDTH && *p && *p!=MARKER_LINE_END; ++x, ++p) {
213  field[y][x]=*p;
214  if (field[y][x] == MARKER_PLAYER || field[y][x] == MARKER_PLAYER_PLACE) {
215  xPl = x; yPl = y;
216  }
217  }
218  if (!*p || (*p == MARKER_LINE_END && !*(p+1))) break;
219  }
220 
221  free(buf);
222  sconf.sokoban_level = lvl;
223  moves = 0;
225 }
static void sokoban_undo ( )
static

Definiert in Zeile 107 der Datei gui_sokoban.c.

107  {
108  int dx=0, dy=0, value;
109 
110  if (undo_curr!=undo_begin) {
111  if (undo_curr==0) undo_curr=UNDO_SIZE;
112  --undo_curr;
113 
114  value = (undo[undo_curr/10]>>((undo_curr%10)*3))&7;
115  if (value&1) dy=1; else dx=1;
116  if (value&2) {dy=-dy; dx=-dx;}
117 
119  if (value&4) {
122  }
123  xPl-=dx; yPl-=dy;
125  --moves;
126  }
127 }
static void sokoban_undo_add ( int  dx,
int  dy,
int  box 
)
static

Definiert in Zeile 84 der Datei gui_sokoban.c.

84  {
85  int offs, bits, value;
86 
87  value = ((box)?1:0)<<2;
88  if (dx) {
89  value |= ((dx<0)?1:0)<<1;
90  } else {
91  value |= (((dy<0)?1:0)<<1)|1;
92  }
93 
94  offs = undo_curr/10;
95  bits = (undo_curr%10)*3;
96  undo[offs] &= ~(7<<bits);
97  undo[offs] |= (value&7)<<bits;
98 
99  if (++undo_curr==UNDO_SIZE) undo_curr=0;
100  if (undo_curr==undo_begin) {
101  if (++undo_begin==UNDO_SIZE) undo_begin=0;
102  }
104 }
static void sokoban_undo_reset ( )
static

Definiert in Zeile 153 der Datei gui_sokoban.c.

153  {
155 }

Variablen-Dokumentation

libsimple_sym _librun
Initialisierung:

Definiert in Zeile 525 der Datei gui_sokoban.c.

int cell_size
static

Definiert in Zeile 77 der Datei gui_sokoban.c.

ConfInfo conf_info[]
static
Initialisierung:
= {
{0,0,0,0,{0}}
}

Definiert in Zeile 25 der Datei gui_sokoban.c.

char field[FIELD_HEIGHT][FIELD_WIDTH]
static

Definiert in Zeile 75 der Datei gui_sokoban.c.

const char* level_file_name ="A/CHDK/GAMES/SOKOBAN.LEV"
static

Definiert in Zeile 66 der Datei gui_sokoban.c.

unsigned char level_length_list[MAX_LEVELS]
static

Definiert in Zeile 69 der Datei gui_sokoban.c.

unsigned short level_start_list[MAX_LEVELS]
static

Definiert in Zeile 68 der Datei gui_sokoban.c.

int moves
static

Definiert in Zeile 74 der Datei gui_sokoban.c.

int need_redraw
static

Definiert in Zeile 72 der Datei gui_sokoban.c.

int need_redraw_all
static

Definiert in Zeile 73 der Datei gui_sokoban.c.

unsigned num_levels
static

Definiert in Zeile 70 der Datei gui_sokoban.c.

int running = 0
static

Definiert in Zeile 462 der Datei gui_sokoban.c.

SokobanConf sconf

Definiert in Zeile 23 der Datei gui_sokoban.c.

int undo[UNDO_SIZE/10]
static

Definiert in Zeile 80 der Datei gui_sokoban.c.

int undo_begin
static

Definiert in Zeile 81 der Datei gui_sokoban.c.

int undo_curr
static

Definiert in Zeile 81 der Datei gui_sokoban.c.

int undo_end
static

Definiert in Zeile 81 der Datei gui_sokoban.c.

int xPl
static

Definiert in Zeile 78 der Datei gui_sokoban.c.

int yPl
static

Definiert in Zeile 78 der Datei gui_sokoban.c.