Puzzle.c
上传用户:lqx1163
上传日期:2014-08-13
资源大小:9183k
文件大小:71k
源码类别:

MTK

开发平台:

C/C++

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*****************************************************************************
  36.  * Filename:
  37.  * ---------
  38.  *  Puzzle.c
  39.  *
  40.  * Project:
  41.  * --------
  42.  *   Maui
  43.  *
  44.  * Description:
  45.  * ------------
  46.  *  Game Puzzle function.
  47.  *
  48.  * Author:
  49.  * -------
  50.  * -------
  51.  * -------
  52.  *
  53.  *============================================================================
  54.  *             HISTORY
  55.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  56.  *------------------------------------------------------------------------------
  57.  * removed!
  58.  *
  59.  * removed!
  60.  * removed!
  61.  * removed!
  62.  *
  63.  * removed!
  64.  * removed!
  65.  * removed!
  66.  *
  67.  * removed!
  68.  * removed!
  69.  * removed!
  70.  *
  71.  * removed!
  72.  * removed!
  73.  * removed!
  74.  *
  75.  * removed!
  76.  * removed!
  77.  * removed!
  78.  *
  79.  * removed!
  80.  * removed!
  81.  * removed!
  82.  *
  83.  * removed!
  84.  * removed!
  85.  * removed!
  86.  *
  87.  * removed!
  88.  * removed!
  89.  * removed!
  90.  *
  91.  * removed!
  92.  * removed!
  93.  * removed!
  94.  *
  95.  * removed!
  96.  * removed!
  97.  * removed!
  98.  *
  99.  * removed!
  100.  * removed!
  101.  * removed!
  102.  *
  103.  *------------------------------------------------------------------------------
  104.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  105.  *============================================================================
  106.  ****************************************************************************/
  107. #include "MMI_features.h"
  108. #include "GameDefs.h"
  109. #ifdef IS_PUZZLE_BASE_GAME
  110. #include "GameInc.h"
  111. #include "PuzzleProts.h"
  112. #include "PuzzleDefs.h"
  113. #ifdef __MMI_TOUCH_SCREEN__
  114. #include "wgui_touch_screen.h"
  115. #endif 
  116. /***************************************************************************** 
  117. * Define
  118. *****************************************************************************/
  119. #define MMI_GX_PUZZLE_BACKGROUND_COLOR (gui_color(71, 71, 71))
  120. #define MMI_GX_PUZZLE_LINE_COLOR       (gui_color(235, 235, 235))
  121. #define MMI_GX_PUZZLE_LIGHT_BORDER     (gui_color(133, 133, 133))
  122. #define MMI_GX_PUZZLE_DARK_BORDER      (gui_color(16, 16, 16))
  123. #define MMI_GX_PUZZLE_EASY             (0)
  124. #define MMI_GX_PUZZLE_NORMAL           (1)
  125. #define MMI_GX_PUZZLE_HARD             (2)
  126. #ifdef __MMI_GAME_PUZZLE_128x160__
  127. #define MMI_GX_PUZZLE_TIME_POSITION_X  (8)
  128. #define MMI_GX_PUZZLE_TIME_POSITION_Y  (14)
  129. #define MMI_GX_PUZZLE_STEP_POSITION_X  (90)
  130. #define MMI_GX_PUZZLE_STEP_POSITION_Y  (14)
  131. #define MMI_GX_PUZZLE_LCD_HEIGHT       (160)
  132. #define MMI_GX_PUZZLE_LCD_WIDTH        (128)
  133. #elif defined(__MMI_GAME_PUZZLE_128x128__)
  134. #define MMI_GX_PUZZLE_TIME_POSITION_X  (24)
  135. #define MMI_GX_PUZZLE_TIME_POSITION_Y  (49)
  136. #define MMI_GX_PUZZLE_STEP_POSITION_X  (24)
  137. #define MMI_GX_PUZZLE_STEP_POSITION_Y  (69)
  138. #define MMI_GX_PUZZLE_LCD_HEIGHT       (128)
  139. #define MMI_GX_PUZZLE_STATUS_POS_X     (17)
  140. #define MMI_GX_PUZZLE_STATUS_POS_Y     (40)
  141. #define MMI_GX_PUZZLE_STATUS_WIDTH     (90)
  142. #define MMI_GX_PUZZLE_STATUS_HEIGHT    (44)
  143. #define MMI_GX_PUZZLE_STATUS_SHADOW    (3)
  144. #define MMI_GX_PUZZLE_LCD_WIDTH        (128)
  145. #elif defined(__MMI_GAME_PUZZLE_176x220__)
  146. #define MMI_GX_PUZZLE_TIME_POSITION_X  (14)
  147. #define MMI_GX_PUZZLE_TIME_POSITION_Y  (63)
  148. #define MMI_GX_PUZZLE_STEP_POSITION_X  (123)
  149. #define MMI_GX_PUZZLE_STEP_POSITION_Y  (63)
  150. #define MMI_GX_PUZZLE_LCD_HEIGHT       (220)
  151. #define MMI_GX_PUZZLE_LCD_WIDTH        (176)
  152. #define MMI_GX_PUZZLE_BAR_COLOR        (MMI_GX_PUZZLE_LINE_COLOR)
  153. #elif defined(__MMI_GAME_PUZZLE_240x320__)
  154. #define MMI_GX_PUZZLE_TIME_POSITION_X  (20)
  155. #define MMI_GX_PUZZLE_TIME_POSITION_Y  (132)
  156. #define MMI_GX_PUZZLE_STEP_POSITION_X  (160)
  157. #define MMI_GX_PUZZLE_STEP_POSITION_Y  (132)
  158. #define MMI_GX_PUZZLE_BAR_COLOR        (MMI_GX_PUZZLE_LINE_COLOR)
  159. #define MMI_GX_PUZZLE_LCD_HEIGHT       (320)
  160. #define MMI_GX_PUZZLE_LCD_WIDTH        (240)
  161. #endif 
  162. #if defined(__MMI_GAME_PUZZLE_128x160__) || defined(__MMI_GAME_PUZZLE_128x128__)
  163. #define MMI_GX_PUZZLE_ORIGIN_X         (4)
  164. #define MMI_GX_PUZZLE_ORIGIN_Y         (4)
  165. #define MMI_GX_PUZZLE_SIZE             (120)
  166. #define MMI_GX_PUZZLE_LEFT_BAR_WIDTH   (4)
  167. #define MMI_GX_PUZZLE_RIGHT_BAR_WIDTH  (4)
  168. #define MMI_GX_PUZZLE_TOP_BAR_WIDTH    (4)
  169. #define MMI_GX_PUZZLE_BOTTOM_BAR_WIDTH (4)
  170. #define MMI_GX_PUZZLE_DIGIT_WIDTH      (4)
  171. #elif defined(__MMI_GAME_PUZZLE_176x220__)
  172. #define MMI_GX_PUZZLE_ORIGIN_X         (6)
  173. #define MMI_GX_PUZZLE_ORIGIN_Y         (6)
  174. #define MMI_GX_PUZZLE_SIZE             (165)
  175. #define MMI_GX_PUZZLE_LEFT_BAR_WIDTH   (6)
  176. #define MMI_GX_PUZZLE_RIGHT_BAR_WIDTH  (5)
  177. #define MMI_GX_PUZZLE_TOP_BAR_WIDTH    (6)
  178. #define MMI_GX_PUZZLE_BOTTOM_BAR_WIDTH (6)
  179. #define MMI_GX_PUZZLE_DIGIT_WIDTH      (6)
  180. #define MMI_GX_PUZZLE_SOFTKEY_X (150)
  181. #define MMI_GX_PUZZLE_SOFTKEY_Y (205)
  182. #define MMI_GX_PUZZLE_SOFTKEY_WIDTH (20)
  183. #define MMI_GX_PUZZLE_SOFTKEY_HEIGHT (10)
  184. #elif defined(__MMI_GAME_PUZZLE_240x320__)
  185. #define MMI_GX_PUZZLE_ORIGIN_X         (7)
  186. #define MMI_GX_PUZZLE_ORIGIN_Y         (7)
  187. #define MMI_GX_PUZZLE_SIZE             (225)
  188. #define MMI_GX_PUZZLE_LEFT_BAR_WIDTH   (8)
  189. #define MMI_GX_PUZZLE_RIGHT_BAR_WIDTH  (7)
  190. #define MMI_GX_PUZZLE_TOP_BAR_WIDTH    (8)
  191. #define MMI_GX_PUZZLE_BOTTOM_BAR_WIDTH (8)
  192. #define MMI_GX_PUZZLE_DIGIT_WIDTH      (9)
  193. #define MMI_GX_PUZZLE_SOFTKEY_X (190)
  194. #define MMI_GX_PUZZLE_SOFTKEY_Y (299)
  195. #define MMI_GX_PUZZLE_SOFTKEY_WIDTH (40)
  196. #define MMI_GX_PUZZLE_SOFTKEY_HEIGHT (20)
  197. #endif 
  198. /***************************************************************************** 
  199. * Typedef 
  200. *****************************************************************************/
  201. /* game context */
  202. typedef struct
  203. {
  204.     S16 x;
  205.     S16 y;
  206. } gx_puzzle_position_struct;
  207. typedef struct
  208. {
  209.     BOOL is_gameover;
  210.     BOOL is_new_game;
  211.     U8 game_level;
  212.     S16 game_grade;
  213.     U16 timer_elapse;
  214.     U16 step;
  215.     U16 tick;
  216.     U8 softkey_state;
  217.     U16 click;
  218.     S16 destination_x;
  219.     S16 destination_y;
  220.     S16 puzzle[5][5];
  221.     MMI_ID_TYPE image;
  222.     MMI_ID_TYPE small_image;
  223.     gx_puzzle_position_struct null_grid;
  224.     S32 long_tick;
  225. #ifdef __MMI_GAME_PUZZLE_128x128__
  226.     BOOL is_show_status;
  227. #endif 
  228. #if defined (__MMI_GAME_MULTICHANNEL_SOUND__)
  229.     S32 move_midi;
  230. #endif 
  231. } gx_puzzle_context_struct;
  232. /***************************************************************************** 
  233. * Local Variable
  234. *****************************************************************************/
  235. gx_puzzle_context_struct g_gx_puzzle_context = 
  236. {
  237.     FALSE,  /* is_gameover */
  238.     TRUE,   /* is_new_game */
  239.     0,      /* game_level */
  240.     0,      /* game_grade */
  241.     100,    /* timer_elapse */
  242.     0,      /* how many steps user went */
  243.     0,      /* global tick */
  244.     0,      /* softkey state */
  245.     0       /* user clicks */
  246. };
  247. /***************************************************************************** 
  248. * Local Function
  249. *****************************************************************************/
  250. /* Game framework related functions */
  251. S16 mmi_gx_puzzle_calc_best_grade(S16 old_grade, S16 new_grade);        /* descide which is best grade */
  252. void mmi_gx_puzzle_enter_game(void);    /* entry function of the game */
  253. void mmi_gx_puzzle_exit_game(void);     /* exit function - usually will stop timer and release buffer */
  254. void mmi_gx_puzzle_draw_gameover(void); /* draw gameover screen */
  255. /* Game play functions */
  256. void mmi_gx_puzzle_framemove(void);
  257. void mmi_gx_puzzle_render(void);
  258. void mmi_gx_puzzle_gameover(void);
  259. void mmi_gx_puzzle_cyclic_timer(void);
  260. void mmi_gx_puzzle_init_game(void); /* draw gameover screen */
  261. void mmi_gx_puzzle_draw_static_background(void);
  262. void mmi_gx_puzzle_key_2_release(void);
  263. void mmi_gx_puzzle_key_4_release(void);
  264. void mmi_gx_puzzle_key_6_release(void);
  265. void mmi_gx_puzzle_key_8_release(void);
  266. #ifdef __MMI_GAME_PUZZLE_128x128__
  267. void mmi_gx_puzzle_key_5_press(void);
  268. void mmi_gx_puzzle_key_5_release(void);
  269. #endif /* __MMI_GAME_PUZZLE_128x128__ */ 
  270. void mmi_gx_puzzle_show_puzzle(void);
  271. void mmi_gx_puzzle_move_and_show(void);
  272. void mmi_gx_puzzle_my_show_image(long x, long y, S16 input_image_id);
  273. void mmi_gx_puzzle_draw_seperation_line(void);
  274. #if defined(__MMI_GAME_PUZZLE_128x160__) || defined(__MMI_GAME_PUZZLE_176x220__) || defined(__MMI_GAME_PUZZLE_240x320__)
  275. void mmi_gx_puzzle_draw_step(void);
  276. #endif 
  277. #ifdef __MMI_TOUCH_SCREEN__
  278. /* touch screen */
  279. void mmi_puzzle_pen_down_hdlr(mmi_pen_point_struct pos);
  280. void mmi_puzzle_pen_up_hdlr(mmi_pen_point_struct pos);
  281. #endif /* __MMI_TOUCH_SCREEN__ */ 
  282. /************************************************************************/
  283. /* Audio [Const]                                                        */
  284. /************************************************************************/
  285. #ifdef __MMI_GAME_MULTICHANNEL_SOUND__
  286. #define  PUZZLEMOVE  138
  287. __align(2)
  288.      const U8 PuzzleMove[138] = 
  289.      {
  290.          0x4D, 0x54, 0x68, 0x64, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x03, 0xC0, 0x4D, 0x54, 0x72,
  291.          0x6B, 0x00, 0x00, 0x00, 0x74, 0x00, 0xFF, 0x02, 0x16, 0x43, 0x6F, 0x70, 0x79, 0x72, 0x69, 0x67,
  292.          0x68, 0x74, 0x20, 0x63, 0x20, 0x32, 0x30, 0x30, 0x33, 0x20, 0x62, 0x79, 0x20, 0x43, 0x43, 0x00,
  293.          0xFF, 0x01, 0x02, 0x43, 0x43, 0x00, 0xFF, 0x58, 0x04, 0x04, 0x02, 0x18, 0x08, 0x00, 0xFF, 0x59,
  294.          0x02, 0x00, 0x00, 0x00, 0xFF, 0x51, 0x03, 0x05, 0x24, 0xB7, 0x00, 0xC0, 0x0A, 0x00, 0xB0, 0x07,
  295.          0x60, 0x00, 0x90, 0x48, 0x64, 0x00, 0xC1, 0x61, 0x00, 0xB1, 0x07, 0x7C, 0x00, 0x91, 0x48, 0x64,
  296.          0x81, 0x70, 0x90, 0x48, 0x00, 0x00, 0x4A, 0x64, 0x81, 0x70, 0x4A, 0x00, 0x00, 0x4C, 0x64, 0x81,
  297.          0x70, 0x4C, 0x00, 0x00, 0x4F, 0x64, 0x81, 0x70, 0x4F, 0x00, 0x00, 0x54, 0x64, 0x81, 0x70, 0x54,
  298.          0x00, 0x00, 0x91, 0x48, 0x00, 0x00, 0xFF, 0x2F, 0x00
  299.      };
  300. #else /* __MMI_GAME_MULTICHANNEL_SOUND__ */ 
  301. #define  PUZZLEMOVE_DVI 890
  302. __align(2)
  303.      const U8 PuzzleMove_dvi[890] = 
  304.      {
  305.          0x77, 0xD7, 0x77, 0x77, 0x0E, 0x08, 0xB7, 0xB7, 0x08, 0x17, 0x8C, 0x10, 0x88, 0xA8, 0x89, 0x18,
  306.          0x32, 0x02, 0xB8, 0xAD, 0x8A, 0x42, 0x33, 0x92, 0xCC, 0xAB, 0x28, 0x44, 0x13, 0xB8, 0xBD, 0x9A,
  307.          0x42, 0x34, 0x81, 0xDA, 0xBB, 0x18, 0x44, 0x13, 0xA0, 0xDC, 0x8A, 0x30, 0x53, 0x82, 0xC9, 0xBB,
  308.          0x09, 0x53, 0x24, 0xA0, 0xDA, 0xAA, 0x20, 0x34, 0x23, 0xC9, 0xAD, 0x8A, 0x41, 0x24, 0x81, 0xC9,
  309.          0xAC, 0x19, 0x52, 0x13, 0x90, 0xDB, 0xAA, 0x18, 0x54, 0x12, 0xA8, 0xBC, 0x9A, 0x30, 0x25, 0x23,
  310.          0xA9, 0xCD, 0x8B, 0x30, 0x35, 0x02, 0xB8, 0xAD, 0x9B, 0x30, 0x35, 0x04, 0xA8, 0xCC, 0xA9, 0x30,
  311.          0x53, 0x13, 0x99, 0xCC, 0xAA, 0x28, 0x44, 0x13, 0x90, 0xBC, 0xCB, 0x19, 0x72, 0x22, 0x90, 0xBA,
  312.          0xCB, 0x89, 0x52, 0x34, 0x80, 0xC9, 0xBB, 0x0B, 0x51, 0x24, 0x03, 0xB9, 0xAE, 0x9A, 0x21, 0x34,
  313.          0x33, 0xA8, 0xBF, 0x9A, 0x10, 0x43, 0x34, 0x98, 0xBD, 0xAA, 0x08, 0x34, 0x25, 0x00, 0xAC, 0xAC,
  314.          0x09, 0x32, 0x26, 0x82, 0xCA, 0xBB, 0x98, 0x40, 0x45, 0x00, 0x99, 0xBB, 0x8D, 0x48, 0x42, 0x11,
  315.          0x98, 0xBC, 0xAB, 0x38, 0x54, 0x12, 0x80, 0xEB, 0x9B, 0x10, 0x31, 0x35, 0x91, 0xBC, 0xBA, 0x8A,
  316.          0x44, 0x24, 0x82, 0xCA, 0xCB, 0x8A, 0x61, 0x22, 0x11, 0xB8, 0xBE, 0x88, 0x10, 0x44, 0x02, 0xA8,
  317.          0xDB, 0xAA, 0x20, 0x44, 0x03, 0x80, 0xDC, 0x8B, 0x00, 0x42, 0x15, 0x98, 0xA9, 0xBC, 0x08, 0x43,
  318.          0x24, 0x00, 0xD9, 0xAC, 0x80, 0x30, 0x26, 0x80, 0xA8, 0xBC, 0x0A, 0x21, 0x26, 0x12, 0xC9, 0xAB,
  319.          0xAB, 0x68, 0x14, 0x21, 0xB0, 0xAD, 0xA9, 0x28, 0x44, 0x12, 0x90, 0xEB, 0x9B, 0x01, 0x51, 0x14,
  320.          0x88, 0xC9, 0x9C, 0x08, 0x42, 0x32, 0x91, 0xBB, 0xDC, 0x19, 0x12, 0x54, 0x91, 0x99, 0xBC, 0x89,
  321.          0x41, 0x33, 0x13, 0xBC, 0xDA, 0x8C, 0x12, 0x62, 0x82, 0x98, 0xCB, 0x9A, 0x40, 0x22, 0x16, 0x9A,
  322.          0xC8, 0x8C, 0x00, 0x43, 0x12, 0xA0, 0xCA, 0x9D, 0x10, 0x50, 0x03, 0x10, 0xCC, 0xA9, 0x08, 0x42,
  323.          0x14, 0x00, 0xF9, 0x89, 0x8A, 0x33, 0x41, 0x81, 0xC9, 0xAB, 0x8B, 0x25, 0x61, 0x91, 0x98, 0xBC,
  324.          0x88, 0x30, 0x44, 0x81, 0xB1, 0x9F, 0xA8, 0x31, 0x22, 0x14, 0xA9, 0x9D, 0xAB, 0x22, 0x71, 0x93,
  325.          0x81, 0xAD, 0xA9, 0x10, 0x34, 0x21, 0xE3, 0x8A, 0x9D, 0x01, 0x41, 0x12, 0x90, 0xE9, 0x0A, 0x1B,
  326.          0x05, 0x32, 0x98, 0xC9, 0xBB, 0x38, 0x68, 0x85, 0x01, 0xAB, 0xCA, 0x08, 0x32, 0x63, 0x90, 0xC0,
  327.          0x8C, 0x8A, 0x14, 0x32, 0x82, 0x9C, 0xFB, 0x00, 0x59, 0x01, 0x82, 0xB8, 0xAA, 0x1D, 0x81, 0x07,
  328.          0x20, 0x9A, 0xAA, 0x9A, 0x23, 0x72, 0x00, 0xD1, 0xA8, 0x8A, 0x30, 0x52, 0x92, 0xA1, 0x0F, 0x8B,
  329.          0x01, 0x33, 0x84, 0x08, 0x9F, 0xB8, 0x02, 0x52, 0x01, 0x88, 0x9D, 0xC8, 0x02, 0x51, 0x10, 0x88,
  330.          0xAC, 0xC8, 0x02, 0x61, 0x00, 0xA1, 0xAA, 0xD9, 0x82, 0x62, 0x28, 0x98, 0xB9, 0xBA, 0xA3, 0x36,
  331.          0x58, 0x89, 0xC9, 0x99, 0x80, 0x86, 0x13, 0x2A, 0xAC, 0xA9, 0x29, 0x13, 0x87, 0x93, 0x8C, 0x8C,
  332.          0x18, 0x60, 0x10, 0xB1, 0xD1, 0x99, 0x08, 0x23, 0x62, 0x2A, 0xAC, 0xB8, 0x18, 0x22, 0x97, 0xA4,
  333.          0xA8, 0x8B, 0x19, 0x52, 0x50, 0x1A, 0xBA, 0xC8, 0x00, 0x31, 0x96, 0xB4, 0xA0, 0x9B, 0x09, 0x15,
  334.          0x42, 0x3A, 0x8E, 0x9A, 0x08, 0x70, 0x28, 0x90, 0xD0, 0x98, 0x08, 0x02, 0x97, 0xB3, 0xB0, 0xA9,
  335.          0x90, 0x87, 0x83, 0x81, 0xAA, 0xCA, 0xA2, 0x15, 0x32, 0x1A, 0xBC, 0xCA, 0x81, 0x72, 0x30, 0x89,
  336.          0xD9, 0xA8, 0x18, 0x70, 0x20, 0x98, 0xC8, 0x99, 0x39, 0x78, 0x10, 0xA0, 0xC8, 0x89, 0x39, 0x78,
  337.          0x10, 0xA0, 0xC8, 0x99, 0x38, 0x60, 0x20, 0xA8, 0xD9, 0xA8, 0x20, 0x71, 0x28, 0x89, 0xCA, 0xA8,
  338.          0x02, 0x34, 0x30, 0x0B, 0x9F, 0x9A, 0x02, 0x06, 0x02, 0x89, 0x8D, 0x1C, 0x28, 0x13, 0x93, 0xD0,
  339.          0xBA, 0x09, 0x70, 0x40, 0x08, 0xA9, 0xBB, 0xA0, 0x15, 0x15, 0x00, 0x9B, 0x0F, 0x1A, 0x40, 0x21,
  340.          0x90, 0xE8, 0xB8, 0x00, 0x32, 0x43, 0x08, 0x9E, 0x0C, 0x29, 0x31, 0x23, 0xB1, 0xFA, 0xBA, 0x01,
  341.          0x24, 0x23, 0x88, 0x9E, 0x0C, 0x4A, 0x40, 0x20, 0x89, 0xBB, 0xBA, 0x12, 0x07, 0x84, 0xB0, 0xD8,
  342.          0x98, 0x11, 0x23, 0x02, 0x9A, 0x8F, 0x2B, 0x59, 0x40, 0x28, 0x0C, 0x0D, 0x2A, 0x38, 0x40, 0x08,
  343.          0x9B, 0x9C, 0x08, 0x33, 0x04, 0xB1, 0xDA, 0xA9, 0x81, 0x06, 0x83, 0xC2, 0xC8, 0xA8, 0x03, 0x06,
  344.          0xA2, 0xD8, 0xB9, 0x80, 0x24, 0x04, 0xA2, 0xE9, 0xB9, 0x00, 0x34, 0x15, 0x91, 0xEA, 0xAA, 0x80,
  345.          0x23, 0x25, 0x91, 0xDA, 0xAA, 0x08, 0x23, 0x15, 0xA1, 0xCA, 0x99, 0x11, 0x24, 0x83, 0xC9, 0xAC,
  346.          0x89, 0x25, 0x24, 0x91, 0xEB, 0xAA, 0x08, 0x42, 0x25, 0x91, 0xDA, 0xBB, 0x38, 0x35, 0x14, 0x90,
  347.          0xDC, 0xAA, 0x18, 0x34, 0x24, 0x90, 0xCC, 0xAA, 0x18, 0x53, 0x23, 0x91, 0xDB, 0xAC, 0x08, 0x52,
  348.          0x32, 0x80, 0xDA, 0xAA, 0x0A, 0x42, 0x24, 0x82, 0xCA, 0xBB, 0x8A, 0x42, 0x44, 0x12, 0xA8, 0xDB,
  349.          0xAB, 0x18, 0x53, 0x23, 0x81, 0xDB, 0xCB, 0x88, 0x31, 0x34, 0x03, 0xC9, 0xDB, 0x89, 0x20, 0x33,
  350.          0x10, 0xB9, 0xBB, 0xAB, 0x38, 0x34, 0x27, 0x81, 0xA8, 0xEB, 0xAA, 0x09, 0x73, 0x33, 0x93, 0xDB,
  351.          0xAD, 0x88, 0x32, 0x43, 0x23, 0xD9, 0xCD, 0x89, 0x22, 0x34, 0x03, 0xC8, 0xDB, 0x9B, 0x20, 0x44,
  352.          0x23, 0xA0, 0xBE, 0x9A, 0x00, 0x33, 0x25, 0xA2, 0xCB, 0xAC, 0x08, 0x62, 0x32, 0x80, 0xCB, 0xCB,
  353.          0x9A, 0x63, 0x24, 0x81, 0xBA, 0xBC, 0x0B, 0x51, 0x25, 0x01, 0xB8, 0xAE, 0x8A, 0x41, 0x33, 0x13,
  354.          0xDA, 0xCC, 0x89, 0x32, 0x25, 0x82, 0xC9, 0xBD, 0x09, 0x43, 0x33, 0x81, 0xEA, 0xAC, 0x08, 0x43,
  355.          0x32, 0x81, 0xDC, 0xAA, 0x19, 0x44, 0x13, 0x90, 0xCC, 0x9B, 0x28, 0x35, 0x03, 0xB8, 0xBD, 0x8B,
  356.          0x42, 0x34, 0x82, 0xCB, 0xBC, 0x18, 0x43, 0x33, 0xB1, 0xCD, 0x9A, 0x20, 0x44, 0x02, 0xAA, 0xBC,
  357.          0x89, 0x42, 0x24, 0x81, 0xDB, 0x9B, 0x20, 0x43, 0x84, 0xA9, 0xBC, 0x0A, 0x43, 0x24, 0x80, 0xCC,
  358.          0x9A, 0x20, 0x35, 0x82, 0xB9, 0xBD, 0x19, 0x33, 0x34, 0x90, 0xBD, 0xAB, 0x40, 0x25, 0x11, 0xD9,
  359.          0x8C, 0x88, 0x42, 0x02, 0x90, 0xCB, 0x9A, 0x41, 0x43, 0x81, 0xD9, 0xAB, 0x18, 0x53, 0x13, 0xA8,
  360.          0xBC, 0xAB, 0x52, 0x23, 0x13, 0xCC, 0xBA, 0x1B, 0x25, 0x34
  361.      };
  362. #endif /* __MMI_GAME_MULTICHANNEL_SOUND__ */ 
  363. /******************************************AUDIO************************************/
  364. /*****************************************************************************
  365.  * FUNCTION
  366.  *  mmi_gx_puzzle_enter_gfx
  367.  * DESCRIPTION
  368.  *  Set Game Framework (GFX) Parameter
  369.  * PARAMETERS
  370.  *  void
  371.  * RETURNS
  372.  *  void
  373.  *****************************************************************************/
  374. /* keep this game old naming style */
  375. void mmi_gx_puzzle_enter_gfx(void)
  376. {
  377.     /*----------------------------------------------------------------*/
  378.     /* Local Variables                                                */
  379.     /*----------------------------------------------------------------*/
  380.     /*----------------------------------------------------------------*/
  381.     /* Code Body                                                      */
  382.     /*----------------------------------------------------------------*/
  383.     /* Game menu */
  384.     GFX.game_data.game_img_id = IMG_ID_GX_PUZZLE_GAME_ICON; /* game icon img ID */
  385.     GFX.game_data.game_str_id = STR_ID_GX_PUZZLE_GAME_NAME; /* game name string ID */
  386.     GFX.game_data.menu_resume_str_id = STR_GAME_RESUME;     /* "Resume" string ID */
  387.     GFX.game_data.menu_new_str_id = STR_GAME_NEW;           /* "New Game" string ID */
  388.     GFX.game_data.menu_level_str_id = STR_GAME_LEVEL;       /* "Game Level" string ID */
  389.     GFX.game_data.menu_type_str_id = 0;                    /* "Game type" string ID */     //KP Jerry add on 2007-3-24
  390.     GFX.game_data.menu_grade_str_id = STR_GAME_GRADE;       /* "Best Grade" string ID */
  391.     GFX.game_data.menu_help_str_id = STR_GAME_HELP;         /* "Game Help" string ID */
  392.     /* level / grade */
  393.     GFX.game_data.level_count = 3;  /* how many levels */
  394.     GFX.game_data.level_str_id_list[0] = STR_ID_GX_PUZZLE_LEVEL_EASY;   /* level string ID */
  395.     GFX.game_data.level_str_id_list[1] = STR_ID_GX_PUZZLE_LEVEL_NORMAL; /* level string ID */
  396.     GFX.game_data.level_str_id_list[2] = STR_ID_GX_PUZZLE_LEVEL_HARD;   /* level string ID */
  397.     /* add slot in NVRAMEnum.h */
  398.     GFX.game_data.grade_nvram_id_list[0] = NVRAM_GX_PUZZLE_EASY_SCORE;  /* grade slot in NVRAM (short) */
  399.     GFX.game_data.grade_nvram_id_list[1] = NVRAM_GX_PUZZLE_NORMAL_SCORE;        /* grade slot in NVRAM */
  400.     GFX.game_data.grade_nvram_id_list[2] = NVRAM_GX_PUZZLE_HARD_SCORE;  /* grade slot in NVRAM */
  401.     GFX.game_data.level_nvram_id = NVRAM_GX_PUZZLE_LEVEL;   /* current lvl idnex  in NVRAM (byte) */
  402.     /* help */
  403. #if defined(__MMI_TOUCH_SCREEN__)
  404.     GFX.game_data.help_str_id = STR_ID_GX_PUZZLE_HELP_DESCRIPTION_TP;   /* help desciption string id */
  405. #elif defined(__MMI_GAME_PUZZLE_128x160__) || defined(__MMI_GAME_PUZZLE_176x220__) || defined(__MMI_GAME_PUZZLE_240x320__)
  406.     GFX.game_data.help_str_id = STR_ID_GX_PUZZLE_HELP_DESCRIPTION;      /* help desciption string id */
  407. #elif defined(__MMI_GAME_PUZZLE_128x128__)
  408.     GFX.game_data.help_str_id = STR_ID_GX_PUZZLE_HELP_DESCRIPTION_128;  /* help desciption string id */
  409. #endif 
  410.     /* misc */
  411.     GFX.game_data.grade_value_ptr = (S16*) (&g_gx_puzzle_context.game_grade);  /* current level's grade (S16*) */
  412.     GFX.game_data.level_index_ptr = (U8*) (&g_gx_puzzle_context.game_level);   /* ptr to current level index (U8*) */
  413.     GFX.game_data.is_new_game = (BOOL*) (&g_gx_puzzle_context.is_new_game);    /* ptr to new game flag (BOOL*) */
  414.     /* function ptr */
  415.     GFX.game_data.best_grade_func_ptr = mmi_gx_puzzle_calc_best_grade;  /* function to calculate best grade */
  416.     GFX.game_data.enter_game_func_ptr = mmi_gx_puzzle_enter_game;       /* function to enter new game */
  417.     GFX.game_data.exit_game_func_ptr = mmi_gx_puzzle_exit_game; /* function to exit game */
  418.     GFX.game_data.draw_gameover_func_ptr = mmi_gx_puzzle_draw_gameover; /* function to draw gameover screen */
  419.     /* some flags */
  420.     GFX.game_data.is_keypad_audio_enable = FALSE;   /* play keypad tone or not */
  421.     mmi_gfx_entry_menu_screen();
  422. }
  423. /*****************************************************************************
  424.  * FUNCTION
  425.  *  mmi_gx_puzzle_calc_best_grade
  426.  * DESCRIPTION
  427.  *  Calculate new best grade [Callback required by GFX]
  428.  * PARAMETERS
  429.  *  old_grade       [IN]        
  430.  *  new_grade       [IN]        
  431.  * RETURNS
  432.  *  S16
  433.  *****************************************************************************/
  434. S16 mmi_gx_puzzle_calc_best_grade(S16 old_grade, S16 new_grade)
  435. {
  436.     /*----------------------------------------------------------------*/
  437.     /* Local Variables                                                */
  438.     /*----------------------------------------------------------------*/
  439.     /*----------------------------------------------------------------*/
  440.     /* Code Body                                                      */
  441.     /*----------------------------------------------------------------*/
  442.     /* compare the best grade and return it */
  443.     if (old_grade == 0)
  444.     {
  445.         return new_grade;
  446.     }
  447.     if (new_grade < old_grade)
  448.     {
  449.         return new_grade;
  450.     }
  451.     else
  452.     {
  453.         return old_grade;
  454.     }
  455. }
  456. /*****************************************************************************
  457.  * FUNCTION
  458.  *  mmi_gx_puzzle_draw_gameover
  459.  * DESCRIPTION
  460.  *  Draw Gameover Screen [Callback required by GFX]
  461.  * PARAMETERS
  462.  *  void
  463.  * RETURNS
  464.  *  void
  465.  *****************************************************************************/
  466. void mmi_gx_puzzle_draw_gameover(void)
  467. {
  468.     /*----------------------------------------------------------------*/
  469.     /* Local Variables                                                */
  470.     /*----------------------------------------------------------------*/
  471.     /*----------------------------------------------------------------*/
  472.     /* Code Body                                                      */
  473.     /*----------------------------------------------------------------*/
  474.     GFX_PLAY_AUDIO_COMPLETE();
  475.     /* call this function to draw standard gameover screen */
  476.     mmi_gfx_draw_gameover_screen(
  477.         IMG_ID_GX_PUZZLE_GOTEXT,
  478.         IMG_ID_GX_PUZZLE_GRADESMAP,
  479.         IMG_ID_GX_PUZZLE_GOPIC,
  480.         g_gx_puzzle_context.game_grade);
  481. }
  482. /*****************************************************************************
  483.  * FUNCTION
  484.  *  mmi_gx_puzzle_enter_game
  485.  * DESCRIPTION
  486.  *  Enter Game [Callback required by GFX]
  487.  * PARAMETERS
  488.  *  void
  489.  * RETURNS
  490.  *  void
  491.  *****************************************************************************/
  492. void mmi_gx_puzzle_enter_game(void)
  493. {
  494.     /*----------------------------------------------------------------*/
  495.     /* Local Variables                                                */
  496.     /*----------------------------------------------------------------*/
  497.     /*----------------------------------------------------------------*/
  498.     /* Code Body                                                      */
  499.     /*----------------------------------------------------------------*/
  500.     if (g_gx_puzzle_context.is_new_game == TRUE)
  501.     {
  502.         mmi_gx_puzzle_init_game();  /* is new game, otherwise resume game */
  503.     }
  504.     g_gx_puzzle_context.is_new_game = FALSE;
  505.     g_gx_puzzle_context.is_gameover = FALSE;
  506. #ifdef __MMI_GAME_PUZZLE_128x128__
  507.     g_gx_puzzle_context.is_show_status = FALSE;
  508.     SetKeyHandler(mmi_gx_puzzle_key_5_press, KEY_5, KEY_EVENT_DOWN);
  509.     SetKeyHandler(mmi_gx_puzzle_key_5_release, KEY_5, KEY_EVENT_UP);
  510. #endif /* __MMI_GAME_PUZZLE_128x128__ */ 
  511.     SetKeyHandler(mmi_gx_puzzle_key_2_release, KEY_2, KEY_EVENT_UP);
  512.     SetKeyHandler(mmi_gx_puzzle_key_2_release, KEY_UP_ARROW, KEY_EVENT_UP);
  513.     SetKeyHandler(mmi_gx_puzzle_key_4_release, KEY_4, KEY_EVENT_UP);
  514.     SetKeyHandler(mmi_gx_puzzle_key_4_release, KEY_LEFT_ARROW, KEY_EVENT_UP);
  515.     SetKeyHandler(mmi_gx_puzzle_key_6_release, KEY_6, KEY_EVENT_UP);
  516.     SetKeyHandler(mmi_gx_puzzle_key_6_release, KEY_RIGHT_ARROW, KEY_EVENT_UP);
  517.     SetKeyHandler(mmi_gx_puzzle_key_8_release, KEY_8, KEY_EVENT_UP);
  518.     SetKeyHandler(mmi_gx_puzzle_key_8_release, KEY_DOWN_ARROW, KEY_EVENT_UP);
  519. #ifdef __MMI_GAME_MULTICHANNEL_SOUND__
  520.     GFX_OPEN_SOUND_EFFECTS_MIDI(PuzzleMove, PUZZLEMOVE, 1, g_gx_puzzle_context.move_midi);
  521. #endif 
  522.     mmi_gx_puzzle_draw_static_background();
  523.     mmi_gx_puzzle_show_puzzle();
  524.     mmi_gx_puzzle_draw_seperation_line();
  525.     /* start game loop */
  526.     mmi_gx_puzzle_cyclic_timer();
  527. #ifdef __MMI_TOUCH_SCREEN__
  528.     wgui_register_pen_down_handler(mmi_puzzle_pen_down_hdlr);
  529.     wgui_register_pen_up_handler(mmi_puzzle_pen_up_hdlr);
  530. #endif /* __MMI_TOUCH_SCREEN__ */ 
  531. }
  532. /*****************************************************************************
  533.  * FUNCTION
  534.  *  mmi_gx_puzzle_draw_static_background
  535.  * DESCRIPTION
  536.  *  Draw the elements that only need to be drawn once
  537.  * PARAMETERS
  538.  *  void
  539.  * RETURNS
  540.  *  void
  541.  *****************************************************************************/
  542. void mmi_gx_puzzle_draw_static_background(void)
  543. {
  544.     /*----------------------------------------------------------------*/
  545.     /* Local Variables                                                */
  546.     /*----------------------------------------------------------------*/
  547.     /*----------------------------------------------------------------*/
  548.     /* Code Body                                                      */
  549.     /*----------------------------------------------------------------*/
  550.     gui_fill_rectangle(0, 0, MMI_GX_PUZZLE_LCD_WIDTH - 1, MMI_GX_PUZZLE_LCD_HEIGHT - 1, MMI_GX_PUZZLE_BACKGROUND_COLOR);
  551. #if defined(__MMI_GAME_PUZZLE_128x160__)
  552.     gui_show_image(49, 129, (PU8) GetImage(g_gx_puzzle_context.small_image));
  553. #elif defined(__MMI_GAME_PUZZLE_176x220__)
  554.     gui_show_image(68, 178, (PU8) GetImage(g_gx_puzzle_context.small_image));
  555.     if (g_gx_puzzle_context.game_level == MMI_GX_PUZZLE_NORMAL)
  556.     {
  557.         gui_draw_vertical_line(
  558.             0,
  559.             MMI_GX_PUZZLE_ORIGIN_Y + MMI_GX_PUZZLE_SIZE,
  560.             MMI_GX_PUZZLE_ORIGIN_X + MMI_GX_PUZZLE_SIZE - 1,
  561.             MMI_GX_PUZZLE_BAR_COLOR);
  562.         gui_draw_horizontal_line(
  563.             0,
  564.             MMI_GX_PUZZLE_LCD_WIDTH - 1,
  565.             MMI_GX_PUZZLE_ORIGIN_Y + MMI_GX_PUZZLE_SIZE - 1,
  566.             MMI_GX_PUZZLE_BAR_COLOR);
  567.     }
  568. #elif defined(__MMI_GAME_PUZZLE_240x320__)
  569.     gui_show_image(95, 245, (PU8) GetImage(g_gx_puzzle_context.small_image));
  570.     if (g_gx_puzzle_context.game_level == MMI_GX_PUZZLE_NORMAL)
  571.     {
  572.         gui_draw_vertical_line(
  573.             0,
  574.             MMI_GX_PUZZLE_ORIGIN_Y + MMI_GX_PUZZLE_SIZE,
  575.             MMI_GX_PUZZLE_ORIGIN_X + MMI_GX_PUZZLE_SIZE - 1,
  576.             MMI_GX_PUZZLE_BAR_COLOR);
  577.         gui_draw_horizontal_line(
  578.             0,
  579.             MMI_GX_PUZZLE_LCD_WIDTH - 1,
  580.             MMI_GX_PUZZLE_ORIGIN_Y + MMI_GX_PUZZLE_SIZE - 1,
  581.             MMI_GX_PUZZLE_BAR_COLOR);
  582.     }
  583. #endif 
  584. #if defined(__MMI_TOUCH_SCREEN__) && defined(__MMI_GAME_PUZZLE_240x320__)       /* || defined(__MMI_GAME_PUZZLE_176x220__)) */
  585.     /* draw softkay image */
  586.     gdi_image_draw_id(MMI_GX_PUZZLE_SOFTKEY_X, MMI_GX_PUZZLE_SOFTKEY_Y, IMG_ID_GX_PUZZLE_UP);
  587. #endif /* defined(__MMI_TOUCH_SCREEN__) && defined(__MMI_GAME_PUZZLE_240x320__) */ 
  588.     gui_show_image(
  589.         MMI_GX_PUZZLE_ORIGIN_X - MMI_GX_PUZZLE_LEFT_BAR_WIDTH,
  590.         MMI_GX_PUZZLE_ORIGIN_Y - MMI_GX_PUZZLE_TOP_BAR_WIDTH,
  591.         (PU8) GetImage(IMG_ID_GX_PUZZLE_TOP_BAR));
  592.     gui_show_image(
  593.         MMI_GX_PUZZLE_ORIGIN_X - MMI_GX_PUZZLE_LEFT_BAR_WIDTH,
  594.         MMI_GX_PUZZLE_ORIGIN_Y - MMI_GX_PUZZLE_TOP_BAR_WIDTH,
  595.         (PU8) GetImage(IMG_ID_GX_PUZZLE_LEFT_BAR));
  596.     gui_show_image(
  597.         MMI_GX_PUZZLE_ORIGIN_X - MMI_GX_PUZZLE_LEFT_BAR_WIDTH,
  598.         MMI_GX_PUZZLE_ORIGIN_Y + MMI_GX_PUZZLE_SIZE,
  599.         (PU8) GetImage(IMG_ID_GX_PUZZLE_BOTTOM_BAR));
  600.     gui_show_image(
  601.         MMI_GX_PUZZLE_ORIGIN_X + MMI_GX_PUZZLE_SIZE,
  602.         MMI_GX_PUZZLE_ORIGIN_Y - MMI_GX_PUZZLE_TOP_BAR_WIDTH,
  603.         (PU8) GetImage(IMG_ID_GX_PUZZLE_RIGHT_BAR));
  604.     gui_show_image(
  605.         MMI_GX_PUZZLE_ORIGIN_X - MMI_GX_PUZZLE_LEFT_BAR_WIDTH,
  606.         MMI_GX_PUZZLE_ORIGIN_Y - MMI_GX_PUZZLE_TOP_BAR_WIDTH,
  607.         (PU8) GetImage(IMG_ID_GX_PUZZLE_TOP_BAR));
  608. #if defined(__MMI_GAME_PUZZLE_128x160__) || defined(__MMI_GAME_PUZZLE_176x220__) || defined(__MMI_GAME_PUZZLE_240x320__)
  609.     /* four lines */
  610.     gui_draw_horizontal_line(
  611.         2,
  612.         MMI_GX_PUZZLE_LCD_WIDTH - 1 - 2,
  613.         MMI_GX_PUZZLE_LCD_WIDTH + 1,
  614.         MMI_GX_PUZZLE_LIGHT_BORDER);
  615.     gui_draw_horizontal_line(
  616.         2,
  617.         MMI_GX_PUZZLE_LCD_WIDTH - 1 - 2,
  618.         MMI_GX_PUZZLE_LCD_HEIGHT - 1 - 1,
  619.         MMI_GX_PUZZLE_DARK_BORDER);
  620.     gui_draw_vertical_line(
  621.         MMI_GX_PUZZLE_LCD_WIDTH + 2,
  622.         MMI_GX_PUZZLE_LCD_HEIGHT - 1 - 1,
  623.         1,
  624.         MMI_GX_PUZZLE_LIGHT_BORDER);
  625.     gui_draw_vertical_line(
  626.         MMI_GX_PUZZLE_LCD_WIDTH + 2,
  627.         MMI_GX_PUZZLE_LCD_HEIGHT - 1 - 1,
  628.         MMI_GX_PUZZLE_LCD_WIDTH - 1 - 1,
  629.         MMI_GX_PUZZLE_DARK_BORDER);
  630.     gui_show_image(
  631.         MMI_GX_PUZZLE_TIME_POSITION_X,
  632.         MMI_GX_PUZZLE_TIME_POSITION_Y + 128,
  633.         (PU8) GetImage(IMG_ID_GX_PUZZLE_BOX));
  634. #ifndef __MMI_GAME_PUZZLE_240x320__
  635.     gui_show_image(MMI_GX_PUZZLE_TIME_POSITION_X + 2,
  636. #if defined(__MMI_GAME_PUZZLE_128x160__)
  637.                    MMI_GX_PUZZLE_TIME_POSITION_Y - 8 + 128,
  638. #elif defined(__MMI_GAME_PUZZLE_176x220__)
  639.                    MMI_GX_PUZZLE_TIME_POSITION_Y - 12 + 128,
  640. #endif 
  641.                    (PU8) GetImage(IMG_ID_GX_PUZZLE_TIME));
  642. #endif /* __MMI_GAME_PUZZLE_240x320__ */ 
  643.     gui_show_image(
  644.         MMI_GX_PUZZLE_STEP_POSITION_X,
  645.         MMI_GX_PUZZLE_STEP_POSITION_Y + 128,
  646.         (PU8) GetImage(IMG_ID_GX_PUZZLE_BOX));
  647. #ifndef __MMI_GAME_PUZZLE_240x320__
  648.     gui_show_image(MMI_GX_PUZZLE_STEP_POSITION_X + 2,
  649. #if defined(__MMI_GAME_PUZZLE_128x160__)
  650.                    MMI_GX_PUZZLE_STEP_POSITION_Y - 8 + 128,
  651. #elif defined(__MMI_GAME_PUZZLE_176x220__)
  652.                    MMI_GX_PUZZLE_STEP_POSITION_Y - 12 + 128,
  653. #endif 
  654.                    (PU8) GetImage(IMG_ID_GX_PUZZLE_STEP));
  655. #endif /* __MMI_GAME_PUZZLE_240x320__ */ 
  656.     mmi_gx_puzzle_draw_step();
  657. #endif /* defined(__MMI_GAME_PUZZLE_128x160__) || defined(__MMI_GAME_PUZZLE_176x220__) || defined(__MMI_GAME_PUZZLE_240x320__) */ 
  658. }
  659. /*****************************************************************************
  660.  * FUNCTION
  661.  *  mmi_gx_puzzle_exit_game
  662.  * DESCRIPTION
  663.  *  Exit Game [Callback required by GFX]
  664.  * PARAMETERS
  665.  *  void
  666.  * RETURNS
  667.  *  void
  668.  *****************************************************************************/
  669. void mmi_gx_puzzle_exit_game(void)
  670. {
  671. #ifdef __MMI_GAME_MULTICHANNEL_SOUND__
  672.     /*----------------------------------------------------------------*/
  673.     /* Local Variables                                                */
  674.     /*----------------------------------------------------------------*/
  675.     /*----------------------------------------------------------------*/
  676.     /* Code Body                                                      */
  677.     /*----------------------------------------------------------------*/
  678.     GFX_STOP_SOUND_EFFECTS_MIDI(g_gx_puzzle_context.move_midi);
  679.     GFX_CLOSE_SOUND_EFFECTS_MIDI(g_gx_puzzle_context.move_midi);
  680. #endif /* __MMI_GAME_MULTICHANNEL_SOUND__ */ 
  681.     gui_cancel_timer(mmi_gx_puzzle_cyclic_timer);   /* cancle the looping timer */
  682. }
  683. /*****************************************************************************
  684.  * FUNCTION
  685.  *  mmi_gx_puzzle_draw_seperation_line
  686.  * DESCRIPTION
  687.  *  Draw seperation lines in the puzzle
  688.  * PARAMETERS
  689.  *  void
  690.  * RETURNS
  691.  *  void
  692.  *****************************************************************************/
  693. void mmi_gx_puzzle_draw_seperation_line(void)
  694. {
  695.     /*----------------------------------------------------------------*/
  696.     /* Local Variables                                                */
  697.     /*----------------------------------------------------------------*/
  698.     U8 i;
  699.     U8 grid_number;
  700.     /*----------------------------------------------------------------*/
  701.     /* Code Body                                                      */
  702.     /*----------------------------------------------------------------*/
  703.     grid_number = g_gx_puzzle_context.game_level + 3;
  704.     /* draw the seperation line */
  705.     for (i = 1; i < grid_number; i++)
  706.     {
  707.         gui_draw_vertical_line(
  708.             MMI_GX_PUZZLE_ORIGIN_Y,
  709.             MMI_GX_PUZZLE_ORIGIN_Y + MMI_GX_PUZZLE_SIZE - 1,
  710.             MMI_GX_PUZZLE_ORIGIN_X + i * MMI_GX_PUZZLE_SIZE / (g_gx_puzzle_context.game_level + 3) - 1,
  711.             MMI_GX_PUZZLE_LINE_COLOR);
  712.         gui_draw_horizontal_line(
  713.             MMI_GX_PUZZLE_ORIGIN_X,
  714.             MMI_GX_PUZZLE_ORIGIN_X + MMI_GX_PUZZLE_SIZE - 1,
  715.             MMI_GX_PUZZLE_ORIGIN_Y + i * MMI_GX_PUZZLE_SIZE / (g_gx_puzzle_context.game_level + 3) - 1,
  716.             MMI_GX_PUZZLE_LINE_COLOR);
  717.     }
  718.     gui_BLT_double_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);
  719. }
  720. /*****************************************************************************
  721.  * FUNCTION
  722.  *  mmi_gx_puzzle_show_puzzle
  723.  * DESCRIPTION
  724.  *  Draw all grids in the puzzle
  725.  * PARAMETERS
  726.  *  void
  727.  * RETURNS
  728.  *  void
  729.  *****************************************************************************/
  730. void mmi_gx_puzzle_show_puzzle(void)
  731. {
  732.     /*----------------------------------------------------------------*/
  733.     /* Local Variables                                                */
  734.     /*----------------------------------------------------------------*/
  735.     U8 grid_number;
  736.     U8 i;
  737.     U8 j;
  738.     /*----------------------------------------------------------------*/
  739.     /* Code Body                                                      */
  740.     /*----------------------------------------------------------------*/
  741.     grid_number = g_gx_puzzle_context.game_level + 3;
  742.     for (i = 0; i < grid_number; i++)
  743.     {
  744.         for (j = 0; j < grid_number; j++)
  745.         {
  746.             mmi_gx_puzzle_my_show_image(
  747.                 MMI_GX_PUZZLE_ORIGIN_X + MMI_GX_PUZZLE_SIZE / grid_number * j,
  748.                 MMI_GX_PUZZLE_ORIGIN_Y + MMI_GX_PUZZLE_SIZE / grid_number * i,
  749.                 g_gx_puzzle_context.puzzle[i][j]);
  750.         }
  751.     }
  752.     gui_BLT_double_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);
  753. }
  754. /*****************************************************************************
  755.  * FUNCTION
  756.  *  mmi_gx_puzzle_init_game
  757.  * DESCRIPTION
  758.  *  Game initilization
  759.  * PARAMETERS
  760.  *  void
  761.  * RETURNS
  762.  *  void
  763.  *****************************************************************************/
  764. void mmi_gx_puzzle_init_game(void)
  765. {
  766.     /*----------------------------------------------------------------*/
  767.     /* Local Variables                                                */
  768.     /*----------------------------------------------------------------*/
  769.     /* init game */
  770.     U16 i;
  771.     U16 j;
  772.     U8 bound_size;      /* height & width of the puzzle */
  773.     U8 index;
  774.     U16 shuffle_times;  /* shuffle for 'shuffle_times' times */
  775.     U8 move_type;
  776.     S16 destination_x;
  777.     S16 destination_y;
  778.     S16 tmp;
  779.     /*----------------------------------------------------------------*/
  780.     /* Code Body                                                      */
  781.     /*----------------------------------------------------------------*/
  782.     /* initialize */
  783.     j = 0;
  784.     bound_size = 0;
  785.     destination_x = 0;
  786.     destination_y = 0;
  787.     g_gx_puzzle_context.long_tick = 0;
  788.     g_gx_puzzle_context.step = 0;
  789.     g_gx_puzzle_context.click = 0;
  790. #ifdef __MMI_GAME_PUZZLE_128x128__
  791.     g_gx_puzzle_context.is_show_status = FALSE;
  792. #endif 
  793.     /* select the image for corresponding game level */
  794.     tmp = g_gx_puzzle_context.game_level;
  795.     if (g_gx_puzzle_context.game_level == MMI_GX_PUZZLE_EASY)
  796.     {
  797.         g_gx_puzzle_context.image = IMG_ID_GX_PUZZLE_3_1;
  798.     }
  799.     else
  800.     {
  801.         g_gx_puzzle_context.image = IMG_ID_GX_PUZZLE_3_1 + tmp * (tmp + 2) * (tmp + 2) / tmp;
  802.     }
  803.     g_gx_puzzle_context.small_image = IMG_ID_GX_PUZZLE_SMALL_3 + tmp;
  804.     /* init puzzle grids */
  805.     if (g_gx_puzzle_context.game_level == MMI_GX_PUZZLE_EASY)
  806.     {
  807.         bound_size = 3;
  808.     }
  809.     else if (g_gx_puzzle_context.game_level == MMI_GX_PUZZLE_NORMAL)
  810.     {
  811.         bound_size = 4;
  812.     }
  813.     else if (g_gx_puzzle_context.game_level == MMI_GX_PUZZLE_HARD)
  814.     {
  815.         bound_size = 5;
  816.     }
  817.     shuffle_times = bound_size * bound_size * 100;
  818.     index = 1;
  819.     for (i = 0; i < bound_size; i++)
  820.     {
  821.         for (j = 0; j < bound_size; j++)
  822.         {
  823.             g_gx_puzzle_context.puzzle[i][j] = index;
  824.             index = index + 1;
  825.         }
  826.     }
  827.     g_gx_puzzle_context.null_grid.y = i - 1;
  828.     g_gx_puzzle_context.null_grid.x = j - 1;
  829.     /* shuffle */
  830.     for (i = 0; i < shuffle_times; i++)
  831.     {
  832.         move_type = rand() % 4;
  833.         switch (move_type)
  834.         {
  835.             case 0: /* up */
  836.                 destination_x = 0;
  837.                 destination_y = -1;
  838.                 break;
  839.             case 1: /* left */
  840.                 destination_x = -1;
  841.                 destination_y = 0;
  842.                 break;
  843.             case 2: /* bottom */
  844.                 destination_x = 0;
  845.                 destination_y = 1;
  846.                 break;
  847.             case 3: /* right */
  848.                 destination_x = 1;
  849.                 destination_y = 0;
  850.                 break;
  851.         }
  852.         /* check if this shuffle is valid */
  853.         if (g_gx_puzzle_context.null_grid.x + destination_x < 0 ||
  854.             g_gx_puzzle_context.null_grid.x + destination_x >= bound_size ||
  855.             g_gx_puzzle_context.null_grid.y + destination_y < 0 ||
  856.             g_gx_puzzle_context.null_grid.y + destination_y >= bound_size)
  857.         {
  858.             i--;
  859.             continue;
  860.         }
  861.         /* switch the null grid and its neighboring grid */
  862.         tmp = g_gx_puzzle_context.puzzle[g_gx_puzzle_context.null_grid.y][g_gx_puzzle_context.null_grid.x];
  863.         g_gx_puzzle_context.puzzle[g_gx_puzzle_context.null_grid.y][g_gx_puzzle_context.null_grid.x] =
  864.             g_gx_puzzle_context.puzzle[g_gx_puzzle_context.null_grid.y +
  865.                                        destination_y][g_gx_puzzle_context.null_grid.x + destination_x];
  866.         g_gx_puzzle_context.puzzle[g_gx_puzzle_context.null_grid.y + destination_y][g_gx_puzzle_context.null_grid.x +
  867.                                                                                     destination_x] = tmp;
  868.         g_gx_puzzle_context.null_grid.x = g_gx_puzzle_context.null_grid.x + destination_x;
  869.         g_gx_puzzle_context.null_grid.y = g_gx_puzzle_context.null_grid.y + destination_y;
  870.     }
  871. }
  872. /*****************************************************************************
  873.  * FUNCTION
  874.  *  mmi_gx_puzzle_framemove
  875.  * DESCRIPTION
  876.  *  Framemove - process the game logic
  877.  * PARAMETERS
  878.  *  void
  879.  * RETURNS
  880.  *  void
  881.  *****************************************************************************/
  882. void mmi_gx_puzzle_framemove(void)
  883. {
  884.     /*----------------------------------------------------------------*/
  885.     /* Local Variables                                                */
  886.     /*----------------------------------------------------------------*/
  887.     /*----------------------------------------------------------------*/
  888.     /* Code Body                                                      */
  889.     /*----------------------------------------------------------------*/
  890.     /* add logic code here */
  891. }
  892. /*****************************************************************************
  893.  * FUNCTION
  894.  *  mmi_gx_puzzle_render
  895.  * DESCRIPTION
  896.  *  Render the game images
  897.  * PARAMETERS
  898.  *  void
  899.  * RETURNS
  900.  *  void
  901.  *****************************************************************************/
  902. void mmi_gx_puzzle_render(void)
  903. {
  904.     /*----------------------------------------------------------------*/
  905.     /* Local Variables                                                */
  906.     /*----------------------------------------------------------------*/
  907.     /* add drawing code here */
  908.     U16 tmp_tick;
  909. #ifdef __MMI_GAME_PUZZLE_128x128__
  910.     U16 tmp_click;
  911. #endif 
  912.     S16 position_shift;     /* the position offset of digit displayed */
  913.     MMI_ID_TYPE image_id;   /* store the last digit of tick */
  914.     /*----------------------------------------------------------------*/
  915.     /* Code Body                                                      */
  916.     /*----------------------------------------------------------------*/
  917. #if defined(__MMI_TOUCH_SCREEN__) && defined(__MMI_GAME_PUZZLE_240x320__)       /* || defined(__MMI_GAME_PUZZLE_176x220__)) */
  918.     /* softkey */
  919.     if (g_gx_puzzle_context.softkey_state == 1)
  920.     {
  921.     #if defined(__MMI_GAME_PUZZLE_240x320__)
  922.         gui_fill_rectangle(
  923.             MMI_GX_PUZZLE_SOFTKEY_X,
  924.             MMI_GX_PUZZLE_SOFTKEY_Y,
  925.             MMI_GX_PUZZLE_SOFTKEY_X + MMI_GX_PUZZLE_SOFTKEY_WIDTH,
  926.             MMI_GX_PUZZLE_SOFTKEY_Y + MMI_GX_PUZZLE_SOFTKEY_HEIGHT,
  927.             MMI_GX_PUZZLE_BACKGROUND_COLOR);
  928.     #elif defined(__MMI_GAME_PUZZLE_176x220__)
  929.         gui_fill_rectangle(
  930.             MMI_GX_PUZZLE_SOFTKEY_X,
  931.             MMI_GX_PUZZLE_SOFTKEY_Y,
  932.             MMI_GX_PUZZLE_SOFTKEY_X + MMI_GX_PUZZLE_SOFTKEY_WIDTH,
  933.             MMI_GX_PUZZLE_SOFTKEY_Y + MMI_GX_PUZZLE_SOFTKEY_HEIGHT,
  934.             MMI_GX_PUZZLE_BACKGROUND_COLOR);
  935.     #endif 
  936.         gdi_image_draw_id(MMI_GX_PUZZLE_SOFTKEY_X, MMI_GX_PUZZLE_SOFTKEY_Y, IMG_ID_GX_PUZZLE_DOWN);
  937.         g_gx_puzzle_context.softkey_state = 0;
  938.     }
  939.     else if (g_gx_puzzle_context.softkey_state == 2)
  940.     {
  941.     #if defined(__MMI_GAME_PUZZLE_240x320__)
  942.         gui_fill_rectangle(
  943.             MMI_GX_PUZZLE_SOFTKEY_X,
  944.             MMI_GX_PUZZLE_SOFTKEY_Y,
  945.             MMI_GX_PUZZLE_SOFTKEY_X + MMI_GX_PUZZLE_SOFTKEY_WIDTH,
  946.             MMI_GX_PUZZLE_SOFTKEY_Y + MMI_GX_PUZZLE_SOFTKEY_HEIGHT,
  947.             MMI_GX_PUZZLE_BACKGROUND_COLOR);
  948.     #elif defined(__MMI_GAME_PUZZLE_176x220__)
  949.         gui_fill_rectangle(
  950.             MMI_GX_PUZZLE_SOFTKEY_X,
  951.             MMI_GX_PUZZLE_SOFTKEY_Y,
  952.             MMI_GX_PUZZLE_SOFTKEY_X + MMI_GX_PUZZLE_SOFTKEY_WIDTH,
  953.             MMI_GX_PUZZLE_SOFTKEY_Y + MMI_GX_PUZZLE_SOFTKEY_HEIGHT,
  954.             MMI_GX_PUZZLE_BACKGROUND_COLOR);
  955.     #endif 
  956.         gdi_image_draw_id(MMI_GX_PUZZLE_SOFTKEY_X, MMI_GX_PUZZLE_SOFTKEY_Y, IMG_ID_GX_PUZZLE_UP);
  957.         g_gx_puzzle_context.softkey_state = 0;
  958.     }
  959. #endif /* defined(__MMI_TOUCH_SCREEN__) && defined(__MMI_GAME_PUZZLE_240x320__) */ 
  960. #if defined(__MMI_GAME_PUZZLE_128x160__) || defined(__MMI_GAME_PUZZLE_176x220__) || defined(__MMI_GAME_PUZZLE_240x320__)
  961.     /* update TIME every second */
  962.     if (g_gx_puzzle_context.long_tick % 10 == 0)
  963.     {
  964.         tmp_tick = g_gx_puzzle_context.long_tick / 10;
  965.     #if defined(__MMI_GAME_PUZZLE_128x160__)
  966.         position_shift = MMI_GX_PUZZLE_TIME_POSITION_X + 15;
  967.     #elif defined(__MMI_GAME_PUZZLE_176x220__)
  968.         position_shift = MMI_GX_PUZZLE_TIME_POSITION_X + 17;
  969.     #elif  defined(__MMI_GAME_PUZZLE_240x320__)
  970.         position_shift = MMI_GX_PUZZLE_TIME_POSITION_X + 22;
  971.     #endif 
  972.         do
  973.         {
  974.             image_id = IMG_ID_GX_PUZZLE_NUMBER_0 + (tmp_tick % 10);
  975.             tmp_tick = tmp_tick / 10;
  976.             gui_show_image(MMI_GX_PUZZLE_TIME_POSITION_X + position_shift,
  977.         #if defined(__MMI_GAME_PUZZLE_240x320__)
  978.                            MMI_GX_PUZZLE_TIME_POSITION_Y + 5 + 128,
  979.         #else 
  980.                            MMI_GX_PUZZLE_TIME_POSITION_Y + 2 + 128,
  981.         #endif 
  982.                            (PU8) GetImage(image_id));
  983.         #if defined(__MMI_GAME_PUZZLE_176x220__) || defined(__MMI_GAME_PUZZLE_240x320__)
  984.             position_shift = position_shift - MMI_GX_PUZZLE_DIGIT_WIDTH - 1;
  985.         #else 
  986.             position_shift = position_shift - MMI_GX_PUZZLE_DIGIT_WIDTH;
  987.         #endif 
  988.         } while (tmp_tick != 0);
  989.     }
  990. #elif defined(__MMI_GAME_PUZZLE_128x128__)
  991.     /* draw status ? */
  992.     if (g_gx_puzzle_context.is_show_status != FALSE)
  993.     {
  994.         gui_fill_rectangle(
  995.             MMI_GX_PUZZLE_STATUS_POS_X + MMI_GX_PUZZLE_STATUS_SHADOW,
  996.             MMI_GX_PUZZLE_STATUS_POS_Y + MMI_GX_PUZZLE_STATUS_SHADOW,
  997.             MMI_GX_PUZZLE_STATUS_POS_X + MMI_GX_PUZZLE_STATUS_WIDTH - 1 + MMI_GX_PUZZLE_STATUS_SHADOW,
  998.             MMI_GX_PUZZLE_STATUS_POS_Y + MMI_GX_PUZZLE_STATUS_HEIGHT - 1 + MMI_GX_PUZZLE_STATUS_SHADOW,
  999.             gui_color(155, 155, 155));
  1000.         gui_fill_rectangle(
  1001.             MMI_GX_PUZZLE_STATUS_POS_X,
  1002.             MMI_GX_PUZZLE_STATUS_POS_Y,
  1003.             MMI_GX_PUZZLE_STATUS_POS_X + MMI_GX_PUZZLE_STATUS_WIDTH - 1,
  1004.             MMI_GX_PUZZLE_STATUS_POS_Y + MMI_GX_PUZZLE_STATUS_HEIGHT - 1,
  1005.             MMI_GX_PUZZLE_BACKGROUND_COLOR);
  1006.         gui_show_image(
  1007.             MMI_GX_PUZZLE_TIME_POSITION_X,
  1008.             MMI_GX_PUZZLE_TIME_POSITION_Y,
  1009.             (PU8) GetImage(IMG_ID_GX_PUZZLE_BOX));
  1010.         gui_show_image(
  1011.             MMI_GX_PUZZLE_TIME_POSITION_X + 2,
  1012.             MMI_GX_PUZZLE_TIME_POSITION_Y - 8,
  1013.             (PU8) GetImage(IMG_ID_GX_PUZZLE_TIME));
  1014.         gui_show_image(
  1015.             MMI_GX_PUZZLE_STEP_POSITION_X,
  1016.             MMI_GX_PUZZLE_STEP_POSITION_Y,
  1017.             (PU8) GetImage(IMG_ID_GX_PUZZLE_BOX));
  1018.         gui_show_image(
  1019.             MMI_GX_PUZZLE_STEP_POSITION_X + 2,
  1020.             MMI_GX_PUZZLE_STEP_POSITION_Y - 8,
  1021.             (PU8) GetImage(IMG_ID_GX_PUZZLE_STEP));
  1022.         gui_show_image(66, 47, (PU8) GetImage(g_gx_puzzle_context.small_image));
  1023.         /* update TIME */
  1024.         tmp_tick = g_gx_puzzle_context.long_tick / 10;
  1025.         position_shift = MMI_GX_PUZZLE_TIME_POSITION_X + 0;
  1026.         do
  1027.         {
  1028.             image_id = IMG_ID_GX_PUZZLE_NUMBER_0 + (tmp_tick % 10);
  1029.             tmp_tick = tmp_tick / 10;
  1030.             gui_show_image(
  1031.                 MMI_GX_PUZZLE_TIME_POSITION_X + position_shift,
  1032.                 MMI_GX_PUZZLE_TIME_POSITION_Y + 2,
  1033.                 (PU8) GetImage(image_id));
  1034.             position_shift = position_shift - 4;
  1035.         } while (tmp_tick != 0);
  1036.         /* draw how many step user went */
  1037.         tmp_click = g_gx_puzzle_context.click;
  1038.         position_shift = MMI_GX_PUZZLE_TIME_POSITION_X + 0;
  1039.         do
  1040.         {
  1041.             image_id = IMG_ID_GX_PUZZLE_NUMBER_0 + (tmp_click % 10);
  1042.             tmp_click = tmp_click / 10;
  1043.             gui_show_image(
  1044.                 MMI_GX_PUZZLE_TIME_POSITION_X + position_shift,
  1045.                 MMI_GX_PUZZLE_TIME_POSITION_Y + 2 + 20,
  1046.                 (PU8) GetImage(image_id));
  1047.             position_shift = position_shift - 4;
  1048.         } while (tmp_click != 0);
  1049.     }
  1050. #endif 
  1051.     gui_BLT_double_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);
  1052. }
  1053. /*****************************************************************************
  1054.  * FUNCTION
  1055.  *  mmi_gx_puzzle_gameover
  1056.  * DESCRIPTION
  1057.  *  Gameover function
  1058.  * PARAMETERS
  1059.  *  void
  1060.  * RETURNS
  1061.  *  void
  1062.  *****************************************************************************/
  1063. void mmi_gx_puzzle_gameover(void)
  1064. {
  1065.     /*----------------------------------------------------------------*/
  1066.     /* Local Variables                                                */
  1067.     /*----------------------------------------------------------------*/
  1068.     /*----------------------------------------------------------------*/
  1069.     /* Code Body                                                      */
  1070.     /*----------------------------------------------------------------*/
  1071.     g_gx_puzzle_context.is_gameover = TRUE;
  1072.     g_gx_puzzle_context.is_new_game = TRUE;
  1073.     g_gx_puzzle_context.game_grade = g_gx_puzzle_context.long_tick / 10;
  1074.     /* call this function to draw gameover screen */
  1075.     mmi_gfx_entry_gameover_screen();
  1076. }
  1077. /*****************************************************************************
  1078.  * FUNCTION
  1079.  *  mmi_gx_puzzle_cyclic_timer
  1080.  * DESCRIPTION
  1081.  *  Timer trigger function - looping
  1082.  * PARAMETERS
  1083.  *  void
  1084.  * RETURNS
  1085.  *  void
  1086.  *****************************************************************************/
  1087. void mmi_gx_puzzle_cyclic_timer(void)
  1088. {
  1089.     /*----------------------------------------------------------------*/
  1090.     /* Local Variables                                                */
  1091.     /*----------------------------------------------------------------*/
  1092.     /*----------------------------------------------------------------*/
  1093.     /* Code Body                                                      */
  1094.     /*----------------------------------------------------------------*/
  1095.     g_gx_puzzle_context.long_tick++;
  1096.     if (g_gx_puzzle_context.long_tick > 6553600)
  1097.     {
  1098.         g_gx_puzzle_context.long_tick = 0;
  1099.     }
  1100.     gui_start_timer(g_gx_puzzle_context.timer_elapse, mmi_gx_puzzle_cyclic_timer);
  1101.     if (g_gx_puzzle_context.is_gameover == FALSE)
  1102.     {
  1103.         mmi_gx_puzzle_framemove();
  1104.         mmi_gx_puzzle_render();
  1105.     }
  1106. }
  1107. /*****************************************************************************
  1108.  * FUNCTION
  1109.  *  mmi_gx_puzzle_key_2_release
  1110.  * DESCRIPTION
  1111.  *  Key handling
  1112.  * PARAMETERS
  1113.  *  void
  1114.  * RETURNS
  1115.  *  void
  1116.  *****************************************************************************/
  1117. void mmi_gx_puzzle_key_2_release(void)
  1118. {
  1119.     /*----------------------------------------------------------------*/
  1120.     /* Local Variables                                                */
  1121.     /*----------------------------------------------------------------*/
  1122.     /*----------------------------------------------------------------*/
  1123.     /* Code Body                                                      */
  1124.     /*----------------------------------------------------------------*/
  1125.     g_gx_puzzle_context.destination_x = 0;
  1126.     g_gx_puzzle_context.destination_y = 1;
  1127.     mmi_gx_puzzle_move_and_show();
  1128. }
  1129. /*****************************************************************************
  1130.  * FUNCTION
  1131.  *  mmi_gx_puzzle_key_4_release
  1132.  * DESCRIPTION
  1133.  *  Key handling
  1134.  * PARAMETERS
  1135.  *  void
  1136.  * RETURNS
  1137.  *  void
  1138.  *****************************************************************************/
  1139. void mmi_gx_puzzle_key_4_release(void)
  1140. {
  1141.     /*----------------------------------------------------------------*/
  1142.     /* Local Variables                                                */
  1143.     /*----------------------------------------------------------------*/
  1144.     /*----------------------------------------------------------------*/
  1145.     /* Code Body                                                      */
  1146.     /*----------------------------------------------------------------*/
  1147.     g_gx_puzzle_context.destination_x = 1;
  1148.     g_gx_puzzle_context.destination_y = 0;
  1149.     mmi_gx_puzzle_move_and_show();
  1150. }
  1151. /*****************************************************************************
  1152.  * FUNCTION
  1153.  *  mmi_gx_puzzle_key_6_release
  1154.  * DESCRIPTION
  1155.  *  Key handling
  1156.  * PARAMETERS
  1157.  *  void
  1158.  * RETURNS
  1159.  *  void
  1160.  *****************************************************************************/
  1161. void mmi_gx_puzzle_key_6_release(void)
  1162. {
  1163.     /*----------------------------------------------------------------*/
  1164.     /* Local Variables                                                */
  1165.     /*----------------------------------------------------------------*/
  1166.     /*----------------------------------------------------------------*/
  1167.     /* Code Body                                                      */
  1168.     /*----------------------------------------------------------------*/
  1169.     g_gx_puzzle_context.destination_x = -1;
  1170.     g_gx_puzzle_context.destination_y = 0;
  1171.     mmi_gx_puzzle_move_and_show();
  1172. }
  1173. /*****************************************************************************
  1174.  * FUNCTION
  1175.  *  mmi_gx_puzzle_key_8_release
  1176.  * DESCRIPTION
  1177.  *  Key handling
  1178.  * PARAMETERS
  1179.  *  void
  1180.  * RETURNS
  1181.  *  void
  1182.  *****************************************************************************/
  1183. void mmi_gx_puzzle_key_8_release(void)
  1184. {
  1185.     /*----------------------------------------------------------------*/
  1186.     /* Local Variables                                                */
  1187.     /*----------------------------------------------------------------*/
  1188.     /*----------------------------------------------------------------*/
  1189.     /* Code Body                                                      */
  1190.     /*----------------------------------------------------------------*/
  1191.     g_gx_puzzle_context.destination_x = 0;
  1192.     g_gx_puzzle_context.destination_y = -1;
  1193.     mmi_gx_puzzle_move_and_show();
  1194. }
  1195. #ifdef __MMI_GAME_PUZZLE_128x128__
  1196. /*****************************************************************************
  1197.  * FUNCTION
  1198.  *  mmi_gx_puzzle_key_5_press
  1199.  * DESCRIPTION
  1200.  *  Key handling
  1201.  * PARAMETERS
  1202.  *  void
  1203.  * RETURNS
  1204.  *  void
  1205.  *****************************************************************************/
  1206. void mmi_gx_puzzle_key_5_press(void)
  1207. {
  1208.     /*----------------------------------------------------------------*/
  1209.     /* Local Variables                                                */
  1210.     /*----------------------------------------------------------------*/
  1211.     /*----------------------------------------------------------------*/
  1212.     /* Code Body                                                      */
  1213.     /*----------------------------------------------------------------*/
  1214.     g_gx_puzzle_context.is_show_status = TRUE;
  1215. }
  1216. /*****************************************************************************
  1217.  * FUNCTION
  1218.  *  mmi_gx_puzzle_key_5_release
  1219.  * DESCRIPTION
  1220.  *  Key handling
  1221.  * PARAMETERS
  1222.  *  void
  1223.  * RETURNS
  1224.  *  void
  1225.  *****************************************************************************/
  1226. void mmi_gx_puzzle_key_5_release(void)
  1227. {
  1228.     /*----------------------------------------------------------------*/
  1229.     /* Local Variables                                                */
  1230.     /*----------------------------------------------------------------*/
  1231.     /*----------------------------------------------------------------*/
  1232.     /* Code Body                                                      */
  1233.     /*----------------------------------------------------------------*/
  1234.     gui_push_clip();
  1235.     gui_set_clip(
  1236.         MMI_GX_PUZZLE_STATUS_POS_X,
  1237.         MMI_GX_PUZZLE_STATUS_POS_Y,
  1238.         MMI_GX_PUZZLE_STATUS_POS_X + MMI_GX_PUZZLE_STATUS_WIDTH - 1 + MMI_GX_PUZZLE_STATUS_SHADOW,
  1239.         MMI_GX_PUZZLE_STATUS_POS_Y + MMI_GX_PUZZLE_STATUS_HEIGHT - 1 + MMI_GX_PUZZLE_STATUS_SHADOW);
  1240.     g_gx_puzzle_context.is_show_status = FALSE;
  1241.     mmi_gx_puzzle_show_puzzle();
  1242.     mmi_gx_puzzle_draw_seperation_line();
  1243.     gui_pop_clip();
  1244.     gui_BLT_double_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);
  1245. }
  1246. #endif /* __MMI_GAME_PUZZLE_128x128__ */ 
  1247. /*****************************************************************************
  1248.  * FUNCTION
  1249.  *  mmi_gx_puzzle_move_and_show
  1250.  * DESCRIPTION
  1251.  *  Move the grid (in memory) and show the movement
  1252.  * PARAMETERS
  1253.  *  void
  1254.  * RETURNS
  1255.  *  void
  1256.  *****************************************************************************/
  1257. void mmi_gx_puzzle_move_and_show(void)
  1258. {
  1259.     /*----------------------------------------------------------------*/
  1260.     /* Local Variables                                                */
  1261.     /*----------------------------------------------------------------*/
  1262.     S16 tmp;
  1263.     U8 bound_size;
  1264. #ifdef __MMI_GAME_PUZZLE_128x128__
  1265.     MMI_ID_TYPE image_id;   /* store the last digit of tick */
  1266.     U8 position_shift;      /* the position offset of digit displayed */
  1267. #endif /* __MMI_GAME_PUZZLE_128x128__ */ 
  1268.     U8 i;
  1269.     U8 j;
  1270.     BOOL is_gameover;
  1271.     /*----------------------------------------------------------------*/
  1272.     /* Code Body                                                      */
  1273.     /*----------------------------------------------------------------*/
  1274. #ifdef __MMI_GAME_PUZZLE_128x128__
  1275.     /* when the status shows, no action taken */
  1276.     if (g_gx_puzzle_context.is_show_status != FALSE)
  1277.     {
  1278.         return;
  1279.     }
  1280. #endif /* __MMI_GAME_PUZZLE_128x128__ */ 
  1281.     /* play moving sound */
  1282. #ifdef __MMI_GAME_MULTICHANNEL_SOUND__
  1283.     GFX_STOP_SOUND_EFFECTS_MIDI(g_gx_puzzle_context.move_midi);
  1284.     GFX_PLAY_SOUND_EFFECTS_MIDI(g_gx_puzzle_context.move_midi);
  1285. #else /* __MMI_GAME_MULTICHANNEL_SOUND__ */ 
  1286.     GFX_PLAY_AUDIO_DVI(PuzzleMove_dvi, PUZZLEMOVE_DVI, DEVICE_AUDIO_PLAY_ONCE);
  1287. #endif /* __MMI_GAME_MULTICHANNEL_SOUND__ */ 
  1288.     bound_size = g_gx_puzzle_context.game_level + 3;
  1289.     /* check if the movement is valid */
  1290.     if (g_gx_puzzle_context.null_grid.x + g_gx_puzzle_context.destination_x < 0 ||
  1291.         g_gx_puzzle_context.null_grid.x + g_gx_puzzle_context.destination_x >= bound_size ||
  1292.         g_gx_puzzle_context.null_grid.y + g_gx_puzzle_context.destination_y < 0 ||
  1293.         g_gx_puzzle_context.null_grid.y + g_gx_puzzle_context.destination_y >= bound_size)
  1294.     {
  1295.         return;
  1296.     }
  1297.     /* update the numbers user clicks */
  1298.     g_gx_puzzle_context.click = g_gx_puzzle_context.click + 1;
  1299.     if (g_gx_puzzle_context.click > 65534)
  1300.     {
  1301.         g_gx_puzzle_context.click = 0;
  1302.     }
  1303. #if defined(__MMI_GAME_PUZZLE_128x160__) || defined(__MMI_GAME_PUZZLE_176x220__) || defined(__MMI_GAME_PUZZLE_240x320__)
  1304.     mmi_gx_puzzle_draw_step();
  1305. #endif 
  1306.     /* switch the null grid and its neighboring grid and draw them */
  1307.     tmp = g_gx_puzzle_context.puzzle[g_gx_puzzle_context.null_grid.y][g_gx_puzzle_context.null_grid.x];
  1308.     g_gx_puzzle_context.puzzle[g_gx_puzzle_context.null_grid.y][g_gx_puzzle_context.null_grid.x] =
  1309.         g_gx_puzzle_context.puzzle[g_gx_puzzle_context.null_grid.y +
  1310.                                    g_gx_puzzle_context.destination_y][g_gx_puzzle_context.null_grid.x +
  1311.                                                                       g_gx_puzzle_context.destination_x];
  1312.     mmi_gx_puzzle_my_show_image(
  1313.         MMI_GX_PUZZLE_ORIGIN_X + MMI_GX_PUZZLE_SIZE / bound_size * g_gx_puzzle_context.null_grid.x,
  1314.         MMI_GX_PUZZLE_ORIGIN_Y + MMI_GX_PUZZLE_SIZE / bound_size * g_gx_puzzle_context.null_grid.y,
  1315.         g_gx_puzzle_context.puzzle[g_gx_puzzle_context.null_grid.y + g_gx_puzzle_context.destination_y][g_gx_puzzle_context. null_grid.x + g_gx_puzzle_context. destination_x]);
  1316.     g_gx_puzzle_context.puzzle[g_gx_puzzle_context.null_grid.y +
  1317.                                g_gx_puzzle_context.destination_y][g_gx_puzzle_context.null_grid.x +
  1318.                                                                   g_gx_puzzle_context.destination_x] = tmp;
  1319.     mmi_gx_puzzle_my_show_image(
  1320.         MMI_GX_PUZZLE_ORIGIN_X + MMI_GX_PUZZLE_SIZE / bound_size * (g_gx_puzzle_context.null_grid.x + g_gx_puzzle_context.destination_x),
  1321.         MMI_GX_PUZZLE_ORIGIN_Y + MMI_GX_PUZZLE_SIZE / bound_size * (g_gx_puzzle_context.null_grid.y + g_gx_puzzle_context.destination_y),
  1322.         tmp);
  1323.     g_gx_puzzle_context.null_grid.x = g_gx_puzzle_context.null_grid.x + g_gx_puzzle_context.destination_x;
  1324.     g_gx_puzzle_context.null_grid.y = g_gx_puzzle_context.null_grid.y + g_gx_puzzle_context.destination_y;
  1325.     mmi_gx_puzzle_draw_seperation_line();
  1326.     gui_BLT_double_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);
  1327.     /* check if gameover */
  1328.     tmp = 1;
  1329.     is_gameover = TRUE;
  1330.     for (i = 0; i < bound_size; i++)
  1331.     {
  1332.         for (j = 0; j < bound_size; j++)
  1333.         {
  1334.             if (g_gx_puzzle_context.puzzle[i][j] != tmp)
  1335.             {
  1336.                 is_gameover = FALSE;
  1337.                 break;
  1338.             }
  1339.             tmp = tmp + 1;
  1340.         }
  1341.         if (is_gameover != TRUE)
  1342.         {
  1343.             break;
  1344.         }
  1345.     }
  1346.     if (is_gameover != FALSE)
  1347.     {
  1348.         mmi_gx_puzzle_gameover();
  1349.     }
  1350. }
  1351. #if defined(__MMI_GAME_PUZZLE_128x160__) || defined(__MMI_GAME_PUZZLE_176x220__) || defined(__MMI_GAME_PUZZLE_240x320__)
  1352. /*****************************************************************************
  1353.  * FUNCTION
  1354.  *  mmi_gx_puzzle_draw_step
  1355.  * DESCRIPTION
  1356.  *  Draw steps
  1357.  * PARAMETERS
  1358.  *  void
  1359.  * RETURNS
  1360.  *  void
  1361.  *****************************************************************************/
  1362. void mmi_gx_puzzle_draw_step(void)
  1363. {
  1364.     /*----------------------------------------------------------------*/
  1365.     /* Local Variables                                                */
  1366.     /*----------------------------------------------------------------*/
  1367.     U16 tmp_click;
  1368.     U8 position_shift;      /* the position offset of digit displayed */
  1369.     MMI_ID_TYPE image_id;   /* store the last digit of tick */
  1370.     /*----------------------------------------------------------------*/
  1371.     /* Code Body                                                      */
  1372.     /*----------------------------------------------------------------*/
  1373.     tmp_click = g_gx_puzzle_context.click;
  1374. #if defined(__MMI_GAME_PUZZLE_128x160__)
  1375.     position_shift = MMI_GX_PUZZLE_STEP_POSITION_X + 15;
  1376. #elif defined(__MMI_GAME_PUZZLE_176x220__)|| defined(__MMI_GAME_PUZZLE_240x320__)
  1377.     position_shift = MMI_GX_PUZZLE_STEP_POSITION_X + 17;
  1378. #endif 
  1379.     do
  1380.     {
  1381.         image_id = IMG_ID_GX_PUZZLE_NUMBER_0 + (tmp_click % 10);
  1382.         tmp_click = tmp_click / 10;
  1383.         gui_show_image(MMI_GX_PUZZLE_TIME_POSITION_X + position_shift,
  1384.     #if defined(__MMI_GAME_PUZZLE_240x320__)
  1385.                        MMI_GX_PUZZLE_TIME_POSITION_Y + 5 + 128,
  1386.     #else 
  1387.                        MMI_GX_PUZZLE_TIME_POSITION_Y + 2 + 128,
  1388.     #endif 
  1389.                        (PU8) GetImage(image_id));
  1390.     #if defined(__MMI_GAME_PUZZLE_176x220__) || defined(__MMI_GAME_PUZZLE_240x320__)
  1391.         position_shift = position_shift - MMI_GX_PUZZLE_DIGIT_WIDTH - 1;
  1392.     #else 
  1393.         position_shift = position_shift - MMI_GX_PUZZLE_DIGIT_WIDTH;
  1394.     #endif 
  1395.     } while (tmp_click != 0);
  1396. }
  1397. #endif /* defined(__MMI_GAME_PUZZLE_128x160__) || defined(__MMI_GAME_PUZZLE_176x220__) || defined(__MMI_GAME_PUZZLE_240x320__) */ 
  1398. /*****************************************************************************
  1399.  * FUNCTION
  1400.  *  mmi_gx_puzzle_my_show_image
  1401.  * DESCRIPTION
  1402.  *  Calculate which part of the image should be shown
  1403.  * PARAMETERS
  1404.  *  x                   [IN]        X position of the shown image
  1405.  *  y                   [IN]        Y position of the shown image
  1406.  *  input_image_id      [IN]        
  1407.  *  i  PU8 for the shown image(?)
  1408.  * RETURNS
  1409.  *  void
  1410.  *****************************************************************************/
  1411. void mmi_gx_puzzle_my_show_image(long x, long y, S16 input_image_id)
  1412. {
  1413.     /*----------------------------------------------------------------*/
  1414.     /* Local Variables                                                */
  1415.     /*----------------------------------------------------------------*/
  1416.     /*
  1417.      * U8                grid_size;
  1418.      * S16               output_x;
  1419.      * S16               output_y;
  1420.      */
  1421.     MMI_ID_TYPE null_image;
  1422.     MMI_ID_TYPE image_id;
  1423.     /*----------------------------------------------------------------*/
  1424.     /* Code Body                                                      */
  1425.     /*----------------------------------------------------------------*/
  1426.     /* initialize */
  1427.     null_image = 0;
  1428.     image_id = 0;
  1429.     if (input_image_id == (g_gx_puzzle_context.game_level + 3) * (g_gx_puzzle_context.game_level + 3))
  1430.     {
  1431.         switch (g_gx_puzzle_context.game_level)
  1432.         {
  1433.             case MMI_GX_PUZZLE_EASY:
  1434.                 null_image = IMG_ID_GX_PUZZLE_3_SPACE;
  1435.                 break;
  1436.             case MMI_GX_PUZZLE_NORMAL:
  1437.                 null_image = IMG_ID_GX_PUZZLE_4_SPACE;
  1438.                 break;
  1439.             case MMI_GX_PUZZLE_HARD:
  1440.                 null_image = IMG_ID_GX_PUZZLE_5_SPACE;
  1441.                 break;
  1442.         }
  1443.         gui_show_image(x, y, (PU8) GetImage(null_image));
  1444.     }
  1445.     else
  1446.     {
  1447.         /*
  1448.          * Old flash memory saving image drawing method
  1449.          * grid_size = MMI_GX_PUZZLE_SIZE / (g_gx_puzzle_context.game_level + 3);
  1450.          * output_x = x - grid_size * ((input_image_id - 1) % (g_gx_puzzle_context.game_level + 3));
  1451.          * output_y = y - grid_size * ((input_image_id - 1) / (g_gx_puzzle_context.game_level + 3));
  1452.          * 
  1453.          * gui_push_clip();
  1454.          * gui_set_clip(x, y, x + grid_size - 1, y + grid_size - 1);
  1455.          * gui_show_image(output_x, output_y, (PU8)GetImage(g_gx_puzzle_context.image));
  1456.          * gui_pop_clip();
  1457.          */
  1458.         switch (g_gx_puzzle_context.game_level)
  1459.         {
  1460.             case MMI_GX_PUZZLE_EASY:
  1461.                 image_id = IMG_ID_GX_PUZZLE_3_1;
  1462.                 break;
  1463.             case MMI_GX_PUZZLE_NORMAL:
  1464.                 image_id = IMG_ID_GX_PUZZLE_4_1;
  1465.                 break;
  1466.             case MMI_GX_PUZZLE_HARD:
  1467.                 image_id = IMG_ID_GX_PUZZLE_5_1;
  1468.                 break;
  1469.         }
  1470.         gui_show_image(x, y, (PU8) GetImage((U16) (image_id - 1 + input_image_id)));
  1471.     }
  1472. }
  1473. /* MTK Leo add */
  1474. #ifdef __MMI_TOUCH_SCREEN__
  1475. typedef enum
  1476. {
  1477.     PUZZLE_MOVE_NONE = 0,
  1478.     PUZZLE_MOVE_RIGHT = 5,
  1479.     PUZZLE_MOVE_DOWN = 4,
  1480.     PUZZLE_MOVE_LEFT = 7,
  1481.     PUZZLE_MOVE_UP = 8
  1482. } PUZZLE_MOVEMENT;
  1483. /*****************************************************************************
  1484.  * FUNCTION
  1485.  *  mmi_puzzle_is_puzzle_moveable
  1486.  * DESCRIPTION
  1487.  *  
  1488.  * PARAMETERS
  1489.  *  coord_x     [IN]        
  1490.  *  coord_y     [IN]        
  1491.  * RETURNS
  1492.  *  
  1493.  *****************************************************************************/
  1494. PUZZLE_MOVEMENT mmi_puzzle_is_puzzle_moveable(S32 coord_x, S32 coord_y)
  1495. {
  1496.     /*----------------------------------------------------------------*/
  1497.     /* Local Variables                                                */
  1498.     /*----------------------------------------------------------------*/
  1499.     U8 grid_number;
  1500.     S16 diff = abs(coord_x - g_gx_puzzle_context.null_grid.x) + abs(coord_y - g_gx_puzzle_context.null_grid.y);
  1501.     /*----------------------------------------------------------------*/
  1502.     /* Code Body                                                      */
  1503.     /*----------------------------------------------------------------*/
  1504.     grid_number = g_gx_puzzle_context.game_level + 3;
  1505.     if (coord_x < 0 || coord_x >= grid_number || coord_y < 0 || coord_y >= grid_number)
  1506.     {
  1507.         return PUZZLE_MOVE_NONE;
  1508.     }
  1509.     else if (diff == 1)
  1510.     {
  1511.         return ((coord_x - g_gx_puzzle_context.null_grid.x + 2) + 2 * (coord_y - g_gx_puzzle_context.null_grid.y + 2));
  1512.     }
  1513.     else
  1514.     {
  1515.         return PUZZLE_MOVE_NONE;
  1516.     }
  1517. }
  1518. #endif /* __MMI_TOUCH_SCREEN__ */ 
  1519. /*****************************************************************************
  1520.  * FUNCTION
  1521.  *  mmi_puzzle_pen_up_hdlr
  1522.  * DESCRIPTION
  1523.  *  pen up event handler
  1524.  * PARAMETERS
  1525.  *  pos     [IN]        Position of pen
  1526.  * RETURNS
  1527.  *  void
  1528.  *****************************************************************************/
  1529. #ifdef __MMI_TOUCH_SCREEN__
  1530. void mmi_puzzle_pen_up_hdlr(mmi_pen_point_struct pos)
  1531. {
  1532.     /*----------------------------------------------------------------*/
  1533.     /* Local Variables                                                */
  1534.     /*----------------------------------------------------------------*/
  1535.     S32 pos_x = pos.x, pos_y = pos.y;
  1536.     /*----------------------------------------------------------------*/
  1537.     /* Code Body                                                      */
  1538.     /*----------------------------------------------------------------*/
  1539.     g_gx_puzzle_context.softkey_state = 2;
  1540. #if defined(__MMI_TOUCH_SCREEN__) && defined(__MMI_GAME_PUZZLE_240x320__)       /* || defined(__MMI_GAME_PUZZLE_176x220__)) */
  1541.     if (pos_x > MMI_GX_PUZZLE_SOFTKEY_X && pos_x < MMI_GX_PUZZLE_SOFTKEY_X + MMI_GX_PUZZLE_SOFTKEY_WIDTH
  1542.         && pos_y > MMI_GX_PUZZLE_SOFTKEY_Y && pos_y < MMI_GX_PUZZLE_SOFTKEY_Y + MMI_GX_PUZZLE_SOFTKEY_HEIGHT)
  1543.     {
  1544.         GoBackHistory();
  1545.     }
  1546. #endif /* defined(__MMI_TOUCH_SCREEN__) && defined(__MMI_GAME_PUZZLE_240x320__) */ /* __MMI_TOUCH_SCREEN__ && ( __MMI_GAME_PUZZLE_240x320__ || __MMI_GAME_PUZZLE_176x220__ ) */
  1547. }
  1548. #endif /* __MMI_TOUCH_SCREEN__ */ 
  1549. #ifdef __MMI_TOUCH_SCREEN__
  1550. /*****************************************************************************
  1551.  * FUNCTION
  1552.  *  mmi_puzzle_pen_down_hdlr
  1553.  * DESCRIPTION
  1554.  *  
  1555.  * PARAMETERS
  1556.  *  pos     [IN]        
  1557.  * RETURNS
  1558.  *  void
  1559.  *****************************************************************************/
  1560. void mmi_puzzle_pen_down_hdlr(mmi_pen_point_struct pos)
  1561. {
  1562.     /*----------------------------------------------------------------*/
  1563.     /* Local Variables                                                */
  1564.     /*----------------------------------------------------------------*/
  1565.     U8 grid_number = g_gx_puzzle_context.game_level + 3;
  1566.     U16 puzzle_size = MMI_GX_PUZZLE_SIZE / grid_number;
  1567.     /* U8   i=0,j=0; */
  1568.     S32 pos_x = pos.x, pos_y = pos.y;   /* pen location */
  1569.     S32 coord_x = 0, coord_y = 0;       /* puzzle coordinate index */
  1570.     /*----------------------------------------------------------------*/
  1571.     /* Code Body                                                      */
  1572.     /*----------------------------------------------------------------*/
  1573. #if defined(__MMI_TOUCH_SCREEN__) && defined(__MMI_GAME_PUZZLE_240x320__)       /* || defined(__MMI_GAME_PUZZLE_176x220__)) */
  1574.     if (pos_x > MMI_GX_PUZZLE_SOFTKEY_X && pos_x < MMI_GX_PUZZLE_SOFTKEY_X + MMI_GX_PUZZLE_SOFTKEY_WIDTH
  1575.         && pos_y > MMI_GX_PUZZLE_SOFTKEY_Y && pos_y < MMI_GX_PUZZLE_SOFTKEY_Y + MMI_GX_PUZZLE_SOFTKEY_HEIGHT)
  1576.     {
  1577.         g_gx_puzzle_context.softkey_state = 1;
  1578.     }
  1579. #endif /* defined(__MMI_TOUCH_SCREEN__) && defined(__MMI_GAME_PUZZLE_240x320__) */ /* __MMI_TOUCH_SCREEN__ && ( __MMI_GAME_PUZZLE_240x320__ || __MMI_GAME_PUZZLE_176x220__ ) */
  1580.     coord_x = (pos_x - MMI_GX_PUZZLE_ORIGIN_X) / puzzle_size;
  1581.     coord_y = (pos_y - MMI_GX_PUZZLE_ORIGIN_Y) / puzzle_size;
  1582.     switch (mmi_puzzle_is_puzzle_moveable(coord_x, coord_y))
  1583.     {
  1584.         case PUZZLE_MOVE_UP:
  1585.             mmi_gx_puzzle_key_2_release();
  1586.             break;
  1587.         case PUZZLE_MOVE_LEFT:
  1588.             mmi_gx_puzzle_key_4_release();
  1589.             break;
  1590.         case PUZZLE_MOVE_RIGHT:
  1591.             mmi_gx_puzzle_key_6_release();
  1592.             break;
  1593.         case PUZZLE_MOVE_DOWN:
  1594.             mmi_gx_puzzle_key_8_release();
  1595.             break;
  1596.     }
  1597. }
  1598. #endif /* __MMI_TOUCH_SCREEN__ */ 
  1599. #endif /* IS_PUZZLE_BASE_GAME */