Subversion Repositories Kolibri OS

Rev

Rev 5232 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include "game.h"
  2.  
  3. struct {
  4.     rect    reset_button;   // new game button place
  5.     rect    highscore_rect; // highscore place
  6.     rect    score_rect;     // score place
  7.     rect    over_rect;      // game over window
  8.     __u8    over;           // flag for game over
  9. } game;
  10.  
  11. void game_draw_ui()
  12. {
  13.     __menuet__make_button(game.reset_button.x,
  14.                           game.reset_button.y,
  15.                           game.reset_button.width,
  16.                           game.reset_button.height,
  17.                           NEW_GAME_BUTTON,
  18.                           BOARD_BG_COLOR);
  19.     rect_draw_text(&game.reset_button,"Restart",7,GAME_BG_COLOR,0);
  20.  
  21.     rect_draw(&game.highscore_rect,BOARD_BG_COLOR);
  22.     rect_draw_value(&game.highscore_rect,board_highscore(),GAME_BG_COLOR,0);
  23.  
  24.     rect_draw(&game.score_rect,BOARD_BG_COLOR);
  25.     rect_draw_value(&game.score_rect,board_score(),GAME_BG_COLOR,0);
  26.  
  27.     if (game.over)
  28.     {
  29.         __u16 line_step = FONT_HEIGHT * 2;
  30.         rect_draw(&game.over_rect,BOARD_BG_COLOR);
  31.  
  32.         rect line_rect = {
  33.             .x = game.over_rect.x,
  34.             .y = game.over_rect.y + line_step,
  35.             .width = game.over_rect.width,
  36.             .height = line_step
  37.         };
  38.  
  39.         rect_draw_text(&line_rect,"It looks like there is",22,0xFFFFFF,0);
  40.  
  41.         line_rect.y += line_step;
  42.         rect_draw_text(&line_rect,"no more moves",13,0xFFFFFF,0);
  43.  
  44.         line_rect.y += line_step;
  45.         rect_draw_text(&line_rect,"available",9,0xFFFFFF,0);
  46.     }
  47. }
  48.  
  49. void game_init()
  50. {
  51.     game.over = false;
  52.     // place window at the center of screen
  53.     __u16 screen_w = 0;
  54.     __u16 screen_h = 0;
  55.     __menuet__get_screen_max(&screen_w,&screen_h);
  56.  
  57.     __menuet__window_redraw(1);
  58.  
  59.     __menuet__define_window((screen_w - WND_WIDTH) / 2,
  60.                             (screen_h - WND_HEIGHT) / 2,
  61.                             WND_WIDTH,
  62.                             WND_HEIGHT,
  63.                             0x34 << 24 | GAME_BG_COLOR,
  64.                             0,
  65.                             (__u32)header);
  66.  
  67.     // find info about window client area
  68.     __menuet__get_process_table(&proc_info,PID_WHOAMI);
  69.  
  70.     // calc board
  71.     rect av_area = {0};
  72.     av_area.x = GAME_BORDER;
  73.     av_area.y = (SCORE_HEIGHT > GAME_BORDER) ? SCORE_HEIGHT : GAME_BORDER;
  74.     av_area.width = proc_info.client_width - av_area.x * 2;
  75.     av_area.height = proc_info.client_height - av_area.y - GAME_BORDER;
  76.     // minimal square
  77.     if (av_area.width < av_area.height)
  78.     {
  79.         av_area.y += (av_area.height - av_area.width) / 2;
  80.         av_area.height = av_area.width;
  81.     }
  82.     else // if (av_area.height < av_area.width)
  83.     {
  84.         av_area.x += (av_area.width - av_area.height) / 2;
  85.         av_area.width = av_area.height;
  86.     }
  87.  
  88.     board_init(&av_area);
  89.  
  90.     rect top_base = {
  91.         .x = av_area.x,
  92.         .y = (av_area.y - SCORE_HEIGHT) / 2,
  93.         .width = (av_area.width - BOARD_SPACING * 2) / 3,
  94.         .height = SCORE_HEIGHT
  95.     };
  96.  
  97.     game.reset_button = top_base;
  98.  
  99.     top_base.x += top_base.width + BOARD_SPACING;
  100.     game.highscore_rect = top_base;
  101.  
  102.     top_base.x += top_base.width + BOARD_SPACING;
  103.     game.score_rect = top_base;
  104.  
  105.     av_area.x += av_area.width / 4;
  106.     av_area.y += av_area.height / 4;
  107.     av_area.width /= 2;
  108.     av_area.height /= 2;
  109.  
  110.     game.over_rect = av_area;
  111.  
  112.     game_draw_ui();
  113.  
  114.     __menuet__window_redraw(2);
  115. }
  116.  
  117. void game_exit()
  118. {
  119.     board_delete();
  120. }
  121.  
  122. void game_redraw()
  123. {
  124.     __menuet__get_process_table(&proc_info,PID_WHOAMI);
  125.  
  126.     // start redraw
  127.     __menuet__window_redraw(1);
  128.  
  129.     __menuet__define_window(0,                          // __u16 x1     : ignored
  130.                             0,                          // __u16 y1     : ignored
  131.                             0,                          // __u16 xsize  : ignored
  132.                             0,                          // __u16 ysize  : ignored
  133.                             0x34 << 24 | GAME_BG_COLOR, // __u32 body_color
  134.                             0,                          // __u32 grab_color
  135.                             (__u32)header);             // __u32 frame_color or header
  136.  
  137.     game_draw_ui();
  138.     board_redraw();
  139.  
  140.     // end redraw
  141.     __menuet__window_redraw(2);
  142. }
  143.  
  144. void game_move_up()
  145. {
  146.     if (board_up())
  147.     {
  148.         board_redraw();
  149.         __u8 added = board_add_random_tile();
  150.         board_redraw();
  151.  
  152.         game.over = !added || !board_has_moves();
  153.         game_draw_ui();
  154.     }
  155. }
  156.  
  157. void game_move_down()
  158. {
  159.     if (board_down())
  160.     {
  161.         board_redraw();
  162.         __u8 added = board_add_random_tile();
  163.         board_redraw();
  164.  
  165.         game.over = !added || !board_has_moves();
  166.         game_draw_ui();
  167.     }
  168. }
  169.  
  170. void game_move_left()
  171. {
  172.     if (board_left())
  173.     {
  174.         board_redraw();
  175.         __u8 added = board_add_random_tile();
  176.         board_redraw();
  177.  
  178.         game.over = !added || !board_has_moves();
  179.         game_draw_ui();
  180.     }
  181. }
  182.  
  183. void game_move_right()
  184. {
  185.     if (board_right())
  186.     {
  187.         board_redraw();
  188.         __u8 added = board_add_random_tile();
  189.         board_redraw();
  190.  
  191.         game.over = !added || !board_has_moves();
  192.         game_draw_ui();
  193.     }
  194. }
  195.